xXm0dzXx

Untitled

Apr 22nd, 2012
206
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. shell.run(sPath.. "/Frame" ..frame)
  175. end,
  176. }
  177.  
  178. local function doMenuItem( _n )
  179. local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
  180. if fnMenu then
  181. fnMenu()
  182. end
  183.  
  184. bMenu = false
  185. term.setCursorBlink( true )
  186. redrawMenu()
  187. end
  188.  
  189. while bRunning do
  190. local sEvent, param = os.pullEvent()
  191. if sEvent == "key" then
  192. if param == 200 then
  193. -- Up
  194. if not bMenu then
  195. -- Move cursor up
  196. if y > 1 then
  197. y = y - 1
  198. x = math.min( x, string.len( tLines[y] ) + 1 )
  199. setCursor( x, y )
  200. end
  201. end
  202.  
  203. elseif param == 208 then
  204. -- Down
  205. if not bMenu then
  206. -- Move cursor down
  207. if y < #tLines then
  208. y = y + 1
  209. x = math.min( x, string.len( tLines[y] ) + 1 )
  210. setCursor( x, y )
  211. end
  212. end
  213.  
  214. elseif param == 203 then
  215. -- Left
  216. if not bMenu then
  217. -- Move cursor left
  218. if x > 1 then
  219. x = x - 1
  220. setCursor( x, y )
  221. end
  222. else
  223. -- Move menu left
  224. nMenuItem = nMenuItem - 1
  225. if nMenuItem < 1 then
  226. nMenuItem = #tMenuItems
  227. end
  228. redrawMenu()
  229. end
  230.  
  231.  
  232. elseif param == 205 then
  233. -- Right
  234. if not bMenu then
  235. -- Move cursor right
  236. if x < string.len( tLines[y] ) + 1 then
  237. x = x + 1
  238. setCursor( x, y )
  239. end
  240. else
  241. -- Move menu right
  242. nMenuItem = nMenuItem + 1
  243. if nMenuItem > #tMenuItems then
  244. nMenuItem = 1
  245. end
  246. redrawMenu()
  247. end
  248.  
  249. elseif param == 14 then
  250. -- Backspace
  251. if not bMenu then
  252. if x > 1 then
  253. -- Remove character
  254. local sLine = tLines[y]
  255. tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  256. redrawLine()
  257.  
  258. x = x - 1
  259. setCursor( x, y )
  260.  
  261. elseif y > 1 then
  262. -- Remove newline
  263. local sPrevLen = string.len( tLines[y-1] )
  264. tLines[y-1] = tLines[y-1] .. tLines[y]
  265. table.remove( tLines, y )
  266. redrawText()
  267.  
  268. x = sPrevLen + 1
  269. y = y - 1
  270. setCursor( x, y )
  271. end
  272. end
  273.  
  274. elseif param == 28 then
  275. -- Enter
  276. if not bMenu then
  277. -- Newline
  278. local sLine = tLines[y]
  279. tLines[y] = string.sub(sLine,1,x-1)
  280. table.insert( tLines, y+1, string.sub(sLine,x) )
  281. redrawText()
  282.  
  283. x = 1
  284. y = y + 1
  285. setCursor( x, y )
  286.  
  287. else
  288. -- Menu selection
  289. doMenuItem( nMenuItem )
  290. end
  291.  
  292. elseif param == 29 then
  293. -- Menu toggle
  294. bMenu = not bMenu
  295. if bMenu then
  296. term.setCursorBlink( false )
  297. nMenuItem = 1
  298. else
  299. term.setCursorBlink( true )
  300. end
  301. redrawMenu()
  302.  
  303. end
  304.  
  305. elseif sEvent == "redstone" then
  306. frame = frame +1
  307. load()
  308. redrawText()
  309. setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
  310. x = 1
  311. y = 1
  312. elseif sEvent == "char" then
  313. if not bMenu then
  314. -- Input text
  315. local sLine = tLines[y]
  316. tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  317. redrawLine()
  318.  
  319. x = x + string.len( param )
  320. setCursor( x, y )
  321.  
  322. else
  323. -- Select menu items
  324. for n,sMenuItem in ipairs( tMenuItems ) do
  325. if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  326. doMenuItem( n )
  327. break
  328. end
  329. end
  330. end
  331. end
  332. end
  333.  
  334. term.clear()
  335. term.setCursorBlink( false )
  336. term.setCursorPos( 1, 1 )
Advertisement
Add Comment
Please, Sign In to add comment