ArabianMine

GUI_MAX

Jun 23rd, 2022 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.93 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3. local event = require("event")
  4. local serialization = require("serialization")
  5. local unicode = require("unicode")
  6. local keyboard = require("keyboard")
  7.  
  8. local gpu = component.gpu
  9.  
  10. local w,h = gpu.maxResolution()
  11.  
  12. local GUI = {}
  13. GUIElements = {} --need to set local
  14.  
  15. local inFocus = nil
  16.  
  17. GUI.gpu = gpu
  18. GUI.background = 0x000000
  19.  
  20. function del(arr,index)
  21.     arr[index] = arr[#arr]
  22.     arr[#arr] = nil
  23. end
  24.  
  25. --TESTPRINT
  26. function pr(str)
  27. GUI.gpu.set(1,24,tostring(str).." ")
  28. end
  29.  
  30. --округление числа
  31. function math.round(num, idp)
  32.  local mult = 10^(idp or 0)
  33.  return math.floor(num * mult + 0.5) / mult
  34. end
  35.  
  36. function inRect(x,y,rect)
  37.     if x >= rect.left and x<= rect.left+rect.width-1 then
  38.         if y >= rect.top and y<= rect.top+rect.height-1 then
  39.             return true
  40.         end
  41.     end
  42.     return false
  43. end
  44.  
  45. function getClickedElement(x,y)
  46.     for k = #GUIElements,1,-1 do
  47.         local l,t = getAbsPos(GUIElements[k])
  48.         if inRect(x,y,{left=l,top=t,width=GUIElements[k].width,height=GUIElements[k].height}) then
  49.             return GUIElements[k]
  50.         end
  51.     end
  52. end
  53.  
  54. function getAbsPos(elem)
  55.     local t = elem.top
  56.     local l = elem.left
  57.     while elem.owner do
  58.         t = t + elem.owner.top-1
  59.         l = l + elem.owner.left-1
  60.         elem = elem.owner
  61.     end
  62.     return l,t
  63. end
  64.  
  65. function GUI.init()
  66. event.listen("touch", onTouch)
  67. event.listen("drag", onDrag)
  68. event.listen("drop", onDrop)
  69. event.listen("scroll", onScroll)
  70. gpu.setResolution(w,h)
  71. end
  72.  
  73. function GUI.Draw()
  74.     GUI.gpu.setBackground(GUI.background)
  75.     GUI.gpu.fill(1,1,w,h," ")
  76.    
  77.     for k = 1,#GUIElements do
  78.         if GUIElements[k] then
  79.             if GUIElements[k].Draw and not GUIElements[k].owner then
  80.                 GUIElements[k].Draw()
  81.             end
  82.         end
  83.     end
  84. end
  85.  
  86. function GUI.Destroy()
  87.     for k = 1,#GUIElements do
  88.         if GUIElements[k].Destroy then
  89.             GUIElements[k].Destroy()
  90.         end
  91.     end
  92.     cursor.stop()
  93. end
  94.  
  95. cursor = {}
  96. do --InitCursor
  97.     cursor.color = 0xffffee
  98.     cursor.x = 1
  99.     cursor.y = 1
  100.     cursor.length = 10
  101.     cursor.buffer = ""
  102.     cursor.isBlinking = false
  103.     cursor.Blink = {state=false,lastChar = "",caretChar=unicode.char(0x2588),timer=nil}
  104.    
  105.     function cursor.toggle()
  106.         if cursor.Blink.state then
  107.             GUI.gpu.set(cursor.cx,cursor.cy,cursor.Blink.lastChar)
  108.         else
  109.             cursor.Blink.lastChar = GUI.gpu.get(cursor.cx,cursor.cy)
  110.             GUI.gpu.set(cursor.cx,cursor.cy,cursor.Blink.caretChar)
  111.         end
  112.         cursor.Blink.state = not cursor.Blink.state
  113.     end
  114.    
  115.     function cursor.SetBlink(flag)
  116.         if flag then
  117.             if not cursor.Blink.timer then
  118.                 cursor.Blink.timer = event.timer(0.5,cursor.toggle,math.huge)
  119.                 cursor.isBlinking = true
  120.             end
  121.         else
  122.             if cursor.Blink.timer then
  123.                 event.cancel(cursor.Blink.timer)
  124.                 if cursor.Blink.state then cursor.toggle() end
  125.                 cursor.Blink.lastChar = ""
  126.                 cursor.Blink.timer = nil
  127.                 cursor.isBlinking = false
  128.             end
  129.         end
  130.     end
  131.    
  132.    
  133.     function cursor.init(settings)
  134.         cursor.x = settings.x or cursor.x
  135.         cursor.y = settings.y or cursor.y
  136.         cursor.length = settings.length or cursor.length
  137.         cursor.buffer = settings.buffer or cursor.buffer
  138.         cursor.cx = cursor.x + string.len(cursor.buffer)
  139.         cursor.cy = cursor.y
  140.        
  141.         event.listen("key_down",cursor.OnKeyDown)
  142.         cursor.SetBlink(true)
  143.     end
  144.    
  145.     function cursor.stop()
  146.         event.ignore("key_down",cursor.OnKeyDown)
  147.         cursor.lastValue = cursor.buffer
  148.         cursor.buffer = ""
  149.         cursor.SetBlink(false)
  150.         return cursor.lastValue
  151.     end
  152.    
  153.     function cursor.add(char)
  154.         if string.len(cursor.buffer)<cursor.length-1 then
  155.             cursor.buffer = cursor.buffer .. char
  156.             GUI.gpu.set(cursor.cx,cursor.cy,char)
  157.             cursor.cx = cursor.cx + 1
  158.         end
  159.     end
  160.    
  161.     function cursor.delete()
  162.         if string.len(cursor.buffer)>0 then
  163.             cursor.buffer = string.sub(cursor.buffer,1,string.len(cursor.buffer)-1)
  164.             GUI.gpu.set(cursor.cx-1,cursor.cy," ")
  165.             cursor.cx = cursor.cx - 1      
  166.         end
  167.     end
  168.    
  169.     function cursor.OnKeyDown(signal,addres,char,code)
  170.         cursor.SetBlink(false)
  171.         if code == keyboard.keys.back then
  172.             cursor.delete()
  173.         elseif code == keyboard.keys.enter then
  174.        
  175.         elseif not keyboard.isControl(char) then
  176.             cursor.add(unicode.char(char))
  177.             --GUI.gpu.set(1,1,cursor.buffer .. "   ")
  178.         end
  179.         cursor.toggle()
  180.         cursor.SetBlink(true)
  181.     end
  182.    
  183.     --function cursor.
  184. end
  185.  
  186.  
  187. -- classes
  188. function GUI.Button (args)
  189.     local button = {}
  190.     local settings = args or {}
  191.     button.width = settings.width or 6
  192.     button.height = settings.height or 1
  193.     button.top = settings.top or 1
  194.     button.left = settings.left or 1
  195.     button.text = settings.text or "Button"
  196.     button.textColor = settings.textColor or 0x000000
  197.     button.backgroundColor = settings.backgroundColor or 0x880000
  198.     button.unableColor = settings.unableColor or 0x404020
  199.     button.enable = settings.enable or true
  200.    
  201.     button.rect = {top = button.top,left = button.left, width = button.width, height = button.height}
  202.     button.type = "BUTTON"
  203.    
  204.     button.OnClick = nil
  205.    
  206.     button.index = #GUIElements+1
  207.     GUIElements[#GUIElements+1] = button
  208.    
  209.     function button.Draw(parameters)
  210.         local left,top = getAbsPos(button)
  211.         local params = parameters or {}
  212.         local bgColor = params.backgroundColor or button.backgroundColor
  213.        
  214.         if button.enable then
  215.             GUI.gpu.setBackground(bgColor)
  216.         else
  217.             GUI.gpu.setBackground(button.unableColor)
  218.         end
  219.         GUI.gpu.setForeground(button.textColor)
  220.         GUI.gpu.fill(left,top,button.width,button.height," ")
  221.         if string.len(button.text) < button.width then
  222.             local indent = math.floor((button.width - string.len(button.text))/2)
  223.             GUI.gpu.set(left+indent,top,button.text)
  224.         else
  225.             GUI.gpu.set(left,top,string.sub(button.text,1,button.width))
  226.         end
  227.        
  228.     end
  229.    
  230.     function button.Destroy()
  231.         del(GUIElements,button.index)
  232.         if button.timer then
  233.             event.cancel(button.timer)
  234.         end
  235.     end
  236.    
  237.     function button.touchController (...)
  238.         if button.enable then
  239.             button.Draw({backgroundColor=0x008800})
  240.             button.timer = event.timer(0.25,function() button.Draw() button.timer = nil end)
  241.             if button.OnClick then
  242.                 button.OnClick(...)
  243.             end
  244.         end
  245.     end
  246.    
  247.     return button
  248. end
  249.  
  250. function GUI.Label (args)
  251.     local label = {}
  252.     local settings = args or {}
  253.     label.width = settings.width or 6
  254.     label.height = settings.height or 1
  255.     label.top = settings.top or 1
  256.     label.left = settings.left or 1
  257.     label.text = settings.text or "label"
  258.     label.textColor = settings.textColor or 0x000000
  259.     label.backgroundColor = settings.backgroundColor or 0x880000
  260.    
  261.     label.rect = {top = label.top,left = label.left, width = label.width, height = label.height}
  262.     label.type = "LABEL"
  263.    
  264.     label.OnClick = nil
  265.    
  266.     label.index = #GUIElements+1
  267.     GUIElements[#GUIElements+1] = label
  268.    
  269.     function label.Draw()
  270.         local left,top = getAbsPos(button)
  271.         GUI.gpu.setBackground(label.backgroundColor)
  272.         GUI.gpu.setForeground(label.textColor)
  273.         GUI.gpu.fill(left,top,label.width,label.height," ")
  274.         if string.len(label.text) > label.width then
  275.             GUI.gpu.set(left,top,string.sub(label.text,1,label.width))
  276.         else
  277.             GUI.gpu.set(left,top,label.text)
  278.         end
  279.        
  280.     end
  281.    
  282.     function label.Destroy()
  283.         del(GUIElements,label.index)
  284.     end
  285.    
  286.     function label.touchController (...)
  287.         if label.OnClick then
  288.             label.OnClick(...)
  289.         end
  290.     end
  291.    
  292.     return label
  293. end
  294.  
  295.  
  296.  
  297. function GUI.Panel (args)
  298.     local FrameChars
  299.     local FrameCharsD = {lt = unicode.char(9556),rt = unicode.char(9559),lb = unicode.char(9562),rb = unicode.char(9565), hor = unicode.char(9552), ver = unicode.char(9553)}
  300.     local FrameCharsS = {lt = unicode.char(9556),rt = unicode.char(9559),lb = unicode.char(9562),rb = unicode.char(9565), hor = unicode.char(9552), ver = unicode.char(9553)}
  301.     local panel = {}
  302.     local settings = args or {}
  303.     panel.width = settings.width or 6
  304.     panel.height = settings.height or 1
  305.     panel.top = settings.top or 1
  306.     panel.left = settings.left or 1
  307.     panel.frame = settings.frame or "D"
  308.     panel.caption = settings.caption or ""
  309.     if panel.frame == "D" then
  310.         FrameChars = FrameCharsD
  311.     elseif panel.frame == "S" then
  312.         FrameChars = FrameCharsS
  313.     end
  314.     panel.frameColor = settings.textColor or 0xffffff
  315.     panel.backgroundColor = settings.backgroundColor or 0x0000ff
  316.     panel.items = {}
  317.     local elements = {}
  318.     panel.rect = {top = panel.top,left = panel.left, width = panel.width, height = panel.height}
  319.     panel.type = "PANEL"
  320.    
  321.     panel.OnClick = nil
  322.    
  323.     panel.index = #GUIElements+1
  324.     GUIElements[#GUIElements+1] = panel
  325.    
  326.     function panel.items.add(name,value)
  327.         if panel.items.has(name) then
  328.             error("List already have that element")
  329.         else
  330.             elements[name] = value
  331.             value.owner = panel
  332.         end
  333.     end
  334.    
  335.     function panel.items.remove(name)
  336.         if panel.items.has(name) then
  337.             elements[name].Destroy()
  338.             elements[name] = nil
  339.         end
  340.     end
  341.    
  342.     function panel.items.has(name)
  343.         if elements[name] then
  344.             return true
  345.         else
  346.             return false
  347.         end
  348.     end
  349.    
  350.     function panel.Draw()
  351.         local left,top = getAbsPos(panel)
  352.         GUI.gpu.setBackground(panel.backgroundColor)
  353.         GUI.gpu.setForeground(panel.frameColor)
  354.         GUI.gpu.fill(left,top,panel.width,panel.height," ")
  355.         if panel.frame then
  356.             GUI.gpu.set(left,top,FrameChars.lt)
  357.             for k = left+1,left+panel.width-2 do
  358.                 GUI.gpu.set(k,top,FrameChars.hor)
  359.             end
  360.             GUI.gpu.set(left+panel.width-1,top,FrameChars.rt)
  361.            
  362.             for k = top+1,top+panel.height-2 do
  363.                 GUI.gpu.set(left,k,FrameChars.ver)
  364.                 GUI.gpu.set(left+panel.width-1,k,FrameChars.ver)
  365.             end
  366.            
  367.             GUI.gpu.set(left,top+panel.height-1,FrameChars.lb)
  368.             for k = left+1,left+panel.width-2 do
  369.                 GUI.gpu.set(k,top+panel.height-1,FrameChars.hor)
  370.             end
  371.             GUI.gpu.set(left+panel.width-1,top+panel.height-1,FrameChars.rb)
  372.         end
  373.         if string.len(panel.caption) > 0 then
  374.             GUI.gpu.set(left+2,top," " .. panel.caption .. " ")
  375.         end
  376.        
  377.        
  378.         for k in pairs(elements) do
  379.             if elements[k].Draw then
  380.                  elements[k].Draw()
  381.             end
  382.         end
  383.        
  384.     end
  385.    
  386.     function panel.Destroy()
  387.         del(GUIElements,panel.index)
  388.         for k in pairs(elements) do
  389.             panel.items.remove(k)
  390.         end
  391.     end
  392.    
  393.     function panel.touchController (...)
  394.         if panel.OnClick then
  395.             panel.OnClick(...)
  396.         end
  397.     end
  398.    
  399.     return panel
  400. end
  401.  
  402. function GUI.Edit (args)
  403.     local edit = {}
  404.     local settings = args or {}
  405.     edit.width = settings.width or 6
  406.     edit.height = settings.height or 1
  407.     edit.top = settings.top or 1
  408.     edit.left = settings.left or 1
  409.     edit.text = settings.text or "edit"
  410.     edit.textColor = settings.textColor or 0x000000
  411.     edit.backgroundColor = settings.backgroundColor or 0x880000
  412.    
  413.     edit.rect = {top = edit.top,left = edit.left, width = edit.width, height = edit.height}
  414.     edit.type = "EDIT"
  415.    
  416.     edit.OnClick = nil
  417.    
  418.     edit.index = #GUIElements+1
  419.     GUIElements[#GUIElements+1] = edit
  420.    
  421.     function edit.Draw()
  422.         local left,top = getAbsPos(edit)
  423.         GUI.gpu.setBackground(edit.backgroundColor)
  424.         GUI.gpu.setForeground(edit.textColor)
  425.         GUI.gpu.fill(left,top,edit.width,edit.height," ")
  426.         if string.len(edit.text) > edit.width then
  427.             GUI.gpu.set(left,top,string.sub(edit.text,1,edit.width))
  428.         else
  429.             GUI.gpu.set(left,top,edit.text)
  430.         end
  431.        
  432.     end
  433.    
  434.     function edit.OnFocus()
  435.         GUI.gpu.setBackground(edit.backgroundColor)
  436.         GUI.gpu.setForeground(edit.textColor)
  437.         local left,top = getAbsPos(edit)
  438.         cursor.init({x=left,y=top,buffer=edit.text,length=edit.width})
  439.     end
  440.    
  441.     function edit.UnFocus()
  442.         edit.text = cursor.stop()
  443.     end
  444.    
  445.     function edit.Destroy()
  446.         del(GUIElements,edit.index)
  447.     end
  448.    
  449.     function edit.touchController (...)
  450.         if edit.OnClick then
  451.             edit.OnClick(...)
  452.         end
  453.     end
  454.    
  455.     return edit
  456. end
  457.  
  458. function GUI.Listbox (args)
  459.     local FrameChars = {lt = unicode.char(9556),rt = unicode.char(9559),lb = unicode.char(9562),rb = unicode.char(9565), hor = unicode.char(9552), ver = unicode.char(9553)}
  460.     local ControlChars = {arUp = unicode.char(9650),arDown = unicode.char(9660),caret = unicode.char(9608),caretLine = unicode.char(9617)}
  461.     local listbox = {}
  462.     local settings = args or {}
  463.     listbox.width = settings.width or 6
  464.     listbox.height = settings.height or 1
  465.     listbox.top = settings.top or 1
  466.     listbox.left = settings.left or 1
  467.     listbox.frame = settings.frame or true
  468.     listbox.frameColor = settings.frameColor or 0xffffff
  469.     listbox.textColor = settings.textColor or 0xffffff
  470.     listbox.selectColor = settings.selectColor or 0xff0000
  471.     listbox.selectTextColor = settings.selectTextColor or 0xffffff
  472.     listbox.backgroundColor = settings.backgroundColor or 0x0000ff
  473.     listbox.items = {}
  474.     listbox.rect = {top = listbox.top,left = listbox.left, width = listbox.width, height = listbox.height}
  475.     listbox.type = "LISTBOX"
  476.     listbox.topIndex = 1
  477.     listbox.selected = 0
  478.     listbox.items= {}
  479.    
  480.     listbox.Scroll = {position = 1}
  481.    
  482.     listbox.index = #GUIElements+1
  483.     GUIElements[#GUIElements+1] = listbox
  484.    
  485.     function listbox.items.add(text)
  486.         listbox.items[#listbox.items+1] = {text = text,OnSelect = nil}
  487.     end
  488.    
  489.     function listbox.items.count()
  490.         return #listbox.items
  491.     end
  492.    
  493.     function listbox.items.remove(index)
  494.         for k = index,#listbox.items do
  495.             listbox.items[k] = listbox.items[k+1]
  496.         end
  497.         if listbox.selected > #listbox.items then
  498.             listbox.selected = #listbox.items
  499.         end
  500.     end
  501.    
  502.     function listbox.Draw()
  503.         local left,top = getAbsPos(listbox)
  504.         GUI.gpu.setBackground(listbox.backgroundColor)
  505.         GUI.gpu.setForeground(listbox.frameColor)
  506.         GUI.gpu.fill(left,top,listbox.width,listbox.height," ")
  507.         if listbox.frame then
  508.             GUI.gpu.set(left,top,FrameChars.lt)
  509.             for k = left+1,left+listbox.width-2 do
  510.                 GUI.gpu.set(k,top,FrameChars.hor)
  511.             end
  512.             GUI.gpu.set(left+listbox.width-1,top,FrameChars.rt)
  513.            
  514.             for k = top+1,top+listbox.height-2 do
  515.                 GUI.gpu.set(left,k,FrameChars.ver)
  516.             end
  517.             listbox.DrawControl()
  518.            
  519.             GUI.gpu.set(left,top+listbox.height-1,FrameChars.lb)
  520.             for k = left+1,left+listbox.width-2 do
  521.                 GUI.gpu.set(k,top+listbox.height-1,FrameChars.hor)
  522.             end
  523.             GUI.gpu.set(left+listbox.width-1,top+listbox.height-1,FrameChars.rb)
  524.            
  525.             local count
  526.             if #listbox.items > listbox.height - 2 then
  527.                 count = listbox.height - 2
  528.             else
  529.                 count = #listbox.items
  530.             end
  531.            
  532.             GUI.gpu.setBackground(listbox.backgroundColor)
  533.             GUI.gpu.setForeground(listbox.textColor)
  534.             for k = 1, count do
  535.                 if listbox.items[k-1+listbox.topIndex] then
  536.                     if string.len(listbox.items[k-1+listbox.topIndex].text) > listbox.width - 2 then
  537.                         GUI.gpu.set(left+1,top+k,string.sub(listbox.items[k-1+listbox.topIndex].text,1,listbox.width-2))
  538.                     else
  539.                         GUI.gpu.set(left+1,top+k,listbox.items[k-1+listbox.topIndex].text)
  540.                     end
  541.                 end
  542.             end
  543.             listbox.DrawSelect(listbox.selected)
  544.            
  545.         end
  546.     end
  547.    
  548.     function listbox.DrawSelect(index)
  549.         local left,top = getAbsPos(listbox)
  550.         if listbox.selected >= listbox.topIndex and listbox.selected <= listbox.topIndex + listbox.height-3 then
  551.             GUI.gpu.setBackground(listbox.backgroundColor)
  552.             GUI.gpu.setForeground(listbox.textColor)
  553.            
  554.             local top = listbox.selected - listbox.topIndex + 2
  555.             GUI.gpu.fill(left+1,top,listbox.width-2,1," ")
  556.             if string.len(listbox.items[listbox.selected].text) > listbox.width - 2 then
  557.                 GUI.gpu.set(left+1,top,string.sub(listbox.items[listbox.selected].text,1,listbox.width-2))
  558.             else
  559.                 GUI.gpu.set(left+1,top,listbox.items[listbox.selected].text)
  560.             end
  561.         end
  562.        
  563.         if index >= listbox.topIndex and index <= listbox.topIndex + listbox.height-3 then
  564.             GUI.gpu.setBackground(listbox.selectColor)
  565.             GUI.gpu.setForeground(listbox.selectTextColor)
  566.            
  567.             local top = index - listbox.topIndex + 2
  568.             GUI.gpu.fill(left+1,top,listbox.width-2,1," ")
  569.             if string.len(listbox.items[index].text) > listbox.width - 2 then
  570.                 GUI.gpu.set(left+1,top,string.sub(listbox.items[index].text,1,listbox.width-2))
  571.             else
  572.                 GUI.gpu.set(left+1,top,listbox.items[index].text)
  573.             end
  574.         end
  575.     end
  576.    
  577.     function listbox.DrawControl()
  578.         local left,top = getAbsPos(listbox)
  579.         GUI.gpu.set(left+listbox.width-1,top+1,ControlChars.arUp)
  580.         for k = top+2,top+listbox.height-3 do
  581.             GUI.gpu.set(left+listbox.width-1,k,ControlChars.caretLine)
  582.         end
  583.         GUI.gpu.set(left+listbox.width-1,top+listbox.height-2,ControlChars.arDown)
  584.                
  585.         GUI.gpu.set(left+listbox.width-1,top+1 + listbox.Scroll.position,ControlChars.caret)   
  586.     end
  587.    
  588.    
  589.     function listbox.Destroy()
  590.         del(GUIElements,listbox.index)
  591.     end
  592.    
  593.     function listbox.touchController (...)
  594.         local args = {...}
  595.         local left,top = getAbsPos(listbox)
  596.         local x = args[3]
  597.         local y = args[4]
  598.         pr(tostring(x) .. " " .. y .. " " .. left .. " " ..top)
  599.         if x == left+ listbox.width-1 and y == top+1 then
  600.             listbox.OnUpControlClick()
  601.         elseif x == left+ listbox.width-1 and y == top+listbox.height-2 then
  602.             listbox.OnDownControlClick()
  603.         elseif x == left + listbox.width-1 and y >= top+2 and y <= top+listbox.height-3 then
  604.             listbox.OnScrollClick(y-(top+1))
  605.         elseif x > left and x<left + listbox.width - 1 and y > top and y <top+listbox.height -1 then
  606.             listbox.OnItemSelect(y-(top+1))
  607.         end
  608.     end
  609.    
  610.     function listbox.UpdateScrollPos()
  611.         local listHeight = listbox.height -2
  612.         local overCount = #listbox.items - listHeight
  613.         if overCount > 0 then
  614.             local linesInScrollPixel = overCount/listHeight
  615.             listbox.Scroll.position = math.floor((listbox.topIndex-1)/linesInScrollPixel)
  616.         else
  617.             listbox.Scroll.position = 1
  618.         end
  619.     end
  620.    
  621.     function listbox.OnUpControlClick()
  622.         if listbox.topIndex > 1 then
  623.             listbox.topIndex = listbox.topIndex - 1
  624.             listbox.UpdateScrollPos()
  625.             listbox.Draw()
  626.         end
  627.     end
  628.    
  629.     function listbox.OnDownControlClick()
  630.         local listHeight = listbox.height -2
  631.         local overCount = #listbox.items - listHeight
  632.         if listbox.topIndex <= overCount   then
  633.             listbox.topIndex = listbox.topIndex + 1
  634.             listbox.UpdateScrollPos()
  635.             listbox.Draw()
  636.         end
  637.     end
  638.    
  639.     function listbox.OnScrollClick(index)
  640.         local listHeight = listbox.height -2
  641.         local overCount = #listbox.items - listHeight
  642.         if overCount > 0 then
  643.             local linesInScrollPixel = overCount/listHeight
  644.             listbox.topIndex = math.round(1+ math.round(linesInScrollPixel*index))
  645.             listbox.Scroll.position = index
  646.         else
  647.             listbox.Scroll.position = 1
  648.             listbox.topIndex = 1
  649.         end
  650.         listbox.Draw()
  651.     end
  652.    
  653.     function listbox.OnItemSelect(index)
  654.         local selIndex = listbox.topIndex + index
  655.         if selIndex <= #listbox.items then
  656.             listbox.selected = selIndex
  657.             listbox.DrawSelect(selIndex)
  658.         end
  659.     end
  660.    
  661.     return listbox
  662. end
  663.  
  664. function onTouch(eventName,screenAddress, x, y, button, playerName)
  665.     local elem = getClickedElement(x,y)
  666.    
  667.     if inFocus then
  668.         if inFocus.UnFocus then
  669.             inFocus.UnFocus()
  670.             inFocus = nil
  671.         end
  672.     end
  673.    
  674.     if elem then
  675.         if elem.OnFocus then
  676.             elem.OnFocus()
  677.             inFocus = elem
  678.         end
  679.         if elem.touchController then
  680.             elem.touchController(eventName,screenAddress, x, y, button, playerName)
  681.         end
  682.     end
  683. end
  684.  
  685. function onDrag(eventName,screenAddress, x, y, button, playerName)
  686.     --print("drag")
  687. end
  688.  
  689. function onDrop(eventName,screenAddress, x, y, button, playerName)
  690.     --print("drop")
  691. end
  692.  
  693. function onScroll(eventName,screenAddress, x, y, direction, playerName)
  694.     --print("scroll")
  695. end
  696.  
  697. function GUI.Destroy()
  698.     event.ignore("touch", onTouch)
  699.     event.ignore("drag", onDrag)
  700.     event.ignore("drop", onDrop)
  701.     event.ignore("scroll", onScroll)
  702. end
  703.  
  704. return GUI
Add Comment
Please, Sign In to add comment