Skip to content

Commit 0a05083

Browse files
authored
Merge pull request #2030 from Semphriss/mobile_control_improvements
Improved management of mobile controls + made them available on all platforms
2 parents 2ef42e8 + 3482e4d commit 0a05083

File tree

10 files changed

+104
-62
lines changed

10 files changed

+104
-62
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,13 @@ endif(WIN32)
152152
option(UBUNTU_TOUCH "Compile the project for an Ubuntu Touch target" OFF)
153153

154154
# Mobile builds
155+
option(ENABLE_TOUCHSCREEN_SUPPORT "Enable on-screen controls and event detection for touchscreen devices" ON)
155156
if (UBUNTU_TOUCH)
156-
option(ENABLE_TOUCHSCREEN_SUPPORT "Enable on-screen controls and event detection for touchscreen devices" ON)
157+
option(SHOW_TOUCHSCREEN_CONTROLS "Show mobile controls by default" ON)
158+
option(HIDE_NONMOBILE_OPTIONS "Hide options that are impractical on mobile devices (e. g. changing screen resolution)" ON)
157159
else()
158-
option(ENABLE_TOUCHSCREEN_SUPPORT "Enable on-screen controls and event detection for touchscreen devices" OFF)
160+
option(SHOW_TOUCHSCREEN_CONTROLS "Show mobile controls by default" OFF)
161+
option(HIDE_NONMOBILE_OPTIONS "Hide options that are impractical on mobile devices (e. g. changing screen resolution)" OFF)
159162
endif()
160163

161164
## Add lots of dependencies to compiler switches

config.h.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
#cmakedefine UBUNTU_TOUCH
3939
#cmakedefine ENABLE_TOUCHSCREEN_SUPPORT
40+
#cmakedefine SHOW_TOUCHSCREEN_CONTROLS
41+
#cmakedefine HIDE_NONMOBILE_OPTIONS
4042

4143
#cmakedefine REMOVE_QUIT_BUTTON
4244

10.9 KB
Loading
4.91 KB
Loading

src/control/mobile_controller.cpp

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,23 @@ MobileController::MobileController() :
5050
m_right(false),
5151
m_jump(false),
5252
m_action(false),
53+
m_cheats(false),
54+
m_debug(false),
5355
m_escape(false),
54-
m_bak_escape(false),
5556
m_old_up(false),
5657
m_old_down(false),
5758
m_old_left(false),
5859
m_old_right(false),
5960
m_old_jump(false),
6061
m_old_action(false),
62+
m_old_cheats(false),
63+
m_old_debug(false),
6164
m_old_escape(false),
6265
m_rect_directions(16.f, -144.f, 144.f, -16.f),
6366
m_rect_jump(-160.f, -80.f, -96.f, -16.f),
6467
m_rect_action(-80.f, -80.f, -16.f, -16.f),
68+
m_rect_cheats(-160.f, 16.f, -96.f, 80.f),
69+
m_rect_debug(-80.f, 16.f, -16.f, 80.f),
6570
m_rect_escape(16.f, 16.f, 64.f, 64.f),
6671
m_tex_dirs(Surface::from_file("/images/engine/mobile/direction.png")),
6772
m_tex_btn(Surface::from_file("/images/engine/mobile/button.png")),
@@ -73,6 +78,8 @@ MobileController::MobileController() :
7378
m_tex_rgt(Surface::from_file("/images/engine/mobile/direction_hightlight_right.png")),
7479
m_tex_jump(Surface::from_file("/images/engine/mobile/jump.png")),
7580
m_tex_action(Surface::from_file("/images/engine/mobile/action.png")),
81+
m_tex_cheats(Surface::from_file("/images/engine/mobile/cheats.png")),
82+
m_tex_debug(Surface::from_file("/images/engine/mobile/debug.png")),
7683
m_screen_width(),
7784
m_screen_height()
7885
{
@@ -87,25 +94,34 @@ MobileController::draw(DrawingContext& context)
8794
m_screen_width = context.get_width();
8895
m_screen_height = context.get_height();
8996

90-
context.color().draw_surface_scaled(m_tex_dirs, apply_corner(m_rect_directions, m_screen_width, m_screen_height), 1650);
97+
context.color().draw_surface_scaled(m_tex_dirs, apply_corner(m_rect_directions, m_screen_width, m_screen_height), LAYER_GUI + 99);
9198

9299
if (m_up)
93-
context.color().draw_surface_scaled(m_tex_up, apply_corner(m_rect_directions, m_screen_width, m_screen_height), 1651);
100+
context.color().draw_surface_scaled(m_tex_up, apply_corner(m_rect_directions, m_screen_width, m_screen_height), LAYER_GUI + 99);
94101
if (m_down)
95-
context.color().draw_surface_scaled(m_tex_dwn, apply_corner(m_rect_directions, m_screen_width, m_screen_height), 1651);
102+
context.color().draw_surface_scaled(m_tex_dwn, apply_corner(m_rect_directions, m_screen_width, m_screen_height), LAYER_GUI + 99);
96103
if (m_left)
97-
context.color().draw_surface_scaled(m_tex_lft, apply_corner(m_rect_directions, m_screen_width, m_screen_height), 1651);
104+
context.color().draw_surface_scaled(m_tex_lft, apply_corner(m_rect_directions, m_screen_width, m_screen_height), LAYER_GUI + 99);
98105
if (m_right)
99-
context.color().draw_surface_scaled(m_tex_rgt, apply_corner(m_rect_directions, m_screen_width, m_screen_height), 1651);
106+
context.color().draw_surface_scaled(m_tex_rgt, apply_corner(m_rect_directions, m_screen_width, m_screen_height), LAYER_GUI + 99);
100107

101-
context.color().draw_surface_scaled(m_action ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_action, m_screen_width, m_screen_height), 1650);
102-
context.color().draw_surface_scaled(m_tex_action, apply_corner(m_rect_action, m_screen_width, m_screen_height), 1651);
108+
context.color().draw_surface_scaled(m_action ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_action, m_screen_width, m_screen_height), LAYER_GUI + 99);
109+
context.color().draw_surface_scaled(m_tex_action, apply_corner(m_rect_action, m_screen_width, m_screen_height), LAYER_GUI + 99);
103110

