This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
160 lines (145 loc) · 4.41 KB
/
script.js
File metadata and controls
160 lines (145 loc) · 4.41 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
const width = 480;
const height = 270;
let script = {
json: null,
currentLine: 0,
currentScene: 0,
}
let textbox = document.getElementById("text");
let textdiv = document.getElementById("text-div");
let frame = document.getElementById("frame");
let image = document.querySelector("img");
let video = document.querySelector("video");
getList().then(function(res) {
response = JSON.parse(res);
if(response.type === "list") {
response.files.forEach((e) => {
textdiv.innerHTML += "<button onclick=\"init('"+e+"')\">"+e+"</button><br>";
});
} else {
document.body.innerHTML += "error: "+response.msg;
}
});
function add_img(name) {
getBg(name).then(data => data.blob()).then(blob => {
frame.innerHTML="<img src=\""+URL.createObjectURL(blob)+"\" style=\"max-height:"+height+"px;max-width:"+width+"px;\"/>";
image = document.querySelector("img");
});
}
function add_video(name) {
getAnim(name).then(data => data.blob()).then(blob => {
frame.innerHTML="<video src=\""+URL.createObjectURL(blob)+"\" type=\"video/mp4\" autoplay loop style=\"max-height:"+height+"px;max-width:"+width+"px;\"/>";
video = document.querySelector("video");
});
}
function add_video_once(name) {
getAnim(name).then(data => data.blob()).then(blob => {
frame.innerHTML="<video src=\""+URL.createObjectURL(blob)+"\" type=\"video/mp4\" autoplay style=\"max-height:"+height+"px;max-width:"+width+"px;\"/>";
video = document.querySelector("video");
});
}
function add_textbox() {
textdiv.innerHTML="<button id=\"text\" onclick=\"parseLine()\"></button>";
textbox = document.getElementById("text");
}
function init(name) {
loadScript(name).then(function(res) {
add_textbox();
script.json = res;
container.style = "max-width:"+width+"px;";
frame.style = "max-height:"+height+"px;";
parseLine();
});
}
function parseLine() {
let scene = script.json.scenes[script.currentScene];
if(script.currentLine >= scene.script.length) {
if(script.currentScene+1 === script.json.scenes.length) {
textdiv.innerHTML="END";
return;
}
script.currentScene++;
script.currentLine = 0;
}
scene = script.json.scenes[script.currentScene];
let line = scene.script[script.currentLine];
if(line.includes("@bg")) {
let variable = line.slice(line.indexOf(' ')+1);
if(line.includes('loop')||line.includes('once'))
variable = variable.slice(0, variable.indexOf(' '));
let file = '';
if(variable.includes('.')) {
file = scene.resources_path+variable;
}
else file = findResource(variable);
if(file === '') {
document.body.innerHTML += "error: could not find variable "+variable;
return
}
if(file.includes('mp4')) {
if(line.includes('loop')) add_video(file);
else add_video_once(file);
} else {
add_img(file);
}
script.currentLine++;
}
textbox.innerText = scene.script[script.currentLine++];
}
function loadScript(name) {
return getScript(name).then(function(result) {
response = JSON.parse(result);
if(response.type === "script") {
return response;
} else {
document.body.innerHTML += "error: "+response.msg;
}
});
}
function findResource(name) {
let global_resources = script.json.global_resources;
let scene_resources = script.json.scenes[script.currentScene].resources;
let resource = '';
global_resources.some((e) => {
for(key in e) if(key === name) resource = e[key];
if(resource !== "") return;
});
if(resource !== "") return resource;
scene_resources.some((e) => {
for(key in e) if(key === name) resource = e[key];
if(resource !== "") return;
});
return resource;
}
async function getList() {
let response = await fetch('/list');
let data = await response.text();
return data;
}
async function getScript(name) {
let response = await fetch('/script', {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({"filename":name})
});
let data = await response.text();
return data;
}
async function getAnim(name) {
let response = await fetch('/anim', {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({"filename":name})
});
let data = await response;
return data;
}
async function getBg(name) {
let response = await fetch('/bg', {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({"filename":name})
});
let data = await response;
return data;
}