Advertisement
klindley

LCARS-UI

Oct 9th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. -- gui library for LCARS V1.2015.10.09
  2. local gpu = require("component").gpu
  3.  
  4. function setHexColor(n, r, g, b)
  5. local hexcolor = rgb2hex(r,g,b)
  6. hexcolortable[n] = hexcolor
  7. darkhexcolors[n] = bit32.rshift(bit32.band(hexcolor, 0xfefefe), 1)
  8. end
  9. function rgb2hex(r,g,b)
  10. return r*65536+g*256+b
  11. end
  12.  
  13. colortable = {{255, 0, 0}, {0, 255, 0}, {0, 102, 255}}
  14. colortable[0] = {0, 0, 0}
  15. hexcolortable = {}
  16. darkhexcolors = {}
  17. for i=0,3 do setHexColor(i, colortable[i][1], colortable[i][2], colortable[i][3]) end
  18.  
  19. -- Colors --
  20. backcolor = 0x000000
  21. forecolor = 0xFFFFFF
  22. -- ============================================= G R A P H I C S ============================================= --
  23. -- check screen/graphics card resolution; for comfortable work necessary resolution is >=HOLOW in height and width
  24. M = {}
  25.  
  26. M.initialize = function()
  27. OLDWIDTH, OLDHEIGHT = gpu.getResolution()
  28. WIDTH, HEIGHT = gpu.maxResolution()
  29. gpu.setForeground(forecolor)
  30. gpu.setBackground(backcolor)
  31. end
  32.  
  33. -- draw a frame
  34. function M.frame(x1, y1, x2, y2, caption)
  35. line(x1, x2, y1)
  36. line(x1, x2, y2)
  37.  
  38. if caption ~= nil then
  39. gpu.set(x1+(x2-x1)/2-unicode.len(caption)/2, y1, caption)
  40. end
  41. end
  42.  
  43. -- draw colored rectangle
  44. function M.drawRect(x, y, color)
  45. gpu.set(x, y, "+------+")
  46. gpu.set(x, y+1, "¦ ¦")
  47. gpu.set(x, y+2, "+------+")
  48. gpu.setForeground(color)
  49. gpu.set(x+2, y+1, "¦¦¦¦")
  50. gpu.setForeground(forecolor)
  51. end
  52.  
  53. -- ============================================== B U T T O N S ============================================== --
  54. M.Button = {}
  55. M.Button.__index = M.Button
  56. function M.Button.new(func, x, y, text, color, width)
  57. self = setmetatable({}, Button)
  58.  
  59. self.form = '[ '
  60. if width == nil then width = 0
  61. else width = (width - unicode.len(text))-4 end
  62. for i=1, math.floor(width/2) do
  63. self.form = self.form.. ' '
  64. end
  65. self.form = self.form..text
  66. for i=1, math.ceil(width/2) do
  67. self.form = self.form.. ' '
  68. end
  69. self.form = self.form..' ]'
  70.  
  71. self.func = func
  72.  
  73. self.x = x; self.y = y
  74. self.color = color
  75. self.visible = true
  76.  
  77. return self
  78. end
  79. function M.Button:draw(color)
  80. if self.visible then
  81. local color = color or self.color
  82. gpu.setBackground(color)
  83. if color > 0x888888 then gpu.setForeground(backcolor) end
  84. gpu.set(self.x, self.y, self.form)
  85. gpu.setBackground(backcolor)
  86. if color > 0x888888 then gpu.setForeground(forecolor) end
  87. end
  88. end
  89. function M.Button:click(x, y)
  90. if self.visible then
  91. if y == self.y then
  92. if x >= self.x and x < self.x+unicode.len(self.form) then
  93. self.func()
  94. self:draw(self.color/2)
  95. os.sleep(0.1)
  96. self:draw()
  97. return true
  98. end
  99. end
  100. end
  101. return false
  102. end
  103.  
  104. buttons = {}
  105. function M.buttonsNew(func, x, y, text, color, width)
  106. table.insert(buttons, M.Button.new(func, x, y, text, color, width))
  107. end
  108. function M.buttonsDraw()
  109. for i=1, #buttons do
  110. buttons[i]:draw()
  111. end
  112. end
  113. function M.buttonsClick(x, y)
  114. for i=1, #buttons do
  115. buttons[i]:click(x, y)
  116. end
  117. end
  118.  
  119. -- ============================================ T E X T B O X E S ============================================ --
  120. M.Textbox = {}
  121. M.Textbox.__index = M.Textbox
  122. function M.Textbox.new(func, x, y, value, width)
  123. self = setmetatable({}, Textbox)
  124.  
  125. self.form = '>'
  126. if width == nil then width = 10 end
  127. for i=1, width-1 do
  128. self.form = self.form..' '
  129. end
  130.  
  131. self.func = func
  132. self.value = tostring(value)
  133.  
  134. self.x = x; self.y = y
  135. self.visible = true
  136.  
  137. return self
  138. end
  139.  
  140. function M.Textbox:draw(content)
  141. if self.visible then
  142. if content then gpu.setBackground(graycolor) end
  143. gpu.set(self.x, self.y, self.form)
  144. if content then gpu.set(self.x+2, self.y, self.value) end
  145. gpu.setBackground(backcolor)
  146. end
  147. end
  148.  
  149. function M.Textbox:click(x, y)
  150. if self.visible then
  151. if y == self.y then
  152. if x >= self.x and x < self.x+unicode.len(self.form) then
  153. self:draw(false)
  154. term.setCursor(self.x+2, self.y)
  155. value = string.sub(term.read({self.value}), 1, -2)
  156. if self.func(value) then
  157. self.value = value
  158. end
  159. self:draw(true)
  160. return true
  161. end
  162. end
  163. end
  164. return false
  165. end
  166.  
  167. function M.Textbox:setValue(value)
  168. self.value = tostring(value)
  169. end
  170.  
  171. function M.Textbox:getValue()
  172. return self.value
  173. end
  174. textboxes = {}
  175.  
  176. function M.textboxesNew(func, x, y, value, width)
  177. textbox = M.Textbox.new(func, x, y, value, width)
  178. table.insert(textboxes, textbox)
  179. return textbox
  180. end
  181. function M.textboxesDraw()
  182. for i=1, #textboxes do
  183. textboxes[i]:draw(true)
  184. end
  185. end
  186. function M.textboxesClick(x, y)
  187. for i=1, #textboxes do
  188. textboxes[i]:click(x, y)
  189. end
  190. end
  191.  
  192. return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement