5
5
using UnityEngine . Scripting ;
6
6
using UnityEngine . UIElements ;
7
7
using Unity . Samples . ScreenReader ;
8
+ using UnityEditor ;
8
9
using Button = UnityEngine . UIElements . Button ;
9
10
10
11
namespace Unity . Samples . LetterSpell
11
12
{
12
- class UITkLetterCard : VisualElement
13
+ class UITkLetterCard : AccessibleVisualElement
13
14
{
14
15
Label m_TextElement ;
15
16
Vector2 m_Start ;
@@ -27,86 +28,75 @@ class UITkLetterCard : VisualElement
27
28
// public override string GetLabel() => (owner as UITkLetterCard).text;
28
29
// }
29
30
30
- public bool selected => cardListView ? . selectedCard == this ;
31
+ private bool m_Selected ;
31
32
32
- public string text
33
+ public bool selected
33
34
{
34
- get => m_TextElement . text ;
35
- set => m_TextElement . text = value ;
35
+ get => m_Selected ;
36
+ set
37
+ {
38
+ if ( m_Selected == value )
39
+ return ;
40
+ m_Selected = value ;
41
+ if ( value )
42
+ Focus ( ) ;
43
+ UpdateSelectedState ( ) ;
44
+ }
36
45
}
37
46
38
- public event Action < int , int > dropped ;
39
-
40
- [ RegisterAccessibilityHandler ( typeof ( UITkLetterCard ) ) ]
41
- [ Preserve ]
42
- class AccessibleLetterCardHandler : VisualElementAccessibilityHandler
47
+ void UpdateSelectedState ( )
43
48
{
44
- UITkLetterCard card => ownerElement as UITkLetterCard ;
45
-
46
- public override string GetLabel ( ) => card . text ;
47
-
48
- protected override void BindToElement ( VisualElement ve )
49
- {
50
- card . m_TextElement . GetOrCreateAccessibleProperties ( ) . ignored = true ;
51
- }
49
+ EnableInClassList ( "selected" , selected ) ;
50
+ }
52
51
53
- public AccessibleLetterCardHandler ( )
52
+ public string text
53
+ {
54
+ get => m_TextElement . text ;
55
+ set
54
56
{
55
- OnSelect += ( ) =>
56
- {
57
- var letter = ownerElement as UITkLetterCard ;
58
-
59
- if ( ! letter . selected )
60
- {
61
- letter . Select ( ) ;
62
- }
63
- else
64
- {
65
- letter . Unselect ( ) ;
66
- }
67
-
68
- return true ;
69
- } ;
57
+ m_TextElement . text = value ;
58
+ accessible . label = value ;
70
59
}
71
60
}
72
61
62
+ public event Action < int , int > dropped ;
63
+
73
64
public void Select ( )
74
65
{
75
- cardListView . selectedCard = this ;
76
- OnScreenDebug . Log ( "Selected card: " + text ) ;
66
+ if ( cardListView . selectedCard != this )
67
+ {
68
+ cardListView . selectedCard = this ;
69
+
70
+ // check whether we are focused or not
71
+ var focused = this . focusController ? . focusedElement == this ;
72
+
73
+ AssistiveSupport . notificationDispatcher . SendAnnouncement ( $ "Card { text } selected. Swipe Left or Right to move the card." + ( focused ? "Or Double tap to unselect it." : "" ) ) ;
74
+
75
+ OnScreenDebug . Log ( "Selected card: " + text ) ;
76
+ }
77
77
}
78
78
79
79
public void Unselect ( )
80
80
{
81
81
if ( this == cardListView . selectedCard )
82
82
{
83
- cardListView . selectedCard = null ;
83
+ // check whether we are focused or not
84
+ var focused = this . focusController ? . focusedElement == this ;
85
+ cardListView . selectedCard = null ;
86
+
87
+ AssistiveSupport . notificationDispatcher . SendAnnouncement ( $ "Card { text } selected. Swipe Left or Right to move the card." + ( focused ? "Or Double tap to unselect it." : "" ) ) ;
88
+
84
89
}
85
90
}
91
+
86
92
public UITkLetterCard ( )
87
93
{
88
94
m_TextElement = new Label ( ) ;
89
95
Add ( m_TextElement ) ;
90
96
AddToClassList ( "lsp-letter-card" ) ;
91
97
AddToClassList ( "lsp-card-view-item" ) ;
92
-
93
- // style.transitionProperty = new StyleList<StylePropertyName>(new List<StylePropertyName>{ new StylePropertyName("left")});
94
- // style.transitionDuration = new StyleList<TimeValue>(new List<TimeValue>{new(0.5f)});
95
- /*style.marginLeft = 4;
96
- style.marginTop = 4;
97
- style.marginRight = 4;
98
- style.marginBottom = 4;
99
- */
100
- /*style.fontSize = 40;
101
- style.alignItems = Align.Center;
102
- style.justifyContent = Justify.Center;
103
-
104
- style.borderBottomLeftRadius = 6;
105
- style.borderTopLeftRadius = 6;
106
- style.borderBottomRightRadius = 6;
107
- style.borderTopRightRadius = 6;*/
108
-
109
- // style.backgroundColor = Color.white;
98
+
99
+ focusable = true ;
110
100
111
101
style . position = Position . Absolute ;
112
102
@@ -146,6 +136,26 @@ public UITkLetterCard()
146
136
147
137
RegisterCallbacksOnTarget ( ) ;
148
138
RegisterCallback < AttachToPanelEvent > ( OnAttachToPanel ) ;
139
+ RegisterCallback < FocusInEvent > ( OnFocusIn ) ;
140
+ RegisterCallback < BlurEvent > ( OnBlur ) ;
141
+
142
+ // Accessibility
143
+ m_TextElement . GetOrCreateAccessibleProperties ( ) . ignored = true ;
144
+ accessible . selected += ( ) =>
145
+ {
146
+ if ( ! selected )
147
+ {
148
+ Select ( ) ;
149
+ OnScreenDebug . Log ( "Selected card: " + text ) ;
150
+ }
151
+ else
152
+ {
153
+ OnScreenDebug . Log ( "UnSelected card: " + text ) ;
154
+ Unselect ( ) ;
155
+ }
156
+
157
+ return true ;
158
+ } ;
149
159
}
150
160
151
161
bool m_Animated ;
@@ -173,6 +183,22 @@ void OnAttachToPanel(AttachToPanelEvent e)
173
183
// TODO: FIX ANIMATION WHEN STARTING A GAME
174
184
// schedule.Execute(() => animated = true).ExecuteLater(500);
175
185
}
186
+
187
+ void OnFocusIn ( FocusInEvent e )
188
+ {
189
+ AssistiveSupport . notificationDispatcher . SendAnnouncement ( $ "Double tap to select Card { text } and start moving.") ;
190
+
191
+ OnScreenDebug . Log ( "OnFocusIn " + text ) ;
192
+ e . StopPropagation ( ) ;
193
+ }
194
+
195
+ void OnBlur ( BlurEvent e )
196
+ {
197
+ OnScreenDebug . Log ( "Un Focus " + text ) ;
198
+ accessible . hint = null ;
199
+ e . StopPropagation ( ) ;
200
+ }
201
+
176
202
protected Rect CalculatePosition ( float x , float y , float width , float height )
177
203
{
178
204
var rect = new Rect ( x , y , width , height ) ;
@@ -266,15 +292,6 @@ protected void OnMouseDown(MouseDownEvent e)
266
292
return ;
267
293
}
268
294
269
- if ( e . ctrlKey )
270
- {
271
- cardListView . selectedCard = cardListView . selectedCard == this ? null : this ;
272
- }
273
- else
274
- {
275
- cardListView . selectedCard = this ;
276
- }
277
-
278
295
if ( m_Active )
279
296
{
280
297
e . StopImmediatePropagation ( ) ;
@@ -302,6 +319,8 @@ protected void OnMouseMove(MouseMoveEvent e)
302
319
{
303
320
if ( m_Active )
304
321
{
322
+ // Ensure the card is selected when we start dragging it.
323
+ Select ( ) ;
305
324
var diff = e . localMousePosition - m_Start ;
306
325
307
326
if ( ! dragging && Math . Abs ( diff . x ) > 5 )
@@ -346,6 +365,18 @@ protected void OnMouseUp(MouseUpEvent e)
346
365
{
347
366
if ( e . button == ( int ) MouseButton . LeftMouse )
348
367
{
368
+ // Select or unselect the card if we didn't drag it.
369
+ if ( ! m_Dragging )
370
+ {
371
+ if ( selected )
372
+ {
373
+ Unselect ( ) ;
374
+ }
375
+ else
376
+ {
377
+ Select ( ) ;
378
+ }
379
+ }
349
380
m_Active = false ;
350
381
dragging = false ;
351
382
@@ -651,10 +682,14 @@ public UITkLetterCard selectedCard
651
682
return ;
652
683
}
653
684
654
- m_SelectedCard ? . RemoveFromClassList ( "selected" ) ;
685
+ if ( m_SelectedCard != null )
686
+ m_SelectedCard . selected = false ;
687
+
655
688
m_SelectedCard = value ;
656
- m_SelectedCard ? . AddToClassList ( "selected" ) ;
657
- m_SelectedCard ? . UpdateButtonEnableState ( ) ;
689
+ OnScreenDebug . Log ( "ListView selected card: " + ( m_SelectedCard != null ? m_SelectedCard . text : "null" ) ) ;
690
+
691
+ if ( m_SelectedCard != null )
692
+ m_SelectedCard . selected = true ;
658
693
}
659
694
}
660
695
0 commit comments