Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local TextBox = {}
- TextBox.__index = TextBox
- function TextBox.new(x, y, w, h, delay, linePadding)
- local this = {
- x = x,
- y = y,
- width = w,
- height = h,
- padding = {
- x = 8,
- y = 16
- },
- lineSpacing = linePadding,
- typingTimer = 0,
- delayTimer = 0,
- typeDelay = delay,
- isTyping = false,
- isDelaying = false,
- text = "",
- currentIndex = 0,
- colourChanges = {},
- delays = {},
- speedChanges = {},
- clearTexts = {},
- }
- setmetatable(this, TextBox)
- return this
- end
- function TextBox:getRect()
- return self.x, self.y, self.width, self.height
- end
- function TextBox:begin()
- self.isTyping = true
- end
- local function rollBackChanges(tbl, currentIndex)
- local newTable = {}
- for index, value in pairs(tbl) do
- if index - currentIndex >= 0 then
- newTable[index - currentIndex] = value
- end
- end
- return newTable
- end
- function TextBox:reset()
- self.colourChanges = rollBackChanges(self.colourChanges, self.currentIndex)
- self.delays = rollBackChanges(self.delays, self.currentIndex)
- self.speedChanges = rollBackChanges(self.speedChanges, self.currentIndex)
- self.clearTexts = rollBackChanges(self.clearTexts, self.currentIndex)
- text = text:sub(currentIndex)
- currentIndex = 0
- end
- function TextBox:addText(newText)
- self.text = self.text .. newText
- end
- function TextBox:addLine(newText)
- self:addText(newText)
- self:addText("\n")
- end
- function TextBox:clear()
- self.clearTexts[self.text:len()] = true
- end
- function TextBox:setColour(r, g, b, a)
- local a = a or 255
- self.colourChanges[self.text:len()] = {r, g, b, a}
- end
- function TextBox:delay(delayDuration)
- self.delays[self.text:len()] = delayDuration
- end
- function TextBox:setSpeed(newDelay)
- self.speedChanges[self.text:len()] = newDelay
- end
- function TextBox:update(dt)
- if self.isDelaying and self.delayTimer <= 0 then
- self.isDelaying = false
- elseif self.isDelaying then
- self.delayTimer = math.max(0, self.delayTimer - dt)
- return
- end
- if self.currentIndex == self.text:len() then
- self.isTyping = false
- end
- if not self.isTyping then return end
- self.typingTimer = self.typingTimer + dt
- while self.typingTimer >= self.typeDelay do
- self.typingTimer = self.typingTimer - self.typeDelay
- self.currentIndex = self.currentIndex + 1
- local shouldReturn = false
- if self.speedChanges[self.currentIndex] then
- self.typeDelay = self.speedChanges[self.currentIndex]
- end
- if self.delays[self.currentIndex] then
- self.delayTimer = self.delays[self.currentIndex]
- self.isDelaying = true
- shouldReturn = true
- end
- if self.clearTexts[self.currentIndex] then
- self:reset()
- shouldReturn = true
- end
- if shouldReturn then return end
- if self.currentIndex >= self.text:len() then
- self.currentIndex = self.text:len()
- self.isTyping = false
- end
- end
- end
- function TextBox:draw()
- local charX = self.padding.x
- local charY = self.padding.y
- for i = 0, self.currentIndex do
- if self.colourChanges[i] then
- love.graphics.setColor(self.colourChanges[i])
- end
- local letter = self.text:sub(i, i)
- if letter == "\n" then
- charY = charY + self.lineSpacing
- charX = self.padding.x
- else
- local word = ""
- for n = i, self.text:len() do
- if self.text:sub(n, n) == " " then break end
- word = word .. self.text:sub(n, n)
- end
- if charX + love.graphics.getFont():getWidth(word) >= self.width + self.padding.x then
- charY = charY + self.lineSpacing
- charX = self.padding.x
- end
- love.graphics.print(self.text:sub(i,i), self.x + charX, self.y + charY)
- charX = charX + love.graphics.getFont():getWidth(self.text:sub(i,i))
- end
- end
- end
- return TextBox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement