1
1
using System ;
2
- using System . Collections ;
3
- using System . Collections . Generic ;
4
- using System . Linq ;
5
2
using UnityEngine ;
6
3
using UnityEngine . UIElements ;
7
4
@@ -16,30 +13,33 @@ public partial class StackView : VisualElement
16
13
int m_Index = - 1 ;
17
14
18
15
public override VisualElement contentContainer => m_ContentContainer ;
19
-
20
-
16
+
17
+
21
18
[ UxmlAttribute ]
22
19
public int index
23
20
{
24
21
get => m_Index ;
25
22
set
26
23
{
27
24
if ( m_Index == value )
25
+ {
28
26
return ;
29
-
27
+ }
28
+
30
29
m_Index = value ;
31
30
UpdateActiveViewFromIndex ( ) ;
32
31
indexChanged ? . Invoke ( m_Index ) ;
33
32
}
34
33
}
35
-
34
+
36
35
void UpdateActiveViewFromIndex ( )
37
36
{
38
37
if ( m_Index < 0 || m_Index >= childCount )
39
38
{
40
39
activeView = null ;
41
40
return ;
42
41
}
42
+
43
43
activeView = this [ m_Index ] ;
44
44
}
45
45
@@ -48,29 +48,35 @@ public VisualElement activeView
48
48
get
49
49
{
50
50
// Ensure the active view is valid. This is because children can be added/removed without being notified.
51
- bool needToEnsureValid = false ;
52
-
53
- if ( m_ActiveView == null && m_Index != - 1 && childCount > 0 )
54
- needToEnsureValid = true ;
55
- else if ( m_ActiveView != null && ( ! Contains ( m_ActiveView ) || IndexOf ( m_ActiveView ) != m_Index ) )
56
- needToEnsureValid = true ;
57
-
51
+ var needToEnsureValid = m_ActiveView == null && m_Index != - 1 && childCount > 0 ||
52
+ m_ActiveView != null && ( ! Contains ( m_ActiveView ) || IndexOf ( m_ActiveView ) != m_Index ) ;
53
+
58
54
if ( needToEnsureValid )
55
+ {
59
56
UpdateActiveViewFromIndex ( ) ;
57
+ }
58
+
60
59
return m_ActiveView ;
61
60
}
62
61
set
63
62
{
64
63
var oldView = m_ActiveView ;
65
-
64
+
66
65
if ( m_ActiveView == value )
66
+ {
67
67
return ;
68
+ }
69
+
68
70
m_ActiveView = value ;
69
-
71
+
70
72
if ( panel != null )
73
+ {
71
74
StartTransition ( oldView , m_ActiveView ) ;
75
+ }
72
76
else
77
+ {
73
78
UpdateViews ( ) ;
79
+ }
74
80
75
81
index = IndexOf ( m_ActiveView ) ;
76
82
activeViewChanged ? . Invoke ( ) ;
@@ -83,20 +89,21 @@ public VisualElement activeView
83
89
public StackView ( )
84
90
{
85
91
AddToClassList ( "lsp-stack-view" ) ;
86
-
92
+
87
93
m_ContentContainer = new VisualElement ( ) ;
88
94
m_ContentContainer . AddToClassList ( "lsp-stack-view__content-container" ) ;
89
95
m_ContentContainer . style . flexGrow = 1 ;
90
96
hierarchy . Add ( m_ContentContainer ) ;
97
+
91
98
RegisterCallback < GeometryChangedEvent > ( OnGeometryChanged ) ;
92
99
}
93
-
100
+
94
101
void StartTransition ( VisualElement from , VisualElement to )
95
102
{
96
103
if ( from != null )
97
104
{
98
105
var fadeIn = from . experimental . animation . Start (
99
- ( element ) => element . style . opacity . value ,
106
+ element => element . style . opacity . value ,
100
107
0 , 400 , ( element , value ) =>
101
108
{
102
109
element . style . opacity = value ;
@@ -112,9 +119,11 @@ void StartTransition(VisualElement from, VisualElement to)
112
119
{
113
120
to . style . display = DisplayStyle . Flex ;
114
121
to . style . opacity = 0.0f ;
122
+
115
123
var fadeOut = to . experimental . animation . Start (
116
- ( element ) => element . style . opacity . value ,
124
+ element => element . style . opacity . value ,
117
125
1 , 400 , ( element , value ) => element . style . opacity = value ) ;
126
+
118
127
fadeOut . onAnimationCompleted += ( ) =>
119
128
{
120
129
} ;
@@ -128,26 +137,24 @@ void StartTransition(VisualElement from, VisualElement to)
128
137
to . style . opacity = 1 ;
129
138
}
130
139
}
131
-
140
+
132
141
void OnGeometryChanged ( GeometryChangedEvent evt )
133
142
{
134
- if ( m_FirstGeometryChange )
143
+ if ( m_FirstGeometryChange )
135
144
{
136
145
m_FirstGeometryChange = false ;
137
146
UpdateViews ( ) ;
138
147
UnregisterCallback < GeometryChangedEvent > ( OnGeometryChanged ) ;
139
148
}
140
149
}
141
-
150
+
142
151
public void UpdateViews ( )
143
152
{
144
153
UpdateActiveViewFromIndex ( ) ;
154
+
145
155
foreach ( var view in Children ( ) )
146
156
{
147
- if ( m_ActiveView != view )
148
- view . style . display = DisplayStyle . None ;
149
- else
150
- view . style . display = DisplayStyle . Flex ;
157
+ view . style . display = m_ActiveView != view ? DisplayStyle . None : DisplayStyle . Flex ;
151
158
}
152
159
}
153
160
}
0 commit comments