Guest User

NeXuS Powerpoint

a guest
Apr 22nd, 2012
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. local frame = 1;
  2.  
  3. local tArgs = { ... }
  4. if #tArgs == 0 then
  5. print( "Usage: powerpoint <folder path>" )
  6. return
  7. end
  8.  
  9. local sPath = shell.resolve( tArgs[1] )
  10. local bReadOnly = fs.isReadOnly( sPath )
  11. if fs.exists( sPath ) and fs.isDir( sPath ) == false then
  12. print( "Cannot edit a frame" )
  13. return
  14. elseif fs.exists( sPath ) == false then
  15. fs.makeDir( sPath )
  16. end
  17.  
  18. local w,h = term.getSize()
  19. local x,y = 1,1
  20. local scrollX, scrollY = 0,0
  21.  
  22. local bMenu = false
  23. local nMenuItem = 1
  24.  
  25. local sStatus = ""
  26. local tLines = {}
  27.  
  28. local function load()
  29. tLines = {}
  30. if fs.exists( sPath.. "/Frame" ..frame ) then
  31. local file = io.open( sPath.. "/Frame" ..frame, "r" )
  32. local sLine = file:read()
  33. while sLine do
  34. table.insert( tLines, sLine )
  35. sLine = file:read()
  36. end
  37. file:close()
  38. else
  39. table.insert( tLines, "" )
  40. end
  41. end
  42.  
  43. local function save()
  44. local file = io.open( sPath.. "/Frame" ..frame, "w" )
  45. if file then
  46. for n, sLine in ipairs( tLines ) do
  47. file:write( sLine .. "\n" )
  48. end
  49. file:close()
  50. return true
  51. end
  52. return false
  53. end
  54.  
  55. local function redrawText()
  56. for y=1,h-1 do
  57. term.setCursorPos( 1 - scrollX, y )
  58. term.clearLine()
  59.  
  60. local sLine = tLines[ y + scrollY ]
  61. if sLine ~= nil then
  62. term.write( sLine )
  63. end
  64. end
  65. term.setCursorPos( x - scrollX, y - scrollY )
  66. end
  67.  
  68. local function redrawLine()
  69. local sLine = tLines[y]
  70. term.setCursorPos( 1 - scrollX, y - scrollY )
  71. term.clearLine()
  72. term.write( sLine )
  73. term.setCursorPos( x - scrollX, y - scrollY )
  74.  
  75. local tMenuItems = { "Save", "Exit", "Next", "Back" }
  76.  
  77. local function redrawMenu()
  78. term.setCursorPos( 1, h )
  79. term.clearLine()
  80.  
  81. if not bMenu then
  82. term.write( sStatus )
  83. for n,sItem in ipairs( tMenuItems ) do
  84. if n == nMenuItem then
  85. term.write( "["..sItem.."]" )
  86. else
  87. term.write( " "..sItem.." " )
  88. end
  89. end
  90. end
  91.  
  92. term.setCursorPos( x - scrollX, y - scrollY )
  93. end
  94.  
  95. local function setStatus( _sText )
  96. sStatus = _sText
  97. redrawMenu()
  98. end
  99.  
  100. local function setCursor( x, y )
  101. local screenX = x - scrollX
  102. local screenY = y - scrollY
  103.  
  104. local bRedraw = false
  105. if screenX < 1 then
  106. scrollX = x - 1
  107. screenX = 1
  108. bRedraw = true
  109. elseif screenX > w then
  110. scrollX = x - w
  111. screenX = w
  112. bRedraw = true
  113.  
  114. if screenY < 1 then
  115. scrollY = y - 1
  116. screenY = 1
  117. bRedraw = true
  118. elseif screenY > h-1 then
  119. scrollY = y - (h-1)
  120. screenY = h-1
  121. bRedraw = true
  122. end
  123.  
  124. if bRedraw then
  125. redrawText()
  126. end
  127. term.setCursorPos( screenX, screenY )
  128. end
  129.  
  130. load()
  131.  
  132. term.clear()
  133. term.setCursorPos(x,y)
  134. term.setCursorBlink( true )
  135.  
  136. redrawText()
  137. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  138.  
  139. local bRunning = true
  140. local tMenuFunctions = {
  141. ["Exit"] = function()
  142. bRunning = false
  143. end,
  144. ["Save"] = function()
  145. if save() then
  146. setStatus( "Saved to "..sPath.." (Frame " ..frame..")" )
  147. else
  148. setStatus( "Access denied" )
  149. end
  150. end,
  151. ["Next"] = function()
  152. if frame+1 == 6 then
  153. error("Please purchase for more details and remove this frame limit!")
  154. else
  155. frame = frame +1
  156. load()
  157. redrawText()
  158. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  159. x = 1
  160. y = 1
  161. end
  162. end,
  163. ["Back"] = function()
  164. if frame-1 == 0 then
  165. else
  166. frame = frame-1
  167. load()
  168. redrawText()
  169. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  170. x = 1
  171. y = 1
  172. end
  173. end,
  174. }
  175.  
  176. local function doMenuItem( _n )
  177. local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
  178. if fnMenu then
  179. fnMenu()
  180. end
  181.  
  182. bMenu = false
  183. term.setCursorBlink( true )
  184. redrawMenu()
  185. end
  186.  
  187. while bRunning do
  188. local sEvent, param = os.pullEvent()
  189. if sEvent == "key" then
  190. if param == 200 then
  191. -- Up
  192. if not bMenu then
  193. -- Move cursor up
  194. if y > 1 then
  195. y = y - 1
  196. x = math.min( x, string.len( tLines[y] ) + 1 )
  197. setCursor( x, y )
  198. end
  199. end
  200.  
  201. elseif param == 208 then
  202. -- Down
  203. if not bMenu then
  204. -- Move cursor down
  205. if y < #tLines then
  206. y = y + 1
  207. x = math.min( x, string.len( tLines[y] ) + 1 )
  208. setCursor( x, y )
  209. end
  210. end
  211.  
  212. elseif param == 203 then
  213. -- Left
  214. if not bMenu then
  215. -- Move cursor left
  216. if x > 1 then
  217. x = x - 1
  218. setCursor( x, y )
  219. end
  220. else
  221. -- Move menu left
  222. nMenuItem = nMenuItem - 1
  223. if nMenuItem < 1 then
  224. nMenuItem = #tMenuItems
  225. end
  226. redrawMenu()
  227. end
  228.  
  229.  
  230. elseif param == 205 then
  231. -- Right
  232. if not bMenu then
  233. -- Move cursor right
  234. if x < string.len( tLines[y] ) + 1 then
  235. x = x + 1
  236. setCursor( x, y )
  237. end
  238. else
  239. -- Move menu right
  240. nMenuItem = nMenuItem + 1
  241. if nMenuItem > #tMenuItems then
  242. nMenuItem = 1
  243. end
  244. redrawMenu()
  245. end
  246.  
  247. elseif param == 14 then
  248. -- Backspace
  249. if not bMenu then
  250. if x > 1 then
  251. -- Remove character
  252. local sLine = tLines[y]
  253. tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  254. redrawLine()
  255.  
  256. x = x - 1
  257. setCursor( x, y )
  258.  
  259. elseif y > 1 then
  260. -- Remove newline
  261. local sPrevLen = string.len( tLines[y-1] )
  262. tLines[y-1] = tLines[y-1] .. tLines[y]
  263. table.remove( tLines, y )
  264. redrawText()
  265.  
  266. x = sPrevLen + 1
  267. y = y - 1
  268. setCursor( x, y )
  269. end
  270. end
  271.  
  272. elseif param == 28 then
  273. -- Enter
  274. if not bMenu then
  275. -- Newline
  276. local sLine = tLines[y]
  277. tLines[y] = string.sub(sLine,1,x-1)
  278. table.insert( tLines, y+1, string.sub(sLine,x) )
  279. redrawText()
  280.  
  281. x = 1
  282. y = y + 1
  283. setCursor( x, y )
  284.  
  285. else
  286. -- Menu selection
  287. doMenuItem( nMenuItem )
  288. end
  289.  
  290. elseif param == 29 then
  291. -- Menu toggle
  292. bMenu = not bMenu
  293. if bMenu then
  294. term.setCursorBlink( false )
  295. nMenuItem = 1
  296. else
  297. term.setCursorBlink( true )
  298. end
  299. redrawMenu()
  300.  
  301. end
  302.  
  303. elseif sEvent == "redstone" then
  304. frame = frame +1
  305. load()
  306. redrawText()
  307. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  308. x = 1
  309. y = 1
  310. sleep(1.2)
  311. elseif sEvent == "char" then
  312. if not bMenu then
  313. -- Input text
  314. local sLine = tLines[y]
  315. tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  316. redrawLine()
  317.  
  318. x = x + string.len( param )
  319. setCursor( x, y )
  320.  
  321. else
  322. -- Select menu items
  323. for n,sMenuItem in ipairs( tMenuItems ) do
  324. if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  325. doMenuItem( n )
  326. break
  327. end
  328. end
  329. end
  330. end
  331. end
  332.  
  333. term.clear()
  334. term.setCursorBlink( false )
  335. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment