Advertisement
GravityCube

Untitled

Oct 10th, 2019
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.33 KB | None | 0 0
  1. local tHex = {
  2. [ colors.white ] = "0",
  3. [ colors.orange ] = "1",
  4. [ colors.magenta ] = "2",
  5. [ colors.lightBlue ] = "3",
  6. [ colors.yellow ] = "4",
  7. [ colors.lime ] = "5",
  8. [ colors.pink ] = "6",
  9. [ colors.gray ] = "7",
  10. [ colors.lightGray ] = "8",
  11. [ colors.cyan ] = "9",
  12. [ colors.purple ] = "a",
  13. [ colors.blue ] = "b",
  14. [ colors.brown ] = "c",
  15. [ colors.green ] = "d",
  16. [ colors.red ] = "e",
  17. [ colors.black ] = "f",
  18. }
  19.  
  20. local type = type
  21. local string_rep = string.rep
  22. local string_sub = string.sub
  23. local table_unpack = table.unpack
  24.  
  25. function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
  26. if type( parent ) ~= "table" then error( "bad argument #1 (expected table, got " .. type( parent ) .. ")", 2 ) end
  27. if type( nX ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( nX ) .. ")", 2 ) end
  28. if type( nY ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nY ) .. ")", 2 ) end
  29. if type( nWidth ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( nWidth ) .. ")", 2 ) end
  30. if type( nHeight ) ~= "number" then error( "bad argument #5 (expected number, got " .. type( nHeight ) .. ")", 2 ) end
  31. if bStartVisible ~= nil and type( bStartVisible ) ~= "boolean" then error( "bad argument #6 (expected boolean, got " .. type( bStartVisible ) .. ")", 2 ) end
  32.  
  33. if parent == term then
  34. error( "term is not a recommended window parent, try term.current() instead", 2 )
  35. end
  36.  
  37. local sEmptySpaceLine
  38. local tEmptyColorLines = {}
  39. local function createEmptyLines( nWidth )
  40. sEmptySpaceLine = string_rep( " ", nWidth )
  41. for n=0,15 do
  42. local nColor = 2^n
  43. local sHex = tHex[nColor]
  44. tEmptyColorLines[nColor] = string_rep( sHex, nWidth )
  45. end
  46. end
  47.  
  48. createEmptyLines( nWidth )
  49.  
  50. -- Setup
  51. local bVisible = (bStartVisible ~= false)
  52. local nCursorX = 1
  53. local nCursorY = 1
  54. local bCursorBlink = false
  55. local nTextColor = colors.white
  56. local nBackgroundColor = colors.black
  57. local tLines = {}
  58. do
  59. local sEmptyText = sEmptySpaceLine
  60. local sEmptyTextColor = tEmptyColorLines[ nTextColor ]
  61. local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]
  62. for y=1,nHeight do
  63. tLines[y] = {
  64. text = sEmptyText,
  65. textColor = sEmptyTextColor,
  66. backgroundColor = sEmptyBackgroundColor,
  67. }
  68. end
  69.  
  70. end
  71.  
  72. -- Helper functions
  73. local function updateCursorPos()
  74. if nCursorX >= 1 and nCursorY >= 1 and
  75. nCursorX <= nWidth and nCursorY <= nHeight then
  76. parent.setCursorPos( nX + nCursorX - 1, nY + nCursorY - 1 )
  77. else
  78. parent.setCursorPos( 0, 0 )
  79. end
  80. end
  81.  
  82. local function updateCursorBlink()
  83. parent.setCursorBlink( bCursorBlink )
  84. end
  85.  
  86. local function updateCursorColor()
  87. parent.setTextColor( nTextColor )
  88. end
  89.  
  90. local function redrawLine( n )
  91. local tLine = tLines[ n ]
  92. parent.setCursorPos( nX, nY + n - 1 )
  93. parent.blit( tLine.text, tLine.textColor, tLine.backgroundColor )
  94. end
  95.  
  96. local function redraw()
  97. for n=1,nHeight do
  98. redrawLine( n )
  99. end
  100. end
  101.  
  102. local function updatePalette()
  103.  
  104. end
  105.  
  106. local function internalBlit( sText, sTextColor, sBackgroundColor )
  107. local nStart = nCursorX
  108. local nEnd = nStart + #sText - 1
  109. if nCursorY >= 1 and nCursorY <= nHeight then
  110. if nStart <= nWidth and nEnd >= 1 then
  111. -- Modify line
  112. local tLine = tLines[ nCursorY ]
  113. if nStart == 1 and nEnd == nWidth then
  114. tLine.text = sText
  115. tLine.textColor = sTextColor
  116. tLine.backgroundColor = sBackgroundColor
  117. else
  118. local sClippedText, sClippedTextColor, sClippedBackgroundColor
  119. if nStart < 1 then
  120. local nClipStart = 1 - nStart + 1
  121. local nClipEnd = nWidth - nStart + 1
  122. sClippedText = string_sub( sText, nClipStart, nClipEnd )
  123. sClippedTextColor = string_sub( sTextColor, nClipStart, nClipEnd )
  124. sClippedBackgroundColor = string_sub( sBackgroundColor, nClipStart, nClipEnd )
  125. elseif nEnd > nWidth then
  126. local nClipEnd = nWidth - nStart + 1
  127. sClippedText = string_sub( sText, 1, nClipEnd )
  128. sClippedTextColor = string_sub( sTextColor, 1, nClipEnd )
  129. sClippedBackgroundColor = string_sub( sBackgroundColor, 1, nClipEnd )
  130. else
  131. sClippedText = sText
  132. sClippedTextColor = sTextColor
  133. sClippedBackgroundColor = sBackgroundColor
  134. end
  135.  
  136. local sOldText = tLine.text
  137. local sOldTextColor = tLine.textColor
  138. local sOldBackgroundColor = tLine.backgroundColor
  139. local sNewText, sNewTextColor, sNewBackgroundColor
  140. if nStart > 1 then
  141. local nOldEnd = nStart - 1
  142. sNewText = string_sub( sOldText, 1, nOldEnd ) .. sClippedText
  143. sNewTextColor = string_sub( sOldTextColor, 1, nOldEnd ) .. sClippedTextColor
  144. sNewBackgroundColor = string_sub( sOldBackgroundColor, 1, nOldEnd ) .. sClippedBackgroundColor
  145. else
  146. sNewText = sClippedText
  147. sNewTextColor = sClippedTextColor
  148. sNewBackgroundColor = sClippedBackgroundColor
  149. end
  150. if nEnd < nWidth then
  151. local nOldStart = nEnd + 1
  152. sNewText = sNewText .. string_sub( sOldText, nOldStart, nWidth )
  153. sNewTextColor = sNewTextColor .. string_sub( sOldTextColor, nOldStart, nWidth )
  154. sNewBackgroundColor = sNewBackgroundColor .. string_sub( sOldBackgroundColor, nOldStart, nWidth )
  155. end
  156.  
  157. tLine.text = sNewText
  158. tLine.textColor = sNewTextColor
  159. tLine.backgroundColor = sNewBackgroundColor
  160. end
  161.  
  162. -- Redraw line
  163. if bVisible then
  164. redrawLine( nCursorY )
  165. end
  166. end
  167. end
  168.  
  169. -- Move and redraw cursor
  170. nCursorX = nEnd + 1
  171. if bVisible then
  172. updateCursorColor()
  173. updateCursorPos()
  174. end
  175. end
  176.  
  177. -- Terminal implementation
  178. local window = {}
  179.  
  180. function window.write( sText )
  181. sText = tostring( sText )
  182. internalBlit( sText, string_rep( tHex[ nTextColor ], #sText ), string_rep( tHex[ nBackgroundColor ], #sText ) )
  183. end
  184.  
  185. function window.blit( sText, sTextColor, sBackgroundColor )
  186. if type( sText ) ~= "string" then error( "bad argument #1 (expected string, got " .. type( sText ) .. ")", 2 ) end
  187. if type( sTextColor ) ~= "string" then error( "bad argument #2 (expected string, got " .. type( sTextColor ) .. ")", 2 ) end
  188. if type( sBackgroundColor ) ~= "string" then error( "bad argument #3 (expected string, got " .. type( sBackgroundColor ) .. ")", 2 ) end
  189. if #sTextColor ~= #sText or #sBackgroundColor ~= #sText then
  190. error( "Arguments must be the same length", 2 )
  191. end
  192. internalBlit( sText, sTextColor, sBackgroundColor )
  193. end
  194.  
  195. function window.clear()
  196. local sEmptyText = sEmptySpaceLine
  197. local sEmptyTextColor = tEmptyColorLines[ nTextColor ]
  198. local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]
  199. for y=1,nHeight do
  200. tLines[y] = {
  201. text = sEmptyText,
  202. textColor = sEmptyTextColor,
  203. backgroundColor = sEmptyBackgroundColor,
  204. }
  205. end
  206. if bVisible then
  207. redraw()
  208. updateCursorColor()
  209. updateCursorPos()
  210. end
  211. end
  212.  
  213. function window.clearLine()
  214. if nCursorY >= 1 and nCursorY <= nHeight then
  215. local sEmptyText = sEmptySpaceLine
  216. local sEmptyTextColor = tEmptyColorLines[ nTextColor ]
  217. local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]
  218. tLines[ nCursorY ] = {
  219. text = sEmptyText,
  220. textColor = sEmptyTextColor,
  221. backgroundColor = sEmptyBackgroundColor,
  222. }
  223. if bVisible then
  224. redrawLine( nCursorY )
  225. updateCursorColor()
  226. updateCursorPos()
  227. end
  228. end
  229. end
  230.  
  231. function window.getCursorPos()
  232. return nCursorX, nCursorY
  233. end
  234.  
  235. function window.setCursorPos( x, y )
  236. if type( x ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( x ) .. ")", 2 ) end
  237. if type( y ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( y ) .. ")", 2 ) end
  238. nCursorX = math.floor( x )
  239. nCursorY = math.floor( y )
  240. if bVisible then
  241. updateCursorPos()
  242. end
  243. end
  244.  
  245. function window.setCursorBlink( blink )
  246. if type( blink ) ~= "boolean" then error( "bad argument #1 (expected boolean, got " .. type( blink ) .. ")", 2 ) end
  247. bCursorBlink = blink
  248. if bVisible then
  249. updateCursorBlink()
  250. end
  251. end
  252.  
  253. function window.getCursorBlink()
  254. return bCursorBlink
  255. end
  256.  
  257. local function isColor()
  258. return parent.isColor()
  259. end
  260.  
  261. function window.isColor()
  262. return isColor()
  263. end
  264.  
  265. function window.isColour()
  266. return isColor()
  267. end
  268.  
  269. local function setTextColor( color )
  270. if type( color ) ~= "number" then
  271. error( "bad argument #1 (expected number, got " .. type( color ) .. ")", 2 )
  272. elseif tHex[color] == nil then
  273. error( "Invalid color (got " .. color .. ")" , 2 )
  274. end
  275. nTextColor = color
  276. if bVisible then
  277. updateCursorColor()
  278. end
  279. end
  280.  
  281. window.setTextColor = setTextColor
  282. window.setTextColour = setTextColor
  283.  
  284. function window.setPaletteColour( colour, r, g, b )
  285.  
  286. end
  287.  
  288. window.setPaletteColor = window.setPaletteColour
  289.  
  290. function window.getPaletteColour( colour )
  291. if type( colour ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( colour ) .. ")", 2 ) end
  292. if tHex[colour] == nil then
  293. error( "Invalid color (got " .. colour .. ")" , 2 )
  294. end
  295. local tCol = tPalette[ colour ]
  296. return tCol[1], tCol[2], tCol[3]
  297. end
  298.  
  299. window.getPaletteColor = window.getPaletteColour
  300.  
  301. local function setBackgroundColor( color )
  302. if type( color ) ~= "number" then
  303. error( "bad argument #1 (expected number, got " .. type( color ) .. ")", 2 )
  304. elseif tHex[color] == nil then
  305. error( "Invalid color (got " .. color .. ")", 2 )
  306. end
  307. nBackgroundColor = color
  308. end
  309.  
  310. window.setBackgroundColor = setBackgroundColor
  311. window.setBackgroundColour = setBackgroundColor
  312.  
  313. function window.getSize()
  314. return nWidth, nHeight
  315. end
  316.  
  317. function window.scroll( n )
  318. if type( n ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( n ) .. ")", 2 ) end
  319. if n ~= 0 then
  320. local tNewLines = {}
  321. local sEmptyText = sEmptySpaceLine
  322. local sEmptyTextColor = tEmptyColorLines[ nTextColor ]
  323. local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]
  324. for newY=1,nHeight do
  325. local y = newY + n
  326. if y >= 1 and y <= nHeight then
  327. tNewLines[newY] = tLines[y]
  328. else
  329. tNewLines[newY] = {
  330. text = sEmptyText,
  331. textColor = sEmptyTextColor,
  332. backgroundColor = sEmptyBackgroundColor,
  333. }
  334. end
  335. end
  336. tLines = tNewLines
  337. if bVisible then
  338. redraw()
  339. updateCursorColor()
  340. updateCursorPos()
  341. end
  342. end
  343. end
  344.  
  345. function window.getTextColor()
  346. return nTextColor
  347. end
  348.  
  349. function window.getTextColour()
  350. return nTextColor
  351. end
  352.  
  353. function window.getBackgroundColor()
  354. return nBackgroundColor
  355. end
  356.  
  357. function window.getBackgroundColour()
  358. return nBackgroundColor
  359. end
  360.  
  361. -- Other functions
  362. function window.setVisible( bVis )
  363. if type( bVis ) ~= "boolean" then error( "bad argument #1 (expected boolean, got " .. type( bVis ) .. ")", 2 ) end
  364. if bVisible ~= bVis then
  365. bVisible = bVis
  366. if bVisible then
  367. window.redraw()
  368. end
  369. end
  370. end
  371.  
  372. function window.redraw()
  373. if bVisible then
  374. redraw()
  375. updateCursorBlink()
  376. updateCursorColor()
  377. updateCursorPos()
  378. end
  379. end
  380.  
  381. function window.restoreCursor()
  382. if bVisible then
  383. updateCursorBlink()
  384. updateCursorColor()
  385. updateCursorPos()
  386. end
  387. end
  388.  
  389. function window.getPosition()
  390. return nX, nY
  391. end
  392.  
  393. function window.reposition( nNewX, nNewY, nNewWidth, nNewHeight )
  394. if type( nNewX ) ~= "number" then error( "bad argument #1 (expected number, got " .. type( nNewX ) .. ")", 2 ) end
  395. if type( nNewY ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( nNewY ) .. ")", 2 ) end
  396. if nNewWidth ~= nil and type( nNewWidth ) ~= "number" then error( "bad argument #3 (expected number, got " .. type( nNewWidth ) .. ")", 2 ) end
  397. if nNewHeight ~= nil and type( nNewHeight ) ~= "number" then error( "bad argument #4 (expected number, got " .. type( nNewHeight ) .. ")", 2 ) end
  398.  
  399. nX = nNewX
  400. nY = nNewY
  401. if nNewWidth and nNewHeight then
  402. local tNewLines = {}
  403. createEmptyLines( nNewWidth )
  404. local sEmptyText = sEmptySpaceLine
  405. local sEmptyTextColor = tEmptyColorLines[ nTextColor ]
  406. local sEmptyBackgroundColor = tEmptyColorLines[ nBackgroundColor ]
  407. for y=1,nNewHeight do
  408. if y > nHeight then
  409. tNewLines[y] = {
  410. text = sEmptyText,
  411. textColor = sEmptyTextColor,
  412. backgroundColor = sEmptyBackgroundColor
  413. }
  414. else
  415. local tOldLine = tLines[y]
  416. if nNewWidth == nWidth then
  417. tNewLines[y] = tOldLine
  418. elseif nNewWidth < nWidth then
  419. tNewLines[y] = {
  420. text = string_sub( tOldLine.text, 1, nNewWidth ),
  421. textColor = string_sub( tOldLine.textColor, 1, nNewWidth ),
  422. backgroundColor = string_sub( tOldLine.backgroundColor, 1, nNewWidth ),
  423. }
  424. else
  425. tNewLines[y] = {
  426. text = tOldLine.text .. string_sub( sEmptyText, nWidth + 1, nNewWidth ),
  427. textColor = tOldLine.textColor .. string_sub( sEmptyTextColor, nWidth + 1, nNewWidth ),
  428. backgroundColor = tOldLine.backgroundColor .. string_sub( sEmptyBackgroundColor, nWidth + 1, nNewWidth ),
  429. }
  430. end
  431. end
  432. end
  433. nWidth = nNewWidth
  434. nHeight = nNewHeight
  435. tLines = tNewLines
  436. end
  437. if bVisible then
  438. window.redraw()
  439. end
  440. end
  441.  
  442. if bVisible then
  443. window.redraw()
  444. end
  445. return window
  446. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement