From f559627951cb1359a506e48f8fabb969accac74a Mon Sep 17 00:00:00 2001 From: Deve Date: Mon, 19 Mar 2018 23:06:20 +0100 Subject: [PATCH] Allow to repeat backspace key on screen keyboard --- src/guiengine/engine.cpp | 16 +++++++++-- src/guiengine/screen_keyboard.cpp | 46 +++++++++++++++++++++++++++++-- src/guiengine/screen_keyboard.hpp | 11 +++++++- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/guiengine/engine.cpp b/src/guiengine/engine.cpp index 87e5c0d06..b1b1c3fde 100644 --- a/src/guiengine/engine.cpp +++ b/src/guiengine/engine.cpp @@ -1162,14 +1162,26 @@ namespace GUIEngine // ---- some menus may need updating if (gamestate != GAME) { - if (ModalDialog::isADialogActive()) + if (ScreenKeyboard::isActive()) + { + ScreenKeyboard::getCurrent()->onUpdate(dt); + } + else if (ModalDialog::isADialogActive()) + { ModalDialog::getCurrent()->onUpdate(dt); + } else + { getCurrentScreen()->onUpdate(elapsed_time); + } } else { - if (ModalDialog::isADialogActive()) + if (ScreenKeyboard::isActive()) + { + ScreenKeyboard::getCurrent()->onUpdate(dt); + } + else if (ModalDialog::isADialogActive()) { ModalDialog::getCurrent()->onUpdate(dt); } diff --git a/src/guiengine/screen_keyboard.cpp b/src/guiengine/screen_keyboard.cpp index 7c7ae5ac1..fb78653fd 100644 --- a/src/guiengine/screen_keyboard.cpp +++ b/src/guiengine/screen_keyboard.cpp @@ -81,6 +81,8 @@ ScreenKeyboard::ScreenKeyboard(float percent_width, float percent_height, m_percent_height = std::min(std::max(percent_height, 0.0f), 1.0f); m_irrlicht_window = NULL; m_edit_box = edit_box; + m_back_button = NULL; + m_repeat_time = 0; init(); } // ScreenKeyboard @@ -135,6 +137,10 @@ void ScreenKeyboard::init() createButtons(); assignButtons(BUTTONS_LOWER); + + Widget* button_widget = getWidget("Back"); + assert(button_widget != NULL); + m_back_button = button_widget->getIrrlichtElement(); } // init // ---------------------------------------------------------------------------- @@ -221,6 +227,42 @@ void ScreenKeyboard::assignButtons(ButtonsType buttons_type) } } // assignButtons +// ---------------------------------------------------------------------------- + +void ScreenKeyboard::onUpdate(float dt) +{ + if (m_back_button->isPressed()) + { + const unsigned int repeat_rate = 40; + const unsigned int repeat_delay = 400; + + SEvent event; + event.KeyInput.Key = IRR_KEY_BACK; + event.KeyInput.Char = 0; + event.EventType = EET_KEY_INPUT_EVENT; + event.KeyInput.PressedDown = true; + event.KeyInput.Control = false; + event.KeyInput.Shift = false; + + if (m_repeat_time == 0) + { + m_edit_box->OnEvent(event); + } + + while (m_repeat_time > repeat_delay + repeat_rate) + { + m_edit_box->OnEvent(event); + m_repeat_time -= repeat_rate; + } + + m_repeat_time += (unsigned int)(dt * 1000); + } + else + { + m_repeat_time = 0; + } +} + // ---------------------------------------------------------------------------- /** A function that handles buttons events * \param eventSource Button ID @@ -272,9 +314,7 @@ EventPropagation ScreenKeyboard::processEvent(const std::string& eventSource) } else if (eventSource == "Back") { - event.KeyInput.Key = IRR_KEY_BACK; - event.KeyInput.Char = 0; - send_event = true; + send_event = false; } else if (eventSource == "Space") { diff --git a/src/guiengine/screen_keyboard.hpp b/src/guiengine/screen_keyboard.hpp index 04df28b43..1c0b9516b 100644 --- a/src/guiengine/screen_keyboard.hpp +++ b/src/guiengine/screen_keyboard.hpp @@ -19,6 +19,7 @@ #ifndef HEADER_SCREEN_KEYBOARD_HPP #define HEADER_SCREEN_KEYBOARD_HPP +#include #include #include "guiengine/abstract_top_level_container.hpp" @@ -67,9 +68,15 @@ namespace GUIEngine * that is used by the keyboard */ float m_percent_height; + /** A time for repeat key feature */ + unsigned int m_repeat_time; + /** The edit box that is assigned to the keyboard */ CGUIEditBox* m_edit_box; + /** A button that is used as backspace key */ + irr::gui::IGUIButton* m_back_button; + /** Remembers currently selected button type */ ButtonsType m_buttons_type; @@ -100,7 +107,6 @@ namespace GUIEngine static void dismiss(); static bool onEscapePressed(); - /** Returns pointer to the created keyboard or NULL if keyboard was * not created */ static ScreenKeyboard* getCurrent() {return m_screen_keyboard;} @@ -108,6 +114,9 @@ namespace GUIEngine /** Returns true if keyboard is created */ static bool isActive() {return m_screen_keyboard != NULL;} + /** Override to be notified of updates */ + virtual void onUpdate(float dt); + /** Get irrlicht window used by the keyboard widget */ irr::gui::IGUIWindow* getIrrlichtElement() {return m_irrlicht_window;}