From ab6df2cc7ae5b8c0a1a807c174733dd2be5e8a13 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 16 Aug 2019 21:34:43 +1000 Subject: [PATCH] cleanup code a bit --- src/Menus.c | 65 ++++++++++++++++++++++----------------------------- src/Screens.c | 25 +++++++------------- src/Widgets.c | 25 +++++++------------- src/Widgets.h | 14 +++++------ 4 files changed, 51 insertions(+), 78 deletions(-) diff --git a/src/Menus.c b/src/Menus.c index 2aecbd136..1600b5cd0 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -49,19 +49,15 @@ struct SimpleButtonDesc { int x, y; const char* title; Widget_LeftClick onClick; *--------------------------------------------------------Menu base--------------------------------------------------------* *#########################################################################################################################*/ static void Menu_Button(void* s, int i, struct ButtonWidget* btn, int width, const String* text, const FontDesc* font, Widget_LeftClick onClick, int horAnchor, int verAnchor, int x, int y) { - struct Menu* menu = (struct Menu*)s; - ButtonWidget_Create(btn, width, text, font, onClick); - - menu->widgets[i] = (struct Widget*)btn; - Widget_SetLocation(menu->widgets[i], horAnchor, verAnchor, x, y); + ButtonWidget_Make(btn, width, onClick, horAnchor, verAnchor, x, y); + ButtonWidget_Set(btn, text, font); + ((struct Menu*)s)->widgets[i] = (struct Widget*)btn; } static void Menu_Label(void* s, int i, struct TextWidget* label, const String* text, const FontDesc* font, int horAnchor, int verAnchor, int x, int y) { - struct Menu* menu = (struct Menu*)s; - TextWidget_Create(label, text, font); - - menu->widgets[i] = (struct Widget*)label; - Widget_SetLocation(menu->widgets[i], horAnchor, verAnchor, x, y); + TextWidget_Make(label, horAnchor, verAnchor, x, y); + TextWidget_Set(label, text, font); + ((struct Menu*)s)->widgets[i] = (struct Widget*)label; } static void Menu_Input(void* s, int i, struct MenuInputWidget* input, int width, const String* text, FontDesc* font, struct MenuInputDesc* desc, int horAnchor, int verAnchor, int x, int y) { @@ -81,8 +77,7 @@ static void Menu_Back(void* s, int i, struct ButtonWidget* btn, const char* labe CC_NOINLINE static void Menu_MakeBack(struct ButtonWidget* btn, Widget_LeftClick onClick) { int width = Gui_ClassicMenu ? 400 : 200; - ButtonWidget_Make(btn, width, onClick); - Widget_SetLocation(btn, ANCHOR_CENTRE, ANCHOR_MAX, 0, 25); + ButtonWidget_Make(btn, width, onClick, ANCHOR_CENTRE, ANCHOR_MAX, 0, 25); } CC_NOINLINE static void Menu_MakeTitleFont(FontDesc* font) { @@ -416,21 +411,20 @@ static void ListScreen_Init(void* screen) { s->currentIndex = 0; for (i = 0; i < LIST_SCREEN_ITEMS; i++) { - ButtonWidget_Make(&s->buttons[i], 300, s->EntryClick); - Widget_SetLocation(&s->buttons[i], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, (i - 2) * 50); + ButtonWidget_Make(&s->buttons[i], 300, s->EntryClick, + ANCHOR_CENTRE, ANCHOR_CENTRE, 0, (i - 2) * 50); } - ButtonWidget_Make(&s->left, 40, ListScreen_MoveBackwards); - ButtonWidget_Make(&s->right, 40, ListScreen_MoveForwards); - TextWidget_Make(&s->title); - TextWidget_Make(&s->page); + ButtonWidget_Make(&s->left, 40, ListScreen_MoveBackwards, + ANCHOR_CENTRE, ANCHOR_CENTRE, -220, 0); + ButtonWidget_Make(&s->right, 40, ListScreen_MoveForwards, + ANCHOR_CENTRE, ANCHOR_CENTRE, 220, 0); + TextWidget_Make(&s->title, + ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -155); + TextWidget_Make(&s->page, + ANCHOR_CENTRE, ANCHOR_MAX, 0, 75); + Menu_MakeBack(&s->done, Menu_SwitchPause); - - Widget_SetLocation(&s->left, ANCHOR_CENTRE, ANCHOR_CENTRE, -220, 0); - Widget_SetLocation(&s->right, ANCHOR_CENTRE, ANCHOR_CENTRE, 220, 0); - Widget_SetLocation(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -155); - Widget_SetLocation(&s->page, ANCHOR_CENTRE, ANCHOR_MAX, 0, 75); - Menu_MakeTitleFont(&s->font); s->LoadEntries(s); } @@ -670,13 +664,12 @@ static void OptionsGroupScreen_Init(void* screen) { s->selectedI = -1; for (i = 0; i < Array_Elems(optsGroup_btns); i++) { - ButtonWidget_Make(&s->buttons[i], 300, optsGroup_btns[i].onClick); - Widget_SetLocation(&s->buttons[i], ANCHOR_CENTRE, ANCHOR_CENTRE, optsGroup_btns[i].x, optsGroup_btns[i].y); + ButtonWidget_Make(&s->buttons[i], 300, optsGroup_btns[i].onClick, + ANCHOR_CENTRE, ANCHOR_CENTRE, optsGroup_btns[i].x, optsGroup_btns[i].y); } Menu_MakeBack(&s->done, Menu_SwitchPause); - TextWidget_Make(&s->desc); - Widget_SetLocation(&s->desc, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 100); + TextWidget_Make(&s->desc, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 100); } static void OptionsGroupScreen_Free(void* screen) { @@ -1024,17 +1017,15 @@ static void GenLevelScreen_Input(struct GenLevelScreen* s, int i, int y, bool se input->base.MenuClick = GenLevelScreen_InputClick; } -static void GenLevelScreen_Label(struct GenLevelScreen* s, int i, int x, int y, const char* title) { +static void GenLevelScreen_Label(struct GenLevelScreen* s, int i, int y, const char* title) { struct TextWidget* label = &s->labels[i]; PackedCol col = PACKEDCOL_CONST(224, 224, 224, 255); String text = String_FromReadonly(title); Menu_Label(s, i + 4, label, &text, &s->textFont, - ANCHOR_CENTRE, ANCHOR_CENTRE, x, y); + ANCHOR_CENTRE_MAX, ANCHOR_CENTRE, 110, y); - label->col = col; - label->xOffset = -110 - label->width / 2; - Widget_Reposition(label); + label->col = col; } static bool GenLevelScreen_KeyDown(void* screen, Key key) { @@ -1064,10 +1055,10 @@ static void GenLevelScreen_ContextRecreated(void* screen) { GenLevelScreen_Input(s, 2, 0, false, World.Length); GenLevelScreen_Input(s, 3, 40, true, 0); - GenLevelScreen_Label(s, 0, -150, -80, "Width:"); - GenLevelScreen_Label(s, 1, -150, -40, "Height:"); - GenLevelScreen_Label(s, 2, -150, 0, "Length:"); - GenLevelScreen_Label(s, 3, -140, 40, "Seed:"); + GenLevelScreen_Label(s, 0, -80, "Width:"); + GenLevelScreen_Label(s, 1, -40, "Height:"); + GenLevelScreen_Label(s, 2, 0, "Length:"); + GenLevelScreen_Label(s, 3, 40, "Seed:"); Menu_Label(s, 8, &s->labels[4], &title, &s->textFont, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -130); diff --git a/src/Screens.c b/src/Screens.c index c798d4079..0c6d690cc 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -350,8 +350,7 @@ static void StatusScreen_ContextRecreated(void* screen) { Drawer2D_MakeFont(&s->font, 16, FONT_STYLE_NORMAL); y = 2; - TextWidget_Make(line1); - Widget_SetLocation(line1, ANCHOR_MIN, ANCHOR_MIN, 2, y); + TextWidget_Make(line1, ANCHOR_MIN, ANCHOR_MIN, 2, y); line1->reducePadding = true; StatusScreen_Update(s, 1.0); @@ -360,8 +359,7 @@ static void StatusScreen_ContextRecreated(void* screen) { s->posAtlas.tex.Y = y; y += s->posAtlas.tex.Height; - TextWidget_Make(line2); - Widget_SetLocation(line2, ANCHOR_MIN, ANCHOR_MIN, 2, y); + TextWidget_Make(line2, ANCHOR_MIN, ANCHOR_MIN, 2, y); line2->reducePadding = true; if (Game_ClassicMode) { @@ -507,10 +505,8 @@ static void LoadingScreen_DrawBackground(void) { static void LoadingScreen_Init(void* screen) { struct LoadingScreen* s = (struct LoadingScreen*)screen; - TextWidget_Make(&s->title); - Widget_SetLocation(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -31); - TextWidget_Make(&s->message); - Widget_SetLocation(&s->message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 17); + TextWidget_Make(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -31); + TextWidget_Make(&s->message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 17); Gfx_SetFog(false); Event_RegisterFloat(&WorldEvents.Loading, s, LoadingScreen_MapLoading); @@ -734,8 +730,7 @@ static void HUDScreen_ConstructWidgets(struct HUDScreen* s) { Widget_SetLocation(&s->clientStatus, ANCHOR_MIN, ANCHOR_MAX, 10, yOffset); Elem_Init(&s->clientStatus); - TextWidget_Create(&s->announcement, &String_Empty, &s->announcementFont); - Widget_SetLocation(&s->announcement, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -Window_Height / 4); + TextWidget_Make(&s->announcement, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -Window_Height / 4); } static void HUDScreen_SetInitialMessages(struct HUDScreen* s) { @@ -1327,13 +1322,11 @@ static void DisconnectScreen_ContextRecreated(void* screen) { static void DisconnectScreen_Init(void* screen) { struct DisconnectScreen* s = (struct DisconnectScreen*)screen; - TextWidget_Make(&s->title); - Widget_SetLocation(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -30); - TextWidget_Make(&s->message); - Widget_SetLocation(&s->message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 10); + TextWidget_Make(&s->title, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -30); + TextWidget_Make(&s->message, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 10); - ButtonWidget_Make(&s->reconnect, 300, NULL); - Widget_SetLocation(&s->reconnect, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 80); + ButtonWidget_Make(&s->reconnect, 300, NULL, + ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 80); s->reconnect.disabled = !s->canReconnect; /* NOTE: changing VSync can't be done within frame, causes crash on some GPUs */ diff --git a/src/Widgets.c b/src/Widgets.c index a35845937..3944a6bb8 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -54,11 +54,12 @@ static struct WidgetVTABLE TextWidget_VTABLE = { Widget_Mouse, Widget_Mouse, Widget_MouseMove, Widget_MouseScroll, TextWidget_Reposition, }; -void TextWidget_Make(struct TextWidget* w) { +void TextWidget_Make(struct TextWidget* w, uint8_t horAnchor, uint8_t verAnchor, int xOffset, int yOffset) { PackedCol col = PACKEDCOL_WHITE; Widget_Reset(w); w->VTABLE = &TextWidget_VTABLE; w->col = col; + Widget_SetLocation(w, horAnchor, verAnchor, xOffset, yOffset); } void TextWidget_Set(struct TextWidget* w, const String* text, const FontDesc* font) { @@ -87,11 +88,6 @@ void TextWidget_SetConst(struct TextWidget* w, const char* text, const FontDesc* TextWidget_Set(w, &str, font); } -void TextWidget_Create(struct TextWidget* w, const String* text, const FontDesc* font) { - TextWidget_Make(w); - TextWidget_Set(w, text, font); -} - /*########################################################################################################################* *------------------------------------------------------ButtonWidget-------------------------------------------------------* @@ -161,12 +157,13 @@ static struct WidgetVTABLE ButtonWidget_VTABLE = { Widget_Mouse, Widget_Mouse, Widget_MouseMove, Widget_MouseScroll, ButtonWidget_Reposition, }; -void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick) { +void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick, uint8_t horAnchor, uint8_t verAnchor, int xOffset, int yOffset) { Widget_Reset(w); w->VTABLE = &ButtonWidget_VTABLE; w->optName = NULL; w->minWidth = minWidth; w->MenuClick = onClick; + Widget_SetLocation(w, horAnchor, verAnchor, xOffset, yOffset); } void ButtonWidget_Set(struct ButtonWidget* w, const String* text, const FontDesc* font) { @@ -191,11 +188,6 @@ void ButtonWidget_SetConst(struct ButtonWidget* w, const char* text, const FontD ButtonWidget_Set(w, &str, font); } -void ButtonWidget_Create(struct ButtonWidget* w, int minWidth, const String* text, const FontDesc* font, Widget_LeftClick onClick) { - ButtonWidget_Make(w, minWidth, onClick); - ButtonWidget_Set(w, text, font); -} - /*########################################################################################################################* *-----------------------------------------------------ScrollbarWidget-----------------------------------------------------* @@ -256,7 +248,7 @@ static bool ScrollbarWidget_MouseDown(void* widget, int x, int y, MouseButton bt struct ScrollbarWidget* w = (struct ScrollbarWidget*)widget; int posY, height; - if (w->draggingMouse) return true; + if (w->draggingMouse) return true; if (btn != MOUSE_LEFT) return false; if (x < w->x || x >= w->x + w->width) return false; @@ -2094,7 +2086,6 @@ static void PlayerListWidget_TabEntryRemoved(void* widget, int id) { } static void PlayerListWidget_Init(void* widget) { - static const String title = String_FromConst("Connected players:"); struct PlayerListWidget* w = (struct PlayerListWidget*)widget; int id; @@ -2103,9 +2094,9 @@ static void PlayerListWidget_Init(void* widget) { PlayerListWidget_AddName(w, (EntityID)id, -1); } - PlayerListWidget_SortAndReposition(w); - TextWidget_Create(&w->title, &title, w->font); - Widget_SetLocation(&w->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0); + PlayerListWidget_SortAndReposition(w); + TextWidget_Make(&w->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0); + TextWidget_SetConst(&w->title, "Connected players:", w->font); Event_RegisterInt(&TabListEvents.Added, w, PlayerListWidget_TabEntryAdded); Event_RegisterInt(&TabListEvents.Changed, w, PlayerListWidget_TabEntryChanged); diff --git a/src/Widgets.h b/src/Widgets.h index 0fa8c7584..ec093e2bd 100644 --- a/src/Widgets.h +++ b/src/Widgets.h @@ -16,14 +16,13 @@ struct TextWidget { bool reducePadding; PackedCol col; }; -/* Resets state of the given text widget to default. */ -CC_NOINLINE void TextWidget_Make(struct TextWidget* w); +/* Initialises a text widget. */ +CC_NOINLINE void TextWidget_Make(struct TextWidget* w, + uint8_t horAnchor, uint8_t verAnchor, int xOffset, int yOffset); /* Draws the given text into a texture, then updates the position and size of this widget. */ CC_NOINLINE void TextWidget_Set(struct TextWidget* w, const String* text, const FontDesc* font); /* Shorthand for TextWidget_Set using String_FromReadonly */ CC_NOINLINE void TextWidget_SetConst(struct TextWidget* w, const char* text, const FontDesc* font); -/* Shorthand for TextWidget_Make then TextWidget_Set */ -CC_NOINLINE void TextWidget_Create(struct TextWidget* w, const String* text, const FontDesc* font); typedef void (*Button_Get)(String* raw); @@ -38,14 +37,13 @@ struct ButtonWidget { Button_Get GetValue; Button_Set SetValue; }; -/* Resets state of the given button widget to default. */ -CC_NOINLINE void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick); +/* Initialises a button widget. */ +CC_NOINLINE void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick, + uint8_t horAnchor, uint8_t verAnchor, int xOffset, int yOffset); /* Draws the given text into a texture, then updates the position and size of this widget. */ CC_NOINLINE void ButtonWidget_Set(struct ButtonWidget* w, const String* text, const FontDesc* font); /* Shorthand for ButtonWidget_Set using String_FromReadonly */ CC_NOINLINE void ButtonWidget_SetConst(struct ButtonWidget* w, const char* text, const FontDesc* font); -/* Resets state of the given button widget, then calls ButtonWidget_Set */ -CC_NOINLINE void ButtonWidget_Create(struct ButtonWidget* w, int minWidth, const String* text, const FontDesc* font, Widget_LeftClick onClick); /* Clickable and draggable scrollbar. */ struct ScrollbarWidget {