1
1
using System . Collections . Generic ;
2
+ using Unity . UIWidgets . foundation ;
2
3
using Unity . UIWidgets . ui ;
3
4
using UnityEngine ;
4
- using Rect = Unity . UIWidgets . ui . Rect ;
5
5
6
6
namespace Unity . UIWidgets . plugins . raycast {
7
7
public class RaycastableRect {
8
- public bool isDirty ;
9
- public Rect rect ;
8
+ bool _isDirty = true ;
10
9
11
- public RaycastableRect ( bool isDirty ) {
12
- this . isDirty = isDirty ;
10
+ public bool isDirty {
11
+ get { return this . _isDirty ; }
12
+ }
13
+
14
+ public float left ;
15
+ public float right ;
16
+ public float top ;
17
+ public float bottom ;
18
+
19
+ public void MarkDirty ( ) {
20
+ this . _isDirty = true ;
21
+ }
22
+
23
+ public void UnmarkDirty ( ) {
24
+ this . _isDirty = false ;
25
+ }
26
+
27
+ public void UpdateRect ( float left , float top , float width , float height ) {
28
+ this . left = left ;
29
+ this . right = left + width ;
30
+ this . top = top ;
31
+ this . bottom = top + height ;
32
+ }
33
+
34
+ public bool CheckInRect ( Vector2 pos ) {
35
+ return pos . x >= this . left &&
36
+ pos . x < this . right &&
37
+ pos . y >= this . top &&
38
+ pos . y < this . bottom ;
13
39
}
14
40
}
15
41
@@ -26,52 +52,66 @@ public static RaycastManager instance {
26
52
}
27
53
}
28
54
29
- public Dictionary < int , Dictionary < int , RaycastableRect > > hashCodeList =
55
+ public readonly Dictionary < int , Dictionary < int , RaycastableRect > > raycastHandlerMap =
30
56
new Dictionary < int , Dictionary < int , RaycastableRect > > ( ) ;
31
57
32
58
public static void VerifyWindow ( int windowHashCode ) {
33
- if ( ! instance . hashCodeList . ContainsKey ( windowHashCode ) ) {
34
- // Debug.Log($"New Window: @[{windowHashCode}] ({instance.hashCodeList .Count})");
35
- instance . hashCodeList . Add ( windowHashCode , new Dictionary < int , RaycastableRect > ( ) ) ;
59
+ if ( ! instance . raycastHandlerMap . ContainsKey ( windowHashCode ) ) {
60
+ Debug . Log ( $ "New Window: @[{ windowHashCode } ] ({ instance . raycastHandlerMap . Count } )") ;
61
+ instance . raycastHandlerMap . Add ( windowHashCode , new Dictionary < int , RaycastableRect > ( ) ) ;
36
62
}
37
63
}
38
64
39
- public static void AddToList ( int key , int windowHashCode ) {
65
+ public static void AddToList ( int widgetHashCode , int windowHashCode ) {
40
66
VerifyWindow ( windowHashCode ) ;
41
- // Debug.Log($"Add To List: [{key}]@[{windowHashCode}]");
42
- if ( ! instance . hashCodeList [ windowHashCode ] . ContainsKey ( key ) ) {
43
- instance . hashCodeList [ windowHashCode ] [ key ] = new RaycastableRect ( true ) ;
67
+ Debug . Log ( $ "Add To List: [{ widgetHashCode } ]@[{ windowHashCode } ]") ;
68
+ if ( ! instance . raycastHandlerMap [ windowHashCode ] . ContainsKey ( widgetHashCode ) ) {
69
+ instance . raycastHandlerMap [ windowHashCode ] [ widgetHashCode ] = new RaycastableRect ( ) ;
70
+
44
71
}
45
72
}
46
73
47
- public static void MarkDirty ( int key , int windowHashCode ) {
48
- // Debug.Log($"Mark Dirty: [{key}]@[{windowHashCode}]");
49
- if ( instance . hashCodeList [ windowHashCode ] . ContainsKey ( key ) ) {
50
- instance . hashCodeList [ windowHashCode ] [ key ] . isDirty = true ;
51
- }
74
+ public static void MarkDirty ( int widgetHashCode , int windowHashCode ) {
75
+ Debug . Log ( $ "Mark Dirty: [{ widgetHashCode } ]@[{ windowHashCode } ]") ;
76
+ D . assert ( instance . raycastHandlerMap . ContainsKey ( windowHashCode ) , ( ) =>
77
+ $ "Raycast Handler Map doesn't contain Window { windowHashCode } ") ;
78
+ D . assert ( instance . raycastHandlerMap [ windowHashCode ] . ContainsKey ( widgetHashCode ) , ( ) =>
79
+ $ "Raycast Handler Map doesn't contain Widget { widgetHashCode } at Window { windowHashCode } ") ;
80
+
81
+ instance . raycastHandlerMap [ windowHashCode ] [ widgetHashCode ] . MarkDirty ( ) ;
52
82
}
53
83
54
- public static void UpdateSizeOffset ( int key , int windowHashCode , Size size , Offset offset ) {
55
- // Debug.Log($"Update Size Offset: [{key}]@[{windowHashCode}]");
56
- if ( instance . hashCodeList [ windowHashCode ] . ContainsKey ( key ) ) {
57
- if ( instance . hashCodeList [ windowHashCode ] [ key ] . isDirty ) {
58
- instance . hashCodeList [ windowHashCode ] [ key ] . rect =
59
- Rect . fromLTWH ( offset . dx , offset . dy , size . width , size . height ) ;
60
- instance . hashCodeList [ windowHashCode ] [ key ] . isDirty = false ;
61
- }
84
+ public static void UpdateSizeOffset ( int widgetHashCode , int windowHashCode , Size size , Offset offset ) {
85
+
86
+ D . assert ( instance . raycastHandlerMap . ContainsKey ( windowHashCode ) , ( ) =>
87
+ $ "Raycast Handler Map doesn't contain Window { windowHashCode } ") ;
88
+ D . assert ( instance . raycastHandlerMap [ windowHashCode ] . ContainsKey ( widgetHashCode ) , ( ) =>
89
+ $ "Raycast Handler Map doesn't contain Widget { widgetHashCode } at Window { windowHashCode } ") ;
90
+
91
+ if ( instance . raycastHandlerMap [ windowHashCode ] [ widgetHashCode ] . isDirty ) {
92
+ Debug . Log ( $ "Update Size Offset: [{ widgetHashCode } ]@[{ windowHashCode } ]") ;
93
+ instance . raycastHandlerMap [ windowHashCode ] [ widgetHashCode ]
94
+ . UpdateRect ( offset . dx , offset . dy , size . width , size . height ) ;
95
+ instance . raycastHandlerMap [ windowHashCode ] [ widgetHashCode ] . UnmarkDirty ( ) ;
62
96
}
63
97
}
64
98
65
- public static void RemoveFromList ( int key , int windowHashCode ) {
66
- // Debug.Log($"Remove From List: [{key}]@[{windowHashCode}]");
67
- if ( instance . hashCodeList [ windowHashCode ] . ContainsKey ( key ) ) {
68
- instance . hashCodeList [ windowHashCode ] . Remove ( key ) ;
69
- }
99
+ public static void RemoveFromList ( int widgetHashCode , int windowHashCode ) {
100
+ Debug . Log ( $ "Remove From List: [{ widgetHashCode } ]@[{ windowHashCode } ]") ;
101
+ D . assert ( instance . raycastHandlerMap . ContainsKey ( windowHashCode ) , ( ) =>
102
+ $ "Raycast Handler Map doesn't contain Window { windowHashCode } ") ;
103
+ D . assert ( instance . raycastHandlerMap [ windowHashCode ] . ContainsKey ( widgetHashCode ) , ( ) =>
104
+ $ "Raycast Handler Map doesn't contain Widget { widgetHashCode } at Window { windowHashCode } ") ;
105
+
106
+ instance . raycastHandlerMap [ windowHashCode ] . Remove ( widgetHashCode ) ;
70
107
}
71
108
72
109
public static bool CheckCastThrough ( int windowHashCode , Vector2 pos ) {
73
- foreach ( var item in instance . hashCodeList [ windowHashCode ] ) {
74
- if ( item . Value . rect . contains ( new Offset ( pos . x , pos . y ) ) ) {
110
+ D . assert ( instance . raycastHandlerMap . ContainsKey ( windowHashCode ) , ( ) =>
111
+ $ "Raycast Handler Map doesn't contain Window { windowHashCode } ") ;
112
+
113
+ foreach ( var item in instance . raycastHandlerMap [ windowHashCode ] ) {
114
+ if ( item . Value . CheckInRect ( pos ) ) {
75
115
return false ;
76
116
}
77
117
}
0 commit comments