Backport Minetest 5 TouchScreenGUI Joystick

master
MoNTE48 2020-09-25 12:51:46 +02:00
parent 515213de92
commit 2d585c6b59
22 changed files with 420 additions and 276 deletions

View File

@ -1,5 +1,7 @@
/* /*
Copyright (C) 2014 sapier Copyright (C) 2014 sapier
Copyright (C) 2018 srifqi, Muhammad Rifqi Priyo Susanto
<muhammadrifqipriyosusanto@gmail.com>
Copyright (C) 2014-2020 Maksim Gamarnik [MoNTE48] MoNTE48@mail.ua Copyright (C) 2014-2020 Maksim Gamarnik [MoNTE48] MoNTE48@mail.ua
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
@ -18,9 +20,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
#include "touchscreengui.h" #include "touchscreengui.h"
#include "irrlichttypes.h"
#include "irr_v2d.h"
#include "log.h" #include "log.h"
#include "keycode.h" #include "keycode.h"
#include "settings.h" #include "settings.h"
#include "gettime.h"
#include "util/numeric.h"
#include "porting.h" #include "porting.h"
#include "guiscalingfilter.h" #include "guiscalingfilter.h"
@ -29,51 +35,41 @@ with this program; if not, write to the Free Software Foundation, Inc.,
using namespace irr::core; using namespace irr::core;
const char *button_imagenames[][2] = { const char **button_imagenames = (const char *[]) {
{"up_one_btn.png", "up_one_press.png"}, "inventory_btn.png",
{"up_two_btn.png","up_two_press.png"}, "drop_btn.png",
{"up_three_btn.png", "up_three_press.png"}, "jump_btn.png",
{"down_one_btn.png", "down_one_press.png"}, "down_btn.png",
{"down_two_btn.png", "down_two_press.png"}, "escape_btn.png",
{"down_three_btn.png", "down_three_press.png"}, "minimap_btn.png",
{"left_btn.png", "left_press.png"}, "rangeview_btn.png",
{"right_btn.png", "right_press.png"}, "camera_btn.png",
{"empty_btn.png"}, "chat_btn.png"
{"inventory_btn.png"}, // "noclip_btn.png",
{"drop_btn.png"}, // "fast_btn.png"
{"jump_btn.png"}, };
{"down_btn.png"},
{"escape_btn.png"}, const char **joystick_imagenames = (const char *[]) {
{"minimap_btn.png"}, "joystick_off.png",
{"rangeview_btn.png"}, "joystick_bg.png",
{"camera_btn.png"}, "joystick_center.png"
{"chat_btn.png"}
// {"noclip_btn.png"},
// {"fast_btn.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 = "";
switch (id) { switch (id) {
case forward_one: case forward_id:
case forward_two:
case forward_three:
key = "forward"; key = "forward";
break; break;
case backward_one:
case backward_two:
case backward_three:
key = "backward";
break;
case left_id: case left_id:
key = "left"; key = "left";
break; break;
case right_id: case right_id:
key = "right"; key = "right";
break; break;
case empty_id: case backward_id:
key = "forward"; key = "backward";
break; break;
case inventory_id: case inventory_id:
key = "inventory"; key = "inventory";
@ -107,7 +103,7 @@ static irr::EKEY_CODE id2keycode(touch_gui_button_id id)
case fast_id: case fast_id:
key = "fast"; key = "fast";
break; */ break; */
case after_last_element_id: default:
break; break;
} }
assert(!key.empty()); assert(!key.empty());
@ -116,39 +112,23 @@ static irr::EKEY_CODE id2keycode(touch_gui_button_id id)
TouchScreenGUI *g_touchscreengui; TouchScreenGUI *g_touchscreengui;
static void load_button_texture(button_info *btn, touch_gui_button_id id, static void load_button_texture(button_info *btn, const char *path,
const rect<s32> &button_rect, ISimpleTextureSource *tsrc, const rect<s32> &button_rect, ISimpleTextureSource *tsrc, video::IVideoDriver *driver)
video::IVideoDriver *driver)
{ {
const char *path = button_imagenames[id][0];
const char *path_pressed = button_imagenames[id][1] ? button_imagenames[id][1] : path;
unsigned int tid; unsigned int tid;
video::ITexture *texture = guiScalingImageButton(driver, video::ITexture *texture = guiScalingImageButton(driver,
tsrc->getTexture(path, &tid), tsrc->getTexture(path, &tid), button_rect.getWidth(),
button_rect.getWidth(), button_rect.getHeight());
button_rect.getHeight());
video::ITexture *texture_pressed = texture;
if (strcmp(path, path_pressed) != 0) {
texture_pressed = guiScalingImageButton(driver,
tsrc->getTexture(path_pressed, &tid),
button_rect.getWidth(),
button_rect.getHeight());
texture_pressed = texture_pressed ? texture_pressed : texture;
}
if (texture) { if (texture) {
btn->guibutton->setUseAlphaChannel(true); btn->guibutton->setUseAlphaChannel(true);
if (g_settings->getBool("gui_scaling_filter")) { if (g_settings->getBool("gui_scaling_filter")) {
rect<s32> txr_rect = rect<s32>(0, 0, button_rect.getWidth(), button_rect.getHeight()); rect<s32> txr_rect = rect<s32>(0, 0, button_rect.getWidth(), button_rect.getHeight());
btn->guibutton->setImage(texture, txr_rect); btn->guibutton->setImage(texture, txr_rect);
btn->guibutton->setPressedImage(texture_pressed, txr_rect); btn->guibutton->setPressedImage(texture, txr_rect);
btn->guibutton->setScaleImage(false); btn->guibutton->setScaleImage(false);
} else { } else {
btn->guibutton->setImage(texture); btn->guibutton->setImage(texture);
btn->guibutton->setPressedImage(texture_pressed); btn->guibutton->setPressedImage(texture);
btn->guibutton->setScaleImage(true); btn->guibutton->setScaleImage(true);
} }
btn->guibutton->setDrawBorder(false); btn->guibutton->setDrawBorder(false);
@ -156,32 +136,28 @@ static void load_button_texture(button_info *btn, touch_gui_button_id id,
} }
} }
TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver) : TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver):
m_device(device), m_device(device),
m_guienv(device->getGUIEnvironment()), m_guienv(device->getGUIEnvironment()),
m_camera_yaw_change(0.0),
m_camera_pitch(0.0),
m_visible(false), m_visible(false),
m_move_id(-1), m_receiver(receiver)
m_receiver(receiver),
m_move_has_really_moved(false),
m_move_downtime(0),
m_move_sent_as_mouse_event(false)
{ {
for (auto & m_button : m_buttons) { for (auto &button : m_buttons) {
m_button.guibutton = nullptr; button.guibutton = nullptr;
m_button.repeatcounter = -1; button.repeatcounter = -1;
m_button.repeatdelay = BUTTON_REPEAT_DELAY; button.repeatdelay = BUTTON_REPEAT_DELAY;
} }
m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold"); m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold");
m_screensize = m_device->getVideoDriver()->getScreenSize(); m_screensize = m_device->getVideoDriver()->getScreenSize();
button_size = MYMIN(m_screensize.Y / 4.5f,
porting::getDisplayDensity() *
g_settings->getFloat("hud_scaling") * 65.0f);
} }
void TouchScreenGUI::initButton(touch_gui_button_id id, const rect<s32> &button_rect, void TouchScreenGUI::initButton(touch_gui_button_id id, const rect<s32> &button_rect,
const std::wstring &caption, bool immediate_release, float repeat_delay) const std::wstring &caption, bool immediate_release, float repeat_delay)
{ {
button_info *btn = &m_buttons[id]; button_info *btn = &m_buttons[id];
btn->guibutton = m_guienv->addButton(button_rect, nullptr, id, caption.c_str()); btn->guibutton = m_guienv->addButton(button_rect, nullptr, id, caption.c_str());
btn->guibutton->grab(); btn->guibutton->grab();
@ -191,81 +167,59 @@ void TouchScreenGUI::initButton(touch_gui_button_id id, const rect<s32> &button_
btn->immediate_release = immediate_release; btn->immediate_release = immediate_release;
btn->ids.clear(); btn->ids.clear();
load_button_texture(btn, id, button_rect, load_button_texture(btn, button_imagenames[id], button_rect,
m_texturesource, m_device->getVideoDriver()); m_texturesource, m_device->getVideoDriver());
}
button_info *TouchScreenGUI::initJoystickButton(touch_gui_button_id id,
const rect<s32> &button_rect, int texture_id, bool visible)
{
auto *btn = new button_info();
btn->guibutton = m_guienv->addButton(button_rect, nullptr, id, L"O");
btn->guibutton->setVisible(visible);
btn->guibutton->grab();
btn->ids.clear();
load_button_texture(btn, joystick_imagenames[texture_id],
button_rect, m_texturesource, m_device->getVideoDriver());
return btn;
} }
void TouchScreenGUI::init(ISimpleTextureSource *tsrc) void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
{ {
assert(tsrc != nullptr); assert(tsrc);
float density = porting::getDisplayDensity() * g_settings->getFloat("hud_scaling"); m_visible = true;
s32 button_size = static_cast<s32>(density * 60); m_texturesource = tsrc;
s32 ctlpad_size = static_cast<s32>(density * 85);
m_visible = true;
m_texturesource = tsrc;
m_control_pad_rect = rect<s32>(0, (m_screensize.Y - ctlpad_size * 3),
(ctlpad_size * 3), m_screensize.Y);
/* /* Init joystick display "button"
draw control pad * Joystick is placed on bottom left of screen.
0 3 6 */
1 4 7 if (m_fixed_joystick) {
2 5 8 m_joystick_btn_off = initJoystickButton(joystick_off_id,
*/ rect<s32>(button_size / 2,
m_screensize.Y - button_size * 4.5,
button_size * 4.5,
m_screensize.Y - button_size / 2), 0);
} else {
m_joystick_btn_off = initJoystickButton(joystick_off_id,
rect<s32>(button_size / 2,
m_screensize.Y - button_size * 3.5,
button_size * 3.5,
m_screensize.Y - button_size / 2), 0);
}
int number = 0; m_joystick_btn_bg = initJoystickButton(joystick_bg_id,
for (int y = 0; y < 3; ++y) rect<s32>(button_size / 2,
for (int x = 0; x < 3; ++x, ++number) { m_screensize.Y - button_size * 4.5,
v2s32 tl; button_size * 4.5,
tl.X = ctlpad_size * y; m_screensize.Y - button_size / 2),
tl.Y = m_screensize.Y - ctlpad_size * (3 - x); 1, false);
m_joystick_btn_center = initJoystickButton(joystick_center_id,
rect<s32>(0, 0, button_size * 1.5, button_size * 1.5), 2, false);
rect<s32> button_rect(tl.X, tl.Y, tl.X + ctlpad_size, tl.Y + ctlpad_size);
touch_gui_button_id id = after_last_element_id;
std::wstring caption;
switch (number) {
case 0:
id = forward_one;
caption = L"^";
break;
case 3:
id = forward_two;
caption = L"^";
break;
case 6:
id = forward_three;
caption = L"^";
break;
case 1:
id = left_id;
caption = L"<";
break;
case 4:
id = empty_id;
break;
case 2:
id = backward_one;
caption = L"v";
break;
case 5:
id = backward_two;
caption = L"v";
break;
case 8:
id = backward_three;
caption = L"v";
break;
case 7:
id = right_id;
caption = L">";
break;
default:
break;
}
if (id != after_last_element_id)
initButton(id, button_rect, caption, false);
}
// init inventory button // init inventory button
initButton(inventory_id, initButton(inventory_id,
@ -273,7 +227,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
m_screensize.Y - button_size * 1.5, m_screensize.Y - button_size * 1.5,
m_screensize.X, m_screensize.X,
m_screensize.Y), m_screensize.Y),
L"inv", false, SLOW_BUTTON_REPEAT); L"inv", false);
// init crunch button // init crunch button
initButton(crunch_id, initButton(crunch_id,
@ -281,7 +235,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
m_screensize.Y - button_size / 1.5, m_screensize.Y - button_size / 1.5,
m_screensize.X - button_size * 1.5, m_screensize.X - button_size * 1.5,
m_screensize.Y), m_screensize.Y),
L"H", false, SLOW_BUTTON_REPEAT); L"H", false);
// init jump button // init jump button
initButton(jump_id, initButton(jump_id,
@ -289,7 +243,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
m_screensize.Y - button_size * 3, m_screensize.Y - button_size * 3,
m_screensize.X - button_size * 1.5, m_screensize.X - button_size * 1.5,
m_screensize.Y - button_size * 1.5), m_screensize.Y - button_size * 1.5),
L"x", false, SLOW_BUTTON_REPEAT); L"x", false);
// init drop button // init drop button
initButton(drop_id, initButton(drop_id,
@ -297,7 +251,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
m_screensize.Y / 2 - button_size, m_screensize.Y / 2 - button_size,
m_screensize.X, m_screensize.X,
m_screensize.Y / 2), m_screensize.Y / 2),
L"drop", false, SLOW_BUTTON_REPEAT); L"drop", false);
// dirty implementation of positions for iOS // dirty implementation of positions for iOS
#ifndef __IOS__ #ifndef __IOS__
@ -316,7 +270,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
0, 0,
m_screensize.X / 2 - button_size / button_05, m_screensize.X / 2 - button_size / button_05,
button_size), button_size),
L"Exit", false, SLOW_BUTTON_REPEAT); L"Exit", false);
// init minimap button [2] // init minimap button [2]
#ifndef __IOS__ #ifndef __IOS__
@ -326,7 +280,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
0, 0,
m_screensize.X / 2, m_screensize.X / 2,
button_size), button_size),
L"minimap", false, SLOW_BUTTON_REPEAT); L"minimap", false);
#endif #endif
// init rangeselect button [3] // init rangeselect button [3]
@ -335,15 +289,15 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
0, 0,
m_screensize.X / 2 + button_size / button_05, m_screensize.X / 2 + button_size / button_05,
button_size), button_size),
L"far", false, SLOW_BUTTON_REPEAT); L"rangeview", false);
// init camera button [4] // init camera button [4]
initButton(camera_id, initButton(camera_id,
rect<s32>(m_screensize.X / 2 + button_size / button_05, rect<s32>(m_screensize.X / 2 + button_size / button_05,
0, 0,
m_screensize.X / 2 + button_size * 2 * button_075, m_screensize.X / 2 + button_size * 2 * button_075,
button_size), button_size),
L"cam", false, SLOW_BUTTON_REPEAT); L"camera", false);
// init chat button // init chat button
initButton(chat_id, initButton(chat_id,
@ -351,7 +305,7 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
0, 0,
m_screensize.X, m_screensize.X,
button_size), button_size),
L"far", false, SLOW_BUTTON_REPEAT); L"Chat", false);
// init noclip button // init noclip button
/* initButton(noclip_id, /* initButton(noclip_id,
@ -359,14 +313,14 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc)
m_screensize.Y - button_size * 4.75, m_screensize.Y - button_size * 4.75,
m_screensize.X, m_screensize.X,
m_screensize.Y - button_size * 4), m_screensize.Y - button_size * 4),
L"clip", false, SLOW_BUTTON_REPEAT); L"clip", false);
// init fast button // init fast button
initButton(fast_id, initButton(fast_id,
rect<s32>(m_screensize.X - button_size * 0.75, rect<s32>(m_screensize.X - button_size * 0.75,
m_screensize.Y - button_size * 4, m_screensize.Y - button_size * 4,
m_screensize.X, m_screensize.X,
m_screensize.Y - button_size * 3.25), m_screensize.Y - button_size * 3.25),
L"fast", false, SLOW_BUTTON_REPEAT); */ L"fast", false); */
} }
touch_gui_button_id TouchScreenGUI::getButtonID(s32 x, s32 y) touch_gui_button_id TouchScreenGUI::getButtonID(s32 x, s32 y)
@ -377,13 +331,12 @@ touch_gui_button_id TouchScreenGUI::getButtonID(s32 x, s32 y)
gui::IGUIElement *element = gui::IGUIElement *element =
rootguielement->getElementFromPoint(core::position2d<s32>(x, y)); rootguielement->getElementFromPoint(core::position2d<s32>(x, y));
if (element) { if (element)
for (unsigned int i = 0; i < after_last_element_id; i++) { for (unsigned int i = 0; i < after_last_element_id; i++)
if (element == m_buttons[i].guibutton) if (element == m_buttons[i].guibutton)
return (touch_gui_button_id) i; return (touch_gui_button_id) i;
}
}
} }
return after_last_element_id; return after_last_element_id;
} }
@ -404,13 +357,13 @@ touch_gui_button_id TouchScreenGUI::getButtonID(size_t eventID)
bool TouchScreenGUI::isHUDButton(const SEvent &event) bool TouchScreenGUI::isHUDButton(const SEvent &event)
{ {
// check if hud item is pressed // check if hud item is pressed
for (auto & m_hud_rect : m_hud_rects) { for (auto &hud_rect : m_hud_rects) {
if (m_hud_rect.second.isPointInside(v2s32(event.TouchInput.X, if (hud_rect.second.isPointInside(v2s32(event.TouchInput.X,
event.TouchInput.Y))) { event.TouchInput.Y))) {
auto *translated = new SEvent(); auto *translated = new SEvent();
memset(translated, 0, sizeof(SEvent)); memset(translated, 0, sizeof(SEvent));
translated->EventType = irr::EET_KEY_INPUT_EVENT; translated->EventType = irr::EET_KEY_INPUT_EVENT;
translated->KeyInput.Key = (irr::EKEY_CODE) (KEY_KEY_1 + m_hud_rect.first); translated->KeyInput.Key = (irr::EKEY_CODE) (KEY_KEY_1 + hud_rect.first);
translated->KeyInput.Control = false; translated->KeyInput.Control = false;
translated->KeyInput.Shift = false; translated->KeyInput.Shift = false;
translated->KeyInput.PressedDown = true; translated->KeyInput.PressedDown = true;
@ -424,7 +377,7 @@ bool TouchScreenGUI::isHUDButton(const SEvent &event)
} }
void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button,
size_t eventID, bool action) size_t eventID, bool action)
{ {
button_info *btn = &m_buttons[button]; button_info *btn = &m_buttons[button];
auto *translated = new SEvent(); auto *translated = new SEvent();
@ -441,18 +394,16 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button,
btn->ids.push_back(eventID); btn->ids.push_back(eventID);
if (btn->ids.size() > 1) if (btn->ids.size() > 1) return;
return;
btn->repeatcounter = 0; btn->repeatcounter = 0;
m_buttons[button].guibutton->setPressed(true);
translated->KeyInput.PressedDown = true; translated->KeyInput.PressedDown = true;
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)) {
auto pos = std::find(btn->ids.begin(), btn->ids.end(), eventID); auto pos = 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());
@ -461,7 +412,6 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button,
if (!btn->ids.empty()) if (!btn->ids.empty())
return; return;
m_buttons[button].guibutton->setPressed(false);
translated->KeyInput.PressedDown = false; translated->KeyInput.PressedDown = false;
btn->repeatcounter = -1; btn->repeatcounter = -1;
m_receiver->OnEvent(*translated); m_receiver->OnEvent(*translated);
@ -473,12 +423,11 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
{ {
touch_gui_button_id button = getButtonID(evt_id); touch_gui_button_id button = getButtonID(evt_id);
// handle button events if (button != after_last_element_id) {
if (button != after_last_element_id) // handle button events
handleButtonEvent(button, evt_id, false); handleButtonEvent(button, evt_id, false);
} else if (evt_id == m_move_id) {
// handle the point used for moving view // handle the point used for moving view
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
@ -515,6 +464,24 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
} }
} }
// 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 {
infostream
<< "TouchScreenGUI::translateEvent released unknown button: "
<< evt_id << std::endl;
}
for (auto iter = m_known_ids.begin(); for (auto iter = m_known_ids.begin();
iter != m_known_ids.end(); ++iter) { iter != m_known_ids.end(); ++iter) {
if (iter->id == evt_id) { if (iter->id == evt_id) {
@ -524,19 +491,12 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id)
} }
} }
void TouchScreenGUI::handleReleaseAll()
{
m_known_ids.clear();
if (m_move_id != -1)
handleReleaseEvent(m_move_id);
for (auto & m_button : m_buttons)
m_button.ids.clear();
}
void TouchScreenGUI::translateEvent(const SEvent &event) void TouchScreenGUI::translateEvent(const SEvent &event)
{ {
if (!m_visible) { if (!m_visible) {
infostream << "TouchScreenGUI::translateEvent got event but not visible?!" << std::endl; infostream
<< "TouchScreenGUI::translateEvent got event but not visible!"
<< std::endl;
return; return;
} }
@ -544,9 +504,11 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
return; return;
if (event.TouchInput.Event == ETIE_PRESSED_DOWN) { if (event.TouchInput.Event == ETIE_PRESSED_DOWN) {
/* add to own copy of eventlist... /*
* android would provide this information but irrlicht guys don't * Add to own copy of event list...
* wanna design a efficient interface */ * android would provide this information but Irrlicht guys don't
* wanna design a efficient interface
*/
id_status toadd{}; id_status toadd{};
toadd.id = event.TouchInput.ID; toadd.id = event.TouchInput.ID;
toadd.X = event.TouchInput.X; toadd.X = event.TouchInput.X;
@ -561,47 +523,61 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
// 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);
// ignore events inside the control pad and HUD if not already handled } else if (isHUDButton(event)) {
} else if (!(m_control_pad_rect.isPointInside(v2s32(toadd.X, toadd.Y)) || isHUDButton(event))) { // already handled in isHUDButton()
} else {
// handle non button events // handle non button events
// if we don't already have a moving point make this the moving one s32 dxj = event.TouchInput.X - button_size * 5.0f / 2.0f;
if (m_move_id == -1) { s32 dyj = event.TouchInput.Y - m_screensize.Y + button_size * 5.0f / 2.0f;
m_move_id = event.TouchInput.ID;
m_move_has_really_moved = false;
m_move_downtime = porting::getTimeMs();
m_move_downlocation = v2s32(event.TouchInput.X, event.TouchInput.Y);
m_move_sent_as_mouse_event = false;
// update shootline (in case the game handles the event we send below) /* Select joystick when left 1/3 of screen dragged or
m_shootline = m_device * when joystick tapped (fixed joystick position)
->getSceneManager() */
->getSceneCollisionManager() if ((m_fixed_joystick && dxj * dxj + dyj * dyj <= button_size * button_size * 1.5 * 1.5) ||
->getRayFromScreenCoordinates(m_move_downlocation); (!m_fixed_joystick && event.TouchInput.X < m_screensize.X / 3.0f)) {
// 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;
// send a middle click event so the game can handle single touches m_joystick_btn_off->guibutton->setVisible(false);
auto *translated = new SEvent; m_joystick_btn_bg->guibutton->setVisible(true);
memset(translated, 0, sizeof(SEvent)); m_joystick_btn_center->guibutton->setVisible(true);
translated->EventType = EET_MOUSE_INPUT_EVENT;
translated->MouseInput.X = m_move_downlocation.X; // If it's a fixed joystick, don't move the joystick "button".
translated->MouseInput.Y = m_move_downlocation.Y; if (!m_fixed_joystick)
translated->MouseInput.ButtonStates = EMBSM_MIDDLE; // << important! m_joystick_btn_bg->guibutton->setRelativePosition(v2s32(
translated->MouseInput.Event = EMIE_MMOUSE_LEFT_UP; event.TouchInput.X - button_size * 3.0f / 1.5f,
m_receiver->OnEvent(*translated); event.TouchInput.Y - button_size * 3.0f / 1.5f));
delete translated;
m_joystick_btn_center->guibutton->setRelativePosition(v2s32(
event.TouchInput.X - button_size / 2.0f * 1.5f,
event.TouchInput.Y - button_size / 2.0f * 1.5f));
}
} else {
// If we don't already have a moving point make this the moving one.
if (m_move_id == -1) {
m_move_id = event.TouchInput.ID;
m_move_has_really_moved = false;
m_move_downtime = porting::getTimeMs();
m_move_downlocation = v2s32(event.TouchInput.X, event.TouchInput.Y);
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);
} else if (event.TouchInput.Event == ETIE_LEFT_UP) { }
verbosestream << "Up event for pointerid: " << event.TouchInput.ID << std::endl; else if (event.TouchInput.Event == ETIE_LEFT_UP) {
verbosestream
<< "Up event for pointerid: " << event.TouchInput.ID << std::endl;
handleReleaseEvent(event.TouchInput.ID); handleReleaseEvent(event.TouchInput.ID);
} else { } else {
assert(event.TouchInput.Event == ETIE_MOVED); assert(event.TouchInput.Event == ETIE_MOVED);
if (m_pointerpos[event.TouchInput.ID] == if (m_pointerpos[event.TouchInput.ID] ==
v2s32(event.TouchInput.X, event.TouchInput.Y)) { v2s32(event.TouchInput.X, event.TouchInput.Y))
return; return;
}
if (m_move_id != -1) { if (m_move_id != -1) {
if ((event.TouchInput.ID == m_move_id) && if ((event.TouchInput.ID == m_move_id) &&
@ -613,8 +589,8 @@ 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;
@ -644,9 +620,90 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
->getRayFromScreenCoordinates( ->getRayFromScreenCoordinates(
v2s32(event.TouchInput.X, event.TouchInput.Y)); v2s32(event.TouchInput.X, event.TouchInput.Y));
} }
} else {
handleChangedButton(event);
} }
if (m_joystick_id != -1 && event.TouchInput.ID == m_joystick_id) {
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.0f / 2.0f;
s32 dyj = event.TouchInput.Y - 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 ||
(!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 (bool & joystick_status : m_joystick_status)
joystick_status = 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;
}
if (distance > button_size * 1.5) {
m_joystick_status[j_special1] = true;
// move joystick "button"
s32 ndx = button_size * dx / distance * 1.5f - button_size / 2.0f * 1.5f;
s32 ndy = button_size * dy / distance * 1.5f - button_size / 2.0f * 1.5f;
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 {
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 * 1.5, Y - button_size / 2 * 1.5));
}
}
}
if (m_move_id == -1 && m_joystick_id == -1)
handleChangedButton(event);
} }
} }
@ -686,7 +743,7 @@ void TouchScreenGUI::handleChangedButton(const SEvent &event)
if (std::find(btn->ids.begin(), btn->ids.end(), event.TouchInput.ID) if (std::find(btn->ids.begin(), btn->ids.end(), event.TouchInput.ID)
== btn->ids.end()) == btn->ids.end())
handleButtonEvent((touch_gui_button_id) current_button_id, handleButtonEvent((touch_gui_button_id) current_button_id,
event.TouchInput.ID, true); event.TouchInput.ID, true);
} }
// Punch or left click // Punch or left click
@ -716,45 +773,81 @@ bool TouchScreenGUI::quickTapDetection()
->getSceneCollisionManager() ->getSceneCollisionManager()
->getRayFromScreenCoordinates(v2s32(m_key_events[0].x, m_key_events[0].y)); ->getRayFromScreenCoordinates(v2s32(m_key_events[0].x, m_key_events[0].y));
translated->MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN; translated->MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
verbosestream << "TouchScreenGUI::translateEvent right click press" << std::endl; verbosestream << "TouchScreenGUI::translateEvent right click press" << std::endl;
m_receiver->OnEvent(*translated); m_receiver->OnEvent(*translated);
translated->MouseInput.ButtonStates = 0; translated->MouseInput.ButtonStates = 0;
translated->MouseInput.Event = EMIE_RMOUSE_LEFT_UP; translated->MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
verbosestream << "TouchScreenGUI::translateEvent right click release" << std::endl; verbosestream << "TouchScreenGUI::translateEvent right click release" << std::endl;
m_receiver->OnEvent(*translated); m_receiver->OnEvent(*translated);
delete translated; delete translated;
return true; return true;
} }
TouchScreenGUI::~TouchScreenGUI() { void TouchScreenGUI::applyJoystickStatus()
for (auto & m_button : m_buttons) { {
button_info *btn = &m_button; for (unsigned int i = 0; i < 5; i++) {
if (btn->guibutton) { if (i == 4)
btn->guibutton->drop(); continue;
btn->guibutton = nullptr;
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()
{
for (auto &button : m_buttons) {
if (button.guibutton) {
button.guibutton->drop();
button.guibutton = nullptr;
}
}
if (m_joystick_btn_off->guibutton) {
m_joystick_btn_off->guibutton->drop();
m_joystick_btn_off->guibutton = nullptr;
}
if (m_joystick_btn_bg->guibutton) {
m_joystick_btn_bg->guibutton->drop();
m_joystick_btn_bg->guibutton = nullptr;
}
if (m_joystick_btn_center->guibutton) {
m_joystick_btn_center->guibutton->drop();
m_joystick_btn_center->guibutton = nullptr;
}
}
void TouchScreenGUI::step(float dtime) void TouchScreenGUI::step(float dtime)
{ {
// simulate keyboard repeats // simulate keyboard repeats
for (auto & m_button : m_buttons) { for (auto &button : m_buttons) {
button_info *btn = &m_button; if (!button.ids.empty()) {
button.repeatcounter += dtime;
if (!btn->ids.empty()) { // in case we're moving around digging does not happen
btn->repeatcounter += dtime; if (m_move_id != -1)
m_move_has_really_moved = true;
if (btn->repeatcounter < btn->repeatdelay) if (button.repeatcounter < button.repeatdelay)
continue; continue;
btn->repeatcounter = 0; button.repeatcounter = 0;
SEvent translated{}; SEvent translated;
memset(&translated, 0, sizeof(SEvent)); memset(&translated, 0, sizeof(SEvent));
translated.EventType = irr::EET_KEY_INPUT_EVENT; translated.EventType = irr::EET_KEY_INPUT_EVENT;
translated.KeyInput.Key = btn->keycode; translated.KeyInput.Key = button.keycode;
translated.KeyInput.PressedDown = false; translated.KeyInput.PressedDown = false;
m_receiver->OnEvent(translated); m_receiver->OnEvent(translated);
@ -763,20 +856,28 @@ void TouchScreenGUI::step(float dtime)
} }
} }
// joystick
for (unsigned int i = 0; i < 4; i++) {
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_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)) {
u64 delta = porting::getDeltaMs(m_move_downtime, porting::getTimeMs()); u64 delta = porting::getDeltaMs(m_move_downtime, porting::getTimeMs());
if (delta > MIN_DIG_TIME) { if (delta > MIN_DIG_TIME_MS) {
m_shootline = m_device m_shootline = m_device
->getSceneManager() ->getSceneManager()
->getSceneCollisionManager() ->getSceneCollisionManager()
->getRayFromScreenCoordinates( ->getRayFromScreenCoordinates(
v2s32(m_move_downlocation.X, m_move_downlocation.Y)); v2s32(m_move_downlocation.X, m_move_downlocation.Y));
SEvent translated{}; SEvent translated;
memset(&translated, 0, sizeof(SEvent)); memset(&translated, 0, sizeof(SEvent));
translated.EventType = EET_MOUSE_INPUT_EVENT; translated.EventType = EET_MOUSE_INPUT_EVENT;
translated.MouseInput.X = m_move_downlocation.X; translated.MouseInput.X = m_move_downlocation.X;
@ -805,11 +906,14 @@ void TouchScreenGUI::registerHudItem(int index, const rect<s32> &rect)
void TouchScreenGUI::Toggle(bool visible) void TouchScreenGUI::Toggle(bool visible)
{ {
m_visible = visible; m_visible = visible;
for (auto & m_button : m_buttons) { for (auto &button : m_buttons) {
if (m_button.guibutton) if (button.guibutton)
m_button.guibutton->setVisible(visible); button.guibutton->setVisible(visible);
} }
if (m_joystick_btn_off->guibutton)
m_joystick_btn_off->guibutton->setVisible(visible);
// clear all active buttons // clear all active buttons
if (!visible) { if (!visible) {
while (!m_known_ids.empty()) while (!m_known_ids.empty())
@ -832,3 +936,12 @@ void TouchScreenGUI::show()
Toggle(true); Toggle(true);
} }
void TouchScreenGUI::handleReleaseAll()
{
m_known_ids.clear();
if (m_move_id != -1)
handleReleaseEvent(m_move_id);
for (auto &button : m_buttons)
button.ids.clear();
}

View File

@ -35,15 +35,6 @@ using namespace irr::gui;
typedef enum typedef enum
{ {
forward_one,
forward_two,
forward_three,
backward_one,
backward_two,
backward_three,
left_id,
right_id,
empty_id,
inventory_id, inventory_id,
drop_id, drop_id,
jump_id, jump_id,
@ -55,28 +46,46 @@ typedef enum
chat_id, chat_id,
// noclip_id, // noclip_id,
// fast_id, // fast_id,
after_last_element_id after_last_element_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;
#define MIN_DIG_TIME 500 typedef enum
{
j_forward = 0,
j_backward,
j_left,
j_right,
j_special1
} touch_gui_joystick_move_id;
#define MIN_DIG_TIME_MS 500
#define BUTTON_REPEAT_DELAY 0.2f #define BUTTON_REPEAT_DELAY 0.2f
#define SLOW_BUTTON_REPEAT 1.0f #define SLOW_BUTTON_REPEAT 1.0f
extern const char **button_imagenames;
extern const char **joystick_imagenames;
struct button_info struct button_info
{ {
float repeatcounter; float repeatcounter;
float repeatdelay; float repeatdelay;
irr::EKEY_CODE keycode; irr::EKEY_CODE keycode;
std::vector<size_t> ids; std::vector<size_t> ids;
IGUIButton *guibutton = NULL; IGUIButton *guibutton = nullptr;
bool immediate_release; bool immediate_release;
}; };
class TouchScreenGUI class TouchScreenGUI
{ {
public: public:
TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver); TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver);
~TouchScreenGUI(); ~TouchScreenGUI();
void translateEvent(const SEvent &event); void translateEvent(const SEvent &event);
@ -92,56 +101,70 @@ public:
double getPitch() { return m_camera_pitch; } double getPitch() { return m_camera_pitch; }
/* Returns a line which describes what the player is pointing at. /*
* Returns a line which describes what the player is pointing at.
* The starting point and looking direction are significant, * The starting point and looking direction are significant,
* the line should be scaled to match its length to the actual distance * the line should be scaled to match its length to the actual distance
* the player can reach. * the player can reach.
* The line starts at the camera and ends on the camera's far plane. * The line starts at the camera and ends on the camera's far plane.
* The coordinates do not contain the camera offset. */ * The coordinates do not contain the camera offset.
*/
line3d<f32> getShootline() { return m_shootline; } line3d<f32> getShootline() { return m_shootline; }
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);
void Toggle(bool visible); void Toggle(bool visible);
void hide(); void hide();
void show(); void show();
// handle all buttons
void handleReleaseAll(); void handleReleaseAll();
private: private:
IrrlichtDevice *m_device; IrrlichtDevice *m_device;
IGUIEnvironment *m_guienv; IGUIEnvironment *m_guienv;
IEventReceiver *m_receiver; IEventReceiver *m_receiver;
ISimpleTextureSource *m_texturesource; ISimpleTextureSource *m_texturesource;
v2u32 m_screensize; v2u32 m_screensize;
s32 button_size;
double m_touchscreen_threshold; double m_touchscreen_threshold;
std::map<int, rect<s32> > m_hud_rects; std::map<int, rect<s32>> m_hud_rects;
std::map<size_t, irr::EKEY_CODE> m_hud_ids; std::map<size_t, 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; double m_camera_yaw_change = 0.0;
double m_camera_pitch; double m_camera_pitch = 0.0;
/* A line starting at the camera and pointing towards the // forward, backward, left, right
touch_gui_button_id m_joystick_names[5] = {
forward_id, backward_id, left_id, right_id};
bool m_joystick_status[5] = {false, false, false, false};
/*
* A line starting at the camera and pointing towards the
* selected object. * selected object.
* The line ends on the camera's far plane. * The line ends on the camera's far plane.
* The coordinates do not contain the camera offset. */ * The coordinates do not contain the camera offset.
*/
line3d<f32> m_shootline; line3d<f32> m_shootline;
rect<s32> m_control_pad_rect; rect<s32> m_control_pad_rect;
size_t m_move_id; size_t m_move_id = -1;
bool m_move_has_really_moved; bool m_move_has_really_moved = false;
u64 m_move_downtime; u64 m_move_downtime = 0;
bool m_move_sent_as_mouse_event; bool m_move_sent_as_mouse_event = false;
v2s32 m_move_downlocation; v2s32 m_move_downlocation = v2s32(-10000, -10000);
size_t m_joystick_id = -1;
bool m_joystick_has_really_moved = false;
bool m_fixed_joystick = true;
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];
@ -156,8 +179,13 @@ private:
// initialize a button // initialize a button
void initButton(touch_gui_button_id id, const rect<s32> &button_rect, void initButton(touch_gui_button_id id, const rect<s32> &button_rect,
const std::wstring &caption, bool immediate_release, const std::wstring &caption, bool immediate_release,
float repeat_delay = BUTTON_REPEAT_DELAY); float repeat_delay = SLOW_BUTTON_REPEAT);
// initialize a joystick button
button_info *initJoystickButton(touch_gui_button_id id,
const rect<s32> &button_rect, int texture_id,
bool visible = true);
struct id_status struct id_status
{ {
@ -175,12 +203,15 @@ private:
// handle pressed hud buttons // handle pressed hud buttons
bool isHUDButton(const SEvent &event); bool isHUDButton(const SEvent &event);
// handle quick touch // handle quick taps
bool quickTapDetection(); bool quickTapDetection();
// handle release event // handle release event
void handleReleaseEvent(size_t evt_id); void handleReleaseEvent(size_t evt_id);
// apply joystick status
void applyJoystickStatus();
// long-click detection variables // long-click detection variables
struct key_event struct key_event
{ {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 283 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 627 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 278 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 639 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 595 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 221 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 220 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 504 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 259 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 570 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 280 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 630 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 586 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 234 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 575 B