-
Notifications
You must be signed in to change notification settings - Fork 0
/
previewer.js
166 lines (130 loc) · 3.63 KB
/
previewer.js
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
const { remote } = require('electron');
var $dialogueBoxPrefab = $('<hgroup class="speech-bubble"><h1>Bubbles!</h1></hgroup>');
var data;
var nextNode;
var playerColor;
var globals = {};
function Init() {
var win = remote.getCurrentWindow();
$(window).on('keydown', function(event)
{
if (String.fromCharCode(event.which).toLowerCase() === 'f12' || event.which === 123) {
win.webContents.openDevTools();
return false;
}
else if (String.fromCharCode(event.which).toLowerCase() === 'f5' || event.which === 116) {
win.reload()
return false;
}
return true;
});
data = remote.getGlobal("current_dialogue").data;
for (var i = 0; i < data.globals.length; i++) {
globals[data.globals[i]] = null;
}
for (var i = 0; i < data.actors.length; i++) {
if (data.actors[i].isPlayer) {
playerColor = data.actors[i].color;
}
}
if (data.nodes.length > 0) {
LoadNode(data.nodes[0].id);
}
}
function Update() {
if (nextNode != null) {
LoadNode(nextNode);
}
else {
console.log("No Next Node!");
}
}
function LoadActor(actorName) {
var actors = data.actors;
for (var i = 0; i < actors.length; i++) {
if (actors[i].name == actorName) {
return actors[i];
}
}
return null;
}
function LoadNode(nodeID) {
if (nodeID == null)
return;
var node = GetNodeById(nodeID);
switch (node.type) {
case "Text":
LoadTextNode(node);
break;
case "Choice":
LoadChoiceNode(node);
break;
case "Set":
LoadSetNode(node);
break;
case "Branch":
LoadBranchNode(node);
break;
default:
break;
}
}
function LoadSetNode(node) {
globals[node.variable] = node.value;
nextNode = node.next;
setTimeout(Update, 800);
}
function LoadChoiceNode(node) {
var choices = node.choices;
var $button = $('<button type="button" class="choice-button"></button>');
var count = Object.keys(choices).length;
for (var i = 0; i < count; i++) {
var $b = $button.clone();
var t = Object.keys(choices)[i];
$b.append('<h1>' + t +'</h1>');
$('#choice-box').append($b);
$b.bind('click', { nextNode: choices[t], text: t}, function(evt) {
var data = evt.data;
nextNode = data.nextNode;
CreateBubble(data.text, true, playerColor);
$('#choice-box').empty();
setTimeout(Update, 800);
});
}
}
function LoadBranchNode(node) {
var branches = node.branches;
nextNode = branches[globals[node.variable]];
setTimeout(Update, 800);
}
function CreateBubble(text, isPlayer, color) {
var $speechBox = $dialogueBoxPrefab.clone();
if (isPlayer) {
$speechBox.attr("class", "speech-bubble left");
$speechBox.css("float", "left");
}
else {
$speechBox.attr("class", "speech-bubble right");
$speechBox.css("float", "right");
}
$speechBox.text("");
$speechBox.append('<h1>' + text + '</h1>');
$speechBox.css("background", color);
$speechBox.css("border-top-color", color);
$('#paper').append($speechBox);
}
function LoadTextNode(node) {
var actor = LoadActor(node.actor);
nextNode = node.next;
CreateBubble(node.name, actor.isPlayer, actor.color);
setTimeout(Update, 800);
}
function GetNodeById(nodeID) {
for (var i = 0; i < data.nodes.length; i++) {
if (data.nodes[i].id == nodeID) {
return data.nodes[i];
}
}
return null;
}
window.onload = Init;