FifeGUI 0.3.0
A C++ GUI library designed for games.
piegraph.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5// Corresponding header include
6#include <fifechan/widgets/piegraph.hpp>
7
8// Standard library includes
9#include <cassert>
10#include <utility>
11#include <vector>
12
13// Project headers (subdirs before local)
14#include "fifechan/exception.hpp"
15#include "fifechan/graphics.hpp"
16
17namespace fcn
18{
19
21 {
22 }
23
24 PieGraph::PieGraph(Point center) : m_opaque(false), m_center(std::move(center)), m_radius(10)
25 {
26 }
27
29 {
30 m_center.x = x;
31 }
32
34 {
35 m_center.y = y;
36 }
37
38 void PieGraph::setCenter(int x, int y)
39 {
40 m_center.x = x;
41 m_center.y = y;
42 }
43
45 {
46 return m_center.x;
47 }
48
50 {
51 return m_center.y;
52 }
53
54 void PieGraph::setCenter(Point const & center)
55 {
56 m_center = center;
57 }
58
59 Point const & PieGraph::getCenter() const
60 {
61 return m_center;
62 }
63
64 void PieGraph::setRadius(int radius)
65 {
66 m_radius = radius;
67 }
68
70 {
71 return m_radius;
72 }
73
74 void PieGraph::addSegment(int startAngle, int stopAngle, Color const & color)
75 {
76 PieGraphSegment segment;
77 segment.startAngle = startAngle;
78 segment.stopAngle = stopAngle;
79 segment.color = color;
80 m_segments.push_back(segment);
81 }
82
84 {
85 m_segments.clear();
86 }
87
88 void PieGraph::setOpaque(bool opaque)
89 {
90 m_opaque = opaque;
91 }
92
93 bool PieGraph::isOpaque() const
94 {
95 return m_opaque;
96 }
97
98 void PieGraph::draw(Graphics* graphics)
99 {
100 assert("graphics must not be null" && graphics != nullptr);
101 bool const active = isFocused();
102
103 if (isOpaque()) {
104 // Fill the background around the content
105 if (active &&
106 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
107 graphics->setColor(getSelectionColor());
108 } else {
109 graphics->setColor(getBackgroundColor());
110 }
111 graphics->fillRectangle(
114 getWidth() - (2 * getBorderSize()),
115 getHeight() - (2 * getBorderSize()));
116 }
117 // draw border or frame
118 if (getBorderSize() > 0) {
119 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
120 drawSelectionFrame(graphics);
121 } else {
122 drawBorder(graphics);
123 }
124 }
125
126 if (m_segments.empty() || m_radius < 1) {
127 return;
128 }
129 // draw circle segments
130 auto it = m_segments.begin();
131 for (; it != m_segments.end(); ++it) {
132 graphics->setColor((*it).color);
133 graphics->drawFillCircleSegment(m_center, m_radius, (*it).startAngle, (*it).stopAngle);
134 }
135 }
136
137}; // namespace fcn
Color.
Definition color.hpp:58
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void drawFillCircleSegment(Point const &p, unsigned int radius, int sangle, int eangle)=0
Draws a filled circle segment.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
Point m_center
The center point of the pie graph.
Definition piegraph.hpp:150
int getCenterX() const
Get the center x-coordinate.
Definition piegraph.cpp:44
bool m_opaque
True if the pie graph is drawn opaque.
Definition piegraph.hpp:147
PieGraph()
Default constructor.
Definition piegraph.cpp:20
void addSegment(int startAngle, int stopAngle, Color const &color)
Add a segment to the pie graph.
Definition piegraph.cpp:74
void setRadius(int radius)
Set the radius of the pie graph.
Definition piegraph.cpp:64
void setCenterX(int x)
Set the center x-coordinate.
Definition piegraph.cpp:28
int getRadius() const
Get the radius of the pie graph.
Definition piegraph.cpp:69
int getCenterY() const
Get the center y-coordinate.
Definition piegraph.cpp:49
int m_radius
The radius of the pie graph in pixels.
Definition piegraph.hpp:153
Point const & getCenter() const
Get the center point.
Definition piegraph.cpp:59
bool isOpaque() const
Definition piegraph.cpp:93
std::vector< PieGraphSegment > m_segments
The list of segments that make up the pie graph.
Definition piegraph.hpp:172
void clearSegments()
Remove all segments from the pie graph.
Definition piegraph.cpp:83
void draw(Graphics *graphics) override
Draws this widget.
Definition piegraph.cpp:98
void setCenterY(int y)
Set the center y-coordinate.
Definition piegraph.cpp:33
void setOpaque(bool opaque)
Sets the opacity of the graph.
Definition piegraph.cpp:88
void setCenter(int x, int y)
Set the center coordinates.
Definition piegraph.cpp:38
Represents a 2D coordinate (X, Y).
Definition point.hpp:34
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
virtual bool isFocused() const
Checks if the widget is focused.
Definition widget.cpp:624
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:762
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:156
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:474
virtual void drawSelectionFrame(Graphics *graphics)
Called when a widget is "active" and the selection mode is Frame or FrameWithBackground.
Definition widget.cpp:217
SelectionMode getSelectionMode() const
Gets the selection mode.
Definition widget.cpp:802
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:265
Color const & getSelectionColor() const
Gets the selection color.
Definition widget.cpp:772
Used replacement tokens by configure_file():
Represents a single segment of a pie graph (angles and color).
Definition piegraph.hpp:160
int stopAngle
Stop angle in degrees.
Definition piegraph.hpp:165
Color color
Color of the segment.
Definition piegraph.hpp:168
int startAngle
Start angle in degrees.
Definition piegraph.hpp:162