FifeGUI 0.2.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#include <fifechan/widgets/piegraph.hpp>
6
7#include <fifechan/exception.hpp>
8#include <fifechan/graphics.hpp>
9
10#include <utility>
11#include <vector>
12
13namespace fcn
14{
15
17
18 PieGraph::PieGraph(Point center) : m_opaque(false), m_center(std::move(center)), m_radius(10) { }
19
21 {
22 m_center.x = x;
23 }
24
26 {
27 m_center.y = y;
28 }
29
30 void PieGraph::setCenter(int x, int y)
31 {
32 m_center.x = x;
33 m_center.y = y;
34 }
35
37 {
38 return m_center.x;
39 }
40
42 {
43 return m_center.y;
44 }
45
46 void PieGraph::setCenter(Point const & center)
47 {
48 m_center = center;
49 }
50
51 Point const & PieGraph::getCenter() const
52 {
53 return m_center;
54 }
55
56 void PieGraph::setRadius(int radius)
57 {
58 m_radius = radius;
59 }
60
62 {
63 return m_radius;
64 }
65
66 void PieGraph::addSegment(int startAngle, int stopAngle, Color const & color)
67 {
68 PieGraphSegment segment;
69 segment.startAngle = startAngle;
70 segment.stopAngle = stopAngle;
71 segment.color = color;
72 m_segments.push_back(segment);
73 }
74
76 {
77 m_segments.clear();
78 }
79
80 void PieGraph::setOpaque(bool opaque)
81 {
82 m_opaque = opaque;
83 }
84
85 bool PieGraph::isOpaque() const
86 {
87 return m_opaque;
88 }
89
90 void PieGraph::draw(Graphics* graphics)
91 {
92 bool const active = isFocused();
93
94 if (isOpaque()) {
95 // Fill the background around the content
96 if (active &&
97 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
98 graphics->setColor(getSelectionColor());
99 } else {
100 graphics->setColor(getBackgroundColor());
101 }
102 graphics->fillRectangle(
105 getWidth() - (2 * getBorderSize()),
106 getHeight() - (2 * getBorderSize()));
107 }
108 // draw border or frame
109 if (getBorderSize() > 0) {
110 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
111 drawSelectionFrame(graphics);
112 } else {
113 drawBorder(graphics);
114 }
115 }
116
117 if (m_segments.empty() || m_radius < 1) {
118 return;
119 }
120 // draw circle segments
121 auto it = m_segments.begin();
122 for (; it != m_segments.end(); ++it) {
123 graphics->setColor((*it).color);
124 graphics->drawFillCircleSegment(m_center, m_radius, (*it).startAngle, (*it).stopAngle);
125 }
126 }
127
128}; // namespace fcn
Color.
Definition color.hpp:56
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
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:148
int getCenterX() const
Get the center x-coordinate.
Definition piegraph.cpp:36
bool m_opaque
True if the pie graph is drawn opaque.
Definition piegraph.hpp:145
PieGraph()
Default constructor.
Definition piegraph.cpp:16
void addSegment(int startAngle, int stopAngle, Color const &color)
Add a segment to the pie graph.
Definition piegraph.cpp:66
void setRadius(int radius)
Set the radius of the pie graph.
Definition piegraph.cpp:56
void setCenterX(int x)
Set the center x-coordinate.
Definition piegraph.cpp:20
int getRadius() const
Get the radius of the pie graph.
Definition piegraph.cpp:61
int getCenterY() const
Get the center y-coordinate.
Definition piegraph.cpp:41
int m_radius
The radius of the pie graph in pixels.
Definition piegraph.hpp:151
Point const & getCenter() const
Get the center point.
Definition piegraph.cpp:51
bool isOpaque() const
Definition piegraph.cpp:85
std::vector< PieGraphSegment > m_segments
The list of segments that make up the pie graph.
Definition piegraph.hpp:170
void clearSegments()
Remove all segments from the pie graph.
Definition piegraph.cpp:75
void draw(Graphics *graphics) override
Draws this widget.
Definition piegraph.cpp:90
void setCenterY(int y)
Set the center y-coordinate.
Definition piegraph.cpp:25
void setOpaque(bool opaque)
Sets the opacity of the graph.
Definition piegraph.cpp:80
void setCenter(int x, int y)
Set the center coordinates.
Definition piegraph.cpp:30
Represents a 2D coordinate (X, Y).
Definition point.hpp:29
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:170
virtual bool isFocused() const
Checks if the widget is focused.
Definition widget.cpp:497
Color const & getBackgroundColor() const
Gets the background color.
Definition widget.cpp:616
virtual void drawBorder(Graphics *graphics)
Called when a widget have a border.
Definition widget.cpp:111
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:381
virtual void drawSelectionFrame(Graphics *graphics)
Called when a widget is "active" and the selection mode is Frame or FrameWithBackground.
Definition widget.cpp:135
SelectionMode getSelectionMode() const
Gets the selection mode.
Definition widget.cpp:656
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:183
Color const & getSelectionColor() const
Gets the selection color.
Definition widget.cpp:626
Represents a single segment of a pie graph (angles and color).
Definition piegraph.hpp:158
int stopAngle
Stop angle in degrees.
Definition piegraph.hpp:163
Color color
Color of the segment.
Definition piegraph.hpp:166
int startAngle
Start angle in degrees.
Definition piegraph.hpp:160