GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/progs/manaplus/gui/viewport.cpp Lines: 1 535 0.2 %
Date: 2021-03-17 Branches: 0 554 0.0 %

Line Branch Exec Source
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 "progs/manaplus/gui/viewport.h"
25
26
#include "actormanager.h"
27
#include "configuration.h"
28
#include "game.h"
29
#include "settings.h"
30
#include "sdlshared.h"
31
#include "textmanager.h"
32
33
#include "being/flooritem.h"
34
#include "being/localplayer.h"
35
#include "being/playerinfo.h"
36
37
#include "enums/resources/map/blockmask.h"
38
#include "enums/resources/map/mapitemtype.h"
39
40
#include "gui/gui.h"
41
#include "gui/popupmanager.h"
42
#include "gui/userpalette.h"
43
44
#include "gui/fonts/font.h"
45
46
#include "gui/popups/beingpopup.h"
47
#include "gui/popups/popupmenu.h"
48
#include "gui/popups/textpopup.h"
49
50
#include "gui/windows/ministatuswindow.h"
51
52
#include "input/inputmanager.h"
53
54
#include "utils/checkutils.h"
55
#include "utils/foreach.h"
56
57
#include "resources/map/map.h"
58
#include "resources/map/mapitem.h"
59
#include "resources/map/speciallayer.h"
60
61
#include "debug.h"
62
63
Viewport *viewport = nullptr;
64
65
extern volatile int tick_time;
66
67
Viewport::Viewport() :
68
    WindowContainer(nullptr),
69
    MouseListener(),
70
    ConfigListener(),
71
    mMouseX(0),
72
    mMouseY(0),
73
    mMap(nullptr),
74
    mHoverBeing(nullptr),
75
    mHoverItem(nullptr),
76
    mHoverSign(nullptr),
77
    mScrollRadius(config.getIntValue("ScrollRadius")),
78
    mScrollLaziness(config.getIntValue("ScrollLaziness")),
79
    mScrollCenterOffsetX(config.getIntValue("ScrollCenterOffsetX")),
80
    mScrollCenterOffsetY(config.getIntValue("ScrollCenterOffsetY")),
81
    mMousePressX(0),
82
    mMousePressY(0),
83
    mPixelViewX(0),
84
    mPixelViewY(0),
85
    mMidTileX(0),
86
    mMidTileY(0),
87
    mViewXmax(0),
88
    mViewYmax(0),
89
    mLocalWalkTime(-1),
90
    mCameraRelativeX(0),
91
    mCameraRelativeY(0),
92
    mShowBeingPopup(config.getBoolValue("showBeingPopup")),
93
    mSelfMouseHeal(config.getBoolValue("selfMouseHeal")),
94
    mEnableLazyScrolling(config.getBoolValue("enableLazyScrolling")),
95
    mMouseDirectionMove(config.getBoolValue("mouseDirectionMove")),
96
    mLongMouseClick(config.getBoolValue("longmouseclick")),
97
    mAllowMoveByMouse(config.getBoolValue("allowMoveByMouse")),
98
    mMouseClicked(false),
99
    mPlayerFollowMouse(false)
