Launcher: Split up more stuff between backend

This commit is contained in:
UnknownShadow200 2022-04-03 09:26:48 +10:00
parent a8d8fb9411
commit aa90b53168
4 changed files with 56 additions and 18 deletions

View File

@ -1,5 +1,10 @@
#include "LBackend.h" #include "LBackend.h"
#ifndef CC_BUILD_WEB #if defined CC_BUILD_WEB
/* Web backend doesn't use the launcher */
#elif defined CC_BUILD_WIN_TEST
/* Testing windows UI backend */
#include "LBackend_Win.c"
#else
#include "Launcher.h" #include "Launcher.h"
#include "Drawer2D.h" #include "Drawer2D.h"
#include "Window.h" #include "Window.h"
@ -48,10 +53,25 @@ static void DrawBoxBounds(BitmapCol col, int x, int y, int width, int height) {
xBorder, height); xBorder, height);
} }
void LBackend_WidgetRepositioned(struct LWidget* w) { }
/*########################################################################################################################* /*########################################################################################################################*
*------------------------------------------------------ButtonWidget-------------------------------------------------------* *------------------------------------------------------ButtonWidget-------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void LBackend_InitButton(struct LButton* w, int width, int height) {
w->width = Display_ScaleX(width);
w->height = Display_ScaleY(height);
}
void LBackend_UpdateButton(struct LButton* w) {
struct DrawTextArgs args;
DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true);
w->_textWidth = Drawer2D_TextWidth(&args);
w->_textHeight = Drawer2D_TextHeight(&args);
}
static BitmapCol LButton_Expand(BitmapCol a, int amount) { static BitmapCol LButton_Expand(BitmapCol a, int amount) {
int r, g, b; int r, g, b;
r = BitmapCol_R(a) + amount; Math_Clamp(r, 0, 255); r = BitmapCol_R(a) + amount; Math_Clamp(r, 0, 255);
@ -231,6 +251,11 @@ void LBackend_DrawCheckbox(struct LCheckbox* w) {
/*########################################################################################################################* /*########################################################################################################################*
*------------------------------------------------------InputWidget--------------------------------------------------------* *------------------------------------------------------InputWidget--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void LBackend_InitInput(struct LInput* w, int width) {
w->width = Display_ScaleX(width);
w->height = Display_ScaleY(30);
}
static void LInput_DrawOuterBorder(struct LInput* w) { static void LInput_DrawOuterBorder(struct LInput* w) {
BitmapCol color = BitmapCol_Make(97, 81, 110, 255); BitmapCol color = BitmapCol_Make(97, 81, 110, 255);
@ -317,6 +342,8 @@ void LBackend_DrawInput(struct LInput* w, const cc_string* text) {
/*########################################################################################################################* /*########################################################################################################################*
*------------------------------------------------------LabelWidget--------------------------------------------------------* *------------------------------------------------------LabelWidget--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void LBackend_InitLabel(struct LLabel* w) { }
void LBackend_UpdateLabel(struct LLabel* w) { void LBackend_UpdateLabel(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);
@ -335,6 +362,11 @@ void LBackend_DrawLabel(struct LLabel* w) {
/*########################################################################################################################* /*########################################################################################################################*
*-------------------------------------------------------LineWidget--------------------------------------------------------* *-------------------------------------------------------LineWidget--------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void LBackend_InitLine(struct LLine* w, int width) {
w->width = Display_ScaleX(width);
w->height = Display_ScaleY(2);
}
#define CLASSIC_LINE_COLOR BitmapCol_Make(128,128,128, 255) #define CLASSIC_LINE_COLOR BitmapCol_Make(128,128,128, 255)
void LBackend_DrawLine(struct LLine* w) { void LBackend_DrawLine(struct LLine* w) {
BitmapCol color = Launcher_Theme.ClassicBackground ? CLASSIC_LINE_COLOR : Launcher_Theme.ButtonBorderColor; BitmapCol color = Launcher_Theme.ClassicBackground ? CLASSIC_LINE_COLOR : Launcher_Theme.ButtonBorderColor;
@ -345,6 +377,11 @@ void LBackend_DrawLine(struct LLine* w) {
/*########################################################################################################################* /*########################################################################################################################*
*------------------------------------------------------SliderWidget-------------------------------------------------------* *------------------------------------------------------SliderWidget-------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
void LBackend_InitSlider(struct LSlider* w, int width, int height) {
w->width = Display_ScaleX(width);
w->height = Display_ScaleY(height);
}
static void LSlider_DrawBoxBounds(struct LSlider* w) { static void LSlider_DrawBoxBounds(struct LSlider* w) {
BitmapCol boundsTop = BitmapCol_Make(119, 100, 132, 255); BitmapCol boundsTop = BitmapCol_Make(119, 100, 132, 255);
BitmapCol boundsBottom = BitmapCol_Make(150, 130, 165, 255); BitmapCol boundsBottom = BitmapCol_Make(150, 130, 165, 255);

View File

@ -5,6 +5,7 @@
Copyright 2014-2021 ClassiCube | Licensed under BSD-3 Copyright 2014-2021 ClassiCube | Licensed under BSD-3
*/ */
struct Bitmap; struct Bitmap;
struct LWidget;
struct LButton; struct LButton;
struct LCheckbox; struct LCheckbox;
struct LInput; struct LInput;
@ -13,18 +14,25 @@ struct LLine;
struct LSlider; struct LSlider;
void LBackend_CalcOffsets(void); void LBackend_CalcOffsets(void);
void LBackend_WidgetRepositioned(struct LWidget* w);
void LBackend_InitButton(struct LButton* w, int width, int height);
void LBackend_UpdateButton(struct LButton* w);
void LBackend_DrawButton(struct LButton* w); void LBackend_DrawButton(struct LButton* w);
void LBackend_InitCheckbox(struct LCheckbox* w); void LBackend_InitCheckbox(struct LCheckbox* w);
void LBackend_DrawCheckbox(struct LCheckbox* w); void LBackend_DrawCheckbox(struct LCheckbox* w);
void LBackend_InitInput(struct LInput* w, int width);
void LBackend_DrawInput(struct LInput* w, const cc_string* text); void LBackend_DrawInput(struct LInput* w, const cc_string* text);
void LBackend_InitLabel(struct LLabel* w);
void LBackend_UpdateLabel(struct LLabel* w); void LBackend_UpdateLabel(struct LLabel* w);
void LBackend_DrawLabel(struct LLabel* w); void LBackend_DrawLabel(struct LLabel* w);
void LBackend_InitLine(struct LLine* w, int width);
void LBackend_DrawLine(struct LLine* w); void LBackend_DrawLine(struct LLine* w);
void LBackend_InitSlider(struct LSlider* w, int width, int height);
void LBackend_DrawSlider(struct LSlider* w); void LBackend_DrawSlider(struct LSlider* w);
#endif #endif

