-
Notifications
You must be signed in to change notification settings - Fork 345
/
helloUMG.js
299 lines (273 loc) · 10.8 KB
/
helloUMG.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
let UMG = require('UMG')
const style = require('stylePreset')
function GetPC() {
return PlayerController.C(GWorld.GetAllActorsOfClass(PlayerController).OutActors[0])
}
function editor() {
let source = `
1 + 1
`
let elem
function fire() {
console.log(eval(elem.GetText()))
}
return UMG.div({'slot.size.size-rule': 'Fill'},
UMG(Button,{OnClicked:_ => fire()},"RUN"),
UMG(MultiLineEditableTextBox, {
'slot.size.size-rule': 'Fill',
WidgetStyle: {
TextStyle: {
'font.font-object' : GEngine.SmallFont,
'font.size': 20
},
ForegroundColor: style.colors.black,
FocusedForegroundColor: style.colors.black,
BackgroundColor: style.colors.gray,
BackgroundImageNormal: { TintColor: style.colors.gray },
BackgroundImageHovered: { TintColor: style.colors.gray },
BackgroundImageFocused: { TintColor: style.colors.gray },
},
Text: source,
$link: _elem => {
elem = _elem
},
$unlink: _ => {
}
})
)
}
function main() {
let widget = null
let PC = GetPC()
// create a widget
widget = GWorld.CreateWidget(JavascriptWidget, PC)
widget.JavascriptContext = Context
widget.bSupportsKeyboardFocus = true
widget.bIsFocusable = true
class TestWidget extends JavascriptWidget {
OnPaint(context) {
let start = {X:0,Y:0}
let r = 300
let t = (new Date() | 0) / 1000
let end = {X:Math.cos(t)*r,Y:Math.sin(t)*r}
PaintContext.C(context).DrawLine(start,end,{R:1,A:1},true)
}
}
let TestWidget_C = require('uclass')()(global,TestWidget)
global.x = new TestWidget_C()
const _= require('lodash')
let onMessage = null
let menu
class MyOutput extends JavascriptOutputDevice {
OnMessage(msg, verbosity, category) {
onMessage && onMessage(msg,verbosity,category)
}
}
let MyOutput_C = require('uclass')()(global,MyOutput)
let myOutput = new MyOutput_C
let history = []
function addHistoryEntry(entry) {
history.push(entry)
}
function setSuggestions(elements, is_history) {
if (menu) {
console.log('sug')
menu.Open(true)
}
console.log('?')
}
let selectedSuggestion = -1
let suggestions = []
function markActiveSuggestion() {
}
class MyConsole extends JavascriptWidget {
AddChild(x) {
console.log('add child')
this.SetRootWidget(x)
return {}
}
RemoveChild(x) {
this.SetRootWidget(null)
}
OnPreviewKeyDown(geom,key) {
let K = KeyEvent.C(key).GetKey().KeyName
if (menu.IsOpen()) {
if (K == 'Up' || K == 'Down') {
if (K == 'Up') {
if (selectedSuggestion < 0) {
selectedSuggestion = suggestions.length - 1
} else {
selectedSuggestion--
}
} else if (K == 'Down') {
if (selectedSuggestion < suggestions.length - 1) {
++selectedSuggestion
} else {
selectedSuggestion = -1
}
}
markActiveSuggestion()
return EventReply.Handled()
} else if (K == 'Tab') {
if (suggestions.length) {
if (selectedSuggestion >= 0 && selectedSuggestion < suggestions.length) {
markActiveSuggestion()
//COMMIT
} else {
selectedSuggestion = 0
markActiveSuggestion()
}
}
return EventReply.Handled()
}
}
if (K == 'Up') {
setSuggestions(history,true)
if (suggestions.length) {
selectedSuggestion = suggestions.length - 1
markActiveSuggestion()
}
return EventReply.Handled()
}
return EventReply.Unhandled()
}
}
let MyConsole_C = require('uclass')()(global,MyConsole)
// low-level json which describes UMG structure to set up.
let design = UMG.span({},
UMG(MyConsole_C,{'slot.size.size-rule':'Fill'},
UMG.div({},
UMG(JavascriptMultiLineEditableTextBox, {
'slot.size.size-rule':'Fill',
AlwaysShowScrollbars:true,
IsReadOnly:true,
$link:elem =>{
let layout
let style = TextBlockStyle({
Font:{
FontObject : GEngine.SmallFont,
Size:20
},
ColorAndOpacity:{
SpecifiedColor:{R:1,G:1,B:1,A:1}
}
})
let style2 = TextBlockStyle({
Font:{
FontObject : GEngine.SmallFont,
Size:20
},
ColorAndOpacity:{
SpecifiedColor:{R:1,G:1,B:1,A:0.5}
}
})
elem.SetTextDelegate = (text,_layout) => {
layout = _layout
}
let timer = null
let userScrolled = false
elem.OnVScrollBarUserScrolled = offset => {
userScrolled = (offset < 1 - 1e-5)
}
let lines = 0
onMessage = (msg,v,category) => {
let str = [category,msg].join(':')
let model = new JavascriptTextModel()
model.SetString(str)
if (!layout) return
layout.AddLine(model,[
model.CreateRun(style2,0,category.length),
model.CreateRun(style,category.length,str.length)
])
lines++
if (!userScrolled) {
elem.ScrollTo(lines-1)
}
}
process.nextTick(_ => {
console.log('welcome UMG');
console.log("here you are")
})
},
$unlink: _ => {
onMessage = null
}
}),
UMG(MenuAnchor,
{
$link:elem => {
menu = elem
elem.OnGetMenuContentEvent = _ => {
return instantiator(
UMG(Border,{BrushColor:{R:0.2,A:0.2}},
UMG(JavascriptListView,{
OnGenerateRowEvent:(row) => {
return instantiator(
UMG.text({},"ROW!")
)
},
$link:elem => {
elem.JavascriptContext = Context
elem.EntryWidgetClass = WidgetBlueprint.Load('/Game/Blueprints/JavascriptUserObjectListEntry_BP.JavascriptUserObjectListEntry_BP').GeneratedClass
elem.proxy = {}
elem.proxy.OnSelectionChanged = (row) => {
console.log('selection')
}
elem.Items = [GWorld,PC]
}
})
)
)
}
}
},
UMG(EditableTextBox,
{
WidgetStyle:{'font.size':20},
HintText:'Enter javascript command',
ClearKeyboardFocusOnCommit:false,
IsCaretMovedWhenGainFocus:false,
OnTextChanged:text => {
},
$link:elem => {
elem.OnTextCommitted = text => {
addHistoryEntry(text)
console.log(text)
elem.SetText('')
console.log(eval(text))
}
}
}
)
)
)
),
UMG.div({'slot.size.size-rule':'Fill'},
editor(),
UMG(Button,{'slot.size.size-rule':'Fill'},UMG(TestWidget_C,{}))
)
)
let instantiator = require('instantiator')
let page = instantiator(design)
page.Visibility = 'Visible'
widget.SetRootWidget(page)
widget.AddToViewport()
// Switch PC to UI only mode.
PC.bShowMouseCursor = true
WidgetBlueprintLibrary.SetInputMode_UIOnlyEx(PC,widget,'DoNotLock',false)
return function () {
onMessage = null
myOutput = null
widget.RemoveFromViewport()
}
}
try {
module.exports = () => {
let cleanup = null
process.nextTick(() => cleanup = main());
return () => cleanup()
}
}
catch (e) {
require('bootstrap')('helloUMG')
}