diff options
author | Andrei Karas <akaras@inbox.ru> | 2015-11-08 16:05:17 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2015-11-08 23:04:34 +0300 |
commit | d9c2fc9b34fbb291b4d42bb1070fdff07cd56914 (patch) | |
tree | ec40e03113e8ef0aef5e80af57a5a18d90a948c9 | |
parent | 4fb22939a08cc361cbe250a8de9001a92e3d5b64 (diff) | |
download | manaverse-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.tar.gz manaverse-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.tar.bz2 manaverse-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.tar.xz manaverse-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.zip |
Move code for converting mouse position to tile into separate function.
-rw-r--r-- | src/gui/viewport.cpp | 75 | ||||
-rw-r--r-- | src/gui/viewport.h | 5 |
2 files changed, 47 insertions, 33 deletions
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<int>(x + mPixelViewX) + / static_cast<float>(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<int>((y + mPixelViewY) / static_cast<float>(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<int>((event.getX() + mPixelViewX) - / static_cast<float>(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<int>((event.getY() + mPixelViewY) - / static_cast<float>(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. */ |