ManaPlus
windowmanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus 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 #include "gui/windowmanager.h"
25 
26 #include "client.h"
27 #include "configuration.h"
28 #include "game.h"
29 #include "main.h"
30 #include "settings.h"
31 #include "spellmanager.h"
32 
33 #include "fs/virtfs/tools.h"
34 
35 #include "gui/gui.h"
36 #include "gui/userpalette.h"
37 
39 
40 #include "gui/popups/textpopup.h"
41 
42 #ifdef DYECMD
43 #include "render/graphics.h"
44 #else // DYECMD
45 #include "gui/popups/beingpopup.h"
46 #include "gui/popups/itempopup.h"
47 #include "gui/popups/popupmenu.h"
48 #include "gui/popups/skillpopup.h"
49 #include "gui/popups/spellpopup.h"
51 
52 #include "gui/windows/chatwindow.h"
56 #include "gui/windows/helpwindow.h"
60 
62 
63 #include "utils/gettext.h"
64 #endif // DYECMD
65 
67 #include "gui/widgets/desktop.h"
68 
70 
71 #include "utils/delete2.h"
72 #include "utils/sdlcheckutils.h"
73 #include "utils/sdlhelper.h"
74 #ifdef __native_client__
75 #include "utils/naclmessages.h"
76 #endif // __native_client__
77 
78 PRAGMA48(GCC diagnostic push)
79 PRAGMA48(GCC diagnostic ignored "-Wshadow")
80 #ifdef ANDROID
81 #ifndef USE_SDL2
82 #include <SDL_screenkeyboard.h>
83 #endif // USE_SDL2
84 #endif // ANDROID
85 
86 #ifdef USE_SDL2
87 #include <SDL2_framerate.h>
88 #else // USE_SDL2
89 #include <SDL_framerate.h>
90 #endif // USE_SDL2
91 
92 #include <SDL_image.h>
93 
94 #ifdef WIN32
95 #include <SDL_syswm.h>
96 #endif // WIN32
97 PRAGMA48(GCC diagnostic pop)
98 
99 #include "debug.h"
100 
102 
103 namespace
104 {
105  SDL_Surface *mIcon(nullptr);
106 #ifndef USE_SDL2
108 #endif // USE_SDL2
109 
110  bool mIsMinimized(false);
111  bool mNewMessageFlag(false);
112 } // namespace
113 
115 {
116  // Initialize frame limiting
118  fpsManager.rateticks = 0;
119  fpsManager.lastticks = 0;
120  fpsManager.rate = 0;
121 }
122 
124 {
125  userPalette = new UserPalette;
126 #ifndef DYECMD
137  if (chatWindow != nullptr)
138  {
140  chatWindow = nullptr;
141  }
143  "DebugChat");
144  // TRANSLATORS: chat tab header
145  debugChatTab = new ChatTab(chatWindow, _("Debug"), "",
146  "#Debug", ChatTabType::DEBUG);
149  if (debugWindow != nullptr)
150  {
152  debugWindow = nullptr;
153  }
155  "DebugDebug");
156 #endif // DYECMD
157 
159 }
160 
162 {
163 #ifndef DYECMD
167  nullptr, nullptr, true);
169 #endif // DYECMD
170 }
171 
173 {
174 #ifndef DYECMD
178 #endif // DYECMD
179 }
180 
182 {
183 #ifndef DYECMD
196 
200 #endif // DYECMD
201 
203 }
204 
206 {
207  if (settings.options.test.empty())
208  {
209  settings.windowCaption = strprintf("%s %s",
210  branding.getStringValue("appName").c_str(),
211  SMALL_VERSION);
212  }
213  else
214  {
216  "Please wait - VIDEO MODE TEST - %s %s - Please wait",
217  branding.getStringValue("appName").c_str(),
218  SMALL_VERSION);
219  }
220 
222  settings.windowCaption.c_str());
223 }
224 
226 {
227  std::string str;
228  if (settings.login.empty())
229  str = settings.serverName;
230  else
231  str.append(settings.login).append(" ").append(settings.serverName);
232  if (str.empty())
233  {
234  settings.windowCaption = strprintf("%s %s",
235  branding.getStringValue("appName").c_str(),
236  SMALL_VERSION);
237  }
238  else
239  {
240  settings.windowCaption = strprintf("%s %s - %s",
241  branding.getStringValue("appName").c_str(),
243  str.c_str());
244  }
246  settings.windowCaption.c_str());
247 }
248 
249 void WindowManager::setFramerate(const unsigned int fpsLimit)
250 {
251  if (fpsLimit == 0U)
252  return;
253 
254  if (!settings.limitFps)
255  return;
256 
257  SDL_setFramerate(&fpsManager, fpsLimit);
258 }
259 
261 {
262  if (!settings.limitFps)
263  return 0;
264 
265  return SDL_getFramerate(&fpsManager);
266 }
267 
268 void WindowManager::doResizeVideo(const int actualWidth,
269  const int actualHeight,
270  const bool always)
271 {
272  if (!always
273  && mainGraphics->mActualWidth == actualWidth
274  && mainGraphics->mActualHeight == actualHeight)
275  {
276  return;
277  }
278 
279 #ifdef __native_client__
280  naclPostMessage("resize-window",
281  strprintf("%d,%d", actualWidth, actualHeight));
282 #else // __native_client__
283 
284  resizeVideo(actualWidth, actualHeight, always);
285 #endif // __native_client__
286 }
287 
288 void WindowManager::resizeVideo(int actualWidth,
289  int actualHeight,
290  const bool always)
291 {
292  // Keep a minimum size. This isn't adhered to by the actual window, but
293  // it keeps some window positions from getting messed up.
294  actualWidth = std::max(470, actualWidth);
295  actualHeight = std::max(320, actualHeight);
296 
297  if (mainGraphics == nullptr)
298  return;
299  if (!always
300  && mainGraphics->mActualWidth == actualWidth
301  && mainGraphics->mActualHeight == actualHeight)
302  {
303  return;
304  }
305 
306  if (mainGraphics->resizeScreen(actualWidth, actualHeight))
307  {
308  const int width = mainGraphics->mWidth;
309  const int height = mainGraphics->mHeight;
310  touchManager.resize(width, height);
311 
312  if (gui != nullptr)
313  gui->videoResized();
314 
315  if (desktop != nullptr)
316  desktop->setSize(width, height);
317 
318  client->moveButtons(width);
319 
320 #ifndef DYECMD
321  Game *const game = Game::instance();
322  if (game != nullptr)
323  Game::videoResized(width, height);
324 #endif // DYECMD
325 
326  if (gui != nullptr)
327  gui->draw();
328 
329  config.setValue("screenwidth", actualWidth);
330  config.setValue("screenheight", actualHeight);
331  }
332 }
333 
334 bool WindowManager::setFullScreen(const bool fs)
335 {
336 #ifdef __native_client__
337  naclPostMessage("set-fullscreen",
338  fs ? "on" : "off");
339  return true;
340 #else // __native_client__
341 
342  if (mainGraphics == nullptr)
343  return false;
344  return mainGraphics->setFullscreen(fs);
345 #endif // __native_client__
346 }
347 
349 {
351  config.getBoolValue("grabinput"));
352 }
353 
355 {
356  if (config.getBoolValue("enableGamma"))
357  {
359  config.getFloatValue("gamma"));
360  }
361 }
362 
364 {
365  const int val = config.getIntValue("vsync");
366  if (val > 0 && val < 2)
367  SDL::setVsync(val);
368 }
369 
371 {
372 #ifndef USE_SDL2
373  SDL_EnableKeyRepeat(config.getIntValue("repeateDelay"),
374  config.getIntValue("repeateInterval"));
375 #endif // USE_SDL2
376 }
377 
379 {
380  const int scale = config.getIntValue("scale");
381  if (mainGraphics->getScale() == scale)
382  return;
383  mainGraphics->setScale(scale);
386  true);
387 }
388 
390 {
391  mIsMinimized = n;
392  if (!n && mNewMessageFlag)
393  {
394  mNewMessageFlag = false;
396  settings.windowCaption.c_str());
397  }
398 }
399 
401 {
403  {
404  // show * on window caption
406  ("*" + settings.windowCaption).c_str());
407  mNewMessageFlag = true;
408  }
409 }
410 
412 {
413 #if !defined(ANDROID) && !defined(__SWITCH__)
414  std::string iconFile = branding.getValue("appIcon", "icons/manaplus");
415 #ifdef WIN32
416  iconFile.append(".ico");
417 #else // WIN32
418 
419  iconFile.append(".png");
420 #endif // WIN32
421 
422  iconFile = VirtFs::getPath(iconFile);
423  logger->log("Loading icon from file: %s", iconFile.c_str());
424 
425 #ifdef WIN32
426  static SDL_SysWMinfo pInfo;
427  if (mainGraphics)
429  else
430  SDL::getWindowWMInfo(nullptr, &pInfo);
431  // Attempt to load icon from .ico file
432  HICON icon = static_cast<HICON>(LoadImage(nullptr,
433  iconFile.c_str(),
434  IMAGE_ICON,
435  64, 64,
436  LR_LOADFROMFILE));
437  // If it's failing, we load the default resource file.
438  if (!icon)
439  {
440  logger->log("icon load error");
441  icon = LoadIcon(GetModuleHandle(nullptr), "A");
442  }
443  if (icon)
444  {
445 #ifdef WIN64
446  SetClassLongPtr(pInfo.window,
447  GCLP_HICON,
448  reinterpret_cast<LONG_PTR>(icon));
449 #else // WIN64
450  SetClassLong(pInfo.window,
451  GCL_HICON,
452  reinterpret_cast<LONG>(icon));
453 #endif // WIN64
454  }
455 #else // WIN32
456 
457  mIcon = MIMG_Load(iconFile.c_str());
458  if (mIcon != nullptr)
459  {
460 #ifdef USE_SDL2
461  SDL_SetSurfaceAlphaMod(mIcon, SDL_ALPHA_OPAQUE);
462 #else // USE_SDL2
463 
464  SDL_SetAlpha(mIcon, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
465 #endif // USE_SDL2
466 
468  }
469 #endif // WIN32
470 #endif // ANDROID
471 }
472 
474 {
475 #ifdef USE_SDL2
476  return SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE;
477 #else // USE_SDL2
478 
479  return mKeyboardHeight > 1;
480 #endif // USE_SDL2
481 }
482 
484 {
485  return mIsMinimized;
486 }
487 
488 #ifndef USE_SDL2
490 {
491  mKeyboardHeight = height;
492 }
493 #endif // USE_SDL2
494 
496 {
498  mIcon = nullptr;
499 }
int SDL_getFramerate(FPSmanager *manager)
Return the current target framerate in Hz.
int SDL_setFramerate(FPSmanager *manager, Uint32 rate)
Set the framerate in Hz.
BeingPopup * beingPopup
Definition: beingpopup.cpp:47
ChatTab * debugChatTab
Definition: chattab.cpp:63
ChatWindow * chatWindow
Definition: chatwindow.cpp:94
void setAllowHighlight(const bool n)
Definition: chattab.h:161
void scheduleDelete()
void setVisible(Visible visible)
void moveButtons(const int width)
Definition: client.cpp:774
std::string getValue(const std::string &key, const std::string &deflt) const
bool getBoolValue(const std::string &key) const
std::string getStringValue(const std::string &key) const
float getFloatValue(const std::string &key) const
void setValue(const std::string &key, const std::string &value)
int getIntValue(const std::string &key) const
Definition: game.h:64
static Game * instance()
Definition: game.h:82
static void videoResized(const int width, const int height)
Definition: game.cpp:1289
int mWidth
Definition: graphics.h:484
virtual bool resizeScreen(const int width, const int height)
Definition: graphics.cpp:558
int mHeight
Definition: graphics.h:485
int mActualHeight
Definition: graphics.h:487
bool setFullscreen(const bool fs)
Definition: graphics.cpp:542
int getScale() const
Definition: graphics.h:366
SDL_Surface * getWindow() const
Definition: graphics.h:144
void setScale(int scale)
Definition: graphics.cpp:206
int mActualWidth
Definition: graphics.h:486
void draw()
Definition: gui.cpp:470
void videoResized() const
Definition: gui.cpp:536
void log(const char *const log_text,...)
Definition: logger.cpp:269
bool limitFps
Definition: settings.h:154
std::string serverName
Definition: settings.h:114
Options options
Definition: settings.h:130
std::string windowCaption
Definition: settings.h:124
std::string login
Definition: settings.h:108
void loadSkills()
void resize(const int width, const int height)
void setSize(const int width, const int height)
Definition: widget.cpp:367
virtual void scheduleDelete()
Definition: window.cpp:831
Configuration config
Configuration branding
#define CREATEWIDGETV0(var, type)
Definition: createwidget.h:32
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:25
#define MSDL_FreeSurface(surface)
Definition: debug.h:54
#define MIMG_Load(file)
Definition: debug.h:61
DebugWindow * debugWindow
Definition: debugwindow.cpp:43
#define delete2(var)
Definition: delete2.h:25
Desktop * desktop
Definition: desktop.cpp:49
DidYouKnowWindow * didYouKnowWindow
Client * client
Definition: client.cpp:118
EquipmentWindow * beingEquipmentWindow
#define _(s)
Definition: gettext.h:35
Graphics * mainGraphics
Definition: graphics.cpp:109
Gui * gui
Definition: gui.cpp:111
HelpWindow * helpWindow
Definition: helpwindow.cpp:54
ItemPopup * itemPopup
Definition: itempopup.cpp:64
#define PRAGMA48(str)
Definition: localconsts.h:199
Logger * logger
Definition: logger.cpp:89
#define SMALL_VERSION
Definition: main.h:44
void SetWindowTitle(const SDL_Surface *const window, const char *const title)
Definition: sdlhelper.cpp:85
bool getWindowWMInfo(const SDL_Surface *const window, SDL_SysWMinfo *const info)
Definition: sdlhelper.cpp:112
void SetWindowIcon(const SDL_Surface *const window, SDL_Surface *const icon)
Definition: sdlhelper.cpp:91
void grabInput(const SDL_Surface *const window, const bool grab)
Definition: sdlhelper.cpp:97
void setGamma(const SDL_Surface *const window, const float gamma)
Definition: sdlhelper.cpp:102
void setVsync(const int val)
Definition: sdlhelper.cpp:107
std::string getPath(const std::string &file)
Definition: tools.cpp:97
bool setFullScreen(const bool fs)
bool isKeyboardVisible()
void setFramerate(const unsigned int fpsLimit)
void deleteValidateWindows()
void updateScreenKeyboard(const int height)
void setIsMinimized(const bool n)
void createValidateWindows()
void doResizeVideo(const int actualWidth, const int actualHeight, const bool always)
void resizeVideo(int actualWidth, int actualHeight, const bool always)
PopupMenu * popupMenu
Definition: popupmenu.cpp:103
QuestsWindow * questsWindow
Settings settings
Definition: settings.cpp:32
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
SkillDialog * skillDialog
Definition: skilldialog.cpp:66
SkillPopup * skillPopup
Definition: skillpopup.cpp:42
SpellManager * spellManager
SpellPopup * spellPopup
Definition: spellpopup.cpp:34
SpellShortcut * spellShortcut
std::string strprintf(const char *const format,...)
Structure holding the state and timing information of the framerate controller.
Uint32 framecount
float rateticks
Uint32 lastticks
std::string test
Definition: options.h:88
TextBoxPopup * textBoxPopup
TextPopup * textPopup
Definition: textpopup.cpp:33
TouchManager touchManager
UserPalette * userPalette
Definition: userpalette.cpp:34
const bool Visible_false
Definition: visible.h:30
FPSmanager fpsManager