Untitled Game engine no.5  1.0
UI.hpp
1 //
2 // Created by jibbo on 4/25/21.
3 //
4 
5 #ifndef ENGINE_PROJ_UI_HPP
6 #define ENGINE_PROJ_UI_HPP
7 
8 #include <utility>
9 
10 #include "MeshRenderer.hpp"
11 #include "Components.hpp"
12 #include "Input.hpp"
13 #include "Font.hpp"
14 #include "Jobs.hpp"
15 
16 namespace Engine {
17 
24  struct UIButton;
25 
31  UIButton * button;
32 
36  explicit ButtonHook(UIButton *button) : button(button) {};
37  void processCollision(const Ref<PhysicsEntity>& other) override;
38  };
39 
46  UIElement() = default;
47 
52  explicit UIElement(const Ref<Texture>& tex) {
53  setImage(tex);
54  }
55 
60  void setImage(const Ref<Texture>& tex) {
61  texture = tex;
62  map = tex->getDefaultMapping();
63  setSize(texture->getWidth(), texture->getHeight());
65  }
66 
71  void setImage(const Ref<TextureLookup>& lookup) {
72  texture = lookup->tex;
73  map = lookup->map;
74  setSize(lookup->width, lookup->height);
76  }
77 
78 
84  void setSize(float width, float height) {
85  this->width = width;
86  this->height = height;
88  }
89 
93  virtual void update() {
94  updateVisual();
95  };
96 
101  visualUpdate = true;
102  }
103 
107  virtual void updateVisual() {
108  if (!visualUpdate) {
109  return;
110  }
111 
112  glm::mat4 mat{1};
113  glm::vec3 pos;
114  switch (origin) {
115  case Allignment::TOP_LEFT:
116  pos = {.5f, .5f, .0f};
117  break;
118  case Allignment::TOP:
119  pos = {.0f, .5f, .0f};
120  break;
121  case Allignment::TOP_RIGHT:
122  pos = {-.5f, .5f, .0f};
123  break;
124 
125  case Allignment::LEFT:
126  pos = {.5f, .0f, .0f};
127  break;
128  case Allignment::CENTER:
129  pos = {.0f, .0f, .0f};
130  break;
131  case Allignment::RIGHT:
132  pos = {-.5f, .0f, .0f};
133  break;
134 
135  case Allignment::BOTTOM_LEFT:
136  pos = {.5f, -.5f, .0f};
137  break;
138  case Allignment::BOTTOM:
139  pos = {.0f, -.5f, .0f};
140  break;
141  case Allignment::BOTTOM_RIGHT:
142  pos = {.5f, -.5f, .0f};
143  break;
144  }
145 
146  mat *= glm::translate(pos);
147  mat *= glm::scale(glm::vec3{width, height, 0.f});
148  if (mesh == nullptr) {
149  mesh = CreateRef<Mesh>();
150  }
151 
152  mesh->verts = Mesh::quad(mat);
153 
154  if (collider != nullptr) {
155  collider->verts = Mesh::quadVerts(mat);
156  }
157 
158  visualUpdate = false;
159  }
160 
161  static void Update(const Scene& scene) {
162  for (auto e : scene.GetECS().GetEntitiesWith<UIElement>()) {
163  scene.GetECS().GetComponent<UIElement>(e)->update();
164  }
165  }
166 
167  Ref<VertexCollider2D> collider = nullptr;
168  Allignment origin = CENTER;
169  private:
170  float width = 0, height = 0;
171  float x = 0, y = 0;
172  bool visualUpdate = false;
173  };
174 
178  enum ButtonState {
179  DEFAULT, HOVER, PRESS
180  };
181 
186  struct UIText : UIElement {
187  Ref<Font> font;
188  std::string text;
189 
199  UIText(const Ref<Engine::Font> &font, const std::string& text, Allignment origin = CENTER) : font(font) {
200  this->origin = origin;
201  setText(text);
202  }
203 
204  void setText(const std::string& newString) {
205  if (text != newString) {
206  text = newString;
207  font->renderText(newString, this, origin, 0.5f);
208  }
209  }
210  };
211 
212  struct UIButton : UIElement {
213  func clickFunc;
214  ButtonState state = DEFAULT;
215  ButtonState nextState = DEFAULT;
216  std::unordered_map<ButtonState, Ref<TextureLookup>> stateLookup
217  = std::unordered_map<ButtonState, Ref<TextureLookup>>();
218 
219  UIButton() {
220  collider = CreateRef<VertexCollider2D>();
221  collider->hook = CreateRef<ButtonHook>(this);
222  }
223 
230  stateLookup[key] = texture;
231  if (key == state) {
232  setImage(stateLookup[key]);
233  }
234  }
235 
240  void setCallback(func f) {
241  clickFunc = std::move(f);
242  }
243 
247  void preformCallback() const {
248  clickFunc();
249  }
250 
255  void setState(ButtonState next) {
256  this->nextState = next;
257  if (this->state == next) {
258  return;
259  }
260  this->state = next;
261  setImage(stateLookup[next]);
262  }
263 
264 
265  void updateVisual() override {
266  if (nextState != state) {
269  }
271  }
272 
273  void update() override {
275  nextState = DEFAULT;
276  }
277  };
278 }
279 
280 #endif //ENGINE_PROJ_UI_HPP
Engine::UIButton::state
ButtonState state
generic function for processing clicks
Definition: UI.hpp:214
Engine::MeshRenderer
Scene Component for passing drawing data to the renderer. Stores vertex data and references to the te...
Definition: MeshRenderer.hpp:15
Engine::Scene
A container for entities currently in the Scene.
Definition: Scene.hpp:9
Engine::UIElement
Definition: UI.hpp:45
Engine::ButtonHook::ButtonHook
ButtonHook(UIButton *button)
button reference TODO: make smart pointer
Definition: UI.hpp:36
Engine::Ref
std::shared_ptr< T > Ref
Has stuff for making references a lot more easily shared smart pointer.
Definition: Base.hpp:21
Engine::UIButton::stateLookup
std::unordered_map< ButtonState, Ref< TextureLookup > > stateLookup
next hover state to reduce redraw calls
Definition: UI.hpp:217
Engine::UIElement::setSize
void setSize(float width, float height)
Definition: UI.hpp:84
Engine::MeshRenderer::map
Ref< TextureMap > map
Definition: MeshRenderer.hpp:24
Engine::MeshRenderer::texture
Ref< Texture > texture
Definition: MeshRenderer.hpp:29
Engine::ECS::GetEntitiesWith
std::vector< EntityID > GetEntitiesWith()
Definition: ECS.hpp:188
Engine::CollisionHook
Wrapper for processing collisions between physics entities.
Definition: Components.hpp:23
Engine::ButtonState
ButtonState
Definition: UI.hpp:178
Engine::UIElement::updateVisual
virtual void updateVisual()
Definition: UI.hpp:107
Engine::UIButton::loadTexture
void loadTexture(const ButtonState key, const Ref< TextureLookup > &texture)
Definition: UI.hpp:229
Engine::Allignment
Allignment
enumeration representing alignment of text in a string of text.
Definition: Components.hpp:358
Engine::UIElement::setImage
void setImage(const Ref< Texture > &tex)
Definition: UI.hpp:60
Engine::UIButton::setState
void setState(ButtonState next)
Definition: UI.hpp:255
Engine::UIElement::origin
Allignment origin
collider reference (generated when setting a picture)
Definition: UI.hpp:168
Engine::ButtonHook
Definition: UI.hpp:30
Engine::UIButton::nextState
ButtonState nextState
hover state
Definition: UI.hpp:215
Engine::UIElement::update
virtual void update()
Definition: UI.hpp:93
Engine::UIButton::preformCallback
void preformCallback() const
Definition: UI.hpp:247
Engine::ECS::GetComponent
Ref< C > GetComponent(const EntityID &id)
Definition: ECS.hpp:80
Engine::UIButton::updateVisual
void updateVisual() override
Definition: UI.hpp:265
Engine
Definition: Animation.hpp:14
Engine::UIElement::queueVisualUpdate
void queueVisualUpdate()
Definition: UI.hpp:100
Engine::UIButton::update
void update() override
Definition: UI.hpp:273
Engine::UIText
Definition: UI.hpp:186
Engine::MeshRenderer::mesh
Ref< Mesh > mesh
Definition: MeshRenderer.hpp:40
Engine::func
std::function< void(void)> func
Alias for any kind of function pointer.
Definition: Jobs.hpp:16
Engine::UIElement::UIElement
UIElement(const Ref< Texture > &tex)
Definition: UI.hpp:52
Engine::UIElement::setImage
void setImage(const Ref< TextureLookup > &lookup)
Definition: UI.hpp:71
Engine::UIButton
Definition: UI.hpp:212
Engine::UIButton::setCallback
void setCallback(func f)
Definition: UI.hpp:240
Engine::UIText::UIText
UIText(const Ref< Engine::Font > &font, const std::string &text, Allignment origin=CENTER)
Definition: UI.hpp:199