104-
context.color().draw_surface_scaled(m_jump ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_jump, m_screen_width, m_screen_height), 1650);
105-
context.color().draw_surface_scaled(m_tex_jump, apply_corner(m_rect_jump, m_screen_width, m_screen_height), 1651);
111+
context.color().draw_surface_scaled(m_jump ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_jump, m_screen_width, m_screen_height), LAYER_GUI + 99);
112+
context.color().draw_surface_scaled(m_tex_jump, apply_corner(m_rect_jump, m_screen_width, m_screen_height), LAYER_GUI + 99);
106113

107-
context.color().draw_surface_scaled(m_bak_escape ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_escape, m_screen_width, m_screen_height), 1650);
108-
context.color().draw_surface_scaled(m_tex_pause, apply_corner(m_rect_escape, m_screen_width, m_screen_height).grown(-8.f), 1650);
114+
context.color().draw_surface_scaled(m_escape ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_escape, m_screen_width, m_screen_height), LAYER_GUI + 99);
115+
context.color().draw_surface_scaled(m_tex_pause, apply_corner(m_rect_escape, m_screen_width, m_screen_height).grown(-8.f), LAYER_GUI + 99);
116+
117+
if (g_config->developer_mode)
118+
{
119+
context.color().draw_surface_scaled(m_cheats ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_cheats, m_screen_width, m_screen_height), LAYER_GUI + 99);
120+
context.color().draw_surface_scaled(m_tex_cheats, apply_corner(m_rect_cheats, m_screen_width, m_screen_height), LAYER_GUI + 99);
121+
122+
context.color().draw_surface_scaled(m_debug ? m_tex_btn_press : m_tex_btn, apply_corner(m_rect_debug, m_screen_width, m_screen_height), LAYER_GUI + 99);
123+
context.color().draw_surface_scaled(m_tex_debug, apply_corner(m_rect_debug, m_screen_width, m_screen_height), LAYER_GUI + 99);
124+
}
109125
}
110126

111127
void
@@ -120,9 +136,19 @@ MobileController::update()
120136
m_old_right = m_right;
121137
m_old_jump = m_jump;
122138
m_old_action = m_action;
139+
m_old_cheats = m_cheats;
140+
m_old_debug = m_debug;
123141
m_old_escape = m_escape;
124142