View File

@ -57,6 +57,7 @@ void LWidget_CalcPosition(void* widget) {
struct LWidget* w = (struct LWidget*)widget; struct LWidget* w = (struct LWidget*)widget;
w->x = Gui_CalcPos(w->horAnchor, Display_ScaleX(w->xOffset), w->width, WindowInfo.Width); w->x = Gui_CalcPos(w->horAnchor, Display_ScaleX(w->xOffset), w->width, WindowInfo.Width);
w->y = Gui_CalcPos(w->verAnchor, Display_ScaleY(w->yOffset), w->height, WindowInfo.Height); w->y = Gui_CalcPos(w->verAnchor, Display_ScaleY(w->yOffset), w->height, WindowInfo.Height);
LBackend_WidgetRepositioned(w);
} }
void LWidget_Draw(void* widget) { void LWidget_Draw(void* widget) {
@ -98,18 +99,13 @@ static const struct LWidgetVTABLE lbutton_VTABLE = {
void LButton_Init(struct LButton* w, int width, int height, const char* text) { void LButton_Init(struct LButton* w, int width, int height, const char* text) {
w->VTABLE = &lbutton_VTABLE; w->VTABLE = &lbutton_VTABLE;
w->tabSelectable = true; w->tabSelectable = true;
w->width = Display_ScaleX(width); LBackend_InitButton(w, width, height);
w->height = Display_ScaleY(height);
LButton_SetConst(w, text); LButton_SetConst(w, text);
} }
void LButton_SetConst(struct LButton* w, const char* text) { void LButton_SetConst(struct LButton* w, const char* text) {
struct DrawTextArgs args;
w->text = String_FromReadonly(text); w->text = String_FromReadonly(text);
DrawTextArgs_Make(&args, &w->text, &Launcher_TitleFont, true); LBackend_UpdateButton(w);
w->_textWidth = Drawer2D_TextWidth(&args);
w->_textHeight = Drawer2D_TextHeight(&args);
} }
@ -404,13 +400,10 @@ void LInput_Init(struct LInput* w, int width, const char* hintText) {
w->TextFilter = LInput_DefaultInputFilter; w->TextFilter = LInput_DefaultInputFilter;
String_InitArray(w->text, w->_textBuffer); String_InitArray(w->text, w->_textBuffer);
w->width = Display_ScaleX(width);
w->height = Display_ScaleY(30);
w->minWidth = w->width;
LWidget_CalcPosition(w);
w->hintText = hintText; w->hintText = hintText;
w->caretPos = -1; w->caretPos = -1;
LBackend_InitInput(w, width);
w->minWidth = w->width;
} }
void LInput_SetText(struct LInput* w, const cc_string* text_) { void LInput_SetText(struct LInput* w, const cc_string* text_) {
@ -472,6 +465,7 @@ void LLabel_Init(struct LLabel* w, const char* text) {
w->font = &Launcher_TextFont; w->font = &Launcher_TextFont;
String_InitArray(w->text, w->_textBuffer); String_InitArray(w->text, w->_textBuffer);
LBackend_InitLabel(w);
LLabel_SetConst(w, text); LLabel_SetConst(w, text);
} }
@ -503,8 +497,7 @@ static const struct LWidgetVTABLE lline_VTABLE = {
}; };
void LLine_Init(struct LLine* w, int width) { void LLine_Init(struct LLine* w, int width) {
w->VTABLE = &lline_VTABLE; w->VTABLE = &lline_VTABLE;
w->width = Display_ScaleX(width); LBackend_InitLine(w, width);
w->height = Display_ScaleY(2);
} }
@ -524,9 +517,8 @@ static const struct LWidgetVTABLE lslider_VTABLE = {
}; };
void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color) { 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->height = Display_ScaleY(height);
w->color = color; w->color = color;
LBackend_InitSlider(w, width, height);
} }

View File

@ -41,7 +41,8 @@ struct LWidgetVTABLE {
cc_uint8 horAnchor, verAnchor; /* Specifies the reference point for when this widget is resized */ \ cc_uint8 horAnchor, verAnchor; /* Specifies the reference point for when this widget is resized */ \
int xOffset, yOffset; /* Offset from the reference point */ \ int xOffset, yOffset; /* Offset from the reference point */ \
void (*OnClick)(void* widget, int idx); /* Called when widget is clicked */ \ void (*OnClick)(void* widget, int idx); /* Called when widget is clicked */ \
Rect2D last; /* Widget's last drawn area */ Rect2D last; /* Widget's last drawn area */ \
void* meta; /* Backend specific data */
/* Represents an individual 2D gui component in the launcher. */ /* Represents an individual 2D gui component in the launcher. */
struct LWidget { LWidget_Layout }; struct LWidget { LWidget_Layout };