label: replace giant if/else block in draw, just change the draw handler function when MwSetSevenSegment is called
All checks were successful
pyrite-dev/milsko/pipeline/head This commit looks good

This commit is contained in:
IoIxD 2026-05-13 00:05:06 -07:00
commit b258f1e05d

View file

@ -122,9 +122,8 @@ static void draw_h(MwWidget handle, unsigned char* raw, int x, int y, int stride
} }
} }
static void draw(MwWidget handle) { static void draw_seven_segment(MwWidget handle) {
MwRect r; MwRect r;
MwPoint p;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground)); MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground)); MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
MwLLColor shadow = MwLightenColor(handle, base, MwDefaultShadow, MwDefaultShadow, MwDefaultShadow); MwLLColor shadow = MwLightenColor(handle, base, MwDefaultShadow, MwDefaultShadow, MwDefaultShadow);
@ -134,6 +133,10 @@ static void draw(MwWidget handle) {
MwLabel lab = handle->internal; MwLabel lab = handle->internal;
int l_one = MwGetInteger(handle, MwNlength) - (MwGetInteger(handle, MwNlength) % 2); int l_one = MwGetInteger(handle, MwNlength) - (MwGetInteger(handle, MwNlength) % 2);
int s_one = (l_one * 3 / 4) - ((l_one * 3 / 4) % 2) + 1; int s_one = (l_one * 3 / 4) - ((l_one * 3 / 4) % 2) + 1;
MwLLPixmap px;
unsigned char* raw;
int w = 0, h = s_one * 3 + l_one * 2, i;
int x = 0;
if(str == NULL) str = ""; if(str == NULL) str = "";
@ -146,11 +149,6 @@ static void draw(MwWidget handle) {
if(bgpx != NULL) MwLLDrawPixmap(handle->lowlevel, &r, bgpx); if(bgpx != NULL) MwLLDrawPixmap(handle->lowlevel, &r, bgpx);
align = MwGetInteger(handle, MwNalignment); align = MwGetInteger(handle, MwNalignment);
if(MwGetInteger(handle, MwNsevenSegment)) {
MwLLPixmap px;
unsigned char* raw;
int w = 0, h = s_one * 3 + l_one * 2, i;
int x = 0;
/* so - this mode cannot do unicode. /* so - this mode cannot do unicode.
* but you wouldn't show unicode on 7 segment anyways * but you wouldn't show unicode on 7 segment anyways
@ -312,7 +310,33 @@ static void draw(MwWidget handle) {
free(raw); free(raw);
MwLLDestroyPixmap(px); MwLLDestroyPixmap(px);
} else { }
static void draw_normal(MwWidget handle) {
MwRect r;
MwPoint p;
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
MwLLColor shadow = MwLightenColor(handle, base, MwDefaultShadow, MwDefaultShadow, MwDefaultShadow);
int align;
const char* str = MwGetText(handle, MwNtext);
MwLLPixmap bgpx = MwGetVoid(handle, MwNbackgroundPixmap);
MwLabel lab = handle->internal;
int l_one = MwGetInteger(handle, MwNlength) - (MwGetInteger(handle, MwNlength) % 2);
int s_one = (l_one * 3 / 4) - ((l_one * 3 / 4) % 2) + 1;
if(str == NULL) str = "";
r.x = 0;
r.y = 0;
r.width = MwGetInteger(handle, MwNwidth);
r.height = MwGetInteger(handle, MwNheight);
MwDrawRect(handle, &r, base);
if(bgpx != NULL) MwLLDrawPixmap(handle->lowlevel, &r, bgpx);
align = MwGetInteger(handle, MwNalignment);
r.width -= MwGetInteger(handle, MwNleftPadding); r.width -= MwGetInteger(handle, MwNleftPadding);
if(align == MwALIGNMENT_CENTER) { if(align == MwALIGNMENT_CENTER) {
@ -333,7 +357,6 @@ static void draw(MwWidget handle) {
p.x -= 1; p.x -= 1;
p.y -= 1; p.y -= 1;
MwDrawText(handle, NULL, &p, str, MwALIGNMENT_CENTER, text); MwDrawText(handle, NULL, &p, str, MwALIGNMENT_CENTER, text);
}
MwLLFreeColor(shadow); MwLLFreeColor(shadow);
MwLLFreeColor(text); MwLLFreeColor(text);
@ -348,6 +371,8 @@ static void mwLabelSetSevenSegmentImpl(MwWidget handle, int index, unsigned char
MwLabel lab = handle->internal; MwLabel lab = handle->internal;
hmput(lab->segment, index, data); hmput(lab->segment, index, data);
handle->widget_class->draw = draw_seven_segment;
} }
static void func_handler(MwWidget handle, const char* name, void* out, va_list va) { static void func_handler(MwWidget handle, const char* name, void* out, va_list va) {
@ -364,7 +389,7 @@ static void func_handler(MwWidget handle, const char* name, void* out, va_list v
MwClassRec MwLabelClassRec = { MwClassRec MwLabelClassRec = {
wcreate, /* create */ wcreate, /* create */
destroy, /* destroy */ destroy, /* destroy */
draw, /* draw */ draw_normal, /* draw */
NULL, /* click */ NULL, /* click */
NULL, /* parent_resize */ NULL, /* parent_resize */
prop_change, /* prop_change */ prop_change, /* prop_change */