Advertisement
nik1111

Untitled

May 19th, 2022
570
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- Created by Grcpils
  3. -- 03/14/2021
  4. -- Github: https://github.com/grcpils/cc-gui_api
  5. -- Please do not delete this header ;)
  6. --
  7.  
  8. local Utils = require("GUI/utils")
  9.  
  10. local drawLabel = function(btn, state)
  11.     local Y = math.floor((btn.ymin + btn.ymax) / 2)
  12.     local X = math.floor((btn.xmax + btn.xmin - string.len(btn.label)) / 2) + 1
  13.     GUI_MONITOR.setBackgroundColor(btn.color[state])
  14.     GUI_MONITOR.setCursorPos(X, Y)
  15.     GUI_MONITOR.write(btn.label)
  16.     GUI_MONITOR.setBackgroundColor(colors.black)
  17. end
  18.  
  19. local draw = function(btn, state)
  20.     GUI_MONITOR.setBackgroundColor(btn.color[state])
  21.     Utils.drawShape(btn)
  22.     drawLabel(btn, state)
  23.     GUI_MONITOR.setBackgroundColor(colors.black)
  24. end
  25.  
  26. local drawAll = function()
  27.     table.foreach(GUI_BUTTONS, function (key, btn)
  28.         Utils.printInfo("[GUI] Draw button '%s' (id:%s)\n", btn.label, key)
  29.         draw(btn, "idle")
  30.     end)
  31. end
  32.  
  33. local flash = function(btn)
  34.     draw(btn, "active")
  35.     sleep(0.2)
  36.     draw(btn, "idle")
  37. end
  38.  
  39. local waitForClick = function(x, y)
  40.     table.foreach(GUI_BUTTONS, function (key, btn)
  41.         if y >= btn.ymin and y <= btn.ymax then
  42.             if x >= btn.xmin and x <= btn.xmax then
  43.                 flash(btn)
  44.                 btn.callback()
  45.                 return
  46.             end
  47.         end
  48.     end)
  49. end
  50.  
  51. local new = function(label, callback, pos, size, color)
  52.     local xmin, xmax, ymin, ymax = Utils.getCoordonate(pos, size)
  53.     local Button = {
  54.         label = label,
  55.         callback = callback,
  56.         xmin = xmin,
  57.         xmax = xmax,
  58.         ymin = ymin,
  59.         ymax = ymax,
  60.         color = color
  61.     }
  62.  
  63.     local setLabel = function (label)
  64.         Button.label = label
  65.         draw(Button, "idle")
  66.     end
  67.  
  68.     local setIdleColor = function (color)
  69.         Button.color["idle"] = color
  70.         draw(Button, "idle")
  71.     end
  72.  
  73.     local setActiveColor = function (color)
  74.         Button.color["active"] = color
  75.         draw(Button, "idle")
  76.     end
  77.  
  78.     local setColors = function (colors)
  79.         Button.color = colors
  80.         draw(Button, "idle")
  81.     end
  82.  
  83.     local setCallback = function (callback)
  84.         Button.callback = callback
  85.     end
  86.  
  87.     table.insert(GUI_BUTTONS, Button)
  88.     return {
  89.         setLabel = setLabel,
  90.         setIdleColor = setIdleColor,
  91.         setActiveColor = setActiveColor,
  92.         setColors = setColors,
  93.         setCallback = setCallback
  94.     }
  95. end
  96.  
  97. return {
  98.     new = new,
  99.     flash = flash,
  100.     waitForClick = waitForClick,
  101.     drawAll = drawAll
  102. }
Advertisement
RAW Paste Data Copied
Advertisement