-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstroke_io.lua
More file actions
92 lines (82 loc) · 3.2 KB
/
stroke_io.lua
File metadata and controls
92 lines (82 loc) · 3.2 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
local sep = package.config:sub(1, 1) -- path separator depends on OS
local sourcePath = debug.getinfo(1).source:match("@?(.*" .. sep .. ")")
local _M = {} -- functions to export
-- Function to read and provide formatted stroke data from a shape-file
function _M.read_strokes_from_file(filepath)
if filepath == nil then return end
local hasFile, content = pcall(dofile, filepath)
if not hasFile then print("Error: " .. content) return end
local strokesToAdd = {}
for _, stroke in ipairs(content) do
if type(stroke) == "table" and stroke.x and stroke.y then
local newStroke = {
x = stroke.x,
y = stroke.y,
pressure = stroke.pressure,
tool = stroke.tool or "pen",
color = stroke.color or 0,
width = stroke.width or 1,
fill = stroke.fill or 0,
lineStyle = stroke.lineStyle or "plain"
}
table.insert(strokesToAdd, newStroke)
end
end
return strokesToAdd -- formatted strokes data for adding
end
-- writes a shape_stroke file with the shape name provided by the user
function _M.store_stroke_info_in_file(shapeName, strokes)
--app.openDialog(shapeName,{"OK"})
-- Open a file for writing in the folder path
local file = assert(io.open(sourcePath .. "Shapes" .. sep .. shapeName .. ".lua", "w"))
-- Start writing the Lua table format
file:write("local strokesData = {\n")
-- Iterate over each stroke and collect information
for i, stroke in ipairs(strokes) do
file:write(string.format(" [%d] = {\n", i))
file:write(" x = { ")
-- Write x coordinates
for j = 1, #stroke.x do
file:write(stroke.x[j])
if j < #stroke.x then
file:write(", ")
end
end
file:write(" },\n")
file:write(" y = { ")
-- Write y coordinates
for j = 1, #stroke.y do
file:write(stroke.y[j])
if j < #stroke.y then
file:write(", ")
end
end
file:write(" },\n")
-- Write pressure values if present
if stroke.pressure then
file:write(" pressure = { ")
for j = 1, #stroke.pressure do
file:write(stroke.pressure[j])
if j < #stroke.pressure then
file:write(", ")
end
end
file:write(" },\n")
else
file:write(" pressure = {},\n")
end
-- Write stroke options
file:write(string.format(" tool = \"%s\",\n", stroke.tool or "N/A"))
file:write(string.format(" color = %s,\n", stroke.color or "N/A"))
file:write(string.format(" width = %.2f,\n", stroke.width or 0.0))
file:write(string.format(" fill = %d,\n", stroke.fill or 0))
file:write(string.format(" lineStyle = \"%s\",\n", stroke.lineStyle or "N/A"))
file:write(" },\n") -- End of stroke table
end
file:write("}\n")
file:write("return strokesData")
file:write(" -- Return the strokesData table") -- End of strokesData table
-- Close the file
file:close()
end
return _M