scrlog.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. local scrlog = {}
  2. scrlog.lines = {}
  3. scrlog.lineNum = 0
  4. scrlog.showLines = 10
  5. scrlog.scroll = 0
  6. scrlog.transparency = 220
  7. scrlog.textxOffset = 2
  8. scrlog.bghOffset = 2
  9. scrlog.font = love.graphics.newFont("oldsansblack.ttf", 12)
  10. scrlog.fontHeight = scrlog.font:getHeight()
  11. scrlog.width = love.graphics.getWidth()
  12. scrlog.hide = true
  13. scrlog._alpha = 0
  14. function scrlog.draw()
  15. local x, y = love.mouse.getPosition()
  16. if x < scrlog.width and y < scrlog.getHeight() then
  17. scrlog._alpha = math.min(scrlog._alpha + 5, 100)
  18. else
  19. scrlog._alpha = math.max(scrlog._alpha - 5, 0)
  20. end
  21. local text_alpha = scrlog.transparency + ((255-scrlog.transparency)*0.01) * scrlog._alpha
  22. local bg_alpha = scrlog.transparency * 0.01 * scrlog._alpha
  23. love.graphics.setColor(10, 10, 10, bg_alpha)
  24. love.graphics.rectangle( "fill", 0, 0, scrlog.width, scrlog.getHeight() )
  25. love.graphics.setColor(255, 255, 255, bg_alpha)
  26. love.graphics.rectangle( "fill", scrlog.width-5, (scrlog.scroll/(scrlog.lineNum - scrlog.getShowLines())*(scrlog.getHeight()-2)), 5, 2 )
  27. --Draw text log
  28. love.graphics.setFont(scrlog.font)
  29. love.graphics.setColor(200, 200, 200, text_alpha)
  30. for i = 1, scrlog.getShowLines() do
  31. if scrlog.lines[i + scrlog.scroll] then
  32. love.graphics.print(scrlog.lines[i + scrlog.scroll], scrlog.textxOffset, (i*scrlog.fontHeight))
  33. end
  34. end
  35. end
  36. function scrlog.config(t)
  37. for i, v in pairs(t) do
  38. if scrlog[i] then
  39. scrlog[i] = v
  40. end
  41. end
  42. end
  43. function scrlog.getHeight()
  44. return scrlog.fontHeight * scrlog.getShowLines() + scrlog.bghOffset;
  45. end
  46. function scrlog.getShowLines()
  47. return (scrlog.hide and 1 or scrlog.showLines);
  48. end
  49. function scrlog.maxScroll()
  50. return ((scrlog.lineNum >= scrlog.getShowLines()) and (scrlog.lineNum - scrlog.getShowLines()) or 0)
  51. end
  52. function scrlog.doScroll(n)
  53. scrlog.scroll = math.max(0, math.min(scrlog.scroll + n, scrlog.maxScroll()))
  54. end
  55. function scrlog.scrollToBottom()
  56. scrlog.scroll = scrlog.maxScroll()
  57. end
  58. function scrlog.toggle()
  59. scrlog.hide = not scrlog.hide
  60. scrlog.scrollToBottom()
  61. end
  62. scrlog.print = print
  63. function print(...)
  64. --scrlog.print(...)
  65. local str = table.concat({...}, " ")
  66. str = string.gsub(str, "\t", " ")
  67. table.insert(scrlog.lines, str)
  68. scrlog.lineNum = scrlog.lineNum + 1
  69. scrlog.scrollToBottom()
  70. end
  71. function scrlog.mousepressed(x, y, mouse)
  72. if x > scrlog.width or y > scrlog.getHeight() then return end
  73. if (mouse == 'l') then
  74. scrlog.toggle()
  75. elseif (mouse == 'wu') then
  76. scrlog.doScroll(-1)
  77. elseif (mouse == 'wd') then
  78. scrlog.doScroll(1)
  79. end
  80. end
  81. return scrlog