-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.lua
More file actions
407 lines (368 loc) · 10.8 KB
/
paint.lua
File metadata and controls
407 lines (368 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
-- Paint created by nitrogenfingers (edited by dan200)
-- http://www.youtube.com/user/NitrogenFingers
------------
-- Fields --
------------
-- The width and height of the terminal
local w,h = term.getSize()
-- The selected colours on the left and right mouse button, and the colour of the canvas
local leftColour, rightColour = colours.white, nil
local canvasColour = colours.black
-- The values stored in the canvas
local canvas = {}
-- The menu options
local mChoices = { "Save","Exit" }
-- The message displayed in the footer bar
local fMessage = "Press Ctrl to access menu"
-------------------------
-- Initialisation --
-------------------------
-- Determine if we can even run this
if not term.isColour() then
print("Requires an Advanced Computer")
return
end
-- Determines if the file exists, and can be edited on this computer
local tArgs = {...}
if #tArgs == 0 then
print("Usage: paint <path>")
return
end
local sPath = shell.resolve(tArgs[1])
local bReadOnly = fs.isReadOnly(sPath)
if fs.exists(sPath) and fs.isDir(sPath) then
print("Cannot edit a directory.")
return
end
-- Create .nfp files by default
if not fs.exists( sPath ) and not string.find( sPath, "%." ) then
local sExtension = settings.get("paint.default_extension", "" )
if sExtension ~= "" and type( sExtension ) == "string" then
sPath = sPath .. "." .. sExtension
end
end
---------------
-- Functions --
---------------
local function getCanvasPixel( x, y )
if canvas[y] then
return canvas[y][x]
end
return nil
end
--[[
Converts a colour value to a text character
params: colour = the number to convert to a hex value
returns: a string representing the chosen colour
]]
local function getCharOf( colour )
-- Incorrect values always convert to nil
if type(colour) == "number" then
local value = math.floor( math.log(colour) / math.log(2) ) + 1
if value >= 1 and value <= 16 then
return string.sub( "0123456789abcdef", value, value )
end
end
return " "
end
--[[
Converts a text character to colour value
params: char = the char (from string.byte) to convert to number
returns: the colour number of the hex value
]]
local tColourLookup = {}
for n=1,16 do
tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
end
local function getColourOf( char )
-- Values not in the hex table are transparent (canvas coloured)
return tColourLookup[char]
end
--[[
Loads the file into the canvas
params: path = the path of the file to open
returns: nil
]]
local function load(path)
-- Load the file
if fs.exists(path) then
local file = fs.open(sPath, "r")
local sLine = file.readLine()
while sLine do
local line = {}
for x=1,w-2 do
line[x] = getColourOf( string.byte(sLine,x,x) )
end
table.insert( canvas, line )
sLine = file.readLine()
end
file.close()
end
end
--[[
Saves the current canvas to file
params: path = the path of the file to save
returns: true if save was successful, false otherwise
]]
local function save(path)
-- Open file
local sDir = string.sub(sPath, 1, #sPath - #fs.getName(sPath))
if not fs.exists(sDir) then
fs.makeDir(sDir)
end
local file, err = fs.open( path, "w" )
if not file then
return false, err
end
-- Encode (and trim)
local tLines = {}
local nLastLine = 0
for y=1,h-1 do
local sLine = ""
local nLastChar = 0
for x=1,w-2 do
local c = getCharOf( getCanvasPixel( x, y ) )
sLine = sLine .. c
if c ~= " " then
nLastChar = x
end
end
sLine = string.sub( sLine, 1, nLastChar )
tLines[y] = sLine
if string.len( sLine ) > 0 then
nLastLine = y
end
end
-- Save out
for n=1,nLastLine do
file.writeLine( tLines[ n ] )
end
file.close()
return true
end
--[[
Draws colour picker sidebar, the pallette and the footer
returns: nil
]]
local function drawInterface()
-- Footer
term.setCursorPos(1, h)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.yellow)
term.clearLine()
term.write(fMessage)
-- Colour Picker
for i=1,16 do
term.setCursorPos(w-1, i)
term.setBackgroundColour( 2^(i-1) )
term.write(" ")
end
term.setCursorPos(w-1, 17)
term.setBackgroundColour( canvasColour )
term.setTextColour( colours.grey )
term.write("\127\127")
-- Left and Right Selected Colours
for i=18,18 do
term.setCursorPos(w-1, i)
if leftColour ~= nil then
term.setBackgroundColour( leftColour )
term.write(" ")
else
term.setBackgroundColour( canvasColour )
term.setTextColour( colours.grey )
term.write("\127")
end
if rightColour ~= nil then
term.setBackgroundColour( rightColour )
term.write(" ")
else
term.setBackgroundColour( canvasColour )
term.setTextColour( colours.grey )
term.write("\127")
end
end
-- Padding
term.setBackgroundColour( canvasColour )
for i=20,h-1 do
term.setCursorPos(w-1, i)
term.write(" ")
end
end
--[[
Converts a single pixel of a single line of the canvas and draws it
returns: nil
]]
local function drawCanvasPixel( x, y )
local pixel = getCanvasPixel( x, y )
if pixel then
term.setBackgroundColour( pixel or canvasColour )
term.setCursorPos(x, y)
term.write(" ")
else
term.setBackgroundColour( canvasColour )
term.setTextColour( colours.grey )
term.setCursorPos(x, y)
term.write("\127")
end
end
--[[
Converts each colour in a single line of the canvas and draws it
returns: nil
]]
local function drawCanvasLine( y )
for x = 1, w-2 do
drawCanvasPixel( x, y )
end
end
--[[
Converts each colour in the canvas and draws it
returns: nil
]]
local function drawCanvas()
for y = 1, h-1 do
drawCanvasLine( y )
end
end
--[[
Draws menu options and handles input from within the menu.
returns: true if the program is to be exited; false otherwise
]]
local function accessMenu()
-- Selected menu option
local selection = 1
term.setBackgroundColour(colours.black)
while true do
-- Draw the menu
term.setCursorPos(1,h)
term.clearLine()
term.setTextColour(colours.white)
for k,v in pairs(mChoices) do
if selection==k then
term.setTextColour(colours.yellow)
local ox,_ = term.getCursorPos()
term.write("["..string.rep(" ",#v).."]")
term.setCursorPos(ox+1,h)
term.setTextColour(colours.white)
term.write(v)
term.setCursorPos(term.getCursorPos()+1,h)
else
term.write(" "..v.." ")
end
end
-- Handle input in the menu
local id,key = os.pullEvent("key")
if id == "key" then
-- S and E are shortcuts
if key == keys.s then
selection = 1
key = keys.enter
elseif key == keys.e then
selection = 2
key = keys.enter
end
if key == keys.right then
-- Move right
selection = selection + 1
if selection > #mChoices then
selection = 1
end
elseif key == keys.left and selection > 1 then
-- Move left
selection = selection - 1
if selection < 1 then
selection = #mChoices
end
elseif key == keys.enter then
-- Select an option
if mChoices[selection]=="Save" then
if bReadOnly then
fMessage = "Access denied"
return false
end
local success, err = save(sPath)
if success then
fMessage = "Saved to "..sPath
else
if err then
fMessage = "Error saving to "..err
else
fMessage = "Error saving to "..sPath
end
end
return false
elseif mChoices[selection]=="Exit" then
return true
end
elseif key == keys.leftCtrl or keys == keys.rightCtrl then
-- Cancel the menu
return false
end
end
end
end
--[[
Runs the main thread of execution. Draws the canvas and interface, and handles
mouse and key events.
returns: nil
]]
local function handleEvents()
local programActive = true
while programActive do
local id,p1,p2,p3 = os.pullEvent()
if id=="mouse_click" or id=="mouse_drag" then
if p2 >= w-1 and p3 >= 1 and p3 <= 17 then
if id ~= "mouse_drag" then
-- Selecting an items in the colour picker
if p3 <= 16 then
if p1==1 then
leftColour = 2^(p3-1)
else
rightColour = 2^(p3-1)
end
else
if p1==1 then
leftColour = nil
else
rightColour = nil
end
end
--drawCanvas()
drawInterface()
end
elseif p2 < w-1 and p3 <= h-1 then
-- Clicking on the canvas
local paintColour = nil
if p1==1 then
paintColour = leftColour
elseif p1==2 then
paintColour = rightColour
end
if not canvas[p3] then
canvas[p3] = {}
end
canvas[p3][p2] = paintColour
drawCanvasPixel( p2, p3 )
end
elseif id=="key" then
if p1==keys.leftCtrl or p1==keys.rightCtrl then
programActive = not accessMenu()
drawInterface()
end
elseif id=="term_resize" then
w,h = term.getSize()
drawCanvas()
drawInterface()
end
end
end
-- Init
load(sPath)
drawCanvas()
drawInterface()
-- Main loop
handleEvents()
-- Shutdown
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.clear()
term.setCursorPos(1,1)