-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu_states.js
624 lines (527 loc) · 21.4 KB
/
menu_states.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
var MenuStates = enchant.Class.create({
initialize : function(system){
/**
* メインメニューを表すステートクラス
*/
var MainMenu = enchant.Class.create({
initialize : function(system){
this.system = system;
this.tag_manager = null;
this.menu = null;
},
prepare : function(tag_obj){
this.xml_manager = this.system.getManager("xml");
if(!this.tag_manager) this.tag_manager = this.system.getManager("tag");
this.menu = tag_obj;
},
operateA : function(operator){
operator.inner_operator.operateA();
this.system.getManager("choices").clear();
var selected_menu = this.menu.children[operator.inner_operator.cur_index];
if(selected_menu.action)
this[selected_menu.action](operator, selected_menu);
},
operateUp : function(operator){
operator.inner_operator.operateUp();
},
operateDown : function(operator){
operator.inner_operator.operateDown();
},
openNew : function(operator, selected_menu){
this.tag_manager.setNextTag(this.xml_manager.getScene(selected_menu.to));
operator.clearMenu();
this.tag_manager.is_available = true;
},
openLoad : function(operator, selected_menu){
var children = [], saves = JSON.parse(localStorage.getItem("save"));
for(var i = 1; i < saves.length; ++i){
if(!saves[i])
continue;
var title = saves[i].scene_str.replace(/\s/g, "").replace(/\\s/g, " ").match(/title:([^,]+)/)[1];
children.push({
type : "save" + i,
data_index : i,
title : title,
saved_time : saves[i].saved_time,
text : "slot" + i,
action : "load"
});
}
if(saves.length == 1){
children.push({
type : "save_data",
data_index : 1,
title : "untitled",
saved_time : "unknown",
text : "No save data is available",
action : "load"
});
}
var scene = {type : "choices", children : children};
operator.setState(selected_menu.to, scene);
},
openOption : function(operator, selected_menu){
operator.setState(selected_menu.to);
},
openFile : function(operator, selected_menu){
var paths = this.xml_manager.getVarStore().getVar("file_paths"), children = [];
for(var i = 0; i < paths.length; ++i){
children.push({
type : "file" + i,
title : paths[i].title,
description : paths[i].description,
text : paths[i].title,
file_name : paths[i].file_name,
action : "loadFile"
});
}
operator.setState(selected_menu.to, {type : "choices", children : children});
}
});
/**
* ポップアップメニューを表すステートクラス
*/
var Menu = enchant.Class.create({
initialize : function(system){
this.system = system;
this.xml_manager = null;
this.console_manager = null;
this.menu = null;
},
prepare : function(tag_obj){
this.menu = tag_obj;
},
operateA : function(operator){
operator.inner_operator.operateA();
this.system.getManager("choices").clear();
var selected_menu = this.menu.children[operator.inner_operator.cur_index];
this[selected_menu.action](operator, selected_menu);
},
operateUp : function(operator){
operator.inner_operator.operateUp();
},
operateDown : function(operator){
operator.inner_operator.operateDown();
},
openSave : function(operator, selected_menu){
var children = [{type: "item", action: "returnToPopupMenu", to: "Menu", text: "戻る"}], saves = JSON.parse(localStorage.getItem("save"));
for(var i = 1; i <= 5; ++i){
if(!saves[i] || i > saves.length){
children.push({
type : "save" + i,
data_index : i,
title : "untitled",
saved_time : "unknown",
text : "slot" + i,
action : "save"
});
}else{
var title = saves[i].scene_str.replace(/\s/g, "").replace(/\\s/g, " ").match(/title:([^,]+)/)[1];
children.push({
type : "save" + i,
data_index : i,
title : title,
saved_time : saves[i].saved_time,
text : "slot" + i,
action : "save"
});
}
}
var scene = {type : "choices", children : children};
operator.setState(selected_menu.to, scene);
},
openLoad : function(operator, selected_menu){
var children = [{type: "item", action: "returnToPopupMenu", to: "Menu", text: "戻る"}], saves = JSON.parse(localStorage.getItem("save"));
for(var i = 1; i < saves.length; ++i){
if(!saves[i])
continue;
var title = saves[i].scene_str.replace(/\s/g, "").replace(/\\s/g, " ").match(/title:([^,]+)/)[1];
children.push({
type : "save" + i,
data_index : i,
title : title,
saved_time : saves[i].saved_time,
text : "slot" + i,
action : "load"
});
}
if(saves.length <= 1){
children.push({
type : "save_data",
data_index : 1,
title : "untitled",
saved_time : "unknown",
text : "No save data is available",
action : "load"
});
}
var scene = {type : "choices", children : children};
operator.setState(selected_menu.to, scene);
},
openOption : function(operator, selected_menu){
operator.setState(selected_menu.to);
},
returnToMenu : function(operator){
this.system.reset();
operator.clearMenu();
},
openConsole : function(operator, selected_menu){
if(!this.console_manager) this.console_manager = this.system.getManager("console");
if(!this.xml_manager) this.xml_manager = this.system.getManager("xml");
var reference_btns = document.getElementsByClassName("tabButton");
var tab_holder = document.getElementById("tab_holder");
var tab_content = document.getElementsByClassName("tabContent")[0];
tab_holder.style.display = "flex";
displayTab("game_console");
//selected_menu.text = "コンソールを隠す";
var reference_rect = reference_btns[0].getBoundingClientRect();
// -1するのは、ボーダーの幅を引くため
var game_height = window.innerHeight - (reference_rect.bottom - reference_rect.top) - 1;
//game.height = game_height;
tab_content.style.height = game_height + "px";
var var_store = this.xml_manager.getVarStore();
var variable_tree = document.getElementById("variable_tree");
var range = document.createRange();
range.selectNodeContents(variable_tree);
range.deleteContents();
var_store.enumerateVars(function(name, variable){
if(typeof variable === "object"){
for(var name2 in variable){
if(variable.hasOwnProperty(name2)){
var element = document.createElement("li");
element.textContent = name + "." + name2 + " = " + variable[name2];
variable_tree.appendChild(element);
}
}
}else{
var element = document.createElement("li");
element.textContent = name + " = " + variable;
variable_tree.appendChild(element);
}
});
if(!this.console_manager.console_initialized){
var loader_layer = document.getElementById("loader_layer");
var loader_image = document.getElementById("loader_image");
var margin_left = (window.innerWidth - 568) / 2.0;
loader_image.style.left = 234 + margin_left + "px"; // 244 = コンテンツ領域の幅 - ローダー画像の幅の半分
loader_image.style.top = 105 + "px"; // 105 = コンテンツ領域の高さ / 2 - ローダー画像の高さの半分
loader_layer.style.display = "block";
var xhr = new XMLHttpRequest();
xhr.onload = function(){
var source_code = document.getElementById("source_code");
source_code.textContent = xhr.responseText;
loadCssLazily("libs/prism.css");
loadScriptLazily("libs/prism_min.js", function(){
Prism.highlightAll(true, function(){
loader_layer.style.display = "none";
});
});
}
xhr.open("get", this.xml_manager.getUrl(), false);
xhr.send(null);
this.console_manager.console_initialized = true;
}
}
});
/**
* セーブ画面を表すステートクラス
*/
var MenuSave = enchant.Class.create({
initialize : function(system){
this.system = system;
this.sound_manager = null;
this.menu = null;
this.success_se_path = null;
this.fail_se_path = null;
var info_window = new enchant.DomLayer();
info_window.x = 25;
info_window.y = game.height / 2;
info_window.font = "bold x-large serif";
this.updateInfoWindow = function(operator){
if(operator.inner_operator.cur_index === 0)
return;
while(info_window._element.firstChild)
info_window._element.removeChild(info_window._element.firstChild);
var cur_menu = this.menu.children[operator.inner_operator.cur_index], title_node = document.createElement("p");
var saved_time_node = document.createElement("p");
title_node.appendChild(document.createTextNode("タイトル:" + cur_menu.title.replace(/\\s/g, " ")));
saved_time_node.appendChild(document.createTextNode("保存日時:" + cur_menu.saved_time));
info_window._element.appendChild(title_node), info_window._element.appendChild(saved_time_node);
};
this.prepare = function(tag_obj, operator){
var xml_manager = this.system.getManager("xml");
if(!this.sound_manager) this.sound_manager = this.system.getManager("sound");
if(!this.success_se_path) this.success_se_path = xml_manager.getHeader("settings")["success_se"];
if(!this.fail_se_path) this.fail_se_path = xml_manager.getHeader("settings")["fail_se"];
this.menu = tag_obj;
this.system.addChild(info_window);
this.updateInfoWindow(operator);
var self = this;
operator.setEventHandlerToChoices("Clicked", function(){
self.updateInfoWindow(operator);
});
};
this.dispose = function(){
this.system.removeChild(info_window);
};
},
operateA : function(operator){
var selected_menu = this.menu.children[operator.inner_operator.cur_index];
this[selected_menu.action](operator, selected_menu);
},
operateUp : function(operator){
operator.inner_operator.operateUp();
this.updateInfoWindow(operator);
},
operateDown : function(operator){
operator.inner_operator.operateDown();
this.updateInfoWindow(operator);
},
save : function(operator, selected_menu){
var label_text = "";
if(!operator.tag_manager.save(selected_menu.data_index)){
label_text = "セーブに失敗しました";
if(this.fail_se_path)
this.sound_manager.add({src : this.fail_se_path, operation : "once", sync : "true"});
}else{
label_text = "セーブ完了";
if(this.success_se_path)
this.sound_manager.add({src : this.success_se_path, operation : "once", sync : "true"});
}
operator.tag_manager.restore(); //tag_managerをメニューを開く前の状態に戻す
operator.clearMenu();
var notice_label = {
x : "centered",
y : operator.msg_manager.msg_window._element.clientTop.toString(),
end_time : 60,
width : "adjust",
style : "font: bold large serif;"
};
this.system.showNoticeLabel(label_text, notice_label);
},
returnToPopupMenu : function(operator, selected_menu){
operator.choices_manager.clear();
var xml_manager = this.system.getManager("xml");
var tag_obj = xml_manager.getScene("title:menu").children[0];
operator.setState(tag_obj.initial_state, tag_obj);
}
});
/**
* ロード画面を表すステートクラス
*/
var MenuLoad = enchant.Class.create({
initialize : function(system){
this.system = system;
this.sound_manager = null;
this.menu = null;
this.success_se_path = null;
var info_window = new enchant.DomLayer();
info_window.x = 25;
info_window.y = game.height / 2;
info_window.font = "bold x-large serif";
this.updateInfoWindow = function(operator){
if(operator.inner_operator.cur_index === 0)
return;
while(info_window._element.firstChild)
info_window._element.removeChild(info_window._element.firstChild);
var cur_menu = this.menu.children[operator.inner_operator.cur_index], title_node = document.createElement("p");
var saved_time_node = document.createElement("p");
title_node.appendChild(document.createTextNode("タイトル:" + cur_menu.title.replace(/\\s/g, " ")));
saved_time_node.appendChild(document.createTextNode("保存日時:" + cur_menu.saved_time));
info_window._element.appendChild(title_node), info_window._element.appendChild(saved_time_node);
};
this.prepare = function(tag_obj, operator){
var xml_manager = this.system.getManager("xml");
if(!this.sound_manager) this.sound_manager = this.system.getManager("sound");
if(!this.success_se_path) this.success_se_path = xml_manager.getHeader("settings")["success_se"];
this.menu = tag_obj;
this.system.addChild(info_window);
this.updateInfoWindow(operator);
var self = this;
operator.setEventHandlerToChoices("Clicked", function(){
self.updateInfoWindow(operator);
});
};
this.dispose = function(){
this.system.removeChild(info_window);
};
},
operateA : function(operator){
var selected_menu = this.menu.children[operator.inner_operator.cur_index];
this[selected_menu.action](operator, selected_menu);
},
operateUp : function(operator){
operator.inner_operator.operateUp();
this.updateInfoWindow(operator);
},
operateDown : function(operator){
operator.inner_operator.operateDown();
this.updateInfoWindow(operator);
},
load : function(operator, selected_menu){
operator.tag_manager.load(selected_menu.data_index);
operator.tag_manager.interpreters["br"].icon = null;
operator.clearMenu();
if(this.success_se_path)
this.sound_manager.add({src : this.success_se_path, operation : "once", sync : "true"});
var notice_label = {
x : "centered",
y : operator.msg_manager.msg_window._element.clientTop.toString(),
end_time : 60,
width : "adjust",
style : "font: bold large serif;"
};
this.system.showNoticeLabel("ロード完了", notice_label);
operator.msg_manager.clearChildNodes();
operator.tag_manager.is_available = true;
},
returnToPopupMenu : function(operator, selected_menu){
operator.choices_manager.clear();
var xml_manager = this.system.getManager("xml");
var tag_obj = xml_manager.getScene("title:menu").children[0];
operator.setState(tag_obj.initial_state, tag_obj);
}
});
/**
* オプション画面を表すステートクラス
*/
var MenuOption = enchant.Class.create({
initialize : function(system){
this.options_layer = new enchant.DomLayer();
this.system = system;
this.sound_manager = null;
this.success_se_path = null;
this.operator = null;
var options = document.getElementById("options");
this.inner_options = document.getElementsByTagName("input");
this.button = document.getElementById("apply_button");
this.options_layer._element.appendChild(options);
},
prepare : function(tag, operator){
var xml_manager = operator.system.getManager("xml");
if(!this.sound_manager) this.sound_manager = operator.system.getManager("sound");
if(!this.success_se_path) this.success_se_path = xml_manager.getHeader("settings")["success_se"];
if(!this.operator) this.operator = operator;
this.system.addChild(this.options_layer);
var options = this.inner_options, var_store = xml_manager.getVarStore();
for(var i = 0; i < options.length; ++i){
options[i].onchange = function(){
var parent = this.parentNode;
parent.replaceChild(document.createTextNode(this.value), parent.lastChild);
};
options[i].value = var_store.getVar("options." + options[i].name);
options[i].onchange();
}
var self = this;
this.button.onclick = function(){
var options_values = [];
for(var i = 0; i < options.length; ++i){
options_values.push({
name : options[i].name,
value : options[i].valueAsNumber || options[i].value
});
}
xml_manager.updateOptions(options_values);
xml_manager.saveOptions(); //変更したオプションを恒久的に保存しておく
if(self.success_se_path)
self.sound_manager.add({src : self.success_se_path, operation : "once", sync : "true"});
var notice_label = {
x : "centered",
y : self.operator.msg_manager.msg_window._element.clientTop.toString(),
end_time : 60,
width : "adjust",
style : "font: bold large serif;"
};
self.system.showNoticeLabel("オプション変更完了", notice_label);
};
},
dispose : function(){
this.system.getManager("tag").text_speed = this.system.getManager("xml").getVarStore().getVar("options.text_speed");
this.system.removeChild(this.options_layer);
},
operateA : function(){
this.button.click();
}
});
var MenuFile = enchant.Class.create({
initialize : function(system){
this.system = system;
this.menu = null;
this.success_se_path = null;
var info_window = new enchant.DomLayer();
info_window.x = 25;
info_window.y = game.height / 2;
info_window.font = "bold x-large serif";
this.updateInfoWindow = function(operator){
if(operator.inner_operator.cur_index === 0)
return;
while(info_window._element.firstChild)
info_window._element.removeChild(info_window._element.firstChild);
var cur_menu = this.menu.children[operator.inner_operator.cur_index], title_node = document.createElement("p");
var description_node = document.createElement("p");
title_node.appendChild(document.createTextNode("タイトル:" + cur_menu.title.replace(/\\s/g, " ")));
description_node.appendChild(document.createTextNode(cur_menu.description));
info_window._element.appendChild(title_node), info_window._element.appendChild(description_node);
};
this.prepare = function(tag_obj, operator){
var xml_manager = this.system.getManager("xml");
if(!this.success_se_path) this.success_se_path = xml_manager.getHeader("settings")["success_se"];
this.menu = tag_obj;
this.system.addChild(info_window);
this.updateInfoWindow(operator);
var self = this;
operator.setEventHandlerToChoices("Clicked", function(){
self.updateInfoWindow(operator);
});
}
this.dispose = function(){
this.system.removeChild(info_window);
}
},
operateA : function(operator){
var selected_menu = this.menu.children[operator.inner_operator.cur_index];
this[selected_menu.action](operator, selected_menu);
},
operateUp : function(operator){
operator.inner_operator.operateUp();
this.updateInfoWindow(operator);
},
operateDown : function(operator){
operator.inner_operator.operateDown();
this.updateInfoWindow(operator);
},
loadFile : function(operator, selected_menu){
var xml_manager = this.system.getManager("xml");
xml_manager.saveOptions();
this.system.setManager("xml", new XmlManager(selected_menu.file_name, this.system));
xml_manager = this.system.getManager("xml");
xml_manager.tag_manager = this.system.getManager("tag");
if(this.success_se_path)
this.sound_manager.add({src : this.success_se_path, operation : "once", sync : "true"});
xml_manager.getVarStore().setVar("file_paths", this.menu.children, true);
this.system.reset();
operator.clearMenu();
var path_header = xml_manager.getHeader("paths"), paths = xml_manager.getVarStore().getVar("paths");
this.system.loadResources(path_header, paths);
var notice_label = {
x : "centered",
y : operator.msg_manager.msg_window._element.clientTop.toString(),
end_time : 60,
width : "adjust",
style : "font: bold large serif;"
};
this.system.showNoticeLabel("ファイル変更完了", notice_label);
},
returnToMenu : function(operator, selected_menu){
this.system.reset();
operator.clearMenu();
}
});
this.MainMenu = new MainMenu(system);
this.Menu = new Menu(system);
this.MenuSave = new MenuSave(system);
this.MenuLoad = new MenuLoad(system);
this.MenuOption = new MenuOption(system);
this.MenuFile = new MenuFile(system);
}
});