Nymph Game Engine
Chaiscript based Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
element.h
Go to the documentation of this file.
1 #ifndef ELEMENT_H
2 #define ELEMENT_H
3 
4 #include <memory>
5 #include <glm/glm.hpp>
6 #include "graphics/ui/skin.h"
7 #include "events/event.h"
8 #include "graphics/renderable.h"
9 //= SCRIPTABLE
10 //= SCRIPTABLE BASES Renderable
11 
12 namespace Graphics {
13  namespace UI {
14  class Element : public Renderable, virtual public el::Loggable {
15  private:
16  std::shared_ptr<Skin> skin;
17  glm::vec4 color;
18  glm::vec2 anchor_point;
19  float width;
20  float height;
21  float text_padding;
22  bool cursor_within;
23  glm::dvec2 last_mouse_scroll_position;
24  unsigned int layer;
25  protected:
26  static std::vector<glm::vec3> generateRect(float screen_width, float screen_height, float x_pos, float y_pos, float width, float height) noexcept;
27  static std::vector<glm::vec2> basisTexCoords() noexcept;
28 
29  public:
36  Element(VertexData vertex_data, std::shared_ptr<Skin> skin, const unsigned int layer);
40  virtual ~Element();
41 
42  //= BEGIN SCRIPTABLE
43 
49  std::shared_ptr<Skin> getSkin() const noexcept;
55  void setSkin(std::shared_ptr<Skin> skin) noexcept;
61  glm::vec2 getAnchorPoint() const noexcept;
67  void setAnchorPoint(const glm::vec2 anchor_point);
73  float getWidth() const noexcept;
79  float getHeight() const noexcept;
85  void setWidth(const float width) noexcept;
91  void setHeight(const float height) noexcept;
97  float getTextPadding() const noexcept;
103  void setTextPadding(const float text_padding) noexcept;
109  glm::vec4 getColor() const noexcept;
115  void setColor(const glm::vec4 color) noexcept;
116 
117  virtual std::string to_string() const noexcept override;
118 
126  bool isPointWithin(glm::vec2 point) noexcept;
127  virtual std::string className() const noexcept override;
128  //= END SCRIPTABLE
129 
130  virtual void onDestroy() override;
131  virtual void onStart() override;
132  virtual bool onUpdate(const double delta) override;
133 
134  void handleQueuedEvent(std::shared_ptr<Events::Event> event) override;
135  void onNotifyNow(std::shared_ptr<Events::Event> event) override;
136  virtual unsigned long long getValueForSorting() const noexcept override;
137 
141  virtual void onLeftClick() = 0;
145  virtual void onRightClick() = 0;
149  virtual void onLeftClickRelease() = 0;
153  virtual void onRightClickRelease() = 0;
157  virtual void onCursorEnter() = 0;
161  virtual void onCursorLeave() = 0;
167  virtual void onKeyDown(const int key) = 0;
173  virtual void onKeyUp(const int key) = 0;
179  virtual void onKeyRepeat(const int key) = 0;
185  virtual void onScroll(const glm::dvec2 position_change) = 0;
186  virtual void log(el::base::type::ostream_t& os) const override;
187  };
188  }
189 }
190 #endif
void setSkin(std::shared_ptr< Skin > skin) noexcept
Sets the UI skin.
Definition: element.cpp:56
void handleQueuedEvent(std::shared_ptr< Events::Event > event) override
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Definition: element.cpp:137
float getHeight() const noexcept
Gets the height.
Definition: element.cpp:79
virtual void onLeftClickRelease()=0
Called when element has left click released over it.
virtual std::string className() const noexceptoverride
Returns a string representing the class name.
Definition: element.cpp:213
virtual void onCursorEnter()=0
Called when the mosue cursor goes over the element.
virtual void onDestroy() override
Called when the engine is shutting down.
Definition: element.cpp:127
glm::vec2 getAnchorPoint() const noexcept
Gets the anchor point.
Definition: element.cpp:60
virtual void onKeyDown(const int key)=0
Called when a key is pressed while element is in focus.
virtual void onRightClickRelease()=0
Called when element has right click released over it.
float getWidth() const noexcept
Gets the width.
Definition: element.cpp:75
virtual void onStart() override
Called when the engine starts and when a new scene is loaded.
Definition: element.cpp:130
Class for renderable.
Definition: renderable.h:22
bool isPointWithin(glm::vec2 point) noexcept
Determines if point is within element.
Definition: element.cpp:111
float getTextPadding() const noexcept
Gets the text padding.
Definition: element.cpp:91
virtual void log(el::base::type::ostream_t &os) const override
Definition: element.cpp:225
std::shared_ptr< Skin > getSkin() const noexcept
Gets the UI skin.
Definition: element.cpp:52
Class for vertex data.
Definition: vertex_data.h:17
Element(VertexData vertex_data, std::shared_ptr< Skin > skin, const unsigned int layer)
Element constructor.
Definition: element.cpp:16
static std::vector< glm::vec3 > generateRect(float screen_width, float screen_height, float x_pos, float y_pos, float width, float height) noexcept
Definition: element.cpp:26
void setColor(const glm::vec4 color) noexcept
Sets the color.
Definition: element.cpp:103
void setAnchorPoint(const glm::vec2 anchor_point)
Sets the anchor point.
Definition: element.cpp:64
void onNotifyNow(std::shared_ptr< Events::Event > event) override
When receiving an event that is immediate, onNotifyNow is used. It acts as an interrupt to make sure ...
Definition: element.cpp:205
virtual void onRightClick()=0
Called when element is right clicked.
virtual void onKeyUp(const int key)=0
Called when a key is released while element is in focus.
virtual void onLeftClick()=0
Called when element is left clicked.
void setHeight(const float height) noexcept
Sets the height.
Definition: element.cpp:87
virtual std::string to_string() const noexceptoverride
Returns a string representation of the object.
Definition: element.cpp:217
virtual bool onUpdate(const double delta) override
Called every engine loop.
Definition: element.cpp:133
glm::vec4 getColor() const noexcept
Gets the color.
Definition: element.cpp:99
virtual void onKeyRepeat(const int key)=0
Called when a key is repeated while element is in focus.
void setWidth(const float width) noexcept
Sets the width.
Definition: element.cpp:83
static std::vector< glm::vec2 > basisTexCoords() noexcept
Definition: element.cpp:39
virtual unsigned long long getValueForSorting() const noexceptoverride
Gets the value for sorting.
Definition: element.cpp:209
virtual void onCursorLeave()=0
Called when the mouse cursor goes off the element.
Definition: element.h:14
virtual ~Element()
Destroys element.
Definition: element.cpp:22
virtual void onScroll(const glm::dvec2 position_change)=0
Called when the mouse wheel is scrolled while element is in focus.
void setTextPadding(const float text_padding) noexcept
Sets the text padding.
Definition: element.cpp:95