joebodo

sys.apis.glasses.lua

Aug 6th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.64 KB | None | 0 0
  1. local class = require('class')
  2. local UI = require('ui')
  3. local Event = require('event')
  4. local Peripheral = require('peripheral')
  5.  
  6. --[[-- Glasses device --]]--
  7. local Glasses = class()
  8. function Glasses:init(args)
  9.  
  10.   local defaults = {
  11.     backgroundColor = colors.black,
  12.     textColor = colors.white,
  13.     textScale = .5,
  14.     backgroundOpacity = .5,
  15.     multiplier = 2.6665,
  16. --    multiplier = 2.333,
  17.   }
  18.   defaults.width, defaults.height = term.getSize()
  19.  
  20.   UI.setProperties(defaults, args)
  21.   UI.setProperties(self, defaults)
  22.  
  23.   self.bridge = Peripheral.get({
  24.     type = 'openperipheral_bridge',
  25.     method = 'addBox',
  26.   })
  27.   self.bridge.clear()
  28.  
  29.   self.setBackgroundColor = function(...) end
  30.   self.setTextColor = function(...) end
  31.  
  32.   self.t = { }
  33.   for i = 1, self.height do
  34.     self.t[i] = {
  35.       text = string.rep(' ', self.width+1),
  36.       --text = self.bridge.addText(0, 40+i*4, string.rep(' ', self.width+1), 0xffffff),
  37.       bg = { },
  38.       textFields = { },
  39.     }
  40.   end
  41. end
  42.  
  43. function Glasses:setBackgroundBox(boxes, ax, bx, y, bgColor)
  44.   local colors = {
  45.     [ colors.black ] = 0x000000,
  46.     [ colors.brown ] = 0x7F664C,
  47.     [ colors.blue  ] = 0x253192,
  48.     [ colors.red   ] = 0xFF0000,
  49.     [ colors.gray  ] = 0x272727,
  50.     [ colors.lime  ] = 0x426A0D,
  51.     [ colors.green ] = 0x2D5628,
  52.     [ colors.white ] = 0xFFFFFF
  53.   }
  54.  
  55.   local function overlap(box, ax, bx)
  56.     if bx < box.ax or ax > box.bx then
  57.       return false
  58.     end
  59.     return true
  60.   end
  61.  
  62.   for _,box in pairs(boxes) do
  63.     if overlap(box, ax, bx) then
  64.       if box.bgColor == bgColor then
  65.         ax = math.min(ax, box.ax)
  66.         bx = math.max(bx, box.bx)
  67.         box.ax = box.bx + 1
  68.       elseif ax == box.ax then
  69.         box.ax = bx + 1
  70.       elseif ax > box.ax then
  71.         if bx < box.bx then
  72.           table.insert(boxes, { -- split
  73.             ax = bx + 1,
  74.             bx = box.bx,
  75.             bgColor = box.bgColor
  76.           })
  77.           box.bx = ax - 1
  78.           break
  79.         else
  80.           box.ax = box.bx + 1
  81.         end
  82.       elseif ax < box.ax then
  83.         if bx > box.bx then
  84.           box.ax = box.bx + 1 -- delete
  85.         else
  86.           box.ax = bx + 1
  87.         end
  88.       end
  89.     end
  90.   end
  91.   if bgColor ~= colors.black then
  92.     table.insert(boxes, {
  93.       ax = ax,
  94.       bx = bx,
  95.       bgColor = bgColor
  96.     })
  97.   end
  98.  
  99.   local deleted
  100.   repeat
  101.     deleted = false
  102.     for k,box in pairs(boxes) do
  103.       if box.ax > box.bx then
  104.         if box.box then
  105.           box.box.delete()
  106.         end
  107.         table.remove(boxes, k)
  108.         deleted = true
  109.         break
  110.       end
  111.       if not box.box then
  112.         box.box = self.bridge.addBox(
  113.           math.floor(self.x + (box.ax - 1) * self.multiplier),
  114.           self.y + y * 4,
  115.           math.ceil((box.bx - box.ax + 1) * self.multiplier),
  116.           4,
  117.           colors[bgColor],
  118.           self.backgroundOpacity)
  119.       else
  120.         box.box.setX(self.x + math.floor((box.ax - 1) * self.multiplier))
  121.         box.box.setWidth(math.ceil((box.bx - box.ax + 1) * self.multiplier))
  122.       end
  123.     end
  124.   until not deleted
  125. end
  126.  
  127. function Glasses:write(x, y, text, bg)
  128.  
  129.   if x < 1 then
  130.     error(' less ', 6)
  131.   end
  132.   if y <= #self.t then
  133.     local line = self.t[y]
  134.     local str = line.text
  135.     str = str:sub(1, x-1) .. text .. str:sub(x + #text)
  136.     self.t[y].text = str
  137.  
  138.     for _,tf in pairs(line.textFields) do
  139.       tf.delete()
  140.     end
  141.     line.textFields = { }
  142.  
  143.     local function split(st)
  144.       local words = { }
  145.       local offset = 0
  146.       while true do
  147.         local b,e,w = st:find('(%S+)')
  148.         if not b then
  149.           break
  150.         end
  151.         table.insert(words, {
  152.           offset = b + offset - 1,
  153.           text = w,
  154.         })
  155.         offset = offset + e
  156.         st = st:sub(e + 1)
  157.       end
  158.       return words
  159.     end
  160.  
  161.     local words = split(str)
  162.     for _,word in pairs(words) do
  163.       local tf = self.bridge.addText(self.x + word.offset * self.multiplier,
  164.                                      self.y+y*4, '', 0xffffff)
  165.       tf.setScale(self.textScale)
  166.       tf.setZ(1)
  167.       tf.setText(word.text)
  168.       table.insert(line.textFields, tf)
  169.     end
  170.  
  171.     self:setBackgroundBox(line.bg, x, x + #text - 1, y, bg)
  172.   end
  173. end
  174.  
  175. function Glasses:clear(bg)
  176.   for _,line in pairs(self.t) do
  177.     for _,tf in pairs(line.textFields) do
  178.       tf.delete()
  179.     end
  180.     line.textFields = { }
  181.     line.text = string.rep(' ', self.width+1)
  182. --    self.t[i].text.setText('')
  183.   end
  184. end
  185.  
  186. function Glasses:reset()
  187.   self:clear()
  188.   self.bridge.clear()
  189.   self.bridge.sync()
  190. end
  191.  
  192. function Glasses:sync()
  193.   self.bridge.sync()
  194. end
  195.  
  196. return Glasses
Add Comment
Please, Sign In to add comment