-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js.copy
357 lines (290 loc) · 9.67 KB
/
main.js.copy
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
import Windows from "./windowSystem.js";
let
ScenesElement = document.querySelector(".scenes"),
NewSceneButton = document.querySelector(".headerButtons.newScene"),
NewSequenceButton = document.querySelector(".headerButtons.newSequence"),
LoadImageButton = document.querySelector(".headerButtons.loadImage"),
WindowsElement = document.querySelector(".windows");
let Scenes = window.Scenes = [];
let windows = new Windows("window", WindowsElement, {
cssInject: true
});
const getOpenButtons = (selector = "button.gotoScene") => {
return [...document.querySelectorAll(selector)]
}
const CreateNewScene = (element) => {
bootbox.prompt("Enter scene name", (name) => {
if (name == null) return bootbox.alert("Enter not empty string!")
Scenes.push({
name: name,
dialogs: []
});
scenesSync(element, Scenes);
});
}
const buttonsSetup = (newScene, newSequence, loadImage, opens) => {
newScene.onclick = () => {
CreateNewScene(ScenesElement); // ScenesElement - global
};
}
const scenesSync = (element, scenes = Scenes) => {
element.innerHTML = "";
scenes.map((scene, idx) => {
let newScene = document.createElement("div");
newScene.classList.add("scene");
newScene.setAttribute("sceneId", idx);
let sceneElements = {
sceneName: { element: document.createElement("span"), sets: {
innerHTML: scene.name
} },
sceneActions: { element: document.createElement("span"), sets: {
innerHTML: scene.dialogs.length > 0 ? scene.dialogs.length : "there are no dialogs"
} },
gotoScene: {
element: document.createElement("button"),
sets: {
className: "btn btn-primary navy gotoScene",
innerHTML: "Open"
}
}
}
Object.keys(sceneElements).forEach(className => {
let sceneElement = sceneElements[className];
sceneElement.element.classList.add(className);
Object.keys(sceneElement.sets).forEach(setting => {
let value = sceneElement.sets[setting];
sceneElement.element[setting] = value;
});
if (sceneElement.attributes != undefined) {
Object.keys(sceneElement.attributes).forEach(attributeName => {
let atttribute = sceneElement.attributes[attributeName];
sceneElement.setAttribute(attributeName, atttribute);
});
}
newScene.appendChild(sceneElement.element);
});
element.appendChild(newScene);
});
getOpenButtons().forEach(button => {
button.onclick = () => {
openScene(button.parentNode, parseInt(button.parentNode.getAttribute("sceneId")));
}
});
}
const inputBox = (options) => {
let box = document.createElement(options.tag ?? "input");
box.innerHTML = options?.innerHTML;
options.classList != undefined ? box.classList.add(...options.classList) : null;
Object.assign(box.style, options.style);
if (options.attributes != undefined) {
Object.keys(options.attributes).forEacha(attr => {
let value = options.attributes[attr];
box.setAttribute(attr, value);
});
}
return box;
}
const textElement = (options) => {
let element = document.createElement(options.tag ?? "span");
element.innerHTML = options?.innerHTML;
Object.assign(element.style, options.style);
options.classList != undefined ? element.classList.add(...options.classList) : null;
return element;
}
const makeDialogArticle = (name, id) => {
let parent = document.createElement("div");
parent.classList.add("dialogArticleOne");
parent.setAttribute("dialogId", id);
let dialogCharNameText = textElement({
innerHTML: name,
classList: ["dialogArticleName"]
});
let gotoDialog = inputBox({
tag: "button",
innerHTML: "Open",
classList: ["openDialogButton" , "btn", "btn-primary"]
});
parent.appendChild(dialogCharNameText);
parent.appendChild(gotoDialog);
return parent;
}
const makeSetupUI = () => {
let parent = document.createElement("div");
parent.classList.add("setupUIRoot");
let nameBoxParent = document.createElement("div");
nameBoxParent.classList.add("nameBoxParent");
let nameBox = inputBox({
classList: ["sceneInputName"]
});
let nameLabel = textElement({
tag: "label",
innerHTML: "Scene name: ",
classList: ["nameBoxLabel"],
style: {
marginRight: "5px"
}
});
let saveButton = inputBox({
tag: "button",
innerHTML: "Save",
classList: ["sceneEditSave", "btn", "btn-primary"],
style: {
position: "absolute",
bottom: 0,
right: 0,
marginBottom: "5px",
marginRight: "5px"
}
});
let newDialogButton = inputBox({
tag: "button",
innerHTML: "New dialog",
classList: ["newDialogButton", "btn", "btn-success"]
});
let dialogs = document.createElement("div");
dialogs.classList.add("dialogSetupArticle");
nameBoxParent.appendChild(nameLabel);
nameLabel.appendChild(nameBox);
parent.appendChild(nameBoxParent);
parent.appendChild(saveButton);
parent.appendChild(dialogs);
parent.appendChild(newDialogButton);
return {
boxes: {
nameLabel: nameLabel,
nameBox: nameBox,
nameBoxParent: nameBoxParent,
saveButton: saveButton,
dialogs: dialogs
},
parent: parent
};
}
const makeInputWithLabel = (options) => {
let Parent = document.createElement("div");
Parent.classList.add(...options.classList);
let Box = inputBox({
classList: ["inputBox"],
style: options.style
});
let Label = textElement({
tag: "label",
innerHTML: options.name,
classList: ["labelBox"],
style: {
marginRight: "5px"
}
});
Parent.appendChild(Label);
Label.appendChild(Box);
return Parent;
}
const makeDialogUI = () => {
let parent = document.createElement("div");
parent.classList.add("dialogSetup");
let charName = makeInputWithLabel({
name: "Character name: ",
classList: ["characterName"]
});
let dialogText = makeInputWithLabel({
name: "Dialog text: ",
classList: ["dialogTextParent"],
style: {
marginTop: "10px"
}
});
let timeoutText = makeInputWithLabel({
name: "Timeout: ",
classList: ["timeoutText"],
style: {
marginTop: "10px"
}
});
let saveButton = inputBox({
tag: "button",
innerHTML: "Save",
classList: ["dialogSaveButton", "btn", "btn-primary"],
style: {
position: "absolute",
bottom: 0,
right: 0,
marginBottom: "5px",
marginRight: "5px"
}
});
parent.appendChild(charName);
parent.appendChild(dialogText);
parent.appendChild(timeoutText);
parent.appendChild(saveButton);
return {
parent: parent
}
}
const openScene = (sceneElement, index) => {
let thisWindow = windows.addWindow();
let ui = makeSetupUI();
let scene = Scenes[index];
thisWindow.open(thisWindow);
thisWindow.setTitle(`Editing scene: ${scene.name}`, thisWindow.id);
thisWindow.window_html(ui.parent.outerHTML, thisWindow.id);
dialogsSync(scene.dialogs, thisWindow.getFromContent(".setupUIRoot > .dialogSetupArticle", thisWindow.id), scene, thisWindow);
let sceneNameInput = thisWindow.getFromContent(".setupUIRoot > .nameBoxParent > .nameBoxLabel > .sceneInputName", thisWindow.id);
let dialogArticles = thisWindow.getFromContent(".setupUIRoot > .dialogSetupArticle", thisWindow.id);
sceneNameInput.value = scene.name;
// -------------------- EVENTS --------------------
thisWindow.getFromContent(".setupUIRoot > .sceneEditSave", thisWindow.id).onclick = (event) => {
scene.name = sceneNameInput.value;
scenesSync(ScenesElement);
thisWindow.setTitle(`Editing scene: ${scene.name}`, thisWindow.id);
};
thisWindow.getFromContent(".setupUIRoot > .newDialogButton", thisWindow.id).onclick = (event) => {
bootbox.prompt("Enter character name", (name) => {
scene.dialogs.push({
name: name
});
dialogsSync(scene.dialogs, thisWindow.getFromContent(".setupUIRoot > .dialogSetupArticle", thisWindow.id), scene, thisWindow);
scenesSync(ScenesElement);
thisWindow.style.minHeight = (dialogArticles.offsetHeight + 100) + "px";
});
}
}
const openDialog = (dialog, scene, sceneDialog, setupWindow) => {
let dialogWindow = windows.addWindow();
let ui = makeDialogUI();
let index = dialog.getAttribute("dialogId");
dialogWindow.open(dialogWindow);
dialogWindow.setTitle(`Editing dialog in ${scene.name}`, dialogWindow.id);
dialogWindow.window_html(ui.parent.outerHTML, dialogWindow.id);
let charNameBox = dialogWindow.getFromContent(".dialogSetup > .characterName > .labelBox > .inputBox", dialogWindow.id);
let dialogText = dialogWindow.getFromContent(".dialogSetup > .dialogTextParent > .labelBox > .inputBox", dialogWindow.id);
let timeoutText = dialogWindow.getFromContent(".dialogSetup > .timeoutText > .labelBox > .inputBox", dialogWindow.id);
charNameBox.value = sceneDialog.name == undefined ? "" : sceneDialog.name;
dialogText.value = sceneDialog.text == undefined ? "" : sceneDialog.text;
timeoutText.value = sceneDialog.timeout == undefined ? "" : sceneDialog.timeout;
dialogWindow.getFromContent(".dialogSetup > .dialogSaveButton", dialogWindow.id).onclick = () => {
if (charNameBox.value.trim() != "") sceneDialog.name = charNameBox.value;
else sceneDialog.name = undefined;
sceneDialog.timeout = timeoutText.value.trim() == "" ? undefined : parseInt(timeoutText.value);
sceneDialog.text = dialogText.value;
dialogsSync(scene.dialogs, setupWindow.getFromContent(".setupUIRoot > .dialogSetupArticle", setupWindow.id), scene, setupWindow);
}
}
const dialogsSync = (dialogs, element, scene, thisWindow) => {
let idGenerator = makeId();
element.innerHTML = "";
dialogs.map(dialog => {
element.appendChild(makeDialogArticle(dialog.name == undefined || dialog.name.trim() == "" ? "prev character" : dialog.name, idGenerator.next().value));
});
[...element.querySelectorAll(".dialogArticleOne")].forEach((dialogArticle, idx) => {
dialogArticle.querySelector(".openDialogButton").onclick = () => {
openDialog(dialogArticle, scene, scene.dialogs[idx], thisWindow);
}
});
}
function* makeId() {
let id = 0;
while (true) {
yield id++;
}
}
buttonsSetup(NewSceneButton, NewSequenceButton, LoadImageButton);