Advertisement
PandaDoddo72Rus

object.lua

Mar 31st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.59 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(name)
  204.   type = 'triger'
  205.   type_id , name_id = object.getIDS('menu',name)
  206.   obj = types[type_id][2][name_id]
  207.   getOld()
  208.   if obj.state == obj.state1 then -- 1
  209.     object.setColor('f',obj.fore1)
  210.     object.setColor('b',obj.back1)
  211.     gpu.fill(obj.x1,obj.y1,obj.w1,obj.h1,' ')
  212.     object.text(obj.x1,obj.y1,obj.w1,obj.h1,obj.state,'first','center')
  213.     object.setColor('f',obj.fore3)
  214.     object.setColor('b',obj.back3)
  215.     gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
  216.     object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
  217.   else
  218.     object.setColor('f',obj.fore2)
  219.     object.setColor('b',obj.back2)
  220.     gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
  221.     object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
  222.     object.setColor('f',obj.fore4)
  223.     object.setColor('b',obj.back4)
  224.     gpu.fill(obj.x2,obj.y2,obj.w2,obj.h2,' ')
  225.     object.text(obj.x2,obj.y2,obj.w2,obj.h2,obj.state,'first','center')
  226.   end
  227. end
  228.  
  229. function object.triger(name,x,y)
  230.   type_id , name_id = object.getIDS('menu',name)
  231.   obj = types[type_id][2][name_id]
  232.   getOld()
  233.   for i=1,2 do
  234.     if x > obj.x1-1 and x < obj.x1+obj.w1 and y > obj.y1-1 and y < obj.y1+obj.h1 then
  235.       obj.state = obj.state1
  236.       break
  237.     elseif x > obj.x2-1 and x < obj.x2+obj.w2 and y > obj.y2-1 and y < obj.y2+obj.h2 then
  238.       obj.state = obj.state2
  239.       break
  240.     end
  241.   end
  242. end
  243.  
  244. function object.addTriger(arr)
  245.   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'}
  246.   obj = object.addArg(arg,arr)
  247.   arr = types[object.getTypeID('triger')][2]
  248.   arr[#arr+1] = obj
  249.   obj , arg  = nil , nil
  250. end
  251. ------------------------------------------
  252. function object.menu(name,x,y,value)
  253.   type_id , name_id = object.getIDS('menu',name)
  254.   obj = types[type_id][2][name_id]
  255.   getOld()
  256.   for i=1,#obj.arr do
  257.     if obj.orient == 'vertical' then
  258.       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
  259.         obj.draw = obj.position
  260.         obj.position = i
  261.         break
  262.       end
  263.     elseif obj.orient == 'horizontal' then
  264.       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
  265.         obj.draw = obj.position
  266.         obj.position = i
  267.         break
  268.       end
  269.     end  
  270.   end
  271.   if value then
  272.     object.drawObject('menu',name)
  273.   end
  274.   return obj.arr[obj.position]
  275. end
  276. ------------------------------------------
  277. function object.setColor(fb,color)
  278.   if fb == 'f' then
  279.     if gpu.getForeground() ~= color then
  280.       gpu.setForeground(tonumber(color))
  281.     end
  282.   elseif fb == 'b' then
  283.     if gpu.getBackground() ~= color then
  284.       gpu.setBackground(tonumber(color))
  285.     end  
  286.   end
  287. end
  288.  
  289. function object.drawObject(type,name)
  290.   type_id , name_id = object.getIDS(type,name)
  291.   obj = types[type_id][2][name_id]
  292.   getOld()
  293.   if type == 'textBox' then
  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(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.   elseif type == 'triger' then
  525.     object.addTriger({...})
  526.   end
  527. end
  528.  
  529. function drawProgressBar()
  530.  
  531. end
  532.  
  533. function object.drawScroll(name,position,maxValue,onPage)
  534.   getOld()
  535.   IDTextBox , id = object.getIDS('scroll',name)
  536.   obj = types[IDTextBox][2][id]
  537.   position = position - 1
  538.   if maxValue == 0 then maxValue = 1 end
  539.   if obj.orient == 'horizontal' then
  540.     object.setColor('b',obj.back)
  541.     gpu.fill(obj.draw_x,obj.draw_y,obj.draw_w,obj.draw_h," ")
  542.     step = maxValue/obj.w
  543.     len_scroll = obj.draw_w/(maxValue/onPage) -- 10 w=100 mv = 100 oP = 10
  544.     newPos=math.floor(position/step)
  545.     object.setColor('b',obj.active_back)
  546.     object.setColor('f',obj.active_fore)
  547.     if position == maxValue then deb = math.ceil(len_scroll) else deb = 0 end
  548.     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
  549.     gpu.fill(obj.draw_x+newPos-deb , obj.draw_y , math.ceil(len_scroll) , obj.draw_h , obj.char)
  550.   elseif obj.orient == 'vertical' then
  551.     object.setColor('b',obj.back)
  552.     gpu.fill(obj.draw_x,obj.draw_y,obj.draw_w,obj.draw_h," ")
  553.     step = maxValue/obj.h
  554.     len_scroll = obj.draw_h/(maxValue/onPage) -- 10 w=100 mv = 100 oP = 10
  555.     newPos=math.floor(position/step)
  556.     object.setColor('b',obj.active_back)
  557.     object.setColor('f',obj.active_fore)
  558.     if position == maxValue then deb = math.ceil(len_scroll) else deb = 0 end
  559.     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
  560.     gpu.fill(obj.draw_x , obj.draw_y +newPos - deb, obj.draw_w , math.ceil(len_scroll), obj.char)
  561.   end
  562.   setOld()
  563. end
  564.  
  565. function object.textBox(name,mode,char)
  566.   IDTextBox , id = object.getIDS('textBox',name)
  567.   getOld()
  568.   local function getValue()
  569.     value = ''
  570.     obj = types[IDTextBox][2][id] -- <draw textBox>
  571.     object.setColor('b',obj.active_back)
  572.     object.setColor('f',obj.active_fore)
  573.     object.frame(obj.x , obj.y , obj.w , obj.h , obj.style)
  574.     object.setColor('f',obj.char_color)
  575.     local function getCode()
  576.       local key , _ , code , y , _ , _ = event.pull()
  577.       return key , y , code
  578.     end
  579.     function redrawCharInTextBox(mode,char)
  580.       if mode then
  581.         maskValue = ''
  582.         for i = 1 , unicode.len(value) do
  583.           maskValue = maskValue .. char
  584.         end
  585.         newValue = maskValue
  586.       else
  587.         newValue = value
  588.       end
  589.       gpu.fill(obj.x+1,obj.y+1,obj.w-2,1,' ')
  590.       if unicode.len(newValue) > obj.w - 3 then
  591.         term.setCursor(obj.x+obj.w-2,obj.y+1)
  592.       else
  593.         term.setCursor(obj.x+unicode.len(newValue)+1,obj.y+1)
  594.       end
  595.       term.setCursorBlink(true)
  596.       object.text(obj.x+1,obj.y+1,obj.w-3,1,newValue,'last','')
  597.     end
  598.     redrawCharInTextBox(mode,char)
  599.     while true do
  600.       err , key , y , code = pcall(getCode)
  601.       if err == true and key == 'key_down' or key == 'key_up' or 'clipboard' then
  602.         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
  603.           if keyboard.isShiftDown() then
  604.             value = value .. unicode.upper(unicode.char(code))
  605.           else
  606.             value = value .. unicode.char(code)
  607.           end
  608.           redrawCharInTextBox(mode,char)
  609.         elseif err == true and code == 13 or key == 'touch' then
  610.           break
  611.         elseif code == 8 and key == 'key_down' then
  612.           value = unicode.sub(value,0,unicode.len(value)-1)
  613.           redrawCharInTextBox(mode,char)
  614.         elseif key == 'clipboard' then
  615.           value = value .. code
  616.           redrawCharInTextBox(mode,char)
  617.         end
  618.       end
  619.     end
  620.     getOld()
  621.     object.setColor('f',obj.hint_fore) -- </draw textBox>
  622.     redrawCharInTextBox(mode,char)
  623.     term.setCursor(1,1)
  624.     term.setCursorBlink(false)
  625.     setOld()
  626.     object.drawObject('textBox',name)
  627.     return value
  628.   end
  629.   setOld()
  630.   return getValue()
  631. end
  632.  
  633. return object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement