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