DVX_GUI/dvx/widgets/widgetImageButton.c

134 lines
4 KiB
C

// widgetImageButton.c — Image button widget (button with image instead of text)
#include "widgetInternal.h"
// ============================================================
// wgtImageButton
// ============================================================
WidgetT *wgtImageButton(WidgetT *parent, uint8_t *data, int32_t w, int32_t h, int32_t pitch) {
if (!parent || !data || w <= 0 || h <= 0) {
return NULL;
}
WidgetT *wgt = widgetAlloc(parent, WidgetImageButtonE);
if (wgt) {
wgt->as.imageButton.data = data;
wgt->as.imageButton.imgW = w;
wgt->as.imageButton.imgH = h;
wgt->as.imageButton.imgPitch = pitch;
wgt->as.imageButton.pressed = false;
} else {
free(data);
}
return wgt;
}
// ============================================================
// wgtImageButtonSetData
// ============================================================
void wgtImageButtonSetData(WidgetT *w, uint8_t *data, int32_t imgW, int32_t imgH, int32_t pitch) {
if (!w || w->type != WidgetImageButtonE) {
return;
}
free(w->as.imageButton.data);
w->as.imageButton.data = data;
w->as.imageButton.imgW = imgW;
w->as.imageButton.imgH = imgH;
w->as.imageButton.imgPitch = pitch;
}
// ============================================================
// widgetImageButtonDestroy
// ============================================================
void widgetImageButtonDestroy(WidgetT *w) {
free(w->as.imageButton.data);
}
// ============================================================
// widgetImageButtonCalcMinSize
// ============================================================
void widgetImageButtonCalcMinSize(WidgetT *w, const BitmapFontT *font) {
(void)font;
// Bevel border only, no extra padding
w->calcMinW = w->as.imageButton.imgW + 4;
w->calcMinH = w->as.imageButton.imgH + 4;
}
// ============================================================
// widgetImageButtonOnKey
// ============================================================
void widgetImageButtonOnKey(WidgetT *w, int32_t key, int32_t mod) {
(void)mod;
if (key == ' ' || key == 0x0D) {
w->as.imageButton.pressed = true;
sKeyPressedBtn = w;
wgtInvalidate(w);
}
}
// ============================================================
// widgetImageButtonOnMouse
// ============================================================
void widgetImageButtonOnMouse(WidgetT *w, WidgetT *root, int32_t vx, int32_t vy) {
(void)root;
(void)vx;
(void)vy;
w->focused = true;
w->as.imageButton.pressed = true;
sPressedButton = w;
}
// ============================================================
// widgetImageButtonPaint
// ============================================================
void widgetImageButtonPaint(WidgetT *w, DisplayT *d, const BlitOpsT *ops, const BitmapFontT *font, const ColorSchemeT *colors) {
(void)font;
uint32_t bgFace = w->bgColor ? w->bgColor : colors->buttonFace;
BevelStyleT bevel;
bevel.highlight = w->as.imageButton.pressed ? colors->windowShadow : colors->windowHighlight;
bevel.shadow = w->as.imageButton.pressed ? colors->windowHighlight : colors->windowShadow;
bevel.face = bgFace;
bevel.width = 2;
drawBevel(d, ops, w->x, w->y, w->w, w->h, &bevel);
if (w->as.imageButton.data) {
int32_t imgX = w->x + (w->w - w->as.imageButton.imgW) / 2;
int32_t imgY = w->y + (w->h - w->as.imageButton.imgH) / 2;
if (w->as.imageButton.pressed) {
imgX++;
imgY++;
}
rectCopy(d, ops, imgX, imgY,
w->as.imageButton.data, w->as.imageButton.imgPitch,
0, 0, w->as.imageButton.imgW, w->as.imageButton.imgH);
}
if (w->focused) {
uint32_t fg = w->fgColor ? w->fgColor : colors->contentFg;
int32_t off = w->as.imageButton.pressed ? 1 : 0;
drawFocusRect(d, ops, w->x + 3 + off, w->y + 3 + off, w->w - 6, w->h - 6, fg);
}
}