125-
m_up = m_down = m_left = m_right = m_jump = m_action = m_escape = false;
143+
m_up = m_down = m_left = m_right = m_jump = m_action = m_cheats = m_debug = m_escape = false;
144+
145+
// Allow using on-screen controls with the mouse
146+
int x, y;
147+
auto buttons = SDL_GetMouseState(&x, &y);
148+
if ((buttons & SDL_BUTTON_LMASK) != 0)
149+
{
150+
activate_widget_at_pos(static_cast<float>(x), static_cast<float>(y));
151+
}
126152

127153
// FIXME: This assumes that 1) there is only one touchscreen and 2) SuperTux
128154
// fills the whole screen
@@ -136,12 +162,6 @@ MobileController::update()
136162

137163
int num_touches = SDL_GetNumTouchFingers(device);
138164

139-
// FIXME: There's some weird problem with the escape button specifically, which
140-
// I had to patch a weird way. If someone in the future finds a fix to handle
141-
// escaping on mobile properly, don't forget to remove those lines.
142-
if (num_touches == 0)
143-
m_bak_escape = false;
144-
145165
for (int i = 0; i < num_touches; i++)
146166
{
147167
SDL_Finger* finger = SDL_GetTouchFinger(device, i);
@@ -159,13 +179,24 @@ MobileController::apply(Controller& controller) const
159179
if (!g_config->mobile_controls)
160180
return;
161181

162-
controller.set_control(Control::UP, m_up || (!m_old_up && controller.hold(Control::UP)));
163-
controller.set_control(Control::DOWN, m_down || (!m_old_down && controller.hold(Control::DOWN)));
164-
controller.set_control(Control::LEFT, m_left || (!m_old_left && controller.hold(Control::LEFT)));
165-
controller.set_control(Control::RIGHT, m_right || (!m_old_right && controller.hold(Control::RIGHT)));
166-
controller.set_control(Control::JUMP, m_jump || (!m_old_jump && controller.hold(Control::JUMP)));
167-
controller.set_control(Control::ACTION, m_action || (!m_old_action && controller.hold(Control::ACTION)));
168-
controller.set_control(Control::ESCAPE, m_escape || (!m_old_escape && controller.hold(Control::ESCAPE)));
182+
if (m_up != m_old_up)
183+
controller.set_control(Control::UP, m_up);
184+
if (m_down != m_old_down)
185+
controller.set_control(Control::DOWN, m_down);
186+
if (m_left != m_old_left)
187+
controller.set_control(Control::LEFT, m_left);
188+
if (m_right != m_old_right)
189+
controller.set_control(Control::RIGHT, m_right);
190+
if (m_jump != m_old_jump)
191+
controller.set_control(Control::JUMP, m_jump);
192+
if (m_action != m_old_action)
193+
controller.set_control(Control::ACTION, m_action);
194+
if (m_cheats != m_old_cheats)
195+
controller.set_control(Control::CHEAT_MENU, m_cheats);
196+
if (m_debug != m_old_debug)
197+
controller.set_control(Control::DEBUG_MENU, m_debug);
198+
if (m_escape != m_old_escape)
199+
controller.set_control(Control::ESCAPE, m_escape);
169200
}
170201

171202
void
@@ -182,20 +213,18 @@ MobileController::activate_widget_at_pos(float x, float y)
182213
if (apply_corner(m_rect_action, m_screen_width, m_screen_height).contains(pos))
183214
m_action = true;
184215

185-
// FIXME: Why do I need an extra variable (m_bak_escape) just for this one?
186-
// Without it, pressing escape will toggle pressed() (not hold(), pressed())
187-
// every single frame, apparently
188-
if (apply_corner(m_rect_escape, m_screen_width, m_screen_height).contains(pos))
189-
{
190-
if (!m_bak_escape)
191-
m_escape = true;
192-
m_bak_escape = true;
193-
}
194-
else
216+
if (g_config->developer_mode)
195217
{
196-
m_bak_escape = false;
218+
if (apply_corner(m_rect_cheats, m_screen_width, m_screen_height).contains(pos))
219+
m_cheats = true;
220+
221+
if (apply_corner(m_rect_debug, m_screen_width, m_screen_height).contains(pos))
222+
m_debug = true;
197223
}
198224