100
{
101
    setOpaque(Opaque_false);
102
    addMouseListener(this);
103
104
    config.addListener("ScrollLaziness", this);
105
    config.addListener("ScrollRadius", this);
106
    config.addListener("showBeingPopup", this);
107
    config.addListener("selfMouseHeal", this);
108
    config.addListener("enableLazyScrolling", this);
109
    config.addListener("mouseDirectionMove", this);
110
    config.addListener("longmouseclick", this);
111
    config.addListener("allowMoveByMouse", this);
112
113
    setFocusable(true);
114
    updateMidVars();
115
}
116
117
Viewport::~Viewport()
118
{
119
    config.removeListeners(this);
120
    CHECKLISTENERS
121
}
122
123
void Viewport::setMap(Map *const map)
124
{
125
    if ((mMap != nullptr) && (map != nullptr))
126
        map->setDrawLayersFlags(mMap->getDrawLayersFlags());
127
    mMap = map;
128
    updateMaxVars();
129
}
130
131
void Viewport::draw(Graphics *const graphics)
132
{
133
    BLOCK_START("Viewport::draw 1")
134
    static int lastTick = tick_time;
135
136
    if ((mMap == nullptr) || (localPlayer == nullptr))
137
    {
138
        graphics->setColor(Color(64, 64, 64, 255));
139
        graphics->fillRectangle(
140
                Rect(0, 0, getWidth(), getHeight()));
141
        BLOCK_END("Viewport::draw 1")
142
        return;
143
    }
144
145
    // Avoid freaking out when tick_time overflows
146
    if (tick_time < lastTick)
147
        lastTick = tick_time;
148
149
    // Calculate viewpoint
150
151
    const int player_x = localPlayer->mPixelX - mMidTileX;
152
    const int player_y = localPlayer->mPixelY - mMidTileY;
153
154
    if (mScrollLaziness < 1)
155
        mScrollLaziness = 1;  // Avoids division by zero
156
157
    if (mEnableLazyScrolling)
158
    {
159
        int cnt = 0;
160
161
        // Apply lazy scrolling
162
        while (lastTick < tick_time && cnt < mapTileSize)
163
        {
164
            if (player_x > mPixelViewX + mScrollRadius)
165
            {
166
                mPixelViewX += CAST_S32(
167
                    static_cast<float>(player_x
168
                    - mPixelViewX - mScrollRadius) /
169
                    static_cast<float>(mScrollLaziness));
170
            }
171
            if (player_x < mPixelViewX - mScrollRadius)
172
            {
173
                mPixelViewX += CAST_S32(
174
                    static_cast<float>(player_x
175
                    - mPixelViewX + mScrollRadius) /
176
                    static_cast<float>(mScrollLaziness));
177
            }
178
            if (player_y > mPixelViewY + mScrollRadius)
179
            {
180
                mPixelViewY += CAST_S32(
181
                    static_cast<float>(player_y
182
                    - mPixelViewY - mScrollRadius) /
183
                    static_cast<float>(mScrollLaziness));
184
            }
185
            if (player_y < mPixelViewY - mScrollRadius)
186
            {
187
                mPixelViewY += CAST_S32(
188
                    static_cast<float>(player_y
189
                    - mPixelViewY + mScrollRadius) /
190
                    static_cast<float>(mScrollLaziness));
191
            }
192
            lastTick ++;
193
            cnt ++;
194
        }
195
196
        // Auto center when player is off screen
197
        if (cnt > 30 || player_x - mPixelViewX
198
            > graphics->mWidth / 2 || mPixelViewX
199
            - player_x > graphics->mWidth / 2 || mPixelViewY
200
            - player_y > graphics->getHeight() / 2 ||  player_y
201
            - mPixelViewY > graphics->getHeight() / 2)
202
        {
203
            if (player_x <= 0 || player_y <= 0)
204
            {
205
                logger->log("incorrect player position: %d, %d, %d, %d",
206
                    player_x, player_y, mPixelViewX, mPixelViewY);
207
                logger->log("tile position: %d, %d",
208
                    localPlayer->getTileX(), localPlayer->getTileY());
209
            }
210
            mPixelViewX = player_x;
211
            mPixelViewY = player_y;
212
        }
213
    }
214
    else
215
    {
216
        mPixelViewX = player_x;
217
        mPixelViewY = player_y;
218
    }
219
220
    if (mPixelViewX < 0)
221
        mPixelViewX = 0;
222
    if (mPixelViewY < 0)
223
        mPixelViewY = 0;
224
    if (mPixelViewX > mViewXmax)
225
        mPixelViewX = mViewXmax;
226
    if (mPixelViewY > mViewYmax)
227
        mPixelViewY = mViewYmax;
228
229
    // Draw tiles and sprites
230
    mMap->draw(graphics, mPixelViewX, mPixelViewY);
231
232
    const MapTypeT drawType = settings.mapDrawType;
233
    if (drawType != MapType::NORMAL)
234
    {
235
        if (drawType != MapType::SPECIAL4)
236
        {
237
            mMap->drawCollision(graphics, mPixelViewX,
238
                mPixelViewY, drawType);
239
        }
240
        if (drawType == MapType::DEBUGTYPE)
241
            drawDebugPath(graphics);
242
    }
243
244
    if (localPlayer->getCheckNameSetting())
245
    {
246
        localPlayer->setCheckNameSetting(false);
247
        localPlayer->setName(localPlayer->getName());
248
    }
249
250
    // Draw text
251
    if (textManager != nullptr)
252
        textManager->draw(graphics, mPixelViewX, mPixelViewY);
253
254
    // Draw player names, speech, and emotion sprite as needed
255
    const ActorSprites &actors = actorManager->getAll();
256
    FOR_EACH (ActorSpritesIterator, it, actors)
257
    {
258
        if ((*it)->getType() == ActorType::FloorItem)
259
            continue;
260
        Being *const b = static_cast<Being*>(*it);
261
        b->drawEmotion(graphics, mPixelViewX, mPixelViewY);
262
        b->drawSpeech(mPixelViewX, mPixelViewY);
263
    }
264
265
    if (miniStatusWindow != nullptr)
266
        miniStatusWindow->drawIcons(graphics);
267
268
    // Draw contained widgets
269
    WindowContainer::draw(graphics);
270
    BLOCK_END("Viewport::draw 1")
271
}
272
273
void Viewport::safeDraw(Graphics *const graphics)
274
{
275
    Viewport::draw(graphics);
276
}
277
278
void Viewport::logic()
279
{
280
    BLOCK_START("Viewport::logic")
281
    // Make the player follow the mouse position
282
    // if the mouse is dragged elsewhere than in a window.
283
    Gui::getMouseState(mMouseX, mMouseY);
284
    BLOCK_END("Viewport::logic")
285
}
286
287
void Viewport::followMouse()
288
{
289
    if (gui == nullptr)
290
        return;
291
    const MouseStateType button = Gui::getMouseState(mMouseX, mMouseY);
292
    // If the left button is dragged
293
    if (mPlayerFollowMouse && ((button & SDL_BUTTON(1)) != 0))
294
    {
295
        // We create a mouse event and send it to mouseDragged.
296
        const MouseEvent event(nullptr,
297
            MouseEventType::DRAGGED,
298
            MouseButton::LEFT,
299
            mMouseX,
300
            mMouseY,
301
            0);
302
303
        walkByMouse(event);
304
    }
305
}
306
307
void Viewport::drawDebugPath(Graphics *const graphics)
308
{
309
    if (localPlayer == nullptr ||
310
        userPalette == nullptr ||
311
        actorManager == nullptr ||
312
        mMap == nullptr ||
313
        gui == nullptr)
314
    {
315
        return;
316
    }
317
318
    Gui::getMouseState(mMouseX, mMouseY);
319
320
    static Path debugPath;
321
    static Vector lastMouseDestination = Vector(0.0F, 0.0F, 0.0F);
322
    const int mousePosX = mMouseX + mPixelViewX;
323
    const int mousePosY = mMouseY + mPixelViewY;
324
    Vector mouseDestination(mousePosX, mousePosY, 0.0F);
325
326
    if (mouseDestination.x != lastMouseDestination.x
327
        || mouseDestination.y != lastMouseDestination.y)
328
    {
329
        debugPath = mMap->findPath(
330
            CAST_S32(localPlayer->mPixelX - mapTileSize / 2) / mapTileSize,
331
            CAST_S32(localPlayer->mPixelY - mapTileSize) / mapTileSize,
332
            mousePosX / mapTileSize,
333
            mousePosY / mapTileSize,
334
            localPlayer->getBlockWalkMask(),
335
            500);
336
        lastMouseDestination = mouseDestination;
337
    }
338
    drawPath(graphics, debugPath, userPalette->getColorWithAlpha(
339
        UserColorId::ROAD_POINT));
340
341
    const ActorSprites &actors = actorManager->getAll();
342
    FOR_EACH (ActorSpritesConstIterator, it, actors)
343
    {
344
        const Being *const being = dynamic_cast<const Being*>(*it);
345
        if ((being != nullptr) && being != localPlayer)
346
        {
347
            const Path &beingPath = being->getPath();
348
            drawPath(graphics, beingPath, userPalette->getColorWithAlpha(
349
                UserColorId::ROAD_POINT));
350
        }
351
    }
352
}
353
354
void Viewport::drawPath(Graphics *const graphics,
355
                        const Path &path,
356
                        const Color &color) const
