Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Graphics Lib by Ceziy
- callUpdt = true
- bgColor = colors.black
- space = term
- alive = true
- local shift_pressed = false
- scW, scH = space.getSize()
- objects = {}
- buttons = {}
- function setSpace(s)
- space = s
- end
- function termProg()
- space.clear()
- space.setCursorPos(1,1)
- space.setBackgroundColor(colors.black)
- space.setTextColor(colors.white)
- alive = false
- end
- function addCloseButton()
- cl = Button(scW - 2, 1, 3, 1, colors.red, nil, colors.white, "x", function() termProg() end)
- cl:blit()
- end
- function centerText(w, ln)
- return math.ceil((w - ln) / 2)
- end
- function callUpdate()
- callUpdt = true
- end
- -- UI Object class
- UIObject = setmetatable(
- {
- __OBJECTS = {},
- },
- {
- __call = function (self, x, y)
- local __class__ = "UIObject"
- local obj = {
- x = x or 1,
- y = y or 1,
- getClass = function(self)
- return self.__class__
- end,
- getPos = function(self)
- return self.x, self.y
- end,
- setPos = function(self, x, y)
- self.x, self.y = x, y
- callUpdate()
- end,
- move = function(self, dx, dy)
- self.x, self.y = self.x + dx, self.y + dy
- callUpdate()
- end
- }
- table.insert(self.__OBJECTS, obj)
- return obj
- end
- }
- )
- -- Text class
- Text = setmetatable({},
- {
- __index = UIObject,
- __call = function (self, text, x, y, color)
- local obj = UIObject(x, y)
- obj.text = text or ""
- obj.x = x or 1
- obj.y = y or 1
- obj.color = color or colors.white
- obj.__class__ = "Text"
- obj.blit = function(self)
- table.insert(objects, self)
- end
- obj.setText = function(self, text)
- self.text = text
- callUpdate()
- end
- obj.getText = function(self)
- return self.text
- end
- obj.setColor = function(self, color)
- self.color = color
- callUpdate()
- end
- obj.draw = function(self)
- space.setCursorPos(self.x, self.y)
- space.setTextColor(self.color)
- space.write(self.text)
- end
- return obj
- end
- }
- )
- -- Button Class
- Button = setmetatable({},
- {
- __index = UIObject,
- __call = function (self, x, y, width, height, color, prColor, textColor, text, func)
- local obj = UIObject(x, y)
- obj.x = x or 1
- obj.y = y or 1
- obj.width = width - 1
- obj.height = height - 1
- obj.color = color or colors.lightGray
- obj.prColor = prColor or colors.gray
- obj.textColor = textColor or colors.white
- obj.text = text or ""
- obj.buffCol = obj.color
- obj.pressed = false
- obj.func = func or function() end
- obj.__class__ = "Button"
- obj.blit = function(self)
- table.insert(objects, self)
- table.insert(buttons, self)
- end
- obj.draw = function(self)
- paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y + self.height, self.color)
- if #self.text > 0 and #self.text <= self.width then
- space.setBackgroundColor(self.color)
- space.setCursorPos(self.x + centerText(self.width, string.len(self.text)), self.y + centerText(self.height, 0))
- space.setTextColor(self.textColor)
- space.write(self.text)
- space.setBackgroundColor(bgColor)
- end
- end
- obj.setColor = function(self, color)
- self.color = color
- callUpdate()
- end
- obj.setPrColor = function(self, color)
- self.prColor = color
- callUpdate()
- end
- return obj
- end
- }
- )
- -- Prograss bar class
- ProgressBar = setmetatable({},
- {
- __index = UIObject,
- __call = function (self, x, y, width, height, progColor, bgColor)
- local obj = UIObject(x, y)
- obj.x = x
- obj.y = y
- obj.width = width - 1
- obj.height = height - 1
- obj.progColor = progColor or colors.lime
- obj.bgColor = bgColor or colors.white
- obj.progress = 0
- obj.__class__ = "ProgressBar"
- obj.blit = function(self)
- table.insert(objects, self)
- end
- obj.draw = function(self)
- paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y + self.height, self.bgColor)
- if self.progress > 0 then
- paintutils.drawFilledBox(self.x, self.y, self.x + math.floor(self.width * self.progress), self.y + self.height, self.progColor)
- end
- end
- obj.setProgress = function(self, value)
- self.progress = math.max(0, math.min(1, value))
- callUpdate()
- end
- obj.addProgress = function(self, value)
- value = self.progress + value
- if value > 1 then
- value = 1
- elseif value < 0 then
- value = 0
- end
- self.progress = value
- callUpdate()
- end
- obj.getProgress = function(self)
- return self.progress
- end
- obj.setBGColor = function(self, color)
- self.bgColor = color
- callUpdate()
- end
- obj.setProgColor = function(self, color)
- self.progColor = color
- callUpdate()
- end
- return obj
- end
- }
- )
- -- Checkbox class
- CheckBox = setmetatable({},
- {
- __index = UIObject,
- __call = function (self, x, y, bgColor, textColor, size)
- local obj = UIObject(x, y)
- obj.x = x or 1
- obj.y = y or 1
- obj.size = size or 1
- obj.width = obj.size - 1
- obj.height = obj.size - 1
- obj.bgColor = bgColor or colors.white
- obj.textColor = textColor or colors.black
- obj.symbol = "v"
- obj.checked = false
- obj.__class__ = "CheckBox"
- obj.blit = function(self)
- table.insert(objects, self)
- table.insert(buttons, self)
- end
- obj.draw = function(self)
- paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y + self.height, self.bgColor)
- if self.checked then
- space.setBackgroundColor(self.bgColor)
- space.setTextColor(self.textColor)
- space.setCursorPos(self.x + centerText(self.width, 1), self.y + centerText(self.width, 1))
- space.write(self.symbol)
- end
- end
- obj.isChecked = function(self)
- return self.checked
- end
- obj.setSymbol = function(self, symbol)
- self.symbol = symbol
- end
- obj.setBGColor = function(self, color)
- self.bgColor = color
- callUpdate()
- end
- obj.setTextColor = function(self, color)
- self.textColor = color
- callUpdate()
- end
- obj.setSize = function(self, size)
- self.width = size - 1
- self.height = size - 1
- callUpdate()
- end
- obj.getSize = function(self)
- return self.width + 1
- end
- return obj
- end
- }
- )
- -- TextInput class
- TextInput = setmetatable({},
- {
- __index = UIObject,
- __call = function (self, x, y, width, bgColor, textColor)
- local obj = UIObject(x, y)
- obj.x = x or 1
- obj.y = y or 1
- obj.width = width or 1
- obj.height = 1
- obj.bgColor = bgColor or colors.white
- obj.textColor = textColor or colors.gray
- obj.text = ""
- obj.active = false
- obj.hide_text = false
- obj.__class__ = "TextInput"
- obj.blit = function(self)
- table.insert(objects, self)
- table.insert(buttons, self)
- end
- obj.draw = function(self)
- paintutils.drawFilledBox(self.x, self.y, self.x + self.width, self.y, self.bgColor)
- space.setCursorPos(self.x + 1, self.y)
- space.setTextColor(self.textColor)
- local text_to_display = ""
- if #self.text > self.width - 2 then
- text_to_display = self.text:sub(#self.text - (self.width - 2), -1)
- else
- text_to_display = self.text
- end
- if self.hide_text then
- local st = "*"
- text_to_display = st:rep(#text_to_display)
- end
- space.write(text_to_display .. (self.active and "_" or ""))
- end
- obj.hideText = function(self, bool)
- self.hide_text = bool
- end
- obj.setText = function(self, text)
- self.text = text
- callUpdate()
- end
- obj.getText = function(self)
- return self.text
- end
- obj.handleKey = function(self, key)
- local char = ""
- local keyName = keys.getName(key)
- if key == keys.leftShift or key == keys.rightShift then
- shift_pressed = true
- return
- end
- if keyName:match("^[%a]$") then
- char = shift_pressed and keyName:upper() or keyName
- elseif keyName == "one" then
- char = shift_pressed and "!" or "1"
- elseif keyName == "two" then
- char = shift_pressed and "@" or "2"
- elseif keyName == "three" then
- char = shift_pressed and "#" or "3"
- elseif keyName == "four" then
- char = shift_pressed and "$" or "4"
- elseif keyName == "five" then
- char = shift_pressed and "%" or "5"
- elseif keyName == "six" then
- char = shift_pressed and "^" or "6"
- elseif keyName == "seven" then
- char = shift_pressed and "&" or "7"
- elseif keyName == "eight" then
- char = shift_pressed and "*" or "8"
- elseif keyName == "nine" then
- char = shift_pressed and "(" or "9"
- elseif keyName == "zero" then
- char = shift_pressed and ")" or "0"
- elseif keyName == "space" then
- char = " "
- elseif keyName == "minus" then
- char = shift_pressed and "_" or "-"
- elseif keyName == "equals" then
- char = shift_pressed and "+" or "="
- elseif keyName == "period" then
- char = shift_pressed and ">" or "."
- elseif keyName == "comma" then
- char = shift_pressed and "<" or ","
- elseif keyName == "slash" then
- char = shift_pressed and "?" or "/"
- elseif keyName == "semiColon" then
- char = shift_pressed and ":" or ";"
- elseif keyName == "apostrophe" then
- char = shift_pressed and "\"" or "'"
- elseif keyName == "leftBracket" then
- char = shift_pressed and "{" or "["
- elseif keyName == "rightBracket" then
- char = shift_pressed and "}" or "]"
- elseif keyName == "backslash" then
- char = shift_pressed and "|" or "\\"
- elseif keyName == "grave" then
- char = shift_pressed and "~" or "`"
- end
- self.text = self.text .. char
- if not self.active then return end
- if key == keys.backspace then
- self.text = self.text:sub(1, -2)
- elseif key == keys.enter then
- self.active = false
- end
- callUpdate()
- end
- obj.setBGColor = function(self, color)
- self.bgColor = color
- callUpdate()
- end
- obj.setTextColor = function(self, color)
- self.textColor = color
- callUpdate()
- end
- return obj
- end
- }
- )
- --Loop class
- function mainloop(func)
- while alive do
- if type(func) == "function" then
- func()
- end
- if callUpdt then
- space.clear()
- for _, obj in pairs(objects) do
- obj:draw()
- end
- callUpdt = false
- space.setBackgroundColor(bgColor)
- end
- parallel.waitForAny(
- function ()
- local event, p1, p2, p3 = os.pullEvent()
- if event == "mouse_click" then
- local mdbutton, mdx, mdy = p1, p2, p3
- if mdbutton == 1 then
- for _, btn in pairs(buttons) do
- if mdx >= btn.x and mdy >= btn.y and mdx <= btn.x + btn.width and mdy <= btn.y + btn.height then
- if btn:getClass() == "Button" then
- btn.color = btn.prColor
- callUpdt = true
- if not btn.pressed then
- btn.func()
- end
- btn.pressed = true
- callUpdate()
- elseif btn:getClass() == "TextInput" then
- if mdx >= btn.x and mdy == btn.y and mdx <= btn.x + btn.width then
- btn.active = true
- else
- btn.active = false
- end
- elseif btn:getClass() == "CheckBox" then
- if btn.checked then
- btn.checked = false
- else
- btn.checked = true
- end
- callUpdate()
- end
- else
- if btn:getClass() == "TextInput" and btn.active then
- btn.active = false
- callUpdate()
- end
- end
- end
- end
- end
- if event == "mouse_up" then
- local mubutton, mux, muy = p1, p2, p3
- if mubutton == 1 then
- for _, btn in pairs(buttons) do
- btn.color = btn.buffCol
- btn.pressed = false
- callUpdate()
- end
- end
- end
- if event == "key" then
- local key = p1
- for _, obj in pairs(objects) do
- if obj:getClass() == "TextInput" and obj.active then
- obj:handleKey(key)
- end
- end
- end
- if event == "key_up" then
- local key = p1
- if key == keys.leftShift or key == keys.rightShift then
- shift_pressed = false
- end
- end
- end
- )
- os.sleep(0);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement