code.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. local cedit = {}
  2. local colorize = require("libraries.colorize_lua")
  3. cedit.colors = {
  4. text = _GetColor(8),
  5. keyword = _GetColor(15),
  6. number = _GetColor(13),
  7. comment = _GetColor(14),
  8. str = _GetColor(13),
  9. }
  10. cedit.codebuffer = {""}
  11. cedit.lineLimit = 14
  12. cedit.cursorX, cedit.cursorY = 1,1
  13. cedit.topLine = 0
  14. local lastCursorX = 1
  15. local blinktime = 0.5
  16. local blinktimer = 0
  17. local blinkstate = false
  18. function cedit:export()
  19. local code = ""
  20. for line,text in ipairs(self.codebuffer) do
  21. code = code..text.."\n"
  22. end
  23. return code
  24. end
  25. local function magiclines(s)
  26. if s:sub(-1)~="\n" then s=s.."\n" end
  27. return s:gmatch("(.-)\n")
  28. end
  29. function cedit:load(code)
  30. self.codebuffer, self.topLine, self.cursorX, self.cursorY = {}, 0, 1, 1
  31. if not code then self.codebuffer[1] = "" return self end
  32. local code = code
  33. for line in magiclines(code) do
  34. table.insert(self.codebuffer,line)
  35. end
  36. return self
  37. end
  38. function cedit:_switch()
  39. end
  40. function cedit:_redraw()
  41. rect(1,9,192,128-16,6) color(7)
  42. local tocolor = {}
  43. for i=self.topLine+1,self.topLine+self.lineLimit do
  44. if self.codebuffer[i] then table.insert(tocolor,self.codebuffer[i]) end
  45. end
  46. local colored = colorize(tocolor,self.colors)
  47. color(8)
  48. for line,text in ipairs(colored) do
  49. print_grid(text,1,line+1)
  50. end
  51. rect(1,128-7,192,8,9)
  52. color(3) print("LINE "..self.topLine+self.cursorY.."/"..#self.codebuffer,2,128-5)
  53. --[[for i=self.topLine+1,self.topLine+self.lineLimit do
  54. if self.codebuffer[i] then print_grid(self.codebuffer[i],1,(i-self.topLine)+1) end
  55. end]]
  56. end
  57. function cedit:_update(dt)
  58. blinktimer = blinktimer+dt if blinktimer > blinktime then blinktimer = blinktimer - blinktime blinkstate = not blinkstate end
  59. local curlen = self.codebuffer[self.topLine+self.cursorY]:len()
  60. if blinkstate then rect((self.cursorX-1)*4+2,(self.cursorY)*8+2,4,5,9) else self:_redraw() end
  61. end
  62. function cedit:_mmove(x,y,dx,dy,it,iw)
  63. if math.abs(y) > 5 then return end --Dead mouse wheen strike
  64. if math.abs(x) > 5 then return end --Dead mouse wheen strike
  65. if y > 0 then
  66. self:_kpress("up",0,false)
  67. elseif y < 0 then
  68. self:_kpress("down",0,false)
  69. end
  70. if x > 0 then
  71. self:_kpress("right",0,false) --Maybe ? or inverted..
  72. elseif x < 0 then
  73. self:_kpress("left",0,false)
  74. end
  75. end
  76. function cedit:_kpress(k,sc,ir)
  77. if k == "return" then
  78. if self.cursorY == self.lineLimit then --check if the cursor is at the last line
  79. self.topLine = self.topLine+1 --In that case
  80. else
  81. self.cursorY = self.cursorY+1
  82. end
  83. for i=#self.codebuffer, self.cursorY+self.topLine, -1 do self.codebuffer[i+1] = self.codebuffer[i] end--Shift down the code
  84. self.codebuffer[self.cursorY+self.topLine] = self.codebuffer[self.cursorY+self.topLine-1]:sub(self.cursorX,-1)
  85. self.codebuffer[self.cursorY+self.topLine-1] = self.codebuffer[self.cursorY+self.topLine-1]:sub(0,self.cursorX-1)
  86. --self.codebuffer[self.cursorY+self.topLine] = "" --Insert a new empty line
  87. self.cursorX, blinktimer, blinkstate = 1, 0, true --Set the cursorX to 1 (since it's an empty line), also reset the blinkstate&timer to force the cursor to blink on
  88. self:_redraw() --Update the screen content for the user
  89. elseif k == "backspace" then
  90. if self.codebuffer[self.cursorY+self.topLine] == "" then
  91. if self.cursorY ~= 1 then
  92. table.remove(self.codebuffer,self.cursorY+self.topLine) --Remove the line so the table size stays correct
  93. if self.cursorY == 1 then
  94. self.topLine = self.topLine-1
  95. if self.topLine < 1 then self.topline=0 end
  96. else
  97. self.cursorY = self.cursorY-1
  98. end
  99. self.cursorX = self.codebuffer[self.cursorY+self.topLine]:len()+1
  100. end
  101. else
  102. if self.cursorX > 1 then
  103. self.codebuffer[self.cursorY+self.topLine] = self.codebuffer[self.cursorY+self.topLine]:sub(0,self.cursorX-2)..self.codebuffer[self.cursorY+self.topLine]:sub(self.cursorX,-1)--self.codebuffer[self.cursorY+self.topLine]:sub(0,-2)
  104. self.cursorX = self.cursorX-1
  105. --[[else
  106. self.codebuffer[self.cursorY+self.topLine-1] = self.codebuffer[self.cursorY+self.topLine-1]..(self.codebuffer[self.cursorY+self.topLine] or "")
  107. for i=self.cursorY+self.topLine, #self.codebuffer do self.codebuffer[i] = self.codebuffer[i+1] end--Shift up the code
  108. table.remove(self.codebuffer,#self.codebuffer) --Drop The last line (because it got duplicated)
  109. self.cursorY = self.cursorY-1
  110. if self.cursorY < 1 then if self.topLine > 0 then self.topLine = self.topLine -1 end self.cursorY = 1 end
  111. self.cursorX = self.codebuffer[self.cursorY+self.topLine-1]:len()+1 MUST SETUP LINE LENGTH SYSTEM FIRST]]
  112. end
  113. end
  114. blinktimer, blinkstate = 0, true
  115. self:_redraw()
  116. elseif k == "up" then
  117. self.cursorY = self.cursorY-1
  118. if self.cursorY < 1 then if self.topLine > 0 then self.topLine = self.topLine -1 end self.cursorY = 1 end
  119. if ir then self.cursorX = lastCursorX else lastCursorX = self.cursorX end
  120. if self.cursorX > self.codebuffer[self.cursorY+self.topLine]:len()+1 then self.cursorX = self.codebuffer[self.cursorY+self.topLine]:len()+1 end
  121. blinktimer, blinkstate = 0, true
  122. self:_redraw()
  123. elseif k == "down" then
  124. blinktimer, blinkstate = 0, true
  125. if self.cursorY+self.topLine == #self.codebuffer then return end
  126. if self.cursorY == self.lineLimit then
  127. self.topLine = self.topLine+1
  128. else
  129. self.cursorY = self.cursorY+1
  130. end
  131. if ir then self.cursorX = lastCursorX else lastCursorX = self.cursorX end
  132. if self.cursorX > self.codebuffer[self.cursorY+self.topLine]:len()+1 then self.cursorX = self.codebuffer[self.cursorY+self.topLine]:len()+1 end
  133. self:_redraw()
  134. elseif k == "left" then
  135. self.cursorX = self.cursorX - 1
  136. if self.cursorX < 1 then self.cursorX = 1 end
  137. blinktimer, blinkstate = 0, true
  138. self:_redraw()
  139. elseif k == "right" then
  140. self.cursorX = self.cursorX + 1
  141. if self.cursorX > self.codebuffer[self.topLine+self.cursorY]:len() then self.cursorX = self.codebuffer[self.topLine+self.cursorY]:len()+1 end
  142. blinktimer, blinkstate = 0, true
  143. self:_redraw()
  144. end
  145. end
  146. function cedit:_krelease(k,sc)
  147. lastCursorX = 1
  148. end
  149. function cedit:_tinput(t)
  150. if t == "\\" then return end --This thing is so bad, so GO AWAY
  151. blinktimer, blinkstate = 0, true
  152. if self.codebuffer[self.cursorY+self.topLine]:len() == 46 then return end
  153. self.codebuffer[self.cursorY+self.topLine] = self.codebuffer[self.cursorY+self.topLine]:sub(0,self.cursorX-1)..t..self.codebuffer[self.cursorY+self.topLine]:sub(self.cursorX,-1)--self.codebuffer[self.cursorY+self.topLine]..t
  154. self.cursorX = self.cursorX+1
  155. self:_redraw()
  156. end
  157. function cedit:_tpress()
  158. --This means the user is using a touch device
  159. self.lineLimit = 7
  160. showkeyboard(true)
  161. end
  162. return cedit