summaryrefslogtreecommitdiff
path: root/ui/modeHelper.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-29 19:07:11 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-29 19:07:11 +0100
commit5a0b3927a33807cca4c77c40eb873f8a3b51b0b0 (patch)
tree4af0585ee8f8f468ab10c0a4fe9994fb30b79599 /ui/modeHelper.h
parentDunno how, but some DOS new lines got in here! (diff)
downloadilt-5a0b3927a33807cca4c77c40eb873f8a3b51b0b0.tar.bz2
ilt-5a0b3927a33807cca4c77c40eb873f8a3b51b0b0.tar.xz
ilt-5a0b3927a33807cca4c77c40eb873f8a3b51b0b0.zip
Drop .hpp for header only things
Half of them acquired a .cpp part anyway
Diffstat (limited to 'ui/modeHelper.h')
-rw-r--r--ui/modeHelper.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/ui/modeHelper.h b/ui/modeHelper.h
new file mode 100644
index 0000000..77707f2
--- /dev/null
+++ b/ui/modeHelper.h
@@ -0,0 +1,39 @@
+#pragma once
+
+#include <memory>
+union SDL_Event;
+
+enum ModeSecondClick { Unset, Reset, NoAction };
+template<typename Target, ModeSecondClick msc = ModeSecondClick::Unset> class Mode {
+public:
+ explicit Mode(Target & t) : target {t} { }
+
+ 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)...);
+ }
+ }
+};