-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTab.cs
103 lines (81 loc) · 3.31 KB
/
Tab.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
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
namespace DockGUI
{
public class Tab : TextElement
{
private TabLayout _tabLayout;
private DockPanel _dockPanelTarget;
private VisualElement _tabLayoutBg;
private Vector2 _originalMousePos;
private bool _isDragging = false;
private bool _mouseDown = false;
private Vector2 _mouseOffset;
private DockLayout _rootDockLayout;
private DockPanelGhost _ghost;
public TabLayout TabLayoutParent => _tabLayout;
public DockPanel DockPanelTarget => _dockPanelTarget;
// public VisualElement TargetElement => this;
public Tab(DockPanel dockPanelTarget, TabLayout tabLayout)
{
_dockPanelTarget = dockPanelTarget;
_tabLayout = tabLayout;
RegisterCallback<MouseDownEvent>(OnMouseDown);
RegisterCallback<MouseUpEvent>(OnMouseUp);
RegisterCallback<MouseMoveEvent>(OnMouseMove);
styleSheets.Add(DockGUIStyles.DefaultStyle);
AddToClassList("TabNormal");
AddToClassList("TabPadding");
AddToClassList("TabLabelNormal");
}
private void OnMouseUp(MouseUpEvent evt)
{
_mouseDown = false;
}
private void OnMouseMove(MouseMoveEvent evt)
{
if (!_mouseDown)
{
return;
}
if (Vector2.Distance(_originalMousePos, evt.mousePosition) < DockGUISettings.DRAG_THRESHOLD)
{
return;
}
// does the user want to drag
if (_tabLayout.DockPanelLayoutParent.IsFloating ||
_tabLayout.tabs.Count > 1 ||
_tabLayout.DockPanelLayoutParent.HasDockLayoutParent
)
{
float x = evt.mousePosition.x - _mouseOffset.x + DockGUI.WORLD_WINDOW_OFFSET_X;
float y = evt.mousePosition.y - _mouseOffset.y + DockGUI.WORLD_WINDOW_OFFSET_Y;
_rootDockLayout = _tabLayout.DockPanelLayoutParent.GetRootDockLayout();
_ghost = new DockPanelGhost(_dockPanelTarget, _mouseOffset);
_ghost.transform.position = new Vector3(x, y, 0);
_rootDockLayout.Add(_ghost);
DragAndDrop.StartDrag(_ghost);
// reset mouse down so it doesnt drag again after switcing mouse capture
_mouseDown = false;
// }
}
}
private void OnMouseDown(MouseDownEvent evt)
{
_tabLayout.OnTabClicked(this);
_originalMousePos = evt.originalMousePosition;
_mouseOffset = evt.localMousePosition;
_mouseDown = true;
}
public void Highlight(bool state)
{
// style.backgroundColor = state ? new StyleColor(Color.white) : new StyleColor(Color.gray);
RemoveFromClassList(state ? "TabNormal" : "TabHighlight");
AddToClassList(state ? "TabHighlight" : "TabNormal");
}
}
}