aboutsummaryrefslogtreecommitdiff
path: root/src/gui/guiKeyChangeMenu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/guiKeyChangeMenu.cpp')
-rw-r--r--src/gui/guiKeyChangeMenu.cpp103
1 files changed, 48 insertions, 55 deletions
diff --git a/src/gui/guiKeyChangeMenu.cpp b/src/gui/guiKeyChangeMenu.cpp
index ca331a7d4..3f270fc7a 100644
--- a/src/gui/guiKeyChangeMenu.cpp
+++ b/src/gui/guiKeyChangeMenu.cpp
@@ -21,6 +21,7 @@
#include "guiKeyChangeMenu.h"
#include "debug.h"
+#include "guiButton.h"
#include "serialization.h"
#include <string>
#include <IGUICheckBox.h>
@@ -85,8 +86,6 @@ GUIKeyChangeMenu::GUIKeyChangeMenu(gui::IGUIEnvironment* env,
GUIModalMenu(env, parent, id, menumgr)
{
init_keys();
- for (key_setting *ks : key_settings)
- key_used.push_back(ks->key);
}
GUIKeyChangeMenu::~GUIKeyChangeMenu()
@@ -111,6 +110,7 @@ void GUIKeyChangeMenu::removeChildren()
for (gui::IGUIElement *i : children_copy) {
i->remove();
}
+ key_used_text = nullptr;
}
void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
@@ -157,7 +157,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect<s32> rect(0, 0, 100 * s, 30 * s);
rect += topleft + v2s32(offset.X + 150 * s, offset.Y - 5 * s);
const wchar_t *text = wgettext(k->key.name());
- k->button = Environment->addButton(rect, this, k->id, text);
+ k->button = GUIButton::addButton(Environment, rect, this, k->id, text);
delete[] text;
}
if ((i + 1) % KMaxButtonPerColumns == 0) {
@@ -217,16 +217,14 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect<s32> rect(0, 0, 100 * s, 30 * s);
rect += topleft + v2s32(size.X / 2 - 105 * s, size.Y - 40 * s);
const wchar_t *text = wgettext("Save");
- Environment->addButton(rect, this, GUI_ID_BACK_BUTTON,
- text);
+ GUIButton::addButton(Environment, rect, this, GUI_ID_BACK_BUTTON, text);
delete[] text;
}
{
core::rect<s32> rect(0, 0, 100 * s, 30 * s);
rect += topleft + v2s32(size.X / 2 + 5 * s, size.Y - 40 * s);
const wchar_t *text = wgettext("Cancel");
- Environment->addButton(rect, this, GUI_ID_ABORT_BUTTON,
- text);
+ GUIButton::addButton(Environment, rect, this, GUI_ID_ABORT_BUTTON, text);
delete[] text;
}
}
@@ -247,7 +245,13 @@ void GUIKeyChangeMenu::drawMenu()
bool GUIKeyChangeMenu::acceptInput()
{
for (key_setting *k : key_settings) {
- g_settings->set(k->setting_name, k->key.sym());
+ std::string default_key;
+ g_settings->getDefaultNoEx(k->setting_name, default_key);
+
+ if (k->key.sym() != default_key)
+ g_settings->set(k->setting_name, k->key.sym());
+ else
+ g_settings->remove(k->setting_name);
}
{
@@ -275,29 +279,28 @@ bool GUIKeyChangeMenu::acceptInput()
bool GUIKeyChangeMenu::resetMenu()
{
- if (activeKey >= 0)
- {
- for (key_setting *k : key_settings) {
- if (k->id == activeKey) {
- const wchar_t *text = wgettext(k->key.name());
- k->button->setText(text);
- delete[] text;
- break;
- }
- }
- activeKey = -1;
+ if (active_key) {
+ const wchar_t *text = wgettext(active_key->key.name());
+ active_key->button->setText(text);
+ delete[] text;
+ active_key = nullptr;
return false;
}
return true;
}
bool GUIKeyChangeMenu::OnEvent(const SEvent& event)
{
- if (event.EventType == EET_KEY_INPUT_EVENT && activeKey >= 0
+ if (event.EventType == EET_KEY_INPUT_EVENT && active_key
&& event.KeyInput.PressedDown) {
bool prefer_character = shift_down;
KeyPress kp(event.KeyInput, prefer_character);
+ if (event.KeyInput.Key == irr::KEY_DELETE)
+ kp = KeyPress(""); // To erase key settings
+ else if (event.KeyInput.Key == irr::KEY_ESCAPE)
+ kp = active_key->key; // Cancel
+
bool shift_went_down = false;
if(!shift_down &&
(event.KeyInput.Key == irr::KEY_SHIFT ||
@@ -305,51 +308,46 @@ bool GUIKeyChangeMenu::OnEvent(const SEvent& event)
event.KeyInput.Key == irr::KEY_RSHIFT))
shift_went_down = true;
- // Remove Key already in use message
- if(this->key_used_text)
- {
- this->key_used_text->remove();
- this->key_used_text = NULL;
- }
// Display Key already in use message
- if (std::find(this->key_used.begin(), this->key_used.end(), kp) != this->key_used.end())
- {
- core::rect < s32 > rect(0, 0, 600, 40);
+ bool key_in_use = false;
+ if (strcmp(kp.sym(), "") != 0) {
+ for (key_setting *ks : key_settings) {
+ if (ks != active_key && ks->key == kp) {
+ key_in_use = true;
+ break;
+ }
+ }
+ }
+
+ if (key_in_use && !this->key_used_text) {
+ core::rect<s32> rect(0, 0, 600, 40);
rect += v2s32(0, 0) + v2s32(25, 30);
const wchar_t *text = wgettext("Key already in use");
this->key_used_text = Environment->addStaticText(text,
rect, false, true, this, -1);
delete[] text;
- //infostream << "Key already in use" << std::endl;
+ } else if (!key_in_use && this->key_used_text) {
+ this->key_used_text->remove();
+ this->key_used_text = nullptr;
}
// But go on
{
- key_setting *k = NULL;
- for (key_setting *ks : key_settings) {
- if (ks->id == activeKey) {
- k = ks;
- break;
- }
- }
- FATAL_ERROR_IF(k == NULL, "Key setting not found");
- k->key = kp;
- const wchar_t *text = wgettext(k->key.name());
- k->button->setText(text);
+ active_key->key = kp;
+ const wchar_t *text = wgettext(kp.name());
+ active_key->button->setText(text);
delete[] text;
- this->key_used.push_back(kp);
-
// Allow characters made with shift
- if(shift_went_down){
+ if (shift_went_down){
shift_down = true;
return false;
}
- activeKey = -1;
+ active_key = nullptr;
return true;
}
- } else if (event.EventType == EET_KEY_INPUT_EVENT && activeKey < 0
+ } else if (event.EventType == EET_KEY_INPUT_EVENT && !active_key
&& event.KeyInput.PressedDown
&& event.KeyInput.Key == irr::KEY_ESCAPE) {
quitMenu();
@@ -378,24 +376,19 @@ bool GUIKeyChangeMenu::OnEvent(const SEvent& event)
quitMenu();
return true;
default:
- key_setting *k = NULL;
-
+ resetMenu();
for (key_setting *ks : key_settings) {
if (ks->id == event.GUIEvent.Caller->getID()) {
- k = ks;
+ active_key = ks;
break;
}
}
- FATAL_ERROR_IF(k == NULL, "Key setting not found");
+ FATAL_ERROR_IF(!active_key, "Key setting not found");
- resetMenu();
shift_down = false;
- activeKey = event.GUIEvent.Caller->getID();
const wchar_t *text = wgettext("press key");
- k->button->setText(text);
+ active_key->button->setText(text);
delete[] text;
- this->key_used.erase(std::remove(this->key_used.begin(),
- this->key_used.end(), k->key), this->key_used.end());
break;
}
Environment->setFocus(this);