Simple SFML GUI  0.2a
HaveAction.hpp
1 /*
2  * HaveAction.hpp
3  *
4  * Created on: 06.05.2017
5  * Author: jakub
6  */
7 
8 #ifndef HAVEACTION_HPP_
9 #define HAVEACTION_HPP_
10 
11 #include <SFML/System/String.hpp>
12 #include <map>
13 #include <functional>
14 #include "HaveEnabled.hpp"
15 
16 template<typename T>
18  using function_type = std::function<void (T)>;
19 };
20 
21 template <>
22 struct HaveActionHelper<void> {
23  using function_type = std::function<void ()>;
24 };
25 
26 
27 namespace ActionType {
28  const char* const main = "main";
29  const char* const destruction = "destruction";
30 }
31 
32 template <typename T>
33 class HaveAction {
34 public:
35  using function_type = typename HaveActionHelper<T>::function_type;
36 
37  HaveAction() {
38  mainActionType = ActionType::main;
39  }
40  virtual ~HaveAction() {
41  doAction(ActionType::destruction);
42  }
43  virtual void onSetAction(const sf::String& type) {}
44  virtual void setAction(function_type&& action, const sf::String& type = ActionType::main) {
45  if(type == ActionType::main) {
46  onSetAction(mainActionType);
47  this->action[mainActionType] = action;
48  } else {
49  onSetAction(type);
50  this->action[type] = action;
51  }
52  }
53  virtual void setAction(function_type& action, const sf::String& type = ActionType::main) {
54  if(type == ActionType::main) {
55  onSetAction(mainActionType);
56  this->action[mainActionType] = action;
57  } else {
58  onSetAction(type);
59  this->action[type] = action;
60  }
61  }
62  virtual void setAction(std::function<void ()>&& action, const sf::String& type = ActionType::main) {
63  if(type == ActionType::main) {
64  onSetAction(mainActionType);
65  actionVoid[mainActionType] = action;
66  } else {
67  onSetAction(type);
68  actionVoid[type] = action;
69  }
70  }
71  virtual void setAction(std::function<void ()>& action, const sf::String& type = ActionType::main) {
72  if(type == ActionType::main) {
73  onSetAction(mainActionType);
74  actionVoid[mainActionType] = action;
75  } else {
76  onSetAction(type);
77  actionVoid[type] = action;
78  }
79  }
80  virtual void doAction(const sf::String& type = ActionType::main) {
81  auto he = dynamic_cast<const HaveEnabled*>(this);
82  if(he != nullptr && !he->isEnabled()) {
83  return;
84  }
85 
86  if(type == ActionType::main) {
87  if(action[mainActionType]) {
88  action[mainActionType](dynamic_cast<T>(*this));
89  } else if(actionVoid[mainActionType]) {
90  actionVoid[mainActionType]();
91  }
92  } else {
93  if(action[type]) {
94  action[type](dynamic_cast<T>(*this));
95  } else if(actionVoid[type]) {
96  actionVoid[type]();
97  }
98  }
99  }
100 
101 protected:
102  std::map<sf::String,function_type> action;
103  std::map<sf::String, std::function<void ()>> actionVoid;
104  sf::String mainActionType;
105 };
106 
107 template<>
108 class HaveAction<void> {
109 public:
110  using function_type = typename HaveActionHelper<void>::function_type;
111 
112  HaveAction() {
113  mainActionType = ActionType::main;
114  }
115  virtual ~HaveAction() {}
116  virtual void onSetAction(const sf::String& type) {}
117  virtual void setAction(function_type&& action, const sf::String& type = ActionType::main) {
118  if(type == ActionType::main) {
119  onSetAction(mainActionType);
120  this->action[mainActionType] = action;
121  } else {
122  onSetAction(type);
123  this->action[type] = action;
124  }
125  }
126  virtual void setAction(function_type& action, const sf::String& type = ActionType::main) {
127  if(type == ActionType::main) {
128  onSetAction(mainActionType);
129  this->action[mainActionType] = action;
130  } else {
131  onSetAction(type);
132  this->action[type] = action;
133  }
134  }
135  virtual void doAction(const sf::String& type = ActionType::main) {
136  auto he = dynamic_cast<const HaveEnabled*>(this);
137  if(he != nullptr && !he->isEnabled()) {
138  return;
139  }
140 
141  if(type == ActionType::main && action[mainActionType]) {
142  action[mainActionType]();
143  } else if(action[type]) {
144  action[type]();
145  }
146  }
147 
148 protected:
149  std::map<sf::String,function_type> action;
150  sf::String mainActionType;
151 
152 };
153 
154 #endif /* HAVEACTION_HPP_ */
Definition: Checkbox.hpp:13
Definition: HaveEnabled.hpp:15
Definition: HaveAction.hpp:33
Definition: HaveAction.hpp:17