Pinkishu

gui-textbox

Jun 9th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 KB | None | 0 0
  1. local self = getfenv()
  2. internal = {}
  3.  
  4. function self.new(name,x,y,width)
  5.     local txt = {}
  6.     txt.name = name
  7.     txt.x = math.floor(x)
  8.     txt.y = math.floor(y)
  9.     txt.width = math.floor(width)
  10.     txt.value = ""
  11.     txt.scrolled = 0
  12.     txt.cursorAt = 1
  13.     txt.active = false
  14.     txt.scrollWhenInactive = false
  15.     txt.scrolling = false
  16.     txt.scrollSpeed = 0.3
  17.     txt.lastScroll = 0
  18.     txt.scrollReverse = false
  19.     txt.placeHolder = nil
  20.     txt.txtColor = colors.white
  21.     txt.bgColor = colors.black
  22.  
  23.     setmetatable(txt,{__index=internal})
  24.     return txt
  25. end
  26.  
  27. function internal:setText(str)
  28.     self.value = str
  29.     if self.scrolling then
  30.         self:initScroll()
  31.     end
  32.     self:draw()
  33. end
  34.  
  35. function internal:updateScroll(dt)
  36.     self.lastScroll = self.lastScroll+dt
  37.     if self.lastScroll > self.scrollSpeed then
  38.         self.lastScroll = self.lastScroll - self.scrollSpeed
  39.         local add = self.scrollReverse and -1 or 1
  40.         self.scrolled = self.scrolled+add
  41.         if self.scrolled + self.width > #self.value then
  42.             self.scrollReverse = not self.scrollReverse
  43.             self.scrolled = self.scrolled-add
  44.         end
  45.         if self.scrolled < 0 then
  46.             self.scrolled = 0
  47.             self.scrollReverse = not self.scrollReverse
  48.         end
  49.     end
  50. end
  51.  
  52. function internal:initScroll()
  53.     if #self.value > self.width then
  54.         self.scrolling = true
  55.     end
  56.     self.scrolled = 0
  57.     self.lastScroll = 0
  58.     self.scrollReverse = false
  59. end
  60.  
  61. function internal:stopScroll()
  62.     self.scrolling = false
  63. end
  64.  
  65. function internal:resetScroll()
  66.     self.scrolled = 0
  67.     self:draw()
  68. end
  69.  
  70. function internal:drawText()
  71.     term.setBackgroundColor(self.bgColor)
  72.     term.setTextColor(self.txtColor)
  73.     term.setCursorPos(self.x,self.y)
  74.     term.write(string.rep(" ",self.width))
  75.     term.setCursorPos(self.x,self.y)
  76.     local dStr = self.value:sub(self.scrolled+1,self.scrolled+self.width)
  77.     if self.placeHolder then
  78.         dStr = string.rep(self.placeHolder,#dStr)
  79.     end
  80.     term.write(dStr)
  81.     if self.active then self:setCursor() end
  82. end
  83.  
  84. function internal:getCursorScreenPos()
  85.     return self.x+self.cursorAt-1-self.scrolled
  86. end
  87.  
  88. function internal:setCursor()
  89.     local cX = self:getCursorScreenPos()
  90.     term.setCursorPos(cX,self.y)
  91. end
  92.  
  93. function internal:update(dt)
  94.     if self.scrolling then
  95.         self:updateScroll(dt)
  96.     end
  97. end
  98.  
  99. function internal:draw()
  100.     self:drawText()
  101. end
  102.  
  103. function internal:key(p1)
  104.     if p1 == keys.left then
  105.         self.cursorAt = math.max(1,self.cursorAt-1)
  106.         if self:scrollToCursor() then self:draw() else self:setCursor() end
  107.     elseif p1 == keys.right then
  108.         self.cursorAt = math.min(#self.value+1,self.cursorAt+1)
  109.         if self:scrollToCursor() then self:draw() else self:setCursor() end
  110.     elseif p1 == keys.backspace then
  111.         if self.cursorAt > 1 then
  112.             self.value = self.value:sub(0,self.cursorAt-2)..self.value:sub(self.cursorAt)
  113.             self.cursorAt = math.max(self.cursorAt-1,1)
  114.  
  115.             self:scrollToCursor()
  116.             self:draw()
  117.         end
  118.     elseif p1 == keys.home then
  119.         self.cursorAt = 1
  120.         self:scrollToCursor()
  121.         self:draw()
  122.     elseif p1 == keys["end"] then
  123.         self.cursorAt = #self.value+1
  124.         self:scrollToCursor()
  125.         self:draw()
  126.     end
  127. end
  128.  
  129. function internal:scrollToCursor()
  130.     local screenPos = self:getCursorScreenPos()
  131.     if screenPos < self.x then
  132.         local scrollSize = self.x - screenPos
  133.         self.scrolled = math.max(0,self.scrolled - scrollSize)
  134.         return true
  135.     elseif screenPos > self.x + self.width - 1 then
  136.         local scrollSize = screenPos - self.x - self.width + 1
  137.         self.scrolled = self.scrolled + scrollSize
  138.         return true
  139.     end
  140.     return false
  141. end
  142.  
  143. function internal:char(p1)
  144.     self.value = self.value:sub(0,self.cursorAt-1)..p1..self.value:sub(self.cursorAt,#self.value)
  145.     self.cursorAt = self.cursorAt + 1
  146.     self:scrollToCursor()
  147.     self:draw()
  148. end
  149.  
  150. function internal:checkClicked()
  151. end
  152.  
  153. function internal:activate()
  154.     gui.activateControl(self)
  155. end
  156.  
  157. function internal:deactivate()
  158.     gui.deactivateControl()
  159. end
  160.  
  161. function internal:onDeactivate()
  162.     self.active=false
  163.     term.setCursorBlink(false)
  164.     if self.scrollWhenInactive then
  165.         self:initScroll()    
  166.     end
  167. end
  168.  
  169. function internal:onActivate()
  170.     self.active=true
  171.     term.setCursorBlink(true)
  172.     if self.scrolling then
  173.         self:stopScroll()
  174.     end
  175.     self:scrollToCursor()
  176.     self:draw()
  177.     self:setCursor()
  178. end
Advertisement
Add Comment
Please, Sign In to add comment