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