FifeGUI 0.3.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// Corresponding header include
6#include <fifechan/widgets/pointgraph.hpp>
7
8// Standard library includes
9#include <algorithm>
10#include <cassert>
11#include <utility>
12
13// Project headers (subdirs before local)
14#include "fifechan/exception.hpp"
15#include "fifechan/graphics.hpp"
16
17namespace fcn
18{
19
23
24 PointGraph::PointGraph(PointVector data) : m_opaque(false), m_thickness(1), m_data(std::move(data))
25 {
26 }
27
29 {
30 m_data = data;
31 }
32
34 {
35 return m_data;
36 }
37
39 {
40 m_data.clear();
41 }
42
43 void PointGraph::setThickness(unsigned int thickness)
44 {
45 m_thickness = thickness;
46 }
47
48 unsigned int PointGraph::getThickness() const
49 {
50 return m_thickness;
51 }
52
53 void PointGraph::setOpaque(bool opaque)
54 {
55 m_opaque = opaque;
56 }
57
59 {
60 return m_opaque;
61 }
62
63 void PointGraph::draw(Graphics* graphics)
64 {
65 assert("graphics must not be null" && graphics != nullptr);
66 bool const active = isFocused();
67
68 if (isOpaque()) {
69 // Fill the background around the content
70 if (active &&
71 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
72 graphics->setColor(getSelectionColor());
73 } else {
74 graphics->setColor(getBackgroundColor());
75 }
76 graphics->fillRectangle(
79 getWidth() - (2 * getBorderSize()),
80 getHeight() - (2 * getBorderSize()));
81 }
82 // draw border or frame
83 if (getBorderSize() > 0) {
84 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
85 drawSelectionFrame(graphics);
86 } else {
87 drawBorder(graphics);
88 }
89 }
90
91 if (m_data.empty()) {
92 return;
93 }
94
95 // draw connected graph line first, then the points on top.
96 graphics->setColor(getBaseColor());
97
98 bool const thick = m_thickness > 1;
99
100 auto pit = m_data.begin();
101 int x1 = (*pit).x;
102 int y1 = (*pit).y;
103 ++pit;
104
105 if (thick) {
106 for (; pit != m_data.end(); ++pit) {
107 int const x2 = (*pit).x;
108 int const y2 = (*pit).y;
109 graphics->drawRoundStroke(x1, y1, x2, y2, m_thickness);
110 x1 = x2;
111 y1 = y2;
112 }
113 } else {
114 for (; pit != m_data.end(); ++pit) {
115 int const x2 = (*pit).x;
116 int const y2 = (*pit).y;
117 graphics->drawLine(x1, y1, x2, y2);
118 x1 = x2;
119 y1 = y2;
120 }
121 }
122
123 pit = m_data.begin();
124
125 unsigned int const markerRadius = std::max(4U, m_thickness);
126
127 for (; pit != m_data.end(); ++pit) {
128 graphics->drawFillCircle(*pit, markerRadius);
129 }
130 }
131
132}; // namespace fcn
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
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:246
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:742
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():
std::vector< Point > PointVector
A list of points.
Definition point.hpp:331