From c0595f7fb6454639cba207852ec66c3deceedd52 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 28 Sep 2019 17:19:33 +1000 Subject: [PATCH] Simplify player list widget related code To be consistent with other widgets, player list widget doesn't hook into events anymore. --- src/Screens.c | 48 ++++++++++++++++++++++++++++++++---------------- src/Widgets.c | 39 +++++++++++---------------------------- src/Widgets.h | 9 ++++++++- 3 files changed, 51 insertions(+), 45 deletions(-) diff --git a/src/Screens.c b/src/Screens.c index f4a1cf44b..43afc6967 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -30,7 +30,7 @@ struct HUDScreen { /* player list state */ struct PlayerListWidget playerList; struct FontDesc playerFont; - bool showingList, wasShowingList; + bool showingList; /* chat state */ float chatAcc; bool suppressNextPress; @@ -940,6 +940,21 @@ static void HUDScreen_DrawChat(struct HUDScreen* s, double delta) { } } +static void HUDScreen_TabEntryAdded(void* screen, int id) { + struct HUDScreen* s = (struct HUDScreen*)screen; + if (s->showingList) PlayerListWidget_Add(&s->playerList, id); +} + +static void HUDScreen_TabEntryChanged(void* screen, int id) { + struct HUDScreen* s = (struct HUDScreen*)screen; + if (s->showingList) PlayerListWidget_Update(&s->playerList, id); +} + +static void HUDScreen_TabEntryRemoved(void* screen, int id) { + struct HUDScreen* s = (struct HUDScreen*)screen; + if (s->showingList) PlayerListWidget_Remove(&s->playerList, id); +} + static void HUDScreen_ContextLost(void* screen) { struct HUDScreen* s = (struct HUDScreen*)screen; Font_Free(&s->playerFont); @@ -953,14 +968,11 @@ static void HUDScreen_ContextLost(void* screen) { Elem_TryFree(&s->clientStatus); Elem_TryFree(&s->announcement); - s->wasShowingList = s->showingList; - if (s->showingList) { Elem_TryFree(&s->playerList); } - s->showingList = false; + if (s->showingList) Elem_Free(&s->playerList); } static void HUDScreen_RemakePlayerList(struct HUDScreen* s) { bool extended = Server.SupportsExtPlayerList && !Gui_ClassicTabList; - if (!s->wasShowingList) return; PlayerListWidget_Create(&s->playerList, &s->playerFont, !extended); s->showingList = true; @@ -977,7 +989,7 @@ static void HUDScreen_ContextRecreated(void* screen) { HUDScreen_Redraw(s); Widget_Reposition(&s->hotbar); HUDScreen_ChatUpdateLayout(s); - HUDScreen_RemakePlayerList(s); + if (s->showingList) HUDScreen_RemakePlayerList(s); } static void HUDScreen_OnResize(void* screen) { @@ -986,7 +998,7 @@ static void HUDScreen_OnResize(void* screen) { if (HUDScreen_ChatUpdateFont(s)) HUDScreen_Redraw(s); HUDScreen_ChatUpdateLayout(s); - if (s->showingList) { Widget_Reposition(&s->playerList); } + if (s->showingList) Widget_Reposition(&s->playerList); } static bool HUDScreen_KeyPress(void* screen, char keyChar) { @@ -1011,7 +1023,6 @@ static bool HUDScreen_KeyDown(void* screen, Key key) { if (key == playerListKey && handlesList) { if (!s->showingList && !Server.IsSinglePlayer) { - s->wasShowingList = true; HUDScreen_RemakePlayerList(s); } return true; @@ -1054,9 +1065,8 @@ static bool HUDScreen_KeyDown(void* screen, Key key) { static bool HUDScreen_KeyUp(void* screen, Key key) { struct HUDScreen* s = (struct HUDScreen*)screen; if (key == KeyBinds[KEYBIND_PLAYER_LIST] && s->showingList) { - s->showingList = false; - s->wasShowingList = false; - Elem_TryFree(&s->playerList); + s->showingList = false; + Elem_Free(&s->playerList); return true; } @@ -1095,7 +1105,7 @@ static bool HUDScreen_PointerDown(void* screen, int id, int x, int y) { /* TODO: Move to PlayerListWidget */ if (s->showingList) { String_InitArray(text, textBuffer); - PlayerListWidget_GetNameUnder(&s->playerList, x, y, &text); + PlayerListWidget_GetNameAt(&s->playerList, x, y, &text); if (text.length) { String_Append(&text, ' '); @@ -1134,12 +1144,14 @@ static bool HUDScreen_PointerDown(void* screen, int id, int x, int y) { static void HUDScreen_Init(void* screen) { struct HUDScreen* s = (struct HUDScreen*)screen; - s->wasShowingList = false; HotbarWidget_Create(&s->hotbar); HUDScreen_ChatInit(s); Event_RegisterChat(&ChatEvents.ChatReceived, s, HUDScreen_ChatReceived); Event_RegisterInt(&ChatEvents.ColCodeChanged, s, HUDScreen_ColCodeChanged); + Event_RegisterInt(&TabListEvents.Added, s, HUDScreen_TabEntryAdded); + Event_RegisterInt(&TabListEvents.Changed, s, HUDScreen_TabEntryChanged); + Event_RegisterInt(&TabListEvents.Removed, s, HUDScreen_TabEntryRemoved); } static void HUDScreen_Render(void* screen, double delta) { @@ -1172,8 +1184,8 @@ static void HUDScreen_Render(void* screen, double delta) { Elem_Render(&s->playerList, delta); /* NOTE: Should usually be caught by KeyUp, but just in case. */ if (!KeyBind_IsPressed(KEYBIND_PLAYER_LIST)) { - Elem_TryFree(&s->playerList); s->showingList = false; + Elem_Free(&s->playerList); } } Gfx_SetTexturing(false); @@ -1181,8 +1193,13 @@ static void HUDScreen_Render(void* screen, double delta) { static void HUDScreen_Free(void* screen) { struct HUDScreen* s = (struct HUDScreen*)screen; + s->showingList = false; + Event_UnregisterChat(&ChatEvents.ChatReceived, s, HUDScreen_ChatReceived); Event_UnregisterInt(&ChatEvents.ColCodeChanged, s, HUDScreen_ColCodeChanged); + Event_UnregisterInt(&TabListEvents.Added, s, HUDScreen_TabEntryAdded); + Event_UnregisterInt(&TabListEvents.Changed, s, HUDScreen_TabEntryChanged); + Event_UnregisterInt(&TabListEvents.Removed, s, HUDScreen_TabEntryRemoved); } static const struct ScreenVTABLE HUDScreen_VTABLE = { @@ -1192,8 +1209,7 @@ static const struct ScreenVTABLE HUDScreen_VTABLE = { HUDScreen_OnResize, HUDScreen_ContextLost, HUDScreen_ContextRecreated }; void HUDScreen_Show(void) { - struct HUDScreen* s = &HUDScreen_Instance; - s->wasShowingList = false; + struct HUDScreen* s = &HUDScreen_Instance; s->lastDownloadStatus = Int32_MinValue; s->VTABLE = &HUDScreen_VTABLE; diff --git a/src/Widgets.c b/src/Widgets.c index dbc62b7f2..a6e3ec0fc 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -1750,27 +1750,20 @@ static void PlayerListWidget_DrawName(struct Texture* tex, struct PlayerListWidg Drawer2D_ReducePadding_Tex(tex, w->font->size, 3); } -static int PlayerListWidget_HighlightedName(struct PlayerListWidget* w, int x, int y) { +void PlayerListWidget_GetNameAt(struct PlayerListWidget* w, int x, int y, String* name) { struct Texture tex; + String player; int i; - if (!w->active) return -1; - + for (i = 0; i < w->namesCount; i++) { if (!w->textures[i].ID || w->ids[i] == GROUP_NAME_ID) continue; - tex = w->textures[i]; - if (Gui_Contains(tex.X, tex.Y, tex.Width, tex.Height, x, y)) return i; + if (!Gui_Contains(tex.X, tex.Y, tex.Width, tex.Height, x, y)) continue; + + player = TabList_UNSAFE_GetPlayer(w->ids[i]); + String_AppendString(name, &player); + return; } - return -1; -} - -void PlayerListWidget_GetNameUnder(struct PlayerListWidget* w, int x, int y, String* name) { - String player; - int i = PlayerListWidget_HighlightedName(w, x, y); - if (i == -1) return; - - player = TabList_UNSAFE_GetPlayer(w->ids[i]); - String_AppendString(name, &player); } static int PlayerListWidget_GetColumnWidth(struct PlayerListWidget* w, int column) { @@ -1980,14 +1973,12 @@ static void PlayerListWidget_SortAndReposition(struct PlayerListWidget* w) { Widget_Reposition(w); } -static void PlayerListWidget_TabEntryAdded(void* widget, int id) { - struct PlayerListWidget* w = (struct PlayerListWidget*)widget; +void PlayerListWidget_Add(struct PlayerListWidget* w, int id) { PlayerListWidget_AddName(w, id, -1); PlayerListWidget_SortAndReposition(w); } -static void PlayerListWidget_TabEntryChanged(void* widget, int id) { - struct PlayerListWidget* w = (struct PlayerListWidget*)widget; +void PlayerListWidget_Update(struct PlayerListWidget* w, int id) { struct Texture tex; int i; @@ -2002,8 +1993,7 @@ static void PlayerListWidget_TabEntryChanged(void* widget, int id) { } } -static void PlayerListWidget_TabEntryRemoved(void* widget, int id) { - struct PlayerListWidget* w = (struct PlayerListWidget*)widget; +void PlayerListWidget_Remove(struct PlayerListWidget* w, int id) { int i; for (i = 0; i < w->namesCount; i++) { if (w->ids[i] != id) continue; @@ -2026,10 +2016,6 @@ static void PlayerListWidget_Init(void* widget) { TextWidget_Make(&w->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0); TextWidget_SetConst(&w->title, "Connected players:", w->font); PlayerListWidget_SortAndReposition(w); - - Event_RegisterInt(&TabListEvents.Added, w, PlayerListWidget_TabEntryAdded); - Event_RegisterInt(&TabListEvents.Changed, w, PlayerListWidget_TabEntryChanged); - Event_RegisterInt(&TabListEvents.Removed, w, PlayerListWidget_TabEntryRemoved); } static void PlayerListWidget_Render(void* widget, double delta) { @@ -2071,9 +2057,6 @@ static void PlayerListWidget_Free(void* widget) { } Elem_TryFree(&w->title); - Event_UnregisterInt(&TabListEvents.Added, w, PlayerListWidget_TabEntryAdded); - Event_UnregisterInt(&TabListEvents.Changed, w, PlayerListWidget_TabEntryChanged); - Event_UnregisterInt(&TabListEvents.Removed, w, PlayerListWidget_TabEntryRemoved); } static const struct WidgetVTABLE PlayerListWidget_VTABLE = { diff --git a/src/Widgets.h b/src/Widgets.h index 383d18491..589641b65 100644 --- a/src/Widgets.h +++ b/src/Widgets.h @@ -246,7 +246,14 @@ struct PlayerListWidget { struct Texture textures[TABLIST_MAX_NAMES * 2]; }; CC_NOINLINE void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font, bool classic); -CC_NOINLINE void PlayerListWidget_GetNameUnder(struct PlayerListWidget* w, int mouseX, int mouseY, String* name); +/* Gets the name of the entry that contains the given coordinates. */ +void PlayerListWidget_GetNameAt(struct PlayerListWidget* w, int x, int y, String* name); +/* Adds a new entry to this widget. */ +void PlayerListWidget_Add(struct PlayerListWidget* w, int id); +/* Updates an existing entry in the given widget. */ +void PlayerListWidget_Update(struct PlayerListWidget* w, int id); +/* Removes the given entry from the given widget. */ +void PlayerListWidget_Remove(struct PlayerListWidget* w, int id); typedef void (*SpecialInputAppendFunc)(void* userData, char c);