1 /*
2 Copyright (C) 2000 The Exult Team
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 
19 #ifndef GUMP_BUTTON_H
20 #define GUMP_BUTTON_H
21 
22 #include "Gump_widget.h"
23 
24 #include <tuple>
25 #include <type_traits>
26 
27 /*
28  *  A pushable button on a gump:
29  */
30 class Gump_button : public Gump_widget {
31 private:
32 	int pushed_button;      // 1 if in pushed state.
33 
34 public:
35 	friend class Gump;
36 	Gump_button(Gump *par, int shnum, int px, int py,
37 	            ShapeFile shfile = SF_GUMPS_VGA)
Gump_widget(par,shnum,px,py,shfile)38 		: Gump_widget(par, shnum, px, py, shfile), pushed_button(0)
39 	{  }
40 	// Is a given point on the checkmark?
on_button(int mx,int my)41 	bool on_button(int mx, int my) const override {
42 		return on_widget(mx, my);
43 	}
44 	// What to do when 'clicked':
45 	virtual bool activate(int button) = 0;
46 	// Or double-clicked.
47 	virtual void double_clicked(int x, int y);
48 	virtual bool push(int button);  // Redisplay as pushed.
49 	virtual void unpush(int button);
50 	void paint() override;
get_pushed()51 	int get_pushed() {
52 		return pushed_button;
53 	}
is_pushed()54 	bool is_pushed() {
55 		return pushed_button != 0;
56 	}
set_pushed(int button)57 	void set_pushed(int button) {
58 		pushed_button = button;
59 	}
set_pushed(bool set)60 	void set_pushed(bool set) {
61 		pushed_button = set ? 1 : 0;
62 	}
is_checkmark()63 	virtual bool is_checkmark() const {
64 		return false;
65 	}
as_button()66 	Gump_button *as_button() override {
67 		return this;
68 	}
69 };
70 
71 template <class Callable, class Tuple, size_t... Is>
call(Callable && func,Tuple && args,std::index_sequence<Is...>)72 inline auto call(Callable&& func, Tuple&& args,
73                        std::index_sequence<Is...>) {
74 	return func(std::get<Is>(args)...);
75 }
76 
77 template <typename Parent, typename Base, typename... Args>
78 class CallbackButtonBase : public Base {
79 public:
80 	using CallbackType = void (Parent::*)(Args...);
81 	using CallbackParams = std::tuple<Args...>;
82 
83 	template <typename... Ts>
CallbackButtonBase(Parent * par,CallbackType && callback,CallbackParams && params,Ts &&...args)84 	CallbackButtonBase(Parent* par, CallbackType&& callback, CallbackParams&& params, Ts&&... args)
85 		: Base(par, std::forward<Ts>(args)...),
86 		  parent(par), on_click(std::forward<CallbackType>(callback)),
87 		  parameters(std::forward<CallbackParams>(params)) {}
88 
activate(int button)89 	bool activate(int button) override {
90 		if (button != 1) return false;
91 		call([this](Args... args) {
92 				(parent->*on_click)(args...);
93 			}, parameters, std::make_index_sequence<sizeof...(Args)>{});
94 		return true;
95 	}
96 
97 private:
98 	Parent* parent;
99 	CallbackType on_click;
100 	CallbackParams parameters;
101 };
102 
103 template <typename Parent, typename Base>
104 class CallbackButtonBase<Parent, Base> : public Base {
105 public:
106 	using CallbackType = void (Parent::*)();
107 	using CallbackParams = std::tuple<>;
108 
109 	template <typename... Ts>
CallbackButtonBase(Parent * par,CallbackType && callback,Ts &&...args)110 	CallbackButtonBase(Parent* par, CallbackType&& callback, Ts&&... args)
111 		: Base(par, std::forward<Ts>(args)...),
112 		  parent(par), on_click(std::forward<CallbackType>(callback)) {}
113 
activate(int button)114 	bool activate(int button) override {
115 		if (button != 1) return false;
116 		(parent->*on_click)();
117 		return true;
118 	}
119 
120 private:
121 	Parent* parent;
122 	CallbackType on_click;
123 };
124 
125 template <typename Parent, typename... Args>
126 using CallbackButton = CallbackButtonBase<Parent, Gump_button, Args...>;
127 
128 #endif
129