diff --git a/src/client/inputhandler.cpp b/src/client/inputhandler.cpp index a6ba87e8d..7b54ac93d 100644 --- a/src/client/inputhandler.cpp +++ b/src/client/inputhandler.cpp @@ -104,7 +104,7 @@ bool MyEventReceiver::OnEvent(const SEvent &event) if (isMenuActive()) { #ifdef HAVE_TOUCHSCREENGUI if (m_touchscreengui) { - m_touchscreengui->Toggle(false); + m_touchscreengui->setVisible(false); } #endif return g_menumgr.preprocessEvent(event); diff --git a/src/gui/touchscreengui.cpp b/src/gui/touchscreengui.cpp index 7d706b927..57b81d955 100644 --- a/src/gui/touchscreengui.cpp +++ b/src/gui/touchscreengui.cpp @@ -19,50 +19,45 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "touchscreengui.h" -#include "irrlichttypes.h" + +#include "gettime.h" #include "irr_v2d.h" #include "log.h" -#include "client/keycode.h" -#include "settings.h" -#include "gettime.h" -#include "util/numeric.h" #include "porting.h" +#include "settings.h" #include "client/guiscalingfilter.h" +#include "client/keycode.h" #include "client/renderingengine.h" +#include "util/numeric.h" #include #include using namespace irr::core; -const char *button_imagenames[] = { +TouchScreenGUI *g_touchscreengui; + +const std::string button_image_names[] = { "jump_btn.png", "down.png", "zoom.png", "aux1_btn.png" }; -const char *joystick_imagenames[] = { +const std::string joystick_image_names[] = { "joystick_off.png", "joystick_bg.png", "joystick_center.png" }; -static irr::EKEY_CODE id2keycode(touch_gui_button_id id) +static EKEY_CODE id_to_keycode(touch_gui_button_id id) { // ESC isn't part of the keymap. - if (id == exit_id) { + if (id == exit_id) return KEY_ESCAPE; - } std::string key = ""; switch (id) { - case inventory_id: - key = "inventory"; - break; - case drop_id: - key = "drop"; - break; case jump_id: key = "jump"; break; @@ -87,21 +82,27 @@ static irr::EKEY_CODE id2keycode(touch_gui_button_id id) case debug_id: key = "toggle_debug"; break; - case toggle_chat_id: - key = "toggle_chat"; - break; - case minimap_id: - key = "minimap"; - break; - case chat_id: - key = "chat"; - break; case camera_id: key = "camera_mode"; break; case range_id: key = "rangeselect"; break; + case minimap_id: + key = "minimap"; + break; + case toggle_chat_id: + key = "toggle_chat"; + break; + case chat_id: + key = "chat"; + break; + case inventory_id: + key = "inventory"; + break; + case drop_id: + key = "drop"; + break; default: break; } @@ -109,29 +110,27 @@ static irr::EKEY_CODE id2keycode(touch_gui_button_id id) return keyname_to_keycode(g_settings->get("keymap_" + key).c_str()); } -TouchScreenGUI *g_touchscreengui; - -static void load_button_texture(button_info *btn, const char *path, +static void load_button_texture(const button_info *btn, const std::string &path, const rect &button_rect, ISimpleTextureSource *tsrc, video::IVideoDriver *driver) { - unsigned int tid; + u32 tid; video::ITexture *texture = guiScalingImageButton(driver, tsrc->getTexture(path, &tid), button_rect.getWidth(), button_rect.getHeight()); if (texture) { - btn->guibutton->setUseAlphaChannel(true); + btn->gui_button->setUseAlphaChannel(true); if (g_settings->getBool("gui_scaling_filter")) { rect txr_rect = rect(0, 0, button_rect.getWidth(), button_rect.getHeight()); - btn->guibutton->setImage(texture, txr_rect); - btn->guibutton->setPressedImage(texture, txr_rect); - btn->guibutton->setScaleImage(false); + btn->gui_button->setImage(texture, txr_rect); + btn->gui_button->setPressedImage(texture, txr_rect); + btn->gui_button->setScaleImage(false); } else { - btn->guibutton->setImage(texture); - btn->guibutton->setPressedImage(texture); - btn->guibutton->setScaleImage(true); + btn->gui_button->setImage(texture); + btn->gui_button->setPressedImage(texture); + btn->gui_button->setScaleImage(true); } - btn->guibutton->setDrawBorder(false); - btn->guibutton->setText(L""); + btn->gui_button->setDrawBorder(false); + btn->gui_button->setText(L""); } } @@ -144,7 +143,7 @@ AutoHideButtonBar::AutoHideButtonBar(IrrlichtDevice *device, } void AutoHideButtonBar::init(ISimpleTextureSource *tsrc, - const char *starter_img, int button_id, const v2s32 &UpperLeft, + const std::string &starter_img, int button_id, const v2s32 &UpperLeft, const v2s32 &LowerRight, autohide_button_bar_dir dir, float timeout) { m_texturesource = tsrc; @@ -152,19 +151,19 @@ void AutoHideButtonBar::init(ISimpleTextureSource *tsrc, m_upper_left = UpperLeft; m_lower_right = LowerRight; - // init settings bar + rect starter_rect = rect(UpperLeft.X, UpperLeft.Y, LowerRight.X, LowerRight.Y); - irr::core::rect current_button = rect(UpperLeft.X, UpperLeft.Y, - LowerRight.X, LowerRight.Y); + IGUIButton *starter_gui_button = m_guienv->addButton(starter_rect, nullptr, + button_id, L"", nullptr); - m_starter.guibutton = m_guienv->addButton(current_button, nullptr, button_id, L"", nullptr); - m_starter.guibutton->grab(); - m_starter.repeatcounter = -1; + m_starter.gui_button = starter_gui_button; + m_starter.gui_button->grab(); + m_starter.repeat_counter = -1.0f; m_starter.keycode = KEY_OEM_8; // use invalid keycode as it's not relevant m_starter.immediate_release = true; m_starter.ids.clear(); - load_button_texture(&m_starter, starter_img, current_button, + load_button_texture(&m_starter, starter_img, starter_rect, m_texturesource, m_driver); m_dir = dir; @@ -175,88 +174,94 @@ void AutoHideButtonBar::init(ISimpleTextureSource *tsrc, AutoHideButtonBar::~AutoHideButtonBar() { - if (m_starter.guibutton) { - m_starter.guibutton->setVisible(false); - m_starter.guibutton->drop(); + if (m_starter.gui_button) { + m_starter.gui_button->setVisible(false); + m_starter.gui_button->drop(); + m_starter.gui_button = nullptr; + } + + for (auto &button : m_buttons) { + if (button->gui_button) { + button->gui_button->drop(); + button->gui_button = nullptr; + } } } -void AutoHideButtonBar::addButton(touch_gui_button_id button_id, - const wchar_t *caption, const char *btn_image) +void AutoHideButtonBar::addButton(touch_gui_button_id button_id, const wchar_t *caption, + const std::string &btn_image) { if (!m_initialized) { - errorstream << "AutoHideButtonBar::addButton not yet initialized!" - << std::endl; + errorstream << "AutoHideButtonBar::addButton not yet initialized!" << std::endl; return; } + int button_size = 0; - if ((m_dir == AHBB_Dir_Top_Bottom) || (m_dir == AHBB_Dir_Bottom_Top)) + if (m_dir == AHBB_Dir_Top_Bottom || m_dir == AHBB_Dir_Bottom_Top) button_size = m_lower_right.X - m_upper_left.X; else button_size = m_lower_right.Y - m_upper_left.Y; irr::core::rect current_button; - if ((m_dir == AHBB_Dir_Right_Left) || (m_dir == AHBB_Dir_Left_Right)) { + if (m_dir == AHBB_Dir_Right_Left || m_dir == AHBB_Dir_Left_Right) { int x_start = 0; int x_end = 0; if (m_dir == AHBB_Dir_Left_Right) { - x_start = m_lower_right.X + (button_size * 1.25 * m_buttons.size()) - + (button_size * 0.25); + x_start = m_lower_right.X + button_size * 1.25f * m_buttons.size() + + button_size * 0.25f; x_end = x_start + button_size; } else { - x_end = m_upper_left.X - (button_size * 1.25 * m_buttons.size()) - - (button_size * 0.25); + x_end = m_upper_left.X - button_size * 1.25f * m_buttons.size() + - button_size * 0.25f; x_start = x_end - button_size; } - current_button = rect(x_start, m_upper_left.Y, x_end, - m_lower_right.Y); + current_button = rect(x_start, m_upper_left.Y, x_end, m_lower_right.Y); } else { double y_start = 0; double y_end = 0; if (m_dir == AHBB_Dir_Top_Bottom) { - y_start = m_lower_right.X + (button_size * 1.25 * m_buttons.size()) - + (button_size * 0.25); + y_start = m_lower_right.X + button_size * 1.25f * m_buttons.size() + + button_size * 0.25f; y_end = y_start + button_size; } else { - y_end = m_upper_left.X - (button_size * 1.25 * m_buttons.size()) - - (button_size * 0.25); + y_end = m_upper_left.X - button_size * 1.25f * m_buttons.size() + - button_size * 0.25f; y_start = y_end - button_size; } - current_button = rect(m_upper_left.X, y_start, - m_lower_right.Y, y_end); + current_button = rect(m_upper_left.X, y_start, m_lower_right.Y, y_end); } - auto *btn = new button_info(); - btn->guibutton = m_guienv->addButton(current_button, - nullptr, button_id, caption, nullptr); - btn->guibutton->grab(); - btn->guibutton->setVisible(false); - btn->guibutton->setEnabled(false); - btn->repeatcounter = -1; - btn->keycode = id2keycode(button_id); + IGUIButton *btn_gui_button = m_guienv->addButton(current_button, nullptr, button_id, + caption, nullptr); + + std::shared_ptr btn(new button_info); + btn->gui_button = btn_gui_button; + btn->gui_button->grab(); + btn->gui_button->setVisible(false); + btn->gui_button->setEnabled(false); + btn->repeat_counter = -1.0f; + btn->keycode = id_to_keycode(button_id); btn->immediate_release = true; btn->ids.clear(); - load_button_texture(btn, btn_image, current_button, m_texturesource, - m_driver); + load_button_texture(btn.get(), btn_image, current_button, m_texturesource, m_driver); m_buttons.push_back(btn); } -void AutoHideButtonBar::addToggleButton(touch_gui_button_id button_id, - const wchar_t *caption, const char *btn_image_1, - const char *btn_image_2) +void AutoHideButtonBar::addToggleButton(touch_gui_button_id button_id, const wchar_t *caption, + const std::string &btn_image_1, const std::string &btn_image_2) { addButton(button_id, caption, btn_image_1); - button_info *btn = m_buttons.back(); - btn->togglable = 1; + std::shared_ptr btn = m_buttons.back(); + btn->toggleable = button_info::FIRST_TEXTURE; btn->textures.push_back(btn_image_1); btn->textures.push_back(btn_image_2); } @@ -269,71 +274,61 @@ bool AutoHideButtonBar::isButton(const SEvent &event) return false; gui::IGUIElement *element = rootguielement->getElementFromPoint( - core::position2d(event.TouchInput.X, event.TouchInput.Y)); + v2s32(event.TouchInput.X, event.TouchInput.Y)); if (element == nullptr) return false; if (m_active) { // check for all buttons in vector - auto iter = m_buttons.begin(); - - while (iter != m_buttons.end()) { - if ((*iter)->guibutton == element) { - - auto *translated = new SEvent(); - memset(translated, 0, sizeof(SEvent)); - translated->EventType = irr::EET_KEY_INPUT_EVENT; - translated->KeyInput.Key = (*iter)->keycode; - translated->KeyInput.Control = false; - translated->KeyInput.Shift = false; - translated->KeyInput.Char = 0; + for (const auto &button : m_buttons) { + if (button->gui_button == element) { + SEvent translated{}; + translated.EventType = irr::EET_KEY_INPUT_EVENT; + translated.KeyInput.Key = button->keycode; + translated.KeyInput.Control = false; + translated.KeyInput.Shift = false; + translated.KeyInput.Char = 0; // add this event - translated->KeyInput.PressedDown = true; - m_receiver->OnEvent(*translated); + translated.KeyInput.PressedDown = true; + m_receiver->OnEvent(translated); // remove this event - translated->KeyInput.PressedDown = false; - m_receiver->OnEvent(*translated); + translated.KeyInput.PressedDown = false; + m_receiver->OnEvent(translated); - delete translated; + button->ids.push_back(event.TouchInput.ID); - (*iter)->ids.push_back(event.TouchInput.ID); + m_timeout = 0.0f; - m_timeout = 0; - - if ((*iter)->togglable == 1) { - (*iter)->togglable = 2; - load_button_texture(*iter, (*iter)->textures[1], - (*iter)->guibutton->getRelativePosition(), + if (button->toggleable == button_info::FIRST_TEXTURE) { + button->toggleable = button_info::SECOND_TEXTURE; + load_button_texture(button.get(), button->textures[1], + button->gui_button->getRelativePosition(), m_texturesource, m_driver); - } else if ((*iter)->togglable == 2) { - (*iter)->togglable = 1; - load_button_texture(*iter, (*iter)->textures[0], - (*iter)->guibutton->getRelativePosition(), + } else if (button->toggleable == button_info::SECOND_TEXTURE) { + button->toggleable = button_info::FIRST_TEXTURE; + load_button_texture(button.get(), button->textures[0], + button->gui_button->getRelativePosition(), m_texturesource, m_driver); } return true; } - ++iter; } } else { // check for starter button only - if (element == m_starter.guibutton) { + if (element == m_starter.gui_button) { m_starter.ids.push_back(event.TouchInput.ID); - m_starter.guibutton->setVisible(false); - m_starter.guibutton->setEnabled(false); + m_starter.gui_button->setVisible(false); + m_starter.gui_button->setEnabled(false); m_active = true; - m_timeout = 0; + m_timeout = 0.0f; - auto iter = m_buttons.begin(); - - while (iter != m_buttons.end()) { - (*iter)->guibutton->setVisible(true); - (*iter)->guibutton->setEnabled(true); - ++iter; + for (const auto &button : m_buttons) { + button->gui_button->setVisible(true); + button->gui_button->setEnabled(true); } return true; @@ -355,32 +350,26 @@ void AutoHideButtonBar::step(float dtime) void AutoHideButtonBar::deactivate() { if (m_visible) { - m_starter.guibutton->setVisible(true); - m_starter.guibutton->setEnabled(true); + m_starter.gui_button->setVisible(true); + m_starter.gui_button->setEnabled(true); } m_active = false; - auto iter = m_buttons.begin(); - - while (iter != m_buttons.end()) { - (*iter)->guibutton->setVisible(false); - (*iter)->guibutton->setEnabled(false); - ++iter; + for (const auto &button : m_buttons) { + button->gui_button->setVisible(false); + button->gui_button->setEnabled(false); } } void AutoHideButtonBar::hide() { m_visible = false; - m_starter.guibutton->setVisible(false); - m_starter.guibutton->setEnabled(false); + m_starter.gui_button->setVisible(false); + m_starter.gui_button->setEnabled(false); - auto iter = m_buttons.begin(); - - while (iter != m_buttons.end()) { - (*iter)->guibutton->setVisible(false); - (*iter)->guibutton->setEnabled(false); - ++iter; + for (const auto &button : m_buttons) { + button->gui_button->setVisible(false); + button->gui_button->setEnabled(false); } } @@ -389,30 +378,27 @@ void AutoHideButtonBar::show() m_visible = true; if (m_active) { - auto iter = m_buttons.begin(); - - while (iter != m_buttons.end()) { - (*iter)->guibutton->setVisible(true); - (*iter)->guibutton->setEnabled(true); - ++iter; + for (const auto &button : m_buttons) { + button->gui_button->setVisible(true); + button->gui_button->setEnabled(true); } } else { - m_starter.guibutton->setVisible(true); - m_starter.guibutton->setEnabled(true); + m_starter.gui_button->setVisible(true); + m_starter.gui_button->setEnabled(true); } } TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver): - m_device(device), - m_guienv(device->getGUIEnvironment()), - m_receiver(receiver), - m_settingsbar(device, receiver), - m_rarecontrolsbar(device, receiver) + m_device(device), + m_guienv(device->getGUIEnvironment()), + m_receiver(receiver), + m_settings_bar(device, receiver), + m_rare_controls_bar(device, receiver) { for (auto &button : m_buttons) { - button.guibutton = nullptr; - button.repeatcounter = -1; - button.repeatdelay = BUTTON_REPEAT_DELAY; + button.gui_button = nullptr; + button.repeat_counter = -1.0f; + button.repeat_delay = BUTTON_REPEAT_DELAY; } m_touchscreen_threshold = g_settings->getU16("touchscreen_threshold"); @@ -420,37 +406,41 @@ TouchScreenGUI::TouchScreenGUI(IrrlichtDevice *device, IEventReceiver *receiver) m_joystick_triggers_aux1 = g_settings->getBool("virtual_joystick_triggers_aux1"); m_screensize = m_device->getVideoDriver()->getScreenSize(); button_size = MYMIN(m_screensize.Y / 4.5f, - RenderingEngine::getDisplayDensity() * - g_settings->getFloat("hud_scaling") * 65.0f); + RenderingEngine::getDisplayDensity() * 65.0f * + g_settings->getFloat("hud_scaling")); } void TouchScreenGUI::initButton(touch_gui_button_id id, const rect &button_rect, const std::wstring &caption, bool immediate_release, float repeat_delay) { + IGUIButton *btn_gui_button = m_guienv->addButton(button_rect, nullptr, id, caption.c_str()); + button_info *btn = &m_buttons[id]; - btn->guibutton = m_guienv->addButton(button_rect, nullptr, id, caption.c_str()); - btn->guibutton->grab(); - btn->repeatcounter = -1; - btn->repeatdelay = repeat_delay; - btn->keycode = id2keycode(id); + btn->gui_button = btn_gui_button; + btn->gui_button->grab(); + btn->repeat_counter = -1.0f; + btn->repeat_delay = repeat_delay; + btn->keycode = id_to_keycode(id); btn->immediate_release = immediate_release; btn->ids.clear(); - load_button_texture(btn, button_imagenames[id], button_rect, + load_button_texture(btn, button_image_names[id], button_rect, m_texturesource, m_device->getVideoDriver()); } -button_info *TouchScreenGUI::initJoystickButton(touch_gui_button_id id, +std::shared_ptr TouchScreenGUI::initJoystickButton(touch_gui_button_id id, const rect &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(); + IGUIButton *btn_gui_button = m_guienv->addButton(button_rect, nullptr, id, L"O"); + + std::shared_ptr btn(new button_info); + btn->gui_button = btn_gui_button; + btn->gui_button->setVisible(visible); + btn->gui_button->grab(); btn->ids.clear(); - load_button_texture(btn, joystick_imagenames[texture_id], - button_rect, m_texturesource, m_device->getVideoDriver()); + load_button_texture(btn.get(), joystick_image_names[texture_id], button_rect, + m_texturesource, m_device->getVideoDriver()); return btn; } @@ -462,9 +452,8 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc) m_visible = true; m_texturesource = tsrc; - /* Init joystick display "button" - * Joystick is placed on bottom left of screen. - */ + // Initialize joystick display "button". + // Joystick is placed on the bottom left of screen. if (m_fixed_joystick) { m_joystick_btn_off = initJoystickButton(joystick_off_id, rect(button_size, @@ -491,72 +480,72 @@ void TouchScreenGUI::init(ISimpleTextureSource *tsrc) // init jump button initButton(jump_id, - rect(m_screensize.X - (1.75 * button_size), + rect(m_screensize.X - 1.75f * button_size, m_screensize.Y - button_size, - m_screensize.X - (0.25 * button_size), + m_screensize.X - 0.25f * button_size, m_screensize.Y), L"x", false); // init crunch button initButton(crunch_id, - rect(m_screensize.X - (3.25 * button_size), + rect(m_screensize.X - 3.25f * button_size, m_screensize.Y - button_size, - m_screensize.X - (1.75 * button_size), + m_screensize.X - 1.75f * button_size, m_screensize.Y), L"H", false); // init zoom button initButton(zoom_id, - rect(m_screensize.X - (1.25 * button_size), - m_screensize.Y - (4 * button_size), - m_screensize.X - (0.25 * button_size), - m_screensize.Y - (3 * button_size)), + rect(m_screensize.X - 1.25f * button_size, + m_screensize.Y - 4 * button_size, + m_screensize.X - 0.25f * button_size, + m_screensize.Y - 3 * button_size), L"z", false); // init aux1 button if (!m_joystick_triggers_aux1) initButton(aux1_id, - rect(m_screensize.X - (1.25 * button_size), - m_screensize.Y - (2.5 * button_size), - m_screensize.X - (0.25 * button_size), - m_screensize.Y - (1.5 * button_size)), + rect(m_screensize.X - 1.25f * button_size, + m_screensize.Y - 2.5f * button_size, + m_screensize.X - 0.25f * button_size, + m_screensize.Y - 1.5f * button_size), L"spc1", false); - m_settingsbar.init(m_texturesource, "gear_icon.png", settings_starter_id, - v2s32(m_screensize.X - (1.25 * button_size), - m_screensize.Y - ((SETTINGS_BAR_Y_OFFSET + 1.0) * button_size) - + (0.5 * button_size)), - v2s32(m_screensize.X - (0.25 * button_size), - m_screensize.Y - (SETTINGS_BAR_Y_OFFSET * button_size) - + (0.5 * button_size)), - AHBB_Dir_Right_Left, 3.0); + m_settings_bar.init(m_texturesource, "gear_icon.png", settings_starter_id, + v2s32(m_screensize.X - 1.25f * button_size, + m_screensize.Y - (SETTINGS_BAR_Y_OFFSET + 1.0f) * button_size + + 0.5f * button_size), + v2s32(m_screensize.X - 0.25f * button_size, + m_screensize.Y - SETTINGS_BAR_Y_OFFSET * button_size + + 0.5f * button_size), + AHBB_Dir_Right_Left, 3.0f); - m_settingsbar.addButton(fly_id, L"fly", "fly_btn.png"); - m_settingsbar.addButton(noclip_id, L"noclip", "noclip_btn.png"); - m_settingsbar.addButton(fast_id, L"fast", "fast_btn.png"); - m_settingsbar.addButton(debug_id, L"debug", "debug_btn.png"); - m_settingsbar.addButton(camera_id, L"camera", "camera_btn.png"); - m_settingsbar.addButton(range_id, L"rangeview", "rangeview_btn.png"); - m_settingsbar.addButton(minimap_id, L"minimap", "minimap_btn.png"); + m_settings_bar.addButton(fly_id, L"fly", "fly_btn.png"); + m_settings_bar.addButton(noclip_id, L"noclip", "noclip_btn.png"); + m_settings_bar.addButton(fast_id, L"fast", "fast_btn.png"); + m_settings_bar.addButton(debug_id, L"debug", "debug_btn.png"); + m_settings_bar.addButton(camera_id, L"camera", "camera_btn.png"); + m_settings_bar.addButton(range_id, L"rangeview", "rangeview_btn.png"); + m_settings_bar.addButton(minimap_id, L"minimap", "minimap_btn.png"); // Chat is shown by default, so chat_hide_btn.png is shown first. - m_settingsbar.addToggleButton(toggle_chat_id, L"togglechat", + m_settings_bar.addToggleButton(toggle_chat_id, L"togglechat", "chat_hide_btn.png", "chat_show_btn.png"); - m_rarecontrolsbar.init(m_texturesource, "rare_controls.png", - rare_controls_starter_id, - v2s32(0.25 * button_size, - m_screensize.Y - ((RARE_CONTROLS_BAR_Y_OFFSET + 1.0) * button_size) - + (0.5 * button_size)), - v2s32(0.75 * button_size, - m_screensize.Y - (RARE_CONTROLS_BAR_Y_OFFSET * button_size) - + (0.5 * button_size)), - AHBB_Dir_Left_Right, 2.0); + m_rare_controls_bar.init(m_texturesource, "rare_controls.png", + rare_controls_starter_id, + v2s32(0.25f * button_size, + m_screensize.Y - (RARE_CONTROLS_BAR_Y_OFFSET + 1.0f) * button_size + + 0.5f * button_size), + v2s32(0.75f * button_size, + m_screensize.Y - RARE_CONTROLS_BAR_Y_OFFSET * button_size + + 0.5f * button_size), + AHBB_Dir_Left_Right, 2.0f); - m_rarecontrolsbar.addButton(chat_id, L"chat", "chat_btn.png"); - m_rarecontrolsbar.addButton(inventory_id, L"inv", "inventory_btn.png"); - m_rarecontrolsbar.addButton(drop_id, L"drop", "drop_btn.png"); - m_rarecontrolsbar.addButton(exit_id, L"exit", "exit_btn.png"); + m_rare_controls_bar.addButton(chat_id, L"chat", "chat_btn.png"); + m_rare_controls_bar.addButton(inventory_id, L"inv", "inventory_btn.png"); + m_rare_controls_bar.addButton(drop_id, L"drop", "drop_btn.png"); + m_rare_controls_bar.addButton(exit_id, L"exit", "exit_btn.png"); m_initialized = true; } @@ -567,11 +556,11 @@ touch_gui_button_id TouchScreenGUI::getButtonID(s32 x, s32 y) if (rootguielement != nullptr) { gui::IGUIElement *element = - rootguielement->getElementFromPoint(core::position2d(x, y)); + rootguielement->getElementFromPoint(v2s32(x, y)); if (element) for (unsigned int i = 0; i < after_last_element_id; i++) - if (element == m_buttons[i].guibutton) + if (element == m_buttons[i].gui_button) return (touch_gui_button_id) i; } @@ -596,18 +585,15 @@ bool TouchScreenGUI::isHUDButton(const SEvent &event) { // check if hud item is pressed for (auto &hud_rect : m_hud_rects) { - if (hud_rect.second.isPointInside(v2s32(event.TouchInput.X, - event.TouchInput.Y))) { - auto *translated = new SEvent(); - memset(translated, 0, sizeof(SEvent)); - translated->EventType = irr::EET_KEY_INPUT_EVENT; - translated->KeyInput.Key = (irr::EKEY_CODE) (KEY_KEY_1 + hud_rect.first); - translated->KeyInput.Control = false; - translated->KeyInput.Shift = false; - translated->KeyInput.PressedDown = true; - m_receiver->OnEvent(*translated); - m_hud_ids[event.TouchInput.ID] = translated->KeyInput.Key; - delete translated; + if (hud_rect.second.isPointInside(v2s32(event.TouchInput.X, event.TouchInput.Y))) { + SEvent translated{}; + translated.EventType = irr::EET_KEY_INPUT_EVENT; + translated.KeyInput.Key = (irr::EKEY_CODE) (KEY_KEY_1 + hud_rect.first); + translated.KeyInput.Control = false; + translated.KeyInput.Shift = false; + translated.KeyInput.PressedDown = true; + m_receiver->OnEvent(translated); + m_hud_ids[event.TouchInput.ID] = translated.KeyInput.Key; return true; } } @@ -618,13 +604,12 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, size_t eventID, bool action) { button_info *btn = &m_buttons[button]; - auto *translated = new SEvent(); - memset(translated, 0, sizeof(SEvent)); - translated->EventType = irr::EET_KEY_INPUT_EVENT; - translated->KeyInput.Key = btn->keycode; - translated->KeyInput.Control = false; - translated->KeyInput.Shift = false; - translated->KeyInput.Char = 0; + SEvent translated{}; + translated.EventType = irr::EET_KEY_INPUT_EVENT; + translated.KeyInput.Key = btn->keycode; + translated.KeyInput.Control = false; + translated.KeyInput.Shift = false; + translated.KeyInput.Char = 0; // add this event if (action) { @@ -632,16 +617,17 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, btn->ids.push_back(eventID); - if (btn->ids.size() > 1) return; + if (btn->ids.size() > 1) + return; - btn->repeatcounter = 0; - translated->KeyInput.PressedDown = true; - translated->KeyInput.Key = btn->keycode; - m_receiver->OnEvent(*translated); + btn->repeat_counter = 0.0f; + translated.KeyInput.PressedDown = true; + translated.KeyInput.Key = btn->keycode; + m_receiver->OnEvent(translated); } // remove event - if ((!action) || (btn->immediate_release)) { + if (!action || btn->immediate_release) { auto pos = std::find(btn->ids.begin(), btn->ids.end(), eventID); // has to be in touch list assert(pos != btn->ids.end()); @@ -650,18 +636,16 @@ void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button, if (!btn->ids.empty()) return; - translated->KeyInput.PressedDown = false; - btn->repeatcounter = -1; - m_receiver->OnEvent(*translated); + translated.KeyInput.PressedDown = false; + btn->repeat_counter = -1.0f; + m_receiver->OnEvent(translated); } - delete translated; } void TouchScreenGUI::handleReleaseEvent(size_t evt_id) { touch_gui_button_id button = getButtonID(evt_id); - if (button != after_last_element_id) { // handle button events handleButtonEvent(button, evt_id, false); @@ -671,21 +655,19 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id) // if this pointer issued a mouse event issue symmetric release here if (m_move_sent_as_mouse_event) { - auto *translated = new SEvent; - memset(translated, 0, sizeof(SEvent)); - translated->EventType = EET_MOUSE_INPUT_EVENT; - translated->MouseInput.X = m_move_downlocation.X; - translated->MouseInput.Y = m_move_downlocation.Y; - translated->MouseInput.Shift = false; - translated->MouseInput.Control = false; - translated->MouseInput.ButtonStates = 0; - translated->MouseInput.Event = EMIE_LMOUSE_LEFT_UP; + SEvent translated {}; + translated.EventType = EET_MOUSE_INPUT_EVENT; + translated.MouseInput.X = m_move_downlocation.X; + translated.MouseInput.Y = m_move_downlocation.Y; + translated.MouseInput.Shift = false; + translated.MouseInput.Control = false; + translated.MouseInput.ButtonStates = 0; + translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP; if (m_draw_crosshair) { - translated->MouseInput.X = m_screensize.X / 2; - translated->MouseInput.Y = m_screensize.Y / 2; + translated.MouseInput.X = m_screensize.X / 2; + translated.MouseInput.Y = m_screensize.Y / 2; } - m_receiver->OnEvent(*translated); - delete translated; + m_receiver->OnEvent(translated); } else if (!m_move_has_really_moved) { doRightClick(); } @@ -701,17 +683,15 @@ void TouchScreenGUI::handleReleaseEvent(size_t evt_id) m_joystick_status_aux1 = false; applyJoystickStatus(); - m_joystick_btn_off->guibutton->setVisible(true); - m_joystick_btn_bg->guibutton->setVisible(false); - m_joystick_btn_center->guibutton->setVisible(false); + m_joystick_btn_off->gui_button->setVisible(true); + m_joystick_btn_bg->gui_button->setVisible(false); + m_joystick_btn_center->gui_button->setVisible(false); } else { - infostream - << "TouchScreenGUI::translateEvent released unknown button: " - << evt_id << std::endl; + infostream << "TouchScreenGUI::translateEvent released unknown button: " + << evt_id << std::endl; } - for (auto iter = m_known_ids.begin(); - iter != m_known_ids.end(); ++iter) { + for (auto iter = m_known_ids.begin(); iter != m_known_ids.end(); ++iter) { if (iter->id == evt_id) { m_known_ids.erase(iter); break; @@ -723,10 +703,10 @@ void TouchScreenGUI::translateEvent(const SEvent &event) { if (!m_initialized) return; + if (!m_visible) { - infostream - << "TouchScreenGUI::translateEvent got event but not visible!" - << std::endl; + infostream << "TouchScreenGUI::translateEvent got event but is not visible!" + << std::endl; return; } @@ -739,37 +719,36 @@ void TouchScreenGUI::translateEvent(const SEvent &event) * android would provide this information but Irrlicht guys don't * wanna design an efficient interface */ - id_status toadd{}; - toadd.id = event.TouchInput.ID; - toadd.X = event.TouchInput.X; - toadd.Y = event.TouchInput.Y; - m_known_ids.push_back(toadd); + id_status to_be_added{}; + to_be_added.id = event.TouchInput.ID; + to_be_added.X = event.TouchInput.X; + to_be_added.Y = event.TouchInput.Y; + m_known_ids.push_back(to_be_added); size_t eventID = event.TouchInput.ID; - touch_gui_button_id button = - getButtonID(event.TouchInput.X, event.TouchInput.Y); + touch_gui_button_id button = getButtonID(event.TouchInput.X, event.TouchInput.Y); // handle button events if (button != after_last_element_id) { handleButtonEvent(button, eventID, true); - m_settingsbar.deactivate(); - m_rarecontrolsbar.deactivate(); + m_settings_bar.deactivate(); + m_rare_controls_bar.deactivate(); } else if (isHUDButton(event)) { - m_settingsbar.deactivate(); - m_rarecontrolsbar.deactivate(); + m_settings_bar.deactivate(); + m_rare_controls_bar.deactivate(); // already handled in isHUDButton() - } else if (m_settingsbar.isButton(event)) { - m_rarecontrolsbar.deactivate(); + } else if (m_settings_bar.isButton(event)) { + m_rare_controls_bar.deactivate(); // already handled in isSettingsBarButton() - } else if (m_rarecontrolsbar.isButton(event)) { - m_settingsbar.deactivate(); + } else if (m_rare_controls_bar.isButton(event)) { + m_settings_bar.deactivate(); // already handled in isSettingsBarButton() } else { // handle non button events - if (m_settingsbar.active() || m_rarecontrolsbar.active()) { - m_settingsbar.deactivate(); - m_rarecontrolsbar.deactivate(); + if (m_settings_bar.active() || m_rare_controls_bar.active()) { + m_settings_bar.deactivate(); + m_rare_controls_bar.deactivate(); return; } @@ -779,30 +758,30 @@ void TouchScreenGUI::translateEvent(const SEvent &event) /* 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) || + if ((m_fixed_joystick && dxj * dxj + dyj * dyj <= button_size * button_size * 1.5f * 1.5f) || (!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 we don't already have a starting point for joystick, make this the one. if (!m_has_joystick_id) { m_has_joystick_id = true; 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); + m_joystick_btn_off->gui_button->setVisible(false); + m_joystick_btn_bg->gui_button->setVisible(true); + m_joystick_btn_center->gui_button->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( + m_joystick_btn_bg->gui_button->setRelativePosition(v2s32( event.TouchInput.X - button_size * 3.0f / 2.0f, event.TouchInput.Y - button_size * 3.0f / 2.0f)); - m_joystick_btn_center->guibutton->setRelativePosition(v2s32( + m_joystick_btn_center->gui_button->setRelativePosition(v2s32( event.TouchInput.X - button_size / 2.0f, event.TouchInput.Y - button_size / 2.0f)); } } else { - // If we don't already have a moving point make this the moving one. + // If we don't already have a moving point, make this the moving one. if (!m_has_move_id) { m_has_move_id = true; m_move_id = event.TouchInput.ID; @@ -816,17 +795,16 @@ void TouchScreenGUI::translateEvent(const SEvent &event) } } - m_pointerpos[event.TouchInput.ID] = v2s32(event.TouchInput.X, event.TouchInput.Y); + m_pointer_pos[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; + verbosestream << "Up event for pointerid: " << event.TouchInput.ID << std::endl; handleReleaseEvent(event.TouchInput.ID); } else { assert(event.TouchInput.Event == ETIE_MOVED); if (!(m_has_joystick_id && m_fixed_joystick) && - m_pointerpos[event.TouchInput.ID] == + m_pointer_pos[event.TouchInput.ID] == v2s32(event.TouchInput.X, event.TouchInput.Y)) return; @@ -834,10 +812,10 @@ void TouchScreenGUI::translateEvent(const SEvent &event) if (event.TouchInput.ID == m_move_id && (!m_move_sent_as_mouse_event || m_draw_crosshair)) { double distance = sqrt( - (m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) * - (m_pointerpos[event.TouchInput.ID].X - event.TouchInput.X) + - (m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y) * - (m_pointerpos[event.TouchInput.ID].Y - event.TouchInput.Y)); + (m_pointer_pos[event.TouchInput.ID].X - event.TouchInput.X) * + (m_pointer_pos[event.TouchInput.ID].X - event.TouchInput.X) + + (m_pointer_pos[event.TouchInput.ID].Y - event.TouchInput.Y) * + (m_pointer_pos[event.TouchInput.ID].Y - event.TouchInput.Y)); if (distance > m_touchscreen_threshold || m_move_has_really_moved) { m_move_has_really_moved = true; @@ -845,15 +823,15 @@ void TouchScreenGUI::translateEvent(const SEvent &event) s32 Y = event.TouchInput.Y; // update camera_yaw and camera_pitch - s32 dx = X - m_pointerpos[event.TouchInput.ID].X; - s32 dy = Y - m_pointerpos[event.TouchInput.ID].Y; - m_pointerpos[event.TouchInput.ID] = v2s32(X, Y); + s32 dx = X - m_pointer_pos[event.TouchInput.ID].X; + s32 dy = Y - m_pointer_pos[event.TouchInput.ID].Y; + m_pointer_pos[event.TouchInput.ID] = v2s32(X, Y); // adapt to similar behavior as pc screen const double d = g_settings->getFloat("mouse_sensitivity", 0.001f, 10.0f) * 3.0f; m_camera_yaw_change -= dx * d; - m_camera_pitch = MYMIN(MYMAX(m_camera_pitch + (dy * d), -180), 180); + m_camera_pitch = MYMIN(MYMAX(m_camera_pitch + (dy * d), -180.0f), 180.0f); // update shootline // no need to update (X, Y) when using crosshair since the shootline is not used @@ -875,8 +853,8 @@ void TouchScreenGUI::translateEvent(const SEvent &event) 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; + s32 dx = X - m_pointer_pos[event.TouchInput.ID].X; + s32 dy = Y - m_pointer_pos[event.TouchInput.ID].Y; if (m_fixed_joystick) { dx = X - button_size * 5.0f / 2.0f; dy = Y - (s32)m_screensize.Y + button_size * 5.0f / 2.0f; @@ -886,7 +864,7 @@ void TouchScreenGUI::translateEvent(const SEvent &event) s32 dxj = event.TouchInput.X - 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); + bool inside_joystick = (dxj * dxj + dyj * dyj <= button_size * button_size * 1.5f * 1.5f); if (m_joystick_has_really_moved || inside_joystick || (!m_fixed_joystick && @@ -911,16 +889,16 @@ void TouchScreenGUI::translateEvent(const SEvent &event) s32 ndx = button_size * dx / distance - button_size / 2.0f; s32 ndy = button_size * dy / distance - button_size / 2.0f; if (m_fixed_joystick) { - m_joystick_btn_center->guibutton->setRelativePosition(v2s32( - button_size * 5.0f / 2.0f + ndx, - m_screensize.Y - button_size * 5.0f / 2.0f + ndy)); + m_joystick_btn_center->gui_button->setRelativePosition(v2s32( + 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, - m_pointerpos[event.TouchInput.ID].Y + ndy)); + m_joystick_btn_center->gui_button->setRelativePosition(v2s32( + m_pointer_pos[event.TouchInput.ID].X + ndx, + m_pointer_pos[event.TouchInput.ID].Y + ndy)); } } else { - m_joystick_btn_center->guibutton->setRelativePosition( + m_joystick_btn_center->gui_button->setRelativePosition( v2s32(X - button_size / 2, Y - button_size / 2)); } } @@ -937,11 +915,9 @@ void TouchScreenGUI::handleChangedButton(const SEvent &event) if (m_buttons[i].ids.empty()) continue; - for (auto iter = m_buttons[i].ids.begin(); - iter != m_buttons[i].ids.end(); ++iter) { + for (auto iter = m_buttons[i].ids.begin(); iter != m_buttons[i].ids.end(); ++iter) { if (event.TouchInput.ID == *iter) { - auto current_button_id = - getButtonID(event.TouchInput.X, event.TouchInput.Y); + auto current_button_id = getButtonID(event.TouchInput.X, event.TouchInput.Y); if (current_button_id == i) continue; @@ -964,10 +940,8 @@ void TouchScreenGUI::handleChangedButton(const SEvent &event) return; button_info *btn = &m_buttons[current_button_id]; - if (std::find(btn->ids.begin(), btn->ids.end(), event.TouchInput.ID) - == btn->ids.end()) - handleButtonEvent((touch_gui_button_id) current_button_id, - event.TouchInput.ID, true); + if (std::find(btn->ids.begin(), btn->ids.end(), event.TouchInput.ID) == btn->ids.end()) + handleButtonEvent((touch_gui_button_id) current_button_id, event.TouchInput.ID, true); } bool TouchScreenGUI::doRightClick() @@ -978,14 +952,13 @@ bool TouchScreenGUI::doRightClick() mPos.Y = m_screensize.Y / 2; } - auto *translated = new SEvent(); - memset(translated, 0, sizeof(SEvent)); - translated->EventType = EET_MOUSE_INPUT_EVENT; - translated->MouseInput.X = mPos.X; - translated->MouseInput.Y = mPos.Y; - translated->MouseInput.Shift = false; - translated->MouseInput.Control = false; - translated->MouseInput.ButtonStates = EMBSM_RIGHT; + SEvent translated {}; + translated.EventType = EET_MOUSE_INPUT_EVENT; + translated.MouseInput.X = mPos.X; + translated.MouseInput.Y = mPos.Y; + translated.MouseInput.Shift = false; + translated.MouseInput.Control = false; + translated.MouseInput.ButtonStates = EMBSM_RIGHT; // update shootline m_shootline = m_device @@ -993,15 +966,15 @@ bool TouchScreenGUI::doRightClick() ->getSceneCollisionManager() ->getRayFromScreenCoordinates(mPos); - translated->MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN; + translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN; verbosestream << "TouchScreenGUI::translateEvent right click press" << std::endl; - m_receiver->OnEvent(*translated); + m_receiver->OnEvent(translated); - translated->MouseInput.ButtonStates = 0; - translated->MouseInput.Event = EMIE_RMOUSE_LEFT_UP; + translated.MouseInput.ButtonStates = 0; + translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP; verbosestream << "TouchScreenGUI::translateEvent right click release" << std::endl; - m_receiver->OnEvent(*translated); - delete translated; + m_receiver->OnEvent(translated); + return true; } @@ -1010,7 +983,7 @@ void TouchScreenGUI::applyJoystickStatus() if (m_joystick_triggers_aux1) { SEvent translated{}; translated.EventType = irr::EET_KEY_INPUT_EVENT; - translated.KeyInput.Key = id2keycode(aux1_id); + translated.KeyInput.Key = id_to_keycode(aux1_id); translated.KeyInput.PressedDown = false; m_receiver->OnEvent(translated); @@ -1027,25 +1000,25 @@ TouchScreenGUI::~TouchScreenGUI() return; for (auto &button : m_buttons) { - if (button.guibutton) { - button.guibutton->drop(); - button.guibutton = nullptr; + if (button.gui_button) { + button.gui_button->drop(); + button.gui_button = nullptr; } } - if (m_joystick_btn_off->guibutton) { - m_joystick_btn_off->guibutton->drop(); - m_joystick_btn_off->guibutton = nullptr; + if (m_joystick_btn_off->gui_button) { + m_joystick_btn_off->gui_button->drop(); + m_joystick_btn_off->gui_button = nullptr; } - if (m_joystick_btn_bg->guibutton) { - m_joystick_btn_bg->guibutton->drop(); - m_joystick_btn_bg->guibutton = nullptr; + if (m_joystick_btn_bg->gui_button) { + m_joystick_btn_bg->gui_button->drop(); + m_joystick_btn_bg->gui_button = nullptr; } - if (m_joystick_btn_center->guibutton) { - m_joystick_btn_center->guibutton->drop(); - m_joystick_btn_center->guibutton = nullptr; + if (m_joystick_btn_center->gui_button) { + m_joystick_btn_center->gui_button->drop(); + m_joystick_btn_center->gui_button = nullptr; } } @@ -1057,14 +1030,13 @@ void TouchScreenGUI::step(float dtime) // simulate keyboard repeats for (auto &button : m_buttons) { if (!button.ids.empty()) { - button.repeatcounter += dtime; + button.repeat_counter += dtime; - if (button.repeatcounter < button.repeatdelay) + if (button.repeat_counter < button.repeat_delay) continue; - button.repeatcounter = 0; - SEvent translated; - memset(&translated, 0, sizeof(SEvent)); + button.repeat_counter = 0.0f; + SEvent translated {}; translated.EventType = irr::EET_KEY_INPUT_EVENT; translated.KeyInput.Key = button.keycode; translated.KeyInput.PressedDown = false; @@ -1082,7 +1054,6 @@ void TouchScreenGUI::step(float dtime) if (m_has_move_id && (!m_move_has_really_moved) && (!m_move_sent_as_mouse_event)) { - u64 delta = porting::getDeltaMs(m_move_downtime, porting::getTimeMs()); if (delta > MIN_DIG_TIME_MS) { @@ -1095,11 +1066,9 @@ void TouchScreenGUI::step(float dtime) m_shootline = m_device ->getSceneManager() ->getSceneCollisionManager() - ->getRayFromScreenCoordinates( - v2s32(mX, mY)); + ->getRayFromScreenCoordinates(v2s32(mX, mY)); - SEvent translated; - memset(&translated, 0, sizeof(SEvent)); + SEvent translated {}; translated.EventType = EET_MOUSE_INPUT_EVENT; translated.MouseInput.X = mX; translated.MouseInput.Y = mY; @@ -1113,8 +1082,8 @@ void TouchScreenGUI::step(float dtime) } } - m_settingsbar.step(dtime); - m_rarecontrolsbar.step(dtime); + m_settings_bar.step(dtime); + m_rare_controls_bar.step(dtime); } void TouchScreenGUI::resetHud() @@ -1127,30 +1096,30 @@ void TouchScreenGUI::registerHudItem(int index, const rect &rect) m_hud_rects[index] = rect; } -void TouchScreenGUI::Toggle(bool visible) +void TouchScreenGUI::setVisible(bool visible) { if (!m_initialized) return; m_visible = visible; for (auto &button : m_buttons) { - if (button.guibutton) - button.guibutton->setVisible(visible); + if (button.gui_button) + button.gui_button->setVisible(visible); } - if (m_joystick_btn_off->guibutton) - m_joystick_btn_off->guibutton->setVisible(visible); + if (m_joystick_btn_off->gui_button) + m_joystick_btn_off->gui_button->setVisible(visible); // clear all active buttons if (!visible) { while (!m_known_ids.empty()) handleReleaseEvent(m_known_ids.begin()->id); - m_settingsbar.hide(); - m_rarecontrolsbar.hide(); + m_settings_bar.hide(); + m_rare_controls_bar.hide(); } else { - m_settingsbar.show(); - m_rarecontrolsbar.show(); + m_settings_bar.show(); + m_rare_controls_bar.show(); } } @@ -1159,7 +1128,7 @@ void TouchScreenGUI::hide() if (!m_visible) return; - Toggle(false); + setVisible(false); } void TouchScreenGUI::show() @@ -1167,5 +1136,5 @@ void TouchScreenGUI::show() if (m_visible) return; - Toggle(true); + setVisible(true); } diff --git a/src/gui/touchscreengui.h b/src/gui/touchscreengui.h index 396cc6836..5b22c29c5 100644 --- a/src/gui/touchscreengui.h +++ b/src/gui/touchscreengui.h @@ -60,15 +60,6 @@ typedef enum joystick_center_id } touch_gui_button_id; -typedef enum -{ - j_forward = 0, - j_backward, - j_left, - j_right, - j_aux1 -} touch_gui_joystick_move_id; - typedef enum { AHBB_Dir_Top_Bottom, @@ -82,24 +73,24 @@ typedef enum #define SETTINGS_BAR_Y_OFFSET 5 #define RARE_CONTROLS_BAR_Y_OFFSET 5 -// Very slow button repeat frequency -#define SLOW_BUTTON_REPEAT 1.0f - -extern const char *button_imagenames[]; -extern const char *joystick_imagenames[]; +extern const std::string button_image_names[]; +extern const std::string joystick_image_names[]; struct button_info { - float repeatcounter; - float repeatdelay; - irr::EKEY_CODE keycode; + float repeat_counter; + float repeat_delay; + EKEY_CODE keycode; std::vector ids; - IGUIButton *guibutton = nullptr; + IGUIButton *gui_button = nullptr; bool immediate_release; - // 0: false, 1: (true) first texture, 2: (true) second texture - int togglable = 0; - std::vector textures; + enum { + NOT_TOGGLEABLE, + FIRST_TEXTURE, + SECOND_TEXTURE + } toggleable = NOT_TOGGLEABLE; + std::vector textures; }; class AutoHideButtonBar @@ -107,7 +98,7 @@ class AutoHideButtonBar public: AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver); - void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id, + void init(ISimpleTextureSource *tsrc, const std::string &starter_img, int button_id, const v2s32 &UpperLeft, const v2s32 &LowerRight, autohide_button_bar_dir dir, float timeout); @@ -115,13 +106,13 @@ public: // add button to be shown void addButton(touch_gui_button_id id, const wchar_t *caption, - const char *btn_image); + const std::string &btn_image); // add toggle button to be shown void addToggleButton(touch_gui_button_id id, const wchar_t *caption, - const char *btn_image_1, const char *btn_image_2); + const std::string &btn_image_1, const std::string &btn_image_2); - // detect settings bar button events + // detect button bar button events bool isButton(const SEvent &event); // step handler @@ -130,13 +121,13 @@ public: // return whether the button bar is active bool active() { return m_active; } - // deactivate button bar + // deactivate the button bar void deactivate(); - // hide the whole buttonbar + // hide the whole button bar void hide(); - // unhide the buttonbar + // unhide the button bar void show(); private: @@ -145,17 +136,16 @@ private: IGUIEnvironment *m_guienv; IEventReceiver *m_receiver; button_info m_starter; - std::vector m_buttons; + std::vector> m_buttons; v2s32 m_upper_left; v2s32 m_lower_right; - // show settings bar + // show button bar bool m_active = false; - bool m_visible = true; - // settings bar timeout + // button bar timeout float m_timeout = 0.0f; float m_timeout_value = 3.0f; bool m_initialized = false; @@ -181,7 +171,7 @@ public: double getPitch() { return m_camera_pitch; } - /* + /** * Returns a line which describes what the player is pointing at. * The starting point and looking direction are significant, * the line should be scaled to match its length to the actual distance @@ -198,8 +188,8 @@ public: void resetHud(); void registerHudItem(int index, const rect &rect); inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; } - void Toggle(bool visible); + void setVisible(bool visible); void hide(); void show(); @@ -213,16 +203,15 @@ private: s32 button_size; double m_touchscreen_threshold; std::map> m_hud_rects; - std::map m_hud_ids; - bool m_visible; // is the gui visible + std::map m_hud_ids; + bool m_visible; // is the whole touch screen gui visible // value in degree double m_camera_yaw_change = 0.0; double m_camera_pitch = 0.0; - /* - * A line starting at the camera and pointing towards the - * selected object. + /** + * A line starting at the camera and pointing towards the selected object. * The line ends on the camera's far plane. * The coordinates do not contain the camera offset. */ @@ -244,9 +233,9 @@ private: bool m_fixed_joystick = false; bool m_joystick_triggers_aux1 = false; bool m_draw_crosshair = false; - button_info *m_joystick_btn_off = nullptr; - button_info *m_joystick_btn_bg = nullptr; - button_info *m_joystick_btn_center = nullptr; + std::shared_ptr m_joystick_btn_off = nullptr; + std::shared_ptr m_joystick_btn_bg = nullptr; + std::shared_ptr m_joystick_btn_center = nullptr; button_info m_buttons[after_last_element_id]; @@ -265,7 +254,7 @@ private: float repeat_delay = BUTTON_REPEAT_DELAY); // initialize a joystick button - button_info *initJoystickButton(touch_gui_button_id id, + std::shared_ptr initJoystickButton(touch_gui_button_id id, const rect &button_rect, int texture_id, bool visible = true); @@ -295,13 +284,13 @@ private: void applyJoystickStatus(); // array for saving last known position of a pointer - std::map m_pointerpos; + std::map m_pointer_pos; // settings bar - AutoHideButtonBar m_settingsbar; + AutoHideButtonBar m_settings_bar; // rare controls bar - AutoHideButtonBar m_rarecontrolsbar; + AutoHideButtonBar m_rare_controls_bar; }; extern TouchScreenGUI *g_touchscreengui;