55using UnityEngine . Scripting ;
66using UnityEngine . UIElements ;
77using Unity . Samples . ScreenReader ;
8+ using UnityEditor ;
89using Button = UnityEngine . UIElements . Button ;
910
1011namespace Unity . Samples . LetterSpell
1112{
12- class UITkLetterCard : VisualElement
13+ class UITkLetterCard : AccessibleVisualElement
1314 {
1415 Label m_TextElement ;
1516 Vector2 m_Start ;
@@ -27,86 +28,75 @@ class UITkLetterCard : VisualElement
2728 // public override string GetLabel() => (owner as UITkLetterCard).text;
2829 // }
2930
30- public bool selected => cardListView ? . selectedCard == this ;
31+ private bool m_Selected ;
3132
32- public string text
33+ public bool selected
3334 {
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+ }
3645 }
3746
38- public event Action < int , int > dropped ;
39-
40- [ RegisterAccessibilityHandler ( typeof ( UITkLetterCard ) ) ]
41- [ Preserve ]
42- class AccessibleLetterCardHandler : VisualElementAccessibilityHandler
47+ void UpdateSelectedState ( )
4348 {
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+ }
5251
53- public AccessibleLetterCardHandler ( )
52+ public string text
53+ {
54+ get => m_TextElement . text ;
55+ set
5456 {
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 ;
7059 }
7160 }
7261
62+ public event Action < int , int > dropped ;
63+
7364 public void Select ( )
7465 {
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+ }
7777 }
7878
7979 public void Unselect ( )
8080 {
8181 if ( this == cardListView . selectedCard )
8282 {
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+
8489 }
8590 }
91+
8692 public UITkLetterCard ( )
8793 {
8894 m_TextElement = new Label ( ) ;
8995 Add ( m_TextElement ) ;
9096 AddToClassList ( "lsp-letter-card" ) ;
9197 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 ;
110100
111101 style . position = Position . Absolute ;
112102
@@ -146,6 +136,26 @@ public UITkLetterCard()
146136
147137 RegisterCallbacksOnTarget ( ) ;
148138 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+ } ;
149159 }
150160
151161 bool m_Animated ;
@@ -173,6 +183,22 @@ void OnAttachToPanel(AttachToPanelEvent e)
173183 // TODO: FIX ANIMATION WHEN STARTING A GAME
174184 // schedule.Execute(() => animated = true).ExecuteLater(500);
175185 }
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+
176202 protected Rect CalculatePosition ( float x , float y , float width , float height )
177203 {
178204 var rect = new Rect ( x , y , width , height ) ;
@@ -266,15 +292,6 @@ protected void OnMouseDown(MouseDownEvent e)
266292 return ;
267293 }
268294
269- if ( e . ctrlKey )
270- {
271- cardListView . selectedCard = cardListView . selectedCard == this ? null : this ;
272- }
273- else
274- {
275- cardListView . selectedCard = this ;
276- }
277-
278295 if ( m_Active )
279296 {
280297 e . StopImmediatePropagation ( ) ;
@@ -302,6 +319,8 @@ protected void OnMouseMove(MouseMoveEvent e)
302319 {
303320 if ( m_Active )
304321 {
322+ // Ensure the card is selected when we start dragging it.
323+ Select ( ) ;
305324 var diff = e . localMousePosition - m_Start ;
306325
307326 if ( ! dragging && Math . Abs ( diff . x ) > 5 )
@@ -346,6 +365,18 @@ protected void OnMouseUp(MouseUpEvent e)
346365 {
347366 if ( e . button == ( int ) MouseButton . LeftMouse )
348367 {
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+ }
349380 m_Active = false ;
350381 dragging = false ;
351382
@@ -651,10 +682,14 @@ public UITkLetterCard selectedCard
651682 return ;
652683 }
653684
654- m_SelectedCard ? . RemoveFromClassList ( "selected" ) ;
685+ if ( m_SelectedCard != null )
686+ m_SelectedCard . selected = false ;
687+
655688 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 ;
658693 }
659694 }
660695
0 commit comments