terminal.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. local Terminal = {}
  2. --local EditorSheet = SpriteSheet(Image("/editorsheet.png"),24,12)
  3. Terminal.blinktime = 0.5
  4. Terminal.blinktimer = 0
  5. Terminal.blinkstate = false
  6. Terminal.textbuffer = {}
  7. Terminal.textcolors = {}
  8. Terminal.linesLimit = 14
  9. Terminal.lengthLimit = 43
  10. Terminal.currentLine = 1
  11. function Terminal:tout(text,col,skipnl)
  12. self.textcolors[self.currentLine] = col or self.textcolors[self.currentLine]
  13. if skipnl then
  14. self.textbuffer[self.currentLine] = self.textbuffer[self.currentLine]..(text or "")
  15. if self.textbuffer[self.currentLine]:len() >= self.lengthLimit then self.textbuffer[self.currentLine] = self.textbuffer[self.currentLine]:sub(0,self.lengthLimit) end
  16. else
  17. self.textbuffer[self.currentLine] = self.textbuffer[self.currentLine]..(text or "")
  18. if self.textbuffer[self.currentLine]:len() >= self.lengthLimit then self.textbuffer[self.currentLine] = self.textbuffer[self.currentLine]:sub(0,self.lengthLimit) end
  19. if self.currentLine == self.linesLimit then
  20. for i=2,self.linesLimit do self.textbuffer[i-1] = self.textbuffer[i] end --Shiftup all the text
  21. for i=2,self.linesLimit do self.textcolors[i-1] = self.textcolors[i] end
  22. self.textbuffer[self.currentLine] = "" --Add a new line
  23. else
  24. self.currentLine = self.currentLine + 1
  25. end
  26. end
  27. self:_redraw()
  28. end
  29. function Terminal:setLine(l) self.currentLine = floor(l or 1) if self.currentLine > 20 then self.currentLine = 20 elseif self.currentLine < 1 then self.currentLine = 1 end end
  30. function Terminal:splitCommand(str)
  31. local t = {}
  32. for val in str:gmatch("%S+") do
  33. if not t[0] then t[0] = val else table.insert(t, val) end
  34. end
  35. return t
  36. end
  37. function Terminal:_startup()
  38. for i=1,self.linesLimit do table.insert(self.textbuffer,"") end --Clean the framebuffer
  39. for i=1,self.linesLimit do table.insert(self.textcolors,8) end
  40. keyrepeat(true)
  41. --tout("12345678901234567890123456789012345678901234567890123456789012345678901234567890",9)
  42. self:tout("-[[liko12]]-")
  43. self:tout(_LK12VER,_LK12VERC)
  44. self:tout()
  45. self:tout("A PICO-8 CLONE WITH EXTRA ABILITIES",7)
  46. --tout()
  47. self:tout("TYPE HELP FOR HELP",10)
  48. --tout()
  49. self:tout("> ",8,true)
  50. end
  51. function Terminal:_update(dt)
  52. self.blinktimer = self.blinktimer+dt if self.blinktimer > self.blinktime then self.blinktimer = self.blinktimer - self.blinktime self.blinkstate = not self.blinkstate end
  53. local curlen = self.textbuffer[self.currentLine]:len()
  54. color(self.blinkstate and 9 or 1)
  55. rect(curlen > 0 and ((curlen)*4+8+3) or 10,(self.currentLine)*8+2,4,5)
  56. end
  57. function Terminal:_redraw()
  58. clear(1)
  59. for line,text in ipairs(self.textbuffer) do
  60. color(self.textcolors[line])
  61. if text == "-[[liko12]]-" then --THE SECRET PHASE
  62. SpriteGroup(67,9,line*8,6,1,1,1,EditorSheet)
  63. else
  64. print_grid(text,2,line+1)
  65. end
  66. end
  67. end
  68. function Terminal:_kpress(k,sc,ir)
  69. if k == "return" then
  70. self:tout()
  71. local splitted = self:splitCommand(self.textbuffer[self.currentLine-1])
  72. local CMDFunc = require("terminal_commands")[string.lower(splitted[1] or "")]
  73. if CMDFunc then
  74. CMDFunc(unpack(splitted))
  75. elseif splitted[1] then
  76. self:tout("UNKNOWN COMMAND '"..splitted[1].."' !",15)
  77. end
  78. self:tout("> ",8,true)
  79. end
  80. if k == "backspace" then self.textbuffer[self.currentLine] = self.textbuffer[self.currentLine]:sub(0,-2) self:_redraw() end
  81. end
  82. function Terminal:_tinput(t)
  83. if t == "\\" then return end --This thing is so bad, so GO AWAY
  84. if self.textbuffer[self.currentLine]:len() < self.lengthLimit then self:tout(t,8,true) end
  85. end
  86. function Terminal:_tpress()
  87. --This means the user is using a touch device
  88. self.linesLimit = 7
  89. showkeyboard(true)
  90. end
  91. return Terminal