Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local self = getfenv()
- internal = {}
- function self.new(name,x,y,width)
- local txt = {}
- txt.name = name
- txt.x = math.floor(x)
- txt.y = math.floor(y)
- txt.width = math.floor(width)
- txt.value = ""
- txt.scrolled = 0
- txt.cursorAt = 1
- txt.active = false
- txt.scrollWhenInactive = false
- txt.scrolling = false
- txt.scrollSpeed = 0.3
- txt.lastScroll = 0
- txt.scrollReverse = false
- txt.placeHolder = nil
- txt.txtColor = colors.white
- txt.bgColor = colors.black
- setmetatable(txt,{__index=internal})
- return txt
- end
- function internal:setText(str)
- self.value = str
- if self.scrolling then
- self:initScroll()
- end
- self:draw()
- end
- function internal:updateScroll(dt)
- self.lastScroll = self.lastScroll+dt
- if self.lastScroll > self.scrollSpeed then
- self.lastScroll = self.lastScroll - self.scrollSpeed
- local add = self.scrollReverse and -1 or 1
- self.scrolled = self.scrolled+add
- if self.scrolled + self.width > #self.value then
- self.scrollReverse = not self.scrollReverse
- self.scrolled = self.scrolled-add
- end
- if self.scrolled < 0 then
- self.scrolled = 0
- self.scrollReverse = not self.scrollReverse
- end
- end
- end
- function internal:initScroll()
- if #self.value > self.width then
- self.scrolling = true
- end
- self.scrolled = 0
- self.lastScroll = 0
- self.scrollReverse = false
- end
- function internal:stopScroll()
- self.scrolling = false
- end
- function internal:resetScroll()
- self.scrolled = 0
- self:draw()
- end
- function internal:drawText()
- term.setBackgroundColor(self.bgColor)
- term.setTextColor(self.txtColor)
- term.setCursorPos(self.x,self.y)
- term.write(string.rep(" ",self.width))
- term.setCursorPos(self.x,self.y)
- local dStr = self.value:sub(self.scrolled+1,self.scrolled+self.width)
- if self.placeHolder then
- dStr = string.rep(self.placeHolder,#dStr)
- end
- term.write(dStr)
- if self.active then self:setCursor() end
- end
- function internal:getCursorScreenPos()
- return self.x+self.cursorAt-1-self.scrolled
- end
- function internal:setCursor()
- local cX = self:getCursorScreenPos()
- term.setCursorPos(cX,self.y)
- end
- function internal:update(dt)
- if self.scrolling then
- self:updateScroll(dt)
- end
- end
- function internal:draw()
- self:drawText()
- end
- function internal:key(p1)
- if p1 == keys.left then
- self.cursorAt = math.max(1,self.cursorAt-1)
- if self:scrollToCursor() then self:draw() else self:setCursor() end
- elseif p1 == keys.right then
- self.cursorAt = math.min(#self.value+1,self.cursorAt+1)
- if self:scrollToCursor() then self:draw() else self:setCursor() end
- elseif p1 == keys.backspace then
- if self.cursorAt > 1 then
- self.value = self.value:sub(0,self.cursorAt-2)..self.value:sub(self.cursorAt)
- self.cursorAt = math.max(self.cursorAt-1,1)
- self:scrollToCursor()
- self:draw()
- end
- elseif p1 == keys.home then
- self.cursorAt = 1
- self:scrollToCursor()
- self:draw()
- elseif p1 == keys["end"] then
- self.cursorAt = #self.value+1
- self:scrollToCursor()
- self:draw()
- end
- end
- function internal:scrollToCursor()
- local screenPos = self:getCursorScreenPos()
- if screenPos < self.x then
- local scrollSize = self.x - screenPos
- self.scrolled = math.max(0,self.scrolled - scrollSize)
- return true
- elseif screenPos > self.x + self.width - 1 then
- local scrollSize = screenPos - self.x - self.width + 1
- self.scrolled = self.scrolled + scrollSize
- return true
- end
- return false
- end
- function internal:char(p1)
- self.value = self.value:sub(0,self.cursorAt-1)..p1..self.value:sub(self.cursorAt,#self.value)
- self.cursorAt = self.cursorAt + 1
- self:scrollToCursor()
- self:draw()
- end
- function internal:checkClicked()
- end
- function internal:activate()
- gui.activateControl(self)
- end
- function internal:deactivate()
- gui.deactivateControl()
- end
- function internal:onDeactivate()
- self.active=false
- term.setCursorBlink(false)
- if self.scrollWhenInactive then
- self:initScroll()
- end
- end
- function internal:onActivate()
- self.active=true
- term.setCursorBlink(true)
- if self.scrolling then
- self:stopScroll()
- end
- self:scrollToCursor()
- self:draw()
- self:setCursor()
- end
Advertisement
Add Comment
Please, Sign In to add comment