ManaPlus
desktop.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009-2010 The Mana World Development Team
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2019-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "gui/widgets/desktop.h"
24 
25 #include "configuration.h"
26 #include "main.h"
27 
28 #include "gui/skin.h"
29 
31 
32 #include "input/inputmanager.h"
33 
35 
36 #include "resources/imagehelper.h"
37 #include "resources/wallpaper.h"
38 
39 #include "resources/image/image.h"
40 
42 
44 
45 #include "render/graphics.h"
46 
47 #include "debug.h"
48 
49 Desktop *desktop = nullptr;
50 
51 Desktop::Desktop(const Widget2 *const widget) :
52  Container(widget),
53  LinkHandler(),
55  mWallpaper(nullptr),
56  mVersionLabel(new StaticBrowserBox(this, Opaque_false,
57  "browserbox.xml")),
58  mSkin(nullptr),
59  mBackgroundGrayColor(getThemeColor(ThemeColorId::BACKGROUND_GRAY, 255U)),
60  mShowBackground(true)
61 {
63 
64  addWidgetListener(this);
65 
67 
68  if (theme != nullptr)
69  {
70  mSkin = theme->load("desktop.xml",
71  "",
72  true,
74  }
75 
76  if (mSkin != nullptr)
77  mShowBackground = (mSkin->getOption("showBackground") != 0);
78 
79  const std::string appName = branding.getValue("appName", std::string());
80  if (appName.empty())
81  {
83  false);
84  }
85  else
86  {
88  appName.c_str()),
89  false);
90  }
91  mVersionLabel->addRow("copyright",
92  "(C) ManaPlus developers, http://manaplus.org");
95 }
96 
98 {
99  if (mWallpaper != nullptr)
100  {
101  mWallpaper->decRef();
102  mWallpaper = nullptr;
103  }
104  if (theme != nullptr)
105  theme->unload(mSkin);
106 }
107 
109 {
110  if (mSkin != nullptr)
111  {
113  mSkin->getOption("versionX", 25),
114  mSkin->getOption("versionY", 2));
115  }
116  else
117  {
118  addXY(mVersionLabel, 25, 2);
119  }
120 }
121 
123 {
126 }
127 
129 {
133 }
134 
135 void Desktop::draw(Graphics *const graphics)
136 {
137  BLOCK_START("Desktop::draw")
138  GLDEBUG_START("Desktop::draw")
139 
140  const Rect &rect = mDimension;
141  const int width = rect.width;
142  const int height = rect.height;
143  if (mWallpaper != nullptr)
144  {
145  const int wallpWidth = mWallpaper->getWidth();
146  const int wallpHeight = mWallpaper->getHeight();
147 
148  if (width > wallpWidth || height > wallpHeight)
149  {
150  graphics->setColor(mBackgroundGrayColor);
151  graphics->fillRectangle(Rect(0, 0, width, height));
152  }
153 
155  {
156  graphics->drawImage(mWallpaper,
157  (width - wallpWidth) / 2,
158  (height - wallpHeight) / 2);
159  }
160  else
161  {
162  graphics->drawRescaledImage(mWallpaper, 0, 0, width, height);
163  }
164  }
165  else
166  {
167  graphics->setColor(mBackgroundGrayColor);
168  graphics->fillRectangle(Rect(0, 0, width, height));
169  }
170 
171  Container::draw(graphics);
172  GLDEBUG_END()
173  BLOCK_END("Desktop::draw")
174 }
175 
176 void Desktop::safeDraw(Graphics *const graphics)
177 {
178  Desktop::draw(graphics);
179 }
180 
182 {
183  if (!mShowBackground || !config.getBoolValue("showBackground"))
184  return;
185 
186  const std::string wallpaperName =
188 
189  Image *const nWallPaper = Theme::getImageFromTheme(wallpaperName);
190 
191  if (nWallPaper != nullptr)
192  {
193  if (mWallpaper != nullptr)
194  {
196  mWallpaper = nullptr;
197  }
198 
199  const Rect &rect = mDimension;
200  const int width = rect.width;
201  const int height = rect.height;
202 
204  (nWallPaper->getWidth() != width ||
205  nWallPaper->getHeight() != height))
206  {
207  // We rescale to obtain a fullscreen wallpaper...
208  Image *const newRsclWlPpr = Loader::getRescaled(
209  nWallPaper, width, height);
210 
211  if (newRsclWlPpr != nullptr)
212  {
213  ResourceManager::decRefDelete(nWallPaper);
214  // We replace the resource in the resource manager
215  mWallpaper = newRsclWlPpr;
216  }
217  else
218  {
219  mWallpaper = nWallPaper;
220  }
221  }
222  else
223  {
224  mWallpaper = nWallPaper;
225  }
226  }
227  else
228  {
229  logger->log("Couldn't load %s as wallpaper", wallpaperName.c_str());
230  }
231 }
232 
233 void Desktop::handleLink(const std::string &link, MouseEvent *event A_UNUSED)
234 {
235  if (link == "copyright")
237 }
void draw(Graphics *const graphics)
void addXY(Widget *const widget, const int x, const int y)
std::string getValue(const std::string &key, const std::string &deflt) const
bool getBoolValue(const std::string &key) const
void setBestFittingWallpaper()
Definition: desktop.cpp:181
void reloadWallpaper()
Definition: desktop.cpp:122
void draw(Graphics *const graphics)
Definition: desktop.cpp:135
Desktop(const Widget2 *const widget)
Definition: desktop.cpp:51
void safeDraw(Graphics *const graphics)
Definition: desktop.cpp:176
StaticBrowserBox * mVersionLabel
Definition: desktop.h:81
void handleLink(const std::string &link, MouseEvent *event)
Definition: desktop.cpp:233
Color mBackgroundGrayColor
Definition: desktop.h:83
~Desktop()
Definition: desktop.cpp:97
bool mShowBackground
Definition: desktop.h:84
Skin * mSkin
Definition: desktop.h:82
void widgetResized(const Event &event)
Definition: desktop.cpp:128
Image * mWallpaper
Definition: desktop.h:80
void postInit()
Definition: desktop.cpp:108
Definition: event.h:79
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
virtual void fillRectangle(const Rect &rectangle)=0
virtual void setColor(const Color &color)
Definition: graphics.h:320
virtual void drawRescaledImage(const Image *const image, int dstX, int dstY, const int desiredWidth, const int desiredHeight)=0
virtual RenderType useOpenGL() const
Definition: imagehelper.h:107
void executeAction(const InputActionT keyNum)
void log(const char *const log_text,...)
Definition: logger.cpp:269
Definition: rect.h:74
int width
Definition: rect.h:219
int height
Definition: rect.h:224
int getOption(const std::string &name) const
Definition: skin.h:106
void addRow(const std::string &row, const bool atTop)
void setLinkHandler(LinkHandler *linkHandler)
void unload(Skin *const skin)
Definition: theme.cpp:250
static std::string getThemePath()
Definition: theme.h:67
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:179
static Image * getImageFromTheme(const std::string &path)
Definition: theme.cpp:655
static void loadWallpapers()
Definition: wallpaper.cpp:88
static std::string getWallpaper(const int width, const int height)
Definition: wallpaper.cpp:137
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setSize(const int width, const int height)
Definition: widget.cpp:367
Rect mDimension
Definition: widget.h:1101
Color mBackgroundColor
Definition: widget.h:1091
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Configuration config
Configuration branding
#define new
Definition: debug_new.h:147
Desktop * desktop
Definition: desktop.cpp:49
ImageHelper * imageHelper
Definition: imagehelper.cpp:44
InputManager inputManager
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
Logger * logger
Definition: logger.cpp:89
#define FULL_VERSION
Definition: main.h:164
Image * getRescaled(const Image *const image, const int width, const int height)
void decRefDelete(Resource *const res)
const bool Opaque_false
Definition: opaque.h:30
#define GLDEBUG_START(text)
Definition: opengldebug.h:35
#define GLDEBUG_END()
Definition: opengldebug.h:36
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
@ RENDER_SOFTWARE
Definition: rendertype.h:27
std::string strprintf(const char *const format,...)
Theme * theme
Definition: theme.cpp:62