HoverRace  2.0
ClickRegion.h
Go to the documentation of this file.
1 
2 // ClickRegion.h
3 //
4 // Copyright (c) 2013, 2014, 2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "UiViewModel.h"
25 
26 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
27 # ifdef MR_ENGINE
28 # define MR_DllDeclare __declspec( dllexport )
29 # else
30 # define MR_DllDeclare __declspec( dllimport )
31 # endif
32 #else
33 # define MR_DllDeclare
34 #endif
35 
36 namespace HoverRace {
37  namespace Display {
38  class Display;
39  }
40 }
41 
42 namespace HoverRace {
43 namespace Display {
44 
50 {
51  using SUPER = UiViewModel;
52 
53 public:
54  struct Props
55  {
56  enum {
58  SIZE = SUPER::Props::NEXT_,
62  };
63  };
64 
65 public:
66  ClickRegion(Display &display, uiLayoutFlags_t layoutFlags = 0);
67  ClickRegion(Display &display, const Vec2 &size,
68  uiLayoutFlags_t layoutFlags = 0);
69  virtual ~ClickRegion();
70 
71 public:
72  void AttachView(Display &disp) override { AttachViewDynamic(disp, this); }
73 
74 public:
75  bool OnMouseMoved(const Vec2 &pos) override;
76  bool OnMousePressed(const Control::Mouse::Click &click) override;
77  bool OnMouseReleased(const Control::Mouse::Click &click) override;
78  bool OnAction() override;
79 
80 protected:
85  virtual void OnMouseDrag(const Vec2 &relPos) { HR_UNUSED(relPos); }
86 
87 public:
88  using clickedSignal_t = boost::signals2::signal<void(ClickRegion&)>;
89  clickedSignal_t &GetClickedSignal() { return clickedSignal; }
90 protected:
91  virtual void FireClickedSignal();
92 
93 public:
94  bool TryFocus(const Control::Nav &nav = Control::Nav::NEUTRAL) override;
95 
96  const Vec2 &GetSize();
97  void SetSize(const Vec2 &size);
99  void SetSize(double w, double h) { SetSize(Vec2(w, h)); }
100 
106  bool IsAutoSize() const { return autoSize; }
107  void SetAutoSize();
108 
114  bool IsEnabled() const { return enabled; }
115  void SetEnabled(bool enabled);
116 
117 protected:
118  bool IsPressed() const { return pressed; }
119  void SetPressed(bool pressed);
120 
121 public:
122  Vec3 Measure() override { return size.Promote(); }
123 
124 protected:
125  void RequestSizing();
126  bool TestHit(const Vec2 &pos);
127  Vec2 ScreenPosToRel(const Vec2 &pos);
128 
129 protected:
131 private:
133  bool autoSize;
135  bool enabled;
136  bool pressed;
138 };
139 
140 } // namespace Display
141 } // namespace HoverRace
142 
143 #undef MR_DllDeclare
constexpr Vec3 Promote(double z=0) const noexcept
Definition: Vec.h:206
Vec3 Measure() override
Calculate the size of the component.
Definition: ClickRegion.h:122
bool pressed
Definition: ClickRegion.h:136
std::enable_if< MPL::HasSetSize< T >::value, void >::type SetSize(T &widget, double w, double h)
Definition: MPL.h:60
Base class for UI (2D) components.
Definition: UiViewModel.h:56
virtual void OnMouseDrag(const Vec2 &relPos)
Handle when the mouse is dragged over the widget.
Definition: ClickRegion.h:85
bool IsPressed() const
Definition: ClickRegion.h:118
bool needsSizing
Definition: ClickRegion.h:134
Mouse click events.
Definition: Action.h:67
Vec2 size
Definition: ClickRegion.h:132
bool IsAutoSize() const
Check if automatic sizing is enabled.
Definition: ClickRegion.h:106
Definition: Vec.h:38
Definition: ClickRegion.h:54
MR_UInt32 uiLayoutFlags_t
Definition: UiLayoutFlags.h:53
clickedSignal_t clickedSignal
Definition: ClickRegion.h:137
#define MR_DllDeclare
Definition: ClickRegion.h:33
clickedSignal_t & GetClickedSignal()
Definition: ClickRegion.h:89
Base class for display managers.
Definition: Display.h:73
bool enabled
Definition: ClickRegion.h:135
void AttachView(Display &disp) override
Definition: ClickRegion.h:72
A navigation direction.
Definition: Nav.h:45
bool autoSize
Definition: ClickRegion.h:133
Base class for clickable areas.
Definition: ClickRegion.h:49
void SetSize(double w, double h)
Convenience function for SetSize(const Vec2&).
Definition: ClickRegion.h:99
boost::signals2::signal< void(ClickRegion &)> clickedSignal_t
Definition: ClickRegion.h:88
static const Vec2 SIZE
Definition: ProfileEditScene.cpp:90
Definition: Announcement.h:24
bool IsEnabled() const
Check if the widget is enabled.
Definition: ClickRegion.h:114
Definition: Vec.h:114
Display & display
Definition: ClickRegion.h:130
First index for subclasses.
Definition: ClickRegion.h:61