357
{
358
    graphics->setColor(color);
359
    Font *const font = getFont();
360
361
    int cnt = 1;
362
    FOR_EACH (Path::const_iterator, i, path)
363
    {
364
        const int squareX = i->x * mapTileSize - mPixelViewX + 12;
365
        const int squareY = i->y * mapTileSize - mPixelViewY + 12;
366
367
        graphics->fillRectangle(Rect(squareX, squareY, 8, 8));
368
        if (mMap != nullptr)
369
        {
370
            const std::string str = toString(cnt);
371
            font->drawString(graphics,
372
                color, color,
373
                str,
374
                squareX + 4 - font->getWidth(str) / 2,
375
                squareY + 12);
376
        }
377
        cnt ++;
378
    }
379
}
380
381
bool Viewport::openContextMenu(const MouseEvent &event)
382
{
383
    mPlayerFollowMouse = false;
384
    const int eventX = event.getX();
385
    const int eventY = event.getY();
386
    if (popupMenu == nullptr)
387
        return false;
388
    if (mHoverBeing != nullptr)
389
    {
390
        validateSpeed();
391
        if (actorManager != nullptr)
392
        {
393
            STD_VECTOR<ActorSprite*> beings;
394
            const int x = mMouseX + mPixelViewX;
395
            const int y = mMouseY + mPixelViewY;
396
            actorManager->findBeingsByPixel(beings, x, y, AllPlayers_true);
397
            if (beings.size() > 1)
398
                popupMenu->showPopup(eventX, eventY, beings);
399
            else
400
                popupMenu->showPopup(eventX, eventY, mHoverBeing);
401
            return true;
402
        }
403
    }
404
    else if (mHoverItem != nullptr)
405
    {
406
        validateSpeed();
407
        popupMenu->showPopup(eventX, eventY, mHoverItem);
408
        return true;
409
    }
410
    else if (mHoverSign != nullptr)
411
    {
412
        validateSpeed();
413
        popupMenu->showPopup(eventX, eventY, mHoverSign);
414
        return true;
415
    }
416
    else if (settings.cameraMode != 0U)
417
    {
418
        if (mMap == nullptr)
419
            return false;
420
        popupMenu->showMapPopup(eventX, eventY,
421
            (mMouseX + mPixelViewX) / mMap->getTileWidth(),
422
            (mMouseY + mPixelViewY) / mMap->getTileHeight(),
423
            false);
424
        return true;
425
    }
426
    return false;
427
}
428
429
bool Viewport::leftMouseAction()
430
{
431
    const bool stopAttack = inputManager.isActionActive(
432
        InputAction::STOP_ATTACK);
433
    // Interact with some being
434
    if (mHoverBeing != nullptr)
435
    {
436
        if (!mHoverBeing->isAlive())
437
            return true;
438
439
        if (mHoverBeing->canTalk())
440
        {
441
            validateSpeed();
442
            mHoverBeing->talkTo();
443
            return true;
444
        }
445
446
        const ActorTypeT type = mHoverBeing->getType();
447
        switch (type)
448
        {
449
            case ActorType::Player:
450
                validateSpeed();
451
                if (actorManager != nullptr)
452
                {
453
#ifdef TMWA_SUPPORT
454
                    if (localPlayer != mHoverBeing || mSelfMouseHeal)
455
                        actorManager->heal(mHoverBeing);
456
#endif  // TMWA_SUPPORT
457
458
                    if (localPlayer == mHoverBeing &&
459
                        mHoverItem != nullptr)
460
                    {
461
                        localPlayer->pickUp(mHoverItem);
462
                    }
463
                    return true;
464
                }
465
                break;
466
            case ActorType::Monster:
467
            case ActorType::Npc:
468
            case ActorType::SkillUnit:
469
                if (!stopAttack)
470
                {
471
                    if (localPlayer->withinAttackRange(mHoverBeing,
472
                        false, 0) ||
473
                        inputManager.isActionActive(InputAction::ATTACK))
474
                    {
475
                        validateSpeed();
476
                        if (!mStatsReUpdated && localPlayer != mHoverBeing)
477
                        {
478
                            localPlayer->attack(mHoverBeing,
479
                                !inputManager.isActionActive(
480
                                InputAction::STOP_ATTACK),
481
                                false);
482
                            return true;
483
                        }
484
                    }
485
                    else if (!inputManager.isActionActive(
486
                             InputAction::ATTACK))
487
                    {
488
                        validateSpeed();
489
                        if (!mStatsReUpdated && localPlayer != mHoverBeing)
490
                        {
491
                            localPlayer->setGotoTarget(mHoverBeing);
492
                            return true;
493
                        }
494
                    }
495
                }
496
                break;
497
            case ActorType::FloorItem:
498
            case ActorType::Portal:
499
            case ActorType::Pet:
500
            case ActorType::Mercenary:
501
            case ActorType::Homunculus:
502
            case ActorType::Elemental:
503
                break;
504
            case ActorType::Unknown:
505
            case ActorType::Avatar:
506
            default:
507
                reportAlways("Left click on unknown actor type: %d",
508
                    CAST_S32(type))
509
                break;
510
        }
511
    }
512
    // Picks up a item if we clicked on one
513
    if (mHoverItem != nullptr)
514
    {
515
        validateSpeed();
516
        localPlayer->pickUp(mHoverItem);
517
    }
518
    else if (stopAttack)
519
    {
520
        if (mMap != nullptr)
521
        {
522
            const int mouseTileX = (mMouseX + mPixelViewX)
523
                / mMap->getTileWidth();
524
            const int mouseTileY = (mMouseY + mPixelViewY)
525
                / mMap->getTileHeight();
526
            inputManager.executeChatCommand(InputAction::PET_MOVE,
527
                strprintf("%d %d", mouseTileX, mouseTileY),
528
                nullptr);
529
        }
530
        return true;
531
    }
532
    // Just walk around
533
    else if (!inputManager.isActionActive(InputAction::ATTACK) &&
534
             localPlayer->canMove())
535
    {
536
        validateSpeed();
537
        localPlayer->stopAttack(false);
538
        localPlayer->cancelFollow();
539
        mPlayerFollowMouse = mAllowMoveByMouse;
540
        if (mPlayerFollowMouse)
541
        {
542
            // Make the player go to the mouse position
543
            followMouse();
544
        }
545
    }
546
    return false;
547
}
548
549
void Viewport::mousePressed(MouseEvent &event)
550
{
551
    if (event.getSource() != this || event.isConsumed())
552
        return;
553
554
    // Check if we are alive and kickin'
555
    if ((mMap == nullptr) || (localPlayer == nullptr))
556
        return;
557
558
    // Check if we are busy
559
    // if commented, allow context menu if npc dialog open
560
    if (PlayerInfo::isTalking())
561
    {
562
        mMouseClicked = false;
563
        return;
564
    }
565
566
    mMouseClicked = true;
567
568
    mMousePressX = event.getX();
569
    mMousePressY = event.getY();
570
    const MouseButtonT eventButton = event.getButton();
571
572
    // Right click might open a popup
573
    if (eventButton == MouseButton::RIGHT)
574
    {
575
        if (openContextMenu(event))
576
            return;
577
    }
578
579
    // If a popup is active, just remove it
580
    if (PopupManager::isPopupMenuVisible())
581
    {
582
        mPlayerFollowMouse = false;
583
        PopupManager::hidePopupMenu();
584
        return;
585
    }
586
587
    // Left click can cause different actions
588
    if (!mLongMouseClick && eventButton == MouseButton::LEFT)
589
    {
590
        if (leftMouseAction())
591
        {
592
            mPlayerFollowMouse = false;
593
            return;
594
        }
595
    }
596
    else if (eventButton == MouseButton::MIDDLE)
597
    {
598
        mPlayerFollowMouse = false;
599
        validateSpeed();
600
        // Find the being nearest to the clicked position
601
        if (actorManager != nullptr)
602
        {
603
            const int pixelX = mMousePressX + mPixelViewX;
604
            const int pixelY = mMousePressY + mPixelViewY;
605
            Being *const target = actorManager->findNearestLivingBeing(
606
                pixelX, pixelY, 20, ActorType::Monster, nullptr);
607
608
            if (target != nullptr)
609
                localPlayer->setTarget(target);
610
        }
611
    }
612
}
613
614
void Viewport::getMouseTile(int &destX, int &destY) const
615
{
616
    getMouseTile(mMouseX, mMouseY, destX, destY);
617
}
618
619
void Viewport::getMouseTile(const int x, const int y,
620
                            int &destX, int &destY) const
