summaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-10-08 16:09:07 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2022-10-08 16:09:07 +0100
commit5a71b6e0ea40d111b3a6ce7ec39406e4d432a3af (patch)
tree7e759c369f74c2a5c9d81e9193022bac95da36d1 /ui
parentAdd addStright to Network which looks up StraightLink in the network type (diff)
downloadilt-5a71b6e0ea40d111b3a6ce7ec39406e4d432a3af.tar.bz2
ilt-5a71b6e0ea40d111b3a6ce7ec39406e4d432a3af.tar.xz
ilt-5a71b6e0ea40d111b3a6ce7ec39406e4d432a3af.zip
Add toolber mode switching helper
Diffstat (limited to 'ui')
-rw-r--r--ui/modeHelper.hpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/ui/modeHelper.hpp b/ui/modeHelper.hpp
new file mode 100644
index 0000000..508beed
--- /dev/null
+++ b/ui/modeHelper.hpp
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <memory>
+union SDL_Event;
+
+enum ModeSecondClick { Unset, Reset, NoAction };
+template<typename Target, ModeSecondClick msc = ModeSecondClick::Unset> class Mode {
+public:
+ Target & target;
+
+ template<typename Mode, typename... Params>
+ auto
+ toggle(Params &&... params)
+ {
+ return [params..., this](const SDL_Event &) {
+ toggleSetMode<Mode>(std::forward<Params>(params)...);
+ };
+ }
+
+private:
+ template<typename Mode, typename... Params>
+ void
+ toggleSetMode(Params &&... params)
+ {
+ if (dynamic_cast<Mode *>(target.get())) {
+ if constexpr (msc == ModeSecondClick::Unset) {
+ target.reset();
+ }
+ if constexpr (msc == ModeSecondClick::Reset) {
+ target = std::make_unique<Mode>(std::forward<Params>(params)...);
+ }
+ }
+ else {
+ target = std::make_unique<Mode>(std::forward<Params>(params)...);
+ }
+ }
+};