TheGameBoy_95

Untitled

May 19th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. local Graph = {
  2. getColor = function(self, value, trace)
  3. local colorIndex = math.ceil((value - self.min[trace]) / ((self.max[trace] - self.min[trace]) / #self.gradient[trace]))
  4. if colorIndex == 0 then colorIndex = 1 end
  5. if self.target.isColor() then
  6. return self.gradient[trace][colorIndex]
  7. else
  8. return colors.white
  9. end
  10. end,
  11. draw = function(self)
  12. --get the updates first.
  13. for i=1, #self.update do
  14. self.history[i][#self.history[i] + 1] = self.update[i]()
  15. end
  16. if self.target.setTextScale then self.target.setTextScale(0.5) end
  17. self.target.setBackgroundColor(colors.black)
  18. self.target.setTextColor(colors.white)
  19. self.target.clear()
  20. local newMax, newMin = self.max[1], self.min[1]
  21. --draw traces
  22. for trace=1,#self.update do
  23. if self.history[trace][#self.history[trace]] > self.max[trace] then
  24. if self.sharedBounds and self.history[trace][#self.history[trace]] > newMax then
  25. newMax = self.history[trace][#self.history[trace]]
  26. else
  27. self.max[trace] = self.history[trace][#self.history[trace]]
  28. end
  29. elseif self.history[trace][#self.history[trace]] < self.min[trace] then
  30. if self.sharedBounds and self.history[trace][#self.history[trace]] < newMin then
  31. newMin = self.history[trace][#self.history[trace]]
  32. else
  33. self.min[trace] = self.history[trace][#self.history[trace]]
  34. end
  35. end
  36. local xLim, yLim = self.target.getSize()
  37. if #self.history[trace] > xLim then table.remove(self.history[trace], 1) end
  38. --we set up our value string for numeric value readout here, but draw it later so it doesn't interfere with the full-screen trace.
  39. for i=1, #self.history[trace] do
  40. self.target.setCursorPos(i, yLim - math.ceil((yLim - 1) * (self.history[trace][i] - self.min[trace]) / (self.max[trace] - self.min[trace])))
  41. self.target.setBackgroundColor(self:getColor(self.history[trace][i], trace))
  42. self.target.write(" ")
  43. self.target.setBackgroundColor(colors.black)
  44. end
  45. end
  46. --adjust min-max for next round.
  47. if self.sharedBounds then
  48. for i=1, #self.update do
  49. self.max[i] = newMax
  50. self.min[i] = newMin
  51. end
  52. end
  53. --draw trace labels.
  54. for trace = 1, #self.update do
  55. local valueString, cStart, cEnd = ""
  56. if self.valueText[trace] then
  57. valueString = self.valueText[trace]..": "
  58. cStart = #valueString + 1
  59. valueString = valueString..tostring(self.history[trace][#self.history[trace]])
  60. cEnd = #valueString
  61. valueString = valueString.." / "..self.max[trace]
  62. end
  63. local xLim, yLim = self.target.getSize()
  64. if trace + 1 <= yLim then
  65. self.target.setCursorPos(2, trace + 1)
  66. for i=1, #valueString do
  67. if i >= cStart and i <= cEnd then
  68. self.target.setTextColor(self:getColor(self.history[trace][#self.history[trace]], trace))
  69. end
  70. self.target.write(string.sub(valueString, i, i))
  71. self.target.setTextColor(colors.white)
  72. end
  73. end
  74. end
  75. end,
  76. tostring = function(self)
  77. local valueString = ""
  78. if self.valueText[1] then
  79. valueString = self.valueText[1]..": "
  80. end
  81. valueString = valueString..tostring(self.history[1][#self.history[1]]).." / "..self.max[1]
  82. return valueString
  83. end,
  84. }
  85.  
  86. local gmetatable = {
  87. __index = Graph,
  88. __tostring = function(g) return g:tostring() end,
  89. }
  90.  
  91. function new(target, updateFunction, valueText, gradientTable, min, max, sharedBounds)
  92. if not target then return nil, "Must specify output target!" end
  93. if not updateFunction then return nil, "Must specify update function!" end
  94. local g = {
  95. min = type(min) == "table" and min or {},
  96. max = type(max) == "table" and max or {},
  97. target = target,
  98. update = type(updateFunction) == "table" and updateFunction or {updateFunction},
  99. history = {},
  100. gradient = {},
  101. valueText = type(valueText) == "table" and valueText or {valueText},
  102. sharedBounds = shareBounds or false
  103. }
  104. for i=1, #g.update do
  105. g.history[i] = {}
  106. end
  107. if type(min) ~= "table" then
  108. local setMin = min or 0
  109. for i=1, #g.update do
  110. g.min[i] = setMin
  111. end
  112. end
  113. if type(max) ~= "table" then
  114. local setMax = max or 1
  115. for i=1, #g.update do
  116. g.max[i] = setMax
  117. end
  118. end
  119. if gradientTable and type(gradientTable) == "table" and type(gradientTable[1]) == "table" then
  120. g.gradient = gradientTable
  121. elseif gradientTable and type(gradientTable) == "table" then
  122. for i=1, #g.update do
  123. g.gradient[i] = gradientTable
  124. end
  125. else
  126. for i=1, #g.update do
  127. g.gradient[i] = {colors.red, colors.orange, colors.yellow, colors.lime, colors.green}
  128. end
  129. end
  130. setmetatable(g, gmetatable)
  131. return g
  132. end
Add Comment
Please, Sign In to add comment