summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-11-08 16:05:17 +0300
committerAndrei Karas <akaras@inbox.ru>2015-11-08 23:04:34 +0300
commitd9c2fc9b34fbb291b4d42bb1070fdff07cd56914 (patch)
treeec40e03113e8ef0aef5e80af57a5a18d90a948c9
parent4fb22939a08cc361cbe250a8de9001a92e3d5b64 (diff)
downloadplus-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.tar.gz
plus-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.tar.bz2
plus-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.tar.xz
plus-d9c2fc9b34fbb291b4d42bb1070fdff07cd56914.zip
Move code for converting mouse position to tile into separate function.
-rw-r--r--src/gui/viewport.cpp75
-rw-r--r--src/gui/viewport.h5
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.
*/