rkinasz

UIUtils.lua

Feb 7th, 2022
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.97 KB | None | 0 0
  1. --handles screen positions
  2.  
  3. local id = 0
  4.  
  5. PositionClass = {}
  6. PositionClass.new = function ()
  7. local self = {}
  8.  
  9. self.x = 0
  10. self.y = 0
  11.  
  12. return self
  13. end
  14.  
  15. --base screen object
  16.  
  17. BaseScreenObjectClass = {}
  18. BaseScreenObjectClass.new = function ()
  19. local self = {}
  20.  
  21. self.pos = PositionClass.new()
  22. self.width = 0
  23. self.height = 0
  24. self.id = id
  25. id = id + 1
  26.  
  27. self.visible = true
  28.  
  29. self.textColor = colors.black
  30. self.primaryColor = colors.green
  31. self.secondaryColor = colors.red
  32.  
  33. self.data = nil
  34. self.text = ""
  35. self.tags = {}
  36.  
  37. self.type = "base"
  38.  
  39. function self.draw()
  40. --override based on class
  41. end
  42.  
  43. function self.setPos (x, y)
  44. self.pos.x = x
  45. self.pos.y = y
  46. end
  47.  
  48. function self.update ()
  49. if self.dataSource then
  50. self.data = self.dataSource()
  51. end
  52. end
  53.  
  54. function self.centerCursor ()
  55. term.setCursorPos(self.pos.x + math.floor(self.width / 2) - math.floor(string.len(self.text) / 2), self.pos.y + math.floor(self.height / 2))
  56. end
  57.  
  58. function self.setTag(tag)
  59. self.tags[tag] = true
  60. end
  61.  
  62. function self.hasTag(tag)
  63. return self.tags[tag]
  64. end
  65.  
  66. function self.removeTag(tag)
  67. table.remove(self.tags, tag)
  68. end
  69.  
  70. return self
  71. end
  72.  
  73. --progress bar
  74. ProgressBarClass = {}
  75. ProgressBarClass.new = function ()
  76. local self = BaseScreenObjectClass.new()
  77.  
  78. self.horizontal = true -- if false will assume vetrical
  79. self.data = 0
  80. self.dataSource = nil
  81.  
  82. self.type = "bar"
  83.  
  84. function self.draw ()
  85. paintutils.drawFilledBox(self.pos.x, self.pos.y, self.pos.x + self.width, self.pos.y + self.height, self.secondaryColor)
  86. if self.data > 0 then
  87. if self.horizontal then
  88. paintutils.drawFilledBox(self.pos.x, self.pos.y, self.pos.x + self.width * self.data, self.pos.y + self.height, self.primaryColor)
  89. else
  90. paintutils.drawFilledBox(self.pos.x, self.pos.y + (self.height * (1 - self.data)), self.pos.x + self.width, self.pos.y + self.height, self.primaryColor)
  91. end
  92. end
  93. local str = (math.floor(self.data * 100)) .. "%"
  94. term.setCursorPos(self.pos.x + ((self.width / 2) - (#str / 2)) + 1, self.pos.y + self.height / 2)
  95. term.setTextColor(self.textColor)
  96.  
  97. local cursorX, cursorY = term.getCursorPos()
  98. if self.horizontal then
  99. for i = 1, #str do
  100. if cursorX > self.pos.x + self.width * self.data then
  101. term.setBackgroundColor(self.secondaryColor)
  102. else
  103. term.setBackgroundColor(self.primaryColor)
  104. end
  105. term.write(string.sub(str,i,i))
  106. cursorX = cursorX + 1
  107. end
  108.  
  109. else
  110. --print(cursorY, math.floor(self.pos.y + (self.height * (1 - self.data)) + 0.5), self.pos.y + (self.height * (1 - self.data)))
  111. if cursorY < math.floor(self.pos.y + (self.height * (1 - self.data))) then
  112. term.setBackgroundColor(self.secondaryColor)
  113. end
  114. term.write(str)
  115. end
  116. end
  117.  
  118. return self
  119. end
  120.  
  121. --button class
  122. ButtonClass = {}
  123. ButtonClass.new = function ()
  124. local self = BaseScreenObjectClass.new()
  125.  
  126. self.onClick = nil
  127. self.active = false
  128. self.page = 0
  129.  
  130. self.subText = ""
  131.  
  132. self.isRadio = false
  133. self.radioTag = nil
  134.  
  135. self.type = "button"
  136.  
  137. --toggles the button active status
  138. function self.toggle ()
  139. self.active = not self.active
  140. end
  141.  
  142. function self.draw(page)
  143. if not page or page == self.page or self.page == 0 then
  144. color = self.secondaryColor
  145. if self.active then
  146. color = self.primaryColor
  147. end
  148. paintutils.drawFilledBox(self.pos.x, self.pos.y, self.pos.x + self.width, self.pos.y + self.height, color)
  149. self.centerCursor()
  150. term.setTextColor(self.textColor)
  151. if #tostring(self.text) <= 2 and self.width <= 2 then
  152. term.setCursorPos(self.pos.x, self.pos.y + self.height / 2)
  153. term.write(self.text)
  154. elseif (#tostring(self.text) > self.width - 2) then
  155. term.setCursorPos(self.pos.x + 1, self.pos.y + (self.height / 2))
  156. term.write(string.sub(self.text,1,self.width - 1))
  157. else
  158. term.write(self.text)
  159. end
  160.  
  161. term.setCursorPos(self.pos.x + (self.width / 2 - #self.subText / 2) + 1, self.pos.y + (self.height / 2) + 1)
  162. term.write(self.subText)
  163.  
  164. end
  165. end
  166.  
  167. return self
  168. end
  169.  
  170. --default button handler
  171. local function buttonHandler (UI, event, side, x, y, page)
  172. for k,v in pairs (UI.objects) do
  173. if v.type == "button" and x >= v.pos.x and x <= v.pos.x + v.width and y >= v.pos.y and y <= v.pos.y + v.height and v.visible then
  174. if not page or v.page == page or v.page == 0 then
  175. if v.isRadio then
  176. v.active = not v.active
  177. for k1,v1 in pairs(UI.objects) do
  178. if v1 ~= v and v1.isRadio and v1.radioTag == v.radioTag then
  179. v1.active = false
  180. end
  181. end
  182. end
  183. if v.onClick then
  184. v.onClick(v.data)
  185. end
  186. end
  187. end
  188. end
  189. end
  190.  
  191. --UI class
  192. UIClass = {}
  193. UIClass.new = function ()
  194. local self = {}
  195. self.name = ""
  196. self.objects = {}
  197. self.page = 1
  198. self.pages = 1
  199.  
  200. function self.update ()
  201. for k,v in pairs (self.objects) do
  202. v.update()
  203. end
  204. end
  205.  
  206. function self.draw ()
  207. for k,v in pairs(self.objects) do
  208. if v.visible then
  209. v.draw(self.page)
  210. end
  211. end
  212. end
  213.  
  214. function self.addObject (o)
  215. table.insert(self.objects, o)
  216. end
  217.  
  218. function self.addObjects(o)
  219. for k,v in pairs(o) do
  220. table.insert(self.objects, v)
  221. end
  222. end
  223.  
  224. function self.findObjectsWithTag(tag)
  225. local objects = {}
  226. for k,v in pairs (self.objects) do
  227. if v.hasTag(tag) then
  228. table.insert(objects, v)
  229. end
  230. end
  231. return objects
  232. end
  233.  
  234. self.handler = buttonHandler
  235.  
  236. return self
  237. end
  238.  
  239. --text class
  240. TextClass = {}
  241. TextClass.new = function ()
  242. local self = BaseScreenObjectClass.new()
  243. self.type = "text"
  244. function self.draw()
  245. term.setTextColor(self.textColor)
  246. term.setBackgroundColor(self.primaryColor)
  247. term.setCursorPos(self.pos.x, self.pos.y)
  248. term.write(self.text)
  249. end
  250.  
  251. function self.update ()
  252. if self.dataSource then
  253. self.text = self.dataSource()
  254. end
  255. end
  256.  
  257. return self
  258. end
  259.  
  260. --navBar
  261. NavbarClass = {}
  262. NavbarClass.new = function ()
  263. local self = BaseScreenObjectClass.new()
  264. self.options = {}
  265. self.divider = ">>"
  266. self.type = "navbar"
  267. function self.draw ()
  268. local t = self.divider .. " "
  269. for k,v in pairs(self.options) do
  270. t = t .. v .. " " .. self.divider .. " "
  271. end
  272. if #t > #self.divider + 1 then
  273. t = string.sub(t, 1, (#self.divider + 2) * -1)
  274. end
  275. paintutils.drawFilledBox(self.pos.x, self.pos.y, self.pos.x + self.width, self.pos.y + self.height, self.primaryColor)
  276. term.setBackgroundColor(self.primaryColor)
  277. term.setTextColor(self.textColor)
  278. if self.pos.x > 1 then
  279. term.setCursorPos(self.pos.x, self.pos.y)
  280. else
  281. term.setCursorPos(2, self.pos.y)
  282. end
  283. local m = term.current()
  284.  
  285. term.write(t)
  286.  
  287. end
  288.  
  289. return self
  290. end
  291.  
  292. --messages used in the log
  293. MessageClass = {}
  294. MessageClass.new = function ()
  295. local self = {}
  296.  
  297. self.infoColor = colors.blue
  298. self.errorColor = colors.red
  299. self.successColor = colors.green
  300. self.warningColor = colors.orange
  301.  
  302. self.text = ""
  303. self.type = 1 -- 1 = info, 2 = error 3 = success 4- warning
  304.  
  305. function self.getColor ()
  306. if self.type == 1 then
  307. return self.infoColor
  308. elseif self.type == 2 then
  309. return self.errorColor
  310. elseif self.type == 3 then
  311. return self.successColor
  312. elseif self.type == 4 then
  313. return self.warningColor
  314. else
  315. return colors.white -- default
  316. end
  317. end
  318.  
  319. return self
  320. end
  321.  
  322. LogClass = {}
  323. LogClass.new = function ()
  324. local self = BaseScreenObjectClass.new()
  325.  
  326. self.messages = {}
  327. self.recentMessages = {}
  328. self.type = "log"
  329.  
  330. function self.addToRecentMessage (m)
  331. local t = m.text
  332.  
  333. if #t > self.width then
  334. while #t > 0 do
  335. local message = MessageClass.new()
  336. message.type = m.type
  337. message.text = string.sub(t,1,self.width)
  338. table.insert(self.recentMessages, message)
  339. t = string.sub(t,self.width + 1, #t)
  340. end
  341. else
  342. table.insert(self.recentMessages, m)
  343. end
  344. while #self.recentMessages > self.height do
  345. table.remove(self.recentMessages, 1)
  346. end
  347. end
  348.  
  349. function self.addMessage (m, t)
  350. if type(m) == "table" then
  351. table.insert(self.messages, m)
  352. self.addToRecentMessage(m)
  353. else
  354. local message = MessageClass.new()
  355. message.text = tostring(m)
  356. message.type = t
  357. self.addToRecentMessage(message)
  358. table.insert(self.messages, message)
  359. end
  360. end
  361.  
  362. function self.clear()
  363. self.messages = {}
  364. self.recentMessages = {}
  365. end
  366.  
  367. function self.draw()
  368. paintutils.drawFilledBox(self.pos.x, self.pos.y, self.pos.x + self.width, self.pos.y + self.height, self.primaryColor)
  369. term.setCursorPos(self.pos.x, self.pos.y)
  370. term.setBackgroundColor(self.primaryColor)
  371. term.setTextColor(self.textColor)
  372. term.write("Log: ")
  373.  
  374. if #self.recentMessages > 0 then
  375. for i = 1, #self.recentMessages do
  376. term.setCursorPos(self.pos.x + 1, self.pos.y + i)
  377. term.setTextColor(self.recentMessages[i].getColor())
  378. term.write(string.sub(self.recentMessages[i].text,1,self.width))
  379. end
  380. end
  381. end
  382.  
  383. return self
  384. end
  385.  
  386. AnimationClass = {}
  387. AnimationClass.new = function ()
  388. local self = BaseScreenObjectClass.new()
  389.  
  390. self.frames = {}
  391. self.currentFrame = 1
  392. self.speed = 10
  393. self.timer = 0
  394. self.paused = false
  395. self.pauseOnLastFrame = false
  396.  
  397. self.pause = function ()
  398. self.paused = true
  399. end
  400.  
  401. self.unpause = function ()
  402. self.paused = false
  403. end
  404.  
  405. self.draw = function ()
  406. paintutils.drawImage(self.frames[self.currentFrame].img, self.pos.x, self.pos.y)
  407. if not self.paused then
  408. self.timer = self.timer + 1
  409. if self.timer >= self.speed then
  410. self.timer = 0
  411. self.currentFrame = self.currentFrame + 1
  412. if self.pauseOnLastFrame and self.currentFrame == #self.frames then
  413. self.pause()
  414. self.pauseOnLastFrame = false
  415. end
  416.  
  417. if self.currentFrame > #self.frames then
  418. self.currentFrame = 1
  419. end
  420. end
  421. end
  422. end
  423.  
  424. self.changeColor = function (oldColor, newColor)
  425. for k,v in pairs (self.frames) do
  426. v.changeColor(oldColor, newColor)
  427. end
  428. end
  429.  
  430. self.setToOriginal = function ()
  431. for k,v in pairs (self.frames) do
  432. v.setToOriginal()
  433. end
  434. end
  435.  
  436. self.reset = function ()
  437. self.currentFrame = 1
  438. self.timer = 0
  439. end
  440.  
  441. return self
  442. end
  443.  
  444. ImageClass = {}
  445. ImageClass.new = function (source)
  446. local self = BaseScreenObjectClass.new()
  447.  
  448. self.source = source
  449. self.original = nil
  450. self.img = nil
  451.  
  452. self.setToOriginal = function ()
  453. self.img = self.original
  454. end
  455.  
  456. self.changeColor = function (oldColor, newColor)
  457. local out = {}
  458. for k,v in pairs (self.img) do
  459. local temp = {}
  460. for k2, v2 in pairs(v) do
  461. if v2 == oldColor then
  462. v2 = newColor
  463. end
  464. table.insert(temp, v2)
  465. end
  466. table.insert(out, temp)
  467. end
  468. self.img = out
  469. end
  470.  
  471. self.reload = function ()
  472. if self.source then
  473. self.img = paintutils.loadImage(source)
  474. self.original = self.img
  475. return true
  476. else
  477. return false
  478. end
  479. end
  480.  
  481. self.draw = function ()
  482. paintutils.drawImage(self.img, self.pos.x, self.pos.y)
  483. end
  484.  
  485. self.reload()
  486.  
  487. return self
  488. end
  489.  
  490. QueueClass = {}
  491. QueueClass.new = function ()
  492. local self = BaseScreenObjectClass.new()
  493.  
  494. self.draw = function ()
  495. local entryCount = 0
  496. local y = 0
  497. for k,v in pairs(self.data) do
  498. if (entryCount % 2) == 0 then
  499. term.setBackgroundColor(self.primaryColor)
  500. else
  501. term.setBackgroundColor(self.secondaryColor)
  502. end
  503.  
  504. local text = v.queueLabel
  505. local messages = {}
  506. if #text > self.width then
  507. while #text > 0 do
  508. table.insert(messages, string.sub(text,1,self.width))
  509. text = string.sub(text, self.width + 1, #text)
  510. end
  511. else
  512. messages[1] = text
  513. end
  514.  
  515. term.setTextColor(self.textColor)
  516.  
  517. for k2, v2 in pairs(messages) do
  518. term.setCursorPos(self.pos.x, self.pos.y + y)
  519. if #v2 < self.width then
  520. local filler = ""
  521. for i = 1, (self.width - #v2) do
  522. filler = filler .. " "
  523. end
  524. v2 = v2 .. filler
  525. end
  526. term.write(v2)
  527. y = y + 1
  528. if y > self.height then
  529. break
  530. end
  531. end
  532. if y > self.height then
  533. break
  534. end
  535. entryCount = entryCount + 1
  536. end
  537. end
  538.  
  539. return self
  540. end
  541.  
  542.  
  543. function generateButtons (monitor, amount , startX, startY, endX, endY, collums, rows, primaryColor, secondaryColor, textColor, onclick)
  544. --local monw, monh = monitor.getSize()
  545. local bWidth = math.floor(((endX - startX) - collums) / collums)
  546. local bHeight = math.floor(((endY - startY) - rows) / rows)
  547. local buttons = {}
  548.  
  549. local curX, curY = startX, startY
  550. local made = 0
  551.  
  552. for i = 1, math.ceil(amount / (collums * rows)) do
  553. for row = 1, rows do
  554. for col = 1, collums do
  555. if made < amount then
  556. local b = ButtonClass.new()
  557. b.pos.x = curX
  558. b.pos.y = curY
  559. b.width = bWidth
  560. b.height = bHeight
  561. b.page = i
  562. b.text = "button"
  563.  
  564. b.primaryColor = primaryColor
  565. b.secondaryColor = secondaryColor
  566. b.textColor = textColor
  567. b.onClick = onclick
  568.  
  569. curX = curX + bWidth + 2
  570. table.insert(buttons, b)
  571. made = made + 1
  572. end
  573. end
  574. curY = curY + bHeight + 2
  575. curX = startX
  576. end
  577. curX, curY = startX, startY
  578. end
  579.  
  580. return buttons
  581. end
  582.  
Advertisement
Add Comment
Please, Sign In to add comment