Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 3f9f23f

Browse files
committed
[Sample] Add a Drag and Drop Sample
1 parent bc20316 commit 3f9f23f

File tree

2 files changed

+296
-0
lines changed

2 files changed

+296
-0
lines changed
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
using System.Collections.Generic;
2+
using Unity.UIWidgets.animation;
3+
using Unity.UIWidgets.editor;
4+
using Unity.UIWidgets.foundation;
5+
using Unity.UIWidgets.material;
6+
using Unity.UIWidgets.painting;
7+
using Unity.UIWidgets.rendering;
8+
using Unity.UIWidgets.widgets;
9+
using UnityEditor;
10+
using UnityEngine;
11+
using Color = Unity.UIWidgets.ui.Color;
12+
using Transform = UnityEngine.Transform;
13+
14+
namespace UIWidgetsSample {
15+
public class DragNDropSample : UIWidgetsEditorWindow {
16+
[MenuItem("UIWidgetsTests/Drag&Drop/DragNDropSample")]
17+
public static void ShowEditorWindow() {
18+
var window = GetWindow<DragNDropSample>();
19+
window.titleContent.text = "DragNDropSample";
20+
}
21+
22+
protected override Widget createWidget() {
23+
Debug.Log("[ WIDGET CREATED ]");
24+
return new WidgetsApp(
25+
home: new DragNDropSampleWidget(),
26+
pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) =>
27+
new PageRouteBuilder(
28+
settings: settings,
29+
pageBuilder: (BuildContext context, Animation<float> animation,
30+
Animation<float> secondaryAnimation) => builder(context)
31+
)
32+
);
33+
}
34+
}
35+
36+
public class DragNDropSampleWidget : StatefulWidget {
37+
public DragNDropSampleWidget(Key key = null) : base(key) {
38+
}
39+
40+
public override State createState() {
41+
return new DragNDropSampleWidgetState();
42+
}
43+
}
44+
45+
public class DragNDropSampleWidgetState : State<DragNDropSampleWidget> {
46+
readonly Color highlightColor = Color.fromARGB(255, 88, 127, 219);
47+
readonly Color defaultColor = Color.fromARGB(255, 211, 211, 211);
48+
readonly List<bool> isHighlighted = new List<bool> { };
49+
readonly List<Object[]> objects = new List<Object[]>();
50+
51+
List<Widget> getUnityObjectDetectorList(int count) {
52+
if (this.isHighlighted.isEmpty()) {
53+
for (int i = 0; i < count; i++) {
54+
this.isHighlighted.Add(false);
55+
}
56+
}
57+
58+
if (this.objects.isEmpty()) {
59+
for (int i = 0; i < count; i++) {
60+
this.objects.Add(null);
61+
}
62+
}
63+
64+
List<Widget> widgetList = new List<Widget>();
65+
widgetList.Add(this.getGapBox("Use UnityObjectDetector"));
66+
67+
for (int i = 0; i < count; i++) {
68+
var _i = i;
69+
70+
Widget widget = new Container(
71+
decoration: this.isHighlighted[_i]
72+
? new BoxDecoration(color: this.highlightColor)
73+
: new BoxDecoration(color: this.defaultColor),
74+
height: 100f,
75+
child: new UnityObjectDetector(
76+
onEnter: () => {
77+
Debug.Log("Widget " + _i + " onEnter");
78+
this.setState(() => { this.isHighlighted[_i] = true; });
79+
},
80+
onRelease: (details) => {
81+
Debug.Log("Widget " + _i + " onRelease");
82+
this.setState(() => {
83+
this.isHighlighted[_i] = false;
84+
this.objects[_i] = details.objectReferences;
85+
});
86+
},
87+
onExit: () => {
88+
Debug.Log("Widget " + _i + " onExit");
89+
this.setState(() => { this.isHighlighted[_i] = false; });
90+
},
91+
child: new Center(
92+
child: new Text(this.objects[_i] != null
93+
? this.getNameString(this.objects[_i])
94+
: "[Drop/Multi-Drop Here]")
95+
)
96+
)
97+
);
98+
99+
widgetList.Add(widget);
100+
if (_i != count - 1) {
101+
widgetList.Add(this.getGapBox());
102+
}
103+
}
104+
105+
return widgetList;
106+
}
107+
108+
string getNameString(Object[] objs) {
109+
var str = "";
110+
for (int i = 0; i < objs.Length; i++) {
111+
str += "[" + objs[i].name + "]";
112+
if (i != objs.Length - 1) {
113+
str += "\n";
114+
}
115+
}
116+
117+
return str;
118+
}
119+
120+
Widget getGapBox(string str = "") {
121+
return new Container(
122+
height: 25,
123+
child: str == ""
124+
? null
125+
: new Center(
126+
child: new Text(str)
127+
)
128+
);
129+
}
130+
131+
Widget getSizedTableCell(string text) {
132+
return new Container(
133+
height: 20f,
134+
child: new Center(
135+
child: new Text(text)
136+
)
137+
);
138+
}
139+
140+
bool highlight = false;
141+
Object[] objRef;
142+
143+
bool isTransformDisplayHighlighted = false;
144+
Transform transformRef;
145+
146+
public override Widget build(BuildContext context) {
147+
var columnList = new List<Widget>();
148+
149+
columnList.AddRange(this.getUnityObjectDetectorList(2));
150+
columnList.AddRange(
151+
new List<Widget> {
152+
this.getGapBox("Use Listener"),
153+
new Container(
154+
decoration: this.highlight
155+
? new BoxDecoration(color: this.highlightColor)
156+
: new BoxDecoration(color: this.defaultColor),
157+
height: 100f,
158+
child: new Listener(
159+
onPointerDragFromEditorEnter: (evt) => {
160+
this.setState(() => { this.highlight = true; });
161+
},
162+
onPointerDragFromEditorExit: (evt) => {
163+
this.setState(() => { this.highlight = false; });
164+
},
165+
// onPointerDragFromEditorHover: (evt) => { },
166+
onPointerDragFromEditorRelease: (evt) => {
167+
this.objRef = evt.objectReferences;
168+
this.setState(() => { this.highlight = false; });
169+
},
170+
child: new Center(
171+
child: new Text(this.objRef != null
172+
? this.getNameString(this.objRef)
173+
: "[Drop/Multi-Drop Here]")
174+
)
175+
)
176+
)
177+
}
178+
);
179+
columnList.AddRange(
180+
new List<Widget>() {
181+
this.getGapBox("Sample: Display Transform"),
182+
new Container(
183+
decoration: this.isTransformDisplayHighlighted
184+
? new BoxDecoration(color: this.highlightColor)
185+
: new BoxDecoration(color: this.defaultColor),
186+
height: 100f,
187+
child: new UnityObjectDetector(
188+
onEnter: () => { this.setState(() => { this.isTransformDisplayHighlighted = true; }); },
189+
onRelease: (details) => {
190+
this.setState(() => {
191+
this.isTransformDisplayHighlighted = false;
192+
var gameObj = details.objectReferences[0] as GameObject;
193+
if (gameObj) {
194+
this.transformRef = gameObj.GetComponent<Transform>();
195+
}
196+
else {
197+
Debug.Log("Not a GameObject");
198+
}
199+
});
200+
},
201+
onExit: () => {
202+
Debug.Log("onExit");
203+
this.setState(() => { this.isTransformDisplayHighlighted = false; });
204+
},
205+
child: new Column(
206+
children: new List<Widget> {
207+
new Container(
208+
height: 20,
209+
child: new Center(
210+
child: new Text(this.transformRef == null
211+
? "[Drop a GameObject from scene here]"
212+
: this.transformRef.name
213+
)
214+
)
215+
),
216+
new Table(
217+
border: TableBorder.all(),
218+
children: new List<TableRow> {
219+
new TableRow(
220+
children: new List<Widget> {
221+
this.getSizedTableCell(""),
222+
this.getSizedTableCell("X"),
223+
this.getSizedTableCell("Y"),
224+
this.getSizedTableCell("Z")
225+
}
226+
),
227+
new TableRow(
228+
children: new List<Widget> {
229+
this.getSizedTableCell("Position"),
230+
this.getSizedTableCell(this.transformRef == null
231+
? ""
232+
: this.transformRef.position.x.ToString()),
233+
this.getSizedTableCell(this.transformRef == null
234+
? ""
235+
: this.transformRef.position.y.ToString()),
236+
this.getSizedTableCell(this.transformRef == null
237+
? ""
238+
: this.transformRef.position.z.ToString())
239+
}
240+
),
241+
new TableRow(
242+
children: new List<Widget> {
243+
this.getSizedTableCell("Rotation"),
244+
this.getSizedTableCell(this.transformRef == null
245+
? ""
246+
: this.transformRef.eulerAngles.x.ToString()),
247+
this.getSizedTableCell(this.transformRef == null
248+
? ""
249+
: this.transformRef.eulerAngles.y.ToString()),
250+
this.getSizedTableCell(this.transformRef == null
251+
? ""
252+
: this.transformRef.eulerAngles.z.ToString())
253+
}
254+
),
255+
new TableRow(
256+
children: new List<Widget> {
257+
this.getSizedTableCell("Scale"),
258+
this.getSizedTableCell(this.transformRef == null
259+
? ""
260+
: this.transformRef.localScale.x.ToString()),
261+
this.getSizedTableCell(this.transformRef == null
262+
? ""
263+
: this.transformRef.localScale.y.ToString()),
264+
this.getSizedTableCell(this.transformRef == null
265+
? ""
266+
: this.transformRef.localScale.z.ToString())
267+
}
268+
)
269+
}
270+
)
271+
}
272+
)
273+
))
274+
}
275+
);
276+
277+
return new Container(
278+
padding: EdgeInsets.symmetric(horizontal: 25f),
279+
color: Colors.grey,
280+
child: new ListView(
281+
children: columnList
282+
));
283+
}
284+
}
285+
}

Samples/UIWidgetSample/DragNDropSample.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)