2014-07-13 12:27:09 +02:00
|
|
|
function UIMinimap:onCreate()
|
|
|
|
self.autowalk = true
|
|
|
|
end
|
|
|
|
|
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')
|
2013-02-18 17:16:22 +01:00
|
|
|
self.flags = {}
|
|
|
|
self.alternatives = {}
|
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()
|
2013-02-18 17:16:22 +01:00
|
|
|
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()
|
2013-02-18 17:16:22 +01:00
|
|
|
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
|
2013-02-18 17:16:22 +01:00
|
|
|
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={} }
|
2013-02-18 17:16:22 +01:00
|
|
|
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
|
|
|
|
|
2013-02-18 17:16:22 +01:00
|
|
|
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
|
2013-02-18 17:16:22 +01:00
|
|
|
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)
|
2013-02-18 17:16:22 +01:00
|
|
|
if not pos or not icon then return end
|
2013-01-31 02:44:57 +01:00
|
|
|
local flag = self:getFlag(pos, icon, description)
|
2013-02-18 17:16:22 +01:00
|
|
|
if flag or not icon then
|
2013-01-31 02:44:57 +01:00
|
|
|
return
|
|
|
|
end
|
2013-02-18 17:16:22 +01:00
|
|
|
|
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
|
2013-02-18 17:16:22 +01:00
|
|
|
flag.icon = icon
|
2013-01-31 02:44:57 +01:00
|
|
|
flag:setIcon('/images/game/minimap/flag' .. icon)
|
|
|
|
flag:setTooltip(description)
|
2013-02-18 17:16:22 +01:00
|
|
|
flag.onMouseRelease = onFlagMouseRelease
|
2013-02-24 21:26:19 +01:00
|
|
|
flag.onDestroy = function() table.removevalue(self.flags, flag) end
|
2013-02-18 17:16:22 +01:00
|
|
|
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)
|
2013-02-18 17:16:22 +01:00
|
|
|
self:centerInPosition(widget, widget.pos)
|
|
|
|
else
|
|
|
|
self:removeChild(widget)
|
2013-01-31 02:44:57 +01:00
|
|
|
end
|
|
|
|
end
|
2013-02-18 17:16:22 +01:00
|
|
|
layout:enableUpdates()
|
|
|
|
layout:update()
|
|
|
|
end
|
2013-01-31 02:44:57 +01:00
|
|
|
|
2013-02-18 17:16:22 +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
|
|
|
|
|
2013-02-18 17:16:22 +01:00
|
|
|
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
|
2013-02-18 17:16:22 +01:00
|
|
|
return nil
|
2013-01-31 02:44:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function UIMinimap:removeFlag(pos, icon, description)
|
2013-02-18 17:16:22 +01:00
|
|
|
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)
|
2013-02-18 17:16:22 +01:00
|
|
|
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()
|
2013-02-18 17:16:22 +01:00
|
|
|
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
|
|
|
|
|
2013-02-18 17:16:22 +01:00
|
|
|
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)
|
2013-02-18 17:16:22 +01:00
|
|
|
self.dragReference = pos
|
|
|
|
self.dragCameraReference = self:getCameraPosition()
|
2013-01-31 02:44:57 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIMinimap:onDragMove(pos, moved)
|
2013-02-18 17:16:22 +01:00
|
|
|
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
|
|
|
|
|
2014-07-13 12:27:09 +02:00
|
|
|
function UIMinimap:onStyleApply(styleName, styleNode)
|
|
|
|
for name,value in pairs(styleNode) do
|
|
|
|
if name == 'autowalk' then
|
|
|
|
print(value)
|
|
|
|
self.autowalk = value
|
|
|
|
end
|
|
|
|
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
|
2013-02-18 17:16:22 +01:00
|
|
|
|
2013-01-31 02:44:57 +01:00
|
|
|
flagRadioGroup:selectWidget(flagRadioGroup:getFirstWidget())
|
2013-02-18 17:16:22 +01:00
|
|
|
|
2014-01-18 15:09:26 +01:00
|
|
|
local successFunc = function()
|
2013-02-18 17:16:22 +01:00
|
|
|
self:addFlag(pos, flagRadioGroup:getSelectedWidget().icon, description:getText())
|
|
|
|
self:destroyFlagWindow()
|
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
|
|
|
local cancelFunc = function()
|
|
|
|
self:destroyFlagWindow()
|
2013-11-18 20:57:50 +01:00
|
|
|
end
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-11-18 20:57:50 +01:00
|
|
|
okButton.onClick = successFunc
|
|
|
|
cancelButton.onClick = cancelFunc
|
2014-01-18 15:09:26 +01:00
|
|
|
|
2013-11-18 20:57:50 +01:00
|
|
|
self.flagWindow.onEnter = successFunc
|
|
|
|
self.flagWindow.onEscape = cancelFunc
|
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
|