FifeGUI 0.2.0
A C++ GUI library designed for games.
linegraph.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/linegraph.hpp>
6
7#include <fifechan/exception.hpp>
8#include <fifechan/graphics.hpp>
9
10#include <utility>
11
12namespace fcn
13{
14
16
17 LineGraph::LineGraph(PointVector data) : m_opaque(false), m_thickness(1), m_data(std::move(data)) { }
18
19 void LineGraph::setPointVector(PointVector const & data)
20 {
21 m_data = data;
22 }
23
24 PointVector const & LineGraph::getPointVector() const
25 {
26 return m_data;
27 }
28
30 {
31 m_data.clear();
32 }
33
34 void LineGraph::setThickness(unsigned int thickness)
35 {
36 m_thickness = thickness;
37 }
38
39 unsigned int LineGraph::getThickness() const
40 {
41 return m_thickness;
42 }
43
44 void LineGraph::setOpaque(bool opaque)
45 {
46 m_opaque = opaque;
47 }
48
50 {
51 return m_opaque;
52 }
53
54 void LineGraph::draw(Graphics* graphics)
55 {
56 bool const active = isFocused();
57
58 if (isOpaque()) {
59 // Fill the background around the content
60 if (active &&
61 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
62 graphics->setColor(getSelectionColor());
63 } else {
64 graphics->setColor(getBackgroundColor());
65 }
66 graphics->fillRectangle(
69 getWidth() - (2 * getBorderSize()),
70 getHeight() - (2 * getBorderSize()));
71 }
72 // draw border or frame
73 if (getBorderSize() > 0) {
74 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
75 drawSelectionFrame(graphics);
76 } else {
77 drawBorder(graphics);
78 }
79 }
80
81 if (m_data.empty()) {
82 return;
83 }
84 // draw lines
85 graphics->setColor(getBaseColor());
86
87 bool const thick = m_thickness > 1;
88
89 auto pit = m_data.begin();
90 int x1 = (*pit).x;
91 int y1 = (*pit).y;
92 ++pit;
93 if (thick) {
94 for (; pit != m_data.end(); ++pit) {
95 int const x2 = (*pit).x;
96 int const y2 = (*pit).y;
97 graphics->drawRoundStroke(x1, y1, x2, y2, m_thickness);
98 x1 = x2;
99 y1 = y2;
100 }
101 } else {
102 for (; pit != m_data.end(); ++pit) {
103 int const x2 = (*pit).x;
104 int const y2 = (*pit).y;
105 graphics->drawLine(x1, y1, x2, y2);
106 x1 = x2;
107 y1 = y2;
108 }
109 }
110 }
111
112}; // 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 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
LineGraph()
Default constructor.
Definition linegraph.cpp:15
bool m_opaque
True if the graph is drawn opaque.
Definition linegraph.hpp:70
PointVector const & getPointVector() const
Get the current point vector.
Definition linegraph.cpp:24
void setPointVector(PointVector const &data)
Set the raw point vector used to draw the graph.
Definition linegraph.cpp:19
bool isOpaque() const
Definition linegraph.cpp:49
void draw(Graphics *graphics) override
Draws this widget.
Definition linegraph.cpp:54
void resetPointVector()
Reset the stored data to an empty vector.
Definition linegraph.cpp:29
PointVector m_data
The point data used to draw the graph.
Definition linegraph.hpp:76
unsigned int m_thickness
Stroke thickness in pixels.
Definition linegraph.hpp:73
void setOpaque(bool opaque)
Sets the opacity of the graph.
Definition linegraph.cpp:44
unsigned int getThickness() const
Get stroke thickness in pixels.
Definition linegraph.cpp:39
void setThickness(unsigned int thickness)
Set stroke thickness in pixels.
Definition linegraph.cpp:34
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