Get rid of Init method in Widgets as unnecessary now

This commit is contained in:
UnknownShadow200 2019-09-28 21:55:19 +10:00
parent c0595f7fb6
commit 5b166ee496
4 changed files with 45 additions and 63 deletions

View File

@ -61,16 +61,15 @@ struct Screen { Screen_Layout };
typedef void (*Widget_LeftClick)(void* screen, void* widget); typedef void (*Widget_LeftClick)(void* screen, void* widget);
struct WidgetVTABLE { struct WidgetVTABLE {
void (*Init)(void* elem);
void (*Render)(void* elem, double delta); void (*Render)(void* elem, double delta);
void (*Free)(void* elem); void (*Free)(void* elem);
void (*Reposition)(void* elem);
bool (*HandlesKeyDown)(void* elem, Key key); bool (*HandlesKeyDown)(void* elem, Key key);
bool (*HandlesKeyUp)(void* elem, Key key); bool (*HandlesKeyUp)(void* elem, Key key);
bool (*HandlesMouseScroll)(void* elem, float delta); bool (*HandlesMouseScroll)(void* elem, float delta);
bool (*HandlesPointerDown)(void* elem, int id, int x, int y); bool (*HandlesPointerDown)(void* elem, int id, int x, int y);
bool (*HandlesPointerUp)(void* elem, int id, int x, int y); bool (*HandlesPointerUp)(void* elem, int id, int x, int y);
bool (*HandlesPointerMove)(void* elem, int id, int x, int y); bool (*HandlesPointerMove)(void* elem, int id, int x, int y);
void (*Reposition)(void* elem);
}; };
#define Widget_Layout const struct WidgetVTABLE* VTABLE; \ #define Widget_Layout const struct WidgetVTABLE* VTABLE; \
int x, y, width, height; /* Top left corner, and dimensions, of this widget */ \ int x, y, width, height; /* Top left corner, and dimensions, of this widget */ \
@ -160,16 +159,13 @@ void TextAtlas_Free(struct TextAtlas* atlas);
void TextAtlas_Add(struct TextAtlas* atlas, int charI, VertexP3fT2fC4b** vertices); void TextAtlas_Add(struct TextAtlas* atlas, int charI, VertexP3fT2fC4b** vertices);
void TextAtlas_AddInt(struct TextAtlas* atlas, int value, VertexP3fT2fC4b** vertices); void TextAtlas_AddInt(struct TextAtlas* atlas, int value, VertexP3fT2fC4b** vertices);
#define Elem_Init(elem) (elem)->VTABLE->Init(elem)
#define Elem_Render(elem, delta) (elem)->VTABLE->Render(elem, delta) #define Elem_Render(elem, delta) (elem)->VTABLE->Render(elem, delta)
#define Elem_Free(elem) (elem)->VTABLE->Free(elem) #define Elem_Free(elem) (elem)->VTABLE->Free(elem)
#define Elem_HandlesKeyPress(elem, key) (elem)->VTABLE->HandlesKeyPress(elem, key) #define Elem_HandlesKeyPress(elem, key) (elem)->VTABLE->HandlesKeyPress(elem, key)
#define Elem_HandlesKeyDown(elem, key) (elem)->VTABLE->HandlesKeyDown(elem, key) #define Elem_HandlesKeyDown(elem, key) (elem)->VTABLE->HandlesKeyDown(elem, key)
#define Elem_HandlesKeyUp(elem, key) (elem)->VTABLE->HandlesKeyUp(elem, key) #define Elem_HandlesKeyUp(elem, key) (elem)->VTABLE->HandlesKeyUp(elem, key)
#define Elem_HandlesMouseDown(elem, x, y, btn) (elem)->VTABLE->HandlesMouseDown(elem, x, y, btn)
#define Elem_HandlesMouseScroll(elem, delta) (elem)->VTABLE->HandlesMouseScroll(elem, delta)
#define Elem_HandlesMouseScroll(elem, delta) (elem)->VTABLE->HandlesMouseScroll(elem, delta)
#define Elem_HandlesPointerDown(elem, id, x, y) (elem)->VTABLE->HandlesPointerDown(elem, id, x, y) #define Elem_HandlesPointerDown(elem, id, x, y) (elem)->VTABLE->HandlesPointerDown(elem, id, x, y)
#define Elem_HandlesPointerUp(elem, id, x, y) (elem)->VTABLE->HandlesPointerUp(elem, id, x, y) #define Elem_HandlesPointerUp(elem, id, x, y) (elem)->VTABLE->HandlesPointerUp(elem, id, x, y)
#define Elem_HandlesPointerMove(elem, id, x, y) (elem)->VTABLE->HandlesPointerMove(elem, id, x, y) #define Elem_HandlesPointerMove(elem, id, x, y) (elem)->VTABLE->HandlesPointerMove(elem, id, x, y)

