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);
struct WidgetVTABLE {
void (*Init)(void* elem);
void (*Render)(void* elem, double delta);
void (*Free)(void* elem);
void (*Reposition)(void* elem);
bool (*HandlesKeyDown)(void* elem, Key key);
bool (*HandlesKeyUp)(void* elem, Key key);
bool (*HandlesMouseScroll)(void* elem, float delta);
bool (*HandlesPointerDown)(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);
void (*Reposition)(void* elem);
};
#define Widget_Layout const struct WidgetVTABLE* VTABLE; \
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_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_Free(elem) (elem)->VTABLE->Free(elem)
#define Elem_HandlesKeyPress(elem, key) (elem)->VTABLE->HandlesKeyPress(elem, key)
#define Elem_HandlesKeyDown(elem, key) (elem)->VTABLE->HandlesKeyDown(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_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)

View File

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

View File

@ -45,10 +45,9 @@ static void TextWidget_Reposition(void* widget) {
}
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_Pointer, Widget_Pointer, Widget_PointerMove,
TextWidget_Reposition
Widget_Pointer, Widget_Pointer, Widget_PointerMove
};
void TextWidget_Make(struct TextWidget* w, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset) {
PackedCol col = PACKEDCOL_WHITE;
@ -143,10 +142,9 @@ static void ButtonWidget_Render(void* widget, double delta) {
}
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_Pointer, Widget_Pointer, Widget_PointerMove,
ButtonWidget_Reposition
Widget_Pointer, Widget_Pointer, Widget_PointerMove
};
void ButtonWidget_Make(struct ButtonWidget* w, int minWidth, Widget_LeftClick onClick, cc_uint8 horAnchor, cc_uint8 verAnchor, int xOffset, int yOffset) {
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 = {
Widget_NullFunc, ScrollbarWidget_Render, Widget_NullFunc,
ScrollbarWidget_Render, Widget_NullFunc, Widget_CalcPosition,
Widget_Key, Widget_Key, ScrollbarWidget_MouseScroll,
ScrollbarWidget_PointerDown, ScrollbarWidget_PointerUp, ScrollbarWidget_PointerMove,
Widget_CalcPosition
ScrollbarWidget_PointerDown, ScrollbarWidget_PointerUp, ScrollbarWidget_PointerMove
};
void ScrollbarWidget_Create(struct ScrollbarWidget* w) {
Widget_Reset(w);
@ -475,10 +472,9 @@ static bool HotbarWidget_MouseScroll(void* widget, float delta) {
}
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_PointerDown, Widget_Pointer, Widget_PointerMove,
HotbarWidget_Reposition
HotbarWidget_PointerDown, Widget_Pointer, Widget_PointerMove
};
void HotbarWidget_Create(struct HotbarWidget* w) {
Widget_Reset(w);
@ -790,10 +786,9 @@ static bool TableWidget_KeyDown(void* widget, Key key) {
}
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_PointerDown, TableWidget_PointerUp, TableWidget_PointerMove,
TableWidget_Reposition
TableWidget_PointerDown, TableWidget_PointerUp, TableWidget_PointerMove
};
void TableWidget_Create(struct TableWidget* w) {
Widget_Reset(w);
@ -1429,10 +1424,9 @@ static bool MenuInputWidget_AllowedChar(void* widget, char c) {
static int MenuInputWidget_GetMaxLines(void) { return 1; }
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_PointerDown, Widget_Pointer, Widget_PointerMove,
InputWidget_Reposition
InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove
};
void MenuInputWidget_Create(struct MenuInputWidget* w, int width, int height, const String* text, struct MenuInputDesc* desc) {
Widget_Reset(w);
@ -1692,10 +1686,9 @@ static int ChatInputWidget_GetMaxLines(void) {
}
static const struct WidgetVTABLE ChatInputWidget_VTABLE = {
Widget_NullFunc, ChatInputWidget_Render, InputWidget_Free,
ChatInputWidget_Render, InputWidget_Free, InputWidget_Reposition,
ChatInputWidget_KeyDown, InputWidget_KeyUp, Widget_MouseScroll,
InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove,
InputWidget_Reposition
InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove
};
void ChatInputWidget_Create(struct ChatInputWidget* 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) {
struct PlayerListWidget* w = (struct PlayerListWidget*)widget;
struct TextWidget* title = &w->title;
@ -2060,13 +2039,14 @@ static void PlayerListWidget_Free(void* widget) {
}
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_Pointer, Widget_Pointer, Widget_PointerMove,
PlayerListWidget_Reposition
Widget_Pointer, Widget_Pointer, Widget_PointerMove
};
void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font, bool classic) {
int id;
Widget_Reset(w);
w->VTABLE = &PlayerListWidget_VTABLE;
w->horAnchor = ANCHOR_CENTRE;
w->verAnchor = ANCHOR_CENTRE;
@ -2075,6 +2055,15 @@ void PlayerListWidget_Create(struct PlayerListWidget* w, struct FontDesc* font,
w->font = font;
w->classic = classic;
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 = {
Widget_NullFunc, TextGroupWidget_Render, TextGroupWidget_Free,
TextGroupWidget_Render, TextGroupWidget_Free, TextGroupWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll,
Widget_Pointer, Widget_Pointer, Widget_PointerMove,
TextGroupWidget_Reposition
Widget_Pointer, Widget_Pointer, Widget_PointerMove
};
void TextGroupWidget_Create(struct TextGroupWidget* w, int lines, struct Texture* textures, TextGroupWidget_Get getLine) {
Widget_Reset(w);
@ -2706,10 +2694,9 @@ void SpecialInputWidget_SetActive(struct SpecialInputWidget* w, bool active) {
}
static const struct WidgetVTABLE SpecialInputWidget_VTABLE = {
Widget_NullFunc, SpecialInputWidget_Render, SpecialInputWidget_Free,
SpecialInputWidget_Render, SpecialInputWidget_Free, SpecialInputWidget_Reposition,
Widget_Key, Widget_Key, Widget_MouseScroll,
SpecialInputWidget_PointerDown, Widget_Pointer, Widget_PointerMove,
SpecialInputWidget_Reposition
SpecialInputWidget_PointerDown, Widget_Pointer, Widget_PointerMove
};
void SpecialInputWidget_Create(struct SpecialInputWidget* w, struct FontDesc* font, struct InputWidget* target) {
Widget_Reset(w);

View File

@ -245,7 +245,8 @@ struct PlayerListWidget {
cc_uint16 ids[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. */
void PlayerListWidget_GetNameAt(struct PlayerListWidget* w, int x, int y, String* name);
/* Adds a new entry to this widget. */