FifeGUI 0.3.0
A C++ GUI library designed for games.
bargraph.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/bargraph.hpp>
7
8// Project headers (subdirs before local)
9#include "fifechan/exception.hpp"
10#include "fifechan/graphics.hpp"
11
12namespace fcn
13{
14
15 BarGraph::BarGraph() = default;
16
17 BarGraph::BarGraph(int x, int y, int w, int h) : m_rec(Rectangle(x, y, w, h))
18 {
19 }
20
22 {
23 m_rec.x = x;
24 }
25
27 {
28 return m_rec.x;
29 }
30
32 {
33 m_rec.y = y;
34 }
35
37 {
38 return m_rec.y;
39 }
40
41 void BarGraph::setBarPosition(int x, int y)
42 {
43 m_rec.x = x;
44 m_rec.y = y;
45 }
46
48 {
49 m_rec.x = pos.x;
50 m_rec.y = pos.y;
51 }
52
54 {
55 m_rec.width = w;
56 }
57
59 {
60 return m_rec.width;
61 }
62
64 {
65 m_rec.height = h;
66 }
67
69 {
70 return m_rec.height;
71 }
72
73 void BarGraph::setBarSize(int w, int h)
74 {
75 m_rec.width = w;
76 m_rec.height = h;
77 }
78
79 void BarGraph::setOpaque(bool opaque)
80 {
81 m_opaque = opaque;
82 }
83
84 bool BarGraph::isOpaque() const
85 {
86 return m_opaque;
87 }
88
89 void BarGraph::draw(Graphics* graphics)
90 {
91 bool const active = isFocused();
92
93 if (isOpaque()) {
94 // Fill the background around the content
95 if (active &&
96 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
97 graphics->setColor(getSelectionColor());
98 } else {
99 graphics->setColor(getBackgroundColor());
100 }
101 graphics->fillRectangle(
104 getWidth() - (2 * getBorderSize()),
105 getHeight() - (2 * getBorderSize()));
106 }
107 // draw border or frame
108 if (getBorderSize() > 0) {
109 if (active && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
110 drawSelectionFrame(graphics);
111 } else {
112 drawBorder(graphics);
113 }
114 }
115
116 // draw bar
117 graphics->setColor(getBaseColor());
118 // top left, bottom left, bottom right, top right
119 graphics->fillRectangle(m_rec);
120 }
121
122}; // namespace fcn
void draw(Graphics *graphics) override
Draws this widget.
Definition bargraph.cpp:89
void setBarHeight(int h)
Sets the height of the bar.
Definition bargraph.cpp:63
int getBarWidth() const
Definition bargraph.cpp:58
void setBarWidth(int w)
Sets the width of the bar.
Definition bargraph.cpp:53
bool isOpaque() const
Definition bargraph.cpp:84
int getBarX() const
Definition bargraph.cpp:26
bool m_opaque
m_opaque is true if the graph is opaque, false otherwise.
Definition bargraph.hpp:131
void setBarPosition(int x, int y)
Sets the position of the bar.
Definition bargraph.cpp:41
void setBarSize(int w, int h)
Sets the size of the bar.
Definition bargraph.cpp:73
Rectangle m_rec
m_rec is the rectangle that represents the bar.
Definition bargraph.hpp:136
int getBarY() const
Definition bargraph.cpp:36
void setBarY(int y)
Sets the y position of the bar.
Definition bargraph.cpp:31
void setBarX(int x)
Sets the x position of the bar.
Definition bargraph.cpp:21
void setOpaque(bool opaque)
Sets the opacity of the graph.
Definition bargraph.cpp:79
int getBarHeight() const
Definition bargraph.cpp:68
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
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.
Represents a 2D coordinate (X, Y).
Definition point.hpp:34
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
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():