MeXaN1cK

object.lua(PandaDoddo)

Jan 3rd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.77 KB | None | 0 0
  1. local component = require('component')
  2. local filesystem = require('filesystem')
  3. local unicode = require('unicode')
  4. local gpu = component.gpu
  5. local event = require('event')
  6. local keyboard = require('keyboard')
  7. local term = require('term')
  8. local serialization = require('serialization')
  9. local object = {}
  10. local rX ,rY = gpu.getResolution()
  11.  
  12. local function saveTbl(tbl, fl)
  13.   file = io.open(fl, 'w')
  14.   file:write(serialization.serialize(tbl))
  15.   file:close()
  16. end
  17.  
  18. local function loadTbl(fl)
  19.   file = io.open(fl, 'r')
  20.   if file == nil then
  21.     file = io.open(fl, 'w')
  22.     file:write('{}')
  23.   file:close()
  24.     return {}
  25.   else
  26.   file = io.open(fl, 'r')
  27.     return serialization.unserialize(file:read('*a'))
  28.   end
  29. end
  30.  
  31. --[Вспомогательные функции]--
  32. function object.drawImage(x,y,w,h,path)
  33.   arr = loadTbl(path)
  34.   for i=1,#arr do
  35.     if arr[i][1] <= w or arr[i][5] >= h then
  36.       change = false
  37.       oldPic={gpu.get(x+arr[i][1]-1,y+arr[i][2]-1)}
  38.       if arr[i][4]~=gpu.getForeground() and arr[i][4] ~= oldPic[2] then
  39.         object.setColor('f',arr[i][4])
  40.         change = true
  41.       end
  42.       if arr[i][5]~=gpu.getBackground() and arr[i][5] ~= oldPic[3] then
  43.         object.setColor('b',arr[i][5])
  44.       change = true
  45.       end
  46.     if change or ( arr[i][3] ~= oldPic[1] and change==true ) then
  47.         gpu.set(x+arr[i][1]-1,y+arr[i][2]-1,arr[i][3])
  48.     end
  49.     end
  50.   end
  51. end
  52.  
  53. function object.delInArr(arr,id)
  54.   local nar ={}
  55.   for i=1,#arr do
  56.     if i ~= id then
  57.     nar[#nar+1] = arr[i]
  58.   end
  59.   end
  60.   return nar
  61. end
  62.  
  63. function object.delObject(type,name)
  64.   type_id , name_id = object.getIDS(type,name)
  65.   types[type_id][2]=object.delInArr(types[type_id][2],name_id)  
  66. end
  67.  
  68. function object.getTypeID(type)
  69.   ID = nil
  70.   for i=1,#types do
  71.     if types[i][1] == type then
  72.     ID = i
  73.     break
  74.   end
  75.   end
  76.   return ID
  77. end
  78.  
  79. function object.getObjectID(type,nam)
  80.   ID,IDT = nil , nil
  81.   IDT = object.getTypeID(type)
  82.   for i=1,#types[IDT][2] do
  83.     if types[IDT][2][i].name == nam then
  84.     ID = i
  85.     break
  86.   end
  87.   end
  88.   return ID
  89. end
  90.  
  91. function object.getIDS(type,name)
  92.   return object.getTypeID(type) , object.getObjectID(type,name)
  93. end
  94.  
  95.  
  96. function getOld()
  97. oldFore,oldBack = gpu.getForeground(),gpu.getBackground()
  98. end
  99.  
  100. function setOld()
  101. object.setColor('f',oldFore)
  102. object.setColor('b',oldBack)
  103. end
  104.  
  105. function object.text(x,y,w,h,text,mode,alig)
  106.   if mode == 'last' then
  107.     if unicode.len(text) > w then
  108.       text = unicode.sub(text,unicode.len(text)-w+1,unicode.len(text))
  109.   end
  110.   elseif mode == 'first' then
  111.     if unicode.len(text) > w then
  112.       text = unicode.sub(text,0,w)
  113.   end
  114.   end
  115.   if alig == 'center' then
  116.     gpu.set(x+math.floor((w-unicode.len(text))/2),y+math.floor(h/2),text)
  117.   else
  118.     gpu.set(x,y,text)
  119.   end
  120. end
  121.  
  122. function object.square(x,y,w,h,char,fore,back)
  123.   getOld()
  124.   object.setColor('b',back)
  125.   object.setColor('f',fore)
  126.   gpu.fill(x,y,w,h,char)
  127.   setOld()
  128. end
  129.  
  130. function object.frame(x,y,w,h,style)
  131.   local styles = {{'┌','┐','└','┘','│','─'},{"╔","╗","╚","╝","║","═"}}
  132.   gpu.set(x,y,styles[style][1])
  133.   gpu.fill(x+1,y,w-1,1,styles[style][6])
  134.   gpu.set(x,y+h-1,styles[style][3])
  135.   gpu.fill(x,y+1,1,h-2,styles[style][5])
  136.   gpu.fill(x+w-1,y+1,1,h-2,styles[style][5])
  137.   gpu.set(x+w-1,y,styles[style][2])
  138.   gpu.fill(x+1,y+h-1,w-2,1,styles[style][6])
  139.   gpu.set(x+w-1,y+h-1,styles[style][4])
  140. end
  141. ------------------------------------------
  142. function object.drawStyleButton(x,y,w,back)
  143.   getOld()
  144.   object.setColor('f',back)
  145.   for i = 1 , w do
  146.     oldSymb = {gpu.get(x-1+i,y)}
  147.   if gpu.getBackground() ~= oldSymb[3] then object.setColor('b',oldSymb[3]) end
  148.   gpu.set(x-1+i,y,'▄')
  149.   end
  150.   object.setColor('b',back)
  151.   gpu.fill(x,y+1,w,1,' ')
  152.   for i = 1 , w do
  153.     oldSymb = {gpu.get(x-1+i,y+2)}
  154.   if gpu.getForeground() ~= oldSymb[3] then object.setColor('f',oldSymb[3]) end
  155.   gpu.set(x-1+i,y+2,'▄')
  156.   end
  157.   setOld()
  158. end
  159.  
  160. function object.addBoard(arr)
  161.   arg = {'x','y','w','h','fore','back','active_fore','active_back','isClicked','style','name'}
  162.   obj = object.addArg(arg,arr)
  163.   arr = types[object.getTypeID('board')][2]
  164.   arr[#arr+1] = obj
  165.   obj , arg  = nil , nil
  166. end
  167.  
  168. -- object.add('board',10,3,32,12,0xffffff,0x999999,0xFF00FF,123123,true,1,'FirstBoard')
  169. function object.drawBoard(name,arr,position)
  170.   getOld()
  171.   type_id , name_id = object.getIDS('board',name)
  172.   obj = types[type_id][2][name_id]
  173.   object.setColor('b',obj.back)
  174.   object.setColor('f',obj.fore)
  175.   gpu.fill(obj.x , obj.y , obj.w , obj.h , ' ')
  176.   tw,th = obj.w-2 , obj.h-2
  177.   deb = math.ceil(position / th)
  178.  
  179.   if obj.style ~= 0 then
  180.     object.frame(obj.x,obj.y,obj.w,obj.h,obj.style)
  181.   end
  182.   p = 0
  183.   k1 = deb*th-th
  184.   k2 = deb*th
  185.   if position > th then k1 = k1 + 1  k2 = k2 end
  186.   for  i = k1 , k2 do
  187.   if arr[i] ~= nil then
  188.     p = p + 1
  189.       if obj.isClicked == true and i == position then
  190.       object.setColor('b',obj.active_back)
  191.       object.setColor('f',obj.active_fore)
  192.     else
  193.       object.setColor('b',obj.back)
  194.       object.setColor('f',obj.fore)
  195.     end
  196.     gpu.fill(obj.x+1,obj.y+p,tw,1,' ')
  197.     object.text(obj.x+1 , obj.y+p , tw , 1 , arr[i],'first','')
  198.     end
  199.   end
  200.   setOld()
  201. end  
  202. ------------------------------------------
  203. function object.drawTriger(type,name)
  204.   type_id , name_id = object.getIDS('menu',name)
  205.   obj = types[type_id][2][name_id]
  206.   getOld()
  207.   if obj.state == obj.state1 then -- 1
  208.     object.setColor('f',obj.fore1)
  209.   object.setColor('b',obj.back1)
  210.   gpu.fill(obj.x1,obj.y1,obj.w1,obj.h1,' ')
  211.   object.text(obj.x1,obj.y1,obj.w1,obj.h1,obj.state,'first','center')
  212.   object.setColor('f',obj.fore3)
  213.   object.setColor('b',obj.back3)
  214.   gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
  215.   object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
  216.   else
  217.     object.setColor('f',obj.fore2)
  218.   object.setColor('b',obj.back2)
  219.   gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
  220.   object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
  221.   object.setColor('f',obj.fore4)
  222.   object.setColor('b',obj.back4)
  223.   gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
  224.   object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
  225.   end
  226. end
  227.  
  228. function object.triger(name,x,y)
  229.   type_id , name_id = object.getIDS('menu',name)
  230.   obj = types[type_id][2][name_id]
  231.   getOld()
  232.   for i=1,2 do
  233.     if x > obj.x1-1 and x < obj.x1+obj.w1 and y > obj.y1-1 and y < obj.y1+obj.h1 then
  234.     obj.state = obj.state1
  235.     break
  236.   elseif x > obj.x2-1 and x < obj.x2+obj.w2 and y > obj.y2-1 and y < obj.y2+obj.h2 then
  237.     obj.state = obj.state2
  238.     break
  239.     end
  240.   end
  241. end
  242.  
  243. function object.addTriger(arr)
  244.   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'}
  245.   obj = object.addArg(arg,arr)
  246.   arr = types[object.getTypeID('triger')][2]
  247.   arr[#arr+1] = obj
  248.   obj , arg  = nil , nil
  249. end
  250. ------------------------------------------
  251. function object.menu(name,x,y,value)
  252.   type_id , name_id = object.getIDS('menu',name)
  253.   obj = types[type_id][2][name_id]
  254.   getOld()
  255.   for i=1,#obj.arr do
  256.     if obj.orient == 'vertical' then
  257.       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
  258.         obj.draw = obj.position
  259.     obj.position = i
  260.     break
  261.       end
  262.     elseif obj.orient == 'horizontal' then
  263.       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
  264.         obj.draw = obj.position
  265.     obj.position = i
  266.     break
  267.       end
  268.   end  
  269.   end
  270.   if value then
  271.     object.drawObject('menu',name)
  272.   end
  273.   return obj.arr[obj.position]
  274. end
  275. ------------------------------------------
  276. function object.setColor(fb,color)
  277.   if fb == 'f' then
  278.     if gpu.getForeground() ~= color then
  279.     gpu.setForeground(tonumber(color))
  280.   end
  281.   elseif fb == 'b' then
  282.     if gpu.getBackground() ~= color then
  283.     gpu.setBackground(tonumber(color))
  284.   end  
  285.   end
  286. end
  287.  
  288. function object.drawObject(type,name)
  289.   type_id , name_id = object.getIDS(type,name)
  290.   obj = types[type_id][2][name_id]
  291.   getOld()
  292.   if type == 'textBox' then
  293.  
  294.   object.frame(obj.x , obj.y , obj.w , obj.h , obj.style)
  295.     object.setColor('f',obj.hint_fore) -- </draw textBox>
  296.   object.setColor('b',obj.back)
  297.   gpu.fill(obj.x+1,obj.y+1,obj.w-2,1,' ')
  298. object.text(obj.x+1,obj.y+1,obj.w-2,1,obj.hint,'first','')
  299.   elseif type == 'button' then
  300.     if obj.style == 1 then
  301.       object.setColor('b',obj.back)
  302.     object.setColor('f',obj.fore)
  303.       gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
  304.     object.text(obj.x,obj.y,obj.w,obj.h,obj.text,'first','center')
  305.   elseif obj.style == 2 then
  306.     object.drawStyleButton(obj.x,obj.y,obj.w,obj.back)
  307.       object.setColor('b',obj.back)
  308.     object.setColor('f',obj.fore)
  309.     object.text(obj.x,obj.y,obj.w,obj.h,obj.text,'first','center')
  310.   end
  311.   elseif type == 'menu' then
  312.     if obj.orient == 'vertical' then
  313.       len_but = obj.h / #obj.arr
  314.     if len_but > 3 then len_but = 3 end
  315.     elseif obj.orient == 'horizontal' then
  316.       len_but = obj.w / #obj.arr
  317.     end
  318.     if obj.draw ~= nil and obj.draw ~= obj.position then
  319.     if math.fmod(obj.draw,2) == 0 then      
  320.         object.setColor('b',obj.second_back)
  321.       object.setColor('f',obj.second_fore)
  322.       else
  323.         object.setColor('b',obj.back)
  324.       object.setColor('f',obj.fore)
  325.       end
  326.       gpu.fill(obj.x , obj.y+(len_but*(obj.draw-1)) , obj.w , len_but, ' ')
  327.       object.text(obj.x , obj.y+(len_but*(obj.draw-1)) , obj.w , len_but , obj.arr[obj.draw],'first','center')
  328.         object.setColor('b',obj.active_back)
  329.     object.setColor('f',obj.active_fore)
  330.       gpu.fill(obj.x , obj.y+(len_but*(obj.position-1)) , obj.w , len_but, ' ')
  331.       object.text(obj.x , obj.y+(len_but*(obj.position-1)) , obj.w , len_but , obj.arr[obj.position],'first','center')
  332.   elseif obj.draw == nil then
  333.       if obj.orient == 'horizontal' then
  334.         for i =1 ,#obj.arr do
  335.         if obj.position ~= i then
  336.         if math.fmod(i,2) == 0 then      
  337.             object.setColor('b',obj.second_back)
  338.           object.setColor('f',obj.second_fore)
  339.           else
  340.             object.setColor('b',obj.back)
  341.         object.setColor('f',obj.fore)
  342.           end
  343.       else
  344.           object.setColor('b',obj.active_back)
  345.       object.setColor('f',obj.active_fore)
  346.         end
  347.       gpu.fill(obj.x+(len_but*(i-1)) , obj.y , len_but , obj.h, ' ')
  348.         object.text(obj.x+(len_but*(i-1)) , obj.y , len_but , obj.h , obj.arr[i],'first','center')
  349.       end
  350.     elseif obj.orient == 'vertical' then
  351.       object.setColor('b',obj.back)
  352.       gpu.fill(obj.x,obj.y,obj.w,obj.h,' ')
  353.       for i =1 ,#obj.arr+1 do
  354.         if obj.position ~= i then
  355.         if math.fmod(i,2) == 0 then      
  356.             object.setColor('b',obj.second_back)
  357.           object.setColor('f',obj.second_fore)
  358.           else
  359.             object.setColor('b',obj.back)
  360.           object.setColor('f',obj.fore)
  361.           end
  362.       else
  363.           object.setColor('b',obj.active_back)
  364.           object.setColor('f',obj.active_fore)
  365.         end
  366.       if i ~= #obj.arr+1 then
  367.         gpu.fill(obj.x , obj.y+(len_but*(i-1)) , obj.w , len_but, ' ')
  368.           object.text(obj.x , obj.y+(len_but*(i-1)) , obj.w , len_but , obj.arr[i],'first','center')
  369.       else
  370.         gpu.fill(obj.x , obj.y+(len_but*(i-1)) , obj.w , obj.h - (len_but*(i-1)), ' ')
  371.       end
  372.       end
  373.     obj.draw = obj.position
  374.     end
  375.   end
  376.   elseif type == 'image' then
  377.     object.drawImage(obj.x,obj.y,obj.w,obj.h,obj.path)
  378.   elseif type == 'triger' then
  379.     object.drawTriger(type,name)
  380.   end
  381.   setOld()
  382. end
  383. ------------------------------------------
  384. -- object.add('button',10,5,8,3,'Кнопка',1,0xffffff,0xFFDD00,'Button_1')
  385. function object.addArg(arg,arr)
  386.   local obj = {}
  387.   for i=1,#arg do
  388.     obj[arg[i]] = arr[i]
  389.   end
  390.   return obj
  391. end
  392. -- object.add('image',10,5,10,5,icon.pic)
  393. function object.addImage(arr)
  394.   arg = {'x','y','path'}
  395.   obj = object.addArg(arg,arr)
  396.   arr = types[object.getTypeID('image')][2]
  397.   arr[#arr+1] = obj
  398.   obj , arg  = nil , nil
  399. end
  400. -- object.add('menu',1,1,120,3,'horizontal',0xffffff,0x999999,123123,0xffffff,0xffffff,0xAAAAAA,'TopMenu',{'Главная','Счета','Вклады','Переводы'},1)
  401. function object.addMenu(arr)
  402.   arg = {'x','y','w','h','orient','fore','back','active_back','active_fore','second_fore','second_back','name','arr','position'}
  403.   obj = object.addArg(arg,arr)
  404.   if obj.orient == 'vertical' then
  405.     len_but = obj.h / #obj.arr
  406.   if len_but > 3 then len_but = 3 end
  407.   elseif obj.orient == 'horizontal' then
  408.     len_but = obj.w / #obj.arr
  409.   end
  410.   butarr = {}
  411.   for i=1,#obj.arr do
  412.     if obj.orient == 'vertical' then
  413.       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}
  414.     elseif obj.orient == 'horizontal' then
  415.       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}
  416.   end  
  417.   end
  418.   obj.arr_bm = butarr
  419.   arr = types[object.getTypeID('menu')][2]
  420.   arr[#arr+1] = obj
  421.   obj , arg  = nil , nil
  422. end
  423.  
  424. function object.addButton(arr)
  425.   arg = {'x','y','w','h','text','style','fore','back','name'}
  426.   obj = object.addArg(arg,arr)
  427.   arr = types[object.getTypeID('button')][2]
  428.   arr[#arr+1] = obj
  429.   obj , arg  = nil , nil
  430. end
  431. --object.add('scroll',30,35,50,1,'|',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',30,35,50,1,'horizontal')
  432. --object.add('scroll',30,10,2,20,'|',0xFFFFFF,0xAAAAAA,0xffffff,0x505050,'FirstScroll',30,10,2,20,'vertical')
  433. function object.addScroll(arr)
  434.   arg = {'x','y','w','h','char','active_fore','active_back','fore','back','name','draw_x','draw_y','draw_w','draw_h','orient'}
  435.   obj = object.addArg(arg,arr)
  436.   arr = types[object.getTypeID('scroll')][2]
  437.   arr[#arr+1] = obj
  438.   obj , arg  = nil , nil
  439. end
  440. -- object.add('textBox',10,5,40,3,'Введите...',1,0x999999,0xffffff,0xFFDD00,0,0xffffff,0,'FirstBox')
  441. function object.addTextBox(arr)
  442.   arg = {'x','y','w','h','hint','style','hint_fore','char_color','active_fore','active_back','fore','back','name'}
  443.   obj = object.addArg(arg,arr)
  444.   arr = types[object.getTypeID('textBox')][2]
  445.   arr[#arr+1] = obj
  446.   obj , arg  = nil , nil
  447. end
  448. ------------------------------------------
  449.  
  450. function object.init(...)
  451.   args = {...}
  452.   if args[1] ~= nil and filesystem.exists(tostring(args[1])) then
  453.     types = loadTbl(tostring(args[1]))
  454.   return true
  455.   else
  456.     types = {
  457.   {'menu',{}},
  458.     {'board',{}},
  459.     {'button',{}},
  460.     {'scroll',{}},
  461.     {'indicator',{}},
  462.     {'lever',{}},
  463.     {'progressbar',{}},
  464.     {'image',{}},
  465.     {'text',{}},
  466.   {'triger',{}},
  467.     {'textBox',{}}
  468.     }
  469.     return false
  470.   end
  471. end
  472.  
  473. function object.saveInit(...)
  474.   args={...}
  475.   if args[1] ~= nil then
  476.     saveTbl(types,tostring(args[1]))
  477.   return true
  478.   else
  479.     return false
  480.   end
  481. end
  482.  
  483. function object.drawAllObject()
  484.   find = false
  485.   for i=1,#types do
  486.     for j=1,#types[i][2] do
  487.     if types[i][2][j].name ~= nil then
  488.       object.drawObject(types[i][1],types[i][2][j].name)
  489.     find = true
  490.     end
  491.   end
  492.   end
  493.   return find
  494. end
  495.  
  496. function object.getPressedObject(x,y)
  497.   find = false
  498.   for i=1,#types do
  499.     for j=1,#types[i][2] do
  500.     if types[i][2][j].name ~= nil then
  501.       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
  502.         find = {types[i][1],types[i][2][j].name}
  503.       break
  504.       end
  505.     end
  506.   end
  507.   end
  508.   return find
  509. end
  510.  
  511. function object.add(type , ...)
  512.   if type == 'textBox' then
  513.     object.addTextBox({...})
  514.   elseif type == 'button' then
  515.     object.addButton({...})
  516.   elseif type == 'scroll' then
  517.     object.addScroll({...})
  518.   elseif type == 'menu' then
  519.     object.addMenu({...})
  520.   elseif type == 'image' then
  521.     object.addImage({...})
  522.   elseif type == 'board' then
  523.     object.addBoard({...})
  524.   end
  525. end
  526.  
  527. function drawProgressBar()
  528.  
  529. end
  530.  
  531. function object.drawScroll(name,position,maxValue,onPage)
  532.   getOld()
  533.   IDTextBox , id = object.getIDS('scroll',name)
  534.   obj = types[IDTextBox][2][id]
  535.   position = position - 1
  536.   if maxValue == 0 then maxValue = 1 end
  537.   if obj.orient == 'horizontal' then
  538.     object.setColor('b',obj.back)
  539.     gpu.fill(obj.draw_x,obj.draw_y,obj.draw_w,obj.draw_h," ")
  540.   step = maxValue/obj.w
  541.   len_scroll = obj.draw_w/(maxValue/onPage) -- 10 w=100 mv = 100 oP = 10
  542.   newPos=math.floor(position/step)
  543.   object.setColor('b',obj.active_back)
  544.   object.setColor('f',obj.active_fore)
  545.   if position == maxValue then deb = math.ceil(len_scroll) else deb = 0 end
  546.   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
  547.   gpu.fill(obj.draw_x+newPos-deb , obj.draw_y , math.ceil(len_scroll) , obj.draw_h , obj.char)
  548.   elseif obj.orient == 'vertical' then
  549.     object.setColor('b',obj.back)
  550.     gpu.fill(obj.draw_x,obj.draw_y,obj.draw_w,obj.draw_h," ")
  551.   step = maxValue/obj.h
  552.   len_scroll = obj.draw_h/(maxValue/onPage) -- 10 w=100 mv = 100 oP = 10
  553.   newPos=math.floor(position/step)
  554.   object.setColor('b',obj.active_back)
  555.   object.setColor('f',obj.active_fore)
  556.   if position == maxValue then deb = math.ceil(len_scroll) else deb = 0 end
  557.   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
  558.   gpu.fill(obj.draw_x , obj.draw_y +newPos - deb, obj.draw_w , math.ceil(len_scroll), obj.char)
  559.   end
  560.   setOld()
  561. end
  562.  
  563. function object.textBox(name,mode,char)
  564.   IDTextBox , id = object.getIDS('textBox',name)
  565.   getOld()
  566.   local function getValue()
  567.     value = ''
  568.     obj = types[IDTextBox][2][id] -- <draw textBox>
  569.     object.setColor('b',obj.active_back)
  570.     object.setColor('f',obj.active_fore)
  571.     object.frame(obj.x , obj.y , obj.w , obj.h , obj.style)
  572.     object.setColor('f',obj.char_color)
  573.   local function getCode()
  574.     local key , _ , code , y , _ , _ = event.pull()
  575.     return key , y , code
  576.   end
  577.   function redrawCharInTextBox(mode,char)
  578.     if mode then
  579.       maskValue = ''
  580.       for i = 1 , unicode.len(value) do
  581.       maskValue = maskValue .. char
  582.     end
  583.     newValue = maskValue
  584.     else
  585.       newValue = value
  586.     end
  587.     gpu.fill(obj.x+1,obj.y+1,obj.w-2,1,' ')
  588.     if unicode.len(newValue) > obj.w - 3 then
  589.     term.setCursor(obj.x+obj.w-2,obj.y+1)
  590.     else
  591.       term.setCursor(obj.x+unicode.len(newValue)+1,obj.y+1)
  592.     end
  593.     term.setCursorBlink(true)
  594.     object.text(obj.x+1,obj.y+1,obj.w-3,1,newValue,'last','')
  595.   end
  596.   redrawCharInTextBox(mode,char)
  597.   while true do
  598.     err , key , y , code = pcall(getCode)
  599.     if err == true and key == 'key_down' or key == 'key_up' or 'clipboard' then
  600.       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
  601.         if keyboard.isShiftDown() then
  602.           value = value .. unicode.upper(unicode.char(code))
  603.         else
  604.           value = value .. unicode.char(code)
  605.         end
  606.           redrawCharInTextBox(mode,char)
  607.       elseif err == true and code == 13 or key == 'touch' then
  608.       break
  609.     elseif code == 8 and key == 'key_down' then
  610.       value = unicode.sub(value,0,unicode.len(value)-1)
  611.       redrawCharInTextBox(mode,char)
  612.     elseif key == 'clipboard' then
  613.       value = value .. code
  614.       redrawCharInTextBox(mode,char)
  615.     end
  616.       end
  617.   end
  618.   getOld()
  619.   object.setColor('f',obj.hint_fore) -- </draw textBox>
  620.     redrawCharInTextBox(mode,char)
  621.   term.setCursor(1,1)
  622.     term.setCursorBlink(false)
  623.   setOld()
  624.   object.drawObject('textBox',name)
  625.   return value
  626.   end
  627.   setOld()
  628.   return getValue()
  629. end
  630.  
  631. return object
Add Comment
Please, Sign In to add comment