xXm0dzXx

Untitled

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