Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.88 KB | None | 0 0
  1.  
  2. --[[ Modded Window API, for univ. ]]--
  3. --[[ Originaly by dan200 ]]--
  4.  
  5. function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
  6.  
  7. if type( parent ) ~= "table" or
  8. type( nX ) ~= "number" or
  9. type( nY ) ~= "number" or
  10. type( nWidth ) ~= "number" or
  11. type( nHeight ) ~= "number" or
  12. (bStartVisible ~= nil and type( bStartVisible ) ~= "boolean") then
  13. error( "Expected object, number, number, number, number, [boolean]", 2 )
  14. end
  15.  
  16. if parent == term then
  17. error( "term is not a recommended window parent, try term.current() instead", 2 )
  18. end
  19.  
  20. -- Setup
  21. local bVisible = (bStartVisible ~= false)
  22. local nCursorX = 1
  23. local nCursorY = 1
  24. local bCursorBlink = false
  25. local nTextColor = colors.white
  26. local nBackgroundColor = colors.black
  27. local sEmpty = string.rep( " ", nWidth )
  28. local tLines = {}
  29. do
  30. local tEmpty = { { sEmpty, nTextColor, nBackgroundColor } }
  31. for y=1,nHeight do
  32. tLines[y] = tEmpty
  33. end
  34. end
  35.  
  36. -- Helper functions
  37. local function updateCursorPos()
  38. if nCursorX >= 1 and nCursorY >= 1 and
  39. nCursorX <= nWidth and nCursorY <= nHeight then
  40. parent.setCursorPos( nX + nCursorX - 1, nY + nCursorY - 1 )
  41. else
  42. parent.setCursorPos( 0, 0 )
  43. end
  44. end
  45.  
  46. local function updateCursorBlink()
  47. parent.setCursorBlink( bCursorBlink )
  48. end
  49.  
  50. local function updateCursorColor()
  51. parent.setTextColor( nTextColor )
  52. end
  53.  
  54. local function redrawLine( n )
  55. parent.setCursorPos( nX, nY + n - 1 )
  56. local tLine = tLines[ n ]
  57. for m=1,#tLine do
  58. local tBit = tLine[ m ]
  59. parent.setTextColor( tBit[2] )
  60. parent.setBackgroundColor( tBit[3] )
  61. parent.write( tBit[1] )
  62. end
  63. end
  64.  
  65. local function lineLen( tLine )
  66. local nLength = 0
  67. for n=1,#tLine do
  68. nLength = nLength + string.len( tLine[n][1] )
  69. end
  70. return nLength
  71. end
  72.  
  73. local function lineSub( tLine, nStart, nEnd )
  74. --assert( math.floor(nStart) == nStart )
  75. --assert( math.floor(nEnd) == nEnd )
  76. --assert( nStart >= 1 )
  77. --assert( nEnd >= nStart )
  78. --assert( nEnd <= lineLen( tLine ) )
  79. local tSubLine = {}
  80. local nBitStart = 1
  81. for n=1,#tLine do
  82. local tBit = tLine[n]
  83. local sBit = tBit[1]
  84. local nBitEnd = nBitStart + string.len( sBit ) - 1
  85. if nBitEnd >= nStart and nBitStart <= nEnd then
  86. if nBitStart >= nStart and nBitEnd <= nEnd then
  87. -- Include bit wholesale
  88. table.insert( tSubLine, tBit )
  89. --assert( lineLen( tSubLine ) == (math.min(nEnd, nBitEnd) - nStart + 1) )
  90. elseif nBitStart < nStart and nBitEnd <= nEnd then
  91. -- Include end of bit
  92. table.insert( tSubLine, {
  93. string.sub( sBit, nStart - nBitStart + 1 ),
  94. tBit[2], tBit[3]
  95. } )
  96. --assert( lineLen( tSubLine ) == (math.min(nEnd, nBitEnd) - nStart + 1) )
  97. elseif nBitStart >= nStart and nBitEnd > nEnd then
  98. -- Include beginning of bit
  99. table.insert( tSubLine, {
  100. string.sub( sBit, 1, nEnd - nBitStart + 1 ),
  101. tBit[2], tBit[3]
  102. } )
  103. --assert( lineLen( tSubLine ) == (math.min(nEnd, nBitEnd) - nStart + 1) )
  104. else
  105. -- Include middle of bit
  106. table.insert( tSubLine, {
  107. string.sub( sBit, nStart - nBitStart + 1, nEnd - nBitStart + 1 ),
  108. tBit[2], tBit[3]
  109. } )
  110. --assert( lineLen( tSubLine ) == (math.min(nEnd, nBitEnd) - nStart + 1) )
  111. end
  112. end
  113. nBitStart = nBitEnd + 1
  114. end
  115. --assert( lineLen( tSubLine ) == (nEnd - nStart + 1) )
  116. return tSubLine
  117. end
  118.  
  119. local function lineJoin( tLine1, tLine2 )
  120. local tNewLine = {}
  121. if tLine1[#tLine1][2] == tLine2[1][2] and
  122. tLine1[#tLine1][3] == tLine2[1][3] then
  123. -- Merge middle bits
  124. for n=1,#tLine1-1 do
  125. table.insert( tNewLine, tLine1[n] )
  126. end
  127. table.insert( tNewLine, {
  128. tLine1[#tLine1][1] .. tLine2[1][1],
  129. tLine2[1][2], tLine2[1][3]
  130. } )
  131. for n=2,#tLine2 do
  132. table.insert( tNewLine, tLine2[n] )
  133. end
  134. --assert( lineLen( tNewLine ) == lineLen(tLine1) + lineLen(tLine2) )
  135. else
  136. -- Just concatenate
  137. for n=1,#tLine1 do
  138. table.insert( tNewLine, tLine1[n] )
  139. end
  140. for n=1,#tLine2 do
  141. table.insert( tNewLine, tLine2[n] )
  142. end
  143. --assert( lineLen( tNewLine ) == lineLen(tLine1) + lineLen(tLine2) )
  144. end
  145. return tNewLine
  146. end
  147.  
  148. local function redraw()
  149. for n=1,nHeight do
  150. redrawLine( n )
  151. end
  152. end
  153.  
  154. local window = {}
  155.  
  156. -- Terminal implementation
  157. function window.write( sText )
  158. local nLen = string.len( sText )
  159. local nStart = nCursorX
  160. local nEnd = nStart + nLen - 1
  161. if nCursorY >= 1 and nCursorY <= nHeight then
  162. -- Work out where to put new line
  163. --assert( math.floor(nStart) == nStart )
  164. --assert( math.floor(nEnd) == nEnd )
  165. if nStart <= nWidth and nEnd >= 1 then
  166. -- Construct new line
  167. local tLine = tLines[ nCursorY ]
  168. if nStart == 1 and nEnd == nWidth then
  169. -- Exactly replace line
  170. tLine = {
  171. { sText, nTextColor, nBackgroundColor }
  172. }
  173. --assert( lineLen( tLine ) == nWidth )
  174. elseif nStart <= 1 and nEnd >= nWidth then
  175. -- Overwrite line with subset
  176. tLine = {
  177. { string.sub( sText, 1 - nStart + 1, nWidth - nStart + 1 ), nTextColor, nBackgroundColor }
  178. }
  179. --assert( lineLen( tLine ) == nWidth )
  180. elseif nStart <= 1 then
  181. -- Overwrite beginning of line
  182. tLine = lineJoin(
  183. { { string.sub( sText, 1 - nStart + 1 ), nTextColor, nBackgroundColor } },
  184. lineSub( tLine, nEnd + 1, nWidth )
  185. )
  186. --assert( lineLen( tLine ) == nWidth )
  187. elseif nEnd >= nWidth then
  188. -- Overwrite end of line
  189. tLine = lineJoin(
  190. lineSub( tLine, 1, nStart - 1 ),
  191. { { string.sub( sText, 1, nWidth - nStart + 1 ), nTextColor, nBackgroundColor } }
  192. )
  193. --assert( lineLen( tLine ) == nWidth )
  194. else
  195. -- Overwrite middle of line
  196. tLine = lineJoin(
  197. lineJoin(
  198. lineSub( tLine, 1, nStart - 1 ),
  199. { { sText, nTextColor, nBackgroundColor } }
  200. ),
  201. lineSub( tLine, nEnd + 1, nWidth )
  202. )
  203. --assert( lineLen( tLine ) == nWidth )
  204. end
  205.  
  206. -- Store and redraw new line
  207. tLines[ nCursorY ] = tLine
  208. if bVisible then
  209. redrawLine( nCursorY )
  210. end
  211. end
  212. end
  213.  
  214. -- Move and redraw cursor
  215. nCursorX = nEnd + 1
  216. if bVisible then
  217. updateCursorColor()
  218. updateCursorPos()
  219. end
  220. end
  221.  
  222. function window.clear()
  223. local tEmpty = { { sEmpty, nTextColor, nBackgroundColor } }
  224. for y=1,nHeight do
  225. tLines[y] = tEmpty
  226. end
  227. if bVisible then
  228. redraw()
  229. updateCursorColor()
  230. updateCursorPos()
  231. end
  232. end
  233.  
  234. function window.clearLine()
  235. if nCursorY >= 1 and nCursorY <= nHeight then
  236. tLines[ nCursorY ] = { { sEmpty, nTextColor, nBackgroundColor } }
  237. if bVisible then
  238. redrawLine( nCursorY )
  239. updateCursorColor()
  240. updateCursorPos()
  241. end
  242. end
  243. end
  244.  
  245. function window.getCursorPos()
  246. return nCursorX, nCursorY
  247. end
  248.  
  249. function window.setCursorPos( x, y )
  250. nCursorX = math.floor( x )
  251. nCursorY = math.floor( y )
  252. if bVisible then
  253. updateCursorPos()
  254. end
  255. end
  256.  
  257. function window.setCursorBlink( blink )
  258. bCursorBlink = blink
  259. if bVisible then
  260. updateCursorBlink()
  261. end
  262. end
  263.  
  264. function window.isColor()
  265. return parent.isColor()
  266. end
  267.  
  268. function window.isColour()
  269. return parent.isColor()
  270. end
  271.  
  272. local function setTextColor( color )
  273. if not parent.isColor() then
  274. if color ~= colors.white and color ~= colors.black then
  275. error( "Colour not supported", 3 )
  276. end
  277. end
  278. nTextColor = color
  279. if bVisible then
  280. updateCursorColor()
  281. end
  282. end
  283.  
  284. function window.setTextColor( color )
  285. setTextColor( color )
  286. end
  287.  
  288. function window.setTextColour( color )
  289. setTextColor( color )
  290. end
  291.  
  292. local function setBackgroundColor( color )
  293. if not parent.isColor() then
  294. if color ~= colors.white and color ~= colors.black then
  295. error( "Colour not supported", 3 )
  296. end
  297. end
  298. nBackgroundColor = color
  299. end
  300.  
  301. function window.setBackgroundColor( color )
  302. setBackgroundColor( color )
  303. end
  304.  
  305. function window.setBackgroundColour( color )
  306. setBackgroundColor( color )
  307. end
  308.  
  309. function window.getSize()
  310. return nWidth, nHeight
  311. end
  312.  
  313. function window.scroll( n )
  314. if n ~= 0 then
  315. local tNewLines = {}
  316. local tEmpty = { { sEmpty, nTextColor, nBackgroundColor } }
  317. for newY=1,nHeight do
  318. local y = newY + n
  319. if y >= 1 and y <= nHeight then
  320. tNewLines[newY] = tLines[y]
  321. else
  322. tNewLines[newY] = tEmpty
  323. end
  324. end
  325. tLines = tNewLines
  326. if bVisible then
  327. redraw()
  328. updateCursorColor()
  329. updateCursorPos()
  330. end
  331. end
  332. end
  333.  
  334. -- Other functions
  335. function window.setVisible( bVis )
  336. if bVisible ~= bVis then
  337. bVisible = bVis
  338. if bVisible then
  339. window.redraw()
  340. end
  341. end
  342. end
  343.  
  344. function window.redraw()
  345. if bVisible then
  346. redraw()
  347. updateCursorBlink()
  348. updateCursorColor()
  349. updateCursorPos()
  350. end
  351. end
  352.  
  353. function window.restoreCursor()
  354. if bVisible then
  355. updateCursorBlink()
  356. updateCursorColor()
  357. updateCursorPos()
  358. end
  359. end
  360.  
  361. function window.getPosition()
  362. return nX, nY
  363. end
  364.  
  365. function window.reposition( nNewX, nNewY, nNewWidth, nNewHeight )
  366. nX = nNewX
  367. nY = nNewY
  368. if nNewWidth and nNewHeight then
  369. sEmpty = string.rep( " ", nNewWidth )
  370. local tNewLines = {}
  371. local tEmpty = { { sEmpty, nTextColor, nBackgroundColor } }
  372. for y=1,nNewHeight do
  373. if y > nHeight then
  374. tNewLines[y] = tEmpty
  375. else
  376. if nNewWidth == nWidth then
  377. tNewLines[y] = tLines[y]
  378. elseif nNewWidth < nWidth then
  379. tNewLines[y] = lineSub( tLines[y], 1, nNewWidth )
  380. else
  381. tNewLines[y] = lineJoin( tLines[y], { { string.sub( sEmpty, nWidth + 1, nNewWidth ), nTextColor, nBackgroundColor } } )
  382. end
  383. end
  384. end
  385. nWidth = nNewWidth
  386. nHeight = nNewHeight
  387. tLines = tNewLines
  388. end
  389. if bVisible then
  390. window.redraw()
  391. end
  392. end
  393.  
  394. function window.getBuffer()
  395. return tLines
  396. end
  397.  
  398. function window.uploadBuffer(newBuffer)
  399. tLines = newBuffer
  400. end
  401.  
  402. if bVisible then
  403. window.redraw()
  404. end
  405. return window
  406. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement