-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockPanelGhost.cs
508 lines (395 loc) · 18.2 KB
/
DockPanelGhost.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Runtime.Remoting.Messaging;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace DockGUI
{
public class DockPanelGhost : VisualElement, IDraggable
{
public enum GhostState
{
None,
TabLayout,
Window,
DockPanelLayout,
}
public enum DockingState
{
None,
Left,
Right,
Top,
Bottom
}
private GhostState _state = GhostState.None;
private DockingState _dockingState = DockingState.None;
private VisualElement _floatingWindow;
private VisualElement _floatingTab;
private VisualElement _proxyTab;
private DockLayout _rootDockLayout;
private Vector2 _mouseOffset;
// private DockTabLayout _tabLayout;
private DockPanel _targetDockPanel;
private TabLayout _dropTabLayout;
private DockPanelLayout _dropPanelLayout;
private DockPanelLayout _dockingPanelLayout;
private List<IDroppable> _droppables;
public VisualElement TargetElement => this;
private IDroppable _oldDroppable;
private Color _oldColour;
public DockPanelGhost(DockPanel dockPanel, Vector2 mouseOffset)
: this(dockPanel, mouseOffset, DockGUIStyles.DefaultStyle)
{
}
public DockPanelGhost(DockPanel dockPanel, Vector2 mouseOffset,
StyleSheet styleSheet)
{
_targetDockPanel = dockPanel;
_rootDockLayout = dockPanel.DockPanelLayoutParent.GetRootDockLayout();
_mouseOffset = mouseOffset;
_targetDockPanel.DockPanelLayoutParent.TabLayout.GetTab(dockPanel).visible = false;
_targetDockPanel.DockPanelLayoutParent.TabLayout.GetTab(dockPanel).style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
// _targetPanel.DockLayoutParent.visible = false;
_droppables = DragAndDrop.GetAllDroppables(_rootDockLayout);
// create ghost panel ----
styleSheets.Add(styleSheet);
AddToClassList("GhostLayout");
_floatingWindow = DockGUI.CreateVisualElement(this, styleSheet, "FloatingLayout", "DockLayoutBg");
VisualElement floatingWindowTabLayout =
DockGUI.CreateVisualElement(_floatingWindow, styleSheet, "TabLayoutHeight", "TabLayout");
DockGUI.CreateVisualElement(floatingWindowTabLayout, styleSheet, "TabLayoutBg");
VisualElement floatingWindowTab =
DockGUI.CreateVisualElement(floatingWindowTabLayout, styleSheet, "TabHighlight", "TabPadding");
floatingWindowTab.Add(new Label(dockPanel.Title));
DockGUI.CreateVisualElement(_floatingWindow, styleSheet).style.flexGrow = new StyleFloat(1);
_floatingWindow.style.opacity = new StyleFloat(0.5f);
_floatingWindow.visible = false;
// -----------------------
// create ghost tab ------
_floatingTab = DockGUI.CreateVisualElement(this, styleSheet, "TabLayoutHeight", "TabLayout");
VisualElement _floatingTabButton =
DockGUI.CreateVisualElement(_floatingTab, styleSheet, "TabHighlight", "TabPadding");
_floatingTabButton.Add(new Label(dockPanel.Title));
// -----------------------
// create proxy tab
_proxyTab = DockGUI.CreateVisualElement(this, styleSheet, "TabLayoutHeight", "TabLayout");
VisualElement _proxyTabButton =
DockGUI.CreateVisualElement(_proxyTab, styleSheet, "TabHighlight", "TabPadding");
_proxyTabButton.Add(new Label(dockPanel.Title));
Tab tab = dockPanel.DockPanelLayoutParent.TabLayout.GetTab(dockPanel);
_proxyTab.visible = false;
_proxyTab.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
dockPanel.DockPanelLayoutParent.TabLayout.Insert(dockPanel.DockPanelLayoutParent.TabLayout.IndexOf(tab), _proxyTab);
RegisterCallback<MouseUpEvent>(OnMouseUp);
RegisterCallback<MouseMoveEvent>(OnMouseMove);
}
private void OnMouseMove(MouseMoveEvent evt)
{
if (DragAndDrop.State != DragAndDrop.DragAndDropState.Dragging && DragAndDrop.DraggingObject != this)
{
return;
}
float x = evt.mousePosition.x - _mouseOffset.x + DockGUI.WORLD_WINDOW_OFFSET_X;
float y = evt.mousePosition.y - _mouseOffset.y + DockGUI.WORLD_WINDOW_OFFSET_Y;
List<IDroppable> droppablesUnderMouse = DragAndDrop.GetDroppablesUnderMouse(evt.mousePosition);
IDroppable droppable = droppablesUnderMouse.Count > 0 ? droppablesUnderMouse[droppablesUnderMouse.Count - 1] : null;
if (_oldDroppable != null)
{
_oldDroppable.TargetElement.style.backgroundColor = new StyleColor(_oldColour);
}
_oldDroppable = droppable;
if (droppable != null)
{
droppable.TargetElement.style.backgroundColor = new StyleColor(Color.magenta);
}
switch (_state)
{
case GhostState.TabLayout:
DragAndDrop.RefreshDroppables();
if (droppable != null && droppable == _dropTabLayout)
{
OnTabLayoutUpdate(new Vector2(x, y));
}
else
{
OnTabLayoutLeave(droppable, evt);
}
break;
case GhostState.DockPanelLayout:
if (droppable != null && droppable == _dropPanelLayout)
{
OnDockPanelLayoutUpdate(new Vector2(x, y));
}
else
{
OnDockPanelLayoutLeave();
}
break;
default:
float minX = 0;
float minY = 0;
float maxX = _rootDockLayout.worldBound.size.x;
float maxY = _rootDockLayout.worldBound.size.y;
// List<IDroppable> droppablesUnderMouse = DragAndDrop.GetDroppablesUnderMouse(evt.mousePosition);
if (droppable != null)
{
if (droppable.GetType() == typeof(TabLayout))
{
OnTabLayoutEnter((TabLayout)droppable);
return;
}
if (droppable.GetType() == typeof(DockPanelLayout))
{
OnDockPanelLayoutEnter((DockPanelLayout) droppable);
return;
}
_state = GhostState.None;
}
UpdatePosition(new Vector2(x, y), new Vector2(minX, minY), new Vector2(maxX, maxY));
break;
}
}
private void OnDockPanelLayoutLeave()
{
_state = GhostState.None;
_dockingState = DockingState.None;
}
private void FreeFloat()
{
_floatingWindow.style.height = 200;
_floatingWindow.style.width = 200;
_dockingState = DockingState.None;
UnsetDockingClass();
}
private void DockLeft()
{
_floatingWindow.style.height = new StyleLength(_dropPanelLayout.worldBound.height);
_floatingWindow.style.width = new StyleLength(_dropPanelLayout.worldBound.width * 0.3f);
_floatingWindow.style.opacity = new StyleFloat(1);
_floatingWindow.transform.position = new Vector3(0, 0, 0);
transform.position = _dropPanelLayout.worldBound.position + new Vector2(0, DockGUI.WORLD_WINDOW_OFFSET_Y);
_dockingState = DockingState.Left;
SetDockingClass(_floatingWindow, DockGUIStyles.DockingGhostLeft);
}
private void SetDockingClass(VisualElement element, string styleClass)
{
foreach (var styleName in DockGUIStyles.DockingGhostStyleNames())
{
if (styleName == styleClass)
{
continue;
}
if (element.ClassListContains(styleName))
{
element.RemoveFromClassList(styleName);
}
}
if (!element.ClassListContains(styleClass))
{
element.AddToClassList(styleClass);
}
}
public void UnsetDockingClass()
{
foreach (var styleName in DockGUIStyles.DockingStyleNames())
{
if (ClassListContains(styleName))
{
RemoveFromClassList(styleName);
}
}
}
private void OnDockPanelLayoutUpdate(Vector2 mouseWithOffsetPosition)
{
if (mouseWithOffsetPosition.x < _dropPanelLayout.worldBound.position.x + (_dropPanelLayout.worldBound.size.x * DockGUI.DOCK_LAYOUT_RATIO))
{
if (_dockingState != DockingState.Left)
{
DockLeft();
}
}
else
{
if (_dockingState != DockingState.None)
{
FreeFloat();
}
UpdatePosition(mouseWithOffsetPosition, Vector2.zero, _rootDockLayout.worldBound.size);
}
}
private void OnDockPanelLayoutEnter(DockPanelLayout dockPanelLayout)
{
ShowGhostPanel();
// if (dockPanelLayout != _targetPanel.DockPanelLayoutParent)
// {
_dropPanelLayout = dockPanelLayout;
_state = GhostState.DockPanelLayout;
// }
}
private void OnTabLayoutEnter(TabLayout tabLayout)
{
_state = GhostState.TabLayout;
_dropTabLayout = tabLayout;
if (!_dropTabLayout.Contains(_proxyTab))
{
// add the panel with an invisible tab to the docklayout for inserting between already existing tabs
_dropTabLayout.Add(_proxyTab);
_proxyTab.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
}
ShowGhostTab();
}
private void OnTabLayoutUpdate(Vector2 mousePositionWithOffset)
{
Vector2 min;
Vector2 max;
min.x = _dropTabLayout.worldBound.x;
min.y = _dropTabLayout.worldBound.y + DockGUI.WORLD_WINDOW_OFFSET_Y;
max.x = min.x + _dropTabLayout.TargetElement.worldBound.width;
max.y = min.y + _dropTabLayout.TargetElement.worldBound.height;
UpdatePosition(new Vector2(mousePositionWithOffset.x, mousePositionWithOffset.y), new Vector2(min.x, min.y), new Vector2(max.x, max.y));
int i = -1;
// check if the mouse position is over another tab and insert the targetPanel tab there instead
foreach (var tab in _dropTabLayout.tabs)
{
i++;
// if (tab.DockPanelTarget == _targetDockPanel)
// {
// continue;
// }
// use minX instead of center for the first tab
// float tabX = i > 0 ? tab.worldBound.center.x : tab.worldBound.x;
// float floatX = i > 0 ? _floatingTab.worldBound.center.x : _floatingTab.worldBound.x;
float tabX = tab.worldBound.x;
float floatX = _floatingTab.worldBound.x;
if (Mathf.Abs(tabX - floatX) < DockGUI.REARRANGE_TAB_DISTANCE)
{
if (tab.DockPanelTarget != _targetDockPanel)
{
_dropTabLayout.Insert(_dropTabLayout.IndexOf(tab), _proxyTab);
}
break;
}
}
}
private void OnTabLayoutLeave(IDroppable droppable, MouseMoveEvent evt)
{
_state = GhostState.None;
var x = evt.mousePosition.x;
var y = evt.mousePosition.y;
// _targetPanel.FreeFloat(x, y);
// _targetPanel.DockPanelLayoutParent.visible = false;
_dropTabLayout = null;
DragAndDrop.RefreshDroppables();
ShowGhostPanel();
}
private void UpdatePosition(Vector2 position, Vector2 min, Vector2 max)
{
if (position.x < min.x)
{
position.x = min.x;
}
else if (position.x + worldBound.size.x > max.x)
{
position.x = max.x - worldBound.size.x;
}
if (position.y < min.y)
{
position.y = min.y;
}
else if (position.y + worldBound.size.y > max.y)
{
position.y = max.y - worldBound.size.y;
}
transform.position = new Vector3(position.x, position.y, transform.position.z);
}
private void ShowGhostTab()
{
_floatingWindow.style.visibility = new StyleEnum<Visibility>(Visibility.Hidden);
_floatingTab.style.visibility = new StyleEnum<Visibility>(Visibility.Visible);
}
private void ShowGhostPanel()
{
_floatingWindow.style.visibility = new StyleEnum<Visibility>(Visibility.Visible);
_floatingTab.style.visibility = new StyleEnum<Visibility>(Visibility.Hidden);
}
private void HideAll()
{
_floatingWindow.style.visibility = new StyleEnum<Visibility>(Visibility.Hidden);
_floatingTab.style.visibility = new StyleEnum<Visibility>(Visibility.Hidden);
}
private void OnMouseUp(MouseUpEvent evt)
{
Debug.Log(_state);
switch (_state)
{
case GhostState.TabLayout:
int index = _proxyTab.parent.IndexOf(_proxyTab) - 1;
if (!_dropTabLayout.DockPanelLayoutParent.ContainsPanel(_targetDockPanel))
{
_dropTabLayout.DockPanelLayoutParent.AddPanel(_targetDockPanel);
}
Tab tab = _dropTabLayout.GetTab(_targetDockPanel);
tab.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
_dropTabLayout.MoveTab(index, tab);
_dropTabLayout.Select(_targetDockPanel);
break;
case GhostState.DockPanelLayout:
if (_dockingState == DockingState.None)
{
_targetDockPanel.DockPanelLayoutParent.transform.position = transform.position;
}
else
{
Debug.Log("SHOULD HAVE DOCKED!?");
_targetDockPanel.DockPanelLayoutParent.transform.position = Vector3.zero;
Dock(_targetDockPanel, _dropPanelLayout, _dockingState);
}
break;
default:
_targetDockPanel.DockPanelLayoutParent.transform.position = transform.position;
break;
}
_dockingPanelLayout = null;
_targetDockPanel.DockPanelLayoutParent.TabLayout.GetTab(_targetDockPanel).visible = true;
_targetDockPanel.DockPanelLayoutParent.visible = true;
_rootDockLayout.Remove(this);
_proxyTab?.parent?.Remove(_proxyTab);
_targetDockPanel = null;
_dropTabLayout = null;
if (_oldDroppable != null)
{
_oldDroppable.TargetElement.style.backgroundColor = _oldColour;
}
EndDrag();
}
private void Dock(DockPanel dockPanel, DockPanelLayout dockPanelLayout, DockingState dockingState)
{
Debug.Log("Docking: " + dockingState);
switch (dockingState)
{
case DockingState.Left:
dockPanelLayout.Dock(dockPanel, Border.Left);
break;
case DockingState.Right:
dockPanelLayout.Dock(dockPanel, Border.Right);
break;
case DockingState.Top:
dockPanelLayout.Dock(dockPanel, Border.Top);
break;
case DockingState.Bottom:
dockPanelLayout.Dock(dockPanel, Border.Bottom);
break;
default:
throw new Exception("Invalid docking state for dock: " + dockingState);
}
}
public void EndDrag()
{
this.ReleaseMouse();
}
}
}