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.

topmenu.lua 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. -- private variables
  2. local topMenu
  3. local leftButtonsPanel
  4. local rightButtonsPanel
  5. local leftGameButtonsPanel
  6. local rightGameButtonsPanel
  7. -- private functions
  8. local function addButton(id, description, icon, callback, panel, toggle, front)
  9. local class
  10. if toggle then
  11. class = 'TopToggleButton'
  12. else
  13. class = 'TopButton'
  14. end
  15. local button = panel:getChildById(id)
  16. if not button then
  17. button = g_ui.createWidget(class)
  18. if front then
  19. panel:insertChild(1, button)
  20. else
  21. panel:addChild(button)
  22. end
  23. end
  24. button:setId(id)
  25. button:setTooltip(description)
  26. button:setIcon(resolvepath(icon, 3))
  27. button.onMouseRelease = function(widget, mousePos, mouseButton)
  28. if widget:containsPoint(mousePos) and mouseButton ~= MouseMidButton then
  29. callback()
  30. return true
  31. end
  32. end
  33. return button
  34. end
  35. -- public functions
  36. function init()
  37. connect(g_game, { onGameStart = online,
  38. onGameEnd = offline,
  39. onPingBack = updatePing })
  40. connect(g_app, { onFps = updateFps })
  41. topMenu = g_ui.displayUI('topmenu')
  42. leftButtonsPanel = topMenu:getChildById('leftButtonsPanel')
  43. rightButtonsPanel = topMenu:getChildById('rightButtonsPanel')
  44. leftGameButtonsPanel = topMenu:getChildById('leftGameButtonsPanel')
  45. rightGameButtonsPanel = topMenu:getChildById('rightGameButtonsPanel')
  46. pingLabel = topMenu:getChildById('pingLabel')
  47. fpsLabel = topMenu:getChildById('fpsLabel')
  48. g_keyboard.bindKeyDown('Ctrl+Shift+T', toggle)
  49. if g_game.isOnline() then
  50. online()
  51. end
  52. end
  53. function terminate()
  54. disconnect(g_game, { onGameStart = online,
  55. onGameEnd = offline,
  56. onPingBack = updatePing })
  57. disconnect(g_app, { onFps = updateFps })
  58. topMenu:destroy()
  59. end
  60. function online()
  61. showGameButtons()
  62. addEvent(function()
  63. if modules.client_options.getOption('showPing') and (g_game.getFeature(GameClientPing) or g_game.getFeature(GameExtendedClientPing)) then
  64. pingLabel:show()
  65. else
  66. pingLabel:hide()
  67. end
  68. end)
  69. end
  70. function offline()
  71. hideGameButtons()
  72. pingLabel:hide()
  73. end
  74. function updateFps(fps)
  75. text = 'FPS: ' .. fps
  76. fpsLabel:setText(text)
  77. end
  78. function updatePing(ping)
  79. local text = 'Ping: '
  80. local color
  81. if ping < 0 then
  82. text = text .. "??"
  83. color = 'yellow'
  84. else
  85. text = text .. ping .. ' ms'
  86. if ping >= 500 then
  87. color = 'red'
  88. elseif ping >= 250 then
  89. color = 'yellow'
  90. else
  91. color = 'green'
  92. end
  93. end
  94. pingLabel:setColor(color)
  95. pingLabel:setText(text)
  96. end
  97. function setPingVisible(enable)
  98. pingLabel:setVisible(enable)
  99. end
  100. function setFpsVisible(enable)
  101. fpsLabel:setVisible(enable)
  102. end
  103. function addLeftButton(id, description, icon, callback, front)
  104. return addButton(id, description, icon, callback, leftButtonsPanel, false, front)
  105. end
  106. function addLeftToggleButton(id, description, icon, callback, front)
  107. return addButton(id, description, icon, callback, leftButtonsPanel, true, front)
  108. end
  109. function addRightButton(id, description, icon, callback, front)
  110. return addButton(id, description, icon, callback, rightButtonsPanel, false, front)
  111. end
  112. function addRightToggleButton(id, description, icon, callback, front)
  113. return addButton(id, description, icon, callback, rightButtonsPanel, true, front)
  114. end
  115. function addLeftGameButton(id, description, icon, callback, front)
  116. return addButton(id, description, icon, callback, leftGameButtonsPanel, false, front)
  117. end
  118. function addLeftGameToggleButton(id, description, icon, callback, front)
  119. return addButton(id, description, icon, callback, leftGameButtonsPanel, true, front)
  120. end
  121. function addRightGameButton(id, description, icon, callback, front)
  122. return addButton(id, description, icon, callback, rightGameButtonsPanel, false, front)
  123. end
  124. function addRightGameToggleButton(id, description, icon, callback, front)
  125. return addButton(id, description, icon, callback, rightGameButtonsPanel, true, front)
  126. end
  127. function showGameButtons()
  128. leftGameButtonsPanel:show()
  129. rightGameButtonsPanel:show()
  130. end
  131. function hideGameButtons()
  132. leftGameButtonsPanel:hide()
  133. rightGameButtonsPanel:hide()
  134. end
  135. function getButton(id)
  136. return topMenu:recursiveGetChildById(id)
  137. end
  138. function getTopMenu()
  139. return topMenu
  140. end
  141. function toggle()
  142. local menu = getTopMenu()
  143. if not menu then
  144. return
  145. end
  146. if menu:isVisible() then
  147. menu:hide()
  148. modules.client_background.getBackground():addAnchor(AnchorTop, 'parent', AnchorTop)
  149. modules.game_interface.getRootPanel():addAnchor(AnchorTop, 'parent', AnchorTop)
  150. modules.game_interface.getShowTopMenuButton():show()
  151. else
  152. menu:show()
  153. modules.client_background.getBackground():addAnchor(AnchorTop, 'topMenu', AnchorBottom)
  154. modules.game_interface.getRootPanel():addAnchor(AnchorTop, 'topMenu', AnchorBottom)
  155. modules.game_interface.getShowTopMenuButton():hide()
  156. end
  157. end