tibia-client/modules/gamelib/ui/uiminimap.lua

291 lines
7.7 KiB
Lua
Raw Normal View History

2013-01-31 02:44:57 +01:00
function UIMinimap:onSetup()
self.flagWindow = nil
self.floorUpWidget = self:getChildById('floorUp')
self.floorDownWidget = self:getChildById('floorDown')
self.zoomInWidget = self:getChildById('zoomIn')
self.zoomOutWidget = self:getChildById('zoomOut')
self.flags = {}
self.alternatives = {}
2013-01-31 04:09:56 +01:00
self.autowalk = true
2013-01-31 02:44:57 +01:00
self.onAddAutomapFlag = function(pos, icon, description) self:addFlag(pos, icon, description) end
2013-01-31 22:43:42 +01:00
self.onRemoveAutomapFlag = function(pos, icon, description) self:removeFlag(pos, icon, description) end
2013-01-31 02:44:57 +01:00
connect(g_game, {
onAddAutomapFlag = self.onAddAutomapFlag,
onRemoveAutomapFlag = self.onRemoveAutomapFlag,
})
end
function UIMinimap:onDestroy()
for _,widget in pairs(self.alternatives) do
widget:destroy()
end
self.alternatives = {}
2013-01-31 02:44:57 +01:00
disconnect(g_game, {
onAddAutomapFlag = self.onAddAutomapFlag,
onRemoveAutomapFlag = self.onRemoveAutomapFlag,
})
self:destroyFlagWindow()
self.flags = {}
2013-01-31 04:09:56 +01:00
end
function UIMinimap:onVisibilityChange()
if not self:isVisible() then
self:destroyFlagWindow()
end
end
2013-02-27 20:24:32 +01:00
function UIMinimap:onCameraPositionChange(cameraPos)
if self.cross then
self:setCrossPosition(self.cross.pos)
end
end
2013-01-31 04:09:56 +01:00
function UIMinimap:hideFloor()
self.floorUpWidget:hide()
self.floorDownWidget:hide()
end
function UIMinimap:hideZoom()
self.zoomInWidget:hide()
self.zoomOutWidget:hide()
end
function UIMinimap:disableAutoWalk()
self.autowalk = false
end
2013-01-31 02:44:57 +01:00
function UIMinimap:load()
local settings = g_settings.getNode('Minimap')
if settings then
if settings.flags then
for _,flag in pairs(settings.flags) do
2013-01-31 02:44:57 +01:00
self:addFlag(flag.position, flag.icon, flag.description)
end
end
self:setZoom(settings.zoom)
end
end
function UIMinimap:save()
local settings = { flags={} }
for _,flag in pairs(self.flags) do
2013-01-31 02:44:57 +01:00
table.insert(settings.flags, {
position = flag.pos,
icon = flag.icon,
description = flag.description,
})
end
settings.zoom = self:getZoom()
g_settings.setNode('Minimap', settings)
end
local function onFlagMouseRelease(widget, pos, button)
if button == MouseRightButton then
local menu = g_ui.createWidget('PopupMenu')
menu:addOption(tr('Delete mark'), function() widget:destroy() end)
menu:display(pos)
return true
end
return false
end
function UIMinimap:setCrossPosition(pos)
local cross = self.cross
if not self.cross then
cross = g_ui.createWidget('MinimapCross', self)
cross:setIcon('/images/game/minimap/cross')
self.cross = cross
end
2013-02-27 20:24:32 +01:00
pos.z = self:getCameraPosition().z
cross.pos = pos
if pos then
self:centerInPosition(cross, pos)
else
cross:breakAnchors()
end
end
2013-01-31 02:44:57 +01:00
function UIMinimap:addFlag(pos, icon, description)
if not pos or not icon then return end
2013-01-31 02:44:57 +01:00
local flag = self:getFlag(pos, icon, description)
if flag or not icon then
2013-01-31 02:44:57 +01:00
return
end
2013-02-27 20:24:32 +01:00
flag = g_ui.createWidget('MinimapFlag')
self:insertChild(1, flag)
2013-01-31 02:44:57 +01:00
flag.pos = pos
flag.description = description
flag.icon = icon
2013-01-31 02:44:57 +01:00
flag:setIcon('/images/game/minimap/flag' .. icon)
flag:setTooltip(description)
flag.onMouseRelease = onFlagMouseRelease
2013-02-24 21:26:19 +01:00
flag.onDestroy = function() table.removevalue(self.flags, flag) end
table.insert(self.flags, flag)
self:centerInPosition(flag, pos)
end
function UIMinimap:addAlternativeWidget(widget, pos, maxZoom)
widget.pos = pos
widget.maxZoom = maxZoom or 0
widget.minZoom = minZoom
table.insert(self.alternatives, widget)
end
function UIMinimap:setAlternativeWidgetsVisible(show)
local layout = self:getLayout()
layout:disableUpdates()
for _,widget in pairs(self.alternatives) do
if show then
2013-02-27 20:24:32 +01:00
self:insertChild(1, widget)
self:centerInPosition(widget, widget.pos)
else
self:removeChild(widget)
2013-01-31 02:44:57 +01:00
end
end
layout:enableUpdates()
layout:update()
end
2013-01-31 02:44:57 +01:00
function UIMinimap:onZoomChange(zoom)
for _,widget in pairs(self.alternatives) do
if (not widget.minZoom or widget.minZoom >= zoom) and widget.maxZoom <= zoom then
widget:show()
else
widget:hide()
end
end
2013-01-31 02:44:57 +01:00
end
function UIMinimap:getFlag(pos)
for _,flag in pairs(self.flags) do
2013-01-31 22:43:42 +01:00
if flag.pos.x == pos.x and flag.pos.y == pos.y and flag.pos.z == pos.z then
2013-01-31 02:44:57 +01:00
return flag
end
end
return nil
2013-01-31 02:44:57 +01:00
end
function UIMinimap:removeFlag(pos, icon, description)
local flag = self:getFlag(pos)
2013-01-31 02:44:57 +01:00
if flag then
flag:destroy()
2013-01-31 17:20:04 +01:00
end
2013-01-31 02:44:57 +01:00
end
function UIMinimap:reset()
self:setZoom(0)
if self.cross then
self:setCameraPosition(self.cross.pos)
end
2013-01-31 02:44:57 +01:00
end
function UIMinimap:move(x, y)
local cameraPos = self:getCameraPosition()
local scale = self:getScale()
if scale > 1 then scale = 1 end
local dx = x/scale
local dy = y/scale
2013-01-31 02:44:57 +01:00
local pos = {x = cameraPos.x - dx, y = cameraPos.y - dy, z = cameraPos.z}
self:setCameraPosition(pos)
end
function UIMinimap:onMouseWheel(mousePos, direction)
local keyboardModifiers = g_keyboard.getModifiers()
if direction == MouseWheelUp and keyboardModifiers == KeyboardNoModifier then
self:zoomIn()
elseif direction == MouseWheelDown and keyboardModifiers == KeyboardNoModifier then
self:zoomOut()
elseif direction == MouseWheelDown and keyboardModifiers == KeyboardCtrlModifier then
self:floorUp(1)
elseif direction == MouseWheelUp and keyboardModifiers == KeyboardCtrlModifier then
self:floorDown(1)
end
end
function UIMinimap:onMousePress(pos, button)
if not self:isDragging() then
self.allowNextRelease = true
end
end
function UIMinimap:onMouseRelease(pos, button)
2013-01-31 04:09:56 +01:00
if not self.allowNextRelease then return true end
2013-01-31 02:44:57 +01:00
self.allowNextRelease = false
local mapPos = self:getTilePosition(pos)
2013-01-31 02:44:57 +01:00
if not mapPos then return end
if button == MouseLeftButton then
local player = g_game.getLocalPlayer()
2013-01-31 17:20:04 +01:00
if self.autowalk then
player:autoWalk(mapPos)
2013-01-31 02:44:57 +01:00
end
return true
elseif button == MouseRightButton then
local menu = g_ui.createWidget('PopupMenu')
2013-01-31 04:09:56 +01:00
menu:addOption(tr('Create mark'), function() self:createFlagWindow(mapPos) end)
2013-01-31 02:44:57 +01:00
menu:display(pos)
return true
end
return false
end
function UIMinimap:onDragEnter(pos)
self.dragReference = pos
self.dragCameraReference = self:getCameraPosition()
2013-01-31 02:44:57 +01:00
return true
end
function UIMinimap:onDragMove(pos, moved)
local scale = self:getScale()
local dx = (self.dragReference.x - pos.x)/scale
local dy = (self.dragReference.y - pos.y)/scale
local pos = {x = self.dragCameraReference.x + dx, y = self.dragCameraReference.y + dy, z = self.dragCameraReference.z}
self:setCameraPosition(pos)
2013-01-31 02:44:57 +01:00
return true
end
function UIMinimap:onDragLeave(widget, pos)
return true
end
function UIMinimap:createFlagWindow(pos)
if self.flagWindow then return end
if not pos then return end
self.flagWindow = g_ui.createWidget('MinimapFlagWindow', rootWidget)
local positionLabel = self.flagWindow:getChildById('position')
local description = self.flagWindow:getChildById('description')
local okButton = self.flagWindow:getChildById('okButton')
local cancelButton = self.flagWindow:getChildById('cancelButton')
positionLabel:setText(string.format('%i, %i, %i', pos.x, pos.y, pos.z))
local flagRadioGroup = UIRadioGroup.create()
for i=0,19 do
local checkbox = self.flagWindow:getChildById('flag' .. i)
checkbox.icon = i
flagRadioGroup:addWidget(checkbox)
end
2013-01-31 02:44:57 +01:00
flagRadioGroup:selectWidget(flagRadioGroup:getFirstWidget())
2013-01-31 02:44:57 +01:00
okButton.onClick = function()
self:addFlag(pos, flagRadioGroup:getSelectedWidget().icon, description:getText())
self:destroyFlagWindow()
end
cancelButton.onClick = function() self:destroyFlagWindow() end
2013-01-31 02:44:57 +01:00
self.flagWindow.onDestroy = function() flagRadioGroup:destroy() end
end
function UIMinimap:destroyFlagWindow()
if self.flagWindow then
self.flagWindow:destroy()
self.flagWindow = nil
end
end