No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

minimap.lua 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. Minimap = {}
  2. -- private variables
  3. local minimapWidget
  4. local minimapButton
  5. local minimapWindow
  6. local DEFAULT_ZOOM = 60
  7. local navigating = false
  8. minimapFirstLoad = true
  9. -- private functions
  10. function onMinimapMouseRelease(self, mousePosition, mouseButton)
  11. if navigating then
  12. navigating = false
  13. return
  14. end
  15. local tile = self:getTile(mousePosition)
  16. if tile and mouseButton == MouseLeftButton and self:isPressed() then
  17. local dirs = g_map.findPath(g_game.getLocalPlayer():getPosition(), tile:getPosition(), 127)
  18. if #dirs == 0 then
  19. TextMessage.displayStatus(tr('There is no way.'))
  20. return true
  21. end
  22. g_game.autoWalk(dirs)
  23. return true
  24. end
  25. return false
  26. end
  27. function onMinimapMouseWheel(self, mousePos, direction)
  28. if direction == MouseWheelUp then
  29. self:zoomIn()
  30. else
  31. self:zoomOut()
  32. end
  33. end
  34. --[[
  35. Known Issue (TODO):
  36. If you move the minimap compass directions and
  37. you change floor it will not update the minimap.
  38. ]]
  39. -- public functions
  40. function Minimap.init()
  41. connect(g_game, { onGameStart = Minimap.reset,
  42. onForceWalk = Minimap.center })
  43. g_keyboard.bindKeyDown('Ctrl+M', Minimap.toggle)
  44. minimapButton = TopMenu.addRightGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)', 'minimap.png', Minimap.toggle)
  45. minimapButton:setOn(true)
  46. minimapWindow = g_ui.loadUI('minimap.otui', GameInterface.getRightPanel())
  47. minimapWidget = minimapWindow:recursiveGetChildById('minimap')
  48. g_mouse.bindAutoPress(minimapWidget, Minimap.compassClick)
  49. minimapWidget:setAutoViewMode(false)
  50. minimapWidget:setViewMode(1) -- mid view
  51. minimapWidget:setDrawMinimapColors(true)
  52. minimapWidget:setMultifloor(false)
  53. minimapWidget:setKeepAspectRatio(false)
  54. minimapWidget.onMouseRelease = onMinimapMouseRelease
  55. minimapWidget.onMouseWheel = onMinimapMouseWheel
  56. Minimap.reset()
  57. -- load only the first time (avoid load/save between reloads)
  58. if minimapFirstLoad then
  59. minimapFirstLoad = false
  60. if g_resources.fileExists('/minimap.otcm') then
  61. if g_game.isOnline() then
  62. perror('cannot load minimap while online')
  63. else
  64. g_map.loadOtcm('/minimap.otcm')
  65. end
  66. end
  67. -- save only when closing the client
  68. connect(g_app, { onTerminate = function()
  69. g_map.saveOtcm('/minimap.otcm')
  70. end})
  71. end
  72. end
  73. function Minimap.terminate()
  74. disconnect(g_game, { onGameStart = Minimap.reset,
  75. onForceWalk = Minimap.center })
  76. g_keyboard.unbindKeyDown('Ctrl+M')
  77. minimapButton:destroy()
  78. minimapWindow:destroy()
  79. minimapWindow = nil
  80. minimapWidget = nil
  81. minimapButton = nil
  82. Minimap = nil
  83. end
  84. function Minimap.toggle()
  85. if minimapButton:isOn() then
  86. minimapWindow:close()
  87. minimapButton:setOn(false)
  88. else
  89. minimapWindow:open()
  90. minimapButton:setOn(true)
  91. end
  92. end
  93. function Minimap.onMiniWindowClose()
  94. minimapButton:setOn(false)
  95. end
  96. function Minimap.isClickInRange(position, fromPosition, toPosition)
  97. return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.x <= toPosition.x and position.y <= toPosition.y)
  98. end
  99. -- hooked functions
  100. function Minimap.compassClick(self, mousePos, mouseButton, elapsed)
  101. if elapsed < 300 then return end
  102. navigating = true
  103. local px = mousePos.x - self:getX()
  104. local py = mousePos.y - self:getY()
  105. local dx = px - self:getWidth()/2
  106. local dy = -(py - self:getHeight()/2)
  107. local radius = math.sqrt(dx*dx+dy*dy)
  108. local movex=0
  109. local movey=0
  110. dx = dx/radius
  111. dy = dy/radius
  112. if dx > 0.5 then movex = 1 end
  113. if dx < -0.5 then movex = -1 end
  114. if dy > 0.5 then movey = -1 end
  115. if dy < -0.5 then movey = 1 end
  116. local cameraPos = minimapWidget:getCameraPosition()
  117. local pos = {x = cameraPos.x + movex, y = cameraPos.y + movey, z = cameraPos.z}
  118. minimapWidget:setCameraPosition(pos)
  119. end
  120. function Minimap.onButtonClick(id)
  121. if id == "zoomIn" then
  122. minimapWidget:setZoom(math.max(minimapWidget:getMaxZoomIn(), minimapWidget:getZoom()-15))
  123. elseif id == "zoomOut" then
  124. minimapWidget:setZoom(math.min(minimapWidget:getMaxZoomOut(), minimapWidget:getZoom()+15))
  125. elseif id == "floorUp" then
  126. local pos = minimapWidget:getCameraPosition()
  127. pos.z = pos.z - 1
  128. minimapWidget:setCameraPosition(pos)
  129. elseif id == "floorDown" then
  130. local pos = minimapWidget:getCameraPosition()
  131. pos.z = pos.z + 1
  132. minimapWidget:setCameraPosition(pos)
  133. end
  134. end
  135. -- hooked events
  136. function Minimap.reset()
  137. local player = g_game.getLocalPlayer()
  138. if not player then return end
  139. minimapWidget:followCreature(player)
  140. minimapWidget:setZoom(DEFAULT_ZOOM)
  141. end
  142. function Minimap.center()
  143. local player = g_game.getLocalPlayer()
  144. if not player then return end
  145. minimapWidget:followCreature(player)
  146. end