225+
if (apply_corner(m_rect_escape, m_screen_width, m_screen_height).contains(pos))
226+
m_escape = true;
227+
199228
Rectf applied = apply_corner(m_rect_directions, m_screen_width, m_screen_height);
200229
Rectf up = applied;
201230
up.set_bottom(up.get_bottom() - up.get_height() * 2.f / 3.f);

src/control/mobile_controller.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ class MobileController final
3939
void activate_widget_at_pos(float x, float y);
4040

4141
private:
42-
bool m_up, m_down, m_left, m_right, m_jump, m_action, m_escape, m_bak_escape;
43-
bool m_old_up, m_old_down, m_old_left, m_old_right, m_old_jump, m_old_action, m_old_escape;
42+
bool m_up, m_down, m_left, m_right, m_jump, m_action, m_cheats, m_debug, m_escape;
43+
bool m_old_up, m_old_down, m_old_left, m_old_right, m_old_jump, m_old_action, m_old_cheats, m_old_debug, m_old_escape;
4444

45-
const Rectf m_rect_directions, m_rect_jump, m_rect_action, m_rect_escape;
45+
const Rectf m_rect_directions, m_rect_jump, m_rect_action, m_rect_cheats,
46+
m_rect_debug, m_rect_escape;
4647
const SurfacePtr m_tex_dirs, m_tex_btn, m_tex_btn_press, m_tex_pause,
4748
m_tex_up, m_tex_dwn, m_tex_lft, m_tex_rgt,
48-
m_tex_jump, m_tex_action;
49+
m_tex_jump, m_tex_action, m_tex_cheats, m_tex_debug;
4950

5051
int m_screen_width, m_screen_height;
5152

src/supertux/gameconfig.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ Config::load()
255255
}
256256

257257
#ifdef ENABLE_TOUCHSCREEN_SUPPORT
258-
config_video_mapping->get("mobile_controls", mobile_controls);
258+
#ifdef SHOW_TOUCHSCREEN_CONTROLS
259+
config_control_mapping->get("mobile_controls", mobile_controls, true);
260+
#else
261+
config_control_mapping->get("mobile_controls", mobile_controls, false);
262+
#endif
259263
#endif
260264
}
261265

src/supertux/main.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,6 @@ Main::launch_game(const CommandLineArguments& args)
567567
}
568568
}
569569

570-
#ifdef UBUNTU_TOUCH
571-
Dialog::show_message(_("The UBports version is under heavy development!\n"
572-
"If you encounter issues, PLEASE contact the maintainter\n"
573-
"at https://github.com/supertux/supertux/issues or on the\n"
574-
"Open Store's Telegram at https://open-store.io/telegram"));
575-
#endif
576-
577570
m_screen_manager->run();
578571
}
579572

src/supertux/menu/options_menu.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ OptionsMenu::OptionsMenu(bool complete) :
102102
// These values go from screen:640/projection:1600 to
103103
// screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
104104
magnifications.push_back(_("auto"));
105-
#ifndef ENABLE_TOUCHSCREEN_SUPPORT
105+
#ifndef HIDE_NONMOBILE_OPTIONS
106106
magnifications.push_back("40%");
107107
magnifications.push_back("50%");
108108
magnifications.push_back("62.5%");
@@ -354,7 +354,7 @@ OptionsMenu::OptionsMenu(bool complete) :
354354
.set_help(_("Select a profile to play with"));
355355
}
356356

357-
#if !defined(ENABLE_TOUCHSCREEN_SUPPORT) && !defined(__EMSCRIPTEN__)
357+
#if !defined(HIDE_NONMOBILE_OPTIONS) && !defined(__EMSCRIPTEN__)
358358
add_toggle(MNID_FULLSCREEN,_("Window Resizable"), &g_config->window_resizable)
359359
.set_help(_("Allow window resizing, might require a restart to take effect"));
360360

@@ -381,7 +381,7 @@ OptionsMenu::OptionsMenu(bool complete) :
381381
MenuItem& vsync = add_string_select(MNID_VSYNC, _("VSync"), &next_vsync, vsyncs);
382382
vsync.set_help(_("Set the VSync mode"));
383383

