From 2ff79baf26c8bd136d1b092390690726b6d77d19 Mon Sep 17 00:00:00 2001
From: GuillaumeBft <54895740+GuillaumeBft@users.noreply.github.com>
Date: Tue, 18 Feb 2020 17:37:26 +0100
Subject: [PATCH] Add rename config option (#4233)
* Add rename config option
* xmlDe/Encode to deal with " character
* m_config_name string -> stringw
* wcslen() -> empty() , remove useless c_str()
---
data/gui/screens/options_device.stkgui | 5 +++
src/input/device_config.cpp | 8 +++-
src/input/device_config.hpp | 17 ++++++--
.../options/options_screen_device.cpp | 22 +++++++++-
.../options/options_screen_input.cpp | 42 ++++++++++++++-----
5 files changed, 78 insertions(+), 16 deletions(-)
diff --git a/data/gui/screens/options_device.stkgui b/data/gui/screens/options_device.stkgui
index bf4717d81..5454e745a 100644
--- a/data/gui/screens/options_device.stkgui
+++ b/data/gui/screens/options_device.stkgui
@@ -53,6 +53,11 @@
+
+
+
+
+
diff --git a/src/input/device_config.cpp b/src/input/device_config.cpp
index 5e89a337e..ab0ffcbb8 100644
--- a/src/input/device_config.cpp
+++ b/src/input/device_config.cpp
@@ -23,6 +23,7 @@
#include "input/gamepad_android_config.hpp"
#include "input/keyboard_config.hpp"
#include "io/xml_node.hpp"
+#include "io/utf_writer.hpp"
#include "utils/log.hpp"
#include
@@ -73,6 +74,7 @@ DeviceConfig::DeviceConfig()
m_name = "";
m_enabled = true;
m_plugged = 0;
+ m_config_name= L"";
} // DeviceConfig
// ------------------------------------------------------------------------
@@ -291,7 +293,10 @@ bool DeviceConfig::doGetAction(Input::InputType type,
void DeviceConfig::save (std::ofstream& stream)
{
stream << "enabled=\""
- << (m_enabled ? "true\">\n" : "false\">\n");
+ << (m_enabled ? "true\"" : "false\"")
+ << " configName=\""
+ << StringUtils::xmlEncode(m_config_name)
+ << "\">\n ";
for(int n = 0; n < PA_COUNT; n++) // Start at 0?
{
@@ -312,6 +317,7 @@ bool DeviceConfig::load(const XMLNode *config)
{
config->get("name", &m_name);
config->get("enabled", &m_enabled);
+ config->getAndDecode("configName", &m_config_name);
bool error = false;
for(unsigned int i=0; igetNumNodes(); i++)
{
diff --git a/src/input/device_config.hpp b/src/input/device_config.hpp
index bbc727c1e..a11c96a81 100644
--- a/src/input/device_config.hpp
+++ b/src/input/device_config.hpp
@@ -49,9 +49,12 @@ private:
/** How many devices connected to the system which uses this config? */
int m_plugged;
- /** Name of this configuratiom. */
+ /** Internal name of this configuration. */
std::string m_name;
+ /** Name of this configuration (given by the user). */
+ irr::core::stringw m_config_name;
+
protected:
Binding m_bindings[PA_COUNT];
@@ -126,11 +129,11 @@ public:
} // getNumberOfAxes
// ------------------------------------------------------------------------
- /** Sets the name of this device. */
+ /** Sets the internal name of this device. */
void setName(const std::string &name) { m_name = name; }
// ------------------------------------------------------------------------
- /** Returns the name for this device configuration. */
+ /** Returns the internal name for this device configuration. */
const std::string& getName() const { return m_name; };
// ------------------------------------------------------------------------
@@ -156,6 +159,14 @@ public:
// ------------------------------------------------------------------------
/** Sets this config to be enabled or disabled. */
void setEnabled(bool new_value) { m_enabled = new_value; }
+
+ // ------------------------------------------------------------------------
+ /** Sets the name of this device configuration */
+ irr::core::stringw getConfigName() const { return m_config_name; }
+
+ // ------------------------------------------------------------------------
+ /** Returns the name of this device configuration */
+ void setConfigName( irr::core::stringw config_name ) { m_config_name = config_name; }
}; // class DeviceConfig
#endif
diff --git a/src/states_screens/options/options_screen_device.cpp b/src/states_screens/options/options_screen_device.cpp
index 01c5ca796..610b97237 100644
--- a/src/states_screens/options/options_screen_device.cpp
+++ b/src/states_screens/options/options_screen_device.cpp
@@ -24,6 +24,7 @@
#include "guiengine/widget.hpp"
#include "guiengine/widgets/button_widget.hpp"
#include "guiengine/widgets/label_widget.hpp"
+#include "guiengine/widgets/text_box_widget.hpp"
#include "guiengine/widgets/list_widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "input/input_manager.hpp"
@@ -31,6 +32,7 @@
#include "input/gamepad_device.hpp"
#include "io/file_manager.hpp"
#include "states_screens/dialogs/press_a_key_dialog.hpp"
+#include "states_screens/dialogs/general_text_field_dialog.hpp"
#include "states_screens/options/options_screen_audio.hpp"
#include "states_screens/options/options_screen_general.hpp"
#include "states_screens/options/options_screen_input.hpp"
@@ -637,6 +639,24 @@ void OptionsScreenDevice::eventCallback(Widget* widget,
input_manager->getDeviceManager()->save();
}
+ else if (name == "rename_config")
+ {
+ core::stringw configName = StringUtils::utf8ToWide(m_config->getName());
+ DeviceConfig *the_config = m_config; //Can't give variable m_config directly
+
+ new GeneralTextFieldDialog(configName, [] (const irr::core::stringw& text) {},
+ [the_config] (GUIEngine::LabelWidget* lw,
+ GUIEngine::TextBoxWidget* tb)->bool
+ {
+ core::stringw info = tb->getText();
+ if (info.empty())
+ return false;
+
+ the_config->setConfigName(info);
+ input_manager->getDeviceManager()->save();
+ return true;
+ });
+ }
} // eventCallback
@@ -695,4 +715,4 @@ bool OptionsScreenDevice::conflictsBetweenKbdConfig(PlayerAction action,
}
}
return false;
-} // conflictsBetweenKbdConfig
+} // conflictsBetweenKbdConfig
\ No newline at end of file
diff --git a/src/states_screens/options/options_screen_input.cpp b/src/states_screens/options/options_screen_input.cpp
index d7b72907e..7b43aab1b 100644
--- a/src/states_screens/options/options_screen_input.cpp
+++ b/src/states_screens/options/options_screen_input.cpp
@@ -107,10 +107,19 @@ void OptionsScreenInput::buildDeviceList()
{
const int icon = (config->isEnabled() ? 0 : 1);
- // since irrLicht's list widget has the nasty tendency to put the
- // icons very close to the text, I'm adding spaces to compensate.
- devices->addItem(internal_name, (core::stringw(" ") +
- _("Keyboard %i", i)).c_str(), icon);
+ //Display the configName instead of default name if it exists
+ if (!config->getConfigName().empty())
+ {
+ // since irrLicht's list widget has the nasty tendency to put the
+ // icons very close to the text, I'm adding spaces to compensate.
+ devices->addItem(internal_name, (core::stringw(" ") +
+ config->getConfigName()), icon);
+ }
+ else
+ {
+ devices->addItem(internal_name, (core::stringw(" ") +
+ _("Keyboard %i", i)).c_str(), icon);
+ }
}
}
@@ -123,15 +132,26 @@ void OptionsScreenInput::buildDeviceList()
// Don't display the configuration if a matching device is not available
if (config->isPlugged())
{
- // since irrLicht's list widget has the nasty tendency to put the
- // icons very close to the text, I'm adding spaces to compensate.
- irr::core::stringw name = (" " + config->getName()).c_str();
+ irr::core::stringw name;
- if (config->getNumberOfDevices() > 1)
+ //Display the configName instead of default name if it exists
+ if (!config->getConfigName().empty())
{
- name += core::stringw(L" (x");
- name += config->getNumberOfDevices();
- name += core::stringw(L")");
+ // since irrLicht's list widget has the nasty tendency to put the
+ // icons very close to the text, I'm adding spaces to compensate.
+ name = (core::stringw(" ") +
+ config->getConfigName());
+ }
+ else
+ {
+ name = (" " + config->getName()).c_str();
+
+ if (config->getNumberOfDevices() > 1)
+ {
+ name += core::stringw(L" (x");
+ name += config->getNumberOfDevices();
+ name += core::stringw(L")");
+ }
}
std::ostringstream gpname;