621
{
622
    if (mMap == nullptr)
623
        return;
624
    const int tw = mMap->getTileWidth();
625
    const int th = mMap->getTileHeight();
626
    destX = CAST_S32(x + mPixelViewX)
627
        / static_cast<float>(tw);
628
629
    if (mMap->isHeightsPresent())
630
    {
631
        const int th2 = th / 2;
632
        const int clickY = y + mPixelViewY - th2;
633
        destY = y + mPixelViewY;
634
        int newDiffY = 1000000;
635
        const int heightTiles = mainGraphics->mHeight / th;
636
        const int tileViewY = mPixelViewY / th;
637
        for (int f = tileViewY; f < tileViewY + heightTiles; f ++)
638
        {
639
            if (!mMap->getWalk(destX,
640
                f,
641
                BlockMask::WALL |
642
                BlockMask::AIR |
643
                BlockMask::WATER |
644
                BlockMask::PLAYERWALL))
645
            {
646
                continue;
647
            }
648
649
            const int offset = mMap->getHeightOffset(
650
                destX, f) * th2;
651
            const int pixelF = f * th;
652
            const int diff = abs(clickY + offset - pixelF);
653
            if (diff < newDiffY)
654
            {
655
                destY = pixelF;
656
                newDiffY = diff;
657
            }
658
        }
659
        destY /= 32;
660
    }
661
    else
662
    {
663
        destY = CAST_S32((y + mPixelViewY) / static_cast<float>(th));
664
    }
665
}
666
667
void Viewport::walkByMouse(const MouseEvent &event)
668
{
669
    if ((mMap == nullptr) || (localPlayer == nullptr))
670
        return;
671
    if (mPlayerFollowMouse
672
        && !inputManager.isActionActive(InputAction::STOP_ATTACK)
673
        && !inputManager.isActionActive(InputAction::UNTARGET))
674
    {
675
        if (!mMouseDirectionMove)
676
            mPlayerFollowMouse = false;
677
        if (mLocalWalkTime != localPlayer->getActionTime())
678
        {
679
            mLocalWalkTime = cur_time;
680
            localPlayer->unSetPickUpTarget();
681
            int playerX = localPlayer->getTileX();
682
            int playerY = localPlayer->getTileY();
683
            if (mMouseDirectionMove)
684
            {
685
                const int width = mainGraphics->mWidth / 2;
686
                const int height = mainGraphics->mHeight / 2;
687
                const float wh = static_cast<float>(width)
688
                    / static_cast<float>(height);
689
                int x = event.getX() - width;
690
                int y = event.getY() - height;
691
                if ((x == 0) && (y == 0))
692
                    return;
693
                const int x2 = abs(x);
694
                const int y2 = abs(y);
695
                const float diff = 2;
696
                int dx = 0;
697
                int dy = 0;
698
                if (x2 > y2)
699
                {
700
                    if (y2 != 0 &&
701
                        static_cast<float>(x2) / static_cast<float>(y2) /
702
                        wh > diff)
703
                    {
704
                        y = 0;
705
                    }
706
                }
707
                else
708
                {
709
                    if ((x2 != 0) && y2 * wh / x2 > diff)
710
                        x = 0;
711
                }
712
                if (x > 0)
713
                    dx = 1;
714
                else if (x < 0)
715
                    dx = -1;
716
                if (y > 0)
717
                    dy = 1;
718
                else if (y < 0)
719
                    dy = -1;
720
721
                if (mMap->getWalk(playerX + dx,
722
                    playerY + dy,
723
                    BlockMask::WALL |
724
                    BlockMask::AIR |
725
                    BlockMask::WATER |
726
                    BlockMask::PLAYERWALL))
727
                {
728
                    localPlayer->navigateTo(playerX + dx, playerY + dy);
729
                }
730
                else
731
                {
732
                    if ((dx != 0) && (dy != 0))
733
                    {
734
                        // try avoid diagonal collision
735
                        if (x2 > y2)
736
                        {
737
                            if (mMap->getWalk(playerX + dx,
738
                                playerY,
739
                                BlockMask::WALL |
740
                                BlockMask::AIR |
741
                                BlockMask::WATER |
742
                                BlockMask::PLAYERWALL))
743
                            {
744
                                dy = 0;
745
                            }
746
                            else
747
                            {
748
                                dx = 0;
749
                            }
750
                        }
751
                        else
752
                        {
753
                            if (mMap->getWalk(playerX,
754
                                playerY + dy,
755
                                BlockMask::WALL |
756
                                BlockMask::AIR |
757
                                BlockMask::WATER |
758
                                BlockMask::PLAYERWALL))
759
                            {
760
                                dx = 0;
761
                            }
762
                            else
763
                            {
764
                                dy = 0;
765
                            }
766
                        }
767
                    }
768
                    else
769
                    {
770
                        // try avoid vertical or horisontal collision
771
                        if (dx == 0)
772
                        {
773
                            if (mMap->getWalk(playerX + 1,
774
                                playerY + dy,
775
                                BlockMask::WALL |
776
                                BlockMask::AIR |
777
                                BlockMask::WATER |
778
                                BlockMask::PLAYERWALL))
779
                            {
780
                                dx = 1;
781
                            }
782
                            if (mMap->getWalk(playerX - 1,
783
                                playerY + dy,
784
                                BlockMask::WALL |
785
                                BlockMask::AIR |
786
                                BlockMask::WATER |
787
                                BlockMask::PLAYERWALL))
788
                            {
789
                                dx = -1;
790
                            }
791
                        }
792
                        if (dy == 0)
793
                        {
794
                            if (mMap->getWalk(playerX + dx,
795
                                playerY + 1,
796
                                BlockMask::WALL |
797
                                BlockMask::AIR |
798
                                BlockMask::WATER |
799
                                BlockMask::PLAYERWALL))
800
                            {
801
                                dy = 1;
802
                            }
803
                            if (mMap->getWalk(playerX + dx,
804
                                playerY - 1,
805
                                BlockMask::WALL |
806
                                BlockMask::AIR |
807
                                BlockMask::WATER |
808
                                BlockMask::PLAYERWALL))
809
                            {
810
                                dy = -1;
811
                            }
812
                        }
813
                    }
814
                    localPlayer->navigateTo(playerX + dx, playerY + dy);
815
                }
816
            }
817
            else
818
            {
819
                int destX;
820
                int destY;
821
                getMouseTile(event.getX(), event.getY(),
822
                    destX, destY);
823
                if (playerX != destX || playerY != destY)
824
                {
825
                    if (!localPlayer->navigateTo(destX, destY))
826
                    {
827
                        if (playerX > destX)
828
                            playerX --;
829
                        else if (playerX < destX)
830
                            playerX ++;
831
                        if (playerY > destY)
832
                            playerY --;
833
                        else if (playerY < destY)
834
                            playerY ++;
835
                        if (mMap->getWalk(playerX, playerY, 0))
836
                            localPlayer->navigateTo(playerX, playerY);
837
                    }
838
                }
839
            }
840
        }
841
    }
842
}
843
844
void Viewport::mouseDragged(MouseEvent &event)
845
{
846
    if (event.getSource() != this || event.isConsumed())
847
    {
848
        mPlayerFollowMouse = false;
849
        return;
850
    }
851
    if (mAllowMoveByMouse &&
852
        mMouseClicked &&
853
        (localPlayer != nullptr) &&
854
        localPlayer->canMove())
855
    {
856
        if (abs(event.getX() - mMousePressX) > 32
857
            || abs(event.getY() - mMousePressY) > 32)
858
        {
859
            mPlayerFollowMouse = true;
860
        }
861
862
        walkByMouse(event);
863
    }
864
}
865
866
void Viewport::mouseReleased(MouseEvent &event)
867
{
868
    mPlayerFollowMouse = false;
869
    mLocalWalkTime = -1;
870
    if (mLongMouseClick && mMouseClicked)
871
    {
872
        mMouseClicked = false;
873
        if (event.getSource() != this || event.isConsumed())
874
            return;
875
        const MouseButtonT eventButton = event.getButton();
876
        if (eventButton == MouseButton::LEFT)
877
        {
878
            // long button press
879
            if ((gui != nullptr) && gui->isLongPress())
880
            {
881
                if (openContextMenu(event))
882
                {
883
                    gui->resetClickCount();
884
                    return;
885
                }
886
            }
887
            else
888
            {
889
                if (leftMouseAction())
890
                    return;
891
            }
892
            walkByMouse(event);
893
        }
894
    }
895
}
896
897
void Viewport::optionChanged(const std::string &name)
898
{
899
    if (name == "ScrollLaziness")
900
        mScrollLaziness = config.getIntValue("ScrollLaziness");
901
    else if (name == "ScrollRadius")
902
        mScrollRadius = config.getIntValue("ScrollRadius");
903
    else if (name == "showBeingPopup")
904
        mShowBeingPopup = config.getBoolValue("showBeingPopup");
905
    else if (name == "selfMouseHeal")
906
        mSelfMouseHeal = config.getBoolValue("selfMouseHeal");
907
    else if (name == "enableLazyScrolling")
908
        mEnableLazyScrolling = config.getBoolValue("enableLazyScrolling");
909
    else if (name == "mouseDirectionMove")
910
        mMouseDirectionMove = config.getBoolValue("mouseDirectionMove");
911
    else if (name == "longmouseclick")
912
        mLongMouseClick = config.getBoolValue("longmouseclick");
913
    else if (name == "allowMoveByMouse")
914
        mAllowMoveByMouse = config.getBoolValue("allowMoveByMouse");
915
}
916
917
void Viewport::mouseMoved(MouseEvent &event)
918
{
919
    // Check if we are on the map
920
    if (mMap == nullptr ||
921
        localPlayer == nullptr ||
922
        actorManager == nullptr)
923
    {
924
        return;
925
    }
926
927
    if (mMouseDirectionMove)
928
        mPlayerFollowMouse = false;
929
930
    const int x = mMouseX + mPixelViewX;
931
    const int y = mMouseY + mPixelViewY;
932
933
    ActorTypeT type = ActorType::Unknown;
934
    mHoverBeing = actorManager->findBeingByPixel(x, y, AllPlayers_true);
935
    if (mHoverBeing != nullptr)
936
        type = mHoverBeing->getType();
937
    if ((mHoverBeing != nullptr)
938
        && (type == ActorType::Player
939
        || type == ActorType::Npc
940
        || type == ActorType::Homunculus
941
        || type == ActorType::Mercenary
942
        || type == ActorType::Pet))
943
    {
944
        PopupManager::hideTextPopup();
945
        if (mShowBeingPopup && (beingPopup != nullptr))
946
            beingPopup->show(mMouseX, mMouseY, mHoverBeing);
947
    }
948
    else
949
    {
950
        PopupManager::hideBeingPopup();
951
    }
952
953
    mHoverItem = actorManager->findItem(x / mMap->getTileWidth(),
954
        y / mMap->getTileHeight());
955
956
    if ((mHoverBeing == nullptr) && (mHoverItem == nullptr))
957
    {
958
        const SpecialLayer *const specialLayer = mMap->getSpecialLayer();
959
        if (specialLayer != nullptr)
960
        {
961
            const int mouseTileX = (mMouseX + mPixelViewX)
962
                / mMap->getTileWidth();
963
            const int mouseTileY = (mMouseY + mPixelViewY)
964
                / mMap->getTileHeight();
965
966
            mHoverSign = specialLayer->getTile(mouseTileX, mouseTileY);
967
            if (mHoverSign != nullptr &&
968
                mHoverSign->getType() != MapItemType::EMPTY)
969
            {
970
                if (!mHoverSign->getComment().empty())
971
                {
972
                    PopupManager::hideBeingPopup();
973
                    if (textPopup != nullptr)
974
                    {
975
                        textPopup->show(mMouseX, mMouseY,
976
                            mHoverSign->getComment());
977
                    }
978
                }
979
                else
980
                {
981
                    if (PopupManager::isTextPopupVisible())
982
                        PopupManager::hideTextPopup();
983
                }
984
                gui->setCursorType(Cursor::CURSOR_UP);
985
                return;
986
            }
987
        }
988
    }
989
    if (!event.isConsumed() &&
990
        PopupManager::isTextPopupVisible())
991
    {
992
        PopupManager::hideTextPopup();
993
    }
994
995
    if (mHoverBeing != nullptr)
996
    {
997
        switch (type)
998
        {
999
            case ActorType::Npc:
1000
            case ActorType::Monster:
1001
            case ActorType::Portal:
1002
            case ActorType::Pet:
1003
            case ActorType::Mercenary:
1004
            case ActorType::Homunculus:
1005
            case ActorType::SkillUnit:
1006
            case ActorType::Elemental:
1007
                gui->setCursorType(mHoverBeing->getHoverCursor());
1008
                break;
1009
1010
            case ActorType::Avatar:
1011
            case ActorType::FloorItem:
1012
            case ActorType::Unknown:
1013
            case ActorType::Player:
1014
            default:
1015
                gui->setCursorType(Cursor::CURSOR_POINTER);
1016
                break;
1017
        }
1018
    }
1019
    // Item mouseover
1020
    else if (mHoverItem != nullptr)
1021
    {
1022
        gui->setCursorType(mHoverItem->getHoverCursor());
1023
    }
1024
    else
1025
    {
1026
        gui->setCursorType(Cursor::CURSOR_POINTER);
1027
    }
1028
}
1029
1030
void Viewport::toggleMapDrawType()
1031
{
1032
    settings.mapDrawType = static_cast<MapTypeT>(
1033
        CAST_S32(settings.mapDrawType) + 1);
1034
    if (settings.mapDrawType > MapType::BLACKWHITE)
1035
        settings.mapDrawType = MapType::NORMAL;
1036
    if (mMap != nullptr)
1037
        mMap->setDrawLayersFlags(settings.mapDrawType);
1038
}
1039
1040
void Viewport::toggleCameraMode()
1041
{
1042
    settings.cameraMode ++;
1043
    if (settings.cameraMode > 1)
1044
        settings.cameraMode = 0;
1045
    if (settings.cameraMode == 0U)
1046
    {
1047
        mCameraRelativeX = 0;
1048
        mCameraRelativeY = 0;
1049
        updateMidVars();
1050
    }
1051
    UpdateStatusListener::distributeEvent();
1052
}
1053
1054
void Viewport::clearHover(const ActorSprite *const actor)
1055
{
1056
    if (mHoverBeing == actor)
1057
        mHoverBeing = nullptr;
1058
1059
    if (mHoverItem == actor)
1060
        mHoverItem = nullptr;
1061
}
1062
1063
void Viewport::cleanHoverItems()
1064
{
1065
    mHoverBeing = nullptr;
1066
    mHoverItem = nullptr;
1067
    mHoverSign = nullptr;
1068
}
1069
1070
void Viewport::moveCamera(const int dx, const int dy)
1071
{
1072
    mCameraRelativeX += dx;
1073
    mCameraRelativeY += dy;
1074
    updateMidVars();
1075
}
1076
1077
void Viewport::moveCameraToActor(const BeingId actorId,
1078
                                 const int x, const int y)
