Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cmp = require("component")
- local gpu = cmp.gpu
- local unicode = require('unicode')
- local serialization = require("serialization")
- local function loadTbl(fl) --Загрузить файл ввиде таблицы.
- file = io.open(fl, 'r')
- if file==nil then
- file = io.open(fl, 'w')
- file:write('{}')
- file:close()
- return {}
- else
- file = io.open(fl, 'r')
- return serialization.unserialize(file:read('*a'))
- end
- end
- local function loadClass(file) return loadTbl(file) end
- local function getOld() return {gpu.getBackground(),gpu.getForeground()} end
- local function setOld(arr) gpu.setBackground(arr[1]) gpu.setForeground(arr[2]) end
- local function drawText(obj,mode)
- local function fancyDraw(x,y,obj)
- local old = getOld()
- for i=1,unicode.len(obj.text) do
- local j=i-1
- local pix = {gpu.get(x+j,y)}
- local symb = string.sub(obj.text,i,i)
- gpu.setBackground(pix[3])
- gpu.setForeground(obj.foreground)
- gpu.set(x+j,y,symb)
- end
- setOld(old)
- end
- if obj.text_align == "top_left" then
- if mode ~= nil then fancyDraw(obj.x , obj.y, obj) else gpu.set(obj.x , obj.y,obj.text) end
- elseif obj.text_align == "top_center" then
- if mode ~= nil then fancyDraw(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y, obj) else gpu.set(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y,obj.text) end
- elseif obj.text_align == "top_right" then
- if mode ~= nil then fancyDraw(obj.x + obj.w - unicode.len(obj.text),obj) else gpu.set(obj.x + obj.w - unicode.len(obj.text), obj.y,obj.text) end
- elseif obj.text_align == "center_left" then
- if mode ~= nil then fancyDraw(obj.x , obj.y + math.floor(obj.h / 2 ) ,obj) else gpu.set(obj.x , obj.y + math.floor(obj.h / 2 ) ,obj.text) end
- elseif obj.text_align == "center" then
- if mode ~= nil then fancyDraw(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + math.floor(obj.h / 2 ) ,obj) else gpu.set(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + math.floor(obj.h / 2 ) ,obj.text) end
- elseif obj.text_align == "center_right" then
- if mode ~= nil then fancyDraw(obj.x + obj.w - unicode.len(obj.text) , obj.y + math.floor(obj.h / 2 ),obj) else gpu.set(obj.x + obj.w - unicode.len(obj.text) , obj.y + math.floor(obj.h / 2 ) ,obj.text) end
- elseif obj.text_align == "bottom_left" then
- if mode ~= nil then fancyDraw(obj.x , obj.y + obj.h -1,obj) else gpu.set(obj.x , obj.y + obj.h -1,obj.text) end
- elseif obj.text_align == "bottom_center" then
- if mode ~= nil then fancyDraw(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + obj.h -1,obj) else gpu.set(obj.x + math.floor((obj.w - unicode.len(obj.text)) / 2 ) , obj.y + obj.h -1,obj.text) end
- elseif obj.text_align == "bottom_right" then
- if mode ~= nil then fancyDraw(obj.x + obj.w - unicode.len(obj.text) , obj.y + obj.h -1,obj) else gpu.set(obj.x + obj.w - unicode.len(obj.text) , obj.y + obj.h -1,obj.text) end
- end
- end
- local obj = {}
- local function draw(obj)
- local old = getOld()
- --
- if obj.w == nil then obj.w = 1 end
- if obj.h == nil or obj.h < 1 then obj.h = 1 end
- if obj.x == nil then io.stderr("error: x is nil") os.exit() end
- if obj.y == nil then io.stderr("error: y is nil") os.exit() end
- --
- if obj.type == "button" then
- if obj.text == nil or unicode.len(obj.text) == 0 then obj.text = "button" end
- if obj.w < unicode.len(obj.text) then obj.w = unicode.len(obj.text) end
- alList = {top_left=true,top_center=true,top_right=true,center_left=true,center=true,center_right=true,bottom_left=true,bottom_center=true,bottom_right=true}
- if alList[obj.text_align] == nil then io.stderr("error: align is not (center)") os.exit() end
- gpu.setBackground(obj.background)
- gpu.setForeground(obj.foreground)
- gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
- drawText(obj)
- elseif obj.type == "scroll" then
- if obj.value > obj.maxValue then obj.value = obj.maxValue end
- if obj.value < 1 then obj.value = 1 end
- if obj.orientation == "vertical" then
- gpu.setBackground(obj.back1)
- gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
- local sizeOfScrollBar = math.ceil(1 / obj.maxValue * obj.h)
- local displayBarFrom = math.floor(obj.y + obj.h * ((obj.value - 1) / obj.maxValue))
- gpu.setBackground(obj.back2)
- gpu.fill(obj.x, displayBarFrom, obj.w, sizeOfScrollBar, ' ')
- sizeOfScrollBar, displayBarFrom = nil, nil
- elseif obj.orientation == "horizontal" then
- gpu.setBackground(obj.back1)
- gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
- local sizeOfScrollBar = math.ceil(1 / obj.maxValue * obj.w)
- local displayBarFrom = math.floor(obj.h + obj.w * ((obj.value - 1) / obj.maxValue))
- gpu.setBackground(obj.back2)
- gpu.fill(obj.x-1+displayBarFrom, obj.y, sizeOfScrollBar, obj.h, ' ')
- sizeOfScrollBar, displayBarFrom = nil, nil
- end
- elseif obj.type == "progressBar" then
- gpu.setBackground(obj.background)
- gpu.setForeground(obj.foreground)
- gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
- local activeWidth = math.floor(obj.w / 100 * obj.value)
- gpu.setBackground(obj.activeBackground)
- gpu.setForeground(obj.activeForeground)
- gpu.fill(obj.x,obj.y,activeWidth,obj.h,' ')
- obj.text = obj.value .. "%"
- drawText(obj,1)
- elseif obj.type == "frame" then
- if obj.w < unicode.len(obj.text) then obj.w = unicode.len(obj.text) end
- alList = {top_left=true,top_center=true,top_right=true,center_left=true,center=true,center_right=true,bottom_left=true,bottom_center=true,bottom_right=true}
- if alList[obj.text_align] == nil then io.stderr("error: align is not (center)") os.exit() end
- gpu.setBackground(obj.background)
- gpu.fill(obj.x,obj.y,obj.w,obj.h," ")
- gpu.setForeground(obj.foreground)
- drawText(obj)
- for i=1,#obj.value do
- obj.value[i].x = obj.x + obj.value[i].x
- obj.value[i].y = obj.y + obj.value[i].y
- obj.value[i].draw()
- end
- end
- setOld(old)
- end
- function obj.draw(obj)
- draw(obj)
- end
- function obj.button()
- local obj = {
- x,
- y,
- w,
- h,
- background=0,
- foreground=0xffffff,
- text_align="center",
- text="button",
- catch = false,
- type="button",
- id
- }
- function obj.setID(id) obj.id = id end
- function obj.getID() return obj.id end
- function obj.setX(x) obj.x = x end
- function obj.setY(y) obj.y = y end
- function obj.getX() return obj.x end
- function obj.getY() return obj.y end
- function obj.setBackground(color) obj.background = color end
- function obj.getBackground() return obj.background end
- function obj.setForeground(color) obj.foreground = color end
- function obj.getForeground() return obj.foreground end
- function obj.setW(w) obj.w = w end
- function obj.getW() return obj.w end
- function obj.setH(h) obj.h = h end
- function obj.getH() return obj.h end
- function obj.setText(text) obj.text = text end
- function obj.getText() return obj.text end
- function obj.setAlign(align) obj.text_align = align end
- function obj.getAlign() return obj.text_align end
- function obj.draw()
- draw(obj)
- end
- return obj
- end
- function obj.progressBar()
- local old = getOld()
- local obj = {
- x=1,
- y=1,
- w,
- h =1,
- foreground=0xffffff,
- background=0xaa0000,
- activeForeground=0xffffff,
- activeBackground=0x00aa00,
- value,
- id,
- type="progressBar",
- text_align="center"
- }
- function obj.setX(x) obj.x = x end
- function obj.setY(y) obj.y = y end
- function obj.getX() return obj.x end
- function obj.getY() return obj.y end
- function obj.setW(w) obj.w = w end
- function obj.getW() return obj.w end
- function obj.setH(h) obj.h = h end
- function obj.getH() return obj.h end
- function obj.setValue(val2) obj.value = val2 end
- function obj.getValue() return obj.value end
- function obj.setID(id) obj.id = id end
- function obj.getID() return obj.id end
- function obj.setActiveBackground(color) obj.activeBackground = color end
- function obj.getActiveBackground() return obj.activeBackground end
- function obj.setActiveForeground(color) obj.activeForeground = color end
- function obj.getActiveForeground() return obj.activeForeground end
- function obj.setForeground(fore) obj.foreground = fore end
- function obj.getForeground() return obj.foreground end
- function obj.setBackground(back) obj.background = back end
- function obj.getBackground() return obj.background end
- function obj.draw()
- draw(obj)
- end
- return obj
- end
- function obj.scroll()
- local obj = {
- x,
- y,
- w=1,
- h=1,
- back1=0,
- back2=0,
- orientation="vertical",
- maxValue=0,
- value=1,
- type="scroll",
- id
- }
- function obj.setX(x) obj.x = x end
- function obj.setY(y) obj.y = y end
- function obj.getX() return obj.x end
- function obj.getY() return obj.y end
- function obj.setW(w) obj.w = w end
- function obj.getW() return obj.w end
- function obj.setH(h) obj.h = h end
- function obj.getH() return obj.h end
- function obj.setBack1(back1) obj.back1 = back1 end
- function obj.getBack1() return obj.back1 end
- function obj.setBack2(back2) obj.back2 = back2 end
- function obj.getBack2() return obj.back2 end
- function obj.setOrientation(orint) obj.orientation = orint end
- function obj.getOrientation() return obj.orientation end
- function obj.setMaxValue(val) obj.maxValue = val end
- function obj.getMaxValue() return obj.maxValue end
- function obj.setValue(val2) obj.value = val2 end
- function obj.getValue() return obj.value end
- function obj.setID(id) obj.id = id end
- function obj.getID() return obj.id end
- function obj.draw()
- draw(obj)
- end
- return obj
- end
- function obj.frame()
- local obj={
- x,
- y,
- w,
- h,
- background=0,
- foreground=0xffffff,
- text_align="top_center",
- text,
- id,
- type="frame",
- value={}
- }
- function obj.setX(x) obj.x = x end
- function obj.setY(y) obj.y = y end
- function obj.getX() return obj.x end
- function obj.getY() return obj.y end
- function obj.setW(w) obj.w = w end
- function obj.getW() return obj.w end
- function obj.setH(h) obj.h = h end
- function obj.getH() return obj.h end
- function obj.setBackground(back) obj.background = back end
- function obj.getBackground() return obj.background end
- function obj.setForeground(fore) obj.foreground = fore end
- function obj.getForeground() return obj.foreground end
- function obj.setID(id) obj.id = id end
- function obj.getID() return obj.id end
- function obj.setText(name) obj.text = name end
- function obj.getText() return obj.text end
- function obj.setValue(val2) table.insert(obj.value , val2) end
- function obj.getValue() return obj.value end
- function obj.loadFile(file)
- obj.value = loadTbl(file)
- end
- function obj.draw()
- draw(obj)
- end
- return obj
- end
- function obj.catchEvent()
- -- {x,y,w,h,callback,id}
- local evnt = {
- touch={},
- scroll={},
- drag={},
- drop={}
- }
- function evnt.addEvent(object,event,callback,data)
- if evnt[event] ~= nil then
- table.insert(evnt[event],{x = object.x, y = object.y, w = object.w, h = object.h, id = object.id,callback = callback,data = data})
- end
- end
- function evnt.getEvent(arrEvent)
- if evnt[arrEvent[1]] ~= nil then
- local arr = evnt[arrEvent[1]]
- local ID = nil
- for i=1,#evnt[arrEvent[1]] do
- if arrEvent[3] > arr[i].x-1 and arrEvent[3] < arr[i].x+arr[i].w and arrEvent[4] > arr[i].y-1 and arrEvent[4] < arr[i].y+arr[i].h then
- ID = {arrEvent,id=arr[i].id}
- if type(arr[i].callback) == "function" then
- arr[i].callback(arr[i].data)
- end
- break
- end
- end
- return ID
- end
- end
- return evnt
- end
- return obj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement