Android: Replace movement buttons with joystick (#7126)

* Android: Replace movement buttons with joystick

Replace movement control buttons (arrows at bottom left screen) with virtual joystick.
Joystick has 8 directions (same as keyboard). Basically, just map it to keyboard input.
Joystick applies only on left 1/3 of screen.
Joystick's position can be fixed by enabling fixed_virtual_joystick setting.
Three new images:
(1) placeholder joystick,
(2) joystick container (background), and
(3) joystick cursor.
Remove unused images: movement control buttons (*_arrow.png).
New data type: touch_gui_joystick_move_id

Joystick's fixed position is spaced one button size from bottom and from left of screen.
Remove unused variable: m_joystick_downlocation
This commit is contained in:
Muhammad Rifqi Priyo Susanto 2018-04-11 03:55:17 +07:00 committed by SmallJoker
parent 22ebbe136a
commit 326eeca306
12 changed files with 339 additions and 121 deletions

@ -118,6 +118,13 @@ random_input (Random input) bool false
# Continuous forward movement, toggled by autoforward key. # Continuous forward movement, toggled by autoforward key.
continuous_forward (Continuous forward) bool false continuous_forward (Continuous forward) bool false
# The length in pixels it takes for touch screen interaction to start.
touchscreen_threshold (Touch screen threshold) int 20 0 100
# (Android) Fixes the position of virtual joystick.
# If disabled, virtual joystick will center to first-touch's position.
fixed_virtual_joystick (Fixed virtual joystick) bool false
# Enable joysticks # Enable joysticks
enable_joysticks (Enable joysticks) bool false enable_joysticks (Enable joysticks) bool false

@ -86,6 +86,15 @@
# type: bool # type: bool
# continuous_forward = false # continuous_forward = false
# The length in pixels it takes for touch screen interaction to start.
# type: int
# touchscreen_threshold = 20
# (Android) Fixes the position of virtual joystick.
# If disabled, virtual joystick will center to first-touch's position.
# type: int
# fixed_virtual_joystick = false
# Enable Joysticks # Enable Joysticks
# type: bool # type: bool
# enable_joysticks = false # enable_joysticks = false

@ -408,6 +408,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("touchtarget", "true"); settings->setDefault("touchtarget", "true");
settings->setDefault("TMPFolder","/sdcard/" PROJECT_NAME_C "/tmp/"); settings->setDefault("TMPFolder","/sdcard/" PROJECT_NAME_C "/tmp/");
settings->setDefault("touchscreen_threshold","20"); settings->setDefault("touchscreen_threshold","20");
settings->setDefault("fixed_virtual_joystick", "false");
settings->setDefault("smooth_lighting", "false"); settings->setDefault("smooth_lighting", "false");
settings->setDefault("max_simultaneous_block_sends_per_client", "3"); settings->setDefault("max_simultaneous_block_sends_per_client", "3");
settings->setDefault("emergequeue_limit_diskonly", "8"); settings->setDefault("emergequeue_limit_diskonly", "8");

@ -1,5 +1,7 @@
/* /*
Copyright (C) 2014 sapier Copyright (C) 2014 sapier
Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
<muhammadrifqipriyosusanto@gmail.com>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@ -38,14 +40,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
using namespace irr::core; using namespace irr::core;
const char **touchgui_button_imagenames = (const char*[]) { const char **touchgui_button_imagenames = (const char*[]) {
"up_arrow.png",
"down_arrow.png",
"left_arrow.png",
"right_arrow.png",
"jump_btn.png", "jump_btn.png",
"down.png" "down.png"
}; };
const char **touchgui_joystick_imagenames = (const char *[]) {
"joystick_off.png",
"joystick_bg.png",
"joystick_center.png"
};
static irr::EKEY_CODE id2keycode(touch_gui_button_id id) static irr::EKEY_CODE id2keycode(touch_gui_button_id id)
{ {
std::string key = ""; std::string key = "";
@ -143,7 +147,7 @@ void AutoHideButtonBar::init(ISimpleTextureSource* tsrc,
m_upper_left = UpperLeft; m_upper_left = UpperLeft;
m_lower_right = LowerRight; m_lower_right = LowerRight;
/* init settings bar */ // init settings bar
irr::core::rect<int> current_button = rect<s32>(UpperLeft.X, UpperLeft.Y, irr::core::rect<int> current_button = rect<s32>(UpperLeft.X, UpperLeft.Y,
LowerRight.X, LowerRight.Y); LowerRight.X, LowerRight.Y);
@ -258,7 +262,7 @@ bool AutoHideButtonBar::isButton(const SEvent &event)
} }
if (m_active) { if (m_active) {
/* check for all buttons in vector */ // check for all buttons in vector
std::vector<button_info*>::iterator iter = m_buttons.begin(); std::vector<button_info*>::iterator iter = m_buttons.begin();
@ -273,11 +277,11 @@ bool AutoHideButtonBar::isButton(const SEvent &event)
translated->KeyInput.Shift = false; translated->KeyInput.Shift = false;
translated->KeyInput.Char = 0; translated->KeyInput.Char = 0;
/* add this event */ // add this event
translated->KeyInput.PressedDown = true; translated->KeyInput.PressedDown = true;
m_receiver->OnEvent(*translated); m_receiver->OnEvent(*translated);
/* remove this event */ // remove this event
translated->KeyInput.PressedDown = false; translated->KeyInput.PressedDown = false;
m_receiver->OnEvent(*translated); m_receiver->OnEvent(*translated);
@ -292,7 +296,7 @@ bool AutoHideButtonBar::isButton(const SEvent &event)
++iter; ++iter;
} }
} else { } else {
/* check for starter button only */ // check for starter button only
if (element == m_starter.guibutton) { if (element == m_starter.guibutton) {
m_starter.ids.push_back(event.TouchInput.ID); m_starter.ids.push_back(event.TouchInput.ID);
m_starter.guibutton->setVisible(false); m_starter.guibutton->setVisible(false);
@ -415,6 +419,8 @@ TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver* receiver)
m_buttons[i].repeatdelay = BUTTON_REPEAT_DELAY; m_buttons[i].repeatdelay = BUTTON_REPEAT_DELAY;
} }
m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold");
m_fixed_joystick = g_settings->getBool("fixed_virtual_joystick");
m_screensize = m_device->getVideoDriver()->getScreenSize(); m_screensize = m_device->getVideoDriver()->getScreenSize();
} }
@ -435,6 +441,21 @@ void TouchScreenGUI::initButton(touch_gui_button_id id, rect<s32> button_rect,
m_texturesource, m_device->getVideoDriver()); m_texturesource, m_device->getVideoDriver());
} }
button_info *TouchScreenGUI::initJoystickButton(touch_gui_button_id id, rect<s32> button_rect,
int texture_id, bool visible)
{
button_info *btn = new button_info();
btn->guibutton = m_guienv->addButton(button_rect, 0, id, L"O");
btn->guibutton->setVisible(visible);
btn->guibutton->grab();
btn->ids.clear();
load_button_texture(btn, touchgui_joystick_imagenames[texture_id], button_rect,
m_texturesource, m_device->getVideoDriver());
return btn;
}
static int getMaxControlPadSize(float density) { static int getMaxControlPadSize(float density) {
return 200 * density * g_settings->getFloat("hud_scaling"); return 200 * density * g_settings->getFloat("hud_scaling");
} }
@ -449,50 +470,40 @@ int TouchScreenGUI::getGuiButtonSize()
void TouchScreenGUI::init(ISimpleTextureSource* tsrc) void TouchScreenGUI::init(ISimpleTextureSource* tsrc)
{ {
assert(tsrc != 0); assert(tsrc);
u32 button_size = getGuiButtonSize(); u32 button_size = getGuiButtonSize();
m_visible = true; m_visible = true;
m_texturesource = tsrc; m_texturesource = tsrc;
/*
draw control pad /* Init joystick display "button"
0 1 2 * Joystick is placed on bottom left of screen.
3 4 5
for now only 0, 1, 2, and 4 are used
*/ */
int number = 0; if (m_fixed_joystick) {
for (int y = 0; y < 2; ++y) m_joystick_btn_off = initJoystickButton(joystick_off_id,
for (int x = 0; x < 3; ++x, ++number) { rect<s32>(button_size,
rect<s32> button_rect( m_screensize.Y - button_size * 4,
x * button_size, m_screensize.Y - button_size * (2 - y), button_size * 4,
(x + 1) * button_size, m_screensize.Y - button_size * (1 - y) m_screensize.Y - button_size), 0);
); } else {
touch_gui_button_id id = after_last_element_id; m_joystick_btn_off = initJoystickButton(joystick_off_id,
std::wstring caption; rect<s32>(button_size,
switch (number) { m_screensize.Y - button_size * 3,
case 0: button_size * 3,
id = left_id; m_screensize.Y - button_size), 0);
caption = L"<";
break;
case 1:
id = forward_id;
caption = L"^";
break;
case 2:
id = right_id;
caption = L">";
break;
case 4:
id = backward_id;
caption = L"v";
break;
}
if (id != after_last_element_id) {
initButton(id, button_rect, caption, false);
}
} }
/* init jump button */ m_joystick_btn_bg = initJoystickButton(joystick_bg_id,
rect<s32>(button_size,
m_screensize.Y - button_size * 4,
button_size * 4,
m_screensize.Y - button_size),
1, false);
m_joystick_btn_center = initJoystickButton(joystick_center_id,
rect<s32>(0, 0, button_size, button_size), 2, false);
// init jump button
initButton(jump_id, initButton(jump_id,
rect<s32>(m_screensize.X-(1.75*button_size), rect<s32>(m_screensize.X-(1.75*button_size),
m_screensize.Y - (0.5*button_size), m_screensize.Y - (0.5*button_size),
@ -500,7 +511,7 @@ void TouchScreenGUI::init(ISimpleTextureSource* tsrc)
m_screensize.Y), m_screensize.Y),
L"x",false); L"x",false);
/* init crunch button */ // init crunch button
initButton(crunch_id, initButton(crunch_id,
rect<s32>(m_screensize.X-(3.25*button_size), rect<s32>(m_screensize.X-(3.25*button_size),
m_screensize.Y - (0.5*button_size), m_screensize.Y - (0.5*button_size),
@ -634,7 +645,7 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button,
translated->KeyInput.Shift = false; translated->KeyInput.Shift = false;
translated->KeyInput.Char = 0; translated->KeyInput.Char = 0;
/* add this event */ // add this event
if (action) { if (action) {
assert(std::find(btn->ids.begin(),btn->ids.end(), eventID) == btn->ids.end()); assert(std::find(btn->ids.begin(),btn->ids.end(), eventID) == btn->ids.end());
@ -647,12 +658,12 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button,
translated->KeyInput.Key = btn->keycode; translated->KeyInput.Key = btn->keycode;
m_receiver->OnEvent(*translated); m_receiver->OnEvent(*translated);
} }
/* remove event */ // remove event
if ((!action) || (btn->immediate_release)) { if ((!action) || (btn->immediate_release)) {
std::vector<int>::iterator pos = std::vector<int>::iterator pos =
std::find(btn->ids.begin(),btn->ids.end(), eventID); std::find(btn->ids.begin(),btn->ids.end(), eventID);
/* has to be in touch list */ // has to be in touch list
assert(pos != btn->ids.end()); assert(pos != btn->ids.end());
btn->ids.erase(pos); btn->ids.erase(pos);
@ -670,23 +681,23 @@ void TouchScreenGUI::handleReleaseEvent(int evt_id)
{ {
touch_gui_button_id button = getButtonID(evt_id); touch_gui_button_id button = getButtonID(evt_id);
/* handle button events */ // handle button events
if (button != after_last_element_id) { if (button != after_last_element_id) {
handleButtonEvent(button, evt_id, false); handleButtonEvent(button, evt_id, false);
} }
/* handle hud button events */ // handle hud button events
else if (isReleaseHUDButton(evt_id)) { else if (isReleaseHUDButton(evt_id)) {
/* nothing to do here */ // nothing to do here
} else if (m_settingsbar.isReleaseButton(evt_id)) { } else if (m_settingsbar.isReleaseButton(evt_id)) {
/* nothing to do here */ // nothing to do here
} else if (m_rarecontrolsbar.isReleaseButton(evt_id)) { } else if (m_rarecontrolsbar.isReleaseButton(evt_id)) {
/* nothing to do here */ // nothing to do here
} }
/* handle the point used for moving view */ // handle the point used for moving view
else if (evt_id == m_move_id) { else if (evt_id == m_move_id) {
m_move_id = -1; m_move_id = -1;
/* if this pointer issued a mouse event issue symmetric release here */ // if this pointer issued a mouse event issue symmetric release here
if (m_move_sent_as_mouse_event) { if (m_move_sent_as_mouse_event) {
SEvent* translated = new SEvent; SEvent* translated = new SEvent;
memset(translated,0,sizeof(SEvent)); memset(translated,0,sizeof(SEvent));
@ -701,10 +712,23 @@ void TouchScreenGUI::handleReleaseEvent(int evt_id)
delete translated; delete translated;
} }
else { else {
/* do double tap detection */ // do double tap detection
doubleTapDetection(); doubleTapDetection();
} }
} }
// handle joystick
else if (evt_id == m_joystick_id) {
m_joystick_id = -1;
// reset joystick
for (unsigned int i = 0; i < 4; i ++)
m_joystick_status[i] = false;
applyJoystickStatus();
m_joystick_btn_off->guibutton->setVisible(true);
m_joystick_btn_bg->guibutton->setVisible(false);
m_joystick_btn_center->guibutton->setVisible(false);
}
else { else {
infostream infostream
<< "TouchScreenGUI::translateEvent released unknown button: " << "TouchScreenGUI::translateEvent released unknown button: "
@ -748,7 +772,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
touch_gui_button_id button = touch_gui_button_id button =
getButtonID(event.TouchInput.X, event.TouchInput.Y); getButtonID(event.TouchInput.X, event.TouchInput.Y);
/* handle button events */ // handle button events
if (button != after_last_element_id) { if (button != after_last_element_id) {
handleButtonEvent(button, eventID, true); handleButtonEvent(button, eventID, true);
m_settingsbar.deactivate(); m_settingsbar.deactivate();
@ -756,19 +780,49 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
} else if (isHUDButton(event)) { } else if (isHUDButton(event)) {
m_settingsbar.deactivate(); m_settingsbar.deactivate();
m_rarecontrolsbar.deactivate(); m_rarecontrolsbar.deactivate();
/* already handled in isHUDButton() */ // already handled in isHUDButton()
} else if (m_settingsbar.isButton(event)) { } else if (m_settingsbar.isButton(event)) {
m_rarecontrolsbar.deactivate(); m_rarecontrolsbar.deactivate();
/* already handled in isSettingsBarButton() */ // already handled in isSettingsBarButton()
} else if (m_rarecontrolsbar.isButton(event)) { } else if (m_rarecontrolsbar.isButton(event)) {
m_settingsbar.deactivate(); m_settingsbar.deactivate();
/* already handled in isSettingsBarButton() */ // already handled in isSettingsBarButton()
} }
/* handle non button events */ // handle non button events
else { else {
m_settingsbar.deactivate(); m_settingsbar.deactivate();
m_rarecontrolsbar.deactivate(); m_rarecontrolsbar.deactivate();
/* if we don't already have a moving point make this the moving one */
u32 button_size = getGuiButtonSize();
s32 dxj = event.TouchInput.X - button_size * 5 / 2;
s32 dyj = event.TouchInput.Y - m_screensize.Y + button_size * 5 / 2;
/* Select joystick when left 1/3 of screen dragged or
* when joystick tapped (fixed joystick position)
*/
if ((m_fixed_joystick && dxj * dxj + dyj * dyj <= button_size * button_size * 1.5 * 1.5) ||
(!m_fixed_joystick && event.TouchInput.X < m_screensize.X / 3)) {
// If we don't already have a starting point for joystick make this the one.
if (m_joystick_id == -1) {
m_joystick_id = event.TouchInput.ID;
m_joystick_has_really_moved = false;
m_joystick_btn_off->guibutton->setVisible(false);
m_joystick_btn_bg->guibutton->setVisible(true);
m_joystick_btn_center->guibutton->setVisible(true);
// If it's a fixed joystick, don't move the joystick "button".
if (!m_fixed_joystick) {
m_joystick_btn_bg->guibutton->setRelativePosition(v2s32(
event.TouchInput.X - button_size * 3 / 2,
event.TouchInput.Y - button_size * 3 / 2));
}
m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
event.TouchInput.X - button_size / 2,
event.TouchInput.Y - button_size / 2));
}
} else {
// If we don't already have a moving point make this the moving one.
if (m_move_id == -1) { if (m_move_id == -1) {
m_move_id = event.TouchInput.ID; m_move_id = event.TouchInput.ID;
m_move_has_really_moved = false; m_move_has_really_moved = false;
@ -777,6 +831,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
m_move_sent_as_mouse_event = false; m_move_sent_as_mouse_event = false;
} }
} }
}
m_pointerpos[event.TouchInput.ID] = v2s32(event.TouchInput.X, event.TouchInput.Y); m_pointerpos[event.TouchInput.ID] = v2s32(event.TouchInput.X, event.TouchInput.Y);
} }
@ -803,7 +858,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 > g_settings->getU16("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;
@ -813,7 +868,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
s32 dx = X - m_pointerpos[event.TouchInput.ID].X; s32 dx = X - m_pointerpos[event.TouchInput.ID].X;
s32 dy = Y - m_pointerpos[event.TouchInput.ID].Y; s32 dy = Y - m_pointerpos[event.TouchInput.ID].Y;
/* adapt to similar behaviour as pc screen */ // adapt to similar behaviour as pc screen
double d = g_settings->getFloat("mouse_sensitivity") * 4; double d = g_settings->getFloat("mouse_sensitivity") * 4;
double old_yaw = m_camera_yaw_change; double old_yaw = m_camera_yaw_change;
double old_pitch = m_camera_pitch; double old_pitch = m_camera_pitch;
@ -837,7 +892,89 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
->getRayFromScreenCoordinates( ->getRayFromScreenCoordinates(
v2s32(event.TouchInput.X, event.TouchInput.Y)); v2s32(event.TouchInput.X, event.TouchInput.Y));
} }
}
if (m_joystick_id != -1 && event.TouchInput.ID == m_joystick_id) {
u32 button_size = getGuiButtonSize();
s32 X = event.TouchInput.X;
s32 Y = event.TouchInput.Y;
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;
}
double distance_sq = dx * dx + dy * dy;
s32 dxj = event.TouchInput.X - button_size * 5 / 2;
s32 dyj = event.TouchInput.Y - m_screensize.Y + button_size * 5 / 2;
bool inside_joystick = (dxj * dxj + dyj * dyj <= button_size * button_size * 1.5 * 1.5);
if (m_joystick_has_really_moved ||
(!m_joystick_has_really_moved && inside_joystick) ||
(!m_fixed_joystick &&
distance_sq > m_touchscreen_threshold * m_touchscreen_threshold)) {
m_joystick_has_really_moved = true;
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 (unsigned int i = 0; i < 4; i ++)
m_joystick_status[i] = false;
if (distance <= m_touchscreen_threshold) {
// do nothing
} else if (angle < 45)
m_joystick_status[j_left] = true;
else if (angle < 90) {
m_joystick_status[j_forward] = true;
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;
}
// move joystick "button"
if (distance > button_size) {
s32 ndx = (s32) button_size * dx / distance - (s32) button_size / 2;
s32 ndy = (s32) button_size * dy / distance - (s32) button_size / 2;
if (m_fixed_joystick) {
m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
button_size * 5 / 2 + ndx,
m_screensize.Y - button_size * 5 / 2 + ndy));
} else { } else {
m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
m_pointerpos[event.TouchInput.ID].X + ndx,
m_pointerpos[event.TouchInput.ID].Y + ndy));
}
} else {
m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
X - button_size / 2, Y - button_size / 2));
}
}
}
if (m_move_id == -1 && m_joystick_id == -1) {
handleChangedButton(event); handleChangedButton(event);
} }
} }
@ -862,7 +999,7 @@ void TouchScreenGUI::handleChangedButton(const SEvent &event)
continue; continue;
} }
/* remove old button */ // remove old button
handleButtonEvent((touch_gui_button_id) i,*iter,false); handleButtonEvent((touch_gui_button_id) i,*iter,false);
if (current_button_id == after_last_element_id) { if (current_button_id == after_last_element_id) {
@ -909,7 +1046,7 @@ bool TouchScreenGUI::doubleTapDetection()
(m_key_events[0].y - m_key_events[1].y) * (m_key_events[0].y - m_key_events[1].y)); (m_key_events[0].y - m_key_events[1].y) * (m_key_events[0].y - m_key_events[1].y));
if (distance > (20 + g_settings->getU16("touchscreen_threshold"))) if (distance > (20 + m_touchscreen_threshold))
return false; return false;
SEvent* translated = new SEvent(); SEvent* translated = new SEvent();
@ -940,27 +1077,58 @@ bool TouchScreenGUI::doubleTapDetection()
} }
void TouchScreenGUI::applyJoystickStatus()
{
for (unsigned int i = 0; i < 4; i ++) {
SEvent translated{};
translated.EventType = irr::EET_KEY_INPUT_EVENT;
translated.KeyInput.Key = id2keycode(m_joystick_names[i]);
translated.KeyInput.PressedDown = false;
m_receiver->OnEvent(translated);
if (m_joystick_status[i]) {
translated.KeyInput.PressedDown = true;
m_receiver->OnEvent(translated);
}
}
}
TouchScreenGUI::~TouchScreenGUI() TouchScreenGUI::~TouchScreenGUI()
{ {
for (unsigned int i = 0; i < after_last_element_id; i++) { for (unsigned int i = 0; i < after_last_element_id; i++) {
button_info* btn = &m_buttons[i]; button_info* btn = &m_buttons[i];
if (btn->guibutton != 0) { if (btn->guibutton) {
btn->guibutton->drop(); btn->guibutton->drop();
btn->guibutton = NULL; btn->guibutton = NULL;
} }
} }
if (m_joystick_btn_off->guibutton) {
m_joystick_btn_off->guibutton->drop();
m_joystick_btn_off->guibutton = NULL;
}
if (m_joystick_btn_bg->guibutton) {
m_joystick_btn_bg->guibutton->drop();
m_joystick_btn_bg->guibutton = NULL;
}
if (m_joystick_btn_center->guibutton) {
m_joystick_btn_center->guibutton->drop();
m_joystick_btn_center->guibutton = NULL;
}
} }
void TouchScreenGUI::step(float dtime) void TouchScreenGUI::step(float dtime)
{ {
/* simulate keyboard repeats */ // simulate keyboard repeats
for (unsigned int i = 0; i < after_last_element_id; i++) { for (unsigned int i = 0; i < after_last_element_id; i++) {
button_info* btn = &m_buttons[i]; button_info* btn = &m_buttons[i];
if (btn->ids.size() > 0) { if (btn->ids.size() > 0) {
btn->repeatcounter += dtime; btn->repeatcounter += dtime;
/* in case we're moving around digging does not happen */ // in case we're moving around digging does not happen
if (m_move_id != -1) if (m_move_id != -1)
m_move_has_really_moved = true; m_move_has_really_moved = true;
@ -979,7 +1147,10 @@ void TouchScreenGUI::step(float dtime)
} }
} }
/* if a new placed pointer isn't moved for some time start digging */ // joystick
applyJoystickStatus();
// if a new placed pointer isn't moved for some time start digging
if ((m_move_id != -1) && if ((m_move_id != -1) &&
(!m_move_has_really_moved) && (!m_move_has_really_moved) &&
(!m_move_sent_as_mouse_event)) { (!m_move_sent_as_mouse_event)) {
@ -1027,12 +1198,16 @@ void TouchScreenGUI::Toggle(bool visible)
m_visible = visible; m_visible = visible;
for (unsigned int i = 0; i < after_last_element_id; i++) { for (unsigned int i = 0; i < after_last_element_id; i++) {
button_info* btn = &m_buttons[i]; button_info* btn = &m_buttons[i];
if (btn->guibutton != 0) { if (btn->guibutton) {
btn->guibutton->setVisible(visible); btn->guibutton->setVisible(visible);
} }
} }
/* clear all active buttons */ if (m_joystick_btn_off->guibutton) {
m_joystick_btn_off->guibutton->setVisible(visible);
}
// clear all active buttons
if (!visible) { if (!visible) {
while (m_known_ids.size() > 0) { while (m_known_ids.size() > 0) {
handleReleaseEvent(m_known_ids.begin()->id); handleReleaseEvent(m_known_ids.begin()->id);

@ -34,11 +34,7 @@ using namespace irr::core;
using namespace irr::gui; using namespace irr::gui;
typedef enum { typedef enum {
forward_id = 0, jump_id = 0,
backward_id,
left_id,
right_id,
jump_id,
crunch_id, crunch_id,
after_last_element_id, after_last_element_id,
settings_starter_id, settings_starter_id,
@ -51,9 +47,18 @@ typedef enum {
range_id, range_id,
chat_id, chat_id,
inventory_id, inventory_id,
drop_id drop_id,
forward_id,
backward_id,
left_id,
right_id,
joystick_off_id,
joystick_bg_id,
joystick_center_id
} touch_gui_button_id; } touch_gui_button_id;
typedef enum { j_forward = 0, j_backward, j_left, j_right } touch_gui_joystick_move_id;
typedef enum { typedef enum {
AHBB_Dir_Top_Bottom, AHBB_Dir_Top_Bottom,
AHBB_Dir_Bottom_Top, AHBB_Dir_Bottom_Top,
@ -69,6 +74,7 @@ typedef enum {
#define RARE_CONTROLS_BAR_Y_OFFSET 4 #define RARE_CONTROLS_BAR_Y_OFFSET 4
extern const char **touchgui_button_imagenames; extern const char **touchgui_button_imagenames;
extern const char **touchgui_joystick_imagenames;
struct button_info struct button_info
{ {
@ -91,26 +97,26 @@ public:
~AutoHideButtonBar(); ~AutoHideButtonBar();
/* add button to be shown */ // add button to be shown
void addButton(touch_gui_button_id id, const wchar_t *caption, void addButton(touch_gui_button_id id, const wchar_t *caption,
const char *btn_image); const char *btn_image);
/* detect settings bar button events */ // detect settings bar button events
bool isButton(const SEvent &event); bool isButton(const SEvent &event);
/* handle released hud buttons */ // handle released hud buttons
bool isReleaseButton(int eventID); bool isReleaseButton(int eventID);
/* step handler */ // step handler
void step(float dtime); void step(float dtime);
/* deactivate button bar */ // deactivate button bar
void deactivate(); void deactivate();
/* hide the whole buttonbar */ // hide the whole buttonbar
void hide(); void hide();
/* unhide the buttonbar */ // unhide the buttonbar
void show(); void show();
private: private:
@ -124,12 +130,12 @@ private:
v2s32 m_upper_left; v2s32 m_upper_left;
v2s32 m_lower_right; v2s32 m_lower_right;
/* show settings bar */ // show settings bar
bool m_active = false; bool m_active = false;
bool m_visible = true; bool m_visible = true;
/* settings bar timeout */ // settings bar timeout
float m_timeout = 0.0f; float m_timeout = 0.0f;
float m_timeout_value = 3.0f; float m_timeout_value = 3.0f;
bool m_initialized = false; bool m_initialized = false;
@ -179,14 +185,20 @@ private:
IEventReceiver *m_receiver; IEventReceiver *m_receiver;
ISimpleTextureSource *m_texturesource; ISimpleTextureSource *m_texturesource;
v2u32 m_screensize; v2u32 m_screensize;
double m_touchscreen_threshold;
std::map<int, rect<s32>> m_hud_rects; std::map<int, rect<s32>> m_hud_rects;
std::map<int, irr::EKEY_CODE> m_hud_ids; std::map<int, irr::EKEY_CODE> m_hud_ids;
bool m_visible; // is the gui visible bool m_visible; // is the gui visible
/* value in degree */ // value in degree
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[4] = {
forward_id, backward_id, left_id, right_id};
bool m_joystick_status[4] = {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.
@ -201,22 +213,33 @@ private:
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);
int m_joystick_id = -1;
bool m_joystick_has_really_moved = false;
bool m_fixed_joystick = false;
button_info *m_joystick_btn_off = nullptr;
button_info *m_joystick_btn_bg = nullptr;
button_info *m_joystick_btn_center = nullptr;
button_info m_buttons[after_last_element_id]; button_info m_buttons[after_last_element_id];
/* gui button detection */ // gui button detection
touch_gui_button_id getButtonID(s32 x, s32 y); touch_gui_button_id getButtonID(s32 x, s32 y);
/* gui button by eventID */ // gui button by eventID
touch_gui_button_id getButtonID(int eventID); touch_gui_button_id getButtonID(int eventID);
/* check if a button has changed */ // check if a button has changed
void handleChangedButton(const SEvent &event); void handleChangedButton(const SEvent &event);
/* initialize a button */ // initialize a button
void initButton(touch_gui_button_id id, rect<s32> button_rect, void initButton(touch_gui_button_id id, rect<s32> button_rect,
std::wstring caption, bool immediate_release, std::wstring caption, bool immediate_release,
float repeat_delay = BUTTON_REPEAT_DELAY); float repeat_delay = BUTTON_REPEAT_DELAY);
// initialize a joystick button
button_info *initJoystickButton(touch_gui_button_id id, rect<s32> button_rect,
int texture_id, bool visible = true);
struct id_status struct id_status
{ {
int id; int id;
@ -224,28 +247,31 @@ private:
int Y; int Y;
}; };
/* vector to store known ids and their initial touch positions*/ // vector to store known ids and their initial touch positions
std::vector<id_status> m_known_ids; std::vector<id_status> m_known_ids;
/* handle a button event */ // handle a button event
void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action); void handleButtonEvent(touch_gui_button_id bID, int eventID, bool action);
/* handle pressed hud buttons */ // handle pressed hud buttons
bool isHUDButton(const SEvent &event); bool isHUDButton(const SEvent &event);
/* handle released hud buttons */ // handle released hud buttons
bool isReleaseHUDButton(int eventID); bool isReleaseHUDButton(int eventID);
/* handle double taps */ // handle double taps
bool doubleTapDetection(); bool doubleTapDetection();
/* handle release event */ // handle release event
void handleReleaseEvent(int evt_id); void handleReleaseEvent(int evt_id);
/* get size of regular gui control button */ // apply joystick status
void applyJoystickStatus();
// get size of regular gui control button
int getGuiButtonSize(); int getGuiButtonSize();
/* doubleclick detection variables */ // doubleclick detection variables
struct key_event struct key_event
{ {
unsigned int down_time; unsigned int down_time;
@ -253,16 +279,16 @@ private:
s32 y; s32 y;
}; };
/* array for saving last known position of a pointer */ // array for saving last known position of a pointer
v2s32 m_pointerpos[MAX_TOUCH_COUNT]; v2s32 m_pointerpos[MAX_TOUCH_COUNT];
/* array for doubletap detection */ // array for doubletap detection
key_event m_key_events[2]; key_event m_key_events[2];
/* settings bar */ // settings bar
AutoHideButtonBar m_settingsbar; AutoHideButtonBar m_settingsbar;
/* rare controls bar */ // rare controls bar
AutoHideButtonBar m_rarecontrolsbar; AutoHideButtonBar m_rarecontrolsbar;
}; };
extern TouchScreenGUI *g_touchscreengui; extern TouchScreenGUI *g_touchscreengui;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 373 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 400 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 396 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 373 B