1079
{
1080
    if ((localPlayer == nullptr) || (actorManager == nullptr))
1081
        return;
1082
1083
    const Actor *const actor = actorManager->findBeing(actorId);
1084
    if (actor == nullptr)
1085
        return;
1086
    settings.cameraMode = 1;
1087
    mCameraRelativeX = actor->mPixelX - localPlayer->mPixelX + x;
1088
    mCameraRelativeY = actor->mPixelY - localPlayer->mPixelY + y;
1089
    updateMidVars();
1090
}
1091
1092
void Viewport::moveCameraToPosition(const int x, const int y)
1093
{
1094
    if (localPlayer == nullptr)
1095
        return;
1096
1097
    settings.cameraMode = 1;
1098
    mCameraRelativeX = x - localPlayer->mPixelX;
1099
    mCameraRelativeY = y - localPlayer->mPixelY;
1100
    updateMidVars();
1101
}
1102
1103
void Viewport::moveCameraRelative(const int x, const int y)
1104
{
1105
    settings.cameraMode = 1;
1106
    mCameraRelativeX += x;
1107
    mCameraRelativeY += y;
1108
    updateMidVars();
1109
}
1110
1111
void Viewport::returnCamera()
1112
{
1113
    settings.cameraMode = 0;
1114
    mCameraRelativeX = 0;
1115
    mCameraRelativeY = 0;
1116
    updateMidVars();
1117
}
1118
1119
void Viewport::validateSpeed()
1120
{
1121
    if (!inputManager.isActionActive(InputAction::TARGET_ATTACK) &&
1122
        !inputManager.isActionActive(InputAction::ATTACK))
1123
    {
1124
        if (Game::instance() != nullptr)
1125
            Game::instance()->setValidSpeed();
1126
    }
1127
}
1128
1129
void Viewport::updateMidVars()
1130
{
1131
    mMidTileX = (mainGraphics->mWidth + mScrollCenterOffsetX) / 2
1132
        - mCameraRelativeX;
1133
    mMidTileY = (mainGraphics->mHeight + mScrollCenterOffsetY) / 2
1134
        - mCameraRelativeY;
1135
}
1136
1137
void Viewport::updateMaxVars()
1138
{
1139
    if (mMap == nullptr)
1140
        return;
1141
    mViewXmax = mMap->getWidth() * mMap->getTileWidth()
1142
        - mainGraphics->mWidth;
1143
    mViewYmax = mMap->getHeight() * mMap->getTileHeight()
1144
        - mainGraphics->mHeight;
1145
}
1146
1147
void Viewport::videoResized()
1148
{
1149
    updateMidVars();
1150
    updateMaxVars();
1151
    if (mMap != nullptr)
1152
        mMap->screenResized();
1153
2
}