Skip to content

Commit

Permalink
TouchControls: Move functions into class so less stuff needs to be pa…
Browse files Browse the repository at this point in the history
…ssed around
  • Loading branch information
grorp committed Dec 29, 2024
1 parent cca65fd commit 8c4f647
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 67 deletions.
109 changes: 45 additions & 64 deletions src/gui/touchcontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,47 @@

TouchControls *g_touchcontrols;

static void load_button_texture(IGUIImage *gui_button, const std::string &path,
const recti &button_rect, ISimpleTextureSource *tsrc, video::IVideoDriver *driver)
void TouchControls::emitKeyboardEvent(EKEY_CODE keycode, bool pressed)
{
video::ITexture *texture = guiScalingImageButton(driver,
tsrc->getTexture(path), button_rect.getWidth(),
button_rect.getHeight());
SEvent e{};
e.EventType = EET_KEY_INPUT_EVENT;
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->setScaleImage(true);
}

void button_info::emitAction(bool action, video::IVideoDriver *driver,
IEventReceiver *receiver, ISimpleTextureSource *tsrc)
void TouchControls::buttonEmitAction(button_info &btn, bool action)
{
if (keycode == KEY_UNKNOWN)
if (btn.keycode == KEY_UNKNOWN)
return;

SEvent translated{};
translated.EventType = EET_KEY_INPUT_EVENT;
translated.KeyInput.Key = keycode;
translated.KeyInput.Control = false;
translated.KeyInput.Shift = false;
translated.KeyInput.Char = 0;
emitKeyboardEvent(btn.keycode, action);

if (action) {
translated.KeyInput.PressedDown = true;
receiver->OnEvent(translated);

if (toggleable == button_info::FIRST_TEXTURE) {
toggleable = button_info::SECOND_TEXTURE;
load_button_texture(gui_button.get(), toggle_textures[1],
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);
if (btn.toggleable == button_info::FIRST_TEXTURE) {
btn.toggleable = button_info::SECOND_TEXTURE;
loadButtonTexture(btn.gui_button.get(), btn.toggle_textures[1]);

} else if (btn.toggleable == button_info::SECOND_TEXTURE) {
btn.toggleable = button_info::FIRST_TEXTURE;
loadButtonTexture(btn.gui_button.get(), btn.toggle_textures[0]);
}
} else {
translated.KeyInput.PressedDown = false;
receiver->OnEvent(translated);
}
}

static bool buttons_handlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element,
video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
bool TouchControls::buttonsHandlePress(std::vector<button_info> &buttons, size_t pointer_id, IGUIElement *element)
{
if (!element)
return false;
Expand All @@ -87,7 +82,7 @@ static bool buttons_handlePress(std::vector<button_info> &buttons, size_t pointe
if (btn.pointer_ids.size() > 1)
return true;

btn.emitAction(true, driver, receiver, tsrc);
buttonEmitAction(btn, true);
btn.repeat_counter = -BUTTON_REPEAT_DELAY;
return true;
}
Expand All @@ -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,
video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
bool TouchControls::buttonsHandleRelease(std::vector<button_info> &buttons, size_t pointer_id)
{
for (button_info &btn : buttons) {
auto it = std::find(btn.pointer_ids.begin(), btn.pointer_ids.end(), pointer_id);
Expand All @@ -108,16 +102,15 @@ static bool buttons_handleRelease(std::vector<button_info> &buttons, size_t poin
if (!btn.pointer_ids.empty())
return true;

btn.emitAction(false, driver, receiver, tsrc);
buttonEmitAction(btn, false);
return true;
}
}

return false;
}

static bool buttons_step(std::vector<button_info> &buttons, float dtime,
video::IVideoDriver *driver, IEventReceiver *receiver, ISimpleTextureSource *tsrc)
bool TouchControls::buttonsStep(std::vector<button_info> &buttons, float dtime)
{
bool has_pointers = false;

Expand All @@ -130,8 +123,8 @@ static bool buttons_step(std::vector<button_info> &buttons, float dtime,
if (btn.repeat_counter < BUTTON_REPEAT_INTERVAL)
continue;

btn.emitAction(false, driver, receiver, tsrc);
btn.emitAction(true, driver, receiver, tsrc);
buttonEmitAction(btn, false);
buttonEmitAction(btn, true);
btn.repeat_counter = 0.0f;
}

Expand Down Expand Up @@ -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);
btn_gui_button->setVisible(visible);
load_button_texture(btn_gui_button, image, rect,
m_texturesource, m_device->getVideoDriver());
loadButtonTexture(btn_gui_button, image);

button_info &btn = buttons.emplace_back();
btn.keycode = id_to_keycode(id);
Expand All @@ -363,8 +355,7 @@ IGUIImage *TouchControls::makeButtonDirect(touch_gui_button_id id,
{
IGUIImage *btn_gui_button = m_guienv->addImage(rect, nullptr, id);
btn_gui_button->setVisible(visible);
load_button_texture(btn_gui_button, button_image_names[id], rect,
m_texturesource, m_device->getVideoDriver());
loadButtonTexture(btn_gui_button, button_image_names[id]);

return btn_gui_button;
}
Expand Down Expand Up @@ -399,11 +390,9 @@ void TouchControls::handleReleaseEvent(size_t pointer_id)
m_pointer_pos.erase(pointer_id);

// handle buttons
if (buttons_handleRelease(m_buttons, pointer_id, m_device->getVideoDriver(),
m_receiver, m_texturesource))
if (buttonsHandleRelease(m_buttons, pointer_id))
return;
if (buttons_handleRelease(m_overflow_buttons, pointer_id, m_device->getVideoDriver(),
m_receiver, m_texturesource))
if (buttonsHandleRelease(m_overflow_buttons, pointer_id))
return;

if (m_has_move_id && pointer_id == m_move_id) {
Expand Down Expand Up @@ -481,8 +470,7 @@ void TouchControls::translateEvent(const SEvent &event)
}
}

if (buttons_handlePress(m_overflow_buttons, pointer_id, element,
m_device->getVideoDriver(), m_receiver, m_texturesource))
if (buttonsHandlePress(m_overflow_buttons, pointer_id, element))
return;

toggleOverflowMenu();
Expand All @@ -494,8 +482,7 @@ void TouchControls::translateEvent(const SEvent &event)
}

// handle buttons
if (buttons_handlePress(m_buttons, pointer_id, element,
m_device->getVideoDriver(), m_receiver, m_texturesource))
if (buttonsHandlePress(m_buttons, pointer_id, element))
return;

// handle hotbar
Expand Down Expand Up @@ -614,16 +601,10 @@ void TouchControls::translateEvent(const SEvent &event)
void TouchControls::applyJoystickStatus()
{
if (m_joystick_triggers_aux1) {
SEvent translated{};
translated.EventType = EET_KEY_INPUT_EVENT;
translated.KeyInput.Key = id_to_keycode(aux1_id);
translated.KeyInput.PressedDown = false;
m_receiver->OnEvent(translated);

if (m_joystick_status_aux1) {
translated.KeyInput.PressedDown = true;
m_receiver->OnEvent(translated);
}
auto key = id_to_keycode(aux1_id);
emitKeyboardEvent(key, false);
if (m_joystick_status_aux1)
emitKeyboardEvent(key, true);
}
}

Expand All @@ -639,8 +620,8 @@ void TouchControls::step(float dtime)
}

// simulate keyboard repeats
buttons_step(m_buttons, dtime, m_device->getVideoDriver(), m_receiver, m_texturesource);
buttons_step(m_overflow_buttons, dtime, m_device->getVideoDriver(), m_receiver, m_texturesource);
buttonsStep(m_buttons, dtime);
buttonsStep(m_overflow_buttons, dtime);

// joystick
applyJoystickStatus();
Expand Down
16 changes: 13 additions & 3 deletions src/gui/touchcontrols.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ struct button_info
SECOND_TEXTURE
} toggleable = NOT_TOGGLEABLE;
std::string toggle_textures[2];

void emitAction(bool action, video::IVideoDriver *driver,
IEventReceiver *receiver, ISimpleTextureSource *tsrc);
};


Expand Down Expand Up @@ -186,6 +183,19 @@ class TouchControls

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 updateVisibility();
void releaseAll();
Expand Down

0 comments on commit 8c4f647

Please sign in to comment.