Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Widgets.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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
33namespace gf {
34#ifndef DOXYGEN_SHOULD_SKIP_THIS
35inline 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
92
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
205
211 void setDefaultTextColor(const Color4f &color);
212
219
225 void setSelectedTextColor(const Color4f &color);
226
233
234
247 return m_basic.getLocalBounds();
248 }
249
260 void setAnchor(Anchor anchor);
261
262 protected:
264 void updateColors(Color4f textColor, Color4f outlineColor);
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
322
329
336
343
350
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:
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
448
455 void setDefaultSprite(const Texture& texture, const RectF& textureRect);
456
462
469 void setSelectedSprite(const Texture& texture, const RectF& textureRect);
470
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 basic sprite.
Definition: BasicSprite.h:46
A basic text.
Definition: BasicText.h:50
A choice sprite widget.
Definition: Widgets.h:526
ChoiceSpriteWidget(const Texture &texture, const RectF &emptyRect, const RectF &chosenRect)
Constructor.
void setEmptySprite(const Texture &texture, const RectF &textureRect)
Set the sprite for the empty state.
bool contains(Vector2f coords) override
Check if the widget contains the coordinates.
void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
ChoiceSpriteWidget(const Texture &emptyTexture, const Texture &chosenTexture)
Constructor.
void setChosen(bool chosen=true)
Set the state of the widget.
bool isChosen() const noexcept
Check if the widget is in the chosen state.
Definition: Widgets.h:563
void triggered() override
Function called when the callback is triggered.
void setChosenSprite(const Texture &texture, const RectF &textureRect)
Set the sprite for the chosen state.
A character font.
Definition: Font.h:109
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Specialized shape representing a rounded rectangle.
Definition: Shapes.h:396
A widget with a set of sprites.
Definition: Widgets.h:405
void unsetDefaultSprite()
Unset the source texture of the default sprite.
bool contains(Vector2f coords) override
Check if the widget contains the coordinates.
void unsetDisabledSprite()
Unset the source texture of the disabled sprite.
SpriteWidget()=default
Constructor with no texture.
void setDisabledSprite(const Texture &texture, const RectF &textureRect)
Set the texture for disabled state.
void unsetSelectedSprite()
Unset the source texture of the selected sprite.
void setDefaultSprite(const Texture &texture, const RectF &textureRect)
Set the texture for default state.
void setSelectedSprite(const Texture &texture, const RectF &textureRect)
Set the texture for selected state.
SpriteWidget(const Texture &texture, const RectF &defaultRect, const RectF &selectedRect, const RectF &disabledRect)
Constructor with a single texture and three rectangles.
SpriteWidget(const Texture &defaultTexture, const Texture &selectedTexture, const Texture &disabledTexture)
Constructor with three full textures.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:488
void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
A text within a rounded rectangle widget.
Definition: Widgets.h:294
void setPadding(float padding)
Set the padding around the text.
Definition: Widgets.h:373
void setDefaultBackgroundOutlineColor(const Color4f &color)
Set the outline's color of the button when it's in a normal state.
void setDisabledBackgroundOutlineColor(const Color4f &color)
Set the outline's color of the button when disabled.
void onStateChanged() override
Function called when the state changes.
void setRadius(float radius)
Set the radius of the corners.
Definition: Widgets.h:363
void setBackgroundOutlineThickness(float thickness)
Set the thickness of the outline.
void setSelectedBackgroundOutlineColor(const Color4f &color)
Set the outline's color of the button when selected.
void setDefaultBackgroundColor(const Color4f &color)
Set the background's color of the button when it's in a normal state.
bool contains(Vector2f coords) override
Check if the widget contains the coordinates.
TextButtonWidget(std::string text, Font &font, unsigned characterSize=30)
Construct a text button widget.
void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
void setSelectedBackgroundColor(const Color4f &color)
Set the background's color of the button when selected.
void setDisabledBackgroundColor(const Color4f &color)
Set the background's color of the button when disabled.
A simple text widget.
Definition: Widgets.h:47
void setDefaultTextOutlineColor(const Color4f &color)
Set the outline's color of the button when it's in a normal state.
TextWidget(std::string text, Font &font, unsigned characterSize=30)
Constructor.
float getLetterSpacing() const
Get the size of the letter spacing factor.
const std::string & getString() const
Get the text's string.
void setDefaultTextColor(const Color4f &color)
Set the text's color of the button when it's in a normal state.
void onStateChanged() override
Function called when the state changes.
void updateColors(Color4f textColor, Color4f outlineColor)
void setDisabledTextColor(const Color4f &color)
Set the text's color of the button when disabled.
void setSelectedTextColor(const Color4f &color)
Set the text's color of the button when selected.
void setParagraphWidth(float paragraphWidth)
Set the paragraph width for aligned text.
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
void setCharacterSize(unsigned characterSize)
Set the character size.
float getLineSpacing() const
Get the size of the line spacing factor.
void setAlignment(Alignment align)
Set the alignement of the text.
void setLineSpacing(float spacingFactor)
Set the line spacing factor.
void setTextOutlineThickness(float thickness)
Set the thickness of the outline.
void updateCurrentStateColors()
void setString(std::string string)
Set the text's string.
float getParagraphWidth() const
Get the paragraph width.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Definition: Widgets.h:246
Alignment getAlignment() const
Get the alignment of the text.
void setSelectedTextOutlineColor(const Color4f &color)
Set the outline's color of the button when selected.
void setDisabledTextOutlineColor(const Color4f &color)
Set the outline's color of the button when disabled.
bool contains(Vector2f coords) override
Check if the widget contains the coordinates.
unsigned getCharacterSize() const
Get the character size.
void setLetterSpacing(float spacingFactor)
Set the letter spacing factor.
void updateGeometry()
void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
BasicText & getText()
Definition: Widgets.h:269
A texture for colored images.
Definition: Texture.h:313
A set of primitives.
Definition: VertexArray.h:65
The widgets abstract class.
Definition: Widget.h:55
Anchor
An anchor of a box.
Definition: Anchor.h:38
Alignment
The alignement of a text.
Definition: Alignment.h:33
The namespace for gf classes.
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75