Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Widgets.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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 "GraphicsApi.h"
28 #include "Shapes.h"
29 #include "Vector.h"
30 #include "VertexArray.h"
31 #include "Widget.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  struct RenderStates;
39  class RenderTarget;
40 
47  class GF_GRAPHICS_API TextWidget : public Widget {
48  public:
56  TextWidget(std::string text, Font& font, unsigned characterSize = 30);
57 
58  void draw(RenderTarget &target, const RenderStates& states) override;
59 
60  bool contains(Vector2f coords) override;
61 
70  void setString(std::string string);
71 
81  const std::string& getString() const;
82 
91  void setAlignment(Alignment align);
92 
99  Alignment getAlignment() const;
100 
109  void setParagraphWidth(float paragraphWidth);
110 
117  float getParagraphWidth() const;
118 
131  void setLineSpacing(float spacingFactor);
132 
140  float getLineSpacing() const;
141 
158  void setLetterSpacing(float spacingFactor);
159 
167  float getLetterSpacing() const;
168 
174  void setCharacterSize(unsigned characterSize);
175 
183  unsigned getCharacterSize() const;
184 
190  void setTextOutlineThickness(float thickness);
191 
197  void setDisabledTextColor(const Color4f &color);
198 
204  void setDisabledTextOutlineColor(const Color4f &color);
205 
211  void setDefaultTextColor(const Color4f &color);
212 
218  void setDefaultTextOutlineColor(const Color4f &color);
219 
225  void setSelectedTextColor(const Color4f &color);
226 
232  void setSelectedTextOutlineColor(const Color4f &color);
233 
234 
247  return m_basic.getLocalBounds();
248  }
249 
260  void setAnchor(Anchor anchor);
261 
262  protected:
263  void updateCurrentStateColors();
264  void updateColors(Color4f textColor, Color4f outlineColor);
265  void updateGeometry();
266 
267  void onStateChanged() override;
268 
270  return m_basic;
271  }
272 
273  private:
274  BasicText m_basic;
275  VertexArray m_vertices;
276  VertexArray m_outlineVertices;
277 
278  Color4f m_disabledTextColor;
279  Color4f m_disabledTextOutlineColor;
280 
281  Color4f m_defaultTextColor;
282  Color4f m_defaultTextOutlineColor;
283 
284  Color4f m_selectedTextColor;
285  Color4f m_selectedTextOutlineColor;
286  };
287 
294  class GF_GRAPHICS_API TextButtonWidget : public TextWidget {
295  public:
303  TextButtonWidget(std::string text, Font& font, unsigned characterSize = 30);
304 
305  void draw(RenderTarget &target, const RenderStates& states) override;
306 
307  bool contains(Vector2f coords) override;
308 
314  void setBackgroundOutlineThickness(float thickness);
315 
321  void setDisabledBackgroundColor(const Color4f &color);
322 
328  void setDisabledBackgroundOutlineColor(const Color4f &color);
329 
335  void setDefaultBackgroundColor(const Color4f &color);
336 
342  void setDefaultBackgroundOutlineColor(const Color4f &color);
343 
349  void setSelectedBackgroundColor(const Color4f &color);
350 
356  void setSelectedBackgroundOutlineColor(const Color4f &color);
357 
363  void setRadius(float radius) {
364  m_radius = radius;
365  updateGeometry();
366  }
367 
373  void setPadding(float padding) {
374  m_padding = padding;
375  updateGeometry();
376  }
377 
378  protected:
379  void updateGeometry();
380 
381  void onStateChanged() override;
382 
383  private:
385 
386  float m_backgroundOutlineThickness;
387 
388  Color4f m_disabledBackgroundColor;
389  Color4f m_disabledBackgroundOutlineColor;
390 
391  Color4f m_defaultBackgroundColor;
392  Color4f m_defaultBackgroundOutlineColor;
393 
394  Color4f m_selectedBackgroundColor;
395  Color4f m_selectedBackgroundOutlineColor;
396 
397  float m_radius;
398  float m_padding;
399  };
400 
405  class GF_GRAPHICS_API SpriteWidget : public Widget {
406  public:
410  SpriteWidget() = default;
411 
420  SpriteWidget(const Texture& texture, const RectF& defaultRect, const RectF& selectedRect, const RectF& disabledRect);
421 
429  SpriteWidget(const Texture& defaultTexture, const Texture& selectedTexture, const Texture& disabledTexture);
430 
431  void draw(RenderTarget &target, const RenderStates& states) override;
432 
433  bool contains(Vector2f coords) override;
434 
441  void setDisabledSprite(const Texture& texture, const RectF& textureRect);
442 
447  void unsetDisabledSprite();
448 
455  void setDefaultSprite(const Texture& texture, const RectF& textureRect);
456 
461  void unsetDefaultSprite();
462 
469  void setSelectedSprite(const Texture& texture, const RectF& textureRect);
470 
475  void unsetSelectedSprite();
476 
489  return getSprite().getLocalBounds();
490  }
491 
502  void setAnchor(Anchor anchor);
503 
504  private:
505  void updateGeometry();
506 
507  void onStateChanged() override;
508 
509  private:
510  BasicSprite& getSprite();
511  const BasicSprite& getSprite() const;
512 
513  private:
514  BasicSprite m_disabledSprite;
515  BasicSprite m_defaultSprite;
516  BasicSprite m_selectedSprite;
517  Vertex m_vertices[4];
518  };
519 
526  class GF_GRAPHICS_API ChoiceSpriteWidget : public Widget {
527  public:
535  ChoiceSpriteWidget(const Texture& texture, const RectF& emptyRect, const RectF& chosenRect);
536 
543  ChoiceSpriteWidget(const Texture& emptyTexture, const Texture& chosenTexture);
544 
545  void draw(RenderTarget &target, const RenderStates& states) override;
546 
547  bool contains(Vector2f coords) override;
548 
556  void setChosen(bool chosen = true);
557 
563  bool isChosen() const noexcept {
564  return m_isChosen;
565  }
566 
575  void setEmptySprite(const Texture& texture, const RectF& textureRect);
576 
585  void setChosenSprite(const Texture& texture, const RectF& textureRect);
586 
587  protected:
588  void triggered() override;
589 
590  private:
591  void updateGeometry();
592 
593  private:
594  BasicSprite& getSprite();
595  const BasicSprite& getSprite() const;
596 
597  private:
598  BasicSprite m_empty;
599  BasicSprite m_chosen;
600  bool m_isChosen;
601  Vertex m_vertices[4];
602  };
603 
604 #ifndef DOXYGEN_SHOULD_SKIP_THIS
605 }
606 #endif
607 }
608 
609 #endif // GF_WIDGETS_H
A simple text widget.
Definition: Widgets.h:47
bool isChosen() const noexcept
Check if the widget is in the chosen state.
Definition: Widgets.h:563
A set of primitives.
Definition: VertexArray.h:65
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
void setRadius(float radius)
Set the radius of the corners.
Definition: Widgets.h:363
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:294
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
A basic text.
Definition: BasicText.h:50
A widget with a set of sprites.
Definition: Widgets.h:405
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:246
The widgets abstract class.
Definition: Widget.h:55
A texture for colored images.
Definition: Texture.h:313
void setPadding(float padding)
Set the padding around the text.
Definition: Widgets.h:373
Alignment
The alignement of a text.
Definition: Alignment.h:33
The namespace for gf classes.
Definition: Action.h:35
A character font.
Definition: Font.h:109
A 4D vector.
Definition: Vector.h:852
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:488
Anchor
An anchor of a box.
Definition: Anchor.h:38
General purpose math vector.
Definition: Vector.h:61
BasicText & getText()
Definition: Widgets.h:269
A basic sprite.
Definition: BasicSprite.h:46
A choice sprite widget.
Definition: Widgets.h:526