FifeGUI 0.2.0
A C++ GUI library designed for games.
icon.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/icon.hpp"
6
7#include <string>
8
9#include "fifechan/graphics.hpp"
10#include "fifechan/image.hpp"
11#include "fifechan/rectangle.hpp"
12
13namespace fcn
14{
15 Icon::Icon() : mImage(nullptr), mInternalImage(false)
16 {
17
18 adjustSizeImpl();
19 }
20
21 Icon::Icon(std::string const & filename) : mImage(Image::load(filename)), mInternalImage(true)
22 {
23
25 }
26
27 Icon::Icon(Image const * image) : mImage(image), mInternalImage(false)
28 {
29
31 }
32
33 Icon::~Icon()
34 {
35 if (mInternalImage) {
36 delete mImage;
37 }
38 }
39
40 void Icon::setImage(std::string const & filename)
41 {
42 if (mInternalImage) {
43 delete mImage;
44 }
45
46 mImage = Image::load(filename);
47 mInternalImage = true;
49 }
50
51 void Icon::setImage(Image const * image)
52 {
53 if (mInternalImage) {
54 delete mImage;
55 }
56
57 mImage = image;
58 mInternalImage = false;
60 }
61
62 Image const * Icon::getImage() const
63 {
64 return mImage;
65 }
66
67 bool Icon::isScaling() const
68 {
69 return mScale;
70 }
71
72 void Icon::setScaling(bool scale)
73 {
74 mScale = scale;
75 }
76
77 bool Icon::isTiling() const
78 {
79 return mTile;
80 }
81
82 void Icon::setTiling(bool tile)
83 {
84 mTile = tile;
85 }
86
87 void Icon::setOpaque(bool opaque)
88 {
89 mOpaque = opaque;
90 }
91
92 bool Icon::isOpaque() const
93 {
94 return mOpaque;
95 }
96
97 void Icon::resizeToContent(bool recursion)
98 {
99 static_cast<void>(recursion);
101 }
102
104 {
106 }
107
109 {
110 // workaround to avoid resizing
111 if (mScale || mTile) {
112 return;
113 }
114 int w = (2 * getBorderSize()) + getPaddingLeft() + getPaddingRight();
115 int h = (2 * getBorderSize()) + getPaddingTop() + getPaddingBottom();
116 if (mImage != nullptr) {
117 w += mImage->getWidth();
118 h += mImage->getHeight();
119 }
120 setSize(w, h);
121 }
122
123 void Icon::draw(Graphics* graphics)
124 {
125 // draw icon background
126 if (mOpaque) {
127 Color color = getBackgroundColor();
128 if (isFocused() &&
129 ((getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
130 color = getSelectionColor();
131 }
132 graphics->setColor(color);
133 graphics->fillRectangle(Rectangle(
136 getWidth() - (2 * getBorderSize()),
137 getHeight() - (2 * getBorderSize())));
138 }
139 // draw icon image
140 if (mImage != nullptr) {
141 Rectangle const contentRect(
146
147 // draw with widget or image size
148 int const w = mScale ? contentRect.width : mImage->getWidth();
149 int const h = mScale ? contentRect.height : mImage->getHeight();
150
151 if (mTile && !mScale) {
152 Rectangle rect(contentRect.x, contentRect.y, w, h);
153 int const tmpW = getWidth() - getBorderSize() - getPaddingRight();
154 int const tmpH = getHeight() - getBorderSize() - getPaddingBottom();
155 while (rect.x < tmpW) {
156 rect.y = contentRect.y;
157 while (rect.y < tmpH) {
158 graphics->drawImage(mImage, 0, 0, rect.x, rect.y, rect.width, rect.height);
159 rect.y += rect.height;
160 }
161 rect.x += rect.width;
162 }
163 } else {
164 graphics->drawImage(mImage, 0, 0, contentRect.x, contentRect.y, w, h);
165 }
166 }
167 // draw border or frame
168 if (getBorderSize() > 0) {
169 if (isFocused() && (getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
170 drawSelectionFrame(graphics);
171 } else {
172 drawBorder(graphics);
173 }
174 }
175 }
176} // namespace fcn
Color.
Definition color.hpp:56
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:57
virtual void drawImage(Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height)=0
Draws a part of an image.
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.
bool mOpaque
True if opaque, otherwise false.
Definition icon.hpp:138
void setTiling(bool tile)
Sets if the image should be tiled to widget size.
Definition icon.cpp:82
void adjustSize() override
Resizes the widget's size to fit the content exactly.
Definition icon.cpp:103
bool mTile
True if tiling is enabled, otherwise false.
Definition icon.hpp:135
bool mInternalImage
True if the image has been loaded internally, false otherwise.
Definition icon.hpp:129
bool isTiling() const
Gets if the image is tiled to widget size.
Definition icon.cpp:77
bool isScaling() const
Gets if the image is scaled to widget size.
Definition icon.cpp:67
void resizeToContent()
Resizes the widget's size to fit the content exactly, calls recursively all childs.
Definition widget.hpp:1417
bool isOpaque() const
Definition icon.cpp:92
Image const * getImage() const
Gets the current image.
Definition icon.cpp:62
bool mScale
True if scaling is enabled, otherwise false.
Definition icon.hpp:132
Image const * mImage
The image to display.
Definition icon.hpp:122
void draw(Graphics *graphics) override
Draws the widget.
Definition icon.cpp:123
void setOpaque(bool opaque)
Sets the opacity of the background.
Definition icon.cpp:87
void setScaling(bool scale)
Sets if the image should be scaled to widget size.
Definition icon.cpp:72
void adjustSizeImpl()
Adjusts the size of the icon to fit the image.
Definition icon.cpp:108
void setImage(std::string const &filename)
Sets the image to display.
Definition icon.cpp:40
Abstract holder for image data.
Definition image.hpp:32
static Image * load(std::string const &filename, bool convertToDisplayFormat=true)
Loads an image by using the class' image loader.
Definition image.cpp:31
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:20
int width
Holds the width of the rectangle.
int y
Holds the x coordinate of the rectangle.
int x
Holds the x coordinate of the rectangle.
int height
Holds the height of the rectangle.
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
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:477
void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:855
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 getPaddingTop() const
Gets the top padding.
Definition widget.cpp:447
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
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:467
SelectionMode getSelectionMode() const
Gets the selection mode.
Definition widget.cpp:656
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:183
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:457
Color const & getSelectionColor() const
Gets the selection color.
Definition widget.cpp:626