Launcher: Tidy up widget/backend code a little bit

This commit is contained in:
UnknownShadow200 2022-04-02 21:36:38 +11:00
parent e02760dfb1
commit a8d8fb9411
4 changed files with 37 additions and 23 deletions

View File

@ -135,6 +135,17 @@ void LBackend_DrawButton(struct LButton* w) {
/*########################################################################################################################* /*########################################################################################################################*
*-----------------------------------------------------CheckboxWidget------------------------------------------------------* *-----------------------------------------------------CheckboxWidget------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
#define CB_SIZE 24
#define CB_OFFSET 8
void LBackend_InitCheckbox(struct LCheckbox* w) {
struct DrawTextArgs args;
DrawTextArgs_Make(&args, &w->text, &Launcher_TextFont, true);
w->width = Display_ScaleX(CB_SIZE + CB_OFFSET) + Drawer2D_TextWidth(&args);
w->height = Display_ScaleY(CB_SIZE);
}
/* Based off checkbox from original ClassiCube Launcher */ /* Based off checkbox from original ClassiCube Launcher */
static const cc_uint8 checkbox_indices[] = { static const cc_uint8 checkbox_indices[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
@ -210,7 +221,7 @@ void LBackend_DrawCheckbox(struct LCheckbox* w) {
} }
DrawBoxBounds(black, w->x, w->y, width, height); DrawBoxBounds(black, w->x, w->y, width, height);
DrawTextArgs_Make(&args, &w->text, w->font, true); DrawTextArgs_Make(&args, &w->text, &Launcher_TextFont, true);
x = w->x + Display_ScaleX(CB_SIZE + CB_OFFSET); x = w->x + Display_ScaleX(CB_SIZE + CB_OFFSET);
y = w->y + (height - Drawer2D_TextHeight(&args)) / 2; y = w->y + (height - Drawer2D_TextHeight(&args)) / 2;
Drawer2D_DrawText(&Launcher_Framebuffer, &args, x, y); Drawer2D_DrawText(&Launcher_Framebuffer, &args, x, y);
@ -306,6 +317,14 @@ void LBackend_DrawInput(struct LInput* w, const cc_string* text) {
/*########################################################################################################################* /*########################################################################################################################*
*------------------------------------------------------LabelWidget--------------------------------------------------------* *------------------------------------------------------LabelWidget--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void LBackend_UpdateLabel(struct LLabel* w) {
struct DrawTextArgs args;
DrawTextArgs_Make(&args, &w->text, w->font, true);
w->width = Drawer2D_TextWidth(&args);
w->height = Drawer2D_TextHeight(&args);
}
void LBackend_DrawLabel(struct LLabel* w) { void LBackend_DrawLabel(struct LLabel* w) {
struct DrawTextArgs args; struct DrawTextArgs args;
DrawTextArgs_Make(&args, &w->text, w->font, true); DrawTextArgs_Make(&args, &w->text, w->font, true);
@ -359,12 +378,13 @@ static void LSlider_DrawBox(struct LSlider* w) {
w->width - xBorder2, halfHeight); w->width - xBorder2, halfHeight);
} }
#define LSLIDER_MAXVALUE 100
void LBackend_DrawSlider(struct LSlider* w) { void LBackend_DrawSlider(struct LSlider* w) {
int curWidth; int curWidth;
LSlider_DrawBoxBounds(w); LSlider_DrawBoxBounds(w);
LSlider_DrawBox(w); LSlider_DrawBox(w);
curWidth = (int)((w->width - xBorder2) * w->value / w->maxValue); curWidth = (int)((w->width - xBorder2) * w->value / LSLIDER_MAXVALUE);
Drawer2D_Clear(&Launcher_Framebuffer, w->color, Drawer2D_Clear(&Launcher_Framebuffer, w->color,
w->x + xBorder, w->y + yBorder, w->x + xBorder, w->y + yBorder,
curWidth, w->height - yBorder2); curWidth, w->height - yBorder2);

View File

@ -12,14 +12,19 @@ struct LLabel;
struct LLine; struct LLine;
struct LSlider; struct LSlider;
#define CB_SIZE 24
#define CB_OFFSET 8
void LBackend_CalcOffsets(void); void LBackend_CalcOffsets(void);
void LBackend_DrawButton(struct LButton* w); void LBackend_DrawButton(struct LButton* w);
void LBackend_InitCheckbox(struct LCheckbox* w);
void LBackend_DrawCheckbox(struct LCheckbox* w); void LBackend_DrawCheckbox(struct LCheckbox* w);
void LBackend_DrawInput(struct LInput* w, const cc_string* text); void LBackend_DrawInput(struct LInput* w, const cc_string* text);
void LBackend_UpdateLabel(struct LLabel* w);
void LBackend_DrawLabel(struct LLabel* w); void LBackend_DrawLabel(struct LLabel* w);
void LBackend_DrawLine(struct LLine* w); void LBackend_DrawLine(struct LLine* w);
void LBackend_DrawSlider(struct LSlider* w); void LBackend_DrawSlider(struct LSlider* w);
#endif #endif

View File

@ -129,17 +129,12 @@ static const struct LWidgetVTABLE lcheckbox_VTABLE = {
NULL, NULL /* Select */ NULL, NULL /* Select */
}; };
void LCheckbox_Init(struct LCheckbox* w, const char* text) { void LCheckbox_Init(struct LCheckbox* w, const char* text) {
struct DrawTextArgs args;
w->VTABLE = &lcheckbox_VTABLE; w->VTABLE = &lcheckbox_VTABLE;
w->font = &Launcher_TextFont;
w->tabSelectable = true; w->tabSelectable = true;
String_InitArray(w->text, w->_textBuffer); String_InitArray(w->text, w->_textBuffer);
String_AppendConst(&w->text, text); String_AppendConst(&w->text, text);
LBackend_InitCheckbox(w);
DrawTextArgs_Make(&args, &w->text, w->font, true);
w->width = Display_ScaleX(CB_SIZE + CB_OFFSET) + Drawer2D_TextWidth(&args);
w->height = Display_ScaleY(CB_SIZE);
} }
@ -481,12 +476,8 @@ void LLabel_Init(struct LLabel* w, const char* text) {
} }
void LLabel_SetText(struct LLabel* w, const cc_string* text) { void LLabel_SetText(struct LLabel* w, const cc_string* text) {
struct DrawTextArgs args;
String_Copy(&w->text, text); String_Copy(&w->text, text);
LBackend_UpdateLabel(w);
DrawTextArgs_Make(&args, &w->text, w->font, true);
w->width = Drawer2D_TextWidth(&args);
w->height = Drawer2D_TextHeight(&args);
LWidget_CalcPosition(w); LWidget_CalcPosition(w);
} }
@ -535,7 +526,6 @@ void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color) {
w->VTABLE = &lslider_VTABLE; w->VTABLE = &lslider_VTABLE;
w->width = Display_ScaleX(width); w->width = Display_ScaleX(width);
w->height = Display_ScaleY(height); w->height = Display_ScaleY(height);
w->maxValue = 100;
w->color = color; w->color = color;
} }

View File

@ -61,7 +61,6 @@ CC_NOINLINE void LButton_SetConst(struct LButton* w, const char* text);
struct LCheckbox { struct LCheckbox {
LWidget_Layout LWidget_Layout
struct FontDesc* font;
cc_bool value; cc_bool value;
cc_string text; cc_string text;
char _textBuffer[STRING_SIZE]; char _textBuffer[STRING_SIZE];
@ -121,7 +120,7 @@ CC_NOINLINE void LLine_Init(struct LLine* w, int width);
/* Represents a slider bar that may or may not be modifiable by the user. */ /* Represents a slider bar that may or may not be modifiable by the user. */
struct LSlider { struct LSlider {
LWidget_Layout LWidget_Layout
int value, maxValue; int value;
BitmapCol color; BitmapCol color;
}; };
CC_NOINLINE void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color); CC_NOINLINE void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color);