View File

@ -972,11 +972,9 @@ static void HUDScreen_ContextLost(void* screen) {
} }
static void HUDScreen_RemakePlayerList(struct HUDScreen* s) { static void HUDScreen_RemakePlayerList(struct HUDScreen* s) {
bool extended = Server.SupportsExtPlayerList && !Gui_ClassicTabList; bool classic = Gui_ClassicTabList || !Server.SupportsExtPlayerList;
PlayerListWidget_Create(&s->playerList, &s->playerFont, !extended); PlayerListWidget_Create(&s->playerList, &s->playerFont, classic);
s->showingList = true; s->showingList = true;
Elem_Init(&s->playerList);
Widget_Reposition(&s->playerList); Widget_Reposition(&s->playerList);
} }

View File

@ -45,10 +45,9 @@ static void TextWidget_Reposition(void* widget) {
} }
static const struct WidgetVTABLE TextWidget_VTABLE = { static const struct WidgetVTABLE TextWidget_VTABLE = {
Widget_NullFunc, TextWidget_Render, TextWidget_Free, TextWidget_Render, TextWidget_Free, TextWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll, Widget_Key, Widget_Key, Widget_MouseScroll,
Widget_Pointer, Widget_Pointer, Widget_PointerMove, Widget_Pointer, Widget_Pointer, Widget_PointerMove
TextWidget_Reposition
}; };
void TextWidget_Make(struct TextWidget* w, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset) { void TextWidget_Make(struct TextWidget* w, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset) {
PackedCol col = PACKEDCOL_WHITE; PackedCol col = PACKEDCOL_WHITE;
@ -143,10 +142,9 @@ static void ButtonWidget_Render(void* widget, double delta) {
} }
static const struct WidgetVTABLE ButtonWidget_VTABLE = { static const struct WidgetVTABLE ButtonWidget_VTABLE = {
Widget_NullFunc, ButtonWidget_Render, ButtonWidget_Free, ButtonWidget_Render, ButtonWidget_Free, ButtonWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll, Widget_Key, Widget_Key, Widget_MouseScroll,
Widget_Pointer, Widget_Pointer, Widget_PointerMove, Widget_Pointer, Widget_Pointer, Widget_PointerMove
ButtonWidget_Reposition
}; };
void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset) { void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset) {
Widget_Reset(w); Widget_Reset(w);
@ -293,10 +291,9 @@ static bool ScrollbarWidget_PointerMove(void* widget, int id, int x, int y) {
} }
static const struct WidgetVTABLE ScrollbarWidget_VTABLE = { static const struct WidgetVTABLE ScrollbarWidget_VTABLE = {
Widget_NullFunc, ScrollbarWidget_Render, Widget_NullFunc, ScrollbarWidget_Render, Widget_NullFunc, Widget_CalcPosition,
Widget_Key, Widget_Key, ScrollbarWidget_MouseScroll, Widget_Key, Widget_Key, ScrollbarWidget_MouseScroll,
ScrollbarWidget_PointerDown, ScrollbarWidget_PointerUp, ScrollbarWidget_PointerMove, ScrollbarWidget_PointerDown, ScrollbarWidget_PointerUp, ScrollbarWidget_PointerMove
Widget_CalcPosition
}; };
void ScrollbarWidget_Create(struct ScrollbarWidget* w) { void ScrollbarWidget_Create(struct ScrollbarWidget* w) {
Widget_Reset(w); Widget_Reset(w);
@ -475,10 +472,9 @@ static bool HotbarWidget_MouseScroll(void* widget, float delta) {
} }
static const struct WidgetVTABLE HotbarWidget_VTABLE = { static const struct WidgetVTABLE HotbarWidget_VTABLE = {
Widget_NullFunc, HotbarWidget_Render, Widget_NullFunc, HotbarWidget_Render, Widget_NullFunc, HotbarWidget_Reposition,
HotbarWidget_KeyDown, HotbarWidget_KeyUp, HotbarWidget_MouseScroll, HotbarWidget_KeyDown, HotbarWidget_KeyUp, HotbarWidget_MouseScroll,
HotbarWidget_PointerDown, Widget_Pointer, Widget_PointerMove, HotbarWidget_PointerDown, Widget_Pointer, Widget_PointerMove
HotbarWidget_Reposition
}; };
void HotbarWidget_Create(struct HotbarWidget* w) { void HotbarWidget_Create(struct HotbarWidget* w) {
Widget_Reset(w); Widget_Reset(w);
@ -790,10 +786,9 @@ static bool TableWidget_KeyDown(void* widget, Key key) {
} }
static const struct WidgetVTABLE TableWidget_VTABLE = { static const struct WidgetVTABLE TableWidget_VTABLE = {
Widget_NullFunc, TableWidget_Render, TableWidget_Free, TableWidget_Render, TableWidget_Free, TableWidget_Reposition,
TableWidget_KeyDown, Widget_Key, TableWidget_MouseScroll, TableWidget_KeyDown, Widget_Key, TableWidget_MouseScroll,
TableWidget_PointerDown, TableWidget_PointerUp, TableWidget_PointerMove, TableWidget_PointerDown, TableWidget_PointerUp, TableWidget_PointerMove
TableWidget_Reposition
}; };
void TableWidget_Create(struct TableWidget* w) { void TableWidget_Create(struct TableWidget* w) {
Widget_Reset(w); Widget_Reset(w);
@ -1429,10 +1424,9 @@ static bool MenuInputWidget_AllowedChar(void* widget, char c) {
static int MenuInputWidget_GetMaxLines(void) { return 1; } static int MenuInputWidget_GetMaxLines(void) { return 1; }
static const struct WidgetVTABLE MenuInputWidget_VTABLE = { static const struct WidgetVTABLE MenuInputWidget_VTABLE = {
Widget_NullFunc, MenuInputWidget_Render, InputWidget_Free, MenuInputWidget_Render, InputWidget_Free, InputWidget_Reposition,
InputWidget_KeyDown, InputWidget_KeyUp, Widget_MouseScroll, InputWidget_KeyDown, InputWidget_KeyUp, Widget_MouseScroll,
InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove, InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove
InputWidget_Reposition
}; };
void MenuInputWidget_Create(struct MenuInputWidget* w, int width, int height, const String* text, struct MenuInputDesc* desc) { void MenuInputWidget_Create(struct MenuInputWidget* w, int width, int height, const String* text, struct MenuInputDesc* desc) {
Widget_Reset(w); Widget_Reset(w);
@ -1692,10 +1686,9 @@ static int ChatInputWidget_GetMaxLines(void) {
} }
static const struct WidgetVTABLE ChatInputWidget_VTABLE = { static const struct WidgetVTABLE ChatInputWidget_VTABLE = {
Widget_NullFunc, ChatInputWidget_Render, InputWidget_Free, ChatInputWidget_Render, InputWidget_Free, InputWidget_Reposition,
ChatInputWidget_KeyDown, InputWidget_KeyUp, Widget_MouseScroll, ChatInputWidget_KeyDown, InputWidget_KeyUp, Widget_MouseScroll,
InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove, InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove
InputWidget_Reposition
}; };
void ChatInputWidget_Create(struct ChatInputWidget* w) { void ChatInputWidget_Create(struct ChatInputWidget* w) {
Widget_Reset(w); Widget_Reset(w);
@ -2004,20 +1997,6 @@ void PlayerListWidget_Remove(struct PlayerListWidget* w, int id) {
} }
} }
static void PlayerListWidget_Init(void* widget) {
struct PlayerListWidget* w = (struct PlayerListWidget*)widget;
int id;
for (id = 0; id < TABLIST_MAX_NAMES; id++) {
if (!TabList.NameOffsets[id]) continue;
PlayerListWidget_AddName(w, (EntityID)id, -1);
}
TextWidget_Make(&w->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0);
TextWidget_SetConst(&w->title, "Connected players:", w->font);
PlayerListWidget_SortAndReposition(w);
}
static void PlayerListWidget_Render(void* widget, double delta) { static void PlayerListWidget_Render(void* widget, double delta) {
struct PlayerListWidget* w = (struct PlayerListWidget*)widget; struct PlayerListWidget* w = (struct PlayerListWidget*)widget;
struct TextWidget* title = &w->title; struct TextWidget* title = &w->title;
@ -2060,13 +2039,14 @@ static void PlayerListWidget_Free(void* widget) {
} }
static const struct WidgetVTABLE PlayerListWidget_VTABLE = { static const struct WidgetVTABLE PlayerListWidget_VTABLE = {
PlayerListWidget_Init, PlayerListWidget_Render, PlayerListWidget_Free, PlayerListWidget_Render, PlayerListWidget_Free, PlayerListWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll, Widget_Key, Widget_Key, Widget_MouseScroll,
Widget_Pointer, Widget_Pointer, Widget_PointerMove, Widget_Pointer, Widget_Pointer, Widget_PointerMove
PlayerListWidget_Reposition
}; };
void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font, bool classic) { void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font, bool classic) {
int id;
Widget_Reset(w); Widget_Reset(w);
w->VTABLE = &PlayerListWidget_VTABLE; w->VTABLE = &PlayerListWidget_VTABLE;
w->horAnchor = ANCHOR_CENTRE; w->horAnchor = ANCHOR_CENTRE;
w->verAnchor = ANCHOR_CENTRE; w->verAnchor = ANCHOR_CENTRE;
@ -2075,6 +2055,15 @@ void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font,
w->font = font; w->font = font;
w->classic = classic; w->classic = classic;
w->elementOffset = classic ? 0 : 10; w->elementOffset = classic ? 0 : 10;
for (id = 0; id < TABLIST_MAX_NAMES; id++) {
if (!TabList.NameOffsets[id]) continue;
PlayerListWidget_AddName(w, (EntityID)id, -1);
}
TextWidget_Make(&w->title, ANCHOR_CENTRE, ANCHOR_MIN, 0, 0);
TextWidget_SetConst(&w->title, "Connected players:", w->font);
PlayerListWidget_SortAndReposition(w);
} }
@ -2460,10 +2449,9 @@ static void TextGroupWidget_Free(void* widget) {
} }
static const struct WidgetVTABLE TextGroupWidget_VTABLE = { static const struct WidgetVTABLE TextGroupWidget_VTABLE = {
Widget_NullFunc, TextGroupWidget_Render, TextGroupWidget_Free, TextGroupWidget_Render, TextGroupWidget_Free, TextGroupWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll, Widget_Key, Widget_Key, Widget_MouseScroll,
Widget_Pointer, Widget_Pointer, Widget_PointerMove, Widget_Pointer, Widget_Pointer, Widget_PointerMove
TextGroupWidget_Reposition
}; };
void TextGroupWidget_Create(struct TextGroupWidget* w, int lines, struct Texture* textures, TextGroupWidget_Get getLine) { void TextGroupWidget_Create(struct TextGroupWidget* w, int lines, struct Texture* textures, TextGroupWidget_Get getLine) {
Widget_Reset(w); Widget_Reset(w);
@ -2706,10 +2694,9 @@ void SpecialInputWidget_SetActive(struct SpecialInputWidget* w, bool active) {
} }
static const struct WidgetVTABLE SpecialInputWidget_VTABLE = { static const struct WidgetVTABLE SpecialInputWidget_VTABLE = {
Widget_NullFunc, SpecialInputWidget_Render, SpecialInputWidget_Free, SpecialInputWidget_Render, SpecialInputWidget_Free, SpecialInputWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll, Widget_Key, Widget_Key, Widget_MouseScroll,
SpecialInputWidget_PointerDown, Widget_Pointer, Widget_PointerMove, SpecialInputWidget_PointerDown, Widget_Pointer, Widget_PointerMove
SpecialInputWidget_Reposition
}; };
void SpecialInputWidget_Create(struct SpecialInputWidget* w, struct FontDesc* font, struct InputWidget* target) { void SpecialInputWidget_Create(struct SpecialInputWidget* w, struct FontDesc* font, struct InputWidget* target) {
Widget_Reset(w); Widget_Reset(w);

View File

@ -245,7 +245,8 @@ struct PlayerListWidget {
cc_uint16 ids[TABLIST_MAX_NAMES * 2]; cc_uint16 ids[TABLIST_MAX_NAMES * 2];
struct Texture textures[TABLIST_MAX_NAMES * 2]; struct Texture textures[TABLIST_MAX_NAMES * 2];
}; };
CC_NOINLINE void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font, bool classic); /* Creates and adds initial names to this widget. */
void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font, bool classic);
/* Gets the name of the entry that contains the given coordinates. */ /* Gets the name of the entry that contains the given coordinates. */
void PlayerListWidget_GetNameAt(struct PlayerListWidget* w, int x, int y, String* name); void PlayerListWidget_GetNameAt(struct PlayerListWidget* w, int x, int y, String* name);
/* Adds a new entry to this widget. */ /* Adds a new entry to this widget. */