Skip to content

Commit 2ac84df

Browse files
committed
drag and drop example
1 parent 02dcae2 commit 2ac84df

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

Examples/Content/Scripts/helloJS.js

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
const _ = require('lodash')
66
const uclass = require('uclass')().bind(this,global)
77

8+
let game_mode, ui_mode
9+
810
function tutorial_WebSocket() {
911
// borrowed from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
1012
function ab2str(buf) {
@@ -56,6 +58,7 @@
5658
}
5759

5860
function tutorial_StaticMeshActor() {
61+
game_mode()
5962
console.log('creating a static mesh actor')
6063
class MySMA extends StaticMeshActor {
6164
ctor() {
@@ -68,13 +71,15 @@
6871
}
6972
let actor = new (uclass(MySMA))(GWorld,{Z:100})
7073
return _ => {
74+
ui_mode()
7175
actor.DestroyActor()
7276
}
7377
}
7478

7579
let tutorials = {
7680
'Static mesh actor' : tutorial_StaticMeshActor,
77-
'Web socket' : tutorial_WebSocket
81+
'Web socket' : tutorial_WebSocket,
82+
'Draggable' : tutorial_Draggable
7883
}
7984

8085
function Logger() {
@@ -102,29 +107,84 @@
102107
window : LogWindow
103108
}
104109
}
110+
111+
function tutorial_Draggable(root) {
112+
class MyDraggable extends JavascriptWidget {
113+
AddChild(x) {
114+
this.SetRootWidget(x)
115+
return {}
116+
}
117+
RemoveChild(x) {
118+
this.SetRootWidget(null)
119+
}
120+
OnDragOver(geom,event,op) {
121+
let abs = KismetInputLibrary.GetScreenSpacePosition(event)
122+
let local = SlateBlueprintLibrary.AbsoluteToLocal(geom,abs)
123+
return WidgetBlueprintLibrary.Handled()
124+
}
125+
OnDragDetected() {
126+
return {
127+
$: WidgetBlueprintLibrary.Handled(),
128+
Operation: WidgetBlueprintLibrary.CreateDragDropOperation(null)
129+
}
130+
}
131+
OnMouseButtonDown(geom,event) {
132+
return WidgetBlueprintLibrary.DetectDragIfPressed(event,this,{KeyName:'LeftMouseButton'})
133+
}
134+
}
135+
class MyDropTarget extends JavascriptWidget {
136+
AddChild(x) {
137+
this.SetRootWidget(x)
138+
return {}
139+
}
140+
RemoveChild(x) {
141+
this.SetRootWidget(null)
142+
}
143+
OnDrop(x) {
144+
console.log('dropped',x)
145+
}
146+
}
147+
let MyDraggable_C = uclass(MyDraggable)
148+
let MyDropTarget_C = uclass(MyDropTarget)
149+
let widget = root.add_child(
150+
UMG.div({},
151+
UMG(MyDraggable_C,{},
152+
UMG(Border,{BrushColor:{A:0.5}},"X")
153+
),
154+
UMG(MyDropTarget_C,{},
155+
UMG(Border,{BrushColor:{R:1,A:0.5}},"Drop target")
156+
)
157+
)
158+
)
159+
return _ => {
160+
root.remove_child(widget)
161+
}
162+
}
105163

106164
function app() {
107165
let logger = Logger()
108166

109-
let cur
167+
let cur, root
110168
return UMG.div({$link:elem => {
111169
elem.bye = _ => {
112170
console.log('done')
113171
cur && cur()
114172
logger.destroy()
115173
}
174+
cur = tutorial_Draggable(root)
116175
}},
117176
_.map(tutorials,(v,k) =>
118177
UMG(Button,{OnClicked:_ => {
119178
cur && cur()
120-
cur = v()
179+
cur = v(root)
121180
}},UMG.text({},k)
122181
)),
182+
UMG.div({$link:elem => root = elem}),
123183
UMG(Border,{
124184
'Slot.Size.Rule':'Fill',
125185
'Slot.VerticalAlignment':'VAlign_Bottom',
126186
'BrushColor':{A:0.5}
127-
},
187+
},
128188
logger.window()
129189
)
130190
)
@@ -145,7 +205,19 @@
145205

146206
widget.SetRootWidget(page)
147207
widget.AddToViewport()
148-
208+
209+
ui_mode = _ => {
210+
PlayerController.C(PC).bShowMouseCursor = true
211+
PlayerController.C(PC).SetInputMode_UIOnly(widget,false)
212+
}
213+
214+
game_mode = _ => {
215+
PlayerController.C(PC).bShowMouseCursor = true
216+
PlayerController.C(PC).SetInputMode_GameAndUI(widget,false,false)
217+
}
218+
219+
ui_mode()
220+
149221
return function () {
150222
page.bye()
151223
widget.RemoveFromViewport()

0 commit comments

Comments
 (0)