Advertisement
Ceziy

GLib

Mar 10th, 2025 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.84 KB | None | 0 0
  1. --Graphics Lib by Ceziy
  2. callUpdt = true
  3. bgColor = colors.black
  4. space = term
  5. alive = true
  6. local shift_pressed = false
  7. scW, scH = space.getSize()
  8.  
  9. objects = {}
  10. buttons = {}
  11.  
  12. function setSpace(s)
  13.     space = s
  14. end
  15.  
  16. function termProg()
  17.     space.clear()
  18.     space.setCursorPos(1,1)
  19.     space.setBackgroundColor(colors.black)
  20.     space.setTextColor(colors.white)
  21.     alive = false
  22. end
  23.  
  24. function addCloseButton()
  25.     cl = Button(scW - 2, 1, 3, 1, colors.red, nil, colors.white, "x", function() termProg() end)
  26.     cl:blit()
  27. end
  28.  
  29. function centerText(w, ln)
  30.     return math.ceil((w - ln) / 2)
  31. end
  32.  
  33. function callUpdate()
  34.     callUpdt = true
  35. end
  36.  
  37. -- UI Object class
  38. UIObject = setmetatable(
  39.     {
  40.         __OBJECTS = {},
  41.     },
  42.     {
  43.         __call = function (self, x, y)
  44.             local __class__ = "UIObject"
  45.             local obj = {
  46.                     x = x or 1,
  47.                     y = y or 1,
  48.  
  49.                     getClass = function(self)
  50.                         return self.__class__
  51.                     end,
  52.                     getPos = function(self)
  53.                         return self.x, self.y
  54.                     end,
  55.                     setPos = function(self, x, y)
  56.                         self.x, self.y = x, y
  57.                         callUpdate()
  58.                     end,
  59.                     move = function(self, dx, dy)
  60.                         self.x, self.y = self.x + dx, self.y + dy
  61.                         callUpdate()
  62.                     end
  63.                 }
  64.             table.insert(self.__OBJECTS, obj)
  65.  
  66.             return obj
  67.         end
  68.     }
  69. )
  70.  
  71.  
  72. -- Text class
  73. Text = setmetatable({},
  74.     {
  75.         __index = UIObject,
  76.         __call = function (self, text, x, y, color)
  77.             local obj = UIObject(x, y)
  78.             obj.text = text or ""
  79.             obj.x = x or 1
  80.             obj.y = y or 1
  81.             obj.color = color or colors.white
  82.             obj.__class__ = "Text"
  83.  
  84.             obj.blit = function(self)
  85.                 table.insert(objects, self)
  86.             end
  87.             obj.setText = function(self, text)
  88.                 self.text = text
  89.                 callUpdate()
  90.             end
  91.             obj.getText = function(self)
  92.                 return self.text
  93.             end
  94.             obj.setColor = function(self, color)
  95.                 self.color = color
  96.                 callUpdate()
  97.             end
  98.             obj.draw = function(self)
  99.                 space.setCursorPos(self.x, self.y)
  100.                 space.setTextColor(self.color)
  101.                 space.write(self.text)
  102.             end
  103.  
  104.             return obj  
  105.         end
  106.     }
  107. )
  108.  
  109. -- Button Class
  110. Button = setmetatable({},
  111.     {
  112.         __index = UIObject,
  113.         __call = function (self, x, y, width, height, color, prColor, textColor, text, func)
  114.             local obj = UIObject(x, y)
  115.             obj.x = x or 1
  116.             obj.y = y or 1
  117.             obj.width = width - 1
  118.             obj.height = height - 1
  119.             obj.color = color or colors.lightGray
  120.             obj.prColor = prColor or colors.gray
  121.             obj.textColor = textColor or colors.white
  122.             obj.text = text or ""
  123.             obj.buffCol = obj.color
  124.             obj.pressed = false
  125.             obj.func = func or function() end
  126.             obj.__class__ = "Button"
  127.  
  128.             obj.blit = function(self)
  129.                 table.insert(objects, self)
  130.                 table.insert(buttons, self)
  131.             end
  132.             obj.draw = function(self)
  133.                 paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y + self.height, self.color)
  134.                 if #self.text > 0 and #self.text <= self.width then
  135.                     space.setBackgroundColor(self.color)
  136.                     space.setCursorPos(self.x + centerText(self.width, string.len(self.text)), self.y + centerText(self.height, 0))
  137.                     space.setTextColor(self.textColor)
  138.                     space.write(self.text)
  139.                     space.setBackgroundColor(bgColor)
  140.                 end
  141.             end
  142.             obj.setColor = function(self, color)
  143.                 self.color = color
  144.                 callUpdate()
  145.             end
  146.             obj.setPrColor = function(self, color)
  147.                 self.prColor = color
  148.                 callUpdate()
  149.             end
  150.  
  151.             return obj
  152.         end
  153.     }
  154. )
  155.  
  156. -- Prograss bar class
  157. ProgressBar = setmetatable({},
  158.     {  
  159.         __index = UIObject,
  160.         __call = function (self, x, y, width, height, progColor, bgColor)
  161.             local obj = UIObject(x, y)
  162.             obj.x = x
  163.             obj.y = y
  164.             obj.width = width - 1
  165.             obj.height = height - 1
  166.             obj.progColor = progColor or colors.lime
  167.             obj.bgColor = bgColor or colors.white
  168.             obj.progress = 0
  169.             obj.__class__ = "ProgressBar"
  170.  
  171.             obj.blit = function(self)
  172.                 table.insert(objects, self)
  173.             end
  174.             obj.draw = function(self)
  175.                 paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y + self.height, self.bgColor)
  176.                 if self.progress > 0 then
  177.                     paintutils.drawFilledBox(self.x, self.y, self.x + math.floor(self.width * self.progress), self.y + self.height, self.progColor)
  178.                 end
  179.             end
  180.             obj.setProgress = function(self, value)
  181.                 self.progress = math.max(0, math.min(1, value))
  182.                 callUpdate()
  183.             end
  184.             obj.addProgress = function(self, value)
  185.                 value = self.progress + value
  186.                 if value > 1 then
  187.                     value = 1
  188.                 elseif value < 0 then
  189.                     value = 0
  190.                 end
  191.                 self.progress = value
  192.                 callUpdate()
  193.             end
  194.             obj.getProgress = function(self)
  195.                 return self.progress
  196.             end
  197.             obj.setBGColor = function(self, color)
  198.                 self.bgColor = color
  199.                 callUpdate()
  200.             end
  201.             obj.setProgColor = function(self, color)
  202.                 self.progColor = color
  203.                 callUpdate()
  204.             end
  205.  
  206.             return obj
  207.         end
  208.     }
  209. )
  210.  
  211. -- Checkbox class
  212. CheckBox = setmetatable({},
  213.     {  
  214.         __index = UIObject,
  215.         __call = function (self, x, y, bgColor, textColor, size)
  216.             local obj = UIObject(x, y)
  217.             obj.x = x or 1
  218.             obj.y = y or 1
  219.             obj.size = size or 1
  220.             obj.width = obj.size - 1
  221.             obj.height = obj.size - 1
  222.             obj.bgColor = bgColor or colors.white
  223.             obj.textColor = textColor or colors.black
  224.             obj.symbol = "v"
  225.             obj.checked = false
  226.             obj.__class__ = "CheckBox"
  227.  
  228.             obj.blit = function(self)
  229.                 table.insert(objects, self)
  230.                 table.insert(buttons, self)
  231.             end
  232.             obj.draw = function(self)
  233.                 paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y + self.height, self.bgColor)
  234.                 if self.checked then
  235.                     space.setBackgroundColor(self.bgColor)
  236.                     space.setTextColor(self.textColor)
  237.                     space.setCursorPos(self.x + centerText(self.width, 1), self.y + centerText(self.width, 1))
  238.                     space.write(self.symbol)
  239.                 end
  240.             end
  241.             obj.isChecked = function(self)
  242.                 return self.checked
  243.             end
  244.             obj.setSymbol = function(self, symbol)
  245.                 self.symbol = symbol
  246.             end
  247.             obj.setBGColor = function(self, color)
  248.                 self.bgColor = color
  249.                 callUpdate()
  250.             end
  251.             obj.setTextColor = function(self, color)
  252.                 self.textColor = color
  253.                 callUpdate()
  254.             end
  255.             obj.setSize = function(self, size)
  256.                 self.width = size - 1
  257.                 self.height = size - 1
  258.                 callUpdate()
  259.             end
  260.             obj.getSize = function(self)
  261.                 return self.width + 1
  262.             end
  263.             return obj
  264.         end
  265.     }
  266. )
  267.  
  268. -- TextInput class
  269. TextInput = setmetatable({},
  270.     {  
  271.         __index = UIObject,
  272.         __call = function (self, x, y, width, bgColor, textColor)
  273.             local obj = UIObject(x, y)
  274.             obj.x = x or 1
  275.             obj.y = y or 1
  276.             obj.width = width or 1
  277.             obj.height = 1
  278.             obj.bgColor = bgColor or colors.white
  279.             obj.textColor = textColor or colors.gray
  280.             obj.text = ""
  281.             obj.active = false
  282.             obj.hide_text = false
  283.             obj.__class__ = "TextInput"
  284.  
  285.             obj.blit = function(self)
  286.                 table.insert(objects, self)
  287.                 table.insert(buttons, self)
  288.             end
  289.             obj.draw = function(self)
  290.                 paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y, self.bgColor)
  291.                 space.setCursorPos(self.x + 1, self.y)
  292.                 space.setTextColor(self.textColor)
  293.            
  294.                 local text_to_display = ""
  295.            
  296.                 if #self.text > self.width - 2 then
  297.                     text_to_display = self.text:sub(#self.text - (self.width - 2), -1)
  298.                 else
  299.                     text_to_display = self.text
  300.                 end
  301.            
  302.                 if self.hide_text then
  303.                     local st = "*"
  304.                     text_to_display = st:rep(#text_to_display)
  305.                 end
  306.            
  307.                 space.write(text_to_display .. (self.active and "_" or ""))
  308.             end
  309.             obj.hideText = function(self, bool)
  310.                 self.hide_text = bool
  311.             end
  312.             obj.setText = function(self, text)
  313.                 self.text = text
  314.                 callUpdate()
  315.             end
  316.             obj.getText = function(self)
  317.                 return self.text
  318.             end
  319.             obj.handleKey = function(self, key)
  320.                 local char = ""
  321.                 local keyName = keys.getName(key)
  322.  
  323.                 if key == keys.leftShift or key == keys.rightShift then
  324.                     shift_pressed = true
  325.                     return
  326.                 end
  327.            
  328.                 if keyName:match("^[%a]$") then
  329.                     char = shift_pressed and keyName:upper() or keyName
  330.                 elseif keyName == "one" then
  331.                     char = shift_pressed and "!" or "1"
  332.                 elseif keyName == "two" then
  333.                     char = shift_pressed and "@" or "2"
  334.                 elseif keyName == "three" then
  335.                     char = shift_pressed and "#" or "3"
  336.                 elseif keyName == "four" then
  337.                     char = shift_pressed and "$" or "4"
  338.                 elseif keyName == "five" then
  339.                     char = shift_pressed and "%" or "5"
  340.                 elseif keyName == "six" then
  341.                     char = shift_pressed and "^" or "6"
  342.                 elseif keyName == "seven" then
  343.                     char = shift_pressed and "&" or "7"
  344.                 elseif keyName == "eight" then
  345.                     char = shift_pressed and "*" or "8"
  346.                 elseif keyName == "nine" then
  347.                     char = shift_pressed and "(" or "9"
  348.                 elseif keyName == "zero" then
  349.                     char = shift_pressed and ")" or "0"
  350.                 elseif keyName == "space" then
  351.                     char = " "
  352.                 elseif keyName == "minus" then
  353.                     char = shift_pressed and "_" or "-"
  354.                 elseif keyName == "equals" then
  355.                     char = shift_pressed and "+" or "="
  356.                 elseif keyName == "period" then
  357.                     char = shift_pressed and ">" or "."
  358.                 elseif keyName == "comma" then
  359.                     char = shift_pressed and "<" or ","
  360.                 elseif keyName == "slash" then
  361.                     char = shift_pressed and "?" or "/"
  362.                 elseif keyName == "semiColon" then
  363.                     char = shift_pressed and ":" or ";"
  364.                 elseif keyName == "apostrophe" then
  365.                     char = shift_pressed and "\"" or "'"
  366.                 elseif keyName == "leftBracket" then
  367.                     char = shift_pressed and "{" or "["
  368.                 elseif keyName == "rightBracket" then
  369.                     char = shift_pressed and "}" or "]"
  370.                 elseif keyName == "backslash" then
  371.                     char = shift_pressed and "|" or "\\"
  372.                 elseif keyName == "grave" then
  373.                     char = shift_pressed and "~" or "`"
  374.                 end
  375.            
  376.                 self.text = self.text .. char
  377.            
  378.                 if not self.active then return end
  379.                 if key == keys.backspace then
  380.                     self.text = self.text:sub(1, -2)
  381.                 elseif key == keys.enter then
  382.                     self.active = false
  383.                 end
  384.                 callUpdate()
  385.             end
  386.             obj.setBGColor = function(self, color)
  387.                 self.bgColor = color
  388.                 callUpdate()
  389.             end
  390.             obj.setTextColor = function(self, color)
  391.                 self.textColor = color
  392.                 callUpdate()
  393.             end
  394.  
  395.             return obj
  396.         end
  397.     }
  398. )
  399.  
  400. --Loop class
  401. function mainloop(func)
  402.     while alive do
  403.         if type(func) == "function" then
  404.             func()
  405.         end
  406.         if callUpdt then
  407.             space.clear()
  408.             for _, obj in pairs(objects) do
  409.                 obj:draw()
  410.             end
  411.             callUpdt = false
  412.             space.setBackgroundColor(bgColor)
  413.         end
  414.         parallel.waitForAny(
  415.             function ()
  416.                 local event, p1, p2, p3 = os.pullEvent()
  417.                 if event == "mouse_click" then
  418.                     local mdbutton, mdx, mdy = p1, p2, p3
  419.                     if mdbutton == 1 then
  420.                         for _, btn in pairs(buttons) do
  421.                             if mdx >= btn.x and mdy >= btn.y and mdx <= btn.x + btn.width and mdy <= btn.y + btn.height then
  422.                                 if btn:getClass() == "Button" then
  423.                                     btn.color = btn.prColor
  424.                                     callUpdt = true
  425.                                     if not btn.pressed then
  426.                                         btn.func()
  427.                                     end
  428.                                     btn.pressed = true
  429.                                     callUpdate()
  430.                                 elseif btn:getClass() == "TextInput" then
  431.                                     if mdx >= btn.x and mdy == btn.y and mdx <= btn.x + btn.width then
  432.                                         btn.active = true
  433.                                     else
  434.                                         btn.active = false
  435.                                     end
  436.                                 elseif btn:getClass() == "CheckBox" then
  437.                                     if btn.checked then
  438.                                         btn.checked = false
  439.                                     else
  440.                                         btn.checked = true
  441.                                     end
  442.                                     callUpdate()
  443.                                 end
  444.                             else
  445.                                 if btn:getClass() == "TextInput" and btn.active then
  446.                                     btn.active = false
  447.                                     callUpdate()
  448.                                 end
  449.                             end
  450.                         end
  451.                     end
  452.                 end
  453.  
  454.                 if event == "mouse_up" then
  455.                     local mubutton, mux, muy = p1, p2, p3
  456.                     if mubutton == 1 then
  457.                         for _, btn in pairs(buttons) do
  458.                             btn.color = btn.buffCol
  459.                             btn.pressed = false
  460.                             callUpdate()
  461.                         end
  462.                     end
  463.                 end
  464.  
  465.                 if event == "key" then
  466.                     local key = p1
  467.                     for _, obj in pairs(objects) do
  468.                         if obj:getClass() == "TextInput" and obj.active then
  469.                             obj:handleKey(key)
  470.                         end
  471.                     end
  472.                 end
  473.  
  474.                 if event == "key_up" then
  475.                     local key = p1
  476.                     if key == keys.leftShift or key == keys.rightShift then
  477.                         shift_pressed = false
  478.                     end
  479.                 end
  480.             end
  481.         )
  482.        
  483.         os.sleep(0);
  484.     end
  485. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement