]> git.lizzy.rs Git - minetest.git/commitdiff
Virtual joystick: Use s32 when using m_screensize as a subtrahend (#12814)
authorMuhammad Rifqi Priyo Susanto <muhammadrifqipriyosusanto@gmail.com>
Sat, 26 Nov 2022 14:16:14 +0000 (21:16 +0700)
committerGitHub <noreply@github.com>
Sat, 26 Nov 2022 14:16:14 +0000 (09:16 -0500)
If still uses u32, m_screensize will yield a big value (underflow) when used as a subtrahend.
ETIE_MOVED is allowed to be run if joystick's ID is available and virtual joystick is fixed.
Add .0f for some float values.

src/gui/touchscreengui.cpp

index 70f1c5163f4f32ca5b4cb2759f557e375ab6fc5c..c52bc59bbcf84714a0b18ccc2f4b03489a597f89 100644 (file)
@@ -773,7 +773,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
                        m_rarecontrolsbar.deactivate();
 
                        s32 dxj = event.TouchInput.X - button_size * 5.0f / 2.0f;
-                       s32 dyj = event.TouchInput.Y - m_screensize.Y + button_size * 5.0f / 2.0f;
+                       s32 dyj = event.TouchInput.Y - (s32)m_screensize.Y + button_size * 5.0f / 2.0f;
 
                        /* Select joystick when left 1/3 of screen dragged or
                         * when joystick tapped (fixed joystick position)
@@ -824,8 +824,9 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
        } else {
                assert(event.TouchInput.Event == ETIE_MOVED);
 
-               if (m_pointerpos[event.TouchInput.ID] ==
-                               v2s32(event.TouchInput.X, event.TouchInput.Y))
+               if (!(m_has_joystick_id && m_fixed_joystick) &&
+                               m_pointerpos[event.TouchInput.ID] ==
+                                               v2s32(event.TouchInput.X, event.TouchInput.Y))
                        return;
 
                if (m_has_move_id) {
@@ -878,14 +879,14 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
                        s32 dx = X - m_pointerpos[event.TouchInput.ID].X;
                        s32 dy = Y - m_pointerpos[event.TouchInput.ID].Y;
                        if (m_fixed_joystick) {
-                               dx = X - button_size * 5 / 2;
-                               dy = Y - m_screensize.Y + button_size * 5 / 2;
+                               dx = X - button_size * 5.0f / 2.0f;
+                               dy = Y - (s32)m_screensize.Y + button_size * 5.0f / 2.0f;
                        }
 
                        double distance_sq = dx * dx + dy * dy;
 
                        s32 dxj = event.TouchInput.X - button_size * 5.0f / 2.0f;
-                       s32 dyj = event.TouchInput.Y - m_screensize.Y + button_size * 5.0f / 2.0f;
+                       s32 dyj = event.TouchInput.Y - (s32)m_screensize.Y + button_size * 5.0f / 2.0f;
                        bool inside_joystick = (dxj * dxj + dyj * dyj <= button_size * button_size * 1.5 * 1.5);
 
                        if (m_joystick_has_really_moved || inside_joystick ||
@@ -936,8 +937,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
                                        s32 ndy = button_size * dy / distance - button_size / 2.0f;
                                        if (m_fixed_joystick) {
                                                m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
-                                                       button_size * 5 / 2 + ndx,
-                                                       m_screensize.Y - button_size * 5 / 2 + ndy));
+                                                       button_size * 5.0f / 2.0f + ndx,
+                                                       m_screensize.Y - button_size * 5.0f / 2.0f + ndy));
                                        } else {
                                                m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
                                                        m_pointerpos[event.TouchInput.ID].X + ndx,