tibia-client/modules/game_minimap/minimap.lua

106 lines
2.6 KiB
Lua
Raw Normal View History

Minimap = {}
-- private variables
local minimapWidget
local minimapButton
2012-06-22 07:26:22 +02:00
local minimapWindow
local DEFAULT_ZOOM = 45
2012-06-22 07:26:22 +02:00
minimapFirstLoad = true
-- 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
TextMessage.displayStatus(tr('There is no way.'))
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()
connect(g_game, { onGameStart = Minimap.reset })
2012-06-26 00:13:30 +02:00
g_keyboard.bindKeyDown('Ctrl+M', Minimap.toggle)
minimapButton = TopMenu.addGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)', 'minimap.png', Minimap.toggle)
minimapButton:setOn(true)
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)
minimapWidget.onMouseRelease = onMinimapMouseRelease
minimapWidget.onMouseWheel = onMinimapMouseWheel
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
end
function Minimap.terminate()
disconnect(g_game, { onGameStart = Minimap.reset })
2012-06-26 00:13:30 +02:00
g_keyboard.unbindKeyDown('Ctrl+M')
minimapButton:destroy()
2012-06-22 07:26:22 +02:00
minimapWindow:destroy()
minimapWindow = nil
minimapWidget = nil
minimapButton = nil
2012-04-27 06:54:14 +02:00
Minimap = nil
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
end
2012-06-24 14:41:39 +02:00
function Minimap.onMiniWindowClose()
minimapButton:setOn(false)
end
function Minimap.reset()
local player = g_game.getLocalPlayer()
if not player then return end
minimapWidget:followCreature(player)
minimapWidget:setZoom(DEFAULT_ZOOM)
end