Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. local TextBox = {}
  2. TextBox.__index = TextBox
  3.  
  4. function TextBox.new(x, y, w, h, delay, linePadding)
  5. local this = {
  6. x = x,
  7. y = y,
  8. width = w,
  9. height = h,
  10. padding = {
  11. x = 8,
  12. y = 16
  13. },
  14. lineSpacing = linePadding,
  15. typingTimer = 0,
  16. delayTimer = 0,
  17. typeDelay = delay,
  18. isTyping = false,
  19. isDelaying = false,
  20. text = "",
  21. currentIndex = 0,
  22.  
  23. colourChanges = {},
  24. delays = {},
  25. speedChanges = {},
  26. clearTexts = {},
  27. }
  28. setmetatable(this, TextBox)
  29. return this
  30. end
  31.  
  32. function TextBox:getRect()
  33. return self.x, self.y, self.width, self.height
  34. end
  35.  
  36. function TextBox:begin()
  37. self.isTyping = true
  38. end
  39.  
  40. local function rollBackChanges(tbl, currentIndex)
  41. local newTable = {}
  42. for index, value in pairs(tbl) do
  43. if index - currentIndex >= 0 then
  44. newTable[index - currentIndex] = value
  45. end
  46. end
  47. return newTable
  48. end
  49.  
  50. function TextBox:reset()
  51. self.colourChanges = rollBackChanges(self.colourChanges, self.currentIndex)
  52. self.delays = rollBackChanges(self.delays, self.currentIndex)
  53. self.speedChanges = rollBackChanges(self.speedChanges, self.currentIndex)
  54. self.clearTexts = rollBackChanges(self.clearTexts, self.currentIndex)
  55.  
  56. text = text:sub(currentIndex)
  57. currentIndex = 0
  58. end
  59.  
  60. function TextBox:addText(newText)
  61. self.text = self.text .. newText
  62. end
  63.  
  64. function TextBox:addLine(newText)
  65. self:addText(newText)
  66. self:addText("\n")
  67. end
  68.  
  69. function TextBox:clear()
  70. self.clearTexts[self.text:len()] = true
  71. end
  72.  
  73. function TextBox:setColour(r, g, b, a)
  74. local a = a or 255
  75. self.colourChanges[self.text:len()] = {r, g, b, a}
  76. end
  77.  
  78. function TextBox:delay(delayDuration)
  79. self.delays[self.text:len()] = delayDuration
  80. end
  81.  
  82. function TextBox:setSpeed(newDelay)
  83. self.speedChanges[self.text:len()] = newDelay
  84. end
  85.  
  86. function TextBox:update(dt)
  87. if self.isDelaying and self.delayTimer <= 0 then
  88. self.isDelaying = false
  89. elseif self.isDelaying then
  90. self.delayTimer = math.max(0, self.delayTimer - dt)
  91. return
  92. end
  93. if self.currentIndex == self.text:len() then
  94. self.isTyping = false
  95. end
  96.  
  97. if not self.isTyping then return end
  98.  
  99. self.typingTimer = self.typingTimer + dt
  100.  
  101. while self.typingTimer >= self.typeDelay do
  102. self.typingTimer = self.typingTimer - self.typeDelay
  103. self.currentIndex = self.currentIndex + 1
  104.  
  105. local shouldReturn = false
  106.  
  107. if self.speedChanges[self.currentIndex] then
  108. self.typeDelay = self.speedChanges[self.currentIndex]
  109. end
  110.  
  111. if self.delays[self.currentIndex] then
  112. self.delayTimer = self.delays[self.currentIndex]
  113. self.isDelaying = true
  114. shouldReturn = true
  115. end
  116.  
  117. if self.clearTexts[self.currentIndex] then
  118. self:reset()
  119. shouldReturn = true
  120. end
  121.  
  122. if shouldReturn then return end
  123.  
  124. if self.currentIndex >= self.text:len() then
  125. self.currentIndex = self.text:len()
  126. self.isTyping = false
  127. end
  128. end
  129.  
  130. end
  131.  
  132. function TextBox:draw()
  133. local charX = self.padding.x
  134. local charY = self.padding.y
  135. for i = 0, self.currentIndex do
  136. if self.colourChanges[i] then
  137. love.graphics.setColor(self.colourChanges[i])
  138. end
  139. local letter = self.text:sub(i, i)
  140. if letter == "\n" then
  141. charY = charY + self.lineSpacing
  142. charX = self.padding.x
  143. else
  144. local word = ""
  145. for n = i, self.text:len() do
  146. if self.text:sub(n, n) == " " then break end
  147. word = word .. self.text:sub(n, n)
  148. end
  149. if charX + love.graphics.getFont():getWidth(word) >= self.width + self.padding.x then
  150. charY = charY + self.lineSpacing
  151. charX = self.padding.x
  152. end
  153. love.graphics.print(self.text:sub(i,i), self.x + charX, self.y + charY)
  154. charX = charX + love.graphics.getFont():getWidth(self.text:sub(i,i))
  155. end
  156. end
  157. end
  158.  
  159. return TextBox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement