Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/libs/Editor_files/App.js
4627 views
1
var pgli = pgli || {};
2
3
pgli.App = gamecore.Base.extend("App",
4
{ // static
5
6
},
7
{ // instance
8
9
project: null,
10
moduleList: null,
11
editor: null,
12
diagram: null,
13
preview: null,
14
nodeCount: 0,
15
console: null,
16
debug: 2, // 0:none, 1:inapp, 2:console, 3:both
17
18
init: function(domDiagram, domModuleList, domEditor, domPreview)
19
{
20
var self = this;
21
22
this.moduleList = new pgli.ui.ModuleList(domModuleList);
23
24
this.editor = ace.edit(domEditor);
25
this.editor.setFontSize("16px");
26
this.editor.setTheme("ace/theme/monokai");
27
this.editor.getSession().setMode("ace/mode/json");
28
29
this.diagram = new pgli.diagram.Diagram(domDiagram, 30);
30
31
this.preview = new pgli.render.CanvasRenderer(domPreview);
32
33
this.console = $('#console-text');
34
35
this.bindEvents();
36
37
pgli.lang.Parser.debug = self.debug;
38
39
window.trace = function(args)
40
{
41
if(!self.debug) return;
42
for(var i=0, len=arguments.length; i<len; ++i)
43
{
44
if(self.debug < 2) console.log(arguments[i]);
45
if(self.debug == 1 || self.debug == 3) return;
46
self.console.append(arguments[i].toString()+"\n");
47
self.console.scrollTop(
48
self.console[0].scrollHeight - self.console.height()
49
);
50
};
51
}
52
53
window.clearTrace = function()
54
{
55
self.console.text("");
56
}
57
},
58
59
bindEvents: function()
60
{
61
var self = this;
62
63
$(window).on('resize', function(){ return self.resize.call(self); });
64
$(document).bind('keydown', function(e){ return self.onKeyDown.call(self,e); });
65
$('#modules').on('drop', function(e) { return self.onDropEvent.call(self,e); });
66
//window.addEventListener("drop",function(e){ return self.onDropEvent.call(self,e); }) ;
67
68
},
69
70
bindProject: function(project)
71
{
72
this.nodeCount = 0;
73
this.project = project;
74
this.project.setAppInstance(this);
75
this.moduleList.bindProject(project);
76
this.preview.bindProject(project);
77
this.draw();
78
},
79
80
draw: function()
81
{
82
this.moduleList != undefined && this.moduleList.draw();
83
//this.preview.draw();
84
},
85
86
showInEditor: function(module)
87
{
88
this.project.setActiveFile(module);
89
this.editor.getSession().setValue(this.project.files.get(module));
90
},
91
92
getEditorContent: function()
93
{
94
return this.editor.getSession().getValue();
95
},
96
97
addDiagramNode: function(key, module)
98
{
99
this.diagram.addNode(new pgli.diagram.Node(key, module, 50 + 160 * this.nodeCount++, 50));
100
},
101
102
resize: function()
103
{
104
this.diagram.resize();
105
this.preview.resize();
106
},
107
108
saveModule: function()
109
{
110
for(var i=0; i<this.project.keys.length;i++)
111
{
112
var name = this.project.keys[i]
113
var fileToSave = this.project.getModule(name);
114
var jsonData = {file :'files/'+name ,obj: fileToSave};
115
116
$.ajax({
117
url:"/",
118
type:"POST",
119
data: JSON.stringify(jsonData),
120
contentType: "application/json; charset=utf-8",
121
dataType: "text",
122
success:function(a)
123
{
124
console.log("AJAX POST OK: ", a);
125
},
126
error: function(a)
127
{
128
console.log("AJAX POST ERROR: ", a);
129
}
130
});
131
132
console.log("STARTED AJAX REQUEST");
133
}
134
135
},
136
137
onKeyDown: function(e)
138
{
139
if(e.keyCode==117)
140
{
141
this.updateDiagram();
142
e.preventDefault();
143
return false;
144
}
145
else if(e.keyCode==118)
146
{
147
this.preview.draw();
148
e.preventDefault();
149
return false;
150
}
151
else if(e.keyCode==119)
152
{
153
this.saveModule();
154
e.preventDefault();
155
return false;
156
}
157
},
158
159
updateDiagram:function()
160
{
161
this.project.rememberActiveFile();
162
163
for(var i = 0, len = this.project.keys.length; i<len; i++)
164
{
165
166
var object = pgli.lang.Parser.parseModule(this.project.files.get(this.project.keys[i]));
167
this.project.modules.put(this.project.keys[i], object);
168
this.diagram.getNode(this.project.keys[i]).module = object;
169
//this.getNode.updateModule()...
170
171
}
172
173
this.diagram.draw();
174
},
175
176
onDropEvent: function(e)
177
{
178
trace("#Parsing dropped file(s)...");
179
e.preventDefault();
180
var self = this;
181
182
var length = e.originalEvent.dataTransfer.files.length;
183
for (var i = 0; i < length; i++)
184
{
185
var file = e.originalEvent.dataTransfer.files[i];
186
console.log(file);
187
188
fileName = file.name;
189
190
if(i == 0 && this.project == null)
191
{
192
var path = window.prompt("Please prove project's root path (with trailing slash).", "../files/");
193
trace("#Opening new project from ["+fileName+"]...");
194
this.bindProject(new pgli.Project(path+fileName, function(){
195
self.draw();
196
self.showInEditor(self.project.root);
197
}));
198
}
199
else
200
this.project.loadFile(self.project.path+fileName,fileName,true,true);
201
202
}
203
204
this.draw();
205
return false;
206
207
208
}
209
210
211
212
});
213