From 5a71b6e0ea40d111b3a6ce7ec39406e4d432a3af Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 8 Oct 2022 16:09:07 +0100 Subject: Add toolber mode switching helper --- ui/modeHelper.hpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ui/modeHelper.hpp (limited to 'ui') 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 +union SDL_Event; + +enum ModeSecondClick { Unset, Reset, NoAction }; +template class Mode { +public: + Target & target; + + template + auto + toggle(Params &&... params) + { + return [params..., this](const SDL_Event &) { + toggleSetMode(std::forward(params)...); + }; + } + +private: + template + void + toggleSetMode(Params &&... params) + { + if (dynamic_cast(target.get())) { + if constexpr (msc == ModeSecondClick::Unset) { + target.reset(); + } + if constexpr (msc == ModeSecondClick::Reset) { + target = std::make_unique(std::forward(params)...); + } + } + else { + target = std::make_unique(std::forward(params)...); + } + } +}; -- cgit v1.2.3