Advertisement
JereTheJuggler

Untitled

Feb 14th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.61 KB | None | 0 0
  1. term = require("term")
  2. local component = require("component")
  3. local event = require("event")
  4. local unicode = require("unicode")
  5. gpu = term.gpu()
  6.  
  7. function newForm(gpu, textBoxes)
  8.     local self = {}
  9.     self.gpu = gpu
  10.     self.textBoxes = textBoxes
  11.     local firstBox = nil
  12.     local previousBox = nil
  13.     for index,box in ipairs(self.textBoxes) do
  14.         if firstBox == nil then
  15.             firstBox = box
  16.         end
  17.         if previousBox ~= nil then
  18.             previousBox.nextBox = box
  19.         end
  20.         box.gpu = gpu
  21.         box:fullRender()
  22.         box.form = self
  23.         previousBox = box
  24.     end
  25.     if previousBox ~= nil and previousBox ~= firstBox then
  26.         previousBox.nextBox = firstBox
  27.     end
  28.  
  29.     function self:changeColors(background, border, value, hint)
  30.         for index,box in ipairs(self.textBoxes) do
  31.             box:changeColors(background, border, value, hint)
  32.         end
  33.     end
  34.    
  35.     function self:changeBackgoundColor(color)
  36.         for index,box in ipairs(self.textBoxes) do
  37.             box:changeBackgroundColor(color)
  38.         end
  39.     end
  40.     function self:changeBorderColor(color)
  41.         for index,box in ipairs(self.textBoxes) do
  42.             box:changeBorderColor(color)
  43.         end
  44.     end
  45.     function self:changeValueColor(color)
  46.         for index,box in ipairs(self.textBoxes) do
  47.             box:changeValueColor(color)
  48.         end
  49.     end
  50.     function self:changeHintTextColor(color)  
  51.         for index,box in ipairs(self.textBoxes) do
  52.             box:changeHintTextColor(color)
  53.         end
  54.     end
  55.  
  56.     function self:checkForFocus(x, y)
  57.         for index,box in ipairs(self.textBoxes) do
  58.             if box:isClickInRange(x,y) then
  59.                 box:onFocus(x)
  60.             end
  61.         end
  62.     end
  63.     return self
  64. end
  65.  
  66. function newTextbox(x,y,width,onValueChanged,initialValue,hintText)
  67.     local self = {}
  68.     self.hintText = hintText or ""
  69.     self.value = initialValue or ""
  70.     self.previousValue = self.value
  71.     self.gpu = nil
  72.     self.scrollPosition = 1
  73.     self.x = x
  74.     self.y = y
  75.     self.cursor = 1
  76.     self.width = width
  77.     self.form = nil
  78.  
  79.     --default cruddy colors
  80.     self.backgroundColor = 0x0000FF
  81.     self.borderColor = 0x0000FF
  82.     self.valueColor = 0x0000FF
  83.     self.hintTextColor = 0x0000FF
  84.  
  85.     function self:changeColors(background, border, value, hint)
  86.         self.backgroundColor, self.borderColor, self.valueColor, self.hintTextColor =
  87.             background or self.backgroundColor, border or self.borderColor,
  88.             value or self.valueColor, hint or self.hintTextColor
  89.         self:fullRender()
  90.     end
  91.  
  92.     function self:changeBackgoundColor(color) self.backgroundColor = color self:fullRender() end
  93.     function self:changeBorderColor(color) self.borderColor = color self:fullRender() end
  94.     function self:changeValueColor(color) self.valueColor = color self:fullRender() end
  95.     function self:changeHintTextColor(color) self.hintTextColor = color self:fullRender() end
  96.  
  97.     self.active = false
  98.     self.onValueChanged = onValueChanged or function(newValue) end
  99.     self.nextBox = nil
  100.     function self:setValue(val)
  101.         self.value = val
  102.         self.cursor = 1
  103.         self.scrollPosition = 1
  104.         self:render()
  105.     end
  106.     function self:clear()
  107.         self:setValue("")
  108.     end        
  109.     function self:fullRender()
  110.         self.gpu.setBackground(self.backgroundColor)
  111.         self.gpu.setForeground(self.borderColor)
  112.         local topLine = unicode.char(0x2554)
  113.         local bottomLine = unicode.char(0x255A)
  114.         for i=1,self.width,1 do
  115.             topLine = topLine..unicode.char(0x2550)
  116.             bottomLine = bottomLine..unicode.char(0x2550)
  117.         end
  118.         topLine = topLine..unicode.char(0x2557)
  119.         bottomLine = bottomLine..unicode.char(0x255D)
  120.         self.gpu.set(self.x-1,self.y-1,topLine)
  121.         self.gpu.set(self.x-1,self.y+1,bottomLine)
  122.         self.gpu.set(self.x-1,self.y,unicode.char(0x2551))
  123.         self.gpu.set(self.x+self.width,self.y,unicode.char(0x2551))
  124.         self:render()
  125.     end
  126.     function self:render()
  127.         if self.cursor - self.scrollPosition >= self.width then
  128.             self.scrollPosition = self.cursor - self.width + 1
  129.         end
  130.         self.gpu.setBackground(self.backgroundColor)
  131.         self.gpu.setForeground(self.valueColor)
  132.         self.gpu.fill(self.x,self.y,self.width,1," ")
  133.         if string.len(self.value) > 0 then
  134.             self.gpu.set(self.x,self.y,string.sub(self.value,self.scrollPosition,self.scrollPosition+self.width-1))
  135.         elseif string.len(self.hintText) > 0 then
  136.             self.gpu.setForeground(self.hintTextColor)
  137.             self.gpu.set(self.x,self.y,self.hintText)
  138.             self.gpu.setForeground(self.valueColor)
  139.         end
  140.         self:moveCursor()
  141.     end
  142.     function self:moveCursor()
  143.         if self.cursor > string.len(self.value) then
  144.             self.cursor = string.len(self.value)+1
  145.         end
  146.         term.setCursor(self.x+self.cursor-self.scrollPosition,self.y)
  147.     end
  148.     function self:isClickInRange(mouseX,mouseY)
  149.         return mouseY == self.y and mouseX >= self.x and mouseX <= self.x + self.width - 1
  150.     end
  151.     function self:onFocus(mouseX)
  152.         mouseX = mouseX or self.x+string.len(self.value)
  153.         self.cursor = mouseX-self.x+self.scrollPosition
  154.         self:render()
  155.         term.setCursorBlink(true)
  156.         self.active = true
  157.         self.previousValue = self.value
  158.         local goToNext = false
  159.         local lastX,lastY = nil,nil
  160.         while self.active do
  161.             local eventType, p1, p2, p3, p4, p5 = term.pull()
  162.             if eventType == "touch" then
  163.                 local screenAddress, x, y, button, player = p1, p2, p3, p4, p5
  164.                 if self:isClickInRange(x,y) then
  165.                     self.cursor = x-self.x+self.scrollPosition
  166.                     self:moveCursor()
  167.                 else
  168.                     self.active = false
  169.                     lastX,lastY = x,y
  170.                 end
  171.             elseif eventType == "key_down" then
  172.                 lastX, lastY = nil,nil
  173.                 local keyboardAddress, chr, code, player = p1, p2, p3, p4
  174.                 -- code 15 = tab
  175.                 -- printable char range = 32-126
  176.                 if chr >= 32 and chr <= 126 then
  177.                     self.value = string.sub(self.value,1,self.cursor-1)..unicode.char(chr)..string.sub(self.value,self.cursor)
  178.                     self.cursor = self.cursor+1
  179.                     if self.cursor - self.scrollPosition > self.width then
  180.                         self.scrollPosition = self.scrollPosition + 1
  181.                     end
  182.                     self:render()
  183.                 elseif code == 14 then --backspace
  184.                     if string.len(self.value) > 0 and self.cursor > 1 then
  185.                         self.value = string.sub(self.value,1,self.cursor-2)..string.sub(self.value,self.cursor)
  186.                         self.cursor = self.cursor-1
  187.                         if self.cursor - self.scrollPosition < 2  and self.scrollPosition > 1 then
  188.                             self.scrollPosition = self.cursor - 1
  189.                         end
  190.                         self:render()
  191.                     end
  192.                 elseif code == 203 then --left
  193.                     if self.cursor > 1 then
  194.                         self.cursor = self.cursor-1
  195.                         if self.scrollPosition > 1 and self.cursor - self.scrollPosition < 1 then
  196.                             self.scrollPosition = self.cursor
  197.                         end
  198.                         self:render()
  199.                     end
  200.                 elseif code == 205 then --right
  201.                     if self.cursor <= string.len(self.value) then
  202.                         self.cursor = self.cursor+1
  203.                         self:render()
  204.                     end
  205.                 elseif code == 28 then --enter
  206.                     self.active = false
  207.                 elseif code == 211 then --delete
  208.                     if string.len(self.value) > 0 and self.cursor <= string.len(self.value) then
  209.                         self.value = string.sub(self.value,1,self.cursor-1)..string.sub(self.value,self.cursor+1)
  210.                         self:render()
  211.                     end
  212.                 elseif code == 199 then --home
  213.                     if self.cursor > 1 then
  214.                         self.cursor = 1
  215.                         self.scrollPosition = 1
  216.                         self:render()
  217.                     end
  218.                 elseif code == 207 then --end
  219.                     if self.cursor <= string.len(self.value) then
  220.                         self.cursor = string.len(self.value) + 1
  221.                         self:render()
  222.                     end
  223.                 elseif code == 15 then
  224.                     if self.nextBox ~= nil then
  225.                         goToNext = true
  226.                         self.active = false
  227.                     end
  228.                 end
  229.             end
  230.         end
  231.         --blur control
  232.         self.cursor = 1
  233.         self.scrollPosition = 1
  234.         self:render()
  235.         if self.value ~= self.previousValue then
  236.             self.onValueChanged(self.value)
  237.         end
  238.         if goToNext then
  239.             self.nextBox:onFocus()
  240.         end
  241.         if self.form ~= nil and lastX ~= nil and lastY ~= nil then
  242.             self.form:checkForFocus(lastX,lastY)
  243.         end
  244.     end
  245.     return self
  246. end
  247.  
  248.  
  249. ----------------------------------------------------------------
  250.  
  251.  
  252. backgroundColor = 0x333333
  253. textColor = 0x00FF00
  254. hintTextColor = 0xCCCCCC
  255.  
  256. gpu.setBackground(backgroundColor)
  257. term.clear()
  258.  
  259. local form = newForm(gpu,{
  260.     newTextbox(5,5,15), newTextbox(22,5,15), newTextbox(39,5,15),
  261.     newTextbox(5,8,15), newTextbox(22,8,15), newTextbox(39,8,15),
  262.     newTextbox(5,11,15), newTextbox(22,11,15), newTextbox(39,11,15),
  263.     newTextbox(20,20,15,function(newValue) term.setCursor(4,30) term.write("you entered "..newValue) end)
  264. })
  265.  
  266. form:changeColors(backgroundColor, 0xFFFFFF, textColor, hintTextColor)
  267.  
  268. local cont = true
  269. while cont do
  270.     local eventType, p1, p2, p3, p4, p5 = event.pull()
  271.     if eventType == "touch" then
  272.         local x,y = p2,p3
  273.         for index,box in ipairs(form.textBoxes) do
  274.             if box:isClickInRange(x,y) then
  275.                 box:onFocus(x)
  276.             end
  277.         end
  278.     elseif eventType == "key_down" then
  279.         if p3 == 14 then --self is backspace probably
  280.             for k,v in ipairs(form.textBoxes) do
  281.                 v:clear()
  282.             end
  283.             cont = false
  284.             gpu.setBackground(0x000000)
  285.             gpu.setForeground(0xFFFFFF)
  286.             term.clear()
  287.             term.setCursor(1,1)
  288.         end
  289.     end
  290. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement