Gorgon Game Engine
Widget.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Layer.h"
4 #include "../Geometry/Point.h"
5 #include "../Input/Keyboard.h"
6 #include "../Geometry/PointProperty.h"
7 #include "../Geometry/SizeProperty.h"
8 
9 namespace Gorgon { namespace UI {
10 
11  class WidgetContainer;
12 
16  class Widget {
17  friend class WidgetContainer;
18  //Non-virtual functions for visual studio
19 
20  void resize(const Geometry::Size &size) {
21  Resize(size);
22  }
23 
24  Geometry::Size getsize() const {
25  return GetSize();
26  }
27 
28  void move(const Geometry::Point &value) {
29  Move(value);
30  }
31 
32  Geometry::Point getlocation() const {
33  return GetLocation();
34  }
35  public:
36 
37  Widget() : Location(this), Size(this) {
38  }
39 
40  Widget(Widget &&) = default;
41 
42  virtual ~Widget() { }
43 
45  void Move(int x, int y) { Move({x, y}); }
46 
48  virtual void Move(const Geometry::Point &location) = 0;
49 
51  virtual Geometry::Point GetLocation() const = 0;
52 
54  virtual void Resize(int w, int h) { Resize({w, h}); };
55 
57  virtual void Resize(const Geometry::Size &size) = 0;
58 
60  virtual Geometry::Size GetSize() const = 0;
61 
63  Geometry::Bounds GetBounds() const { return {GetLocation(), GetSize()}; }
64 
66  int GetWidth() const { return GetSize().Width; }
67 
69  int GetHeight() const { return GetSize().Height; }
70 
72  void SetWidth(int width) { Resize(width, GetHeight()); }
73 
75  void SetHeight(int height) { Resize(GetWidth(), height); }
76 
80  virtual bool Activate() = 0;
81 
84  bool Remove();
85 
87  bool AllowFocus() const { return allowfocus() && visible; }
88 
91  bool Focus();
92 
95  bool Defocus();
96 
98  bool IsFocused() const { return focus; }
99 
101  void Show() { SetVisible(true); }
102 
104  void Hide() { SetVisible(false); }
105 
108 
110  void SetVisible(bool value);
111 
113  bool IsVisible() const {
114  return visible;
115  }
116 
122  bool EnsureVisible() const;
123 
125  void Enable() { SetEnabled(true); }
126 
128  void Disable() { SetEnabled(false); }
129 
132 
134  virtual void SetEnabled(bool value) = 0;
135 
137  virtual bool IsEnabled() const = 0;
138 
140  bool HasParent() const { return parent != nullptr; }
141 
144  WidgetContainer &GetParent() const;
145 
148  virtual void SetIsFloating(bool value) {
149  if(floating != value)
150  boundschanged();
151 
152  floating = value;
153  }
154 
157  bool IsFloating() const {
158  return floating;
159  }
160 
162  virtual bool KeyEvent(Input::Key, float) { return false; }
163 
166  virtual bool CharacterEvent(Char) { return false; }
167 
171  virtual bool Done() { return true; }
172 
175 
184 
187 
189  void setname(std::string value) {
190 #ifndef NDEBUG
191  dbgname = value;
192 #endif
193  }
194 
195 #ifndef NDEBUG
196  std::string dbgname;
197 #endif
198 
199  protected:
201  virtual bool addingto(WidgetContainer &) { return true; }
202 
204  virtual void addto(Layer &layer) = 0;
205 
207  virtual void addedto(WidgetContainer &container);
208 
210  virtual void removefrom(Layer &layer) = 0;
211 
213  virtual bool removingfrom() { return true; }
214 
216  virtual void removed();
217 
219  virtual void setlayerorder(Layer &layer, int order) = 0;
220 
222  virtual bool allowfocus() const { return true; }
223 
225  virtual void focused();
226 
230  virtual bool canloosefocus() const { return true; }
231 
234  virtual void focuslost();
235 
237  virtual void boundschanged();
238 
242  virtual void displaced() { }
243 
245  virtual void parentenabledchanged(bool state) { }
246 
247  private:
248  bool visible = true;
249  bool enabled = true;
250  bool focus = false;
251  bool floating= false;
252 
254  virtual void hide() = 0;
255 
257  virtual void show() = 0;
258 
259  WidgetContainer *parent = nullptr;
260  };
261 
262 
263 } }
Gorgon::UI::Widget::HasParent
bool HasParent() const
Returns if this widget has a parent.
Definition: Widget.h:140
Gorgon::UI::Widget::dbgname
std::string dbgname
Definition: Widget.h:196
Gorgon::Geometry::basic_Bounds
This class represents boundaries of 2D objects.
Definition: Bounds.h:27
Widget.h
Gorgon::UI::Widget::focused
virtual void focused()
This is called after the focus is transferred to this widget.
Definition: Widget.cpp:102
Gorgon::UI::Widget::ToggleVisible
void ToggleVisible()
Toggles the visibility state of the widget.
Definition: Widget.h:107
Gorgon::UI::Widget::GetWidth
int GetWidth() const
Returns the width of the widget.
Definition: Widget.h:66
Gorgon::UI::Widget::Widget
Widget()
Definition: Widget.h:37
Gorgon::UI::Widget::Focus
bool Focus()
Transfers the focus to this widget.
Definition: Widget.cpp:13
Gorgon::UI::Widget::allowfocus
virtual bool allowfocus() const
Should return true if the widget can be focused.
Definition: Widget.h:222
Gorgon::Event
This class provides event mechanism.
Definition: Event.h:134
Gorgon::UI::Widget::Done
virtual bool Done()
For widgets that supports it, this will trigger finalization the user interaction.
Definition: Widget.h:171
Gorgon::UI::Widget::Move
void Move(int x, int y)
Moves this widget to the given position.
Definition: Widget.h:45
Gorgon::UI::Widget::FocusEvent
Event< Widget > FocusEvent
This event will be fired when the widget receives or looses focus.
Definition: Widget.h:174
Gorgon::UI::Widget::addedto
virtual void addedto(WidgetContainer &container)
Called when this widget added to the given container.
Definition: Widget.cpp:71
Gorgon::UI::Widget::Show
void Show()
Shows this widget, widgets are visible by default.
Definition: Widget.h:101
Gorgon::UI::Widget::SetIsFloating
virtual void SetIsFloating(bool value)
Sets floating status of this widget.
Definition: Widget.h:148
Gorgon::UI::Widget::AllowFocus
bool AllowFocus() const
If this widget can be focused currently.
Definition: Widget.h:87
Gorgon::UI::Widget::parentenabledchanged
virtual void parentenabledchanged(bool state)
This function is called when the parent's enabled state changes.
Definition: Widget.h:245
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::UI::Widget::~Widget
virtual ~Widget()
Definition: Widget.h:42
Gorgon::UI::Widget::Enable
void Enable()
Enables the widget so that the user can interact with it.
Definition: Widget.h:125
Gorgon::UI::Widget::Activate
virtual bool Activate()=0
Activates the widget.
Gorgon::UI::Widget::removed
virtual void removed()
Called after this widget is removed from its parent.
Definition: Widget.cpp:83
Gorgon::UI::WidgetContainer::childboundschanged
virtual void childboundschanged(Widget *source)
The boundary of any of the children is changed. Source could be nullptr.
Definition: WidgetContainer.cpp:431
Gorgon::UI::Widget::Hide
void Hide()
Hides this widget, when hidden, widgets cannot gain focus.
Definition: Widget.h:104
Gorgon::UI::Widget::Size
Geometry::SizeProperty< Widget, &Widget::getsize, &Widget::resize > Size
Definition: Widget.h:186
Gorgon::Geometry::basic_SizeProperty
Property support for point class.
Definition: SizeProperty.h:13
Gorgon::UI::Widget::displaced
virtual void displaced()
Call this function when the widget container causes it to be displaced.
Definition: Widget.h:242
Gorgon::UI::Widget::SetVisible
void SetVisible(bool value)
Changes the visibility of the widget.
Definition: Widget.cpp:38
Gorgon::UI::Widget::Resize
virtual void Resize(int w, int h)
Changes the size of the widget.
Definition: Widget.h:54
Gorgon::UI::WidgetContainer::IsEnabled
virtual bool IsEnabled() const
Returns whether container is enabled.
Definition: WidgetContainer.h:190
Gorgon::Input::Key
int Key
A type to represent an input key.
Definition: Input.h:14
Gorgon::UI::Widget::GetParent
WidgetContainer & GetParent() const
Returns the parent of this widget, throws if it does not have a parent.
Definition: Widget.cpp:30
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Char
uint32_t Char
Definition: Types.h:46
Gorgon::UI::Widget::GetSize
virtual Geometry::Size GetSize() const =0
Returns the size of the widget.
Gorgon::UI::Widget::Disable
void Disable()
Disables the widget so that the user cannot interact with it.
Definition: Widget.h:128
Gorgon::UI::Widget::setname
void setname(std::string value)
This is a debug feature.
Definition: Widget.h:189
Gorgon::UI::Widget::addto
virtual void addto(Layer &layer)=0
When called, widget should locate itself on to this layer.
Gorgon::Geometry::Point
basic_Point< int > Point
Definition: Point.h:598
Gorgon::UI::Widget::ToggleEnabled
void ToggleEnabled()
Toggles enabled state of the widget.
Definition: Widget.h:131
Gorgon::UI::Widget::Remove
bool Remove()
Removes the widget from its parent.
Definition: Widget.cpp:6
Gorgon::UI::Widget::canloosefocus
virtual bool canloosefocus() const
Should return true if the widget can loose the focus right now.
Definition: Widget.h:230
Gorgon::UI::WidgetContainer::RemoveFocus
bool RemoveFocus()
Removes the focus from the focused widget.
Definition: WidgetContainer.cpp:335
Gorgon::UI::Widget::GetHeight
int GetHeight() const
Returns the height of the widget.
Definition: Widget.h:69
Gorgon::UI::Widget::removefrom
virtual void removefrom(Layer &layer)=0
When called, widget should remove itself from the given layer.
Gorgon::UI::Widget::EnsureVisible
bool EnsureVisible() const
Ensures this widget is visible in its container by scrolling it into view.
Definition: Widget.cpp:55
Gorgon::Layer
This class is the base class for all layer types.
Definition: Layer.h:79
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::UI::Widget::Widget
Widget(Widget &&)=default
Gorgon::UI::Widget::Defocus
bool Defocus()
Removes the focus from this widget if this widget is focused.
Definition: Widget.cpp:23
Gorgon::UI::Widget::IsEnabled
virtual bool IsEnabled() const =0
Returns whether the widget is enabled.
Gorgon::UI::WidgetContainer::Remove
bool Remove(Widget &widget)
Removes the given widget from this container.
Definition: WidgetContainer.cpp:57
Gorgon::UI::Widget::SetEnabled
virtual void SetEnabled(bool value)=0
Sets the enabled state of the widget.
Gorgon::UI::Widget::IsFocused
bool IsFocused() const
Returns if this widget is focused.
Definition: Widget.h:98
Gorgon::UI::Widget::CharacterEvent
virtual bool CharacterEvent(Char)
This function should be called whenever a character is received from operating system.
Definition: Widget.h:166
Gorgon::UI::Widget::focuslost
virtual void focuslost()
This is called after the focus is lost.
Definition: Widget.cpp:96
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::UI::Widget::GetLocation
virtual Geometry::Point GetLocation() const =0
Returns the location of the widget.
Gorgon::UI::Widget::Location
Geometry::PointProperty< Widget, &Widget::getlocation, &Widget::move > Location
Definition: Widget.h:185
Gorgon::UI::Widget::IsFloating
bool IsFloating() const
Returns floating status of this widget.
Definition: Widget.h:157
Gorgon::UI::Widget::setlayerorder
virtual void setlayerorder(Layer &layer, int order)=0
When called, widget should reorder itself in layer hierarchy.
Gorgon::UI::Widget::SetHeight
void SetHeight(int height)
Sets the height of the widget.
Definition: Widget.h:75
Gorgon::UI::WidgetContainer::EnsureVisible
virtual bool EnsureVisible(const Widget &widget)=0
Ensures the widget is visible.
Gorgon::UI::WidgetContainer
This class is the base class for all widget containers.
Definition: WidgetContainer.h:37
Gorgon::UI::Widget::boundschanged
virtual void boundschanged()
Call this function when the widget bounds is changed.
Definition: Widget.cpp:62
WidgetContainer.h
Gorgon::UI::Widget::addingto
virtual bool addingto(WidgetContainer &)
Called when it is about to be added to the given container.
Definition: Widget.h:201
Gorgon::UI::Widget::BoundsChangedEvent
Event< Widget > BoundsChangedEvent
This event will be fired when the area that the widget occupies on its container is changed.
Definition: Widget.h:183
Gorgon::UI::Widget::SetWidth
void SetWidth(int width)
Sets the width of the widget.
Definition: Widget.h:72
Gorgon::UI::Widget::IsVisible
bool IsVisible() const
Returns if the widget is visible.
Definition: Widget.h:113
Gorgon::UI::Widget::KeyEvent
virtual bool KeyEvent(Input::Key, float)
This function should be called whenever a key is pressed or released.
Definition: Widget.h:162
Gorgon::UI::WidgetContainer::SetFocusTo
bool SetFocusTo(Widget &widget)
Sets the focus to the given widget.
Definition: WidgetContainer.cpp:297
Gorgon::UI::Widget::removingfrom
virtual bool removingfrom()
Called before this widget is removed from its parent.
Definition: Widget.h:213
Gorgon::Geometry::basic_PointProperty
Property support for point class.
Definition: PointProperty.h:12
Gorgon::UI::Widget
This class is the base for all widgets.
Definition: Widget.h:16
Gorgon::UI::Widget::GetBounds
Geometry::Bounds GetBounds() const
Returns the bounds of the widget.
Definition: Widget.h:63