-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo_godot_ui.js
337 lines (288 loc) · 11.3 KB
/
demo_godot_ui.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
import { LX } from 'lexgui';
// init library and get main area
let area = LX.init();
// menu bar
area.addMenubar( m => {
// {options}: callback, icon, short
m.add( "Add/");
m.add( "Scene/New Scene", () => { console.log("New scene created!") });
m.add( "Scene/");
m.add( "Scene/Open Scene", { icon: "fa-solid fa-folder-open", short: "S", callback: () => { console.log("Opening SCENE Dialog") } } );
m.add( "Scene/Open Recent/hello.scene", name => { console.log("Opening " + name) });
m.add( "Scene/Open Recent/goodbye.scene", name => { console.log("Opening " + name) });
m.add( "Project/Project Settings" );
m.add( "Project/Export", { icon: "fa-solid fa-download" });
m.add( "Project/Export/DAE", { icon: "fa-solid fa-cube", short: "D", callback: () => { console.log("Exporting DAE...") }} );
m.add( "Project/Export/GLTF", { short: "G" } );
m.add( "Debug/Search Help", { icon: "fa-solid fa-magnifying-glass", short: "F1", callback: () => { window.open("./docs/") }});
m.add( "Debug/Support LexGUI/Please", { icon: "fa-solid fa-heart" } );
m.add( "Debug/Support LexGUI/Do it" );
m.addButtons( [
{
title: "Play",
icon: "fa-solid fa-play",
callback: (domEl) => {
console.log("play!");
domEl.classList.toggle('fa-play'), domEl.classList.toggle('fa-stop');
}
},
{
title: "Pause",
icon: "fa-solid fa-pause",
disabled: true,
callback: (domEl) => { console.log("pause!") }
},
{
icon: "fa-solid fa-magnifying-glass",
callback: (domEl) => { console.log("glass!") }
}
]);
m.setButtonIcon("Github", "fa-brands fa-github", () => {window.open("https://github.com/jxarco/lexgui.js/")})
m.setButtonImage("lexgui.js", "data/icon_godot_version.png", () => {window.open("https://github.com/jxarco/lexgui.js/")}, {float: "left"})
});
// split main area
var [_left,right] = area.split({sizes:["83%","17%"]});
// split main area
var [left,middle] = _left.split({sizes:["20%","80%"]});
// split left area
var [up, bottom] = middle.split({type: 'vertical', sizes:["70vh","30vh"]});
const bottom_tabs = bottom.addTabs({folding: "up"});
// Output
const output_area = new LX.Area({width: "calc(100% - 8px)", height: "calc(100% - 8px)"});
output_area.root.style.backgroundColor = LX.getThemeColor('global-color-secondary');
output_area.root.style.margin = "4px";
output_area.root.style.borderRadius = "6px";
const output_panel = output_area.addPanel();
output_panel.addTextArea(null, "Godot Engine v4.1.stable.official (c) 2007-present Juan Linietsky, Ariel Manzur & Godot Contributors.", null, {
height: "100%",
disabled: true,
style: {
'font-family': 'Inconsolata, monospace',
'border': 'none',
'resize': 'none'
}
});
// Debugger
const debugger_area = new LX.Area({width: "calc(100% - 8px)", height: "calc(100% - 8px)"});
const debugger_tabs = debugger_area.addTabs();
debugger_tabs.add( "Stack Trace", document.createElement('div'));
debugger_tabs.add( "Errors", document.createElement('div'));
// const debug_tabs = output_area.addTabs();
bottom_tabs.add( "Output", output_area);
bottom_tabs.add( "Debugger", debugger_area);
bottom_tabs.add( "Search Results", document.createElement('div'));
bottom_tabs.add( "Audio", document.createElement('div'));
bottom_tabs.add( "Animation", document.createElement('div'));
bottom_tabs.add( "Shader Editor", document.createElement('div'));
// Get new content area to fill it
const top_tabs = up.addTabs();
// add canvas to left upper part
var canvas = document.createElement('canvas');
canvas.style.width = "100%";
canvas.style.height = "100%";
const resize_canvas = ( bounding ) => {
canvas.width = bounding.width;
canvas.height = bounding.height;
};
top_tabs.add( "Node_3D", canvas, true, resize_canvas );
top_tabs.add( "Scene_1", document.createElement('div'));
// add on resize event to control canvas size
top_tabs.area.onresize = resize_canvas;
top_tabs.area.addOverlayButtons( [
[
{
name: "Select",
icon: "fa fa-arrow-pointer",
callback: (value, event) => console.log(value),
selectable: true
},
{
name: "Move",
icon: "fa-solid fa-arrows-up-down-left-right",
img: "https://webglstudio.org/latest/imgs/mini-icon-gizmo.png",
callback: (value, event) => console.log(value),
selectable: true
},
{
name: "Rotate",
icon: "fa-solid fa-rotate-right",
callback: (value, event) => console.log(value),
selectable: true
},
{
name: "Unselect",
icon: "fa-solid fa-x",
callback: (value, event) => console.log(value),
selectable: true
}
]
], { float: "vtl" } );
// add widgets
fillRightSide( right );
fillLeftSide( left );
const img = new Image();
img.src = "data/godot_canvas.png";
img.onload = () => {
requestAnimationFrame(loop);
}
function drawImageScaled(img, ctx) {
var hRatio = canvas.width / img.width ;
var vRatio = canvas.height / img.height ;
var ratio = Math.min ( hRatio, vRatio );
var centerShift_x = ( canvas.width - img.width*ratio ) / 2;
var centerShift_y = ( canvas.height - img.height*ratio ) / 2;
ctx.clearRect(0,0,canvas.width, canvas.height);
ctx.drawImage(img, 0,0, img.width, img.height,
centerShift_x,centerShift_y,img.width*ratio, img.height*ratio);
}
function loop(dt) {
var ctx = canvas.getContext("2d");
drawImageScaled(img, ctx);
// var aspect = img.width / img.height;
// ctx.drawImage(img, 0, 0, canvas.width * aspect, canvas.height);
requestAnimationFrame(loop);
}
// **** **** **** **** **** **** **** **** **** **** **** ****
function fillRightSide( area ) {
const tabs = area.addTabs({ fit: true });
var inspPanel = new LX.Panel();
var nodePanel = new LX.Panel();
var historyPanel = new LX.Panel();
LX.ADD_CUSTOM_WIDGET( 'Skeleton', {
// icon: "fa-dice-d6",
default: {
'position': [0, 0],
'velocity': [0, 0, 0],
'color': [0, 0, 0, 0],
'hex_color': '#000',
'high_res': false
}
});
const skeleton_instance = {
'hex_color': '#f5f505',
'high_res': true
};
inspPanel.addTitle("Mesh Instance 3D", {icon: "fa-brands fa-hashnode"});
inspPanel.addFile("Mesh");
inspPanel.branch("Skeleton");
inspPanel.addText("Skin", "...");
inspPanel.addNumber("NUMBER", 12);
inspPanel.addSkeleton("Skeleton", skeleton_instance);
inspPanel.merge();
inspPanel.addTitle("Geometry Instance 3D", {icon: "fa-regular fa-square-full", icon_color: "#d63434"});
inspPanel.branch("Geometry", {closed: true});
inspPanel.branch("Global Illumination", {closed: true});
inspPanel.branch("Visibility Range", {closed: true});
inspPanel.merge();
inspPanel.addTitle("Node 3D", {icon: "fa-regular fa-circle", icon_color: "#fff"});
inspPanel.branch("Transform", {closed: true});
inspPanel.branch("Visibility", {closed: true});
inspPanel.merge();
tabs.add( "Inspector", inspPanel );
tabs.add( "Node", nodePanel );
tabs.add( "History", historyPanel );
}
function fillLeftSide( area ) {
const tabs = area.addTabs({ fit: true });
var scenePanel = new LX.Panel();
var importPanel = new LX.Panel();
var filesPanel = new LX.Panel();
tabs.add( "Scene", scenePanel );
tabs.add( "Import", importPanel );
tabs.add( "Files", filesPanel );
// add data tree
let sceneData = {
'id': 'Node 3D',
'children': [
{
'id': 'WorldEnvironment',
'icon': 'fa-solid fa-globe',
'closed': true,
'children': [
{
'id': 'node_1_1',
'icon': 'fa-solid fa-cube',
'children': [],
'actions': [
{
'name': 'Open script',
'icon': 'fa-solid fa-scroll',
'callback': function(node) {
console.log(node.id + ": Script opened!")
}
}
]
}
]
},
{
'id': 'AnimatedSprite3D',
'icon': 'fa-solid fa-film',
'closed': true,
'children': [
{
'id': 'node_2_1',
'icon': 'fa-solid fa-circle',
'children': []
}
]
}
]
};
// This is optional!
const treeIcons = [
{
'name':'Add node',
'icon': 'fa-solid fa-plus',
'callback': () => { console.log("Node added!") }
},
{
'name':'Instantiate scene',
'icon': 'fa-solid fa-link',
'callback': () => { console.log("Scene instantiated!") }
}
];
scenePanel.addTree("Scene Tree", sceneData, {
icons: treeIcons,
// filter: false,
onevent: (event) => {
console.log(event.string());
switch(event.type) {
case LX.TreeEvent.NODE_SELECTED:
if(event.multiple)
console.log("Selected: ", event.node);
else
console.log(event.node.id + " selected");
break;
case LX.TreeEvent.NODE_DELETED:
console.log(event.node.id + " deleted");
break;
case LX.TreeEvent.NODE_DBLCLICKED:
console.log(event.node.id + " dbl clicked");
break;
case LX.TreeEvent.NODE_CONTEXTMENU:
LX.addContextMenu( event.multiple ? "Selected Nodes" : event.node.id, event.value, m => {
// {options}: callback, color
m.add( "Select Children", () => console.log("select children") );
m.add( "Clone", { callback: () => console.log("Clone"), color: "#0d5" } );
m.add( "Components/Transform");
m.add( "Components/MeshRenderer");
m.add( "Move before sibling" );
m.add( "Move after sibling" );
m.add( "Move to parent" );
m.add( "Delete" );
});
break;
case LX.TreeEvent.NODE_DRAGGED:
console.log(event.node.id + " is now child of " + event.value.id);
break;
case LX.TreeEvent.NODE_RENAMED:
console.log(event.node.id + " is now called " + event.value);
break;
case LX.TreeEvent.NODE_VISIBILITY:
console.log(event.node.id + " visibility: " + event.value);
break;
}
}
});
}