-
Notifications
You must be signed in to change notification settings - Fork 0
/
Booklet.aspl
117 lines (108 loc) · 3.56 KB
/
Booklet.aspl
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
import io
import graphics
import json
class Booklet {
[readpublic]
property string name
[readpublic]
property Color color
[public]
property LazyChunkedCanvas canvas
[public]
property list<string> sheetFiles
[public]
property list<Sheet> sheets
[public]
property Sheet? currentSheet
[public]
property int scrollX
[public]
property int scrollY
[readpublic]
property bool loaded
[public]
method construct(string name, Color color, LazyChunkedCanvas canvas, list<string> sheetFiles, int scrollX, int scrollY) {
this.name = name
this.color = color
this.canvas = canvas
this.sheetFiles = sheetFiles
this.scrollX = scrollX
this.scrollY = scrollY
}
[public]
method prepare(){
var folder = io.join_path([self:getBookletsPath(), name])
canvas = LazyChunkedCanvas:fromFile(io.join_path([folder, "canvas.lci"]), io.join_path([folder, "chunks"]))
var list<string> sheetPaths = []
foreach(sheetFiles as file){
if(io.exists_file(string(folder) + "/sheets/" + string(file))){
sheets.add(Sheet:fromFile(string(folder) + "/sheets/" + string(file)))
sheetPaths.add(string(folder) + "/sheets/" + string(file))
}
}
foreach(io.files(string(folder) + "/sheets") as file){
if(!file.endsWith(".canvas.png") && !sheetPaths.contains(string(folder) + "/sheets/" + string(file))){
sheets.add(Sheet:fromFile(string(folder) + "/sheets/" + string(file)))
sheetFiles.add(string(file))
}
}
loaded = true
}
// unprepare unloades the booklet from memory
// this will automatically save the canvas of the booklet but not the sheets
[public]
method unprepare(){
loaded = false
canvas.requireArea(0, 0, 0, 0)
sheets = []
}
[public]
method save(bool saveOnlyMeta = false){
var bookletsPath = self:getBookletsPath()
if(!io.exists_directory(bookletsPath)){
io.create_directory(bookletsPath)
}
if(!io.exists_directory(bookletsPath + "/" + name)){
io.create_directory(bookletsPath + "/" + name)
}
var list<any> sheetsMap = []
foreach(sheets as sheet){
sheetsMap.add(io.file_name(sheet.file))
}
io.write_file(bookletsPath + "/" + name + "/booklet.json", json.encode(map<string, any>{
"color" => map<string, any>{
"r" => color.r,
"g" => color.g,
"b" => color.b,
"a" => color.a
},
"sheets" => sheetsMap,
"scroll" => map<string, any>{
"x" => scrollX,
"y" => scrollY
}
}))
if(!saveOnlyMeta){
canvas.save(bookletsPath + "/" + name + "/canvas.lci")
if(!io.exists_directory(bookletsPath + "/" + name + "/sheets")){
io.create_directory(bookletsPath + "/" + name + "/sheets")
}
foreach(sheets as sheet){
if(sheet.hasChanged){
sheet.hasChanged = false
sheet.save()
}
}
}
}
[public]
[static]
method getBookletsPath() returns string{
var bookletsPath = "booklets"
$if android{
var packageId = "com.wertzui123.libellus"
bookletsPath = "/data/data/" + packageId + "/files/booklets"
}
return bookletsPath
}
}