Gamedev Framework (gf)  0.8.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 "Shapes.h"
25 #include "Vector.h"
26 #include "Widget.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
33  struct RenderStates;
34  class RenderTarget;
35 
36  class Text;
37  class Sprite;
38 
45  class GF_API TextWidget : public Widget {
46  public:
47 
53  TextWidget(Text &text);
54 
55  virtual void render(RenderTarget &target, const RenderStates &states) override;
56 
57  virtual bool contains(Vector2f coords) override;
58 
64  void setTextOutlineThickness(float thickness);
65 
71  void setDisabledTextColor(const Color4f &color);
72 
78  void setDisabledTextOutlineColor(const Color4f &color);
79 
85  void setDefaultTextColor(const Color4f &color);
86 
92  void setDefaultTextOutlineColor(const Color4f &color);
93 
99  void setSelectedTextColor(const Color4f &color);
100 
106  void setSelectedTextOutlineColor(const Color4f &color);
107 
108  protected:
110  return *m_text;
111  }
112 
113  private:
114  Text *m_text;
115  float m_textOutlineThickness;
116 
117  Color4f m_disabledTextColor;
118  Color4f m_disabledTextOutlineColor;
119 
120  Color4f m_defaultTextColor;
121  Color4f m_defaultTextOutlineColor;
122 
123  Color4f m_selectedTextColor;
124  Color4f m_selectedTextOutlineColor;
125  };
126 
133  class GF_API TextShapeWidget : public TextWidget {
134  public:
141  TextShapeWidget(Text &text, Shape& shape);
142 
143  virtual void render(RenderTarget &target, const RenderStates &states) override;
144 
145  virtual bool contains(Vector2f coords) override;
146 
152  void setBackgroundOutlineThickness(float thickness);
153 
159  void setDisabledBackgroundColor(const Color4f &color);
160 
166  void setDisabledBackgroundOutlineColor(const Color4f &color);
167 
173  void setDefaultBackgroundColor(const Color4f &color);
174 
180  void setDefaultBackgroundOutlineColor(const Color4f &color);
181 
187  void setSelectedBackgroundColor(const Color4f &color);
188 
194  void setSelectedBackgroundOutlineColor(const Color4f &color);
195 
196  private:
197  Shape *m_shape;
198  float m_shapeOutlineThickness;
199 
200  Color4f m_disabledBackgroundColor;
201  Color4f m_disabledBackgroundOutlineColor;
202 
203  Color4f m_defaultBackgroundColor;
204  Color4f m_defaultBackgroundOutlineColor;
205 
206  Color4f m_selectedBackgroundColor;
207  Color4f m_selectedBackgroundOutlineColor;
208  };
209 
216  class GF_API TextButtonWidget : public TextShapeWidget {
217  public:
221  TextButtonWidget(Text& text);
222 
228  void setRadius(float radius) {
229  m_radius = radius;
230  updateGeometry();
231  }
232 
238  void setPadding(float padding) {
239  m_padding = padding;
240  updateGeometry();
241  }
242 
243  private:
244  void updateGeometry();
245 
246  private:
248  float m_radius;
249  float m_padding;
250  };
251 
256  class GF_API SpriteWidget : public Widget {
257  public:
263  SpriteWidget(Sprite& sprite);
264 
271  SpriteWidget(Sprite& defaultSprite, Sprite& selectedSprite);
272 
280  SpriteWidget(Sprite& defaultSprite, Sprite& selectedSprite, Sprite& disabledSprite);
281 
287  void setDisabledSprite(Sprite& sprite);
288 
294  void setDefaultSprite(Sprite& sprite);
295 
301  void setSelectedSprite(Sprite& sprite);
302 
303  virtual void render(RenderTarget &target, const RenderStates &states) override;
304 
305  virtual bool contains(Vector2f coords) override;
306 
307  private:
308  Sprite *m_disabledSprite;
309  Sprite *m_defaultSprite;
310  Sprite *m_selectedSprite;
311  };
312 
319  class GF_API ChoiceSpriteWidget : public SpriteWidget {
320  public:
327  ChoiceSpriteWidget(Sprite& empty, Sprite& chosen);
328 
336  void setChosen(bool chosen = true);
337 
343  bool isChosen() const noexcept {
344  return m_isChosen;
345  }
346 
354  void setEmptySprite(Sprite& sprite);
355 
363  void setChosenSprite(Sprite& sprite);
364 
365  protected:
366  virtual void triggered() override;
367 
368  private:
369  void updateSprites();
370 
371  private:
372  Sprite *m_empty;
373  Sprite *m_chosen;
374  bool m_isChosen;
375  };
376 
377 #ifndef DOXYGEN_SHOULD_SKIP_THIS
378 }
379 #endif
380 }
381 
382 #endif // GF_WIDGETS_H
Base class for textured shapes with outline.
Definition: Shape.h:73
A text within a representative shape widget.
Definition: Widgets.h:133
A simple text widget.
Definition: Widgets.h:45
bool isChosen() const noexcept
Check if the widget is in the chosen state.
Definition: Widgets.h:343
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:228
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Specialized shape representing a rounded rectangle.
Definition: Shapes.h:395
Text & getText()
Definition: Widgets.h:109
A text within a rounded rectangle widget.
Definition: Widgets.h:216
A widget with a set of sprites.
Definition: Widgets.h:256
A drawable representation of a texture, with its own transformations, color, etc. ...
Definition: Sprite.h:87
The widgets abstract class.
Definition: Widget.h:52
Graphical text that can be drawn to a render target.
Definition: Text.h:91
void setPadding(float padding)
Set the padding around the text.
Definition: Widgets.h:238
The namespace for gf classes.
Definition: Action.h:34
A choice sprite widget.
Definition: Widgets.h:319