mrWhiskasss

Библиотека sky.lua

Nov 8th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.31 KB | None | 0 0
  1. -- pastebin get fnZVXppU sky.lua
  2. -- ▀ ▄ █
  3. local sky = {}
  4. local component = require("component")
  5. local computer=require("computer")
  6. local serial = require("serialization")
  7. local term = require("term")
  8. local event = require("event")
  9. local unicode = require("unicode")
  10. local fs = require("filesystem")
  11. local internet = require("internet")
  12. local g = component.gpu
  13. local back = 0xffffff
  14.  
  15. function sky.mid(w,y,text) --Центровка
  16. local _,n = string.gsub(text, "&","")
  17. local l = unicode.len(text) - n * 2
  18. x = (w / 2) - (l / 2)
  19. sky.text(x, y, text)
  20. end
  21.  
  22. function sky.drawImage(x,y,path) --Отрисовка картинок
  23. local back, font = g.getBackground(), g.getForeground()
  24. if (fs.exists(path)) then
  25. local start_x = x
  26. local image
  27. local text = ""
  28. local file = fs.open(path)
  29. while true do
  30. local temp = file:read(9999999)
  31. if (temp) then
  32. text = text .. temp
  33. else
  34. break
  35. end
  36. end
  37. local text2 = "{" .. text .. "}"
  38. image = serial.unserialize(text2)
  39. for i = 1, #image / 2 do
  40. x = start_x
  41. for j = 1, #image[i] do
  42. g.setBackground(image[i * 2 - 1][j])
  43. g.setForeground(image[i * 2][j])
  44. g.set(x,y,"▄")
  45. x = x + 1
  46. end
  47. y = y + 1
  48. end
  49. end
  50. g.setBackground(back)
  51. g.setForeground(font)
  52. end
  53.  
  54. function sky.logo(name,col1,col2,w,h,offset)
  55. term.clear()
  56. g.setBackground(0x000000)
  57. g.setForeground(col2)
  58. for i = 1, (w-1) do
  59. g.set(i,1,"▀")
  60. g.set(i,h,"▄")
  61. end
  62. for i = 1, h do
  63. g.set(1, i, "█")
  64. g.set(w-1, i, "█")
  65. end
  66. if offset == nil then
  67. offset = 0
  68. end
  69. sky.text(w/2 + offset - unicode.len("" .. name .. "")/2, 1, "" .. name .. "")
  70. g.set(w-24, h, "")
  71. g.setForeground(col1)
  72. g.set(w/2+1 + offset - unicode.len(name)/2, 1, name)
  73. g.set(w-22, h, "")
  74. end
  75.  
  76. function sky.setColor(index) --Список цветов
  77. if (index ~= "r") then back = g.getForeground() end
  78. if (index == "0") then g.setForeground(0x333333) end
  79. if (index == "1") then g.setForeground(0x0000ff) end
  80. if (index == "2") then g.setForeground(0x00ff00) end
  81. if (index == "3") then g.setForeground(0x24b3a7) end
  82. if (index == "4") then g.setForeground(0xff0000) end
  83. if (index == "5") then g.setForeground(0x8b00ff) end
  84. if (index == "6") then g.setForeground(0xffa500) end
  85. if (index == "7") then g.setForeground(0xbbbbbb) end
  86. if (index == "8") then g.setForeground(0x808080) end
  87. if (index == "9") then g.setForeground(0x0000ff) end
  88. if (index == "a") then g.setForeground(0x66ff66) end
  89. if (index == "b") then g.setForeground(0x00ffff) end
  90. if (index == "c") then g.setForeground(0xff6347) end
  91. if (index == "d") then g.setForeground(0xff00ff) end
  92. if (index == "e") then g.setForeground(0xffff00) end
  93. if (index == "f") then g.setForeground(0xffffff) end
  94. if (index == "g") then g.setForeground(0x00ff00) end
  95. if (index == "r") then g.setForeground(back) end
  96. end
  97.  
  98. function sky.text(x,y,text) --Цветной текст
  99. local n = 1
  100. for i = 1, unicode.len(text) do
  101. if unicode.sub(text, i,i) == "&" then
  102. sky.setColor(unicode.sub(text, i + 1, i + 1))
  103. elseif unicode.sub(text, i - 1, i - 1) ~= "&" then
  104. g.set(x+n,y, unicode.sub(text, i,i))
  105. n = n + 1
  106. end
  107. end
  108. end
  109.  
  110. function sky.button(x,y,w,h,col1,col2,text) -- Кнопка
  111. g.setForeground(col1)
  112. g.set(x + w/2 - unicode.len(text)/2, y+h/2, text)
  113. g.setForeground(col2)
  114. for i = 1, w-2 do
  115. g.set(x+i,y,"─")
  116. g.set(x+i,y+h-1,"─")
  117. end
  118. for i = 1, h-2 do
  119. g.set(x,y+i,"│")
  120. g.set(x+w-1,y+i,"│")
  121. end
  122. g.set(x,y,"┌")
  123. g.set(x+w-1,y,"┐")
  124. g.set(x,y+h-1,"└")
  125. g.set(x+w-1,y+h-1,"┘")
  126. end
  127.  
  128. function sky.takeItem(nick, item, numb) --Забрать итем
  129. return true
  130.  
  131. end
  132.  
  133. function sky.giveItem(nick, item, numb) --Выдать предмет и чекнуть влезло ли в инвентарь, если нет, вернуть остаток
  134.  
  135. return 0
  136. end
  137.  
  138. function sky.mathRound(roundIn , roundDig) --Округлить число
  139. local mul = math.pow(10, roundDig)
  140. return ( math.floor(( roundIn * mul) + 0.5)/mul)
  141. end
  142.  
  143. function sky.swap(array, index1, index2) --Свап
  144. array[index1], array[index2] = array[index2], array[index1]
  145. end
  146.  
  147. function sky.shake(array) --Шафл
  148. local counter = #array
  149. while counter > 1 do
  150. local index = math.random(counter)
  151. sky.swap(array, index, counter)
  152. counter = counter - 1
  153. end
  154. end
  155.  
  156. function sky.get(url, filename,x,y) --Получить поток
  157. local f, reason = io.open(filename, "w")
  158. if not f then
  159. g.set(x,y," Ошибка чтения файла ")
  160. os.sleep(2)
  161. return
  162. end
  163.  
  164. g.set(x,y," Идёт скачивание... ")
  165. os.sleep(2)
  166. local result, response = pcall(internet.request, url)
  167. if result then
  168. g.set(x,y," Файл успешно загружен ")
  169. os.sleep(2)
  170. for chunk in response do
  171. f:write(chunk)
  172. end
  173. f:close()
  174. else
  175. f:close()
  176. fs.remove(filename)
  177. g.set(x,y," HTTP запрос не дал результатов ")
  178. os.sleep(2)
  179. end
  180. end
  181.  
  182. function sky.run(url, ...) --Запуск и удаление файла
  183. local tmpFile = os.tmpname()
  184. sky.read(url, tmpFile)
  185. term.clear() -- <=== Очистка экрана перед запуском проги, если чё, она тута
  186. local success, reason = shell.execute(tmpFile, nil, ...)
  187. if not success then
  188. --mid(23," Битый файлик ")
  189. --os.sleep(2)
  190. end
  191. fs.remove(tmpFile)
  192. end
  193.  
  194. function sky.getHostTime(timezone) --Получить текущее реальное время компьютера, хостящего сервер майна
  195. timezone = timezone or 2
  196. local file = io.open("/HostTime.tmp", "w")
  197. file:write("123")
  198. file:close()
  199. local timeCorrection = timezone * 3600
  200. local lastModified = tonumber(string.sub(fs.lastModified("/HostTime.tmp"), 1, -4)) + timeCorrection
  201. fs.remove("HostTime.tmp")
  202. local year, month, day, hour, minute, second = os.date("%Y", lastModified), os.date("%m", lastModified), os.date("%d", lastModified), os.date("%H", lastModified), os.date("%M", lastModified), os.date("%S", lastModified)
  203. return tonumber(day), tonumber(month), tonumber(year), tonumber(hour), tonumber(minute), tonumber(second)
  204. end
  205.  
  206. function sky.time(timezone) --Получет настоящее время, стоящее на Хост-машине
  207. local time = {sky.getHostTime(timezone)}
  208. local text = string.format("%02d:%02d:%02d", time[4], time[5], time[6])
  209. return text
  210. end
  211.  
  212. function sky.hex(Hcolor) --Конвертация Dec в Hex
  213. local hex = "000000" .. string.format('%x', Hcolor)
  214. hex = string.sub(hex, unicode.len(hex)-5, unicode.len(hex))
  215. return hex
  216. end
  217.  
  218. function sky.dec(Dcolor) --Конвертация Hex в Dec
  219. if Dcolor == "" then
  220. Dcolor = "ffffff"
  221. end
  222. local dec = string.format('%d', '0x'.. Dcolor)
  223. return tonumber(dec)
  224. end
  225.  
  226. function sky.TF(value) --Если false, то вернёт true и наоборот
  227. if value then
  228. return false
  229. end
  230. return true
  231. end
  232.  
  233. function sky.palitra(col) --Палитра
  234. local OldColor = g.getForeground()
  235. if col ~= nil then
  236. OldColor = col
  237. NewColor = col
  238. end
  239. local NewColor = g.getForeground()
  240. local x,y = g.getResolution()
  241. x = x/2-14
  242. y = y/2-5
  243. local palitra = {
  244. {0x9b2d30, 0xff0000, 0xff9900, 0xffff00},
  245. {0x66ff00, 0x008000, 0x00ffff, 0x0000ff},
  246. {0x00cccc, 0x8b00ff, 0xff00ff, 0xf78fa7},
  247. {0x666666, 0x222222, 0xffffff},
  248. }
  249.  
  250. g.setForeground(0x333333)
  251.  
  252. g.set(x,y, "█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█▀▀▀▀▀▀▀▀█")
  253. g.set(x,y+1,"█ █ ██████ █")
  254. g.set(x,y+2,"█ █ ██████ █")
  255. g.set(x,y+3,"█ █▄▄▄▄▄▄▄▄█")
  256. g.set(x,y+4,"█ █ █")
  257. g.set(x,y+5,"█ █ █")
  258. g.set(x,y+6,"█ █ █")
  259. g.set(x,y+7,"█ █ Ок █")
  260. g.set(x,y+8,"█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█▄▄▄▄▄▄▄▄█")
  261.  
  262. for i = 1, #palitra do
  263. for j = 1, #palitra[i] do
  264. g.setForeground(palitra[i][j])
  265. g.set(j*4-4+x+2,i*2-2+y+1, "██")
  266. end
  267. end
  268.  
  269. g.setForeground(OldColor)
  270. g.set(x+19,y+1,"██████")
  271. g.set(x+19,y+2,"██████")
  272. g.set(x+19, y+5, sky.hex(OldColor))
  273.  
  274. while true do
  275. local e,_,w,h = event.pull("touch")
  276. if e == "touch" then
  277. for i = 1, #palitra do
  278. for j = 1, #palitra[i] do
  279. if w>=j*4-4+x+2 and w<=j*4-4+x+3 and h==i*2-2+y+1 then
  280. NewColor = palitra[i][j]
  281. g.setForeground(NewColor)
  282. g.set(x+19,y+1,"██████")
  283. g.set(x+19,y+2,"██████")
  284. g.set(x+19, y+5, sky.hex(NewColor))
  285. end
  286. end
  287. end
  288. if w>=x+19 and w<=x+24 and h==y+5 then
  289. g.set(x+19,y+5," ")
  290. term.setCursor(x+19,y+5)
  291. NewColor = sky.read({max = 6, accept = "0-9a-f", blink = true})
  292. NewColor = sky.dec(NewColor)
  293. g.setForeground(NewColor)
  294. g.set(x+19,y+1,"██████")
  295. g.set(x+19,y+2,"██████")
  296. g.set(x+19, y+5, sky.hex(NewColor))
  297. elseif w>=x+21 and w<=x+22 and h==y+7 then
  298. g.setForeground(OldColor)
  299. g.fill(x,y,x+26,y+8," ")
  300. return NewColor
  301. end
  302. end
  303. end
  304. end
  305.  
  306. function sky.pressButton(Pw,Ph,mass)
  307. local x,y,w,h = mass[1], mass[2], mass[3], mass[4]
  308. if Pw>=x and Pw<=x+w-1 and Ph>=y and Ph<=y+h-1 then
  309. return true
  310. end
  311. return false
  312. end
  313.  
  314. function sky.drawButton(mass)
  315. local x,y,w,h,text,col1,col2 = mass[1], mass[2], mass[3], mass[4], mass[5], mass[6], mass[7]
  316. g.fill(x,y,w,h," ")
  317. g.setForeground(col1)
  318. g.set(x + w/2 - unicode.len(text)/2, y+h/2, text)
  319. g.setForeground(col2)
  320. for i = 1, w-2 do
  321. g.set(x+i,y,"─")
  322. g.set(x+i,y+h-1,"─")
  323. end
  324. for i = 1, h-2 do
  325. g.set(x,y+i,"│")
  326. g.set(x+w-1,y+i,"│")
  327. end
  328. g.set(x,y,"┌")
  329. g.set(x+w-1,y,"┐")
  330. g.set(x,y+h-1,"└")
  331. g.set(x+w-1,y+h-1,"┘")
  332. end
  333.  
  334. function sky.pressSwitch(Pw,Ph, mass)
  335. local x,y = mass[1], mass[2]
  336. if Pw>=x and Pw<=x+4 and Ph>=y and Ph<=y+2 then
  337. return true
  338. end
  339. return false
  340. end
  341.  
  342. function sky.drawSwitch(mass) --Свич
  343. local x,y,col1,col2,value = mass[1], mass[2], mass[3], mass[4], mass[5]
  344. g.setForeground(col2)
  345. g.set(x,y, "┌───┐")
  346. g.set(x,y+1,"│ │")
  347. g.set(x,y+2,"└───┘")
  348. if value == true then
  349. g.setForeground(col1)
  350. g.set(x+2,y+1,"√")
  351. elseif value ~= false then
  352. g.setForeground(value)
  353. g.set(x+1,y+1,"▐█▌")
  354. g.setForeground(col2)
  355. end
  356. end
  357.  
  358. function sky.word(x,y,text,ramka) --Шрифт
  359. text = unicode.lower(text)
  360. for i = 1, unicode.len(text) do
  361. sky.symbol(i*4-4 + x, y, string.sub(text,i,i), ramka)
  362. end
  363. end
  364.  
  365. function sky.symbol(x,y,symbol,ramka) --Символы шрифта
  366. local WBack = g.getBackground()
  367.  
  368. if ramka ~= nil then
  369. local WColor = g.getForeground()
  370. g.setForeground(ramka)
  371.  
  372. g.set(x+1,y-1,"████")
  373. g.set(x+1,y, "████")
  374. g.set(x+1,y+1,"████")
  375. g.set(x+1,y+2,"████")
  376.  
  377. g.setBackground(ramka)
  378. g.setForeground(WColor)
  379. end
  380.  
  381. if symbol == "a" then
  382. g.set(x+1,y, "▄▄▄▄▄")
  383. g.set(x+1,y+1,"█▄▄▄█")
  384. g.set(x+1,y+2,"█ █")
  385. elseif symbol == "b" then
  386. g.set(x+1,y, "▄▄▄▄")
  387. g.set(x+1,y+1,"█▄▄█▄")
  388. g.set(x+1,y+2,"█▄▄▄█")
  389. elseif symbol == "c" then
  390. g.set(x+1,y, "▄▄▄▄▄")
  391. g.set(x+1,y+1,"█")
  392. g.set(x+1,y+2,"█▄▄▄▄")
  393. elseif symbol == "d" then
  394. g.set(x+1,y, "▄▄▄▄")
  395. g.set(x+1,y+1,"█ █")
  396. g.set(x+1,y+2,"█▄▄▄▀")
  397. elseif symbol == "e" then
  398. g.set(x+1,y, "▄▄▄▄▄")
  399. g.set(x+1,y+1,"█▄▄▄")
  400. g.set(x+1,y+2,"█▄▄▄▄")
  401. elseif symbol == "f" then
  402. g.set(x+1,y, "▄▄▄▄▄")
  403. g.set(x+1,y+1,"█▄▄")
  404. g.set(x+1,y+2,"█")
  405. elseif symbol == "g" then
  406. g.set(x+1,y, "▄▄▄▄▄")
  407. g.set(x+1,y+1,"█ ▄▄")
  408. g.set(x+1,y+2,"█▄▄▄█")
  409. elseif symbol == "h" then
  410. g.set(x+1,y, "▄ ▄")
  411. g.set(x+1,y+1,"█▄▄▄█")
  412. g.set(x+1,y+2,"█ █")
  413. elseif symbol == "i" then
  414. g.set(x+2,y, "▄▄▄")
  415. g.set(x+2,y+1," █")
  416. g.set(x+2,y+2,"▄█▄")
  417. elseif symbol == "j" then
  418. g.set(x+1,y, "▄▄▄▄▄")
  419. g.set(x+1,y+1," █")
  420. g.set(x+1,y+2,"█▄▄▄█")
  421. elseif symbol == "k" then
  422. g.set(x+1,y, "▄ ▄")
  423. g.set(x+1,y+1,"█▄▀")
  424. g.set(x+1,y+2,"█ ▀▄")
  425. elseif symbol == "l" then
  426. g.set(x+1,y, "▄")
  427. g.set(x+1,y+1,"█")
  428. g.set(x+1,y+2,"█▄▄▄")
  429. elseif symbol == "m" then
  430. g.set(x+1,y, "▄ ▄")
  431. g.set(x+1,y+1,"█▀▄▀█")
  432. g.set(x+1,y+2,"█ █")
  433. elseif symbol == "n" then
  434. g.set(x+1,y, "▄▄ ▄")
  435. g.set(x+1,y+1,"█ █ █")
  436. g.set(x+1,y+2,"█ ▀█")
  437. elseif symbol == "o" then
  438. g.set(x+1,y, "▄▄▄▄▄")
  439. g.set(x+1,y+1,"█ █")
  440. g.set(x+1,y+2,"█▄▄▄█")
  441. elseif symbol == "p" then
  442. g.set(x+1,y, "▄▄▄▄▄")
  443. g.set(x+1,y+1,"█▄▄▄█")
  444. g.set(x+1,y+2,"█")
  445. elseif symbol == "q" then
  446. g.set(x+1,y, "▄▄▄▄▄")
  447. g.set(x+1,y+1,"█ █")
  448. g.set(x+1,y+2,"█▄▄██")
  449. elseif symbol == "r" then
  450. g.set(x+1,y, "▄▄▄▄▄")
  451. g.set(x+1,y+1,"█▄▄▄█")
  452. g.set(x+1,y+2,"█ ▀▄")
  453. elseif symbol == "s" then
  454. g.set(x+1,y, "▄▄▄▄▄")
  455. g.set(x+1,y+1,"█▄▄▄▄")
  456. g.set(x+1,y+2,"▄▄▄▄█")
  457. elseif symbol == "t" then
  458. g.set(x+1,y, "▄▄▄▄▄")
  459. g.set(x+1,y+1," █")
  460. g.set(x+1,y+2," █")
  461. elseif symbol == "u" then
  462. g.set(x+1,y, "▄ ▄")
  463. g.set(x+1,y+1,"█ █")
  464. g.set(x+1,y+2,"▀▄▄▄▀")
  465. elseif symbol == "v" then
  466. g.set(x+1,y, "▄ ▄")
  467. g.set(x+1,y+1,"█ █")
  468. g.set(x+1,y+2," ▀▄▀")
  469. elseif symbol == "w" then
  470. g.set(x+1,y, "▄ ▄")
  471. g.set(x+1,y+1,"█ █ █")
  472. g.set(x+1,y+2,"▀▄█▄▀")
  473. elseif symbol == "x" then
  474. g.set(x+1,y, "▄ ▄")
  475. g.set(x+1,y+1," ▀▄▀")
  476. g.set(x+1,y+2,"▄▀ ▀▄")
  477. elseif symbol == "y" then
  478. g.set(x+1,y, "▄ ▄")
  479. g.set(x+1,y+1," ▀▄▀ ")
  480. g.set(x+1,y+2," █")
  481. elseif symbol == "z" then
  482. g.set(x+1,y, "▄▄▄▄▄")
  483. g.set(x+1,y+1," ▄▄▄▀")
  484. g.set(x+1,y+2,"█▄▄▄▄")
  485. elseif symbol == "-" then
  486. g.set(x+1,y, " ")
  487. g.set(x+1,y+1," ")
  488. g.set(x+1,y+2," ")
  489. elseif symbol == "+" then
  490. g.set(x+1,y-1," ▄▄ ")
  491. g.set(x+1,y, "▄ ▄▄")
  492. g.set(x+1,y+1,"█ █ █")
  493. g.set(x+1,y+2,"██ █")
  494. elseif symbol == "(" then
  495. g.set(x+1,y-1," ▄ ")
  496. g.set(x+1,y ," █ ")
  497. g.set(x+1,y+1,"▄ █ ▄")
  498. g.set(x+1,y+2," ▄█▄ " )
  499. elseif symbol == "0" then
  500. g.set(x+1,y-1,"▄▄▄▄")
  501. g.set(x+1,y, "█ █")
  502. g.set(x+1,y+1,"█ █")
  503. g.set(x+1,y+2,"▀▀▀▀")
  504. elseif symbol == "9" then
  505. g.set(x+1,y-1,"▄▄▄▄")
  506. g.set(x+1,y, "█ █")
  507. g.set(x+1,y+1," ▀▀█")
  508. g.set(x+1,y+2,"▀▀▀▀")
  509. elseif symbol == "8" then
  510. g.set(x+1,y-1,"▄▄▄▄")
  511. g.set(x+1,y, "█ █")
  512. g.set(x+1,y+1,"█▀▀█")
  513. g.set(x+1,y+2,"▀▀▀▀")
  514. elseif symbol == "7" then
  515. g.set(x+1,y-1,"▄▄▄▄")
  516. g.set(x+1,y, " ▀")
  517. g.set(x+1,y+1," █ ")
  518. g.set(x+1,y+2," ▀ ")
  519. elseif symbol == "6" then
  520. g.set(x+1,y-1,"▄▄▄▄")
  521. g.set(x+1,y, "█ ")
  522. g.set(x+1,y+1,"█▀▀█")
  523. g.set(x+1,y+2,"▀▀▀▀")
  524. elseif symbol == "5" then
  525. g.set(x+1,y-1,"▄▄▄▄")
  526. g.set(x+1,y, "█ ")
  527. g.set(x+1,y+1,"▀▀▀█")
  528. g.set(x+1,y+2,"▀▀▀▀")
  529. elseif symbol == "4" then
  530. g.set(x+1,y-1,"▄ ▄")
  531. g.set(x+1,y, "█ █")
  532. g.set(x+1,y+1,"▀▀▀█")
  533. g.set(x+1,y+2," ▀")
  534. elseif symbol == "3" then
  535. g.set(x+1,y-1,"▄▄▄▄")
  536. g.set(x+1,y, " ▄▄█")
  537. g.set(x+1,y+1," █")
  538. g.set(x+1,y+2,"▀▀▀▀")
  539. elseif symbol == "2" then
  540. g.set(x+1,y-1,"▄▄▄ ")
  541. g.set(x+1,y, " █ ")
  542. g.set(x+1,y+1,"█ ")
  543. g.set(x+1,y+2,"▀▀▀ ")
  544. elseif symbol == "1" then
  545. g.set(x+1,y-1," ▄ ")
  546. g.set(x+1,y, "▀█ ")
  547. g.set(x+1,y+1," █ ")
  548. g.set(x+1,y+2,"▀▀▀ ")
  549. elseif symbol == "." then
  550. g.set(x+1,y-1," ")
  551. g.set(x+1,y, " ")
  552. g.set(x+1,y+1," ")
  553. g.set(x+1,y+2," ▀ ")
  554.  
  555. end
  556.  
  557. g.setBackground(WBack)
  558. end
  559.  
  560. function sky.read(settings)
  561. --mask, max, rim - returnIfMax, accept, blink, center, nick, clip, maxDisplay
  562. if not settings then
  563. settings = {}
  564. end
  565. local nextBlink = 0
  566. if not settings.text then
  567. settings.text = ""
  568. end
  569. if settings.blink ~= false then
  570. settings.blink = true
  571. end
  572. if settings.max then
  573. settings.max = settings.max - 1
  574. else
  575. settings.max = 999
  576. end
  577. local sx, sy = term.getCursor()
  578. local oldFg = g.getForeground()
  579. local oldBg = g.getBackground()
  580. local SX = sx
  581. while true do
  582.  
  583. function maxD(maxd, text)
  584. if maxd and maxd <= unicode.len(text) then
  585. return string.sub(text, unicode.len(text)-maxd, unicode.len(text))
  586. else
  587. return text
  588. end
  589. end
  590.  
  591. local tempText = maxD(settings.maxDisplay, settings.text)
  592.  
  593. if settings.center then
  594. SX = sx - unicode.len(tempText) / 2
  595. end
  596.  
  597. local event, address, char, code, nick = event.pull(settings.blink and math.min(0, nextBlink - computer.uptime())) --, "key_down"
  598. if char == 13 and event == "key_down" then --Enter
  599. local char, fg, bg = g.get(SX + unicode.len(tempText), sy)
  600. if settings.center then
  601. SX = sx - unicode.len(tempText) / 2
  602. end
  603. g.set(SX + unicode.len(tempText), sy, char or " ")
  604. if settings.nick then
  605. return settings.text, nick
  606. end
  607. return settings.text
  608. elseif char == 8 and event == "key_down" then --Backspace
  609. settings.text = unicode.sub(settings.text, 1, -2)
  610. tempText = unicode.sub(tempText, 1, -2)
  611. if settings.center then
  612. SX = sx - unicode.len(tempText) / 2
  613. end
  614. g.set(SX, sy, (not settings.mask and tempText or settings.mask:rep(unicode.len(tempText))) .. " ")
  615. if settings.center then
  616. g.set(SX-1, sy, " ")
  617. end
  618. elseif char and char ~= 0 and event == "key_down" then --Keyboard
  619. local acceptRegx = settings.accept and ("[" .. settings.accept .. "]") or "."
  620. if unicode.char(char):find(acceptRegx) then
  621. if settings.max and settings.max >= unicode.len(settings.text) then
  622. settings.text = settings.text .. unicode.char(char)
  623. tempText = tempText .. unicode.char(char)
  624. if settings.center then
  625. SX = sx - unicode.len(tempText) / 2
  626. end
  627. g.set(SX, sy, not settings.mask and tempText or settings.mask:rep(unicode.len(tempText)))
  628. if settings.max and settings.rim and settings.max == unicode.len(settings.text) then
  629. local char, fg, bg = g.get(SX + unicode.len(tempText), sy)
  630. g.set(SX + unicode.len(tempText), sy, char or " ")
  631. return settings.text
  632. end
  633. end
  634. end
  635. elseif event == "clipboard" and settings.clip then --Paste
  636. settings.text = settings.text .. char
  637. if settings.max and settings.max < unicode.len(settings.text) then
  638. settings.text = string.sub(settings.text, 1, settings.max - 3) .. "..."
  639. end
  640. tempText = maxD(settings.maxDisplay, settings.text)
  641. if settings.center then
  642. SX = sx - unicode.len(tempText) / 2
  643. end
  644. g.set(SX, sy, not settings.mask and tempText or settings.mask:rep(unicode.len(tempText)))
  645. end
  646.  
  647. if settings.blink and nextBlink <= computer.uptime() then
  648. oldFg = g.getForeground()
  649. oldBg = g.getBackground()
  650. local char, fg, bg = g.get(SX + unicode.len(tempText), sy)
  651. g.setForeground(bg)
  652. g.setBackground(fg)
  653. g.set(SX + unicode.len(tempText), sy, char or " ")
  654. g.setForeground(oldFg)
  655. g.setBackground(oldBg)
  656. nextBlink = computer.uptime() + 0.5
  657. end
  658. end
  659. end
  660.  
  661. --Old function
  662. function sky.clearL(h) --Очистка левой части
  663. g.fill(3,2,26,h-2," ")
  664. end
  665.  
  666. function sky.clearR(w,h) --Очистка правой части
  667. g.fill(31,2,w-32,h-2," ")
  668. end
  669.  
  670. function sky.midL(w,y,text) --Центровка слева
  671. local _,n = string.gsub(text, "&","")
  672. local l = unicode.len(text) - n * 2
  673. x = 13 - (l / 2)
  674. sky.text(x+2, y, text)
  675. end
  676.  
  677. function sky.midR(w,y,text) --Центровка справа
  678. local _,n = string.gsub(text, "&","")
  679. local l = unicode.len(text) - n * 2
  680. x = ((w - 34) / 2) - (l / 2)
  681. sky.text(x+31, y, text)
  682. end
  683.  
  684. return sky
Advertisement
Add Comment
Please, Sign In to add comment