summaryrefslogtreecommitdiff
path: root/src/gui/windowmanager.cpp
blob: e0172cca5dcd2c6ad567cb42f8f24c3ae13a2f18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2019  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gui/windowmanager.h"

#include "client.h"
#include "configuration.h"
#include "game.h"
#include "main.h"
#include "settings.h"
#include "spellmanager.h"

#include "fs/virtfs/tools.h"

#include "gui/gui.h"
#include "gui/userpalette.h"

#include "gui/shortcut/spellshortcut.h"

#include "gui/popups/textpopup.h"

#ifdef DYECMD
#include "render/graphics.h"
#else  // DYECMD
#include "gui/popups/beingpopup.h"
#include "gui/popups/itempopup.h"
#include "gui/popups/popupmenu.h"
#include "gui/popups/skillpopup.h"
#include "gui/popups/spellpopup.h"
#include "gui/popups/textboxpopup.h"

#include "gui/windows/chatwindow.h"
#include "gui/windows/debugwindow.h"
#include "gui/windows/didyouknowwindow.h"
#include "gui/windows/equipmentwindow.h"
#include "gui/windows/helpwindow.h"
#include "gui/windows/setupwindow.h"
#include "gui/windows/skilldialog.h"
#include "gui/windows/questswindow.h"

#include "gui/widgets/tabs/chat/chattab.h"

#include "utils/gettext.h"
#endif  // DYECMD

#include "gui/widgets/createwidget.h"
#include "gui/widgets/desktop.h"

#include "input/touch/touchmanager.h"

#include "utils/delete2.h"
#include "utils/sdlcheckutils.h"
#include "utils/sdlhelper.h"
#ifdef __native_client__
#include "utils/naclmessages.h"
#endif  // __native_client__

PRAGMA48(GCC diagnostic push)
PRAGMA48(GCC diagnostic ignored "-Wshadow")
#ifdef ANDROID
#ifndef USE_SDL2
#include <SDL_screenkeyboard.h>
#endif  // USE_SDL2
#endif  // ANDROID

#ifdef USE_SDL2
#include <SDL2_framerate.h>
#else  // USE_SDL2
#include <SDL_framerate.h>
#endif  // USE_SDL2

#include <SDL_image.h>

#ifdef WIN32
#include <SDL_syswm.h>
#endif  // WIN32
PRAGMA48(GCC diagnostic pop)

#include "debug.h"

FPSmanager fpsManager;

namespace
{
    SDL_Surface *mIcon(nullptr);
#ifndef USE_SDL2
    int mKeyboardHeight(0);
#endif  // USE_SDL2

    bool mIsMinimized(false);
    bool mNewMessageFlag(false);
}  // namespace

void WindowManager::init()
{
    // Initialize frame limiting
    fpsManager.framecount = 0;
    fpsManager.rateticks = 0;
    fpsManager.lastticks = 0;
    fpsManager.rate = 0;
}

void WindowManager::createWindows()
{
    userPalette = new UserPalette;
#ifndef DYECMD
    CREATEWIDGETV0(setupWindow, SetupWindow);
    CREATEWIDGETV0(helpWindow, HelpWindow);
    CREATEWIDGETV0(didYouKnowWindow, DidYouKnowWindow);
    CREATEWIDGETV0(popupMenu, PopupMenu);
    CREATEWIDGETV0(beingPopup, BeingPopup);
    CREATEWIDGETV0(textBoxPopup, TextBoxPopup);
    CREATEWIDGETV0(itemPopup, ItemPopup);
    CREATEWIDGETV0(spellPopup, SpellPopup);
    CREATEWIDGETV0(skillPopup, SkillPopup);
    delete2(debugChatTab)
    if (chatWindow != nullptr)
    {
        chatWindow->scheduleDelete();
        chatWindow = nullptr;
    }
    CREATEWIDGETV(chatWindow, ChatWindow,
        "DebugChat");
    // TRANSLATORS: chat tab header
    debugChatTab = new ChatTab(chatWindow, _("Debug"), "",
        "#Debug", ChatTabType::DEBUG);
    debugChatTab->setAllowHighlight(false);
    chatWindow->setVisible(Visible_false);
    if (debugWindow != nullptr)
    {
        debugWindow->scheduleDelete();
        debugWindow = nullptr;
    }
    CREATEWIDGETV(debugWindow, DebugWindow,
        "DebugDebug");
#endif  // DYECMD

    CREATEWIDGETV0(textPopup, TextPopup);
}

