Mana
Loading...
Searching...
No Matches
theme.h
Go to the documentation of this file.
1/*
2 * Gui Skinning
3 * Copyright (C) 2008 The Legend of Mazzeroth Development Team
4 * Copyright (C) 2009 Aethyra Development Team
5 * Copyright (C) 2009 The Mana World Development Team
6 * Copyright (C) 2009-2012 The Mana Developers
7 *
8 * This file is part of The Mana Client.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include "graphics.h"
27#include "eventlistener.h"
28
29#include "gui/palette.h"
30#include "resources/image.h"
31#include "utils/xml.h"
32
33#include <array>
34#include <map>
35#include <memory>
36#include <optional>
37#include <string>
38#include <variant>
39
40namespace gcn {
41class Widget;
42}
43
44class DyePalette;
45class Image;
46class ImageSet;
47class ProgressBar;
48
50{
51public:
52 ThemeInfo() = default;
53 explicit ThemeInfo(const std::string &path);
54
55 bool isValid() const { return !name.empty(); }
56
57 const std::string &getName() const { return name; }
58 const std::string &getPath() const { return path; }
59 std::string getFullPath() const;
60 const XML::Document &getDocument() const { return *doc; }
61
62private:
63 std::string name;
64 std::string path;
65 std::unique_ptr<XML::Document> doc;
66};
67
102
110
112{
113 gcn::Color color;
114 bool filled = true;
115};
116
118{
119 int offsetX = 0;
120 int offsetY = 0;
121 std::variant<ImageRect, Image *, ColoredRectangle> data;
122};
123
125{
126 bool bold = false;
127 gcn::Color color;
128 std::optional<gcn::Color> outlineColor;
129 std::optional<gcn::Color> shadowColor;
130};
131
133{
137 std::vector<SkinPart> parts;
138};
139
141{
142 WidgetState() = default;
143 explicit WidgetState(const gcn::Widget *widget);
144 explicit WidgetState(const gcn::Rectangle &dim, uint8_t flags = 0);
145
146 int x = 0;
147 int y = 0;
148 int width = 0;
149 int height = 0;
151};
152
153class Skin
154{
155 public:
156 Skin() = default;
157 ~Skin();
158
159 void addState(SkinState state);
160
161 void draw(Graphics *graphics, const WidgetState &state) const;
162
163 const SkinState *getState(uint8_t flags) const;
164
168 int getMinWidth() const;
169
173 int getMinHeight() const;
174
178 void updateAlpha(float alpha);
179
180 int width = 0;
181 int height = 0;
182 int frameSize = 0;
183 int padding = 0;
184 int spacing = 0;
188 int palette = 0;
189 bool showButtons = true;
190
191 private:
192 std::vector<SkinState> mStates;
193};
194
195class Theme : public EventListener
196{
197 public:
198 static std::string prepareThemePath();
199 static std::vector<ThemeInfo> getAvailableThemes();
200
201 Theme(const ThemeInfo &themeInfo);
202 ~Theme() override;
203
204 const std::string &getThemePath() const { return mThemePath; }
205
210 std::string resolvePath(const std::string &path) const;
211 static ResourceRef<Image> getImageFromTheme(const std::string &path);
212
267
279
286 static const gcn::Color &getThemeColor(int type);
287
288 static gcn::Color getProgressColor(int type, float progress);
289
290 const Palette &getPalette(size_t index) const;
291
295 const gcn::Color &getColor(int type) const;
296
304 static std::optional<int> getColorIdForChar(char c);
305
306 void drawSkin(Graphics *graphics, SkinType type, const WidgetState &state) const;
308 const gcn::Rectangle &area,
309 const gcn::Color &color,
310 float progress,
311 const std::string &text = std::string(),
313
314 const Skin &getSkin(SkinType skinType) const;
315
316 const Image *getIcon(const std::string &name) const;
317
321 int getGuiAlpha() const { return static_cast<int>(mAlpha * 255.0f); }
322
326 float getMinimumOpacity() const { return mMinimumOpacity; }
327
332 void setMinimumOpacity(float minimumOpacity);
333
334 void event(Event::Channel channel, const Event &event) override;
335
336 private:
340 void updateAlpha();
341
342 ResourceRef<Image> getImage(const std::string &path) const;
343
344 bool readTheme(const ThemeInfo &themeInfo);
345 void readSkinNode(XML::Node node);
346 void readSkinStateNode(XML::Node node, Skin &skin) const;
347 void readSkinStateImgNode(XML::Node node, SkinState &state) const;
348 void readIconNode(XML::Node node);
349 void readPaletteNode(XML::Node node);
351
352 std::string mThemePath;
353 std::map<SkinType, Skin> mSkins;
354 std::map<std::string, Image *> mIcons;
355
360 float mMinimumOpacity = 0.0f;
361 float mAlpha = 1.0;
362
363 std::vector<Palette> mPalettes;
364 std::array<std::unique_ptr<DyePalette>, THEME_PROG_END> mProgressColors;
365 std::array<std::optional<TextFormat>, THEME_PROG_END> mProgressTextFormats;
366};
Class for performing a linear interpolation between colors.
Definition dye.h:31
Definition event.h:42
Channel
Definition event.h:45
A central point of control for graphics.
Definition graphics.h:78
Stores a set of subimages originating from a single image.
Definition imageset.h:34
Defines a class for loading and storing images.
Definition image.h:45
Class controlling the game's color palette.
Definition palette.h:42
A progress bar.
Definition progressbar.h:34
Automatically counting Resource reference.
Definition resource.h:74
Definition theme.h:154
~Skin()
Definition theme.cpp:108
Skin()=default
void updateAlpha(float alpha)
Updates the alpha value of the skin.
Definition theme.cpp:213
int titleOffsetY
Definition theme.h:187
int frameSize
Definition theme.h:182
int titleOffsetX
Definition theme.h:186
void draw(Graphics *graphics, const WidgetState &state) const
Definition theme.cpp:122
int spacing
Definition theme.h:184
void addState(SkinState state)
Definition theme.cpp:117
int getMinWidth() const
Returns the minimum width which can be used with this skin.
Definition theme.cpp:177
int width
Definition theme.h:180
std::vector< SkinState > mStates
Definition theme.h:192
const SkinState * getState(uint8_t flags) const
Definition theme.cpp:168
int height
Definition theme.h:181
int padding
Definition theme.h:183
int palette
Definition theme.h:188
int getMinHeight() const
Returns the minimum height which can be used with this skin.
Definition theme.cpp:195
bool showButtons
Definition theme.h:189
int titleBarHeight
Definition theme.h:185
std::string path
Definition theme.h:64
const std::string & getPath() const
Definition theme.h:58
bool isValid() const
Definition theme.h:55
const std::string & getName() const
Definition theme.h:57
std::unique_ptr< XML::Document > doc
Definition theme.h:65
const XML::Document & getDocument() const
Definition theme.h:60
ThemeInfo()=default
std::string getFullPath() const
Definition theme.cpp:80
std::string name
Definition theme.h:63
Definition theme.h:196
void readSkinStateNode(XML::Node node, Skin &skin) const
Definition theme.cpp:605
float mAlpha
Definition theme.h:361
static std::string prepareThemePath()
Definition theme.cpp:250
bool readTheme(const ThemeInfo &themeInfo)
Definition theme.cpp:493
float mMinimumOpacity
Tells if the current skins opacity should not get less than the given value.
Definition theme.h:360
std::string resolvePath(const std::string &path) const
Returns the patch to the given GUI resource relative to the theme or, if it isn't in the theme,...
Definition theme.cpp:284
int getGuiAlpha() const
Get the current GUI alpha value.
Definition theme.h:321
const Skin & getSkin(SkinType skinType) const
Definition theme.cpp:434
static const gcn::Color & getThemeColor(int type)
Gets the color associated with the type in the default palette (0).
Definition theme.cpp:313
std::array< std::unique_ptr< DyePalette >, THEME_PROG_END > mProgressColors
Definition theme.h:364
std::string mThemePath
Definition theme.h:352
ResourceRef< Image > getImage(const std::string &path) const
Definition theme.cpp:303
std::vector< Palette > mPalettes
Definition theme.h:363
const Palette & getPalette(size_t index) const
Definition theme.cpp:328
void updateAlpha()
Updates the alpha values of all of the skins and images.
Definition theme.cpp:459
static ResourceRef< Image > getImageFromTheme(const std::string &path)
Definition theme.cpp:308
~Theme() override
Definition theme.cpp:244
void readProgressBarNode(XML::Node node)
Definition theme.cpp:918
static std::optional< int > getColorIdForChar(char c)
Returns the color ID associated with a character, if it exists.
Definition theme.cpp:338
void readSkinNode(XML::Node node)
Definition theme.cpp:562
void readIconNode(XML::Node node)
Definition theme.cpp:739
void readSkinStateImgNode(XML::Node node, SkinState &state) const
Definition theme.cpp:648
void setMinimumOpacity(float minimumOpacity)
Set the minimum opacity allowed to skins.
Definition theme.cpp:450
void drawProgressBar(Graphics *graphics, const gcn::Rectangle &area, const gcn::Color &color, float progress, const std::string &text=std::string(), ProgressPalette progressType=ProgressPalette::THEME_PROG_END) const
Definition theme.cpp:378
static std::vector< ThemeInfo > getAvailableThemes()
Definition theme.cpp:265
ProgressPalette
Definition theme.h:268
@ PROG_HP
Definition theme.h:270
@ PROG_JOB
Definition theme.h:276
@ THEME_PROG_END
Definition theme.h:277
@ PROG_INVY_SLOTS
Definition theme.h:274
@ PROG_DEFAULT
Definition theme.h:269
@ PROG_EXP
Definition theme.h:273
@ PROG_WEIGHT
Definition theme.h:275
@ PROG_MP
Definition theme.h:271
@ PROG_NO_MP
Definition theme.h:272
const gcn::Color & getColor(int type) const
Returns a color from the default palette (0).
Definition theme.cpp:333
static gcn::Color getProgressColor(int type, float progress)
Definition theme.cpp:318
float getMinimumOpacity() const
Get the minimum opacity allowed to skins.
Definition theme.h:326
ThemePalette
Definition theme.h:213
@ PARTY_TAB
Definition theme.h:228
@ WHISPER_TAB
Definition theme.h:229
@ HYPERLINK
Definition theme.h:249
@ PINK
Definition theme.h:221
@ LOGGER
Definition theme.h:248
@ TWOHAND
Definition theme.h:258
@ LEGS
Definition theme.h:256
@ PLAYER
Definition theme.h:242
@ HIGHLIGHT_TEXT
Definition theme.h:232
@ UNKNOWN_ITEM
Definition theme.h:250
@ OLDCHAT
Definition theme.h:237
@ IS
Definition theme.h:244
@ BUBBLE_TEXT
Definition theme.h:239
@ HEAD
Definition theme.h:252
@ BLACK
Definition theme.h:215
@ GENERIC
Definition theme.h:251
@ SHADOW
Definition theme.h:226
@ TAB_FLASH
Definition theme.h:233
@ CARET
Definition theme.h:225
@ SERVER
Definition theme.h:247
@ ARMS
Definition theme.h:262
@ OUTLINE
Definition theme.h:227
@ GREEN
Definition theme.h:217
@ HIGHLIGHT
Definition theme.h:231
@ AMMO
Definition theme.h:263
@ RED
Definition theme.h:216
@ GLOBAL
Definition theme.h:241
@ SERVER_VERSION_NOT_SUPPORTED
Definition theme.h:264
@ ITEM_EQUIPPED
Definition theme.h:235
@ SHOP_WARNING
Definition theme.h:234
@ TEXT
Definition theme.h:214
@ THEME_COLORS_END
Definition theme.h:265
@ BACKGROUND
Definition theme.h:230
@ TORSO
Definition theme.h:254
@ AWAYCHAT
Definition theme.h:238
@ USABLE
Definition theme.h:253
@ YELLOW
Definition theme.h:220
@ FEET
Definition theme.h:257
@ ONEHAND
Definition theme.h:255
@ SHIELD
Definition theme.h:259
@ GRAY
Definition theme.h:223
@ BLUE
Definition theme.h:218
@ NECKLACE
Definition theme.h:261
@ RING
Definition theme.h:260
@ ORANGE
Definition theme.h:219
@ CHAT
Definition theme.h:236
@ GUILD
Definition theme.h:246
@ PURPLE
Definition theme.h:222
@ WHISPER
Definition theme.h:243
@ BROWN
Definition theme.h:224
@ PARTY
Definition theme.h:245
@ GM
Definition theme.h:240
const Image * getIcon(const std::string &name) const
Definition theme.cpp:441
void event(Event::Channel channel, const Event &event) override
Definition theme.cpp:471
const std::string & getThemePath() const
Definition theme.h:204
void readPaletteNode(XML::Node node)
Definition theme.cpp:878
std::map< std::string, Image * > mIcons
Definition theme.h:354
void drawSkin(Graphics *graphics, SkinType type, const WidgetState &state) const
Definition theme.cpp:373
std::array< std::optional< TextFormat >, THEME_PROG_END > mProgressTextFormats
Definition theme.h:365
std::map< SkinType, Skin > mSkins
Definition theme.h:353
A helper class for parsing an XML document, which also cleans it up again (RAII).
Definition xml.h:190
Graphics * graphics
Definition client.cpp:104
unsigned char uint8_t
Definition sha256.cpp:81
gcn::Color color
Definition theme.h:113
int offsetY
Definition theme.h:120
int offsetX
Definition theme.h:119
std::variant< ImageRect, Image *, ColoredRectangle > data
Definition theme.h:121
uint8_t stateFlags
Definition theme.h:134
std::vector< SkinPart > parts
Definition theme.h:137
uint8_t setFlags
Definition theme.h:135
TextFormat textFormat
Definition theme.h:136
std::optional< gcn::Color > shadowColor
Definition theme.h:129
std::optional< gcn::Color > outlineColor
Definition theme.h:128
bool bold
Definition theme.h:126
gcn::Color color
Definition theme.h:127
uint8_t flags
Definition theme.h:150
WidgetState()=default
int width
Definition theme.h:148
int height
Definition theme.h:149
StateFlags
Definition theme.h:104
@ STATE_HOVERED
Definition theme.h:105
@ STATE_DISABLED
Definition theme.h:107
@ STATE_FOCUSED
Definition theme.h:108
@ STATE_SELECTED
Definition theme.h:106
SkinType
Definition theme.h:69
@ DropDownButton
@ ScrollAreaHBar
@ ScrollAreaHMarker
@ ScrollAreaVMarker
@ ScrollAreaVBar