From 53ead20de5ddf3fb4fa1815e319914c2930b0810 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 6 Feb 2012 23:35:46 -0200 Subject: [PATCH] restore map clicking --- modules/game/game.otmod | 1 + .../game_combatcontrols/combatcontrols.lua | 7 +++- .../game_combatcontrols/combatcontrols.otmod | 5 +++ src/otclient/core/mapview.cpp | 39 ++++++++++++++++++ src/otclient/core/mapview.h | 1 + src/otclient/ui/uimap.cpp | 41 +------------------ 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/modules/game/game.otmod b/modules/game/game.otmod index ca0ed02b..a23f5cdf 100644 --- a/modules/game/game.otmod +++ b/modules/game/game.otmod @@ -13,6 +13,7 @@ Module - game_console - game_outfit - game_containers + - game_combatcontrols onLoad: | dofile 'game' diff --git a/modules/game_combatcontrols/combatcontrols.lua b/modules/game_combatcontrols/combatcontrols.lua index ed909851..4494fe70 100644 --- a/modules/game_combatcontrols/combatcontrols.lua +++ b/modules/game_combatcontrols/combatcontrols.lua @@ -1,8 +1,13 @@ CombatControls = {} +local combatControlsButton + function CombatControls.init() + combatControlsButton = TopMenu.addGameButton('combatControlsButton', 'Combat Controls', 'combatcontrols.png', CombatControls.toggle) end function CombatControls.terminate() -end \ No newline at end of file + combatControlsButton:destroy() + combatControlsButton = nil +end diff --git a/modules/game_combatcontrols/combatcontrols.otmod b/modules/game_combatcontrols/combatcontrols.otmod index c0438b8d..19a54913 100644 --- a/modules/game_combatcontrols/combatcontrols.otmod +++ b/modules/game_combatcontrols/combatcontrols.otmod @@ -3,5 +3,10 @@ Module description: Combat controls window author: OTClient team website: https://github.com/edubart/otclient + onLoad: | dofile 'combatcontrols' + CombatControls.init() + + onUnload: | + CombatControls.terminate() diff --git a/src/otclient/core/mapview.cpp b/src/otclient/core/mapview.cpp index c277580f..fb19b669 100644 --- a/src/otclient/core/mapview.cpp +++ b/src/otclient/core/mapview.cpp @@ -522,6 +522,45 @@ Position MapView::getCameraPosition() return m_customCameraPosition; } +TilePtr MapView::getTile(const Point& mousePos, const Rect& mapRect) +{ + Point relativeMousePos = mousePos - mapRect.topLeft(); + Size visibleSize = getVisibleSize(); + Position cameraPosition = getCameraPosition(); + + float scaleFactor = m_tileSize / (float)Otc::TILE_PIXELS; + + + + float horizontalStretchFactor = visibleSize.width() / (float)mapRect.width(); + float verticalStretchFactor = visibleSize.height() / (float)mapRect.height(); + + Point tilePos2D = Point(relativeMousePos.x * horizontalStretchFactor, relativeMousePos.y * verticalStretchFactor); + + if(m_followingCreature) + tilePos2D += m_followingCreature->getWalkOffset() * scaleFactor; + tilePos2D /= m_tileSize; + + Position tilePos = Position(1 + (int)tilePos2D.x - m_virtualCenterOffset.x, 1 + (int)tilePos2D.y - m_virtualCenterOffset.y, 0) + cameraPosition; + if(!tilePos.isValid()) + return nullptr; + + // we must check every floor, from top to bottom to check for a clickable tile + TilePtr tile; + tilePos.coveredUp(tilePos.z - m_cachedFirstVisibleFloor); + for(int i = m_cachedFirstVisibleFloor; i <= m_cachedLastVisibleFloor; i++) { + tile = g_map.getTile(tilePos); + if(tile && tile->isClickable()) + break; + tilePos.coveredDown(); + } + + if(!tile || !tile->isClickable()) + return nullptr; + + return tile; +} + Point MapView::transformPositionTo2D(const Position& position) { Position cameraPosition = getCameraPosition(); diff --git a/src/otclient/core/mapview.h b/src/otclient/core/mapview.h index 982490b9..53e893cd 100644 --- a/src/otclient/core/mapview.h +++ b/src/otclient/core/mapview.h @@ -80,6 +80,7 @@ public: int getFirstVisibleFloor(); int getLastVisibleFloor(); Position getCameraPosition(); + TilePtr getTile(const Point& mousePos, const Rect& mapRect); Size getVisibleDimension() { return m_visibleDimension; } Size getVisibleSize() { return m_visibleDimension * m_tileSize; } CreaturePtr getFollowingCreature() { return m_followingCreature; } diff --git a/src/otclient/ui/uimap.cpp b/src/otclient/ui/uimap.cpp index eccd9269..411ec563 100644 --- a/src/otclient/ui/uimap.cpp +++ b/src/otclient/ui/uimap.cpp @@ -103,48 +103,11 @@ void UIMap::setCameraPosition(const Position& pos) TilePtr UIMap::getTile(const Point& mousePos) { - /* if(!m_mapRect.contains(mousePos)) return nullptr; - // Get tile position - Point relativeStretchMousePos = mousePos - m_mapRect.topLeft(); - - LocalPlayerPtr localPlayer = g_game.getLocalPlayer(); - if(localPlayer) - relativeStretchMousePos += localPlayer->getWalkOffset(); - - Size mapSize(g_map.getVibibleSize().width() * Otc::TILE_PIXELS, g_map.getVibibleSize().height() * Otc::TILE_PIXELS); - - PointF stretchFactor(m_mapRect.width() / (float)mapSize.width(), m_mapRect.height() / (float)mapSize.height()); - PointF relativeMousePos = PointF(relativeStretchMousePos.x, relativeStretchMousePos.y) / stretchFactor; - - PointF tilePosF = relativeMousePos / Otc::TILE_PIXELS; - Position tilePos = Position(1 + (int)tilePosF.x - g_map.getCentralOffset().x, 1 + (int)tilePosF.y - g_map.getCentralOffset().y, 0) + g_map.getCentralPosition(); - if(!tilePos.isValid()) - return nullptr; - - // Get tile - TilePtr tile = nullptr; - - // We must check every floor, from top to bottom to check for a clickable tile - int firstFloor = g_map.getFirstVisibleFloor(); - tilePos.coveredUp(tilePos.z - firstFloor); - for(int i = firstFloor; i <= Map::MAX_Z; i++) { - tile = g_map.getTile(tilePos); - if(tile && tile->isClickable()) - break; - tilePos.coveredDown(); - } - - // todo: get creature, using walkOffset etc. - - if(!tile || !tile->isClickable()) - return nullptr; - - return tile; - */ - return nullptr; + //TODO: move MapView code to UIMap and rework this shit + return m_mapView->getTile(mousePos, m_mapRect); } void UIMap::onGeometryChange(const Rect& oldRect, const Rect& newRect)