Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require('component')
- local filesystem = require('filesystem')
- local unicode = require('unicode')
- local gpu = component.gpu
- local event = require('event')
- local keyboard = require('keyboard')
- local term = require('term')
- local serialization = require('serialization')
- local object = {}
- local rX ,rY = gpu.getResolution()
- local function saveTbl(tbl, fl)
- file = io.open(fl, 'w')
- file:write(serialization.serialize(tbl))
- file:close()
- end
- 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
- --[Вспомогательные функции]--
- function object.drawImage(x,y,w,h,path)
- arr = loadTbl(path)
- for i=1,#arr do
- if arr[i][1] <= w or arr[i][5] >= h then
- change = false
- oldPic={gpu.get(x+arr[i][1]-1,y+arr[i][2]-1)}
- if arr[i][4]~=gpu.getForeground() and arr[i][4] ~= oldPic[2] then
- object.setColor('f',arr[i][4])
- change = true
- end
- if arr[i][5]~=gpu.getBackground() and arr[i][5] ~= oldPic[3] then
- object.setColor('b',arr[i][5])
- change = true
- end
- if change or ( arr[i][3] ~= oldPic[1] and change==true ) then
- gpu.set(x+arr[i][1]-1,y+arr[i][2]-1,arr[i][3])
- end
- end
- end
- end
- function object.delInArr(arr,id)
- local nar ={}
- for i=1,#arr do
- if i ~= id then
- nar[#nar+1] = arr[i]
- end
- end
- return nar
- end
- function object.delObject(type,name)
- type_id , name_id = object.getIDS(type,name)
- types[type_id][2]=object.delInArr(types[type_id][2],name_id)
- end
- function object.getTypeID(type)
- ID = nil
- for i=1,#types do
- if types[i][1] == type then
- ID = i
- break
- end
- end
- return ID
- end
- function object.getObjectID(type,nam)
- ID,IDT = nil , nil
- IDT = object.getTypeID(type)
- for i=1,#types[IDT][2] do
- if types[IDT][2][i].name == nam then
- ID = i
- break
- end
- end
- return ID
- end
- function object.getIDS(type,name)
- return object.getTypeID(type) , object.getObjectID(type,name)
- end
- function getOld()
- oldFore,oldBack = gpu.getForeground(),gpu.getBackground()
- end
- function setOld()
- object.setColor('f',oldFore)
- object.setColor('b',oldBack)
- end
- function object.text(x,y,w,h,text,mode,alig)
- if mode == 'last' then
- if unicode.len(text) > w then
- text = unicode.sub(text,unicode.len(text)-w+1,unicode.len(text))
- end
- elseif mode == 'first' then
- if unicode.len(text) > w then
- text = unicode.sub(text,0,w)
- end
- end
- if alig == 'center' then
- gpu.set(x+math.floor((w-unicode.len(text))/2),y+math.floor(h/2),text)
- else
- gpu.set(x,y,text)
- end
- end
- function object.square(x,y,w,h,char,fore,back)
- getOld()
- object.setColor('b',back)
- object.setColor('f',fore)
- gpu.fill(x,y,w,h,char)
- setOld()
- end
- function object.frame(x,y,w,h,style)
- local styles = {{'┌','┐','└','┘','│','─'},{"╔","╗","╚","╝","║","═"}}
- gpu.set(x,y,styles[style][1])
- gpu.fill(x+1,y,w-1,1,styles[style][6])
- gpu.set(x,y+h-1,styles[style][3])
- gpu.fill(x,y+1,1,h-2,styles[style][5])
- gpu.fill(x+w-1,y+1,1,h-2,styles[style][5])
- gpu.set(x+w-1,y,styles[style][2])
- gpu.fill(x+1,y+h-1,w-2,1,styles[style][6])
- gpu.set(x+w-1,y+h-1,styles[style][4])
- end
- ------------------------------------------
- function object.drawStyleButton(x,y,w,back)
- getOld()
- object.setColor('f',back)
- for i = 1 , w do
- oldSymb = {gpu.get(x-1+i,y)}
- if gpu.getBackground() ~= oldSymb[3] then object.setColor('b',oldSymb[3]) end
- gpu.set(x-1+i,y,'▄')
- end
- object.setColor('b',back)
- gpu.fill(x,y+1,w,1,' ')
- for i = 1 , w do
- oldSymb = {gpu.get(x-1+i,y+2)}
- if gpu.getForeground() ~= oldSymb[3] then object.setColor('f',oldSymb[3]) end
- gpu.set(x-1+i,y+2,'▄')
- end
- setOld()
- end
- function object.addBoard(arr)
- arg = {'x','y','w','h','fore','back','active_fore','active_back','isClicked','style','name'}
- obj = object.addArg(arg,arr)
- arr = types[object.getTypeID('board')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- -- object.add('board',10,3,32,12,0xffffff,0x999999,0xFF00FF,123123,true,1,'FirstBoard')
- function object.drawBoard(name,arr,position)
- getOld()
- type_id , name_id = object.getIDS('board',name)
- obj = types[type_id][2][name_id]
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- gpu.fill(obj.x , obj.y , obj.w , obj.h , ' ')
- tw,th = obj.w-2 , obj.h-2
- deb = math.ceil(position / th)
- if obj.style ~= 0 then
- object.frame(obj.x,obj.y,obj.w,obj.h,obj.style)
- end
- p = 0
- k1 = deb*th-th
- k2 = deb*th
- if position > th then k1 = k1 + 1 k2 = k2 end
- for i = k1 , k2 do
- if arr[i] ~= nil then
- p = p + 1
- if obj.isClicked == true and i == position then
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- else
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- end
- gpu.fill(obj.x+1,obj.y+p,tw,1,' ')
- object.text(obj.x+1 , obj.y+p , tw , 1 , arr[i],'first','')
- end
- end
- setOld()
- end
- ------------------------------------------
- function object.drawTriger(name)
- type = 'triger'
- type_id , name_id = object.getIDS('menu',name)
- obj = types[type_id][2][name_id]
- getOld()
- if obj.state == obj.state1 then -- 1
- object.setColor('f',obj.fore1)
- object.setColor('b',obj.back1)
- gpu.fill(obj.x1,obj.y1,obj.w1,obj.h1,' ')
- object.text(obj.x1,obj.y1,obj.w1,obj.h1,obj.state,'first','center')
- object.setColor('f',obj.fore3)
- object.setColor('b',obj.back3)
- gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
- object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
- else
- object.setColor('f',obj.fore2)
- object.setColor('b',obj.back2)
- gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
- object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
- object.setColor('f',obj.fore4)
- object.setColor('b',obj.back4)
- gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
- object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
- end
- end
- function object.triger(name,x,y)
- type_id , name_id = object.getIDS('menu',name)
- obj = types[type_id][2][name_id]
- getOld()
- for i=1,2 do
- if x > obj.x1-1 and x < obj.x1+obj.w1 and y > obj.y1-1 and y < obj.y1+obj.h1 then
- obj.state = obj.state1
- break
- elseif x > obj.x2-1 and x < obj.x2+obj.w2 and y > obj.y2-1 and y < obj.y2+obj.h2 then
- obj.state = obj.state2
- break
- end
- end
- end
- function object.addTriger(arr)
- arg = {'x','y','w','h','x1','y1','w1','h1','state1','x2','y2','w2','h2','state2','fore1','back1','fore2','back2','fore3','back3','fore4','back4','state','name'}
- obj = object.addArg(arg,arr)
- arr = types[object.getTypeID('triger')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- ------------------------------------------
- function object.menu(name,x,y,value)
- type_id , name_id = object.getIDS('menu',name)
- obj = types[type_id][2][name_id]
- getOld()
- for i=1,#obj.arr do
- if obj.orient == 'vertical' then
- if x > obj.arr_bm[i][1] and x < obj.arr_bm[i][2] and y > obj.arr_bm[i][3] and y < obj.arr_bm[i][4] then
- obj.draw = obj.position
- obj.position = i
- break
- end
- elseif obj.orient == 'horizontal' then
- if x > obj.arr_bm[i][1] and x < obj.arr_bm[i][2] and y > obj.arr_bm[i][3] and y < obj.arr_bm[i][4] then
- obj.draw = obj.position
- obj.position = i
- break
- end
- end
- end
- if value then
- object.drawObject('menu',name)
- end
- return obj.arr[obj.position]
- end
- ------------------------------------------
- function object.setColor(fb,color)
- if fb == 'f' then
- if gpu.getForeground() ~= color then
- gpu.setForeground(tonumber(color))
- end
- elseif fb == 'b' then
- if gpu.getBackground() ~= color then
- gpu.setBackground(tonumber(color))
- end
- end
- end
- function object.drawObject(type,name)
- type_id , name_id = object.getIDS(type,name)
- obj = types[type_id][2][name_id]
- getOld()
- if type == 'textBox' then
- object.frame(obj.x , obj.y , obj.w , obj.h , obj.style)
- object.setColor('f',obj.hint_fore) -- </draw textBox>
- object.setColor('b',obj.back)
- gpu.fill(obj.x+1,obj.y+1,obj.w-2,1,' ')
- object.text(obj.x+1,obj.y+1,obj.w-2,1,obj.hint,'first','')
- elseif type == 'button' then
- if obj.style == 1 then
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
- object.text(obj.x,obj.y,obj.w,obj.h,obj.text,'first','center')
- elseif obj.style == 2 then
- object.drawStyleButton(obj.x,obj.y,obj.w,obj.back)
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- object.text(obj.x,obj.y,obj.w,obj.h,obj.text,'first','center')
- end
- elseif type == 'menu' then
- if obj.orient == 'vertical' then
- len_but = obj.h / #obj.arr
- if len_but > 3 then len_but = 3 end
- elseif obj.orient == 'horizontal' then
- len_but = obj.w / #obj.arr
- end
- if obj.draw ~= nil and obj.draw ~= obj.position then
- if math.fmod(obj.draw,2) == 0 then
- object.setColor('b',obj.second_back)
- object.setColor('f',obj.second_fore)
- else
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- end
- gpu.fill(obj.x , obj.y+(len_but*(obj.draw-1)) , obj.w , len_but, ' ')
- object.text(obj.x , obj.y+(len_but*(obj.draw-1)) , obj.w , len_but , obj.arr[obj.draw],'first','center')
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- gpu.fill(obj.x , obj.y+(len_but*(obj.position-1)) , obj.w , len_but, ' ')
- object.text(obj.x , obj.y+(len_but*(obj.position-1)) , obj.w , len_but , obj.arr[obj.position],'first','center')
- elseif obj.draw == nil then
- if obj.orient == 'horizontal' then
- for i =1 ,#obj.arr do
- if obj.position ~= i then
- if math.fmod(i,2) == 0 then
- object.setColor('b',obj.second_back)
- object.setColor('f',obj.second_fore)
- else
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- end
- else
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- end
- gpu.fill(obj.x+(len_but*(i-1)) , obj.y , len_but , obj.h, ' ')
- object.text(obj.x+(len_but*(i-1)) , obj.y , len_but , obj.h , obj.arr[i],'first','center')
- end
- elseif obj.orient == 'vertical' then
- object.setColor('b',obj.back)
- gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
- for i =1 ,#obj.arr+1 do
- if obj.position ~= i then
- if math.fmod(i,2) == 0 then
- object.setColor('b',obj.second_back)
- object.setColor('f',obj.second_fore)
- else
- object.setColor('b',obj.back)
- object.setColor('f',obj.fore)
- end
- else
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- end
- if i ~= #obj.arr+1 then
- gpu.fill(obj.x , obj.y+(len_but*(i-1)) , obj.w , len_but, ' ')
- object.text(obj.x , obj.y+(len_but*(i-1)) , obj.w , len_but , obj.arr[i],'first','center')
- else
- gpu.fill(obj.x , obj.y+(len_but*(i-1)) , obj.w , obj.h - (len_but*(i-1)), ' ')
- end
- end
- obj.draw = obj.position
- end
- end
- elseif type == 'image' then
- object.drawImage(obj.x,obj.y,obj.w,obj.h,obj.path)
- elseif type == 'triger' then
- object.drawTriger(name)
- end
- setOld()
- end
- ------------------------------------------
- -- object.add('button',10,5,8,3,'Кнопка',1,0xffffff,0xFFDD00,'Button_1')
- function object.addArg(arg,arr)
- local obj = {}
- for i=1,#arg do
- obj[arg[i]] = arr[i]
- end
- return obj
- end
- -- object.add('image',10,5,10,5,icon.pic)
- function object.addImage(arr)
- arg = {'x','y','path'}
- obj = object.addArg(arg,arr)
- arr = types[object.getTypeID('image')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- -- object.add('menu',1,1,120,3,'horizontal',0xffffff,0x999999,123123,0xffffff,0xffffff,0xAAAAAA,'TopMenu',{'Главная','Счета','Вклады','Переводы'},1)
- function object.addMenu(arr)
- arg = {'x','y','w','h','orient','fore','back','active_back','active_fore','second_fore','second_back','name','arr','position'}
- obj = object.addArg(arg,arr)
- if obj.orient == 'vertical' then
- len_but = obj.h / #obj.arr
- if len_but > 3 then len_but = 3 end
- elseif obj.orient == 'horizontal' then
- len_but = obj.w / #obj.arr
- end
- butarr = {}
- for i=1,#obj.arr do
- if obj.orient == 'vertical' then
- butarr[#butarr+1]={obj.x-1 , obj.x+obj.w , obj.y+(len_but*(i-1))-1 , obj.y+(len_but*(i-1))+len_but}
- elseif obj.orient == 'horizontal' then
- butarr[#butarr+1]={obj.x+(len_but*(i-1))-1 , obj.x+(len_but*(i-1))+len_but , obj.y-1 , obj.y+obj.h}
- end
- end
- obj.arr_bm = butarr
- arr = types[object.getTypeID('menu')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- function object.addButton(arr)
- arg = {'x','y','w','h','text','style','fore','back','name'}
- obj = object.addArg(arg,arr)
- arr = types[object.getTypeID('button')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- --object.add('scroll',30,35,50,1,'|',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',30,35,50,1,'horizontal')
- --object.add('scroll',30,10,2,20,'|',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',30,10,2,20,'vertical')
- function object.addScroll(arr)
- arg = {'x','y','w','h','char','active_fore','active_back','fore','back','name','draw_x','draw_y','draw_w','draw_h','orient'}
- obj = object.addArg(arg,arr)
- arr = types[object.getTypeID('scroll')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- -- object.add('textBox',10,5,40,3,'Введите...',1,0x999999,0xffffff,0xFFDD00,0,0xffffff,0,'FirstBox')
- function object.addTextBox(arr)
- arg = {'x','y','w','h','hint','style','hint_fore','char_color','active_fore','active_back','fore','back','name'}
- obj = object.addArg(arg,arr)
- arr = types[object.getTypeID('textBox')][2]
- arr[#arr+1] = obj
- obj , arg = nil , nil
- end
- ------------------------------------------
- function object.init(...)
- args = {...}
- if args[1] ~= nil and filesystem.exists(tostring(args[1])) then
- types = loadTbl(tostring(args[1]))
- return true
- else
- types = {
- {'menu',{}},
- {'board',{}},
- {'button',{}},
- {'scroll',{}},
- {'indicator',{}},
- {'lever',{}},
- {'progressbar',{}},
- {'image',{}},
- {'text',{}},
- {'triger',{}},
- {'textBox',{}}
- }
- return false
- end
- end
- function object.saveInit(...)
- args={...}
- if args[1] ~= nil then
- saveTbl(types,tostring(args[1]))
- return true
- else
- return false
- end
- end
- function object.drawAllObject()
- find = false
- for i=1,#types do
- for j=1,#types[i][2] do
- if types[i][2][j].name ~= nil then
- object.drawObject(types[i][1],types[i][2][j].name)
- find = true
- end
- end
- end
- return find
- end
- function object.getPressedObject(x,y)
- find = false
- for i=1,#types do
- for j=1,#types[i][2] do
- if types[i][2][j].name ~= nil then
- if x > types[i][2][j].x-1 and x < types[i][2][j].x+types[i][2][j].w and y > types[i][2][j].y-1 and y < types[i][2][j].y+types[i][2][j].h then
- find = {types[i][1],types[i][2][j].name}
- break
- end
- end
- end
- end
- return find
- end
- function object.add(type , ...)
- if type == 'textBox' then
- object.addTextBox({...})
- elseif type == 'button' then
- object.addButton({...})
- elseif type == 'scroll' then
- object.addScroll({...})
- elseif type == 'menu' then
- object.addMenu({...})
- elseif type == 'image' then
- object.addImage({...})
- elseif type == 'board' then
- object.addBoard({...})
- elseif type == 'triger' then
- object.addTriger({...})
- end
- end
- function drawProgressBar()
- end
- function object.drawScroll(name,position,maxValue,onPage)
- getOld()
- IDTextBox , id = object.getIDS('scroll',name)
- obj = types[IDTextBox][2][id]
- position = position - 1
- if maxValue == 0 then maxValue = 1 end
- if obj.orient == 'horizontal' then
- object.setColor('b',obj.back)
- gpu.fill(obj.draw_x,obj.draw_y,obj.draw_w,obj.draw_h," ")
- step = maxValue/obj.w
- len_scroll = obj.draw_w/(maxValue/onPage) -- 10 w=100 mv = 100 oP = 10
- newPos=math.floor(position/step)
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- if position == maxValue then deb = math.ceil(len_scroll) else deb = 0 end
- if obj.draw_x +newPos - deb + math.ceil(len_scroll) > obj.draw_x + obj.draw_h - 1 then len_scroll = obj.draw_y +newPos - deb - math.ceil(len_scroll) end
- gpu.fill(obj.draw_x+newPos-deb , obj.draw_y , math.ceil(len_scroll) , obj.draw_h , obj.char)
- elseif obj.orient == 'vertical' then
- object.setColor('b',obj.back)
- gpu.fill(obj.draw_x,obj.draw_y,obj.draw_w,obj.draw_h," ")
- step = maxValue/obj.h
- len_scroll = obj.draw_h/(maxValue/onPage) -- 10 w=100 mv = 100 oP = 10
- newPos=math.floor(position/step)
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- if position == maxValue then deb = math.ceil(len_scroll) else deb = 0 end
- if obj.draw_y +newPos - deb + math.ceil(len_scroll) > obj.draw_y + obj.draw_h - 1 then len_scroll = obj.draw_y+obj.draw_h - (obj.draw_y +newPos - deb) end
- gpu.fill(obj.draw_x , obj.draw_y +newPos - deb, obj.draw_w , math.ceil(len_scroll), obj.char)
- end
- setOld()
- end
- function object.textBox(name,mode,char)
- IDTextBox , id = object.getIDS('textBox',name)
- getOld()
- local function getValue()
- value = ''
- obj = types[IDTextBox][2][id] -- <draw textBox>
- object.setColor('b',obj.active_back)
- object.setColor('f',obj.active_fore)
- object.frame(obj.x , obj.y , obj.w , obj.h , obj.style)
- object.setColor('f',obj.char_color)
- local function getCode()
- local key , _ , code , y , _ , _ = event.pull()
- return key , y , code
- end
- function redrawCharInTextBox(mode,char)
- if mode then
- maskValue = ''
- for i = 1 , unicode.len(value) do
- maskValue = maskValue .. char
- end
- newValue = maskValue
- else
- newValue = value
- end
- gpu.fill(obj.x+1,obj.y+1,obj.w-2,1,' ')
- if unicode.len(newValue) > obj.w - 3 then
- term.setCursor(obj.x+obj.w-2,obj.y+1)
- else
- term.setCursor(obj.x+unicode.len(newValue)+1,obj.y+1)
- end
- term.setCursorBlink(true)
- object.text(obj.x+1,obj.y+1,obj.w-3,1,newValue,'last','')
- end
- redrawCharInTextBox(mode,char)
- while true do
- err , key , y , code = pcall(getCode)
- if err == true and key == 'key_down' or key == 'key_up' or 'clipboard' then
- if code ~= 0 and code ~= 13 and code ~= 8 and code ~= 9 and code ~= 200 and code ~= 208 and code ~= 203 and code ~= 205 and key == 'key_down' and keyboard.isControlDown() == false then
- if keyboard.isShiftDown() then
- value = value .. unicode.upper(unicode.char(code))
- else
- value = value .. unicode.char(code)
- end
- redrawCharInTextBox(mode,char)
- elseif err == true and code == 13 or key == 'touch' then
- break
- elseif code == 8 and key == 'key_down' then
- value = unicode.sub(value,0,unicode.len(value)-1)
- redrawCharInTextBox(mode,char)
- elseif key == 'clipboard' then
- value = value .. code
- redrawCharInTextBox(mode,char)
- end
- end
- end
- getOld()
- object.setColor('f',obj.hint_fore) -- </draw textBox>
- redrawCharInTextBox(mode,char)
- term.setCursor(1,1)
- term.setCursorBlink(false)
- setOld()
- object.drawObject('textBox',name)
- return value
- end
- setOld()
- return getValue()
- end
- return object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement