View difference between Paste ID: XZdipXQ6 and GMcQ19qZ
SHOW: | | - or go back to the newest paste.
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.. "/Frame" ..frame, "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.. "/Frame" ..frame, "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.." (Frame " ..frame..")" )
151
		else
152
			setStatus( "Access denied" )
153
		end
154
	end,
155
	["Next"] = function()
156
		frame = frame +1
157
		load()
158
		redrawText()
159
		setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
160
	end,
161-
		if not frame == 1 then
161+
162
		if frame-1 == 0 then
163
		else
164
			frame = frame-1
165
			load()
166
			redrawText()
167
			setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
168
		end
169
	end,
170
}
171
172
local function doMenuItem( _n )
173
	local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
174
	if fnMenu then
175
		fnMenu()
176
	end
177
178
	bMenu = false
179
	term.setCursorBlink( true )
180
	redrawMenu()
181
end
182
183
while bRunning do
184
  local sEvent, param = os.pullEvent()
185
  if sEvent == "key" then
186
    if param == 200 then
187
		-- Up
188
		if not bMenu then
189
			-- Move cursor up
190
			if y > 1 then
191
				y = y - 1
192
				x = math.min( x, string.len( tLines[y] ) + 1 )
193
				setCursor( x, y )
194
			end
195
		end
196
		
197
	elseif param == 208 then
198
		-- Down
199
		if not bMenu then
200
			-- Move cursor down
201
			if y < #tLines then
202
				y = y + 1
203
				x = math.min( x, string.len( tLines[y] ) + 1 )
204
				setCursor( x, y )
205
			end
206
		end
207
	
208
	elseif param == 203 then
209
		-- Left
210
		if not bMenu then
211
			-- Move cursor left
212
			if x > 1 then
213
				x = x - 1
214
				setCursor( x, y )
215
			end
216
		else
217
			-- Move menu left
218
			nMenuItem = nMenuItem - 1
219
			if nMenuItem < 1 then
220
				nMenuItem = #tMenuItems
221
			end
222
			redrawMenu()
223
		end
224
			
225
		
226
	elseif param == 205 then
227
		-- Right
228
		if not bMenu then
229
			-- Move cursor right
230
   		 	if x < string.len( tLines[y] ) + 1 then
231
				x = x + 1
232
				setCursor( x, y )
233
		  	end
234
		else
235
			-- Move menu right
236
			nMenuItem = nMenuItem + 1
237
		  	if nMenuItem > #tMenuItems then
238
				nMenuItem = 1
239
		  	end
240
		  	redrawMenu()
241
		end
242
		
243
	elseif param == 14 then
244
		-- Backspace
245
		if not bMenu then
246
   		 	if x > 1 then
247
				-- Remove character
248
				local sLine = tLines[y]
249
				tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
250
				redrawLine()
251
		
252
				x = x - 1
253
				setCursor( x, y )
254
			
255
			elseif y > 1 then
256
				-- Remove newline
257
				local sPrevLen = string.len( tLines[y-1] )
258
				tLines[y-1] = tLines[y-1] .. tLines[y]
259
				table.remove( tLines, y )
260
				redrawText()
261
			
262
				x = sPrevLen + 1
263
				y = y - 1
264
				setCursor( x, y )
265
		  	end
266
		end
267
		
268
	elseif param == 28 then
269
		-- Enter
270
		if not bMenu then
271
			-- Newline
272
			local sLine = tLines[y]
273
			tLines[y] = string.sub(sLine,1,x-1)
274
			table.insert( tLines, y+1, string.sub(sLine,x) )
275
			redrawText()
276
		
277
			x = 1
278
			y = y + 1
279
			setCursor( x, y )		
280
			
281
		else
282
			-- Menu selection
283
			doMenuItem( nMenuItem )
284
		end
285
		
286
	elseif param == 29 then
287
		-- Menu toggle
288
		bMenu = not bMenu
289
		if bMenu then
290
			term.setCursorBlink( false )
291
			nMenuItem = 1
292
		else
293
			term.setCursorBlink( true )
294
		end
295
		redrawMenu()
296
	
297
	end
298
		
299
  elseif sEvent == "char" then
300
	if not bMenu then
301
    	-- Input text
302
		local sLine = tLines[y]
303
		tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
304
		redrawLine()
305
	
306
		x = x + string.len( param )
307
		setCursor( x, y )
308
	
309
	else
310
		-- Select menu items
311
		for n,sMenuItem in ipairs( tMenuItems ) do
312
			if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
313
				doMenuItem( n )
314
				break
315
			end
316
		end
317
	end
318
  end
319
end
320
321
term.clear()
322
term.setCursorBlink( false )
323
term.setCursorPos( 1, 1 )