From 3e49f0f2a7221f4526d253b27a7722bf3b68b9f7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 1 Feb 2021 22:15:29 +1100 Subject: [PATCH] Webclient: WIP on properly supporting touchscreen input When touch mode is off and a touchstart event is received, switch to touch mode. When touch mode is on and a mousedown event is received, disable touch mode. Unfortunately this still has a problem where touch mode will always get disabled if you touch while the keyboard is open. (because the game does NOT prevent the default behaviour of touchstart becoming a mousedown event when the keyboard is open, so that you can still click on the caret in text input box) --- src/Event.c | 1 + src/Event.h | 1 + src/Gui.c | 18 +++++++++++++++--- src/Input.c | 8 ++++++++ src/Screens.c | 4 ++-- src/Screens.h | 1 + 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Event.c b/src/Event.c index 2cc3fc060..4c54e36b5 100644 --- a/src/Event.c +++ b/src/Event.c @@ -97,6 +97,7 @@ void Event_UnregisterAll(void) { InputEvents.Up.Count = 0; InputEvents.Wheel.Count = 0; InputEvents.TextChanged.Count = 0; + InputEvents.ModeChanged.Count = 0; PointerEvents.Moved.Count = 0; PointerEvents.Down.Count = 0; diff --git a/src/Event.h b/src/Event.h index 1b4c34c6b..3f377ead2 100644 --- a/src/Event.h +++ b/src/Event.h @@ -164,6 +164,7 @@ CC_VAR extern struct _InputEventsList { struct Event_Int Up; /* Key or button is released. Arg is a member of Key enumeration */ struct Event_Float Wheel; /* Mouse wheel is moved/scrolled (Arg is wheel delta) */ struct Event_String TextChanged; /* Text in the on-screen input keyboard changed (for Mobile) */ + struct Event_Void ModeChanged; /* Input_TouchMode has changed */ } InputEvents; CC_VAR extern struct _PointerEventsList { diff --git a/src/Gui.c b/src/Gui.c index cb03a08f0..be399509f 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -71,12 +71,22 @@ int Gui_ContainsPointers(int x, int y, int width, int height) { return false; } +#ifdef CC_BUILD_TOUCH +static void UpdateTouchMenu(void) { + if (Input_TouchMode) { + TouchScreen_Show(); + } else { + TouchScreen_Remove(); + } +} +#else +static void UpdateTouchMenu(void) { } +#endif + void Gui_ShowDefault(void) { HUDScreen_Show(); ChatScreen_Show(); -#ifdef CC_BUILD_TOUCH - TouchScreen_Show(); -#endif + UpdateTouchMenu(); } static void LoadOptions(void) { @@ -536,6 +546,7 @@ static void OnTextChanged(void* obj, const cc_string* str) { if (s->VTABLE->HandlesTextChanged(s, str)) return; } } +static void OnModeChanged(void* obj) { UpdateTouchMenu(); } #endif static void OnContextLost(void* obj) { @@ -559,6 +570,7 @@ static void OnInit(void) { #ifdef CC_BUILD_TOUCH Event_Register_(&InputEvents.TextChanged, NULL, OnTextChanged); + Event_Register_(&InputEvents.ModeChanged, NULL, OnModeChanged); Gui._onscreenButtons = Options_GetInt(OPT_TOUCH_BUTTONS, 0, Int32_MaxValue, 0); #endif diff --git a/src/Input.c b/src/Input.c index 1ce1485b8..b4286bb3b 100644 --- a/src/Input.c +++ b/src/Input.c @@ -78,10 +78,13 @@ void Input_SetTouchMode(cc_bool enabled) { ClearTouches(); Input_TouchMode = enabled; Pointers_Count = enabled ? 0 : 1; + Event_RaiseVoid(&InputEvents.ModeChanged); } void Input_AddTouch(long id, int x, int y) { int i; + if (!Input_TouchMode) Input_SetTouchMode(true); + for (i = 0; i < INPUT_MAX_POINTERS; i++) { if (touches[i].type) continue; @@ -232,6 +235,11 @@ void Input_SetPressed(int key) { /* don't allow multiple left mouse down events */ if (key != KEY_LMOUSE || wasPressed) return; + +#ifdef CC_BUILD_TOUCH + /* If touch mode, disable it since using mouse now */ + if (Input_TouchMode) Input_SetTouchMode(false); +#endif Pointer_SetPressed(0, true); } diff --git a/src/Screens.c b/src/Screens.c index 5bc1479e7..e6c81e2b5 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -2186,8 +2186,8 @@ static const struct ScreenVTABLE TouchScreen_VTABLE = { void TouchScreen_Show(void) { struct TouchScreen* s = &TouchScreen; s->VTABLE = &TouchScreen_VTABLE; - - if (!Input_TouchMode) return; Gui_Add((struct Screen*)s, GUI_PRIORITY_TOUCH); } + +void TouchScreen_Remove(void) { Gui_Remove((struct Screen*)&TouchScreen); } #endif diff --git a/src/Screens.h b/src/Screens.h index 877e76a2c..10b3559fe 100644 --- a/src/Screens.h +++ b/src/Screens.h @@ -32,6 +32,7 @@ void DisconnectScreen_Show(const cc_string* title, const cc_string* message); #ifdef CC_BUILD_TOUCH void TouchScreen_Refresh(void); void TouchScreen_Show(void); +void TouchScreen_Remove(void); #endif /* Opens chat input for the HUD with the given initial text. */