Advertisement
1lann

dialog

Jun 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. Dialog = {}
  2. Dialog.__index = Dialog
  3.  
  4. Dialog.titleBarColor = colors.blue
  5. Dialog.titleTextColor = colors.white
  6. Dialog.contentBgColor = colors.lightGray
  7. Dialog.contentTextColor = colors.black
  8. Dialog.buttonColor = colors.white
  9. Dialog.buttonTextColor = colors.gray
  10.  
  11. function Dialog.new(x, y, width, height, title, textArray, okButton, cancelButton)
  12.     local self = setmetatable({}, Dialog)
  13.     self:init(x, y, width, height, title, textArray, okButton, cancelButton)
  14.     self:redraw(x, y)
  15.     return self
  16. end
  17.  
  18. function Dialog:init(x, y, width, height, title, textArray, okButton, cancelButton)
  19.     self.x = x
  20.     self.y = y
  21.     self.title = title
  22.     self.textArray = textArray
  23.     self.okButton = okButton
  24.     self.cancelButton = cancelButton
  25.     self.textArray = textArray
  26.  
  27.     if not height then
  28.         if okButton then
  29.             self.height = #textArray + 5
  30.         else
  31.             self.height = #textArray + 3
  32.         end
  33.     end
  34.  
  35.     if not width then
  36.         local widest = 0
  37.         for _, v in pairs(textArray) do
  38.             widest = math.max(widest, #v)
  39.         end
  40.         self.width = widest+4
  41.     end
  42. end
  43.  
  44. function Dialog:redraw(x, y)
  45.     local wid, hei = term.getSize()
  46.    
  47.     if not x then
  48.         x = math.floor(wid/2 - self.width/2 + 0.5)
  49.     end
  50.     if not y then
  51.         y = math.floor(hei/2 - self.height/2 + 0.5)
  52.     end
  53.    
  54.     self.x = x
  55.     self.y = y
  56.    
  57.     -- Draw title bar
  58.     term.setCursorPos(self.x, self.y)
  59.     term.setBackgroundColor(Dialog.titleBarColor)
  60.     term.setTextColor(Dialog.titleTextColor)
  61.  
  62.     term.write(string.rep(" ", self.width))
  63.     term.setCursorPos(self.x+math.floor((self.width/2)-(#self.title/2)), self.y)
  64.     term.write(self.title)
  65.  
  66.     -- Draw contents area
  67.     term.setBackgroundColor(Dialog.contentBgColor)
  68.     term.setTextColor(Dialog.contentTextColor)
  69.  
  70.     for i = 1, self.height-1 do
  71.         term.setCursorPos(self.x, self.y+i)
  72.         term.write(string.rep(" ", self.width))
  73.     end
  74.    
  75.     for i = 1, self.height-1 do
  76.         if i <= #self.textArray then
  77.             term.setCursorPos(self.x+math.floor((self.width/2)-(#self.textArray[i]/2)), self.y+i+1)
  78.             term.write(self.textArray[i])
  79.         else
  80.             break
  81.         end
  82.     end
  83.  
  84.     -- Draw buttons
  85.     term.setBackgroundColor(Dialog.buttonColor)
  86.     term.setTextColor(Dialog.buttonTextColor)
  87.  
  88.     if self.okButton then
  89.         self.buttonY = self.y+self.height-2
  90.         if self.cancelButton then
  91.             self.cancelButtonX = self.x+math.floor((self.width / 2) - 7) - 1
  92.             self.cancelButtonWidth = 8
  93.             term.setCursorPos(self.cancelButtonX, self.buttonY)
  94.             term.write(" CANCEL ")
  95.            
  96.             self.okButtonX = self.cancelButtonX + self.cancelButtonWidth + 4
  97.             self.okButtonWidth = 4
  98.             term.setCursorPos(self.okButtonX, self.buttonY)
  99.             term.write(" OK ")
  100.         else
  101.             self.okButtonX = self.x+math.floor((self.width / 2) - 1.5)
  102.             self.okButtonWidth = 4
  103.             term.setCursorPos(self.okButtonX, self.buttonY)
  104.             term.write(" OK ")
  105.         end
  106.     end
  107. end
  108.  
  109. function Dialog:getButtonPressed(x, y)
  110.     if self.okButtonX and (x >= self.okButtonX) and (x < self.okButtonX + self.okButtonWidth) and (y == self.buttonY) then
  111.         return "ok"
  112.     elseif self.cancelButtonX and (x >= self.cancelButtonX) and (x < self.cancelButtonX + self.cancelButtonWidth) and (y == self.buttonY) then
  113.         return "cancel"
  114.     else
  115.         return false
  116.     end
  117. end
  118.  
  119. function Dialog:autoCaptureEvents()
  120.     while true do
  121.         local e, button, x, y = os.pullEvent()
  122.         if e == "mouse_click" then
  123.             local ret = self:getButtonPressed(x, y)
  124.             if ret then
  125.                 return ret
  126.             end
  127.         elseif e == "key" and button == 28 then
  128.             return "ok"
  129.         end
  130.     end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement