summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/touchmanager.cpp119
-rw-r--r--src/touchmanager.h16
2 files changed, 108 insertions, 27 deletions
diff --git a/src/touchmanager.cpp b/src/touchmanager.cpp
index 5b04b2023..359eebbf9 100644
--- a/src/touchmanager.cpp
+++ b/src/touchmanager.cpp
@@ -47,6 +47,7 @@ TouchManager::TouchManager() :
TouchManager::~TouchManager()
{
+ config.removeListeners(this);
clear();
delete mVertexes;
mVertexes = nullptr;
@@ -54,23 +55,21 @@ TouchManager::~TouchManager()
void TouchManager::init()
{
+ config.addListener("showScreenJoystick", this);
+ config.addListener("showScreenButtons", this);
+
+ mShowJoystick = config.getBoolValue("showScreenJoystick");
+ mShowButtons = config.getBoolValue("showScreenButtons");
+
#ifdef ANDROID
loadTouchItem(&mKeyboard, "keyboard_icon.xml", 28, 28, NORMAL,
nullptr, nullptr, &showKeyboard, nullptr);
#endif
- if (config.getBoolValue("showScreenJoystick"))
- {
- loadTouchItem(&mPad, "dpad.xml", 100, 100, LEFT,
- &padEvents, &padClick, &padUp, &padOut);
- }
- if (config.getBoolValue("showScreenButtons"))
- {
- loadTouchItem(&mAttack, "dpad_attack.xml", 60, 60, RIGHT,
- nullptr, &attackClick, nullptr, nullptr);
- loadTouchItem(&mCancel, "dpad_cancel.xml", 60, 60, RIGHT,
- nullptr, &cancelClick, nullptr, nullptr);
- }
+ if (mShowJoystick)
+ loadPad();
+ if (mShowButtons)
+ loadButtons();
}
void TouchManager::loadTouchItem(TouchItem **item, std::string name,
@@ -122,26 +121,13 @@ void TouchManager::loadTouchItem(TouchItem **item, std::string name,
void TouchManager::clear()
{
-// unloadTouchItem(&mPad);
-// unloadTouchItem(&mKeyboard);
-
Theme *theme = Theme::instance();
if (!theme)
return;
for (TouchItemVectorCIter it = mObjects.begin(), it_end = mObjects.end();
it != it_end; ++ it)
{
- TouchItem *item = *it;
- if (item)
- {
- if (item->images)
- {
- theme->unloadRect(*item->images);
- delete item->images;
- item->images = nullptr;
- }
- delete item;
- }
+ unload(*it);
}
mObjects.clear();
mRedraw = true;
@@ -270,3 +256,84 @@ void TouchManager::resize(int width, int height)
}
}
}
+
+void TouchManager::unload(TouchItem *item)
+{
+ if (item)
+ {
+ Theme *const theme = Theme::instance();
+ if (!theme)
+ return;
+ if (item->images)
+ {
+ theme->unloadRect(*item->images);
+ delete item->images;
+ item->images = nullptr;
+ }
+ delete item;
+ }
+}
+
+void TouchManager::unloadTouchItem(TouchItem **unloadItem)
+{
+ Theme *theme = Theme::instance();
+ if (!theme)
+ return;
+ for (TouchItemVectorIter it = mObjects.begin(), it_end = mObjects.end();
+ it != it_end; ++ it)
+ {
+ TouchItem *item = *it;
+ if (item && *unloadItem == item)
+ {
+ mObjects.erase(it);
+ unload(item);
+ return;
+ }
+ }
+}
+
+void TouchManager::loadPad()
+{
+ loadTouchItem(&mPad, "dpad.xml", 100, 100, LEFT,
+ &padEvents, &padClick, &padUp, &padOut);
+}
+
+void TouchManager::loadButtons()
+{
+ loadTouchItem(&mAttack, "dpad_attack.xml", 60, 60, RIGHT,
+ nullptr, &attackClick, nullptr, nullptr);
+ loadTouchItem(&mCancel, "dpad_cancel.xml", 60, 60, RIGHT,
+ nullptr, &cancelClick, nullptr, nullptr);
+}
+
+void TouchManager::optionChanged(const std::string &value)
+{
+ logger->log("changed");
+ if (value == "showScreenJoystick")
+ {
+ if (mShowJoystick == config.getBoolValue("showScreenJoystick"))
+ return;
+ mShowJoystick = config.getBoolValue("showScreenJoystick");
+ if (mShowJoystick)
+ loadPad();
+ else
+ unloadTouchItem(&mPad);
+ mRedraw = true;
+ }
+ else if (value == "showScreenButtons")
+ {
+ if (mShowButtons == config.getBoolValue("showScreenButtons"))
+ return;
+ mShowButtons = config.getBoolValue("showScreenButtons");
+ if (mShowButtons)
+ {
+ loadButtons();
+ }
+ else
+ {
+ unloadTouchItem(&mAttack);
+ unloadTouchItem(&mCancel);
+ }
+ mRedraw = true;
+ }
+}
diff --git a/src/touchmanager.h b/src/touchmanager.h
index fb81a0beb..e3d76d910 100644
--- a/src/touchmanager.h
+++ b/src/touchmanager.h
@@ -23,6 +23,7 @@
#include "resources/image.h"
+#include "configlistener.h"
#include "keydata.h"
#include <guichan/mouseinput.hpp>
@@ -80,8 +81,9 @@ struct TouchItem final
typedef std::vector<TouchItem*> TouchItemVector;
typedef TouchItemVector::const_iterator TouchItemVectorCIter;
+typedef TouchItemVector::iterator TouchItemVectorIter;
-class TouchManager final
+class TouchManager final : public ConfigListener
{
public:
TouchManager();
@@ -120,6 +122,16 @@ class TouchManager final
void resize(int width, int height);
+ void unload(TouchItem *item);
+
+ void unloadTouchItem(TouchItem **unloadItem);
+
+ void optionChanged(const std::string &value);
+
+ void loadPad();
+
+ void loadButtons();
+
private:
TouchItem *mKeyboard;
TouchItem *mPad;
@@ -129,6 +141,8 @@ class TouchManager final
ImageCollection *mVertexes;
bool mActions[actionsSize];
bool mRedraw;
+ bool mShowJoystick;
+ bool mShowButtons;
};
extern TouchManager touchManager;