TouchControls: More methods instead of static functions (#15602)

This commit is contained in:
grorp 2024-12-30 19:03:32 +01:00 committed by GitHub
parent f2b1cc3e61
commit 27c3aade5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 66 deletions

@ -29,52 +29,47 @@
TouchControls *g_touchcontrols; TouchControls *g_touchcontrols;
static void load_button_texture(IGUIImage *gui_button, const std::string &path, void TouchControls::emitKeyboardEvent(EKEY_CODE keycode, bool pressed)
const recti &button_rect, ISimpleTextureSource *tsrc, video::IVideoDriver *driver)
{ {
video::ITexture *texture = guiScalingImageButton(driver, SEvent e{};
tsrc->getTexture(path), button_rect.getWidth(), e.EventType = EET_KEY_INPUT_EVENT;
button_rect.getHeight()); e.KeyInput.Key = keycode;
e.KeyInput.Control = false;
e.KeyInput.Shift = false;
e.KeyInput.Char = 0;
e.KeyInput.PressedDown = pressed;
m_receiver->OnEvent(e);
}
void TouchControls::loadButtonTexture(IGUIImage *gui_button, const std::string &path)
{
auto rect = gui_button->getRelativePosition();
video::ITexture *texture = guiScalingImageButton(m_device->getVideoDriver(),
m_texturesource->getTexture(path), rect.getWidth(), rect.getHeight());
gui_button->setImage(texture); gui_button->setImage(texture);
gui_button->setScaleImage(true); gui_button->setScaleImage(true);
} }
void button_info::emitAction(bool action, video::IVideoDriver *driver, void TouchControls::buttonEmitAction(button_info &btn, bool action)
IEventReceiver *receiver, ISimpleTextureSource *tsrc)
{ {
if (keycode == KEY_UNKNOWN) if (btn.keycode == KEY_UNKNOWN)
return; return;
SEvent translated{}; emitKeyboardEvent(btn.keycode, action);
translated.EventType = EET_KEY_INPUT_EVENT;
translated.KeyInput.Key = keycode;
translated.KeyInput.Control = false;
translated.KeyInput.Shift = false;
translated.KeyInput.Char = 0;
if (action) { if (action) {
translated.KeyInput.PressedDown = true; if (btn.toggleable == button_info::FIRST_TEXTURE) {
receiver->OnEvent(translated); btn.toggleable = button_info::SECOND_TEXTURE;
loadButtonTexture(btn.gui_button.get(), btn.toggle_textures[1]);
if (toggleable == button_info::FIRST_TEXTURE) { } else if (btn.toggleable == button_info::SECOND_TEXTURE) {
toggleable = button_info::SECOND_TEXTURE; btn.toggleable = button_info::FIRST_TEXTURE;
load_button_texture(gui_button.get(), toggle_textures[1], loadButtonTexture(btn.gui_button.get(), btn.toggle_textures[0]);
gui_button->getRelativePosition(),
tsrc, driver);
} else if (toggleable == button_info::SECOND_TEXTURE) {
toggleable = button_info::FIRST_TEXTURE;
load_button_texture(gui_button.get(), toggle_textures[0],
gui_button->getRelativePosition(),
tsrc, driver);
} }
} else {
translated.KeyInput.PressedDown = false;
receiver->OnEvent(translated);
} }
} }
static bool buttons_handlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element, bool TouchControls::buttonsHandlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element)
video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
{ {
if (!element) if (!element)
return false; return false;
@ -87,7 +82,7 @@ static bool buttons_handlePress(std::vector<button_info> &buttons, size_t pointe
if (btn.pointer_ids.size() > 1) if (btn.pointer_ids.size() > 1)
return true; return true;
btn.emitAction(true, driver, receiver, tsrc); buttonEmitAction(btn, true);
btn.repeat_counter = -BUTTON_REPEAT_DELAY; btn.repeat_counter = -BUTTON_REPEAT_DELAY;
return true; return true;
} }
@ -97,8 +92,7 @@ static bool buttons_handlePress(std::vector<button_info> &buttons, size_t pointe
} }
static bool buttons_handleRelease(std::vector<button_info> &buttons, size_t pointer_id, bool TouchControls::buttonsHandleRelease(std::vector<button_info> &buttons, size_t pointer_id)
video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
{ {
for (button_info &btn : buttons) { for (button_info &btn : buttons) {
auto it = std::find(btn.pointer_ids.begin(), btn.pointer_ids.end(), pointer_id); auto it = std::find(btn.pointer_ids.begin(), btn.pointer_ids.end(), pointer_id);
@ -108,7 +102,7 @@ static bool buttons_handleRelease(std::vector<button_info> &buttons, size_t poin
if (!btn.pointer_ids.empty()) if (!btn.pointer_ids.empty())
return true; return true;
btn.emitAction(false, driver, receiver, tsrc); buttonEmitAction(btn, false);
return true; return true;
} }
} }
@ -116,8 +110,7 @@ static bool buttons_handleRelease(std::vector<button_info> &buttons, size_t poin
return false; return false;
} }
static bool buttons_step(std::vector<button_info> &buttons, float dtime, bool TouchControls::buttonsStep(std::vector<button_info> &buttons, float dtime)
video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
{ {
bool has_pointers = false; bool has_pointers = false;
@ -130,8 +123,8 @@ static bool buttons_step(std::vector<button_info> &buttons, float dtime,
if (btn.repeat_counter < BUTTON_REPEAT_INTERVAL) if (btn.repeat_counter < BUTTON_REPEAT_INTERVAL)
continue; continue;
btn.emitAction(false, driver, receiver, tsrc); buttonEmitAction(btn, false);
btn.emitAction(true, driver, receiver, tsrc); buttonEmitAction(btn, true);
btn.repeat_counter = 0.0f; btn.repeat_counter = 0.0f;
} }
@ -340,8 +333,7 @@ void TouchControls::addButton(std::vector<button_info> &buttons, touch_gui_butto
{ {
IGUIImage *btn_gui_button = m_guienv->addImage(rect, nullptr, id); IGUIImage *btn_gui_button = m_guienv->addImage(rect, nullptr, id);
btn_gui_button->setVisible(visible); btn_gui_button->setVisible(visible);
load_button_texture(btn_gui_button, image, rect, loadButtonTexture(btn_gui_button, image);
m_texturesource, m_device->getVideoDriver());
button_info &btn = buttons.emplace_back(); button_info &btn = buttons.emplace_back();
btn.keycode = id_to_keycode(id); btn.keycode = id_to_keycode(id);
@ -363,8 +355,7 @@ IGUIImage *TouchControls::makeButtonDirect(touch_gui_button_id id,
{ {
IGUIImage *btn_gui_button = m_guienv->addImage(rect, nullptr, id); IGUIImage *btn_gui_button = m_guienv->addImage(rect, nullptr, id);
btn_gui_button->setVisible(visible); btn_gui_button->setVisible(visible);
load_button_texture(btn_gui_button, button_image_names[id], rect, loadButtonTexture(btn_gui_button, button_image_names[id]);
m_texturesource, m_device->getVideoDriver());
return btn_gui_button; return btn_gui_button;
} }
@ -399,11 +390,9 @@ void TouchControls::handleReleaseEvent(size_t pointer_id)
m_pointer_pos.erase(pointer_id); m_pointer_pos.erase(pointer_id);
// handle buttons // handle buttons
if (buttons_handleRelease(m_buttons, pointer_id, m_device->getVideoDriver(), if (buttonsHandleRelease(m_buttons, pointer_id))
m_receiver, m_texturesource))
return; return;
if (buttons_handleRelease(m_overflow_buttons, pointer_id, m_device->getVideoDriver(), if (buttonsHandleRelease(m_overflow_buttons, pointer_id))
m_receiver, m_texturesource))
return; return;
if (m_has_move_id && pointer_id == m_move_id) { if (m_has_move_id && pointer_id == m_move_id) {
@ -481,8 +470,7 @@ void TouchControls::translateEvent(const SEvent &event)
} }
} }
if (buttons_handlePress(m_overflow_buttons, pointer_id, element, if (buttonsHandlePress(m_overflow_buttons, pointer_id, element))
m_device->getVideoDriver(), m_receiver, m_texturesource))
return; return;
toggleOverflowMenu(); toggleOverflowMenu();
@ -494,8 +482,7 @@ void TouchControls::translateEvent(const SEvent &event)
} }
// handle buttons // handle buttons
if (buttons_handlePress(m_buttons, pointer_id, element, if (buttonsHandlePress(m_buttons, pointer_id, element))
m_device->getVideoDriver(), m_receiver, m_texturesource))
return; return;
// handle hotbar // handle hotbar
@ -614,16 +601,10 @@ void TouchControls::translateEvent(const SEvent &event)
void TouchControls::applyJoystickStatus() void TouchControls::applyJoystickStatus()
{ {
if (m_joystick_triggers_aux1) { if (m_joystick_triggers_aux1) {
SEvent translated{}; auto key = id_to_keycode(aux1_id);
translated.EventType = EET_KEY_INPUT_EVENT; emitKeyboardEvent(key, false);
translated.KeyInput.Key = id_to_keycode(aux1_id); if (m_joystick_status_aux1)
translated.KeyInput.PressedDown = false; emitKeyboardEvent(key, true);
m_receiver->OnEvent(translated);
if (m_joystick_status_aux1) {
translated.KeyInput.PressedDown = true;
m_receiver->OnEvent(translated);
}
} }
} }
@ -639,8 +620,8 @@ void TouchControls::step(float dtime)
} }
// simulate keyboard repeats // simulate keyboard repeats
buttons_step(m_buttons, dtime, m_device->getVideoDriver(), m_receiver, m_texturesource); buttonsStep(m_buttons, dtime);
buttons_step(m_overflow_buttons, dtime, m_device->getVideoDriver(), m_receiver, m_texturesource); buttonsStep(m_overflow_buttons, dtime);
// joystick // joystick
applyJoystickStatus(); applyJoystickStatus();

@ -67,9 +67,6 @@ struct button_info
SECOND_TEXTURE SECOND_TEXTURE
} toggleable = NOT_TOGGLEABLE; } toggleable = NOT_TOGGLEABLE;
std::string toggle_textures[2]; std::string toggle_textures[2];
void emitAction(bool action, video::IVideoDriver *driver,
IEventReceiver *receiver, ISimpleTextureSource *tsrc);
}; };
@ -186,6 +183,19 @@ private:
std::shared_ptr<IGUIStaticText> m_status_text; std::shared_ptr<IGUIStaticText> m_status_text;
// Note: TouchControls intentionally uses IGUIImage instead of IGUIButton
// for its buttons. We only want static image display, not interactivity,
// from Irrlicht.
void emitKeyboardEvent(EKEY_CODE keycode, bool pressed);
void loadButtonTexture(IGUIImage *gui_button, const std::string &path);
void buttonEmitAction(button_info &btn, bool action);
bool buttonsHandlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element);
bool buttonsHandleRelease(std::vector<button_info> &buttons, size_t pointer_id);
bool buttonsStep(std::vector<button_info> &buttons, float dtime);
void toggleOverflowMenu(); void toggleOverflowMenu();
void updateVisibility(); void updateVisibility();
void releaseAll(); void releaseAll();