-
Notifications
You must be signed in to change notification settings - Fork 351
/
extension-spiralGenerator.js
586 lines (542 loc) · 19.1 KB
/
extension-spiralGenerator.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
const tags = ["PCG"]
const StyleSetName = 'UnrealJS'
function purge(world) {
let prev_actors = world.GetAllActorsOfClassAndTags(StaticMeshActor, tags).OutActors
prev_actors.forEach((actor) => world.EditorDestroyActor(actor))
}
function generate_spiral(world, opts) {
const mesh = opts.mesh
const mtrl = opts.mtrl
let N = opts.N || 10
let num_spirals = opts.num_spirals || 5
let radius = opts.radius || 200
let height = opts.height || 200 * 5
purge(world)
for (let i = 0; i < N; ++i) {
let v = i / N
let u = v * num_spirals * 2 * Math.PI
let t = {
Translation: {
X: Math.sin(u) * radius,
Y: Math.cos(u) * radius,
Z: v * height
}
}
let color = {
R: t.Translation.X * 2 + 1,
G: t.Translation.Y * 2 + 1,
B: v,
A: 1
}
let mi = world.CreateDynamicMaterialInstance(mtrl)
MaterialEditingLibrary.SetMaterialInstanceVectorParameterValue(mi, 'color', color)
let sma = StaticMeshActor.C(world.BeginDeferredActorSpawnFromClass(StaticMeshActor, t, 'AlwaysSpawn', null))
sma.StaticMeshComponent.SetMobility('Movable')
sma.StaticMeshComponent.SetStaticMesh(mesh)
sma.StaticMeshComponent.SetMaterial(0, mi)
sma.StaticMeshComponent.ReregisterComponent()
sma.FinishSpawningActor(t)
sma.Tags = tags
}
}
function get_engine() {
return Root.GetEngine()
}
function get_world() {
return get_engine().GetEditorWorld()
}
function redraw() {
get_engine().RedrawAllViewports(true)
}
function generate(data) {
generate_spiral(get_world(), data)
redraw()
}
function clear() {
purge(get_world())
redraw()
}
function main() {
let UMG = require('UMG')
let instantiator = require('instantiator')
let schema = {
"title": "SpiralMetaData",
"type": "object",
"properties": {
"desc" : {
"type" : "string"
},
"mesh" : {
"type" : "StaticMesh",
},
"mtrl" : {
"type" : "Material",
},
"N" : {
"type" : "integer",
},
"height" : {
"type" : "float",
},
"num_spirals" : {
"type" : "integer",
},
"radius" : {
"type" : "float"
},
"test" : {
"type" : "array",
"items" : {
"title": "TestStruct",
"type": "object",
"struct": "true",
"properties": {
"A": {
"type" : "integer"
},
"B": {
"type" : "string"
}
}
}
}
},
"required" : [ "N", "height", "num_spirals", "radius" ]
}
let json2u = require('./json2u')()
let meta = json2u.create('spiral',schema)
let previewWorld
function gen() {
let data = new meta()
data.num_spirals = 10;
data.radius = Math.random() * 300 + 100;
data.N = 100;
data.height = Math.random() * 800 + 200
data.mesh = StaticMesh.Load('/Engine/BasicShapes/Sphere')
data.mtrl = Material.Load('/Game/Geometry/Materials/Color.Color')
data.desc = Math.random().toString(16)
return data
}
let data = gen()
let GEngine = Root.GetEngine()
const buttonTextStyle = {
Font : {
FontObject : GEngine.SmallFont,
Size : 10
},
ColorAndOpacity : {
SpecifiedColor:{R:0,G:0,B:0,A:1}
}
}
let spin
let viewport
function preview() {
purge(previewWorld)
generate_spiral(previewWorld, data)
viewport.Redraw()
}
function tick() {
if (--spin > 0) {
process.nextTick(tick)
} else {
spin = null
preview()
}
}
function touch() {
if (!spin) {
process.nextTick(tick)
}
spin = 10
}
let filter = ''
let filter_listeners = []
let listeners = []
let viewportDesign =
UMG(JavascriptEditorViewport,
{
OnDraw:(_PDI,elem) => {
let PDI = JavascriptPDI.C(_PDI)
PDI.SetHitProxy('Line')
PDI.DrawDashedLine({},{X:1000,Z:1000},{R:1,A:1},4,'SDPG_World',0)
PDI.SetHitProxy('Circle')
PDI.DrawCircle({},{X:1},{Y:1},{R:1,A:1},1000,32,'SDPG_World',2)
PDI.SetHitProxy('None')
},
OnClick:(click,proxy) => {
console.log(proxy.GetName())
},
$link:elem => {
viewport = JavascriptEditorViewport.C(elem)
process.nextTick(__ => {
previewWorld = viewport.GetViewportWorld()
generate_spiral(previewWorld, data)
})
elem.updateData = _ => {
purge(previewWorld)
generate_spiral(previewWorld, data)
redraw()
}
listeners.push(elem)
},
$unlink:elem => {
listeners.splice(listeners.indexOf(elem),1)
}
},
UMG.text(
{
Font : {
FontObject : GEngine.SmallFont,
Size : 7
},
},
"SPIRAL GENERATOR PREVIEW"
)
)
let editorDesign =
UMG(PropertyEditor,
{
'slot.size.size-rule':'Fill',
OnChange: _ => {
touch()
},
$link:elem => {
elem.SetObject(data)
elem.updateData = _ => {
elem.SetObject(data)
}
listeners.push(elem)
},
$unlink:elem => {
listeners.splice(listeners.indexOf(elem),1)
}
})
let _ = require('lodash')
let editorStyle = new JavascriptStyleSet
editorStyle.StyleSetName = 'EditorStyle'
let browserDesign =
UMG.div({},
UMG(JavascriptSearchBox,
{
HintText: 'Spiral database',
OnTextChanged: text => {
filter = text
filter_listeners.forEach(filter => filter.updateFilter(filter))
}
}
),
UMG(Border,
{
'slot.size.size-rule' : 'Fill',
Background: editorStyle.GetBrush('ProjectBrowser.Background')
},
UMG(JavascriptTreeView,
{
ItemHeight: 20,
Columns: [
{
Id: 'First',
Width: 0.5
},
{
Id: 'Second',
Width: 0.5
}
],
OnGenerateRowEvent: (item, column) => {
let design =
UMG(JavascriptTextBlock,
{
Font : {
FontObject : GEngine.SmallFont,
Size : 10
},
HighlightTextDelegate : _ => filter,
Text : item ? item.desc : column
}
)
return instantiator(design)
},
OnGetChildren: (item,list) => {
list.Children = _.range(3).map(gen)
},
$link:elem => {
elem.JavascriptContext = Context
elem.EntryWidgetClass = WidgetBlueprint.Load('/Game/Blueprints/JavascriptUserObjectListEntry_BP.JavascriptUserObjectListEntry_BP').GeneratedClass
elem.proxy = {
OnSelectionChanged: item => {
data = item
listeners.forEach(listener => listener.updateData())
},
}
let items = _.range(10).map(gen)
function update() {
elem.Items = _.filter(items,item => filter == '' || item.desc.indexOf(filter) >= 0)
elem.RequestTreeRefresh()
}
update()
filter_listeners.push(elem)
elem.updateFilter = __ => {
update()
}
},
$unlink:elem => {
filter_listeners.splice(filter_listeners.indexOf(elem),1)
}
}
)
)
)
function makeTab(id,design) {
let tabs = global[id] = []
var tab = new JavascriptEditorTab
tab.TabId = id
tab.Role = 'PanelTab'
tab.DisplayName = 'Inner'
tab.OnSpawnTab = _ => {
let widget = instantiator(design)
tabs.push(widget)
return widget
}
tab.OnCloseTab = tab => {
tab.destroy()
tabs.splice(tabs.indexOf(tab),1)
}
tab.destroy = _ => {
tabs.forEach(t => tab.CloseTab(t))
tabs.length = 0
}
return tab
}
function browserTab() {
return makeTab('TestBrowserTab',browserDesign)
}
function viewportTab() {
return makeTab('TestInnerTabViewport',viewportDesign)
}
function editorTab() {
return makeTab('TestInnerTab',editorDesign)
}
let layout = JSON.stringify({
Type:'Layout',
Name:'TestLayout',
PrimaryAreaIndex: 0,
Areas: [
{
Type: 'Area',
Orientation: 'Orient_Horizontal',
WindowPlacement: 'Placement_NoWindow',
Nodes: [
{
Type: 'Stack',
SizeCoefficient : 0.3,
HideTabWell: 'true',
Tabs: [
{
TabId: 'TestBrowserTab',
TabState: 'OpenedTab'
}
]
},
{
Type: 'Splitter',
Orientation: 'Orient_Vertical',
SizeCoefficient : 0.7,
Nodes : [
{
Type: 'Stack',
SizeCoefficient : 0.5,
HideTabWell: 'true',
Tabs: [
{
TabId: 'TestInnerTabViewport',
TabState: 'OpenedTab'
}
]
},
{
Type: 'Stack',
SizeCoefficient : 0.5,
HideTabWell: 'true',
Tabs: [
{
TabId: 'TestInnerTab',
TabState: 'OpenedTab'
}
]
}
]
}
]
}
]
})
let tabManager = new JavascriptEditorTabManager(JavascriptLibrary.CreatePackage(null,'/Script/Javascript'))
tabManager.Tabs = [editorTab(),viewportTab(),browserTab()]
tabManager.Layout = layout
function makeCommands() {
let context = JavascriptMenuLibrary.NewBindingContext('SpiralGenerator','Test menu','',StyleSetName);
let commands = new JavascriptUICommands
function init() {
commands.BindingContext = context
commands.Commands = [
{
Id: 'Generate',
FriendlyName : 'Generate!',
Description : 'Hello Javascript',
ActionType : 'Button',
DefaultChord: {
Key: {
KeyName: 'B',
},
bAlt: true
}
},
{
Id : 'Purge',
FriendlyName : 'Purge',
Description : 'Hello Javascript',
ActionType : 'Button',
DefaultChord: {
Key: {
KeyName: 'A',
},
bAlt: true
}
},
{
Id: 'Connect',
FriendlyName : 'Connect',
Description : 'Hello Javascript',
ActionType : 'RadioButton'
},
{
Id: 'Disconnect',
FriendlyName : 'Disconnect',
Description : 'Hello Javascript',
ActionType : 'RadioButton'
}
]
commands.Initialize();
}
let connected = false
commands.OnExecuteAction = (what) => {
switch (what) {
case 'Connect' :
connected = true
break
case 'Disconnect' :
connected = false
break
case 'Generate' :
generate(data)
break
case 'Purge' :
clear()
break
}
}
commands.OnCanExecuteAction = (what) => {
if (what == 'Connect' && connected || what == 'Disconnect' && !connected) return false
return true
}
commands.OnIsActionChecked = (what) => {
if (what == 'Connect') {
return !connected
} else if (what == 'Disconnect') {
return connected
} else {
return true
}
}
function uninit() {
commands.Uninitialize();
context.Destroy();
}
init();
commands.destroy = uninit
return commands
}
let commands = makeCommands()
let commandList = JavascriptMenuLibrary.CreateUICommandList()
commands.Bind(commandList)
return instantiator(
UMG.div(
{
$link:elem => {
elem.AddChild(tabManager).Size.SizeRule = 'Fill'
},
$unlink:elem => {
tabManager.Tabs.forEach(tab => tab.destroy())
tabManager = null
commands.destroy()
}
},
UMG(JavascriptMultiBox,{
CommandList : commandList,
OnHook : (id,elem,builder) => {
if (id == 'Main') {
try {
JavascriptMenuLibrary.CreateMenuBarBuilder(commandList,builder => {
elem.AddPullDownMenu(builder,'Test',"TEST","TEST")
JavascriptMultiBox.Bind(builder)
})
} catch (e) {
console.error(String(e))
}
} else if (id == 'Test') {
builder.PushCommandList(commandList)
builder.BeginSection("Test Section")
builder.AddToolBarButton(commands.CommandInfos[0]);
builder.AddToolBarButton(commands.CommandInfos[1]);
elem.AddSubMenu(builder,'Sub','Sub menu','Sub menu tooltip',false)
builder.EndSection()
} else if (id == 'Sub') {
builder.PushCommandList(commandList)
builder.BeginSection("Sub Section")
builder.AddToolBarButton(commands.CommandInfos[2]);
builder.AddToolBarButton(commands.CommandInfos[3]);
builder.AddWidget(instantiator(UMG.text({TextDelegate:_ => `${Math.random().toFixed(3)}`},"TEST")),"Widget")
builder.EndSection()
} else {
console.log("UNHANDLED",id)
}
}
}),
UMG(JavascriptMultiBox,{
CommandList : commandList,
OnHook : __ => {
JavascriptMenuLibrary.CreateToolbarBuilder(commandList,'Orient_Horizontal',builder => {
builder.BeginSection("Spiral")
builder.AddToolBarButton(commands.CommandInfos[0]);
builder.AddToolBarButton(commands.CommandInfos[1]);
builder.EndSection()
builder.AddSeparator()
builder.AddToolBarButton(commands.CommandInfos[2]);
builder.AddToolBarButton(commands.CommandInfos[3]);
builder.AddWidget(instantiator(UMG.text({TextDelegate:_ => `${Math.random().toFixed(3)}`},"TEST")))
JavascriptMultiBox.Bind(builder)
})
}
})
)
)
}
module.exports = function () {
let maker = require('editor-maker')
let Icon40x40 = {X:40,Y:40}
let style = JavascriptUMGLibrary.CreateSlateStyle(StyleSetName)
style.SetContentRoot( Context.GetDir('EngineContent') + "Editor/Slate" );
style.SetCoreContentRoot( Context.GetDir('EngineContent') + "Slate" );
style.AddImageBrush("SpiralGenerator.Purge", style.RootToContentDir("Icons/icon_DevicePowerOff_40x.png"), Icon40x40, {R:1,G:1,B:1,A:1}, 'NoTile', 'FullColor' )
style.AddImageBrush("SpiralGenerator.Generate", style.RootToContentDir("Icons/icon_DevicePowerOn_40x.png"), Icon40x40, {R:1,G:1,B:1,A:1}, 'NoTile', 'FullColor' )
style.Register()
let opts = {
DisplayName: "SpiralGenerator",
TabId: "SpiralGenerator@"
}
maker.tabSpawner(opts,main);
return _ => {
style.Unregister()
}
}