Advertisement
PandaDoddo72Rus

Untitled

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