Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- gui library for LCARS V1.2015.10.09
- local gpu = require("component").gpu
- function setHexColor(n, r, g, b)
- local hexcolor = rgb2hex(r,g,b)
- hexcolortable[n] = hexcolor
- darkhexcolors[n] = bit32.rshift(bit32.band(hexcolor, 0xfefefe), 1)
- end
- function rgb2hex(r,g,b)
- return r*65536+g*256+b
- end
- colortable = {{255, 0, 0}, {0, 255, 0}, {0, 102, 255}}
- colortable[0] = {0, 0, 0}
- hexcolortable = {}
- darkhexcolors = {}
- for i=0,3 do setHexColor(i, colortable[i][1], colortable[i][2], colortable[i][3]) end
- -- Colors --
- backcolor = 0x000000
- forecolor = 0xFFFFFF
- -- ============================================= G R A P H I C S ============================================= --
- -- check screen/graphics card resolution; for comfortable work necessary resolution is >=HOLOW in height and width
- M = {}
- M.initialize = function()
- OLDWIDTH, OLDHEIGHT = gpu.getResolution()
- WIDTH, HEIGHT = gpu.maxResolution()
- gpu.setForeground(forecolor)
- gpu.setBackground(backcolor)
- end
- -- draw a frame
- function M.frame(x1, y1, x2, y2, caption)
- line(x1, x2, y1)
- line(x1, x2, y2)
- if caption ~= nil then
- gpu.set(x1+(x2-x1)/2-unicode.len(caption)/2, y1, caption)
- end
- end
- -- draw colored rectangle
- function M.drawRect(x, y, color)
- gpu.set(x, y, "+------+")
- gpu.set(x, y+1, "¦ ¦")
- gpu.set(x, y+2, "+------+")
- gpu.setForeground(color)
- gpu.set(x+2, y+1, "¦¦¦¦")
- gpu.setForeground(forecolor)
- end
- -- ============================================== B U T T O N S ============================================== --
- M.Button = {}
- M.Button.__index = M.Button
- function M.Button.new(func, x, y, text, color, width)
- self = setmetatable({}, Button)
- self.form = '[ '
- if width == nil then width = 0
- else width = (width - unicode.len(text))-4 end
- for i=1, math.floor(width/2) do
- self.form = self.form.. ' '
- end
- self.form = self.form..text
- for i=1, math.ceil(width/2) do
- self.form = self.form.. ' '
- end
- self.form = self.form..' ]'
- self.func = func
- self.x = x; self.y = y
- self.color = color
- self.visible = true
- return self
- end
- function M.Button:draw(color)
- if self.visible then
- local color = color or self.color
- gpu.setBackground(color)
- if color > 0x888888 then gpu.setForeground(backcolor) end
- gpu.set(self.x, self.y, self.form)
- gpu.setBackground(backcolor)
- if color > 0x888888 then gpu.setForeground(forecolor) end
- end
- end
- function M.Button:click(x, y)
- if self.visible then
- if y == self.y then
- if x >= self.x and x < self.x+unicode.len(self.form) then
- self.func()
- self:draw(self.color/2)
- os.sleep(0.1)
- self:draw()
- return true
- end
- end
- end
- return false
- end
- buttons = {}
- function M.buttonsNew(func, x, y, text, color, width)
- table.insert(buttons, M.Button.new(func, x, y, text, color, width))
- end
- function M.buttonsDraw()
- for i=1, #buttons do
- buttons[i]:draw()
- end
- end
- function M.buttonsClick(x, y)
- for i=1, #buttons do
- buttons[i]:click(x, y)
- end
- end
- -- ============================================ T E X T B O X E S ============================================ --
- M.Textbox = {}
- M.Textbox.__index = M.Textbox
- function M.Textbox.new(func, x, y, value, width)
- self = setmetatable({}, Textbox)
- self.form = '>'
- if width == nil then width = 10 end
- for i=1, width-1 do
- self.form = self.form..' '
- end
- self.func = func
- self.value = tostring(value)
- self.x = x; self.y = y
- self.visible = true
- return self
- end
- function M.Textbox:draw(content)
- if self.visible then
- if content then gpu.setBackground(graycolor) end
- gpu.set(self.x, self.y, self.form)
- if content then gpu.set(self.x+2, self.y, self.value) end
- gpu.setBackground(backcolor)
- end
- end
- function M.Textbox:click(x, y)
- if self.visible then
- if y == self.y then
- if x >= self.x and x < self.x+unicode.len(self.form) then
- self:draw(false)
- term.setCursor(self.x+2, self.y)
- value = string.sub(term.read({self.value}), 1, -2)
- if self.func(value) then
- self.value = value
- end
- self:draw(true)
- return true
- end
- end
- end
- return false
- end
- function M.Textbox:setValue(value)
- self.value = tostring(value)
- end
- function M.Textbox:getValue()
- return self.value
- end
- textboxes = {}
- function M.textboxesNew(func, x, y, value, width)
- textbox = M.Textbox.new(func, x, y, value, width)
- table.insert(textboxes, textbox)
- return textbox
- end
- function M.textboxesDraw()
- for i=1, #textboxes do
- textboxes[i]:draw(true)
- end
- end
- function M.textboxesClick(x, y)
- for i=1, #textboxes do
- textboxes[i]:click(x, y)
- end
- end
- return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement