Gamedev Framework (gf)  0.10.0
A C++14 framework for 2D games
Widgets.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Lilian Franchi
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_WIDGETS_H
22 #define GF_WIDGETS_H
23 
24 #include "BasicSprite.h"
25 #include "BasicText.h"
26 #include "Shapes.h"
27 #include "Vector.h"
28 #include "VertexArray.h"
29 #include "Widget.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  struct RenderStates;
37  class RenderTarget;
38 
45  class GF_API TextWidget : public Widget {
46  public:
47 
55  TextWidget(std::string text, Font& font, unsigned characterSize = 30);
56 
57  virtual void draw(RenderTarget &target, const RenderStates& states) override;
58 
59  virtual bool contains(Vector2f coords) override;
60 
66  void setTextOutlineThickness(float thickness);
67 
73  void setDisabledTextColor(const Color4f &color);
74 
80  void setDisabledTextOutlineColor(const Color4f &color);
81 
87  void setDefaultTextColor(const Color4f &color);
88 
94  void setDefaultTextOutlineColor(const Color4f &color);
95 
101  void setSelectedTextColor(const Color4f &color);
102 
108  void setSelectedTextOutlineColor(const Color4f &color);
109 
110 
123  return m_basic.getLocalBounds();
124  }
125 
136  void setAnchor(Anchor anchor);
137 
138  protected:
139  void updateCurrentStateColors();
140  void updateColors(Color4f textColor, Color4f outlineColor);
141  void updateGeometry();
142 
143  virtual void onStateChanged() override;
144 
146  return m_basic;
147  }
148 
149  private:
150  BasicText m_basic;
151  VertexArray m_vertices;
152  VertexArray m_outlineVertices;
153 
154  Color4f m_disabledTextColor;
155  Color4f m_disabledTextOutlineColor;
156 
157  Color4f m_defaultTextColor;
158  Color4f m_defaultTextOutlineColor;
159 
160  Color4f m_selectedTextColor;
161  Color4f m_selectedTextOutlineColor;
162  };
163 
170  class GF_API TextButtonWidget : public TextWidget {
171  public:
179  TextButtonWidget(std::string text, Font& font, unsigned characterSize = 30);
180 
181  virtual void draw(RenderTarget &target, const RenderStates& states) override;
182 
183  virtual bool contains(Vector2f coords) override;
184 
190  void setBackgroundOutlineThickness(float thickness);
191 
197  void setDisabledBackgroundColor(const Color4f &color);
198 
204  void setDisabledBackgroundOutlineColor(const Color4f &color);
205 
211  void setDefaultBackgroundColor(const Color4f &color);
212 
218  void setDefaultBackgroundOutlineColor(const Color4f &color);
219 
225  void setSelectedBackgroundColor(const Color4f &color);
226 
232  void setSelectedBackgroundOutlineColor(const Color4f &color);
233 
239  void setRadius(float radius) {
240  m_radius = radius;
241  updateGeometry();
242  }
243 
249  void setPadding(float padding) {
250  m_padding = padding;
251  updateGeometry();
252  }
253 
254  protected:
255  void updateGeometry();
256 
257  virtual void onStateChanged() override;
258 
259  private:
261 
262  float m_backgroundOutlineThickness;
263 
264  Color4f m_disabledBackgroundColor;
265  Color4f m_disabledBackgroundOutlineColor;
266 
267  Color4f m_defaultBackgroundColor;
268  Color4f m_defaultBackgroundOutlineColor;
269 
270  Color4f m_selectedBackgroundColor;
271  Color4f m_selectedBackgroundOutlineColor;
272 
273  float m_radius;
274  float m_padding;
275  };
276 
281  class GF_API SpriteWidget : public Widget {
282  public:
286  SpriteWidget() = default;
287 
296  SpriteWidget(const Texture& texture, const RectF& defaultRect, const RectF& selectedRect, const RectF& disabledRect);
297 
305  SpriteWidget(const Texture& defaultTexture, const Texture& selectedTexture, const Texture& disabledTexture);
306 
307  virtual void draw(RenderTarget &target, const RenderStates& states) override;
308 
309  virtual bool contains(Vector2f coords) override;
310 
317  void setDisabledSprite(const Texture& texture, const RectF& textureRect);
318 
325  void setDefaultSprite(const Texture& texture, const RectF& textureRect);
326 
333  void setSelectedSprite(const Texture& texture, const RectF& textureRect);
334 
347  return getSprite().getLocalBounds();
348  }
349 
360  void setAnchor(Anchor anchor);
361 
362  private:
363  void updateGeometry();
364 
365  virtual void onStateChanged() override;
366 
367  private:
368  BasicSprite& getSprite();
369  const BasicSprite& getSprite() const;
370 
371  private:
372  BasicSprite m_disabledSprite;
373  BasicSprite m_defaultSprite;
374  BasicSprite m_selectedSprite;
375  Vertex m_vertices[4];
376  };
377 
384  class GF_API ChoiceSpriteWidget : public Widget {
385  public:
393  ChoiceSpriteWidget(const Texture& texture, const RectF& emptyRect, const RectF& chosenRect);
394 
401  ChoiceSpriteWidget(const Texture& emptyTexture, const Texture& chosenTexture);
402 
403  virtual void draw(RenderTarget &target, const RenderStates& states) override;
404 
405  virtual bool contains(Vector2f coords) override;
406 
414  void setChosen(bool chosen = true);
415 
421  bool isChosen() const noexcept {
422  return m_isChosen;
423  }
424 
433  void setEmptySprite(const Texture& texture, const RectF& textureRect);
434 
443  void setChosenSprite(const Texture& texture, const RectF& textureRect);
444 
445  protected:
446  virtual void triggered() override;
447 
448  private:
449  void updateGeometry();
450 
451  private:
452  BasicSprite& getSprite();
453  const BasicSprite& getSprite() const;
454 
455  private:
456  BasicSprite m_empty;
457  BasicSprite m_chosen;
458  bool m_isChosen;
459  Vertex m_vertices[4];
460  };
461 
462 #ifndef DOXYGEN_SHOULD_SKIP_THIS
463 }
464 #endif
465 }
466 
467 #endif // GF_WIDGETS_H
A simple text widget.
Definition: Widgets.h:45
bool isChosen() const noexcept
Check if the widget is in the chosen state.
Definition: Widgets.h:421
A set of primitives.
Definition: VertexArray.h:65
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
void setRadius(float radius)
Set the radius of the corners.
Definition: Widgets.h:239
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Specialized shape representing a rounded rectangle.
Definition: Shapes.h:395
A text within a rounded rectangle widget.
Definition: Widgets.h:170
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
Definition: BasicText.h:47
A widget with a set of sprites.
Definition: Widgets.h:281
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:122
The widgets abstract class.
Definition: Widget.h:53
A texture for colored images.
Definition: Texture.h:339
void setPadding(float padding)
Set the padding around the text.
Definition: Widgets.h:249
The namespace for gf classes.
Definition: Action.h:34
A character font.
Definition: Font.h:130
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:346
Anchor
An anchor of a box.
Definition: Anchor.h:38
BasicText & getText()
Definition: Widgets.h:145
Definition: BasicSprite.h:43
A choice sprite widget.
Definition: Widgets.h:384