Gamedev Framework (gf)  0.17.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:
55  TextWidget(std::string text, Font& font, unsigned characterSize = 30);
56 
57  void draw(RenderTarget &target, const RenderStates& states) override;
58 
59  bool contains(Vector2f coords) override;
60 
69  void setString(std::string string);
70 
80  const std::string& getString() const;
81 
90  void setAlignment(Alignment align);
91 
98  Alignment getAlignment() const;
99 
108  void setParagraphWidth(float paragraphWidth);
109 
116  float getParagraphWidth() const;
117 
130  void setLineSpacing(float spacingFactor);
131 
139  float getLineSpacing() const;
140 
157  void setLetterSpacing(float spacingFactor);
158 
166  float getLetterSpacing() const;
167 
173  void setCharacterSize(unsigned characterSize);
174 
182  unsigned getCharacterSize() const;
183 
189  void setTextOutlineThickness(float thickness);
190 
196  void setDisabledTextColor(const Color4f &color);
197 
203  void setDisabledTextOutlineColor(const Color4f &color);
204 
210  void setDefaultTextColor(const Color4f &color);
211 
217  void setDefaultTextOutlineColor(const Color4f &color);
218 
224  void setSelectedTextColor(const Color4f &color);
225 
231  void setSelectedTextOutlineColor(const Color4f &color);
232 
233 
246  return m_basic.getLocalBounds();
247  }
248 
259  void setAnchor(Anchor anchor);
260 
261  protected:
262  void updateCurrentStateColors();
263  void updateColors(Color4f textColor, Color4f outlineColor);
264  void updateGeometry();
265 
266  void onStateChanged() override;
267 
269  return m_basic;
270  }
271 
272  private:
273  BasicText m_basic;
274  VertexArray m_vertices;
275  VertexArray m_outlineVertices;
276 
277  Color4f m_disabledTextColor;
278  Color4f m_disabledTextOutlineColor;
279 
280  Color4f m_defaultTextColor;
281  Color4f m_defaultTextOutlineColor;
282 
283  Color4f m_selectedTextColor;
284  Color4f m_selectedTextOutlineColor;
285  };
286 
293  class GF_API TextButtonWidget : public TextWidget {
294  public:
302  TextButtonWidget(std::string text, Font& font, unsigned characterSize = 30);
303 
304  void draw(RenderTarget &target, const RenderStates& states) override;
305 
306  bool contains(Vector2f coords) override;
307 
313  void setBackgroundOutlineThickness(float thickness);
314 
320  void setDisabledBackgroundColor(const Color4f &color);
321 
327  void setDisabledBackgroundOutlineColor(const Color4f &color);
328 
334  void setDefaultBackgroundColor(const Color4f &color);
335 
341  void setDefaultBackgroundOutlineColor(const Color4f &color);
342 
348  void setSelectedBackgroundColor(const Color4f &color);
349 
355  void setSelectedBackgroundOutlineColor(const Color4f &color);
356 
362  void setRadius(float radius) {
363  m_radius = radius;
364  updateGeometry();
365  }
366 
372  void setPadding(float padding) {
373  m_padding = padding;
374  updateGeometry();
375  }
376 
377  protected:
378  void updateGeometry();
379 
380  void onStateChanged() override;
381 
382  private:
384 
385  float m_backgroundOutlineThickness;
386 
387  Color4f m_disabledBackgroundColor;
388  Color4f m_disabledBackgroundOutlineColor;
389 
390  Color4f m_defaultBackgroundColor;
391  Color4f m_defaultBackgroundOutlineColor;
392 
393  Color4f m_selectedBackgroundColor;
394  Color4f m_selectedBackgroundOutlineColor;
395 
396  float m_radius;
397  float m_padding;
398  };
399 
404  class GF_API SpriteWidget : public Widget {
405  public:
409  SpriteWidget() = default;
410 
419  SpriteWidget(const Texture& texture, const RectF& defaultRect, const RectF& selectedRect, const RectF& disabledRect);
420 
428  SpriteWidget(const Texture& defaultTexture, const Texture& selectedTexture, const Texture& disabledTexture);
429 
430  void draw(RenderTarget &target, const RenderStates& states) override;
431 
432  bool contains(Vector2f coords) override;
433 
440  void setDisabledSprite(const Texture& texture, const RectF& textureRect);
441 
446  void unsetDisabledSprite();
447 
454  void setDefaultSprite(const Texture& texture, const RectF& textureRect);
455 
460  void unsetDefaultSprite();
461 
468  void setSelectedSprite(const Texture& texture, const RectF& textureRect);
469 
474  void unsetSelectedSprite();
475 
488  return getSprite().getLocalBounds();
489  }
490 
501  void setAnchor(Anchor anchor);
502 
503  private:
504  void updateGeometry();
505 
506  void onStateChanged() override;
507 
508  private:
509  BasicSprite& getSprite();
510  const BasicSprite& getSprite() const;
511 
512  private:
513  BasicSprite m_disabledSprite;
514  BasicSprite m_defaultSprite;
515  BasicSprite m_selectedSprite;
516  Vertex m_vertices[4];
517  };
518 
525  class GF_API ChoiceSpriteWidget : public Widget {
526  public:
534  ChoiceSpriteWidget(const Texture& texture, const RectF& emptyRect, const RectF& chosenRect);
535 
542  ChoiceSpriteWidget(const Texture& emptyTexture, const Texture& chosenTexture);
543 
544  void draw(RenderTarget &target, const RenderStates& states) override;
545 
546  bool contains(Vector2f coords) override;
547 
555  void setChosen(bool chosen = true);
556 
562  bool isChosen() const noexcept {
563  return m_isChosen;
564  }
565 
574  void setEmptySprite(const Texture& texture, const RectF& textureRect);
575 
584  void setChosenSprite(const Texture& texture, const RectF& textureRect);
585 
586  protected:
587  void triggered() override;
588 
589  private:
590  void updateGeometry();
591 
592  private:
593  BasicSprite& getSprite();
594  const BasicSprite& getSprite() const;
595 
596  private:
597  BasicSprite m_empty;
598  BasicSprite m_chosen;
599  bool m_isChosen;
600  Vertex m_vertices[4];
601  };
602 
603 #ifndef DOXYGEN_SHOULD_SKIP_THIS
604 }
605 #endif
606 }
607 
608 #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:562
A set of primitives.
Definition: VertexArray.h:65
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:90
void setRadius(float radius)
Set the radius of the corners.
Definition: Widgets.h:362
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:293
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:404
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:245
The widgets abstract class.
Definition: Widget.h:54
A texture for colored images.
Definition: Texture.h:309
void setPadding(float padding)
Set the padding around the text.
Definition: Widgets.h:372
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:487
Anchor
An anchor of a box.
Definition: Anchor.h:38
Alignment
The alignement of a text.
Definition: Alignment.h:33
BasicText & getText()
Definition: Widgets.h:268
A basic sprite.
Definition: BasicSprite.h:46
A choice sprite widget.
Definition: Widgets.h:525