From d9c2fc9b34fbb291b4d42bb1070fdff07cd56914 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 8 Nov 2015 16:05:17 +0300 Subject: Move code for converting mouse position to tile into separate function. --- src/gui/viewport.cpp | 75 +++++++++++++++++++++++++++++----------------------- src/gui/viewport.h | 5 ++++ 2 files changed, 47 insertions(+), 33 deletions(-) (limited to 'src/gui') diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index c0c0e4803..745b11bed 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -568,6 +568,45 @@ void Viewport::mousePressed(MouseEvent &event) } } +void Viewport::getMouseTile(const int x, int y, + int &destX, int &destY) +{ + const int tw = mMap->getTileWidth(); + const int th = mMap->getTileHeight(); + destX = static_cast(x + mPixelViewX) + / static_cast(tw); + + if (mMap->isHeightsPresent()) + { + const int th2 = th / 2; + const int clickY = y + mPixelViewY - th2; + destY = y + mPixelViewY; + int newDiffY = 1000000; + const int heightTiles = mainGraphics->mHeight / th; + const int tileViewY = mPixelViewY / th; + for (int f = tileViewY; f < tileViewY + heightTiles; f ++) + { + if (!mMap->getWalk(destX, f)) + continue; + + const int offset = mMap->getHeightOffset( + destX, f) * th2; + const int pixelF = f * th; + const int diff = abs(clickY + offset - pixelF); + if (diff < newDiffY) + { + destY = pixelF; + newDiffY = diff; + } + } + destY /= 32; + } + else + { + destY = static_cast((y + mPixelViewY) / static_cast(th)); + } +} + void Viewport::walkByMouse(const MouseEvent &event) { if (!mMap || !localPlayer) @@ -668,40 +707,10 @@ void Viewport::walkByMouse(const MouseEvent &event) } else { - const int destX = static_cast((event.getX() + mPixelViewX) - / static_cast(mMap->getTileWidth())); + int destX; int destY; - - if (mMap->isHeightsPresent()) - { - const int clickY = event.getY() + mPixelViewY - 16; - destY = event.getY() + mPixelViewY; - int newDiffY = 1000000; - const int heightTiles = mainGraphics->mHeight - / mMap->getTileHeight(); - const int tileViewY = mPixelViewY / 32; - for (int f = tileViewY; f < tileViewY + heightTiles; f ++) - { - if (!mMap->getWalk(destX, f)) - continue; - - const int offset = mMap->getHeightOffset( - destX, f) * 16; - const int pixelF = f * 32; - const int diff = abs(clickY + offset - pixelF); - if (diff < newDiffY) - { - destY = pixelF; - newDiffY = diff; - } - } - destY /= 32; - } - else - { - destY = static_cast((event.getY() + mPixelViewY) - / static_cast(mMap->getTileHeight())); - } + getMouseTile(event.getX(), event.getY(), + destX, destY); if (playerX != destX || playerY != destY) { if (!localPlayer->navigateTo(destX, destY)) diff --git a/src/gui/viewport.h b/src/gui/viewport.h index fa2dd7508..f682cd96f 100644 --- a/src/gui/viewport.h +++ b/src/gui/viewport.h @@ -204,6 +204,11 @@ class Viewport final : public WindowContainer, void walkByMouse(const MouseEvent &event); + void getMouseTile(const int x, + const int y, + int &destX, + int &destY); + /** * Make the player go to the mouse position. */ -- cgit v1.2.3-70-g09d2