2012-04-07 01:12:46 +02:00
|
|
|
Minimap = {}
|
|
|
|
|
|
|
|
-- private variables
|
|
|
|
local minimapWidget
|
|
|
|
local minimapButton
|
2012-06-22 07:26:22 +02:00
|
|
|
local minimapWindow
|
2012-04-27 08:30:54 +02:00
|
|
|
local DEFAULT_ZOOM = 45
|
2012-06-22 07:26:22 +02:00
|
|
|
minimapFirstLoad = true
|
2012-04-07 01:12:46 +02:00
|
|
|
|
|
|
|
-- private functions
|
|
|
|
function onMinimapMouseRelease(self, mousePosition, mouseButton)
|
|
|
|
local tile = self:getTile(mousePosition)
|
|
|
|
if tile and mouseButton == MouseLeftButton and self:isPressed() then
|
|
|
|
local dirs = g_map.findPath(g_game.getLocalPlayer():getPosition(), tile:getPosition(), 255)
|
|
|
|
if #dirs == 0 then
|
2012-04-26 21:54:16 +02:00
|
|
|
TextMessage.displayStatus(tr('There is no way.'))
|
2012-04-07 01:12:46 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
g_game.autoWalk(dirs)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function onMinimapMouseWheel(self, mousePos, direction)
|
|
|
|
if direction == MouseWheelUp then
|
|
|
|
self:zoomIn()
|
|
|
|
else
|
|
|
|
self:zoomOut()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- public functions
|
|
|
|
function Minimap.init()
|
2012-05-10 15:06:06 +02:00
|
|
|
connect(g_game, { onGameStart = Minimap.reset })
|
2012-06-26 00:13:30 +02:00
|
|
|
g_keyboard.bindKeyDown('Ctrl+M', Minimap.toggle)
|
2012-04-07 01:12:46 +02:00
|
|
|
|
2012-04-26 21:54:16 +02:00
|
|
|
minimapButton = TopMenu.addGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)', 'minimap.png', Minimap.toggle)
|
2012-06-23 16:26:18 +02:00
|
|
|
minimapButton:setOn(true)
|
2012-04-07 01:12:46 +02:00
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
minimapWindow = g_ui.loadUI('minimap.otui', GameInterface.getRightPanel())
|
2012-06-22 07:26:22 +02:00
|
|
|
|
|
|
|
minimapWidget = minimapWindow:recursiveGetChildById('minimap')
|
|
|
|
minimapWidget:setAutoViewMode(false)
|
|
|
|
minimapWidget:setViewMode(1) -- mid view
|
|
|
|
minimapWidget:setDrawMinimapColors(true)
|
|
|
|
minimapWidget:setMultifloor(false)
|
|
|
|
minimapWidget:setKeepAspectRatio(false)
|
2012-04-07 01:12:46 +02:00
|
|
|
minimapWidget.onMouseRelease = onMinimapMouseRelease
|
2012-06-26 07:54:55 +02:00
|
|
|
minimapWidget.onMouseWheel = onMinimapMouseWheel
|
|
|
|
|
2012-04-27 08:30:54 +02:00
|
|
|
Minimap.reset()
|
2012-06-22 07:26:22 +02:00
|
|
|
|
|
|
|
-- load only the first time (avoid load/save between reloads)
|
|
|
|
if minimapFirstLoad then
|
|
|
|
minimapFirstLoad = false
|
|
|
|
if g_resources.fileExists('/minimap.otcm') then
|
|
|
|
if g_game.isOnline() then
|
|
|
|
perror('cannot load minimap while online')
|
|
|
|
else
|
|
|
|
g_map.loadOtcm('/minimap.otcm')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- save only when closing the client
|
|
|
|
connect(g_app, { onTerminate = function()
|
|
|
|
g_map.saveOtcm('/minimap.otcm')
|
|
|
|
end})
|
|
|
|
end
|
2012-04-07 01:12:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Minimap.terminate()
|
2012-05-10 15:06:06 +02:00
|
|
|
disconnect(g_game, { onGameStart = Minimap.reset })
|
2012-06-26 00:13:30 +02:00
|
|
|
g_keyboard.unbindKeyDown('Ctrl+M')
|
2012-04-07 01:12:46 +02:00
|
|
|
|
2012-06-24 13:20:17 +02:00
|
|
|
minimapButton:destroy()
|
2012-06-22 07:26:22 +02:00
|
|
|
minimapWindow:destroy()
|
|
|
|
minimapWindow = nil
|
2012-04-07 01:12:46 +02:00
|
|
|
minimapWidget = nil
|
|
|
|
minimapButton = nil
|
2012-04-27 06:54:14 +02:00
|
|
|
Minimap = nil
|
2012-04-07 01:12:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Minimap.toggle()
|
2012-06-24 14:41:39 +02:00
|
|
|
if minimapButton:isOn() then
|
2012-06-22 07:26:22 +02:00
|
|
|
minimapWindow:close()
|
|
|
|
minimapButton:setOn(false)
|
2012-06-24 14:41:39 +02:00
|
|
|
else
|
|
|
|
minimapWindow:open()
|
|
|
|
minimapButton:setOn(true)
|
2012-06-22 07:26:22 +02:00
|
|
|
end
|
2012-04-07 01:12:46 +02:00
|
|
|
end
|
|
|
|
|
2012-06-24 14:41:39 +02:00
|
|
|
function Minimap.onMiniWindowClose()
|
|
|
|
minimapButton:setOn(false)
|
|
|
|
end
|
|
|
|
|
2012-04-07 01:12:46 +02:00
|
|
|
function Minimap.reset()
|
2012-04-27 08:30:54 +02:00
|
|
|
local player = g_game.getLocalPlayer()
|
|
|
|
if not player then return end
|
|
|
|
minimapWidget:followCreature(player)
|
|
|
|
minimapWidget:setZoom(DEFAULT_ZOOM)
|
2012-04-07 01:12:46 +02:00
|
|
|
end
|
|
|
|
|
2012-06-26 07:54:55 +02:00
|
|
|
function Minimap.isClickInRange(position, fromPosition, toPosition)
|
|
|
|
return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.x <= toPosition.x and position.y <= toPosition.y)
|
|
|
|
end
|
|
|
|
-- hooked functions
|
|
|
|
local compassZones = {}
|
|
|
|
compassZones.west = {x = 0, y = 30, posx = -1, posy = 0}
|
|
|
|
compassZones.north = {x = 30, y = 0, posx = 0, posy = -1}
|
|
|
|
compassZones.south = {x = 30, y = 57, posx = 0, posy = 1}
|
|
|
|
compassZones.east = {x = 57, y = 30, posx = 1, posy = 0}
|
|
|
|
compassZones.center = {x = 30, y = 30, posx = 0, posy = 0, center = true}
|
|
|
|
function Minimap.compassClick(self, mousePos)
|
|
|
|
local compassPos = self:getRect()
|
|
|
|
local pos = {x = mousePos.x-compassPos.x, y = mousePos.y-compassPos.y}
|
|
|
|
local move = {x = 0, y = 0}
|
|
|
|
local center = false
|
|
|
|
for i,v in pairs(compassZones) do
|
|
|
|
local lowPos = {x = v.x-15, y = v.y-15}
|
|
|
|
local highPos = {x = v.x+15, y = v.y+15}
|
|
|
|
if Minimap.isClickInRange(pos, lowPos, highPos) then
|
|
|
|
move.x = move.x + v.posx * minimapWidget:getZoom()/10
|
|
|
|
move.y = move.y + v.posy * minimapWidget:getZoom()/10
|
|
|
|
if v.center then center = true end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if center then
|
|
|
|
local player = g_game.getLocalPlayer()
|
|
|
|
if not player then return end
|
|
|
|
minimapWidget:followCreature(player)
|
|
|
|
else
|
|
|
|
local cameraPos = minimapWidget:getCameraPosition()
|
|
|
|
local pos = {x = cameraPos.x + move.x, y = cameraPos.y + move.y, z = cameraPos.z}
|
|
|
|
minimapWidget:setCameraPosition(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Minimap.onButtonClick(id)
|
|
|
|
if id == "zoomIn" then
|
|
|
|
minimapWidget:setZoom(math.max(minimapWidget:getMaxZoomIn(), minimapWidget:getZoom()-15))
|
|
|
|
elseif id == "zoomOut" then
|
|
|
|
minimapWidget:setZoom(math.min(minimapWidget:getMaxZoomOut(), minimapWidget:getZoom()+15))
|
|
|
|
elseif id == "levelUp" then
|
|
|
|
local pos = minimapWidget:getCameraPosition()
|
|
|
|
pos.z = pos.z - 1
|
|
|
|
minimapWidget:setCameraPosition(pos)
|
|
|
|
elseif id == "levelDown" then
|
|
|
|
local pos = minimapWidget:getCameraPosition()
|
|
|
|
pos.z = pos.z + 1
|
|
|
|
minimapWidget:setCameraPosition(pos)
|
|
|
|
end
|
|
|
|
end
|