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 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. TopMenu = {}
  2. -- private variables
  3. local topMenu
  4. local leftButtonsPanel
  5. local rightButtonsPanel
  6. local gameButtonsPanel
  7. -- private functions
  8. local function onLogout()
  9. if Game.isOnline() then
  10. Game.logout(false)
  11. else
  12. exit()
  13. end
  14. end
  15. -- public functions
  16. function TopMenu.init()
  17. topMenu = displayUI('topmenu.otui')
  18. leftButtonsPanel = topMenu:getChildById('leftButtonsPanel')
  19. rightButtonsPanel = topMenu:getChildById('rightButtonsPanel')
  20. gameButtonsPanel = topMenu:getChildById('gameButtonsPanel')
  21. TopMenu.addRightButton('logoutButton', 'Logout (Ctrl+Q)', '/core_styles/icons/logout.png', onLogout)
  22. Keyboard.bindKeyDown('Ctrl+Q', onLogout)
  23. connect(Game, { onGameStart = TopMenu.showGameButtons,
  24. onGameEnd = TopMenu.hideGameButtons })
  25. end
  26. function TopMenu.terminate()
  27. Keyboard.unbindKeyDown('Ctrl+Q')
  28. leftButtonsPanel = nil
  29. rightButtonsPanel = nil
  30. gameButtonsPanel = nil
  31. topMenu:destroy()
  32. topMenu = nil
  33. disconnect(Game, { onGameStart = TopMenu.showGameButtons,
  34. onGameEnd = TopMenu.hideGameButtons })
  35. TopMenu = nil
  36. end
  37. function TopMenu.addButton(id, description, icon, callback, right)
  38. local panel
  39. local class
  40. if right then
  41. panel = rightButtonsPanel
  42. class = 'TopRightButton'
  43. else
  44. panel = leftButtonsPanel
  45. class = 'TopLeftButton'
  46. end
  47. local button = createWidget(class, panel)
  48. button:setId(id)
  49. button:setTooltip(description)
  50. button:setIcon(resolvepath(icon, 2))
  51. button.onClick = callback
  52. return button
  53. end
  54. function TopMenu.addGameButton(id, description, icon, callback)
  55. local button = createWidget('GameTopButton', gameButtonsPanel)
  56. button:setId(id)
  57. button:setTooltip(description)
  58. button:setIcon(resolvepath(icon, 2))
  59. button.onClick = callback
  60. return button
  61. end
  62. function TopMenu.addLeftButton(id, description, icon, callback)
  63. return TopMenu.addButton(id, description, resolvepath(icon, 2), callback, false)
  64. end
  65. function TopMenu.addRightButton(id, description, icon, callback)
  66. return TopMenu.addButton(id, description, resolvepath(icon, 2), callback, true)
  67. end
  68. function TopMenu.hideGameButtons()
  69. gameButtonsPanel:hide()
  70. end
  71. function TopMenu.showGameButtons()
  72. gameButtonsPanel:show()
  73. end
  74. function TopMenu.getButton(id)
  75. return topMenu:recursiveGetChildById(id)
  76. end