Code cleanup

This commit is contained in:
UnknownShadow200 2020-11-13 01:19:49 +11:00
parent 37673e35e7
commit 531a4ad34d
6 changed files with 85 additions and 81 deletions

View file

@ -77,7 +77,7 @@ void Gui_ShowDefault(void) {
#endif
}
static void Gui_LoadOptions(void) {
static void LoadOptions(void) {
Gui.DefaultLines = Game_ClassicMode ? 10 : 12;
Gui.Chatlines = Options_GetInt(OPT_CHATLINES, 0, 30, Gui.DefaultLines);
Gui.ClickableChat = Options_GetBool(OPT_CLICKABLE_CHAT, true) && !Game_ClassicMode;
@ -444,6 +444,39 @@ void Screen_BuildMesh(void* screen) {
Gfx_UnlockDynamicVb(s->vb);
}
int Screen_DoPointerDown(void* screen, int id, int x, int y) {
struct Screen* s = (struct Screen*)screen;
struct Widget** widgets = s->widgets;
int i, count = s->numWidgets;
/* iterate backwards (because last elements rendered are shown over others) */
for (i = count - 1; i >= 0; i--) {
struct Widget* w = widgets[i];
if (!w || !Widget_Contains(w, x, y)) continue;
if (w->disabled) return i;
if (w->MenuClick) {
w->MenuClick(s, w);
} else {
Elem_HandlesPointerDown(w, id, x, y);
}
return i;
}
return -1;
}
int Screen_Index(void* screen, void* widget) {
struct Screen* s = (struct Screen*)screen;
struct Widget** widgets = s->widgets;
int i;
struct Widget* w = (struct Widget*)widget;
for (i = 0; i < s->numWidgets; i++) {
if (widgets[i] == w) return i;
}
return -1;
}
/*########################################################################################################################*
*------------------------------------------------------Gui component------------------------------------------------------*
@ -501,12 +534,14 @@ static void OnInit(void) {
Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost);
Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated);
Event_Register_(&InputEvents.Press, NULL, OnKeyPress);
Event_Register_(&WindowEvents.Resized, NULL, OnResize);
#ifdef CC_BUILD_TOUCH
Event_Register_(&InputEvents.TextChanged, NULL, OnTextChanged);
Gui._onscreenButtons = Options_GetInt(OPT_TOUCH_BUTTONS, 0, Int32_MaxValue, 0);
#endif
Event_Register_(&WindowEvents.Resized, NULL, OnResize);
Gui_LoadOptions();
LoadOptions();
Gui_ShowDefault();
}

View file

@ -108,6 +108,8 @@ void Screen_ContextLost(void* screen);
void Screen_CreateVb(void* screen);
struct VertexTextured* Screen_LockVb(void* screen);
void Screen_BuildMesh(void* screen);
int Screen_DoPointerDown(void* screen, int id, int x, int y);
int Screen_Index(void* screen, void* w);
typedef void (*Widget_LeftClick)(void* screen, void* widget);
struct WidgetVTABLE {

View file

@ -86,28 +86,8 @@ static void Menu_RenderBounds(void) {
Gfx_Draw2DGradient(0, 0, WindowInfo.Width, WindowInfo.Height, topCol, bottomCol);
}
static int Menu_DoPointerDown(void* screen, int id, int x, int y) {
struct Screen* s = (struct Screen*)screen;
struct Widget** widgets = s->widgets;
int i, count = s->numWidgets;
/* iterate backwards (because last elements rendered are shown over others) */
for (i = count - 1; i >= 0; i--) {
struct Widget* w = widgets[i];
if (!w || !Widget_Contains(w, x, y)) continue;
if (w->disabled) return i;
if (w->MenuClick) {
w->MenuClick(s, w);
} else {
Elem_HandlesPointerDown(w, id, x, y);
}
return i;
}
return -1;
}
int Menu_PointerDown(void* screen, int id, int x, int y) {
Menu_DoPointerDown(screen, id, x, y); return true;
Screen_DoPointerDown(screen, id, x, y); return true;
}
static int Menu_DoPointerMove(void* screen, int id, int x, int y) {
@ -139,18 +119,6 @@ int Menu_PointerMove(void* screen, int id, int x, int y) {
/*########################################################################################################################*
*------------------------------------------------------Menu utilities-----------------------------------------------------*
*#########################################################################################################################*/
static int Menu_Index(void* screen, void* widget) {
struct Screen* s = (struct Screen*)screen;
struct Widget** widgets = s->widgets;
int i;
struct Widget* w = (struct Widget*)widget;
for (i = 0; i < s->numWidgets; i++) {
if (widgets[i] == w) return i;
}
return -1;
}
static void Menu_Remove(void* screen, int i) {
struct Screen* s = (struct Screen*)screen;
struct Widget** widgets = s->widgets;
@ -339,7 +307,7 @@ CC_NOINLINE static void ListScreen_Sort(struct ListScreen* s) {
}
static cc_string ListScreen_UNSAFE_GetCur(struct ListScreen* s, void* widget) {
int i = Menu_Index(s, widget);
int i = Screen_Index(s, widget);
return ListScreen_UNSAFE_Get(s, s->currentIndex + i);
}
@ -1073,7 +1041,7 @@ static int GenLevelScreen_TextChanged(void* screen, const cc_string* str) {
static int GenLevelScreen_PointerDown(void* screen, int id, int x, int y) {
struct GenLevelScreen* s = (struct GenLevelScreen*)screen;
int i = Menu_DoPointerDown(screen, id, x, y);
int i = Screen_DoPointerDown(screen, id, x, y);
if (i == -1 || i >= 4) return true;
if (s->selected) s->selected->base.showCaret = false;
@ -1772,7 +1740,7 @@ static void KeyBindsScreen_Update(struct KeyBindsScreen* s, int i) {
static void KeyBindsScreen_OnBindingClick(void* screen, void* widget) {
struct KeyBindsScreen* s = (struct KeyBindsScreen*)screen;
int old = s->curI;
s->curI = Menu_Index(s, widget);
s->curI = Screen_Index(s, widget);
s->closable = false;
KeyBindsScreen_Update(s, s->curI);
@ -2046,7 +2014,7 @@ static int MenuInputOverlay_KeyDown(void* screen, int key) {
static int MenuInputOverlay_PointerDown(void* screen, int id, int x, int y) {
struct MenuInputOverlay* s = (struct MenuInputOverlay*)screen;
return Menu_DoPointerDown(screen, id, x, y) >= 0 || s->screenMode;
return Screen_DoPointerDown(screen, id, x, y) >= 0 || s->screenMode;
}
static int MenuInputOverlay_PointerMove(void* screen, int id, int x, int y) {
@ -2301,7 +2269,7 @@ static void MenuOptionsScreen_Bool(void* screen, void* widget) {
isOn = String_CaselessEqualsConst(&value, "ON");
value = String_FromReadonly(isOn ? "OFF" : "ON");
MenuOptionsScreen_Set(s, Menu_Index(s, btn), &value);
MenuOptionsScreen_Set(s, Screen_Index(s, btn), &value);
}
static void MenuOptionsScreen_Enum(void* screen, void* widget) {
@ -2313,7 +2281,7 @@ static void MenuOptionsScreen_Enum(void* screen, void* widget) {
const char* const* names;
int raw, count;
index = Menu_Index(s, btn);
index = Screen_Index(s, btn);
String_InitArray(value, valueBuffer);
btn->GetValue(&value);
@ -2333,7 +2301,7 @@ static void MenuOptionsScreen_Input(void* screen, void* widget) {
struct MenuInputDesc* desc;
MenuOptionsScreen_FreeExtHelp(s);
s->activeI = Menu_Index(s, btn);
s->activeI = Screen_Index(s, btn);
String_InitArray(value, valueBuffer);
btn->GetValue(&value);
@ -3463,7 +3431,7 @@ static struct Widget* texpack_widgets[8] = {
};
#define TEXPACK_MAX_VERTICES (4 * TEXTWIDGET_MAX + 4 * BUTTONWIDGET_MAX)
static cc_bool TexPackOverlay_IsAlways(void* screen, void* w) { return Menu_Index(screen, w) >= 6; }
static cc_bool TexPackOverlay_IsAlways(void* screen, void* w) { return Screen_Index(screen, w) >= 6; }
static void TexPackOverlay_YesClick(void* screen, void* widget) {
struct TexPackOverlay* s = (struct TexPackOverlay*)screen;
@ -3638,8 +3606,13 @@ static struct Widget* touchOnscreen_widgets[1 + ONSCREEN_MAX_BTNS] = {
#define TOUCHONSCREEN_MAX_VERTICES (BUTTONWIDGET_MAX + ONSCREEN_MAX_BTNS * BUTTONWIDGET_MAX)
static void TouchOnscreen_Any(void* s, void* w) {
int index = Menu_Index(s, w) - 1;
Gui._onscreenButtons |= (1 << index);
int bit = 1 << (Screen_Index(s, w) - 1);
if (Gui._onscreenButtons & bit) {
Gui._onscreenButtons &= ~bit;
} else {
Gui._onscreenButtons |= bit;
}
Options_SetInt(OPT_TOUCH_BUTTONS, Gui._onscreenButtons);
}
static void TouchOnscreen_More(void* s, void* w) { TouchCtrlsScreen_Show(); }

View file

@ -68,6 +68,7 @@
#define OPT_MAX_CHUNK_UPDATES "gfx-maxchunkupdates"
#define OPT_CAMERA_MASS "cameramass"
#define OPT_GRAB_CURSOR "win-grab-cursor"
#define OPT_TOUCH_BUTTONS "gui-touchbuttons"
struct StringsBuffer;
extern struct StringsBuffer Options;

View file

@ -1911,21 +1911,25 @@ static struct TouchScreen {
} TouchScreen;
static struct Widget* touch_widgets[1 + TOUCH_MAX_BTNS] = {
(struct Widget*)&TouchScreen.thumbstick, (struct Widget*)&TouchScreen.btns[0],
(struct Widget*)&TouchScreen.btns[1], (struct Widget*)&TouchScreen.btns[2]
NULL,NULL,NULL, (struct Widget*)&TouchScreen.thumbstick
};
#define TOUCH_MAX_VERTICES (THUMBSTICKWIDGET_MAX + TOUCH_MAX_BTNS * BUTTONWIDGET_MAX)
static void TouchScreen_MoreClick(void* s, void* w) { TouchMoreScreen_Show(); }
static void TouchScreen_BindClick(void* screen, void* widget) {
struct TouchScreen* s = (struct TouchScreen*)screen;
int i = Screen_Index(screen, widget);
Input_SetPressed(KeyBinds[s->descs[i].bind], true);
}
static const struct TouchBindDesc normDescs[2] = {
{ "More", KEYBIND_COUNT, 100, 50, 90, TouchScreen_MoreClick },
{ "Jump", KEYBIND_JUMP, 100, 50, 10, NULL }
{ "Jump", KEYBIND_JUMP, 100, 50, 10, TouchScreen_BindClick }
};
static const struct TouchBindDesc hackDescs[3] = {
{ "More", KEYBIND_COUNT, 100, 50, 130, TouchScreen_MoreClick },
{ "Up", KEYBIND_FLY_UP, 100, 50, 50, NULL },
{ "Down", KEYBIND_FLY_DOWN, 100, 50, 10, NULL }
{ "Up", KEYBIND_FLY_UP, 100, 50, 50, TouchScreen_BindClick },
{ "Down", KEYBIND_FLY_DOWN, 100, 50, 10, TouchScreen_BindClick }
};
static void TouchScreen_InitButtons(struct TouchScreen* s) {
@ -1940,12 +1944,13 @@ static void TouchScreen_InitButtons(struct TouchScreen* s) {
s->descs = normDescs;
s->numDescs = Array_Elems(normDescs);
}
s->numWidgets = 1 + s->numDescs;
for (i = 0; i < s->numDescs; i++) {
s->widgets[i] = (struct Widget*)&s->btns[i];
desc = &s->descs[i];
ButtonWidget_Init(&s->btns[i], desc->width, desc->OnClick);
}
for (; i < TOUCH_MAX_BTNS; i++) s->widgets[i] = NULL;
}
static void TouchScreen_HacksChanged(void* screen) {
@ -1990,22 +1995,9 @@ static int TouchScreen_PointerDown(void* screen, int id, int x, int y) {
//Chat_Add1("POINTER DOWN: %i", &id);
if (Gui_GetInputGrab()) return false;
if (Widget_Contains(&s->thumbstick, x, y)) {
s->thumbstick.active |= id; return true;
}
for (i = 0; i < s->numDescs; i++) {
if (!Widget_Contains(&s->btns[i], x, y)) continue;
if (s->descs[i].bind < KEYBIND_COUNT) {
Input_SetPressed(KeyBinds[s->descs[i].bind], true);
} else {
s->btns[i].MenuClick(screen, &s->btns[i]);
}
s->btns[i].active |= id;
return true;
}
return false;
i = Screen_DoPointerDown(screen, id, x, y);
if (i >= 0) s->widgets[i]->active |= id;
return i >= 0;
}
static int TouchScreen_PointerUp(void* screen, int id, int x, int y) {
@ -2035,6 +2027,7 @@ static void TouchScreen_Init(void* screen) {
struct TouchScreen* s = (struct TouchScreen*)screen;
s->widgets = touch_widgets;
s->numWidgets = Array_Elems(touch_widgets);
s->maxVertices = TOUCH_MAX_VERTICES;
Event_Register_(&UserEvents.HacksStateChanged, screen, TouchScreen_HacksChanged);

View file

@ -1467,19 +1467,19 @@ const struct MenuInputVTABLE StringInput_VTABLE = {
/*########################################################################################################################*
*-----------------------------------------------------TextInputWidget-----------------------------------------------------*
*#########################################################################################################################*/
static void MenuInputWidget_Render(void* widget, double delta) {
static void TextInputWidget_Render(void* widget, double delta) {
struct InputWidget* w = (struct InputWidget*)widget;
Texture_Render(&w->inputTex);
InputWidget_RenderCaret(w, delta);
}
static void MenuInputWidget_BuildMesh(void* widget, struct VertexTextured** vertices) {
static void TextInputWidget_BuildMesh(void* widget, struct VertexTextured** vertices) {
struct InputWidget* w = (struct InputWidget*)widget;
Gfx_Make2DQuad(&w->inputTex, PACKEDCOL_WHITE, vertices);
Gfx_Make2DQuad(&w->caretTex, w->caretCol, vertices);
}
static int MenuInputWidget_Render2(void* widget, int offset) {
static int TextInputWidget_Render2(void* widget, int offset) {
struct InputWidget* w = (struct InputWidget*)widget;
Gfx_BindTexture(w->inputTex.ID);
Gfx_DrawVb_IndexedTris_Range(4, offset);
@ -1492,7 +1492,7 @@ static int MenuInputWidget_Render2(void* widget, int offset) {
return offset + 4;
}
static void MenuInputWidget_RemakeTexture(void* widget) {
static void TextInputWidget_RemakeTexture(void* widget) {
cc_string range; char rangeBuffer[STRING_SIZE];
struct TextInputWidget* w = (struct TextInputWidget*)widget;
PackedCol backCol = PackedCol_Make(30, 30, 30, 200);
@ -1541,7 +1541,7 @@ static void MenuInputWidget_RemakeTexture(void* widget) {
tex->X = w->base.x; tex->Y = w->base.y;
}
static cc_bool MenuInputWidget_AllowedChar(void* widget, char c) {
static cc_bool TextInputWidget_AllowedChar(void* widget, char c) {
struct InputWidget* w = (struct InputWidget*)widget;
struct MenuInputDesc* desc;
int maxChars;
@ -1561,16 +1561,16 @@ static cc_bool MenuInputWidget_AllowedChar(void* widget, char c) {
return valid;
}
static int MenuInputWidget_GetMaxLines(void) { return 1; }
static const struct WidgetVTABLE MenuInputWidget_VTABLE = {
MenuInputWidget_Render, InputWidget_Free, InputWidget_Reposition,
static int TextInputWidget_GetMaxLines(void) { return 1; }
static const struct WidgetVTABLE TextInputWidget_VTABLE = {
TextInputWidget_Render, InputWidget_Free, InputWidget_Reposition,
InputWidget_KeyDown, InputWidget_KeyUp, Widget_MouseScroll,
InputWidget_PointerDown, Widget_Pointer, Widget_PointerMove,
MenuInputWidget_BuildMesh, MenuInputWidget_Render2
TextInputWidget_BuildMesh, TextInputWidget_Render2
};
void TextInputWidget_Create(struct TextInputWidget* w, int width, const cc_string* text, struct MenuInputDesc* desc) {
InputWidget_Reset(&w->base);
w->base.VTABLE = &MenuInputWidget_VTABLE;
w->base.VTABLE = &TextInputWidget_VTABLE;
w->minWidth = Display_ScaleX(width);
w->minHeight = Display_ScaleY(30);
@ -1580,10 +1580,10 @@ void TextInputWidget_Create(struct TextInputWidget* w, int width, const cc_strin
w->base.padding = 3;
w->base.showCaret = true;
w->base.GetMaxLines = MenuInputWidget_GetMaxLines;
w->base.RemakeTexture = MenuInputWidget_RemakeTexture;
w->base.GetMaxLines = TextInputWidget_GetMaxLines;
w->base.RemakeTexture = TextInputWidget_RemakeTexture;
w->base.OnPressedEnter = InputWidget_OnPressedEnter;
w->base.AllowedChar = MenuInputWidget_AllowedChar;
w->base.AllowedChar = TextInputWidget_AllowedChar;
String_InitArray(w->base.text, w->_textBuffer);
String_Copy(&w->base.text, text);