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