void WindowManager::createValidateWindows()
{
#ifndef DYECMD
    CREATEWIDGETV0(skillDialog, SkillDialog);
    skillDialog->loadSkills();
    CREATEWIDGETV(beingEquipmentWindow, EquipmentWindow,
        nullptr, nullptr, true);
    CREATEWIDGETV0(questsWindow, QuestsWindow);
#endif  // DYECMD
}

void WindowManager::deleteValidateWindows()
{
#ifndef DYECMD
    delete2(skillDialog)
    delete2(beingEquipmentWindow)
    delete2(questsWindow)
#endif  // DYECMD
}

void WindowManager::deleteWindows()
{
#ifndef DYECMD
    delete2(textBoxPopup)
    delete2(beingPopup)
    delete2(itemPopup)
    delete2(spellPopup)
    delete2(skillPopup)
    delete2(popupMenu)
    delete2(didYouKnowWindow)
    delete2(helpWindow)
    delete2(setupWindow)
    delete2(userPalette)
    delete2(spellManager)
    delete2(spellShortcut)

    delete2(debugChatTab)
    delete2(chatWindow)
    delete2(debugWindow)
#endif  // DYECMD

    delete2(textPopup)
}

void WindowManager::initTitle()
{
    if (settings.options.test.empty())
    {
        settings.windowCaption = strprintf("%s %s",
            branding.getStringValue("appName").c_str(),
            SMALL_VERSION);
    }
    else
    {
        settings.windowCaption = strprintf(
            "Please wait - VIDEO MODE TEST - %s %s - Please wait",
            branding.getStringValue("appName").c_str(),
            SMALL_VERSION);
    }

    SDL::SetWindowTitle(mainGraphics->getWindow(),
        settings.windowCaption.c_str());
}

void WindowManager::updateTitle()
{
    std::string str;
    if (settings.login.empty())
        str = settings.serverName;
    else
    {
        // dont show ugly account names in title, use "Session" insteed.
        if (settings.options.uniqueSession)
            str.append("Session ");
        else
            str.append(settings.login);
        str.append(" ").append(settings.serverName);
    }
    if (str.empty())
    {
        settings.windowCaption = strprintf("%s %s",
            branding.getStringValue("appName").c_str(),
            SMALL_VERSION);
    }
    else
    {
        settings.windowCaption = strprintf("%s %s - %s",
            branding.getStringValue("appName").c_str(),
            SMALL_VERSION,
            str.c_str());
    }
    SDL::SetWindowTitle(mainGraphics->getWindow(),
        settings.windowCaption.c_str());
}

void WindowManager::setFramerate(const unsigned int fpsLimit)
{
    if (fpsLimit == 0U)
        return;

    if (!settings.limitFps)
        return;

    SDL_setFramerate(&fpsManager, fpsLimit);
}

int WindowManager::getFramerate()
{
    if (!settings.limitFps)
        return 0;

    return SDL_getFramerate(&fpsManager);
}

void WindowManager::doResizeVideo(const int actualWidth,
                                  const int actualHeight,
                                  const bool always)
{
    if (!always
        && mainGraphics->mActualWidth == actualWidth
        && mainGraphics->mActualHeight == actualHeight)
    {
        return;
    }

#ifdef __native_client__
    naclPostMessage("resize-window",
        strprintf("%d,%d", actualWidth, actualHeight));
#else  // __native_client__

    resizeVideo(actualWidth, actualHeight, always);
#endif  // __native_client__
}

void WindowManager::resizeVideo(int actualWidth,
                                int actualHeight,
                                const bool always)
{
    // Keep a minimum size. This isn't adhered to by the actual window, but
    // it keeps some window positions from getting messed up.
    actualWidth = std::max(470, actualWidth);
    actualHeight = std::max(320, actualHeight);

    if (mainGraphics == nullptr)
        return;
    if (!always
        && mainGraphics->mActualWidth == actualWidth
        && mainGraphics->mActualHeight == actualHeight)
    {
        return;
    }

    if (mainGraphics->resizeScreen(actualWidth, actualHeight))
    {
        const int width = mainGraphics->mWidth;
        const int height = mainGraphics->mHeight;
        touchManager.resize(width, height);

        if (gui != nullptr)
            gui->videoResized();

        if (desktop != nullptr)
            desktop->setSize(width, height);

        client->moveButtons(width);

#ifndef DYECMD
        Game *const game = Game::instance();
        if (game != nullptr)
            Game::videoResized(width, height);
#endif  // DYECMD

        if (gui != nullptr)
            gui->draw();

        config.setValue("screenwidth", actualWidth);
        config.setValue("screenheight", actualHeight);
    }
}

bool WindowManager::setFullScreen(const bool fs)
{
#ifdef __native_client__
    naclPostMessage("set-fullscreen",
        fs ? "on" : "off");
    return true;
#else  // __native_client__

    if (mainGraphics == nullptr)
        return false;
    return mainGraphics->setFullscreen(fs);
#endif  // __native_client__
}

void WindowManager::applyGrabMode()
{
    SDL::grabInput(mainGraphics->getWindow(),
        config.getBoolValue("grabinput"));
}

void WindowManager::applyGamma()
{
    if (config.getBoolValue("enableGamma"))
    {
        SDL::setGamma(mainGraphics->getWindow(),
            config.getFloatValue("gamma"));
    }
}

void WindowManager::applyVSync()
{
    const int val = config.getIntValue("vsync");
    if (val > 0 && val < 2)
        SDL::setVsync(val);
}

void WindowManager::applyKeyRepeat()
{
#ifndef USE_SDL2
    SDL_EnableKeyRepeat(config.getIntValue("repeateDelay"),
        config.getIntValue("repeateInterval"));
#endif  // USE_SDL2
}

void WindowManager::applyScale()
{
    const int scale = config.getIntValue("scale");
    if (mainGraphics->getScale() == scale)
        return;
    mainGraphics->setScale(scale);
    doResizeVideo(mainGraphics->mActualWidth,
        mainGraphics->mActualHeight,
        true);
}

void WindowManager::setIsMinimized(const bool n)
{
    mIsMinimized = n;
    if (!n && mNewMessageFlag)
    {
        mNewMessageFlag = false;
        SDL::SetWindowTitle(mainGraphics->getWindow(),
            settings.windowCaption.c_str());
    }
}

void WindowManager::newChatMessage()
{
    if (!mNewMessageFlag && mIsMinimized)
    {
        // show * on window caption
        SDL::SetWindowTitle(mainGraphics->getWindow(),
            ("*" + settings.windowCaption).c_str());
        mNewMessageFlag = true;
    }
}

void WindowManager::setIcon()
{
#if !defined(ANDROID) && !defined(__SWITCH__)
    std::string iconFile = branding.getValue("appIcon", "icons/manaplus");
#ifdef WIN32
    iconFile.append(".ico");
#else  // WIN32

    iconFile.append(".png");
#endif  // WIN32

    iconFile = VirtFs::getPath(iconFile);
    logger->log("Loading icon from file: %s", iconFile.c_str());

#ifdef WIN32
    static SDL_SysWMinfo pInfo;
    if (mainGraphics)
        SDL::getWindowWMInfo(mainGraphics->getWindow(), &pInfo);
    else
        SDL::getWindowWMInfo(nullptr, &pInfo);

    // Attempt to load icon from .ico file
    HICON icon = static_cast<HICON>(LoadImage(nullptr,
        iconFile.c_str(),
        IMAGE_ICON,
        64, 64,
        LR_LOADFROMFILE));
    // If it's failing, we load the default resource file.
    if (!icon)
    {
        logger->log("icon load error");
        icon = LoadIcon(GetModuleHandle(nullptr), "A");
    }


#ifdef USE_SDL2
    const HWND& windowRef = pInfo.info.win.window;
#else
    const HWND& windowRef = pInfo.window;
#endif  // USE_SDL2

    if (icon)
    {
#ifdef WIN64
        SetClassLongPtr(windowRef,
                        GCLP_HICON,
                        reinterpret_cast<LONG_PTR>(icon));
#else  // WIN64
        SetClassLong(windowRef,
                     GCL_HICON,
                     reinterpret_cast<LONG>(icon));
#endif  // WIN64
    }
#else  // WIN32

    mIcon = MIMG_Load(iconFile.c_str());
    if (mIcon != nullptr)
    {
#ifdef USE_SDL2
        SDL_SetSurfaceAlphaMod(mIcon, SDL_ALPHA_OPAQUE);
#else  // USE_SDL2

        SDL_SetAlpha(mIcon, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
#endif  // USE_SDL2

        SDL::SetWindowIcon(mainGraphics->getWindow(), mIcon);
    }
#endif  // WIN32
#endif  // ANDROID
}

bool WindowManager::isKeyboardVisible()
{
#ifdef USE_SDL2
    return SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE;
#else  // USE_SDL2

    return mKeyboardHeight > 1;
#endif  // USE_SDL2
}

bool WindowManager::getIsMinimized()
{
    return mIsMinimized;
}

#ifndef USE_SDL2
void WindowManager::updateScreenKeyboard(const int height)
{
    mKeyboardHeight = height;
}
#endif  // USE_SDL2

void WindowManager::deleteIcon()
{
    MSDL_FreeSurface(mIcon);
    mIcon = nullptr;
}