xXm0dzXx

Untitled

Apr 22nd, 2012
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. end
  75.  
  76. local tMenuItems = { "Save", "Exit", "Next", "Back", "Execute" }
  77.  
  78. local function redrawMenu()
  79. term.setCursorPos( 1, h )
  80. term.clearLine()
  81.  
  82. if not bMenu then
  83. term.write( sStatus )
  84. else
  85. for n,sItem in ipairs( tMenuItems ) do
  86. if n == nMenuItem then
  87. term.write( "["..sItem.."]" )
  88. else
  89. term.write( " "..sItem.." " )
  90. end
  91. end
  92. end
  93.  
  94. term.setCursorPos( x - scrollX, y - scrollY )
  95. end
  96.  
  97. local function setStatus( _sText )
  98. sStatus = _sText
  99. redrawMenu()
  100. end
  101.  
  102. local function setCursor( x, y )
  103. local screenX = x - scrollX
  104. local screenY = y - scrollY
  105.  
  106. local bRedraw = false
  107. if screenX < 1 then
  108. scrollX = x - 1
  109. screenX = 1
  110. bRedraw = true
  111. elseif screenX > w then
  112. scrollX = x - w
  113. screenX = w
  114. bRedraw = true
  115. end
  116.  
  117. if screenY < 1 then
  118. scrollY = y - 1
  119. screenY = 1
  120. bRedraw = true
  121. elseif screenY > h-1 then
  122. scrollY = y - (h-1)
  123. screenY = h-1
  124. bRedraw = true
  125. end
  126.  
  127. if bRedraw then
  128. redrawText()
  129. end
  130. term.setCursorPos( screenX, screenY )
  131. end
  132.  
  133. load()
  134.  
  135. term.clear()
  136. term.setCursorPos(x,y)
  137. term.setCursorBlink( true )
  138.  
  139. redrawText()
  140. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  141.  
  142. local bRunning = true
  143. local tMenuFunctions = {
  144. ["Exit"] = function()
  145. bRunning = false
  146. end,
  147. ["Save"] = function()
  148. if save() then
  149. setStatus( "Saved to "..sPath.." (Frame " ..frame..")" )
  150. else
  151. setStatus( "Access denied" )
  152. end
  153. end,
  154. ["Next"] = function()
  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. ["Back"] = function()
  163. if frame-1 == 0 then
  164. else
  165. frame = frame-1
  166. load()
  167. redrawText()
  168. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  169. x = 1
  170. y = 1
  171. end
  172. end,
  173. ["Execute"] = function()
  174. term.clear()
  175. term.setCursorPos(1,1)
  176. shell.run(sPath.. "/Frame" ..frame)
  177. setStatus( "Testing... Press Ctrl to access menu (Frame " ..frame.. ")" )
  178. end,
  179. }
  180.  
  181. local function doMenuItem( _n )
  182. local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
  183. if fnMenu then
  184. fnMenu()
  185. end
  186.  
  187. bMenu = false
  188. term.setCursorBlink( true )
  189. redrawMenu()
  190. end
  191.  
  192. while bRunning do
  193. local sEvent, param = os.pullEvent()
  194. if sEvent == "key" then
  195. if param == 200 then
  196. -- Up
  197. if not bMenu then
  198. -- Move cursor up
  199. if y > 1 then
  200. y = y - 1
  201. x = math.min( x, string.len( tLines[y] ) + 1 )
  202. setCursor( x, y )
  203. end
  204. end
  205.  
  206. elseif param == 208 then
  207. -- Down
  208. if not bMenu then
  209. -- Move cursor down
  210. if y < #tLines then
  211. y = y + 1
  212. x = math.min( x, string.len( tLines[y] ) + 1 )
  213. setCursor( x, y )
  214. end
  215. end
  216.  
  217. elseif param == 203 then
  218. -- Left
  219. if not bMenu then
  220. -- Move cursor left
  221. if x > 1 then
  222. x = x - 1
  223. setCursor( x, y )
  224. end
  225. else
  226. -- Move menu left
  227. nMenuItem = nMenuItem - 1
  228. if nMenuItem < 1 then
  229. nMenuItem = #tMenuItems
  230. end
  231. redrawMenu()
  232. end
  233.  
  234.  
  235. elseif param == 205 then
  236. -- Right
  237. if not bMenu then
  238. -- Move cursor right
  239. if x < string.len( tLines[y] ) + 1 then
  240. x = x + 1
  241. setCursor( x, y )
  242. end
  243. else
  244. -- Move menu right
  245. nMenuItem = nMenuItem + 1
  246. if nMenuItem > #tMenuItems then
  247. nMenuItem = 1
  248. end
  249. redrawMenu()
  250. end
  251.  
  252. elseif param == 14 then
  253. -- Backspace
  254. if not bMenu then
  255. if x > 1 then
  256. -- Remove character
  257. local sLine = tLines[y]
  258. tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  259. redrawLine()
  260.  
  261. x = x - 1
  262. setCursor( x, y )
  263.  
  264. elseif y > 1 then
  265. -- Remove newline
  266. local sPrevLen = string.len( tLines[y-1] )
  267. tLines[y-1] = tLines[y-1] .. tLines[y]
  268. table.remove( tLines, y )
  269. redrawText()
  270.  
  271. x = sPrevLen + 1
  272. y = y - 1
  273. setCursor( x, y )
  274. end
  275. end
  276.  
  277. elseif param == 28 then
  278. -- Enter
  279. if not bMenu then
  280. -- Newline
  281. local sLine = tLines[y]
  282. tLines[y] = string.sub(sLine,1,x-1)
  283. table.insert( tLines, y+1, string.sub(sLine,x) )
  284. redrawText()
  285.  
  286. x = 1
  287. y = y + 1
  288. setCursor( x, y )
  289.  
  290. else
  291. -- Menu selection
  292. doMenuItem( nMenuItem )
  293. end
  294.  
  295. elseif param == 29 then
  296. -- Menu toggle
  297. bMenu = not bMenu
  298. if bMenu then
  299. term.setCursorBlink( false )
  300. nMenuItem = 1
  301. else
  302. term.setCursorBlink( true )
  303. end
  304. redrawMenu()
  305.  
  306. end
  307.  
  308. elseif sEvent == "redstone" then
  309. frame = frame +1
  310. load()
  311. redrawText()
  312. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  313. x = 1
  314. y = 1
  315. sleep(1.2)
  316. elseif sEvent == "char" then
  317. if not bMenu then
  318. -- Input text
  319. local sLine = tLines[y]
  320. tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  321. redrawLine()
  322.  
  323. x = x + string.len( param )
  324. setCursor( x, y )
  325.  
  326. else
  327. -- Select menu items
  328. for n,sMenuItem in ipairs( tMenuItems ) do
  329. if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  330. doMenuItem( n )
  331. break
  332. end
  333. end
  334. end
  335. end
  336. end
  337.  
  338. term.clear()
  339. term.setCursorBlink( false )
  340. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment