-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathextension-exampleWindow.js
More file actions
555 lines (496 loc) · 18.8 KB
/
Copy pathextension-exampleWindow.js
File metadata and controls
555 lines (496 loc) · 18.8 KB
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
/// <reference path="typings/ue.d.ts" />
"use strict"
const I = require('instantiator')
const _ = require('lodash')
const UMG = require('UMG')
function packageRegistryService() {
const repositoryUrl = 'https://raw.githubusercontent.com/ncsoft/Unreal.js-packages/master/repository.json'
const svnPath = `${Context.GetDir('Engine')}/Binaries/ThirdParty/svn/${JavascriptProcess.GetString('BinariesSubdirectory')}/svn`
const request = require('request')
let workDir = Context.GetDir('GameContent')+'/Scripts/Downloaded'
function makeDir() {
JavascriptLibrary.MakeDirectory(workDir,false)
return Promise.resolve()
}
function packageDetail(p) {
let {name,details} = p
let split = details.lastIndexOf('/')
let packageDir = workDir + details.substr(split)
function install() {
return makeDir()
.then(_ => new Promise(resolve => {
let p = JavascriptProcess.Create(
svnPath,
`co ${details}`,
false,
false,
false,0,workDir)
function tick() {
if (!p.IsRunning()) {
resolve()
return
}
process.nextTick(tick)
}
tick()
})
)
}
function remove() {
return new Promise((resolve,reject) => {
if (JavascriptLibrary.DeleteDirectory(packageDir,true,true)) {
resolve()
} else {
reject(new Error('delete failed'))
}
})
}
return {
packageDir : packageDir,
details : details,
name : name,
installed : JavascriptLibrary.DirectoryExists(packageDir),
install : install,
remove : remove
}
}
function fetchPackages() {
return request("GET",repositoryUrl)
.then(repository => repository.packages.map(packageDetail))
}
return {
fetch: fetchPackages
}
}
function makeContext(opts) {
let selectedItem
function makeCommands() {
let context = JavascriptMenuLibrary.NewBindingContext(opts.Id + extensionId,'Test menu','','EditorStyle');
let commands = new JavascriptUICommands
let hasPendingExecution
function init() {
commands.BindingContext = context
commands.Commands = opts.Commands
commands.Initialize()
}
commands.OnExecuteAction = (what) => {
hasPendingExecution = true
selectedItem.actions[what]()
.then(_ => console.log(`${what} completed`))
.catch(e => console.error(String(e)))
.then(_ => hasPendingExecution = false)
}
commands.OnCanExecuteAction = (what) => {
return !hasPendingExecution && selectedItem && !!selectedItem.actions[what]
}
function uninit() {
commands.Uninitialize()
context.Destroy()
}
init()
commands.destroy = uninit
return commands
}
let commands = makeCommands()
let commandList = JavascriptMenuLibrary.CreateUICommandList()
commands.Bind(commandList)
return {
commands : commands,
commandList : commandList,
setCurrent : what => selectedItem = what,
destroy : _ => {
commands.destroy()
},
contextMenu : () => I(
UMG(JavascriptMultiBox,{
CommandList : commandList,
OnHook : __ => {
JavascriptMenuLibrary.CreateMenuBuilder(commandList,true,builder => {
opts.Commands.forEach((v,k) => {
builder.AddToolBarButton(commands.CommandInfos[k])
})
JavascriptMultiBox.Bind(builder)
})
}
})
)
}
}
const githubUrl = 'https://api.github.com/repos/ncsoft/Unreal.js'
const homepageUrl = "https://github.com/ncsoft/Unreal.js"
let extensionId = global.$extensionUnreal = (global.$extensionUnreal || 0) + 1
function main() {
let registry = packageRegistryService()
let {EventEmitter} = require('events')
let font = {
Size:10,
FontObject:Root.GetEngine().SmallFont
}
const request = require('request')
const style = new JavascriptStyleSet
style.StyleSetName = 'EditorStyle'
function fetchGithub() {
return request('GET',githubUrl)
}
function getNumClassesExported() {
return _.filter(_.keys(global),x=> global[x] && !!global[x].StaticClass).length
}
function packageToObject(p,E) {
let o = new JavascriptObject()
o.package = p
let details = p.details
o.installed = p.installed
function refreshPackages() {
E.refreshPackages()
return Promise.resolve()
}
function installPackage() {
var note = new JavascriptNotification
note.Text = `Installing ${details}`
note.bFireAndForget = false
note.bUseImage = true
note.Image = style.GetBrush('LevelEditor.Build')
note.Pending()
note.Fire()
return o.package.install()
.then(refreshPackages)
.then(_ => {
note.Success()
note.Fadeout()
})
.catch(e => {
note.Fail()
note.Fadeout()
console.error(String(e),e.stack)
throw e
})
}
function removePackage() {
return p.remove().then(refreshPackages)
}
o.actions = {
install: installPackage,
uninstall : removePackage
}
if (o.installed) {
delete o.actions.install
} else {
delete o.actions.uninstall
}
return o
}
let contexts = makeContext({
Id:'UnrealJS_Contexts',
Commands:[
{
Id: 'debug',
FriendlyName : 'Set as debug context',
Description : 'Set as debug context',
ActionType : 'Button'
},
{
Id: 'undebug',
FriendlyName : 'Reset to normal context',
Description : 'Reset to normal context',
ActionType : 'Button'
}
]
})
let packages = makeContext({
Id:'UnrealJS_Packages',
Commands: [
{
Id: 'install',
FriendlyName : 'Install this package',
Description : 'Install this package',
ActionType : 'Button'
},
{
Id: 'uninstall',
FriendlyName : 'Remove this package',
Description : 'Remove this package',
ActionType : 'Button'
}
]
})
let throbber
makeWindow("$window",
{
SizingRule:'AutoSized',
Title:'Unreal.js'
})(finish => {
let E = new EventEmitter()
E.refreshPackages = _ => E.emit('refreshPackages')
E.refreshContexts = _ => E.emit('refreshContexts')
let root = {}
function contextList() {
return UMG(JavascriptListView,{
ItemHeight:20,
Columns:[
{
Id: 'Name',
Width: 0.7
},
{
Id: 'Status',
Width: 0.3
}
],
OnContextMenuOpening: contexts.contextMenu,
OnGenerateRowEvent:(item,column) => I(
UMG.text({Font:font},column == 'Name' ?
item ? item.target : 'Context' :
item ? item.status : 'Status'
)
),
$link:elem => {
elem.JavascriptContext = Context
elem.EntryWidgetClass = WidgetBlueprint.Load('/Game/Blueprints/EntryWidget_C').GeneratedClass
elem.alive = true
elem.proxy = {
OnSelectionChanged: item => contexts.setCurrent(item)
}
let old
function refresh() {
function fetch() {
let out
{
out = JavascriptLibrary.GetObjectsOfClass(JavascriptContext, [], false, 0x10).Results
out = out.map(x => [x.GetDisplayName(),x.IsDebugContext() ? 'Debug' : ''])
let cur = _.flatten(out).join(',')
if (cur != old) {
old = cur
out = out.map(x => {
let y = new JavascriptObject()
let [a,b] = x
y.target = x[0]
y.status = x[1]
function find() {
let out = JavascriptLibrary.GetObjectsOfClass(JavascriptContext, [], false, 0x10).Results
let x = _.filter(out,x => x.GetDisplayName() == y.target)
if (x.length) {
return Promise.resolve(x[0])
} else {
return Promise.reject(new Error('not found'))
}
}
function refreshContexts() {
E.refreshContexts()
return Promise.resolve()
}
y.actions = {
debug : _ => find().then(obj=>obj.SetAsDebugContext()).then(gc).then(refreshContexts),
undebug : _ => find().then(obj=>obj.ResetAsDebugContext()).then(gc).then(refreshContexts),
}
if (y.status == 'Debug') {
delete y.actions.debug
} else {
delete y.actions.undebug
}
return y
})
} else {
out = null
}
}
gc()
return out
}
let cur = fetch()
if (cur) {
root.Contexts = elem.Items = cur
elem.RequestListRefresh()
}
}
function tick() {
if (!elem.alive) return
refresh()
//setTimeout(tick,1000)
}
elem.refresh = refresh
tick()
E.on('refreshContexts',elem.refresh)
},
$unlink:elem => {
elem.alive = false
E.removeListener('refreshContexts',elem.refresh)
}
})
}
function packageList(opts) {
return UMG.div(
{
Slot:opts.Slot
},
UMG(JavascriptListView,{
ItemHeight:20,
OnContextMenuOpening: packages.contextMenu,
EntryWidgetClass: WidgetBlueprint.Load('/Game/Blueprints/EntryWidget_C').GeneratedClass,
OnGenerateRowEvent:(item,column) => {
const isName = column == 'Name'
return I(
UMG.text(
{
Font:font,
ToolTipText: isName && item ? item.package.details : ''
},
isName ?
(item ? item.package.name : 'Package name') :
(item ? item.installed ? "Installed" : "" : 'Status')
)
)
},
Columns:[
{
Id: 'Name',
Width: 0.7
},
{
Id: 'Status',
Width: 0.3
}
],
$link:elem => {
elem.JavascriptContext = Context
elem.alive = true
elem.proxy = {
OnDoubleClick : item => item.actions.install(),
OnSelectionChanged: item => packages.setCurrent(item)
}
function refresh() {
throbber.SetVisibility('Visible')
registry.fetch().then(packages => {
if (!elem.alive) throw new Error("interrupted")
// root.Items = ... is necessary to keep these items not to be collected by GC
// because JavascriptObject has a JS object attached.
root.Items = elem.Items = packages.map(x => packageToObject(x,E))
throbber.SetVisibility('Hidden')
elem.RequestListRefresh()
})
}
process.nextTick(refresh)
elem.refresh = refresh
E.on('refreshPackages',elem.refresh)
},
$unlink:elem => {
elem.alive = false
E.removeListener('refreshPackages',elem.refresh)
}
}),
UMG(Throbber,{
'Slot.HorizontalAlignment':'HAlign_Center',
$link:elem => throbber = elem
})
)
}
return UMG(SizeBox,{WidthOverride:400},
UMG.div({Size:{Rule:'Fill'}},
UMG.span({},
UMG.text({},"Unreal.js"),
UMG.text({
Font:font,
'Slot.Size.Rule':'Fill',
'Slot.HorizontalAlignment':'HAlign_Right',
'Slot.VerticalAlignment':'VAlign_Center',
$link:elem => {
elem.alive = true
fetchGithub().then(json => {
const {stargazers_count} = json
elem.SetText(`${stargazers_count} stars`)
})
},
$unlink:elem => {
elem.alive = false
contexts.destroy()
packages.destroy()
}
})
),
UMG.text({AutoWrapText:true,Font:font,'Slot.Padding':{Left:20,Top:10}},
`Unreal.js is a plug-in which brings V8-powered Javascript into UnrealEngine4.
Copyright (c) 2016 NCSOFT Corporation
${getNumClassesExported()} classes exported`
),
UMG(SizeBox,{HeightOverride:20}),
UMG(SizeBox,{HeightOverride:100},
contextList()
),
UMG(SizeBox,{HeightOverride:200},
packageList({Slot:{
HorizontalAlignment:'HAlign_Fill',
VerticalAlignment:'VAlign_Fill'}})
),
UMG(Spacer,{'Slot.Size.Rule' : 'Fill'}),
UMG(Button,
{
WidgetStyle: style.GetButtonStyle("Credits.Button"),
OnClicked: _ => {
JavascriptProcess.LaunchURL(homepageUrl)
}
},
UMG.text({Font:font},"Visit project page")
),
UMG(Button,
{
WidgetStyle: style.GetButtonStyle("FlatButton.Dark"),
OnClicked: finish
},
UMG.text({Font:font},"Close this window!")
)
)
)
})
return _ => 0
}
function makeWindow(key,opts) {
const _ = require('lodash')
const UMG = require('UMG')
const I = require('instantiator')
if (!global[key]) {
let window
let container
let widget = I(
UMG(JavascriptWindow,_.extend(
{
$link:elem => window = elem
},opts),
UMG(SizeBox,{$link:elem => container = elem})
)
)
widget.TakeWidget().AddWindow()
let prev
function add(child) {
if (prev) {
container.remove_child(prev)
}
prev = container.add_child(child(finish))
process.nextTick(_ => window.BringToFront())
}
global[key] = add
function finish() {
if (window) {
window.RequestDestroyWindow()
window = null
global[key] = null
}
}
}
return global[key]
}
module.exports = function() {
try {
let bye
let alive = true
process.nextTick(_ => {
if (!alive) return
bye = main()
})
return _ => {
alive = false
if (bye) bye()
}
} catch (e) {
console.error(String(e),'got error')
return _ => null
}
}