384-
#if !defined(ENABLE_TOUCHSCREEN_SUPPORT) && !defined(__EMSCRIPTEN__)
384+
#if !defined(HIDE_NONMOBILE_OPTIONS) && !defined(__EMSCRIPTEN__)
385385
MenuItem& aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"), &next_aspect_ratio, aspect_ratios);
386386
aspect.set_help(_("Adjust the aspect ratio"));
387387
#endif
@@ -420,10 +420,14 @@ OptionsMenu::OptionsMenu(bool complete) :
420420
MenuItem& enable_transitions = add_toggle(MNID_TRANSITIONS, _("Enable transitions"), &g_config->transitions_enabled);
421421
enable_transitions.set_help(_("Enable screen transitions and smooth menu animation"));
422422

423+
#ifndef HIDE_NONMOBILE_OPTIONS
423424
if (g_config->developer_mode)
424425
{
425426
add_toggle(MNID_DEVELOPER_MODE, _("Developer Mode"), &g_config->developer_mode);
426427
}
428+
#else
429+
add_toggle(MNID_DEVELOPER_MODE, _("Developer Mode"), &g_config->developer_mode);
430+
#endif
427431

428432
if (g_config->is_christmas() || g_config->christmas_mode)
429433
{

src/supertux/screen_manager.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,13 @@ ScreenManager::process_events()
338338
{
339339
case SDL_FINGERDOWN:
340340
{
341+
SDL_Event old_event = event;
342+
341343
SDL_Event event2;
342344
event2.type = SDL_MOUSEBUTTONDOWN;
343345
event2.button.button = SDL_BUTTON_LEFT;
344-
event2.button.x = Sint32(event.tfinger.x * float(m_video_system.get_window_size().width));
345-
event2.button.y = Sint32(event.tfinger.y * float(m_video_system.get_window_size().height));
346+
event2.button.x = Sint32(old_event.tfinger.x * float(m_video_system.get_window_size().width));
347+
event2.button.y = Sint32(old_event.tfinger.y * float(m_video_system.get_window_size().height));
346348
SDL_PushEvent(&event2);
347349

348350
event.type = SDL_MOUSEMOTION;
@@ -354,11 +356,13 @@ ScreenManager::process_events()
354356

355357
case SDL_FINGERUP:
356358
{
359+
SDL_Event old_event = event;
360+
357361
SDL_Event event2;
358362
event2.type = SDL_MOUSEBUTTONUP;
359363
event2.button.button = SDL_BUTTON_LEFT;
360-
event2.button.x = Sint32(event.tfinger.x * float(m_video_system.get_window_size().width));
361-
event2.button.y = Sint32(event.tfinger.y * float(m_video_system.get_window_size().height));
364+
event2.button.x = Sint32(old_event.tfinger.x * float(m_video_system.get_window_size().width));
365+
event2.button.y = Sint32(old_event.tfinger.y * float(m_video_system.get_window_size().height));
362366
SDL_PushEvent(&event2);
363367

364368
event.type = SDL_MOUSEMOTION;
@@ -369,11 +373,13 @@ ScreenManager::process_events()
369373
}
370374

371375
case SDL_FINGERMOTION:
376+
SDL_Event old_event = event;
377+
372378
event.type = SDL_MOUSEMOTION;
373-
event.motion.x = Sint32(event.tfinger.x * float(m_video_system.get_window_size().width));
374-
event.motion.y = Sint32(event.tfinger.y * float(m_video_system.get_window_size().height));
375-
event.motion.xrel = Sint32(event.tfinger.dx * float(m_video_system.get_window_size().width));
376-
event.motion.yrel = Sint32(event.tfinger.dy * float(m_video_system.get_window_size().height));
379+
event.motion.x = Sint32(old_event.tfinger.x * float(m_video_system.get_window_size().width));
380+
event.motion.y = Sint32(old_event.tfinger.y * float(m_video_system.get_window_size().height));
381+
event.motion.xrel = Sint32(old_event.tfinger.dx * float(m_video_system.get_window_size().width));
382+
event.motion.yrel = Sint32(old_event.tfinger.dy * float(m_video_system.get_window_size().height));
377383
MouseCursor::current()->set_pos(event.motion.x, event.motion.y);
378384
break;
379385
}

0 commit comments

Comments
 (0)