summaryrefslogtreecommitdiff
path: root/ui/modeHelper.hpp
blob: 508beedf408a7d8fd79410a96e71186a049ca5f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)...);
		}
	}
};