Gorgon Game Engine
Button.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../UI/ComponentStackWidget.h"
4 #include "../Property.h"
5 #include "Registry.h"
6 
7 namespace Gorgon { namespace Graphics { class Bitmap; } }
8 
9 namespace Gorgon { namespace Widgets {
10 
12  public:
13  Button(const Button &) = delete;
14 
15  explicit Button(std::string text, Registry::TemplateType type = Registry::Button_Regular) :
16  Button(Registry::Active()[type], text)
17  {
18  }
19 
20  explicit Button(const char *text, Registry::TemplateType type = Registry::Button_Regular) :
21  Button(Registry::Active()[type], text)
22  {
23  }
24 
26  Button(Registry::Active()[type], "")
27  {
28  }
29 
30  template<class F_>
31  explicit Button(F_ clickfn, Registry::TemplateType type = Registry::Button_Regular) :
32  Button(Registry::Active()[type], "", clickfn)
33  {
34  }
35 
36  template<class F_>
37  explicit Button(std::string text, F_ clickfn, Registry::TemplateType type = Registry::Button_Regular) :
38  Button(Registry::Active()[type], text, clickfn)
39  {
40  }
41 
42  template<class F_>
43  explicit Button(const char *text, F_ clickfn, Registry::TemplateType type = Registry::Button_Regular) :
44  Button(Registry::Active()[type], text, clickfn)
45  {
46  }
47 
48  explicit Button(const UI::Template &temp, std::string text = "");
49 
50  Button(const UI::Template &temp, const char *text) : Button(temp, std::string(text)) { }
51 
52  template<class F_>
53  Button(const UI::Template &temp, F_ clickfn) : Button(temp, "") {
54  ClickEvent.Register(clickfn);
55  }
56 
57  template<class F_>
58  Button(const UI::Template &temp, std::string text, F_ clickfn) : Button(temp, text) {
59  ClickEvent.Register(clickfn);
60  }
61 
62  template<class F_>
63  Button(const UI::Template &temp, const char *text, F_ clickfn) : Button(temp, std::string(text), clickfn) { }
64 
65  virtual ~Button();
66 
68  void SetText(const std::string &value);
69 
71  std::string GetText() const { return text; }
72 
76  void SetIcon(const Graphics::Bitmap &value);
77 
81  void SetIcon(const Graphics::Drawable &value);
82 
85 
87 
91 
93  void RemoveIcon();
94 
96  bool HasIcon() const { return icon != nullptr; }
97 
100  const Graphics::Drawable &GetIcon() const {
101  if(!HasIcon())
102  throw std::runtime_error("This widget has no icon.");
103 
104  return *icon;
105  }
106 
108  void OwnIcon();
109 
111  void OwnIcon(const Graphics::Animation &value);
112 
114  void OwnIcon(Graphics::Bitmap &&value);
115 
120  void ActivateClickRepeat(int delay = 500, int repeat = 400, int accelerationtime = 2000, int minrepeat = 200);
121 
122 
124  void DeactivateClickRepeat();
125 
126  virtual bool Activate() override;
127 
128  bool KeyEvent(Input::Key key, float state) override;
129 
131 
133 
137 
138  private:
139  std::string text;
140  const Graphics::Drawable *icon = nullptr;
141  const Graphics::AnimationProvider *iconprov = nullptr;
142  bool ownicon = false;
143  bool spacedown = false;
144  bool mousedown = false;
145 
146  enum repeatstate {
147  repeatdisabled,
148  repeatstandby,
149  repeatondelay,
150  repeating,
151  };
152 
153  int repeatdelay = 500;
154  int repeatinit = 400;
155  int repeatacc = 2000;
156  int repeatfin = 400;
157  int repeatdiff = -200;
158  float repeatcur = 0;
159  int repeatleft = -1;
160  repeatstate repeaten = repeatdisabled;
161 
162  protected:
163  virtual bool allowfocus() const override;
164 
165  void repeattick();
166  };
167 
168 } }
Gorgon::UI::Widget::HasParent
bool HasParent() const
Returns if this widget has a parent.
Definition: Widget.h:140
Gorgon::Input::Keyboard::Keycodes::Numpad_Enter
constexpr Key Numpad_Enter
Definition: Keyboard.h:131
Gorgon::Widgets::Button::HasIcon
bool HasIcon() const
Returns if the button has an icon.
Definition: Button.h:96
Gorgon::Input::Keyboard::Keycodes::Enter
constexpr Key Enter
Definition: Keyboard.h:56
Gorgon::UI::ComponentStackWidget::stack
ComponentStack & stack
Definition: ComponentStackWidget.h:76
Gorgon::ObjectProperty
Object property allows the consumers of the property to be able to access object's member functions a...
Definition: Property.h:323
Gorgon::Widgets::Button::Button
Button(F_ clickfn, Registry::TemplateType type=Registry::Button_Regular)
Definition: Button.h:31
Gorgon::Widgets::Button::Button
Button(const UI::Template &temp, const char *text, F_ clickfn)
Definition: Button.h:63
Gorgon::UI::Widget::Focus
bool Focus()
Transfers the focus to this widget.
Definition: Widget.cpp:13
Gorgon::UI::ComponentTemplate::Icon
@ Icon
Data will effect the displayed graphics.
Definition: Template.h:678
Gorgon::UI::ComponentStack::SetData
void SetData(ComponentTemplate::DataEffect effect, const std::string &text)
Sets the data for a specific data effect.
Definition: ComponentStack.cpp:725
Gorgon::Input::Keyboard::Keycodes::Space
constexpr Key Space
Definition: Keyboard.h:59
Gorgon::Event
This class provides event mechanism.
Definition: Event.h:134
Gorgon::Graphics::AnimationProvider
A regular drawable animation provider.
Definition: Animations.h:28
Gorgon::Widgets::Registry::Button_Regular
@ Button_Regular
Definition: Registry.h:19
Gorgon::Widgets::Button::GetText
std::string GetText() const
Returns the text displayed on the button.
Definition: Button.h:71
Gorgon::Input::Keyboard::CurrentModifier
Modifier CurrentModifier
Current keyboard modifier, this is a global value.
Definition: Input.cpp:7
Gorgon::UI::Template
This class stores visual information about a widget template.
Definition: Template.h:392
Gorgon::UI::WidgetContainer::CurrentFocusStrategy
FocusStrategy CurrentFocusStrategy() const
Returns the active focus strategy. This function will not return Inherit.
Definition: WidgetContainer.h:340
Gorgon::Widgets::Button::Button
Button(const UI::Template &temp, const char *text)
Definition: Button.h:50
Gorgon::Widgets::Button::Button
Button(const UI::Template &temp, std::string text, F_ clickfn)
Definition: Button.h:58
Gorgon::TextualProperty
Supports everything that string class supports including +, +=, length()
Definition: Property.h:562
Gorgon::Resource::GID::Text
constexpr Type Text
Stores text data, no longer a resource.
Definition: GID.h:134
Gorgon::Widgets::Button::Button
Button(const Button &)=delete
Gorgon::Widgets::Button::Text
TextualProperty< Button, std::string, &Button::GetText, &Button::SetText > Text
Definition: Button.h:130
Button.h
Gorgon::Widgets::Button::Activate
virtual bool Activate() override
Activates the widget.
Definition: Button.cpp:137
Gorgon::Widgets::Button::Button
Button(Registry::TemplateType type=Registry::Button_Regular)
Definition: Button.h:25
Gorgon::Time::DeltaTime
unsigned long DeltaTime()
Returns the time passed since the last frame.
Definition: Time.h:258
Gorgon::Widgets::Button::repeattick
void repeattick()
Definition: Button.cpp:216
Gorgon::Graphics::Drawable::~Drawable
virtual ~Drawable()
Definition: Drawables.h:19
Gorgon::Widgets::Button::SetIcon
void SetIcon(const Graphics::Bitmap &value)
Changes the icon on the button.
Definition: Button.cpp:91
Gorgon::Widgets::Button::Button
Button(const char *text, Registry::TemplateType type=Registry::Button_Regular)
Definition: Button.h:20
Gorgon::UI::ComponentStack::RemoveCondition
void RemoveCondition(ComponentCondition condition, bool transition=true)
Removes a condition and its associated components.
Definition: ComponentStack.h:54
Gorgon::UI::ComponentStack::RemoveData
void RemoveData(ComponentTemplate::DataEffect effect)
Removes the data associated with data effect.
Definition: ComponentStack.cpp:759
Gorgon::Widgets::Button::KeyEvent
bool KeyEvent(Input::Key key, float state) override
This function should be called whenever a key is pressed or released.
Definition: Button.cpp:148
Gorgon::Widgets::Registry::TemplateType
TemplateType
This enum lists all possible template types.
Definition: Registry.h:18
Gorgon::UI::ComponentTemplate::Text
@ Text
Works only for TextholderTemplate, data will affect the text that is displayed.
Definition: Template.h:651
Gorgon::Input::Key
int Key
A type to represent an input key.
Definition: Input.h:14
Gorgon::Widgets::Button::RemoveIcon
void RemoveIcon()
Removes the icon on the button.
Definition: Button.cpp:109
Gorgon::Widgets::Button::Button
Button(std::string text, Registry::TemplateType type=Registry::Button_Regular)
Definition: Button.h:15
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::UI::ComponentStack::SetFrameEvent
void SetFrameEvent(std::function< void()> handler)
Sets a function to be called before update check.
Definition: ComponentStack.h:413
Gorgon::Graphics::AnimationProvider::CreateAnimation
virtual Animation & CreateAnimation(Gorgon::Animation::ControllerBase &timer) const override=0
This function should create a new animation with the given controller and if owner parameter is set t...
Gorgon::Widgets::Button::PressEvent
Event< Button > PressEvent
Definition: Button.h:135
Gorgon::Widgets::Button::GetIcon
const Graphics::Drawable & GetIcon() const
Returns the icon on the button.
Definition: Button.h:100
Gorgon::Graphics::Bitmap
This object contains an bitmap image.
Definition: Bitmap.h:25
Gorgon::Widgets::Button::Button
Button(const char *text, F_ clickfn, Registry::TemplateType type=Registry::Button_Regular)
Definition: Button.h:43
Gorgon::UI::ComponentStack::RemoveFrameEvent
void RemoveFrameEvent()
Removes the function that will be called before update check.
Definition: ComponentStack.h:433
Gorgon::UI::ComponentStack::AddCondition
void AddCondition(ComponentCondition condition, bool transition=true)
Adds a condition and its associated components to the stack.
Definition: ComponentStack.h:48
Registry.h
Gorgon::Widgets::Button::~Button
virtual ~Button()
Definition: Button.cpp:57
Gorgon::UI::ComponentStack::SetClickEvent
void SetClickEvent(std::function< void(ComponentTemplate::Tag, Geometry::Point, Input::Mouse::Button)> handler)
Sets the mouse down event.
Definition: ComponentStack.h:490
Gorgon::UI::Active
@ Active
This is for widgets that can be activated, like a count down timer.
Definition: Template.h:245
Gorgon::Graphics::Animation
A regular drawable animation.
Definition: Animations.h:14
Gorgon::Widgets::Button::SetText
void SetText(const std::string &value)
Changes the text displayed on the button.
Definition: Button.cpp:68
Gorgon::Widgets::Button::Icon
ObjectProperty< Button, const Graphics::Drawable, &Button::GetIcon, &Button::SetIcon > Icon
Definition: Button.h:132
Gorgon::UI::Graphics
@ Graphics
Definition: Template.h:164
Gorgon::Widgets::Button::Button
Button(const UI::Template &temp, F_ clickfn)
Definition: Button.h:53
Gorgon::Widgets::Button
Definition: Button.h:11
Gorgon::Widgets::Button::DeactivateClickRepeat
void DeactivateClickRepeat()
Deactivates click repeat.
Definition: Button.cpp:209
Gorgon::Graphics::Drawable
Represents a drawable object, that can be drawn to the given point.
Definition: Drawables.h:17
Gorgon::Widgets::Button::ClickEvent
Event< Button > ClickEvent
Definition: Button.h:134
Gorgon::Widgets::Button::SetIconProvider
void SetIconProvider(const Graphics::AnimationProvider &value)
Changes the icon on the button.
Definition: Button.cpp:96
Gorgon::UI::WidgetContainer::AllowAll
@ AllowAll
All widgets that can be focused are allowed to receive focus, including buttons but not labels.
Definition: WidgetContainer.h:48
Gorgon::Widgets::Button::ActivateClickRepeat
void ActivateClickRepeat(int delay=500, int repeat=400, int accelerationtime=2000, int minrepeat=200)
Activates click repeat.
Definition: Button.cpp:198
Gorgon::Widgets::Button::ReleaseEvent
Event< Button > ReleaseEvent
Definition: Button.h:136
Gorgon::UI::ComponentStack::HandleMouse
void HandleMouse(Input::Mouse::Button accepted=Input::Mouse::Button::All)
This function instructs stack to handle mouse to automatically change hover/down states,...
Definition: ComponentStack.cpp:1023
Gorgon::UI::ComponentStack::SetMouseDownEvent
void SetMouseDownEvent(std::function< void(ComponentTemplate::Tag, Geometry::Point, Input::Mouse::Button)> handler)
Sets the mouse down event.
Definition: ComponentStack.h:465
Gorgon::Widgets::Button::Button
Button(std::string text, F_ clickfn, Registry::TemplateType type=Registry::Button_Regular)
Definition: Button.h:37
Gorgon::UI::ComponentStackWidget
This class acts as a widget base that uses component stack to handle rendering, resizing and other op...
Definition: ComponentStackWidget.h:14
Gorgon::Widgets::Button::allowfocus
virtual bool allowfocus() const override
Should return true if the widget can be focused.
Definition: Button.cpp:143
Gorgon::Input::Keyboard::Keycodes::Left
constexpr Key Left
Definition: Keyboard.h:62
Gorgon::Widgets::Registry
This class stores templates for elements.
Definition: Registry.h:12
Gorgon::Widgets::Button::OwnIcon
void OwnIcon()
Transfers the ownership of the current icon.
Definition: Button.cpp:122
Gorgon::Input::Keyboard::Keycodes::Down
constexpr Key Down
Definition: Keyboard.h:65
Gorgon::UI::ComponentStack::SetMouseUpEvent
void SetMouseUpEvent(std::function< void(ComponentTemplate::Tag, Geometry::Point, Input::Mouse::Button)> handler)
Sets the mouse up event.
Definition: ComponentStack.h:478