diff --git a/modules/game_interface/widgets/uigamemap.lua b/modules/game_interface/widgets/uigamemap.lua index 0d341144..3f0b7598 100644 --- a/modules/game_interface/widgets/uigamemap.lua +++ b/modules/game_interface/widgets/uigamemap.lua @@ -58,10 +58,9 @@ function UIGameMap:onMouseRelease(mousePosition, mouseButton) end local tile = self:getTile(mousePosition) - if tile == nil then return false end local localPlayerPos = g_game.getLocalPlayer():getPosition() - local autoWalkPos = tile:getPosition() + local autoWalkPos = self:getPosition(mousePosition) if autoWalkPos.z ~= localPlayerPos.z then local dz = autoWalkPos.z - localPlayerPos.z autoWalkPos.x = autoWalkPos.x + dz @@ -69,10 +68,17 @@ function UIGameMap:onMouseRelease(mousePosition, mouseButton) autoWalkPos.z = localPlayerPos.z end - local lookThing = tile:getTopLookThing() - local useThing = tile:getTopUseThing() - local creatureThing = tile:getTopCreature() - local multiUseThing = tile:getTopMultiUseThing() + local lookThing + local useThing + local creatureThing + local multiUseThing + + if tile then + lookThing = tile:getTopLookThing() + useThing = tile:getTopUseThing() + creatureThing = tile:getTopCreature() + multiUseThing = tile:getTopMultiUseThing() + end local ret = modules.game_interface.processMouseAction(mousePosition, mouseButton, autoWalkPos, lookThing, useThing, creatureThing, multiUseThing) if ret then diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 955128b1..dc69727a 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -494,6 +494,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("getFollowingCreature", &UIMap::getFollowingCreature); g_lua.bindClassMemberFunction("getDrawFlags", &UIMap::getDrawFlags); g_lua.bindClassMemberFunction("getCameraPosition", &UIMap::getCameraPosition); + g_lua.bindClassMemberFunction("getPosition", &UIMap::getPosition); g_lua.bindClassMemberFunction("getTile", &UIMap::getTile); g_lua.bindClassMemberFunction("getMaxZoomIn", &UIMap::getMaxZoomIn); g_lua.bindClassMemberFunction("getMaxZoomOut", &UIMap::getMaxZoomOut); diff --git a/src/otclient/mapview.cpp b/src/otclient/mapview.cpp index 91dd0d01..b8fe8bc6 100644 --- a/src/otclient/mapview.cpp +++ b/src/otclient/mapview.cpp @@ -617,48 +617,6 @@ Position MapView::getCameraPosition() return m_customCameraPosition; } -TilePtr MapView::getTile(const Point& mousePos, const Rect& mapRect) -{ - Point relativeMousePos = mousePos - mapRect.topLeft(); - Size visibleSize = m_visibleDimension * m_tileSize; - Position cameraPosition = getCameraPosition(); - - // if we have no camera, its impossible to get the tile - if(!cameraPosition.isValid()) - return nullptr; - - 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(isFollowingCreature()) - tilePos2D += m_followingCreature->getWalkOffset() * scaleFactor; - tilePos2D /= m_tileSize; - - Position tilePos = Position(1 + (int)tilePos2D.x - m_visibleCenterOffset.x, 1 + (int)tilePos2D.y - m_visibleCenterOffset.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; -} - void MapView::setDrawMinimapColors(bool enable) { if(m_drawMinimapColors == enable) diff --git a/src/otclient/mapview.h b/src/otclient/mapview.h index ef72231e..e01d4aab 100644 --- a/src/otclient/mapview.h +++ b/src/otclient/mapview.h @@ -67,6 +67,10 @@ public: // map dimension related void setVisibleDimension(const Size& visibleDimension); Size getVisibleDimension() { return m_visibleDimension; } + int getTileSize() { return m_tileSize; } + Point getVisibleCenterOffset() { return m_visibleCenterOffset; } + int getCachedFirstVisibleFloor() { return m_cachedFirstVisibleFloor; } + int getCachedLastVisibleFloor() { return m_cachedLastVisibleFloor; } // view mode related void setViewMode(ViewMode viewMode); @@ -100,8 +104,6 @@ public: void setShader(const PainterShaderProgramPtr& shader) { m_shader = shader; } PainterShaderProgramPtr getShader() { return m_shader; } - // get tile - TilePtr getTile(const Point& mousePos, const Rect& mapRect); MapViewPtr asMapView() { return static_self_cast(); } diff --git a/src/otclient/uimap.cpp b/src/otclient/uimap.cpp index 69775484..76c8f509 100644 --- a/src/otclient/uimap.cpp +++ b/src/otclient/uimap.cpp @@ -116,18 +116,57 @@ void UIMap::setKeepAspectRatio(bool enable) updateMapSize(); } -TilePtr UIMap::getTile(const Point& mousePos) +Position UIMap::getPosition(const Point& mousePos) { - /* - * Known Issue: If you move a container widget into the map rect - * and you move an item onto itself it will allow this to execute - * still dropping the item on the ground. - */ if(!m_mapRect.contains(mousePos)) + return Position(); + + Point relativeMousePos = mousePos - m_mapRect.topLeft(); + Size visibleSize = getVisibleDimension() * m_mapView->getTileSize(); + Position cameraPosition = getCameraPosition(); + + // if we have no camera, its impossible to get the tile + if(!cameraPosition.isValid()) + return Position(); + + float scaleFactor = m_mapView->getTileSize() / (float)Otc::TILE_PIXELS; + float horizontalStretchFactor = visibleSize.width() / (float)m_mapRect.width(); + float verticalStretchFactor = visibleSize.height() / (float)m_mapRect.height(); + + Point tilePos2D = Point(relativeMousePos.x * horizontalStretchFactor, relativeMousePos.y * verticalStretchFactor); + + if(m_mapView->isFollowingCreature()) + tilePos2D += getFollowingCreature()->getWalkOffset() * scaleFactor; + tilePos2D /= m_mapView->getTileSize(); + + Point visibleCenterOffset = m_mapView->getVisibleCenterOffset(); + Position position = Position(1 + (int)tilePos2D.x - visibleCenterOffset.x, 1 + (int)tilePos2D.y - visibleCenterOffset.y, 0) + cameraPosition; + if(!position.isValid()) + return Position(); + + return position; +} + +TilePtr UIMap::getTile(const Point& mousePos) +{ + Position tilePos = getPosition(mousePos); + 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_mapView->getCachedFirstVisibleFloor()); + for(int i = m_mapView->getCachedFirstVisibleFloor(); i <= m_mapView->getCachedLastVisibleFloor(); i++) { + tile = g_map.getTile(tilePos); + if(tile && tile->isClickable()) + break; + tilePos.coveredDown(); + } + + if(!tile || !tile->isClickable()) return nullptr; - //TODO: move MapView code to UIMap and rework this shit - return m_mapView->getTile(mousePos, m_mapRect); + return tile; } void UIMap::onStyleApply(const std::string& styleName, const OTMLNodePtr& styleNode) diff --git a/src/otclient/uimap.h b/src/otclient/uimap.h index 51f8404f..0fc6c8a5 100644 --- a/src/otclient/uimap.h +++ b/src/otclient/uimap.h @@ -68,6 +68,7 @@ public: CreaturePtr getFollowingCreature() { return m_mapView->getFollowingCreature(); } Otc::DrawFlags getDrawFlags() { return m_mapView->getDrawFlags(); } Position getCameraPosition() { return m_mapView->getCameraPosition(); } + Position getPosition(const Point& mousePos); TilePtr getTile(const Point& mousePos); int getMaxZoomIn() { return m_maxZoomIn; } int getMaxZoomOut() { return m_maxZoomOut; }