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