From 64f4e1132355eaac4f5594bee516b56adb2ad083 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 6 Sep 2011 19:54:39 +0300 Subject: Add ability to move by clicking on minimap. --- src/gui/minimap.cpp | 67 +++++++++++++++++++++++++++++++++++------------------ src/gui/minimap.h | 4 ++++ 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index c3a4417f6..a16da2b65 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -51,7 +51,9 @@ Minimap::Minimap(): mMapImage(0), mWidthProportion(0.5), mHeightProportion(0.5), - mCustomMapImage(false) + mCustomMapImage(false), + mMapOriginX(0), + mMapOriginY(0) { setWindowName("Minimap"); mShow = config.getValueBool(getWindowName() + "Show", true); @@ -215,8 +217,11 @@ void Minimap::draw(gcn::Graphics *graphics) graphics->pushClipArea(a); - int mapOriginX = 0; - int mapOriginY = 0; + if (!actorSpriteManager) + return; + + mMapOriginX = 0; + mMapOriginY = 0; if (mMapImage) { @@ -224,33 +229,30 @@ void Minimap::draw(gcn::Graphics *graphics) mMapImage->mBounds.h > a.height) { const Vector &p = player_node->getPosition(); - mapOriginX = ((a.width) / 2) - static_cast((p.x + mMapOriginX = ((a.width) / 2) - static_cast((p.x + viewport->getCameraRelativeX()) * static_cast( mWidthProportion)) / 32; - mapOriginY = ((a.height) / 2) - static_cast((p.y + mMapOriginY = ((a.height) / 2) - static_cast((p.y + viewport->getCameraRelativeX()) * static_cast( mHeightProportion)) / 32; const int minOriginX = a.width - mMapImage->mBounds.w; const int minOriginY = a.height - mMapImage->mBounds.h; - if (mapOriginX < minOriginX) - mapOriginX = minOriginX; - if (mapOriginY < minOriginY) - mapOriginY = minOriginY; - if (mapOriginX > 0) - mapOriginX = 0; - if (mapOriginY > 0) - mapOriginY = 0; + if (mMapOriginX < minOriginX) + mMapOriginX = minOriginX; + if (mMapOriginY < minOriginY) + mMapOriginY = minOriginY; + if (mMapOriginX > 0) + mMapOriginX = 0; + if (mMapOriginY > 0) + mMapOriginY = 0; } - graph->drawImage(mMapImage, mapOriginX, mapOriginY); + graph->drawImage(mMapImage, mMapOriginX, mMapOriginY); } - if (!actorSpriteManager) - return; - const ActorSprites &actors = actorSpriteManager->getAll(); for (ActorSpritesConstIterator it = actors.begin(), it_end = actors.end(); @@ -313,9 +315,9 @@ void Minimap::draw(gcn::Graphics *graphics) graphics->fillRectangle(gcn::Rectangle( static_cast(pos.x * mWidthProportion) / 32 - + mapOriginX - offsetWidth, + + mMapOriginX - offsetWidth, static_cast(pos.y * mHeightProportion) / 32 - + mapOriginY - offsetHeight, + + mMapOriginY - offsetHeight, dotSize, dotSize)); } @@ -351,9 +353,9 @@ void Minimap::draw(gcn::Graphics *graphics) graphics->fillRectangle(gcn::Rectangle( static_cast(member->getX() - * mWidthProportion) + mapOriginX - offsetWidth, + * mWidthProportion) + mMapOriginX - offsetWidth, static_cast(member->getY() - * mHeightProportion) + mapOriginY - offsetHeight, + * mHeightProportion) + mMapOriginY - offsetHeight, 2, 2)); } ++ it; @@ -367,10 +369,10 @@ void Minimap::draw(gcn::Graphics *graphics) int x = static_cast((pos.x - (graph->getWidth() / 2) + viewport->getCameraRelativeX()) - * mWidthProportion) / 32 + mapOriginX; + * mWidthProportion) / 32 + mMapOriginX; int y = static_cast((pos.y - (graph->getHeight() / 2) + viewport->getCameraRelativeY()) - * mHeightProportion) / 32 + mapOriginY; + * mHeightProportion) / 32 + mMapOriginY; const int w = static_cast(static_cast( graph->getWidth()) * mWidthProportion / 32); @@ -396,3 +398,22 @@ void Minimap::draw(gcn::Graphics *graphics) graphics->drawRectangle(gcn::Rectangle(x, y, w, h)); graphics->popClipArea(); } + +void Minimap::mouseReleased(gcn::MouseEvent &event) +{ + gcn::Window::mouseReleased(event); + + if (!player_node) + return; + + if (event.getButton() == gcn::MouseEvent::LEFT) + { + const gcn::Rectangle a = getChildrenArea(); + const int x = event.getX() - a.x; + const int y = event.getY() - a.y; + + player_node->navigateTo((x - mMapOriginX + mWidthProportion) + / mWidthProportion, (y - mMapOriginY + mHeightProportion) + / mHeightProportion); + } +} diff --git a/src/gui/minimap.h b/src/gui/minimap.h index 33e0f14ee..86996f51c 100644 --- a/src/gui/minimap.h +++ b/src/gui/minimap.h @@ -58,12 +58,16 @@ class Minimap : public Window */ void draw(gcn::Graphics *graphics); + void mouseReleased(gcn::MouseEvent &event); + private: Image *mMapImage; float mWidthProportion; float mHeightProportion; static bool mShow; bool mCustomMapImage; + int mMapOriginX; + int mMapOriginY; }; extern Minimap *minimap; -- cgit v1.2.3-70-g09d2