TouchScreenGUI: Read coordinates directly for virtual joystick (#13567)

The movement's direction and speed are calculated directly from the button's relative screen coordinate. The previous method was to trigger the movement using a keyboard event.
The only virtual joystick status left is Aux1 button.

---------

Co-authored-by: Gregor Parzefall <gregor.parzefall@posteo.de>
This commit is contained in:
Muhammad Rifqi Priyo Susanto 2023-06-16 22:40:16 +07:00 committed by GitHub
parent c549e84abb
commit a4e69d6843
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 75 deletions

@ -332,7 +332,11 @@ public:
return 0.0f; return 0.0f;
return 1.0f; // If there is a keyboard event, assume maximum speed return 1.0f; // If there is a keyboard event, assume maximum speed
} }
#ifdef HAVE_TOUCHSCREENGUI
return m_receiver->m_touchscreengui->getMovementSpeed();
#else
return joystick.getMovementSpeed(); return joystick.getMovementSpeed();
#endif
} }
virtual float getMovementDirection() virtual float getMovementDirection()
@ -352,7 +356,11 @@ public:
if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */ if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
return atan2(x, z); return atan2(x, z);
else else
#ifdef HAVE_TOUCHSCREENGUI
return m_receiver->m_touchscreengui->getMovementDirection();
#else
return joystick.getMovementDirection(); return joystick.getMovementDirection();
#endif
} }
virtual bool cancelPressed() virtual bool cancelPressed()

@ -52,18 +52,6 @@ static irr::EKEY_CODE id2keycode(touch_gui_button_id id)
{ {
std::string key = ""; std::string key = "";
switch (id) { switch (id) {
case forward_id:
key = "forward";
break;
case left_id:
key = "left";
break;
case right_id:
key = "right";
break;
case backward_id:
key = "backward";
break;
case inventory_id: case inventory_id:
key = "inventory"; key = "inventory";
break; break;
@ -702,8 +690,9 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
m_has_joystick_id = false; m_has_joystick_id = false;
// reset joystick // reset joystick
for (unsigned int i = 0; i < 4; i++) m_joystick_direction = 0.0f;
m_joystick_status[i] = false; m_joystick_speed = 0.0f;
m_joystick_status_aux1 = false;
applyJoystickStatus(); applyJoystickStatus();
m_joystick_btn_off->guibutton->setVisible(true); m_joystick_btn_off->guibutton->setVisible(true);
@ -844,8 +833,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
(m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y) * (m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y) *
(m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y)); (m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y));
if ((distance > m_touchscreen_threshold) || if (distance > m_touchscreen_threshold || m_move_has_really_moved) {
(m_move_has_really_moved)) {
m_move_has_really_moved = true; m_move_has_really_moved = true;
s32 X = event.TouchInput.X; s32 X = event.TouchInput.X;
s32 Y = event.TouchInput.Y; s32 Y = event.TouchInput.Y;
@ -868,8 +856,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
->getSceneCollisionManager() ->getSceneCollisionManager()
->getRayFromScreenCoordinates(v2s32(X, Y)); ->getRayFromScreenCoordinates(v2s32(X, Y));
} }
} else if ((event.TouchInput.ID == m_move_id) && } else if (event.TouchInput.ID == m_move_id && m_move_sent_as_mouse_event) {
(m_move_sent_as_mouse_event)) {
m_shootline = m_device m_shootline = m_device
->getSceneManager() ->getSceneManager()
->getSceneCollisionManager() ->getSceneCollisionManager()
@ -899,45 +886,21 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
(!m_fixed_joystick && (!m_fixed_joystick &&
distance_sq > m_touchscreen_threshold * m_touchscreen_threshold)) { distance_sq > m_touchscreen_threshold * m_touchscreen_threshold)) {
m_joystick_has_really_moved = true; m_joystick_has_really_moved = true;
m_joystick_direction = atan2(dx, -dy);
double distance = sqrt(distance_sq); double distance = sqrt(distance_sq);
// angle in degrees
double angle = acos(dx / distance) * 180 / M_PI;
if (dy < 0)
angle *= -1;
// rotate to make comparing easier
angle = fmod(angle + 180 + 22.5, 360);
// reset state before applying
for (bool & joystick_status : m_joystick_status)
joystick_status = false;
if (distance <= m_touchscreen_threshold) { if (distance <= m_touchscreen_threshold) {
// do nothing m_joystick_speed = 0.0f;
} else if (angle < 45) } else {
m_joystick_status[j_left] = true; m_joystick_speed = distance / button_size;
else if (angle < 90) { if (m_joystick_speed > 1.0f)
m_joystick_status[j_forward] = true; m_joystick_speed = 1.0f;
m_joystick_status[j_left] = true;
} else if (angle < 135)
m_joystick_status[j_forward] = true;
else if (angle < 180) {
m_joystick_status[j_forward] = true;
m_joystick_status[j_right] = true;
} else if (angle < 225)
m_joystick_status[j_right] = true;
else if (angle < 270) {
m_joystick_status[j_backward] = true;
m_joystick_status[j_right] = true;
} else if (angle < 315)
m_joystick_status[j_backward] = true;
else if (angle <= 360) {
m_joystick_status[j_backward] = true;
m_joystick_status[j_left] = true;
} }
m_joystick_status_aux1 = distance > (button_size * 1.5f);
if (distance > button_size) { if (distance > button_size) {
m_joystick_status[j_aux1] = true;
// move joystick "button" // move joystick "button"
s32 ndx = button_size * dx / distance - button_size / 2.0f; s32 ndx = button_size * dx / distance - button_size / 2.0f;
s32 ndy = button_size * dy / distance - button_size / 2.0f; s32 ndy = button_size * dy / distance - button_size / 2.0f;
@ -971,7 +934,7 @@ void TouchScreenGUI::handleChangedButton(const SEvent &event)
for (auto iter = m_buttons[i].ids.begin(); for (auto iter = m_buttons[i].ids.begin();
iter != m_buttons[i].ids.end(); ++iter) { iter != m_buttons[i].ids.end(); ++iter) {
if (event.TouchInput.ID == *iter) { if (event.TouchInput.ID == *iter) {
int current_button_id = auto current_button_id =
getButtonID(event.TouchInput.X, event.TouchInput.Y); getButtonID(event.TouchInput.X, event.TouchInput.Y);
if (current_button_id == i) if (current_button_id == i)
@ -1038,17 +1001,14 @@ bool TouchScreenGUI::doRightClick()
void TouchScreenGUI::applyJoystickStatus() void TouchScreenGUI::applyJoystickStatus()
{ {
for (unsigned int i = 0; i < 5; i++) { if (m_joystick_triggers_aux1) {
if (i == 4 && !m_joystick_triggers_aux1)
continue;
SEvent translated{}; SEvent translated{};
translated.EventType = irr::EET_KEY_INPUT_EVENT; translated.EventType = irr::EET_KEY_INPUT_EVENT;
translated.KeyInput.Key = id2keycode(m_joystick_names[i]); translated.KeyInput.Key = id2keycode(aux1_id);
translated.KeyInput.PressedDown = false; translated.KeyInput.PressedDown = false;
m_receiver->OnEvent(translated); m_receiver->OnEvent(translated);
if (m_joystick_status[i]) { if (m_joystick_status_aux1) {
translated.KeyInput.PressedDown = true; translated.KeyInput.PressedDown = true;
m_receiver->OnEvent(translated); m_receiver->OnEvent(translated);
} }
@ -1110,12 +1070,7 @@ void TouchScreenGUI::step(float dtime)
} }
// joystick // joystick
for (unsigned int i = 0; i < 4; i++) { applyJoystickStatus();
if (m_joystick_status[i]) {
applyJoystickStatus();
break;
}
}
// if a new placed pointer isn't moved for some time start digging // if a new placed pointer isn't moved for some time start digging
if (m_has_move_id && if (m_has_move_id &&

@ -54,10 +54,6 @@ typedef enum
chat_id, chat_id,
inventory_id, inventory_id,
drop_id, drop_id,
forward_id,
backward_id,
left_id,
right_id,
joystick_off_id, joystick_off_id,
joystick_bg_id, joystick_bg_id,
joystick_center_id joystick_center_id
@ -194,6 +190,9 @@ public:
*/ */
line3d<f32> getShootline() { return m_shootline; } line3d<f32> getShootline() { return m_shootline; }
float getMovementDirection() { return m_joystick_direction; }
float getMovementSpeed() { return m_joystick_speed; }
void step(float dtime); void step(float dtime);
void resetHud(); void resetHud();
void registerHudItem(int index, const rect<s32> &rect); void registerHudItem(int index, const rect<s32> &rect);
@ -220,11 +219,6 @@ private:
double m_camera_yaw_change = 0.0; double m_camera_yaw_change = 0.0;
double m_camera_pitch = 0.0; double m_camera_pitch = 0.0;
// forward, backward, left, right
touch_gui_button_id m_joystick_names[5] = {
forward_id, backward_id, left_id, right_id, aux1_id};
bool m_joystick_status[5] = {false, false, false, false, false};
/* /*
* A line starting at the camera and pointing towards the * A line starting at the camera and pointing towards the
* selected object. * selected object.
@ -238,11 +232,14 @@ private:
bool m_move_has_really_moved = false; bool m_move_has_really_moved = false;
u64 m_move_downtime = 0; u64 m_move_downtime = 0;
bool m_move_sent_as_mouse_event = false; bool m_move_sent_as_mouse_event = false;
v2s32 m_move_downlocation = v2s32(-10000, -10000); v2s32 m_move_downlocation = v2s32(-10000, -10000); // off-screen
bool m_has_joystick_id = false; bool m_has_joystick_id = false;
size_t m_joystick_id; size_t m_joystick_id;
bool m_joystick_has_really_moved = false; bool m_joystick_has_really_moved = false;
float m_joystick_direction = 0.0f; // assume forward
float m_joystick_speed = 0.0f; // no movement
bool m_joystick_status_aux1 = false;
bool m_fixed_joystick = false; bool m_fixed_joystick = false;
bool m_joystick_triggers_aux1 = false; bool m_joystick_triggers_aux1 = false;
bool m_draw_crosshair = false; bool m_draw_crosshair = false;