From c812e7294132b5b37aef78eb8ab0d7fdbf157527 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 7 May 2022 09:32:02 +1000 Subject: [PATCH] Port MainScreen to use new layout system --- src/LScreens.c | 68 +++++++++++++++++++++++++---------------------- src/LWidgets.c | 72 +++++++++++++++++++++++++------------------------- src/LWidgets.h | 26 +++++++++--------- 3 files changed, 85 insertions(+), 81 deletions(-) diff --git a/src/LScreens.c b/src/LScreens.c index fb4f22141..95b7642a4 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -17,6 +17,7 @@ #include "Options.h" #include "Utils.h" #include "LBackend.h" +#define LAYOUTS static const struct LLayout /*########################################################################################################################* *---------------------------------------------------------Screen base-----------------------------------------------------* @@ -41,6 +42,14 @@ CC_NOINLINE static struct LWidget* LScreen_WidgetAt(struct LScreen* s, int idx) return NULL; } +static void LScreen_DoLayout(struct LScreen* s) { + int i; + for (i = 0; i < s->numWidgets; i++) + { + LWidget_CalcPosition(s->widgets[i]); + } +} + static void LScreen_Tick(struct LScreen* s) { struct LWidget* w = s->selectedWidget; if (w && w->VTABLE->Tick) w->VTABLE->Tick(w); @@ -173,6 +182,7 @@ CC_NOINLINE static void LScreen_Reset(struct LScreen* s) { s->Init = NULL; /* screens should always override this */ s->Show = LScreen_NullFunc; s->Free = LScreen_NullFunc; + s->Layout = LScreen_DoLayout; s->Tick = LScreen_Tick; s->KeyDown = LScreen_KeyDown; s->KeyPress = LScreen_KeyPress; @@ -732,7 +742,19 @@ static struct LWidget* main_widgets[] = { (struct LWidget*)&MainScreen.btnRegister, (struct LWidget*)&MainScreen.btnOptions }; -static const struct LConstraint main_login[] = { LConstraint_X(ANCHOR_CENTRE, -90), LConstraint_Y(ANCHOR_CENTRE, -25), LConstraint_None }; +LAYOUTS main_iptUsername[] = { LLayout_X(ANCHOR_CENTRE_MIN, -140), LLayout_Y(ANCHOR_CENTRE, -120), LLayout_End }; +LAYOUTS main_iptPassword[] = { LLayout_X(ANCHOR_CENTRE_MIN, -140), LLayout_Y(ANCHOR_CENTRE, -75), LLayout_End }; + +LAYOUTS main_btnLogin[] = { LLayout_X(ANCHOR_CENTRE, -90), LLayout_Y(ANCHOR_CENTRE, -25), LLayout_End }; +LAYOUTS main_lblStatus[] = { LLayout_X(ANCHOR_CENTRE, 0), LLayout_Y(ANCHOR_CENTRE, 20), LLayout_End }; + +LAYOUTS main_btnResume[] = { LLayout_X(ANCHOR_CENTRE, 90), LLayout_Y(ANCHOR_CENTRE, -25), LLayout_End }; +LAYOUTS main_btnDirect[] = { LLayout_X(ANCHOR_CENTRE, 0), LLayout_Y(ANCHOR_CENTRE, 60), LLayout_End }; +LAYOUTS main_btnSPlayer[] = { LLayout_X(ANCHOR_CENTRE, 0), LLayout_Y(ANCHOR_CENTRE, 110), LLayout_End }; + +LAYOUTS main_lblUpdate[] = { LLayout_X(ANCHOR_MAX, 10), LLayout_Y(ANCHOR_MAX, 45), LLayout_End }; +LAYOUTS main_btnOptions[] = { LLayout_X(ANCHOR_MAX, 6), LLayout_Y(ANCHOR_MAX, 6), LLayout_End }; +LAYOUTS main_btnRegister[] = { LLayout_X(ANCHOR_MIN, 6), LLayout_Y(ANCHOR_MAX, 6), LLayout_End }; struct ResumeInfo { cc_string user, ip, port, server, mppass; @@ -851,18 +873,18 @@ static void MainScreen_Init(struct LScreen* s_) { s->iptPassword.inputType = KEYBOARD_TYPE_PASSWORD; s->lblUpdate.small = true; - LInput_Init( &s->iptUsername, 280, "Username..", NULL); - LInput_Init( &s->iptPassword, 280, "Password..", NULL); - LButton_Init(&s->btnLogin, 100, 35, "Sign in", main_login); - LButton_Init(&s->btnResume, 100, 35, "Resume", NULL); + LInput_Init( &s->iptUsername, 280, "Username..", main_iptUsername); + LInput_Init( &s->iptPassword, 280, "Password..", main_iptPassword); + LButton_Init(&s->btnLogin, 100, 35, "Sign in", main_btnLogin); + LButton_Init(&s->btnResume, 100, 35, "Resume", main_btnResume); - LLabel_Init( &s->lblStatus, "", NULL); - LButton_Init(&s->btnDirect, 200, 35, "Direct connect", NULL); - LButton_Init(&s->btnSPlayer, 200, 35, "Singleplayer", NULL); + LLabel_Init( &s->lblStatus, "", main_lblStatus); + LButton_Init(&s->btnDirect, 200, 35, "Direct connect", main_btnDirect); + LButton_Init(&s->btnSPlayer, 200, 35, "Singleplayer", main_btnSPlayer); - LLabel_Init( &s->lblUpdate, "&eChecking..", NULL); - LButton_Init(&s->btnRegister, 100, 35, "Register", NULL); - LButton_Init(&s->btnOptions, 100, 35, "Options", NULL); + LLabel_Init( &s->lblUpdate, "&eChecking..", main_lblUpdate); + LButton_Init(&s->btnRegister, 100, 35, "Register", main_btnRegister); + LButton_Init(&s->btnOptions, 100, 35, "Options", main_btnOptions); s->btnLogin.OnClick = MainScreen_Login; s->btnResume.OnClick = MainScreen_Resume; @@ -893,23 +915,6 @@ static void MainScreen_Free(struct LScreen* s_) { LLabel_SetConst(&s->lblStatus, ""); } -static void MainScreen_Layout(struct LScreen* s_) { - struct MainScreen* s = (struct MainScreen*)s_; - LWidget_SetLocation(&s->iptUsername, ANCHOR_CENTRE_MIN, ANCHOR_CENTRE, -140, -120); - LWidget_SetLocation(&s->iptPassword, ANCHOR_CENTRE_MIN, ANCHOR_CENTRE, -140, -75); - - LWidget_SetLocation(&s->btnLogin, ANCHOR_CENTRE, ANCHOR_CENTRE, -90, -25); - LWidget_SetLocation(&s->lblStatus, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 20); - - LWidget_SetLocation(&s->btnResume, ANCHOR_CENTRE, ANCHOR_CENTRE, 90, -25); - LWidget_SetLocation(&s->btnDirect, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 60); - LWidget_SetLocation(&s->btnSPlayer, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 110); - - LWidget_SetLocation(&s->lblUpdate, ANCHOR_MAX, ANCHOR_MAX, 10, 45); - LWidget_SetLocation(&s->btnOptions, ANCHOR_MAX, ANCHOR_MAX, 6, 6); - LWidget_SetLocation(&s->btnRegister, ANCHOR_MIN, ANCHOR_MAX, 6, 6); -} - CC_NOINLINE static cc_uint32 MainScreen_GetVersion(const cc_string* version) { cc_uint8 raw[4] = { 0, 0, 0, 0 }; cc_string parts[4]; @@ -1018,10 +1023,9 @@ static void MainScreen_Tick(struct LScreen* s_) { void MainScreen_SetActive(void) { struct MainScreen* s = &MainScreen; LScreen_Reset((struct LScreen*)s); - s->Init = MainScreen_Init; - s->Free = MainScreen_Free; - s->Tick = MainScreen_Tick; - s->Layout = MainScreen_Layout; + s->Init = MainScreen_Init; + s->Free = MainScreen_Free; + s->Tick = MainScreen_Tick;\ s->title = "ClassiCube"; s->onEnterWidget = (struct LWidget*)&s->btnLogin; diff --git a/src/LWidgets.c b/src/LWidgets.c index 77cb23fbf..25154e32c 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -35,23 +35,23 @@ void LWidget_SetLocation(void* widget, cc_uint8 horAnchor, cc_uint8 verAnchor, i void LWidget_CalcPosition(void* widget) { struct LWidget* w = (struct LWidget*)widget; - const struct LConstraint* c = w->constraints; + const struct LLayout* l = w->layouts; int type, anchor; - if (c) { - while ((type = c->type & 0xFF00)) + if (l) { + while ((type = l->type & 0xFF00)) { - anchor = c->type & 0xFF; + anchor = l->type & 0xFF; switch (type) { - case LCONSTRAINT_TYPE_X: - w->x = Gui_CalcPos(anchor, Display_ScaleX(c->offset), w->width, WindowInfo.Width); + case LLAYOUT_TYPE_X: + w->x = Gui_CalcPos(anchor, Display_ScaleX(l->offset), w->width, WindowInfo.Width); break; - case LCONSTRAINT_TYPE_Y: - w->y = Gui_CalcPos(anchor, Display_ScaleY(c->offset), w->height, WindowInfo.Height); + case LLAYOUT_TYPE_Y: + w->y = Gui_CalcPos(anchor, Display_ScaleY(l->offset), w->height, WindowInfo.Height); break; } - c++; + l++; } } else { w->x = Gui_CalcPos(w->horAnchor, Display_ScaleX(w->xOffset), w->width, WindowInfo.Width); @@ -156,10 +156,10 @@ static const struct LWidgetVTABLE lbutton_VTABLE = { LButton_Hover, LButton_Unhover, /* Hover */ NULL, NULL /* Select */ }; -void LButton_Init(struct LButton* w, int width, int height, const char* text, const struct LConstraint* constraints) { - w->VTABLE = &lbutton_VTABLE; - w->type = LWIDGET_BUTTON; - w->constraints = constraints; +void LButton_Init(struct LButton* w, int width, int height, const char* text, const struct LLayout* layouts) { + w->VTABLE = &lbutton_VTABLE; + w->type = LWIDGET_BUTTON; + w->layouts = layouts; w->tabSelectable = true; LBackend_ButtonInit(w, width, height); LButton_SetConst(w, text); @@ -185,10 +185,10 @@ static const struct LWidgetVTABLE lcheckbox_VTABLE = { NULL, NULL, /* Hover */ NULL, NULL /* Select */ }; -void LCheckbox_Init(struct LCheckbox* w, const char* text, const struct LConstraint* constraints) { - w->VTABLE = &lcheckbox_VTABLE; - w->type = LWIDGET_CHECKBOX; - w->constraints = constraints; +void LCheckbox_Init(struct LCheckbox* w, const char* text, const struct LLayout* layouts) { + w->VTABLE = &lcheckbox_VTABLE; + w->type = LWIDGET_CHECKBOX; + w->layouts = layouts; w->tabSelectable = true; w->text = String_FromReadonly(text); @@ -350,12 +350,12 @@ static const struct LWidgetVTABLE linput_VTABLE = { LInput_Select, LInput_Unselect, /* Select */ NULL, LInput_TextChanged /* TextChanged */ }; -void LInput_Init(struct LInput* w, int width, const char* hintText, const struct LConstraint* constraints) { - w->VTABLE = &linput_VTABLE; - w->type = LWIDGET_INPUT; +void LInput_Init(struct LInput* w, int width, const char* hintText, const struct LLayout* layouts) { + w->VTABLE = &linput_VTABLE; + w->type = LWIDGET_INPUT; w->tabSelectable = true; - w->opaque = true; - w->constraints = constraints; + w->opaque = true; + w->layouts = layouts; String_InitArray(w->text, w->_textBuffer); w->hintText = hintText; @@ -406,10 +406,10 @@ static const struct LWidgetVTABLE llabel_VTABLE = { NULL, NULL, /* Hover */ NULL, NULL /* Select */ }; -void LLabel_Init(struct LLabel* w, const char* text, const struct LConstraint* constraints) { - w->VTABLE = &llabel_VTABLE; - w->type = LWIDGET_LABEL; - w->constraints = constraints; +void LLabel_Init(struct LLabel* w, const char* text, const struct LLayout* layouts) { + w->VTABLE = &llabel_VTABLE; + w->type = LWIDGET_LABEL; + w->layouts = layouts; String_InitArray(w->text, w->_textBuffer); LBackend_LabelInit(w); @@ -442,10 +442,10 @@ static const struct LWidgetVTABLE lline_VTABLE = { NULL, NULL, /* Hover */ NULL, NULL /* Select */ }; -void LLine_Init(struct LLine* w, int width, const struct LConstraint* constraints) { - w->VTABLE = &lline_VTABLE; - w->type = LWIDGET_LINE; - w->constraints = constraints; +void LLine_Init(struct LLine* w, int width, const struct LLayout* layouts) { + w->VTABLE = &lline_VTABLE; + w->type = LWIDGET_LINE; + w->layouts = layouts; LBackend_LineInit(w, width); } @@ -469,12 +469,12 @@ static const struct LWidgetVTABLE lslider_VTABLE = { NULL, NULL, /* Hover */ NULL, NULL /* Select */ }; -void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color, const struct LConstraint* constraints) { - w->VTABLE = &lslider_VTABLE; - w->type = LWIDGET_SLIDER; - w->color = color; - w->opaque = true; - w->constraints = constraints; +void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color, const struct LLayout* layouts) { + w->VTABLE = &lslider_VTABLE; + w->type = LWIDGET_SLIDER; + w->color = color; + w->opaque = true; + w->layouts = layouts; LBackend_SliderInit(w, width, height); } diff --git a/src/LWidgets.h b/src/LWidgets.h index f1480865d..d6c11fb15 100644 --- a/src/LWidgets.h +++ b/src/LWidgets.h @@ -11,12 +11,12 @@ enum LWIDGET_TYPE { LWIDGET_LABEL, LWIDGET_LINE, LWIDGET_SLIDER, LWIDGET_TABLE }; -#define LCONSTRAINT_TYPE_X 0x0100 -#define LCONSTRAINT_TYPE_Y 0x0200 -#define LConstraint_None { 0, 0 } -#define LConstraint_X(anchor, offset) { LCONSTRAINT_TYPE_X | anchor, offset } -#define LConstraint_Y(anchor, offset) { LCONSTRAINT_TYPE_Y | anchor, offset } -struct LConstraint { short type, offset; }; +#define LLAYOUT_TYPE_X 0x0100 +#define LLAYOUT_TYPE_Y 0x0200 +#define LLayout_End { 0, 0 } +#define LLayout_X(anchor, offset) { LLAYOUT_TYPE_X | anchor, offset } +#define LLayout_Y(anchor, offset) { LLAYOUT_TYPE_Y | anchor, offset } +struct LLayout { short type, offset; }; struct LWidgetVTABLE { /* Called to draw contents of this widget */ @@ -59,7 +59,7 @@ struct LWidgetVTABLE { void (*OnUnhover)(void* widget);/*Called when widget is no longer hovered over */ \ Rect2D last; /* Widget's last drawn area */ \ void* meta; /* Backend specific data */ \ - const struct LConstraint* constraints; + const struct LLayout* layouts; /* Represents an individual 2D gui component in the launcher. */ struct LWidget { LWidget_Layout }; @@ -72,7 +72,7 @@ struct LButton { cc_string text; int _textWidth, _textHeight; }; -CC_NOINLINE void LButton_Init(struct LButton* w, int width, int height, const char* text, const struct LConstraint* constraints); +CC_NOINLINE void LButton_Init(struct LButton* w, int width, int height, const char* text, const struct LLayout* layouts); CC_NOINLINE void LButton_SetConst(struct LButton* w, const char* text); CC_NOINLINE void LButton_DrawBackground(struct LButton* w, struct Bitmap* bmp, int x, int y); @@ -83,7 +83,7 @@ struct LCheckbox { cc_string text; void (*ValueChanged)(struct LCheckbox* w); }; -CC_NOINLINE void LCheckbox_Init(struct LCheckbox* w, const char* text, const struct LConstraint* constraints); +CC_NOINLINE void LCheckbox_Init(struct LCheckbox* w, const char* text, const struct LLayout* layouts); struct LInput; struct LInput { @@ -107,7 +107,7 @@ struct LInput { int _textHeight; char _textBuffer[STRING_SIZE]; }; -CC_NOINLINE void LInput_Init(struct LInput* w, int width, const char* hintText, const struct LConstraint* constraints); +CC_NOINLINE void LInput_Init(struct LInput* w, int width, const char* hintText, const struct LLayout* layouts); CC_NOINLINE void LInput_UNSAFE_GetText(struct LInput* w, cc_string* text); CC_NOINLINE void LInput_SetText(struct LInput* w, const cc_string* text); CC_NOINLINE void LInput_ClearText(struct LInput* w); @@ -124,7 +124,7 @@ struct LLabel { cc_string text; char _textBuffer[STRING_SIZE]; }; -CC_NOINLINE void LLabel_Init(struct LLabel* w, const char* text, const struct LConstraint* constraints); +CC_NOINLINE void LLabel_Init(struct LLabel* w, const char* text, const struct LLayout* layouts); CC_NOINLINE void LLabel_SetText(struct LLabel* w, const cc_string* text); CC_NOINLINE void LLabel_SetConst(struct LLabel* w, const char* text); @@ -133,7 +133,7 @@ struct LLine { LWidget_Layout int _width; }; -CC_NOINLINE void LLine_Init(struct LLine* w, int width, const struct LConstraint* constraints); +CC_NOINLINE void LLine_Init(struct LLine* w, int width, const struct LLayout* layouts); CC_NOINLINE BitmapCol LLine_GetColor(void); /* Represents a slider bar that may or may not be modifiable by the user. */ @@ -142,7 +142,7 @@ struct LSlider { int value, _width, _height; BitmapCol color; }; -CC_NOINLINE void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color, const struct LConstraint* constraints); +CC_NOINLINE void LSlider_Init(struct LSlider* w, int width, int height, BitmapCol color, const struct LLayout* layouts); CC_NOINLINE void LSlider_SetProgress(struct LSlider* w, int progress); struct ServerInfo;