From fc27a37a77cc8da9552479af49ec5acd5b54fc0f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 27 Oct 2019 22:10:11 +1100 Subject: [PATCH] Redesign the touch controls a bit. Now there's just 4 directions and 3 buttons - jump (obvious), mode (toggles between place/delete), more (opens another menu that lets you touch for chat/inventory/fullscreen/speed/noclip/fly Still a WIP though --- src/Gui.h | 13 +++--- src/Menus.c | 111 ++++++++++++++++++++++++++++++++++++++++---------- src/Menus.h | 3 ++ src/Screens.c | 33 ++++++++++----- 4 files changed, 121 insertions(+), 39 deletions(-) diff --git a/src/Gui.h b/src/Gui.h index 6083f6f6d..0d3a832b4 100644 --- a/src/Gui.h +++ b/src/Gui.h @@ -93,12 +93,13 @@ extern GfxResourceID Gui_GuiTex, Gui_GuiClassicTex, Gui_IconsTex; /* Higher priority handles input first and draws on top */ enum GuiPriority { - GUI_PRIORITY_DISCONNECT = 55, - GUI_PRIORITY_OLDLOADING = 50, - GUI_PRIORITY_URLWARNING = 45, - GUI_PRIORITY_TEXPACK = 40, - GUI_PRIORITY_TEXIDS = 35, - GUI_PRIORITY_MENU = 30, + GUI_PRIORITY_DISCONNECT = 60, + GUI_PRIORITY_OLDLOADING = 55, + GUI_PRIORITY_URLWARNING = 50, + GUI_PRIORITY_TEXPACK = 45, + GUI_PRIORITY_TEXIDS = 40, + GUI_PRIORITY_MENU = 35, + GUI_PRIORITY_TOUCHMORE = 30, GUI_PRIORITY_TOUCH = 25, GUI_PRIORITY_INVENTORY = 20, GUI_PRIORITY_STATUS = 15, diff --git a/src/Menus.c b/src/Menus.c index 071bfd594..e941bfff2 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -47,6 +47,21 @@ static void Menu_Button(void* s, int i, struct ButtonWidget* btn, int width, Wid ((struct Screen*)s)->widgets[i] = (struct Widget*)btn; } +static void Menu_Buttons(void* s, struct ButtonWidget* btns, int width, struct SimpleButtonDesc* descs, int count) { + int i; + for (i = 0; i < count; i++) { + Menu_Button(s, i, &btns[i], width, descs[i].onClick, + ANCHOR_CENTRE, ANCHOR_CENTRE, descs[i].x, descs[i].y); + } +} + +static void Menu_SetButtons(struct ButtonWidget* btns, struct FontDesc* font, struct SimpleButtonDesc* descs, int count) { + int i; + for (i = 0; i < count; i++) { + ButtonWidget_SetConst(&btns[i], descs[i].title, font); + } +} + static void Menu_Label(void* s, int i, struct TextWidget* label, int horAnchor, int verAnchor, int x, int y) { TextWidget_Make(label, horAnchor, verAnchor, x, y); ((struct Screen*)s)->widgets[i] = (struct Widget*)label; @@ -480,12 +495,9 @@ static void PauseScreen_CheckHacksAllowed(void* screen) { static void PauseScreen_ContextRecreated(void* screen) { struct PauseScreen* s = (struct PauseScreen*)screen; struct FontDesc titleFont; - int i; Menu_MakeTitleFont(&titleFont); - for (i = 0; i < s->numWidgets - 2; i++) { - ButtonWidget_SetConst(&s->buttons[i], s->descs[i].title, &titleFont); - } + Menu_SetButtons(&s->buttons, &titleFont, s->descs, s->numWidgets - 2); if (!Gui_ClassicMenu) ButtonWidget_SetConst(&s->quit, "Quit game", &titleFont); ButtonWidget_SetConst(&s->back, "Back to game", &titleFont); @@ -501,7 +513,7 @@ static void PauseScreen_ContextRecreated(void* screen) { static void PauseScreen_Init(void* screen) { static struct Widget* widgets[8]; struct PauseScreen* s = (struct PauseScreen*)screen; - int i, count, width; + int count, width; static const struct SimpleButtonDesc classicDescs[5] = { { 0, -100, "Options...", Menu_SwitchClassicOptions }, @@ -533,11 +545,7 @@ static void PauseScreen_Init(void* screen) { s->numWidgets = count + 2; width = Gui_ClassicMenu ? 400 : 300; - - for (i = 0; i < count; i++) { - Menu_Button(s, i, &s->buttons[i], width, s->descs[i].onClick, - ANCHOR_CENTRE, ANCHOR_CENTRE, s->descs[i].x, s->descs[i].y); - } + Menu_Buttons(s, &s->buttons, width, s->descs, count); Menu_Button(s, count, &s->quit, 120, PauseScreen_Quit, ANCHOR_MAX, ANCHOR_MAX, 5, 5); @@ -613,13 +621,10 @@ static void OptionsGroupScreen_ContextLost(void* screen) { static void OptionsGroupScreen_ContextRecreated(void* screen) { struct OptionsGroupScreen* s = (struct OptionsGroupScreen*)screen; struct FontDesc titleFont; - int i; Menu_MakeTitleFont(&titleFont); Menu_MakeBodyFont(&s->textFont); - for (i = 0; i < Array_Elems(optsGroup_btns); i++) { - ButtonWidget_SetConst(&s->buttons[i], optsGroup_btns[i].title, &titleFont); - } + Menu_SetButtons(&s->buttons, &titleFont, optsGroup_btns, 7); ButtonWidget_SetConst(&s->done, "Done", &titleFont); if (s->selectedI >= 0) OptionsGroupScreen_UpdateDesc(s); @@ -637,14 +642,9 @@ static void OptionsGroupScreen_Init(void* screen) { s->numWidgets = Array_Elems(widgets); s->selectedI = -1; - for (i = 0; i < Array_Elems(optsGroup_btns); i++) { - Menu_Button(s, i, &s->buttons[i], 300, optsGroup_btns[i].onClick, - ANCHOR_CENTRE, ANCHOR_CENTRE, optsGroup_btns[i].x, optsGroup_btns[i].y); - } - - Menu_Label(s, 7, &s->desc, - ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 100); - Menu_Back(s, 8, &s->done, Menu_SwitchPause); + Menu_Buttons(s, &s->buttons, 300, optsGroup_btns, 7); + Menu_Label(s, 7, &s->desc, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 100); + Menu_Back(s, 8, &s->done, Menu_SwitchPause); } static void OptionsGroupScreen_Free(void* screen) { @@ -3250,3 +3250,70 @@ void TexPackOverlay_Show(const String* url) { Http_AsyncGetHeaders(url, true, &s->identifier); Gui_Replace((struct Screen*)s, GUI_PRIORITY_TEXPACK); } + + +#ifdef CC_BUILD_TOUCH +/*########################################################################################################################* +*----------------------------------------------------UrlWarningOverlay----------------------------------------------------* +*#########################################################################################################################*/ +static struct TouchMoreOverlay { + Screen_Layout + struct ButtonWidget buttons[6]; +} TouchMoreOverlay_Instance; + +static void TouchMore_Toggle(KeyBind bind) { + Key key = KeyBinds[bind]; + Gui_Remove((struct Screen*)&TouchMoreOverlay_Instance); + Input_SetPressed(key, !Input_Pressed[key]); +} + +static void TouchMore_Chat(void* s, void* w) { TouchMore_Toggle(KEYBIND_CHAT); } +static void TouchMore_Speed(void* s, void* w) { TouchMore_Toggle(KEYBIND_SPEED); } +static void TouchMore_Fly(void* s, void* w) { TouchMore_Toggle(KEYBIND_FLY); } +static void TouchMore_Inv(void* s, void* w) { TouchMore_Toggle(KEYBIND_INVENTORY); } +static void TouchMore_Screen(void* s, void* w) { TouchMore_Toggle(KEYBIND_FULLSCREEN); } +static void TouchMore_Noclip(void* s, void* w) { TouchMore_Toggle(KEYBIND_NOCLIP); } + +static const struct SimpleButtonDesc touchOverlay_btns[6] = { + { -160, -50, "Chat", TouchMore_Chat }, + { -160, 0, "Speed", TouchMore_Speed }, + { -160, 50, "Fly", TouchMore_Fly }, + { 160, -50, "Inventory", TouchMore_Inv }, + { 160, 0, "Fullscreen", TouchMore_Screen }, + { 160, 50, "Noclip", TouchMore_Noclip } +}; + +static void TouchMoreOverlay_ContextRecreated(void* screen) { + struct TouchMoreOverlay* s = (struct TouchMoreOverlay*)screen; + struct FontDesc titleFont; + Menu_MakeTitleFont(&titleFont); + + Menu_SetButtons(&s->buttons, &titleFont, touchOverlay_btns, 6); + Font_Free(&titleFont); +} + +static void TouchMoreOverlay_Init(void* screen) { + static struct Widget* widgets[6]; + struct TouchMoreOverlay* s = (struct TouchMoreOverlay*)screen; + s->widgets = widgets; + s->numWidgets = Array_Elems(widgets); + + Menu_Buttons(s, &s->buttons, 300, touchOverlay_btns, 6); + /* TODO: Close button */ +} + +static const struct ScreenVTABLE TouchMoreOverlay_VTABLE = { + TouchMoreOverlay_Init, MenuScreen_Render, Menu_NullFunc, + Screen_TKey, Screen_TKey, Screen_TKeyPress, + Menu_PointerDown, Screen_TPointer, Menu_PointerMove, Screen_TMouseScroll, + Menu_OnResize, Menu_ContextLost, TouchMoreOverlay_ContextRecreated +}; +void TouchMoreOverlay_Show(void) { + struct TouchMoreOverlay* s = &TouchMoreOverlay_Instance; + s->grabsInput = true; + s->closable = true; + s->VTABLE = &TouchMoreOverlay_VTABLE; + + Gui_Replace((struct Screen*)s, GUI_PRIORITY_TOUCHMORE); +} +#endif diff --git a/src/Menus.h b/src/Menus.h index fa75feff6..43f0ca58b 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -36,4 +36,7 @@ void NostalgiaScreen_Show(void); void UrlWarningOverlay_Show(const String* url); void TexIdsOverlay_Show(void); void TexPackOverlay_Show(const String* url); +#ifdef CC_BUILD_TOUCH +void TouchMoreOverlay_Show(void); +#endif #endif diff --git a/src/Screens.c b/src/Screens.c index bfc90b9d6..cefcd0c84 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -1432,17 +1432,14 @@ static const struct TouchBindDesc { const char* text; cc_uint8 bind, width; cc_int16 xOffset, yOffset; -} touchDescs[11] = { +} touchDescs[8] = { { "<", KEYBIND_LEFT, 40, 150, 50 }, { ">", KEYBIND_RIGHT, 40, 10, 50 }, { "^", KEYBIND_FORWARD, 40, 80, 10 }, { "\\/", KEYBIND_BACK, 40, 80, 90 }, - { "Jump", KEYBIND_JUMP, 100, 50, 50 }, - { "Chat", KEYBIND_CHAT, 100, 50, 150 }, - { "Inv", KEYBIND_INVENTORY, 100, 50, 190 }, - { "Speed", KEYBIND_SPEED, 100, 50, 230 }, - { "Noclip", KEYBIND_NOCLIP, 100, 50, 270 }, - { "Fly", KEYBIND_FLY, 100, 50, 310 }, + { "Jump", KEYBIND_JUMP, 100, 50, 150 }, + { "Mode", KEYBIND_COUNT, 100, 50, 190 }, + { "More", KEYBIND_COUNT, 100, 50, 230 }, /* Chat labels */ { "Send ", KEYBIND_SEND_CHAT, 100, 50, 10 }, }; @@ -1473,6 +1470,9 @@ CC_NOINLINE static void TouchScreen_Set(struct TouchScreen* s, int i, const char s->binds[i] = bind; } +static void TouchScreen_ModeClick(void* s, void* w) { Input_Placing = !Input_Placing; } +static void TouchScreen_MoreClick(void* s, void* w) { TouchMoreOverlay_Show(); } + static void TouchScreen_ContextRecreated(void* screen) { struct TouchScreen* s = (struct TouchScreen*)screen; const struct TouchBindDesc* desc; @@ -1484,10 +1484,10 @@ static void TouchScreen_ContextRecreated(void* screen) { switch (s->layout) { case TOUCH_LAYOUT_FULL: - s->numButtons = 10; + s->numButtons = 7; break; case TOUCH_LAYOUT_CHAT: - offset = 10; + offset = 7; s->numButtons = 1; break; } @@ -1499,6 +1499,11 @@ static void TouchScreen_ContextRecreated(void* screen) { ButtonWidget_SetConst(&s->buttons[i], desc->text, &s->font); s->binds[i] = desc->bind; } + + /* TODO: Mode should display 'Place' or 'Delete' */ + /* TODO: this is pretty nasty hacky. rewrite! */ + s->buttons[5].MenuClick = TouchScreen_ModeClick; + s->buttons[6].MenuClick = TouchScreen_MoreClick; } static void TouchScreen_Render(void* screen, double delta) { @@ -1533,7 +1538,11 @@ static cc_bool TouchScreen_PointerDown(void* screen, int id, int x, int y) { for (i = 0; i < s->numButtons; i++) { if (!Widget_Contains(&s->buttons[i], x, y)) continue; - Input_SetPressed(KeyBinds[s->binds[i]], true); + if (s->binds[i] < KEYBIND_COUNT) { + Input_SetPressed(KeyBinds[s->binds[i]], true); + } else { + s->buttons[i].MenuClick(screen, &s->buttons[i]); + } s->buttons[i].active |= id; return true; } @@ -1548,7 +1557,9 @@ static cc_bool TouchScreen_PointerUp(void* screen, int id, int x, int y) { for (i = 0; i < s->numButtons; i++) { if (!(s->buttons[i].active & id)) continue; - Input_SetPressed(KeyBinds[s->binds[i]], false); + if (s->binds[i] < KEYBIND_COUNT) { + Input_SetPressed(KeyBinds[s->binds[i]], false); + } s->buttons[i].active &= ~id; return true; }