xXm0dzXx

Untitled

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