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

340 lines
9.3 KiB
Lua
Raw Normal View History

2013-01-31 02:44:57 +01:00
function UIMinimap:onSetup()
self.flagWindow = nil
self.flagsWidget = self:getChildById('flags')
self.floorUpWidget = self:getChildById('floorUp')
self.floorDownWidget = self:getChildById('floorDown')
self.zoomInWidget = self:getChildById('zoomIn')
self.zoomOutWidget = self:getChildById('zoomOut')
self.dx = 0
self.dy = 0
2013-01-31 04:09:56 +01:00
self.autowalk = true
2013-01-31 21:34:15 +01:00
self.allowFollowLocalPlayer = true
2013-01-31 02:44:57 +01:00
self.onPositionChange = function() self:followLocalPlayer() end
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,
})
connect(LocalPlayer, { onPositionChange = self.onPositionChange })
end
function UIMinimap:onDestroy()
disconnect(LocalPlayer, { onPositionChange = self.onPositionChange })
disconnect(g_game, {
onAddAutomapFlag = self.onAddAutomapFlag,
onRemoveAutomapFlag = self.onRemoveAutomapFlag,
})
self:destroyFlagWindow()
2013-01-31 04:09:56 +01:00
self:destroyFullPanel()
end
function UIMinimap:onVisibilityChange()
if not self:isVisible() then
self:destroyFlagWindow()
self:destroyFullPanel()
end
end
function UIMinimap:hideFlags()
self.flagsWidget:hide()
end
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 21:34:15 +01:00
function UIMinimap:disableFollowPlayer()
self.allowFollowLocalPlayer = false
end
2013-01-31 04:09:56 +01:00
function UIMinimap:enableFullPanel(image)
self.fullImage = image
2013-01-31 02:44:57 +01:00
end
function UIMinimap:load()
local settings = g_settings.getNode('Minimap')
if settings then
if settings.flags then
for i=1,#settings.flags do
local flag = settings.flags[i]
self:addFlag(flag.position, flag.icon, flag.description)
end
end
self:setZoom(settings.zoom)
end
self:updateFlags()
end
function UIMinimap:save()
local settings = { flags={} }
local children = self.flagsWidget:getChildren()
for i=1,#children do
local flag = children[i]
table.insert(settings.flags, {
position = flag.pos,
icon = flag.icon,
description = flag.description,
})
end
settings.zoom = self:getZoom()
g_settings.setNode('Minimap', settings)
end
function UIMinimap:addFlag(pos, icon, description)
local flag = self:getFlag(pos, icon, description)
if flag then
return
end
flag = g_ui.createWidget('MinimapFlag', self.flagsWidget)
flag.pos = pos
flag.icon = icon
flag.description = description
flag:setIcon('/images/game/minimap/flag' .. icon)
flag:setTooltip(description)
flag.onMouseRelease = function(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
self:updateFlag(flag)
end
function UIMinimap:getFlag(pos, icon, description)
local children = self.flagsWidget:getChildren()
for i=1,#children do
local flag = children[i]
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
end
function UIMinimap:removeFlag(pos, icon, description)
local flag = self:getFlag(pos, icon, description)
if flag then
flag:destroy()
end
end
function UIMinimap:updateFlag(flag)
2013-01-31 06:38:06 +01:00
local point = self:getPoint(flag.pos)
2013-01-31 21:34:15 +01:00
if self:containsPoint(point) and self:getZoom() >= 0 and flag.pos.z == self:getCameraPosition().z then
2013-01-31 02:44:57 +01:00
flag:setVisible(true)
2013-01-31 06:38:06 +01:00
flag:setMarginLeft(point.x - self:getX() - flag:getWidth()/2)
flag:setMarginTop(point.y - self:getY() - flag:getHeight()/2)
2013-01-31 02:44:57 +01:00
else
flag:setVisible(false)
end
end
function UIMinimap:updateFlags()
local children = self.flagsWidget:getChildren()
for i=1,#children do
self:updateFlag(children[i])
end
end
2013-01-31 21:34:15 +01:00
UIMinimap.realSetCameraPosition = UIMinimap.realSetCameraPosition or UIMinimap.setCameraPosition
function UIMinimap:setCameraPosition(pos)
self:realSetCameraPosition(pos)
self:updateFlags()
end
2013-01-31 17:20:04 +01:00
UIMinimap.realZoomIn = UIMinimap.realZoomIn or UIMinimap.zoomIn
2013-01-31 02:44:57 +01:00
function UIMinimap:zoomIn()
self:realZoomIn()
self:updateFlags()
end
2013-01-31 17:20:04 +01:00
UIMinimap.realZoomOut = UIMinimap.realZoomOut or UIMinimap.zoomOut
2013-01-31 02:44:57 +01:00
function UIMinimap:zoomOut()
self:realZoomOut()
self:updateFlags()
end
2013-01-31 17:20:04 +01:00
UIMinimap.realSetZoom = UIMinimap.realSetZoom or UIMinimap.setZoom
function UIMinimap:setZoom(zoom)
self:realSetZoom(zoom)
self:updateFlags()
end
2013-01-31 02:44:57 +01:00
function UIMinimap:floorUp(floors)
local pos = self:getCameraPosition()
pos.z = pos.z - floors
if pos.z >= FloorHigher then
self:setCameraPosition(pos)
end
self:updateFlags()
end
function UIMinimap:floorDown(floors)
local pos = self:getCameraPosition()
pos.z = pos.z + floors
if pos.z <= FloorLower then
self:setCameraPosition(pos)
end
self:updateFlags()
end
function UIMinimap:followLocalPlayer()
2013-01-31 21:34:15 +01:00
if not self:isDragging() and self.allowFollowLocalPlayer then
2013-01-31 17:20:04 +01:00
local player = g_game.getLocalPlayer()
self:followCreature(player)
self:updateFlags()
end
2013-01-31 02:44:57 +01:00
end
function UIMinimap:reset()
self:followLocalPlayer()
self:setZoom(0)
end
function UIMinimap:move(x, y)
local topLeft, bottomRight = self:getArea()
self.dx = self.dx + ((bottomRight.x - topLeft.x) / self:getWidth() ) * x
self.dy = self.dy + ((bottomRight.y - topLeft.y) / self:getHeight()) * y
local dx = math.floor(self.dx)
local dy = math.floor(self.dy)
self.dx = self.dx - dx
self.dy = self.dy - dy
local cameraPos = self:getCameraPosition()
local pos = {x = cameraPos.x - dx, y = cameraPos.y - dy, z = cameraPos.z}
self:setCameraPosition(pos)
self:updateFlags()
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
self:updateFlags()
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:getPosition(pos)
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
2013-01-31 02:44:57 +01:00
player.onAutoWalkFail = function() modules.game_textmessage.displayFailureMessage(tr('There is no way.')) end
2013-01-31 17:20:04 +01:00
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)
if self.fullImage then menu:addOption(tr('Full map'), function() self:createFullPanel() end) end
2013-01-31 02:44:57 +01:00
menu:display(pos)
return true
end
return false
end
function UIMinimap:onDragEnter(pos)
return true
end
function UIMinimap:onDragMove(pos, moved)
self:move(moved.x, moved.y)
return true
end
function UIMinimap:onDragLeave(widget, pos)
return true
end
2013-01-31 04:09:56 +01:00
function UIMinimap:createFullPanel()
self.fullPanel = g_ui.createWidget('MinimapFullPanel', rootWidget)
self.fullPanel.onDestroy = function() self.fullPanel = nil end
2013-01-31 17:20:04 +01:00
local image = self.fullPanel:getChildById('image')
image:setImage(self.fullImage)
2013-01-31 04:09:56 +01:00
end
function UIMinimap:destroyFullPanel()
if self.fullPanel then
self.fullPanel:destroy()
self.fullPanel = nil
end
end
2013-01-31 02:44:57 +01:00
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
flagRadioGroup:selectWidget(flagRadioGroup:getFirstWidget())
okButton.onClick = function()
self:addFlag(pos, flagRadioGroup:getSelectedWidget().icon, description:getText())
self:destroyFlagWindow()
end
cancelButton.onClick = function()
self:destroyFlagWindow()
end
self.flagWindow.onDestroy = function() flagRadioGroup:destroy() end
end
function UIMinimap:destroyFlagWindow()
if self.flagWindow then
self.flagWindow:destroy()
self.flagWindow = nil
end
end
function UIMinimap:getArea()
local topLeft = self:getPosition({ x = self:getX() + 1, y = self:getY() + 1 })
local bottomRight = self:getPosition({ x = self:getX() + self:getWidth() - 2, y = self:getY() + self:getHeight() - 2 })
return topLeft, bottomRight
end