Advertisement
NonSequitur

Untitled

Dec 11th, 2015
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. if not modalWindows then
  2.     modalWindows = {
  3.         modalWindowConstructor = ModalWindow,
  4.         nextFreeId = 500,
  5.  
  6.         windows = {}
  7.     }
  8. end
  9.  
  10. local MT = {}
  11. MT.__index = MT
  12.  
  13. function ModalWindow(...)
  14.     local args = {...}
  15.     if type(args[1]) == 'table' then
  16.         local self = setmetatable(args[1], MT)
  17.         local id = modalWindows.nextFreeId     
  18.         self.id = id
  19.         self.buttons = {}
  20.         self.choices = {}
  21.         self.players = {}
  22.         self.created = false
  23.  
  24.         modalWindows.nextFreeId = id + 1
  25.         table.insert(modalWindows.windows, self)
  26.         return self
  27.     end
  28.  
  29.     return modalWindows.modalWindowConstructor(...)
  30. end
  31.  
  32. function MT:setDefaultCallback(callback)
  33.     self.defaultCallback = callback
  34. end
  35.  
  36. function MT:addButton(text, callback)
  37.     local button = {text = tostring(text), callback = callback}
  38.     table.insert(self.buttons, button)
  39.     return button
  40. end
  41.  
  42. function MT:addButtons(...)
  43.     for _, text in ipairs({...}) do
  44.         table.insert(self.buttons, {text = tostring(text)})
  45.     end
  46. end
  47.  
  48. function MT:addChoice(text)
  49.     local choice = {text = tostring(text)}
  50.     table.insert(self.choices, choice)
  51.     return choice
  52. end
  53.  
  54. function MT:addChoices(...)
  55.     for _, text in ipairs({...}) do
  56.         table.insert(self.choices, {text = tostring(text)})
  57.     end
  58. end
  59.  
  60. function MT:setDefaultEnterButton(text)
  61.     self.defaultEnterButton = text
  62. end
  63.  
  64. function MT:setDefaultEscapeButton(text)
  65.     self.defaultEscapeButton = text
  66. end
  67.  
  68. function MT:setTitle(title)
  69.     self.title = tostring(title)
  70. end
  71.  
  72. function MT:setMessage(message)
  73.     self.message = tostring(message)
  74. end
  75.  
  76. local buttonOrder = {
  77.     [4] = {3, 4, 2, 1},
  78.     [3] = {2, 3, 1},
  79.     [2] = {1, 2},
  80.     [1] = {1}
  81. }
  82. function MT:create()
  83.     local modalWindow = modalWindows.modalWindowConstructor(self.id, self.title, self.message)
  84.     local order = buttonOrder[math.min(#self.buttons, 4)]
  85.  
  86.     if order then
  87.         for _, i in ipairs(order) do
  88.             local button = self.buttons[i]
  89.             modalWindow:addButton(i, button.text)
  90.             button.id = i
  91.  
  92.             if button.text == self.defaultEnterButton then
  93.                 modalWindow:setDefaultEnterButton(i)
  94.             elseif button.text == self.defaultEscapeButton then
  95.                 modalWindow:setDefaultEscapeButton(i)
  96.             end
  97.         end
  98.     end
  99.  
  100.     for _, choice in ipairs(self.choices) do
  101.         modalWindow:addChoice(_, choice.text)
  102.         choice.id = _
  103.     end
  104.  
  105.     self.modalWindow = modalWindow
  106. end
  107.  
  108. function MT:sendToPlayer(player)
  109.     if not self.modalWindow then
  110.         self:create()
  111.     end
  112.  
  113.     player:registerEvent('ModalWindowHelper')
  114.     self.players[player:getId()] = true
  115.     return self.modalWindow:sendToPlayer(player)
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement