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.

viplist.lua 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. VipList = {}
  2. -- private variables
  3. local vipWindow
  4. local vipButton
  5. local addVipWindow
  6. -- public functions
  7. function VipList.create()
  8. vipWindow = displayUI('viplist.otui', { parent = Game.gameRightPanel })
  9. vipWindow:hide()
  10. vipButton = TopMenu.addGameButton('vipListButton', 'VIP list', 'viplist.png', VipList.toggle)
  11. end
  12. function VipList.destroy()
  13. vipWindow:destroy()
  14. vipWindow = nil
  15. vipButton:destroy()
  16. vipButton = nil
  17. end
  18. function VipList.toggle()
  19. local visible = not vipWindow:isExplicitlyVisible()
  20. vipWindow:setVisible(visible)
  21. vipButton:setOn(visible)
  22. end
  23. function VipList.createAddWindow()
  24. addVipWindow = displayUI('addvip.otui')
  25. end
  26. function VipList.destroyAddWindow()
  27. addVipWindow:destroy()
  28. addVipWindow = nil
  29. end
  30. function VipList.addVip()
  31. Game.addVip(addVipWindow:getChildById('name'):getText())
  32. VipList.destroyAddWindow()
  33. end
  34. -- hooked events
  35. function VipList.onAddVip(id, name, online)
  36. local vipList = vipWindow:getChildById('vipList')
  37. local label = createWidget('VipListLabel', nil)
  38. label:setId('vip' .. id)
  39. label:setText(name)
  40. if online then
  41. label:setColor('#00ff00')
  42. else
  43. label:setColor('#ff0000')
  44. end
  45. label.vipOnline = online
  46. label:setPhantom(false)
  47. connect(label, { onDoubleClick = function () Game.openPrivateChannel(label:getText()) return true end } )
  48. local nameLower = name:lower()
  49. local childrenCount = vipList:getChildCount()
  50. for i=1,childrenCount do
  51. local child = vipList:getChildByIndex(i)
  52. if online and not child.vipOnline then
  53. vipList:insertChild(i, label)
  54. return
  55. end
  56. if (not online and not child.vipOnline) or (online and child.vipOnline) then
  57. local childText = child:getText():lower()
  58. local length = math.min(childText:len(), nameLower:len())
  59. for j=1,length do
  60. if nameLower:byte(j) < childText:byte(j) then
  61. vipList:insertChild(i, label)
  62. return
  63. elseif nameLower:byte(j) > childText:byte(j) then
  64. break
  65. end
  66. end
  67. end
  68. end
  69. vipList:insertChild(childrenCount+1, label)
  70. end
  71. function VipList.onVipStateChange(id, online)
  72. local vipList = vipWindow:getChildById('vipList')
  73. local label = vipList:getChildById('vip' .. id)
  74. local text = label:getText()
  75. vipList:removeChild(label)
  76. VipList.onAddVip(id, text, online)
  77. end
  78. function VipList.onVipListMousePress(widget, mousePos, mouseButton)
  79. if mouseButton ~= MouseRightButton then return end
  80. local vipList = vipWindow:getChildById('vipList')
  81. local menu = createWidget('PopupMenu')
  82. menu:addOption('Add new VIP', function() VipList.createAddWindow() end)
  83. menu:display(mousePos)
  84. return true
  85. end
  86. function VipList.onVipListLabelMousePress(widget, mousePos, mouseButton)
  87. if mouseButton ~= MouseRightButton then return end
  88. local vipList = vipWindow:getChildById('vipList')
  89. local menu = createWidget('PopupMenu')
  90. menu:addOption('Add new VIP', function() VipList.createAddWindow() end)
  91. menu:addOption('Remove ' .. widget:getText(), function() if widget then Game.removeVip(widget:getId():sub(4)) vipList:removeChild(widget) end end)
  92. menu:addSeparator()
  93. menu:addOption('Copy Name', function() g_window.setClipboardText(widget:getText()) end)
  94. menu:display(mousePos)
  95. return true
  96. end
  97. connect(Game, { onGameStart = VipList.create,
  98. onGameEnd = VipList.destroy,
  99. onAddVip = VipList.onAddVip,
  100. onVipStateChange = VipList.onVipStateChange })