Rose
Application.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <utility>
11 
12 #include "Visual.h"
13 #include "GraphicsModel.h"
14 
15 namespace rose {
16 
22  public:
27  enum WindowEventType : int {
28  Shown, Hidden, Exposed, SizeChanged, Minimized, Maximized, FullScreen, Restored, Enter, Leave, Focus, UnFocus,
29  Close, Moved, Resized,
30  };
31 
32  using WindowStateChangeCallback = std::function<void(Application&, WindowEventType)>;
33  using WindowPositionChangeCallback = std::function<void(Application&, WindowEventType, Position<int>&)>;
34  using WindowSizeChangeCallback = std::function<void(Application&, WindowEventType, Size)>;
35  using KeyboardEventCallback = std::function<bool(Application&, const SDL_KeyboardEvent&)>;
36  using MouseMotionEventCallback = std::function<bool(Application&, const SDL_MouseMotionEvent&)>;
37  using MouseButtonEventCallback = std::function<bool(Application&, const SDL_MouseButtonEvent&)>;
38  using MouseWheelEventCallback = std::function<bool(Application&, const SDL_MouseWheelEvent&)>;
39  using FingerTouchEventCallback = std::function<bool(Application&, const SDL_TouchFingerEvent&)>;
40  using TextInputEventCallback = std::function<bool(Application&, const SDL_TextInputEvent&)>;
41 
42  protected:
45  WindowSizeChangeCallback windowSizeChangeCallback;
46  WindowPositionChangeCallback windowPositionChangeCallback;
47  WindowStateChangeCallback windowStateChangeCallback;
48  KeyboardEventCallback keyboardEventCallback;
49  MouseMotionEventCallback mouseMotionEventCallback;
50  MouseButtonEventCallback mouseButtonEventCallback;
51  MouseWheelEventCallback mouseWheelEventCallback;
52  FingerTouchEventCallback fingerTouchEventCallback;
53  TextInputEventCallback textInputEventCallback;
54 
55  void windowStateChange(WindowEventType type) {
56  if (windowStateChangeCallback)
57  windowStateChangeCallback(mApplication, type);
58  }
59 
60  void windowSizeChange(WindowEventType type, Size size) {
61  if (windowSizeChangeCallback)
62  windowSizeChangeCallback(mApplication, type, size);
63  }
64 
65  void windowPositionChange(WindowEventType type, Position<int> position) {
66  if (windowPositionChangeCallback)
67  windowPositionChangeCallback(mApplication, type, position);
68  }
69 
70  public:
71  EventSemantics() = delete;
72 
73  explicit EventSemantics(Application& application) : mApplication(application) {}
74 
75  void onEvent(SDL_Event &e);
76 
77  void windowEvent(SDL_WindowEvent &e);
78 
79  void keyboardEvent(SDL_KeyboardEvent &e);
80 
81  void mouseMotionEvent(SDL_MouseMotionEvent &e);
82 
83  void mouseButtonEvent(SDL_MouseButtonEvent &e);
84 
85  void mouseWheelEvent(SDL_MouseWheelEvent &e);
86 
87  void fingerTouchEvent(SDL_TouchFingerEvent &e);
88 
89  void textInputEvent(SDL_TextInputEvent &e);
90 
91  void setWindowStateChangeCallback(WindowStateChangeCallback callback) {
92  windowStateChangeCallback = std::move(callback);
93  }
94 
95  void setWindowPositionChangeCallback(WindowPositionChangeCallback callback) {
96  windowPositionChangeCallback = std::move(callback);
97  }
98 
99  void setWindowSizeChangeCallback(WindowSizeChangeCallback callback) {
100  windowSizeChangeCallback = std::move(callback);
101  }
102 
103  void setKeyboardEventCallback(KeyboardEventCallback callback) {
104  keyboardEventCallback = std::move(callback);
105  }
106 
107  void setMouseMotionEventCallback(MouseMotionEventCallback callback) {
108  mouseMotionEventCallback = std::move(callback);
109  }
110 
111  void setMouseButtonEventCallback(MouseButtonEventCallback callback) {
112  mouseButtonEventCallback = std::move(callback);
113  }
114 
115  void setMouseWheelEventCallback(MouseWheelEventCallback callback) {
116  mouseWheelEventCallback = std::move(callback);
117  }
118 
119  void setFingerTouchEventCallback(FingerTouchEventCallback callback) {
120  fingerTouchEventCallback = std::move(callback);
121  }
122 
123  void setTextInputEventCallback(TextInputEventCallback callback) {
124  textInputEventCallback = std::move(callback);
125  }
126  };
127 
132  class InputParser {
133  public:
139  InputParser(int &argc, char **argv) {
140  for (int i = 1; i < argc; ++i)
141  this->tokens.emplace_back(argv[i]);
142  }
143 
145  [[nodiscard]] const std::string &getCmdOption(const std::string_view &option) const {
146  std::vector<std::string>::const_iterator itr;
147  itr = std::find(this->tokens.begin(), this->tokens.end(), option);
148  if (itr != this->tokens.end() && ++itr != this->tokens.end()) {
149  return *itr;
150  }
151  static const std::string empty_string;
152  return empty_string;
153  }
154 
156  [[nodiscard]] bool cmdOptionExists(const std::string_view &option) const {
157  return std::find(this->tokens.begin(), this->tokens.end(), option)
158  != this->tokens.end();
159  }
160 
161  private:
162  std::vector<std::string> tokens;
163  };
164 
169  class Application {
170  protected:
171  static constexpr std::string_view UsbDeviceByPath{"/dev/input/by-path/"};
172  static constexpr std::string_view KeyboardPathRegEx{".*-kbd"};
173 
174  std::shared_ptr<Screen> mScreen{};
175  EventSemantics mEventSemantics;
176  gm::GraphicsModel mGraphicsModel{};
177  EventSemantics::WindowEventType mAppState{EventSemantics::Restored};
178  InputParser mInputParser;
179 
180  std::shared_ptr<Widget> mPointerWidget{};
181  std::shared_ptr<Widget> mClickFocusWidget{};
182  std::shared_ptr<Widget> mDragFocusWidget{};
183  std::shared_ptr<Widget> mScrollFocusWidget{};
184  std::shared_ptr<Widget> mKeyFocusWidget{};
185 
186  std::map<SDL_Keycode, std::pair<uint32_t,std::weak_ptr<Widget>>> mKeyboardShortcuts{};
187 
188  bool mMouseButtonPressed{false};
189  uint mMouseButtonId{0};
190  Position<int> mMousePosition{};
191 
192  bool mKeyboardFound;
193 
194  public:
195  Application() = delete;
196 
197  Application(int argc, char **argv);
198 
199  virtual void initialize(const std::string &title, Size defaultSize);
200 
201  virtual void windowStateChange(EventSemantics::WindowEventType type);
202 
203  virtual void windowSizeChange(EventSemantics::WindowEventType type, Size size);
204 
205  virtual void windowPositionChange(EventSemantics::WindowEventType type, Position<int>& position);
206 
207  virtual bool keyboardEventCallback(const SDL_KeyboardEvent& keyboardEvent);
208 
209  virtual bool mouseMotionEventCallback(const SDL_MouseMotionEvent& mouseMotionEvent);
210 
211  virtual bool mouseButtonEventCallback(const SDL_MouseButtonEvent& mouseButtonEvent);
212 
213  virtual bool mouseWheelEventCallback(const SDL_MouseWheelEvent& mouseWheelEvent);
214 
215  virtual bool fingerTouchEventCallback(const SDL_TouchFingerEvent& fingerTouchEvent);
216 
217  virtual bool textInputEventCallback(const SDL_TextInputEvent& textInputEvent);
218 
219  gm::Context& context() { return mGraphicsModel.context(); }
220 
221  std::shared_ptr<Screen>& screen() { return mScreen; }
222 
223  void layout();
224 
225  std::shared_ptr<Widget> pointerWidget(const Position<int>& position);
226 
227  [[nodiscard]] Padding windowBorders() const noexcept {
228  return mGraphicsModel.windowBorders();
229  }
230 
231  gm::SdlWindow& getSdlWindow() {
232  return mGraphicsModel.getSdlWindow();
233  }
234 
235  void capturePointerWidget(std::shared_ptr<Widget> widget) {
236  if (mPointerWidget)
237  mPointerWidget->leaveEvent();
238  mPointerWidget = std::move(widget);
239  }
240 
241  void captureScrollWheelWidget(std::shared_ptr<Widget> widget) {
242  if (mPointerWidget)
243  mPointerWidget->leaveEvent();
244  mPointerWidget = std::move(widget);
245  }
246 
247  void
248  registerKeyboardShortcut(SDL_Keycode keycode, const std::shared_ptr<Widget> &widget, uint32_t shortcutCode) {
249  std::cout << __PRETTY_FUNCTION__ << " keycode: " << keycode << '\n';
250  mKeyboardShortcuts[keycode] = std::make_pair(shortcutCode, widget);
251  }
252 
253  void redrawBackground() {
254  mGraphicsModel.redrawBackground();
255  }
256 
257  [[nodiscard]] bool keyboardFound() const noexcept {
258  return mKeyboardFound;
259  }
260 
261  virtual void run();
262  };
263 }
264 
Definition: Application.h:169
WindowEventType
The type of window event.
Definition: Application.h:27
A class to process event semantics.
Definition: Application.h:21
const std::string & getCmdOption(const std::string_view &option) const
Definition: Application.h:145
Application & mApplication
The Application the EventSemantics are processed for.
Definition: Application.h:44
Abstraction of space consumed around an object for space, borders, etc.
Definition: Types.h:454
Context
Definition: GraphicsModel.h:76
Abstraction of the graphics model.
Parse command line arguments.
Definition: Application.h:132
std::unique_ptr< SDL_Window, SdlWindowDestroy > SdlWindow
An SDL_Window unique pointer.
Definition: GraphicsModel.h:44
A size in integer dimensions.
Definition: Types.h:230
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
Definition: GraphicsModel.h:461
InputParser(int &argc, char **argv)
Constructor.
Definition: Application.h:139
User Interface Visual types.
bool cmdOptionExists(const std::string_view &option) const
Definition: Application.h:156