kpmpgsmkii/client/src/gui/label.c

142 lines
3.6 KiB
C

/*
* Kangaroo Punch MultiPlayer Game Server Mark II
* Copyright (C) 2020-2021 Scott Duensing
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "label.h"
static void labelMouseEvent(WidgetT *widget, MouseT *mouse, uint16_t x, uint16_t y, uint8_t event);
static void labelPaint(WidgetT *widget, RectT pos);
void labelDel(WidgetT **widget) {
LabelT *l = (LabelT *)*widget;
if (l->title) free(l->title);
free(l);
l = NULL;
}
WidgetT *labelInit(WidgetT *widget, char *title) {
LabelT *l = (LabelT *)widget;
l->base.delMethod = labelDel;
l->base.paintMethod = labelPaint;
l->base.mouseEventMethod = labelMouseEvent;
l->title = NULL;
l->background = _guiColor[COLOR_WINDOW_BACKGROUND];
l->foreground = _guiColor[COLOR_LABEL_TEXT_INACTIVE];
l->clicked = NULL;
labelSetTitle(l, title);
return widget;
}
static void labelMouseEvent(WidgetT *widget, MouseT *mouse, uint16_t x, uint16_t y, uint8_t event) {
LabelT *l = (LabelT *)widget;
(void)x;
(void)y;
(void)mouse;
// Label pressed?
if (event == MOUSE_EVENT_LEFT_HOLD) {
if (!GUI_GET_FLAG(widget, WIDGET_FLAG_ACTIVE)) {
GUI_SET_FLAG(widget, WIDGET_FLAG_ACTIVE);
GUI_SET_FLAG(widget, WIDGET_FLAG_DIRTY);
}
} else {
if (GUI_GET_FLAG(widget, WIDGET_FLAG_ACTIVE)) {
GUI_CLEAR_FLAG(widget, WIDGET_FLAG_ACTIVE);
GUI_SET_FLAG(widget, WIDGET_FLAG_DIRTY);
}
}
// Fire callback on mouse up.
if (event == MOUSE_EVENT_LEFT_UP) {
if (l->clicked) l->clicked(widget);
}
}
LabelT *labelNew(uint16_t x, uint16_t y, char *title) {
LabelT *label = (LabelT *)malloc(sizeof(LabelT));
WidgetT *widget = NULL;
uint16_t h = fontHeightGet(_guiFont);
if (!label) return NULL;
// Width is set in Init
widget = widgetInit(W(label), MAGIC_LABEL, x, y, 0, h, 0, 0, 0, 0);
if (!widget) {
free(label);
return NULL;
}
label = (LabelT *)labelInit((WidgetT *)label, title);
return label;
}
static void labelPaint(WidgetT *widget, RectT pos) {
LabelT *l = (LabelT *)widget;
ColorT text = GUI_GET_FLAG(widget, WIDGET_FLAG_ACTIVE) ? l->active : l->foreground;
if (GUI_GET_FLAG(widget, WIDGET_FLAG_DIRTY)) {
// Draw title.
fontRender(_guiFont, l->title, text, l->background, pos.x, pos.y);
GUI_CLEAR_FLAG(widget, WIDGET_FLAG_DIRTY);
}
}
void labelSetActiveColor(LabelT *label, ColorT color) {
label->active = color;
GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}
void labelSetBackgroundColor(LabelT *label, ColorT color) {
label->background = color;
GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}
void labelSetClickHandler(LabelT *label, widgetCallback callback) {
label->clicked = callback;
}
void labelSetForegroundColor(LabelT *label, ColorT color) {
label->foreground = color;
GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}
void labelSetTitle(LabelT *label, char *title) {
if (label->title) free(label->title);
label->title = strdup(title);
label->base.pos.w = (strlen(title) * fontWidthGet(_guiFont));
GUI_SET_FLAG((WidgetT *)label, WIDGET_FLAG_DIRTY);
}