FifeGUI 0.2.0
A C++ GUI library designed for games.
pointgraph.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/pointgraph.hpp>
6
7#include <fifechan/exception.hpp>
8#include <fifechan/graphics.hpp>
9
10#include <algorithm>
11#include <utility>
12
13namespace fcn
14{
15
17
18 PointGraph::PointGraph(PointVector data) : m_opaque(false), m_thickness(1), m_data(std::move(data)) { }
19
20 void PointGraph::setPointVector(PointVector const & data)
21 {
22 m_data = data;
23 }
24
25 PointVector const & PointGraph::getPointVector() const
26 {
27 return m_data;
28 }
29
31 {
32 m_data.clear();
33 }
34
35 void PointGraph::setThickness(unsigned int thickness)
36 {
37 m_thickness = thickness;
38 }
39
40 unsigned int PointGraph::getThickness() const
41 {
42 return m_thickness;
43 }
44
45 void PointGraph::setOpaque(bool opaque)
46 {
47 m_opaque = opaque;
48 }
49
51 {
52 return m_opaque;
53 }
54
55 void PointGraph::draw(Graphics* graphics)
56 {
57 bool const active = isFocused();
58
59 if (isOpaque()) {
60 // Fill the background around the content
61 if (active &&
62 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
63 graphics->setColor(getSelectionColor());
64 } else {
65 graphics->setColor(getBackgroundColor());
66 }
67 graphics->fillRectangle(
70 getWidth() - (2 * getBorderSize()),
71 getHeight() - (2 * getBorderSize()));
72 }
73 // draw border or frame
74 if (getBorderSize() > 0) {
75 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
76 drawSelectionFrame(graphics);
77 } else {
78 drawBorder(graphics);
79 }
80 }
81
82 if (m_data.empty()) {
83 return;
84 }
85
86 // draw connected graph line first, then the points on top.
87 graphics->setColor(getBaseColor());
88
89 bool const thick = m_thickness > 1;
90
91 auto pit = m_data.begin();
92 int x1 = (*pit).x;
93 int y1 = (*pit).y;
94 ++pit;
95
96 if (thick) {
97 for (; pit != m_data.end(); ++pit) {
98 int const x2 = (*pit).x;
99 int const y2 = (*pit).y;
100 graphics->drawRoundStroke(x1, y1, x2, y2, m_thickness);
101 x1 = x2;
102 y1 = y2;
103 }
104 } else {
105 for (; pit != m_data.end(); ++pit) {
106 int const x2 = (*pit).x;
107 int const y2 = (*pit).y;
108 graphics->drawLine(x1, y1, x2, y2);
109 x1 = x2;
110 y1 = y2;
111 }
112 }
113
114 pit = m_data.begin();
115
116 unsigned int const markerRadius = std::max(4U, m_thickness);
117
118 for (; pit != m_data.end(); ++pit) {
119 graphics->drawFillCircle(*pit, markerRadius);
120 }
121 }
122
123}; // namespace fcn
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void drawFillCircle(Point const &p, unsigned int radius)=0
Draws a filled circle.
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.
virtual void drawRoundStroke(int x1, int y1, int x2, int y2, unsigned int width)
Draws a round brush stroke along the line segment.
Definition graphics.hpp:209
bool isOpaque() const
unsigned int m_thickness
Line thickness in pixels.
PointVector m_data
The point data used to draw the graph.
PointGraph()
Default constructor.
unsigned int getThickness() const
Get the thickness of the lines drawn between points.
void draw(Graphics *graphics) override
Draws this widget.
PointVector const & getPointVector() const
void setOpaque(bool opaque)
Sets the opacity of the graph.
void resetPointVector()
Resets the data to draw.
bool m_opaque
True if the graph is drawn opaque.
void setThickness(unsigned int thickness)
Set the thickness of the lines drawn between points.
void setPointVector(PointVector const &data)
Sets the data to draw.
Color const & getBaseColor() const
Gets the base color.
Definition widget.cpp:596
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