Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit d1ebb51

Browse files
committed
[Raycast] Update
1 parent 1334379 commit d1ebb51

File tree

2 files changed

+79
-46
lines changed

2 files changed

+79
-46
lines changed

Runtime/Plugins/Raycast/RaycastManager.cs

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
using System.Collections.Generic;
2+
using Unity.UIWidgets.foundation;
23
using Unity.UIWidgets.ui;
34
using UnityEngine;
4-
using Rect = Unity.UIWidgets.ui.Rect;
55

66
namespace Unity.UIWidgets.plugins.raycast {
77
public class RaycastableRect {
8-
public bool isDirty;
9-
public Rect rect;
8+
bool _isDirty = true;
109

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;
1339
}
1440
}
1541

@@ -26,52 +52,66 @@ public static RaycastManager instance {
2652
}
2753
}
2854

29-
public Dictionary<int, Dictionary<int, RaycastableRect>> hashCodeList =
55+
public readonly Dictionary<int, Dictionary<int, RaycastableRect>> raycastHandlerMap =
3056
new Dictionary<int, Dictionary<int, RaycastableRect>>();
3157

3258
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>());
3662
}
3763
}
3864

39-
public static void AddToList(int key, int windowHashCode) {
65+
public static void AddToList(int widgetHashCode, int windowHashCode) {
4066
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+
4471
}
4572
}
4673

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();
5282
}
5383

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();
6296
}
6397
}
6498

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);
70107
}
71108

72109
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)) {
75115
return false;
76116
}
77117
}

Runtime/Plugins/Raycast/RaycastableContainer.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,25 @@ public override RenderObject createRenderObject(BuildContext context) {
2222
}
2323

2424
public override Element createElement() {
25-
return new _RaycastableBoxRenderElement(this.windowHashCode, this);
25+
return new _RaycastableBoxRenderElement(windowHashCode: this.windowHashCode, widget: this);
2626
}
2727
}
2828

2929
class RenderRaycastableBox : RenderProxyBox {
3030
public RenderRaycastableBox(
3131
int windowHashCode,
32-
RenderBox child = null,
33-
RaycastableBox widget = null
34-
) : base(child) {
35-
this.widget = widget;
32+
RaycastableBox widget
33+
) {
34+
this.widgetHashCode = widget.GetHashCode();
3635
this.windowHashCode = windowHashCode;
3736
}
3837

38+
readonly int widgetHashCode;
3939
readonly int windowHashCode;
40-
RaycastableBox widget;
41-
42-
public override void detach() {
43-
base.detach();
44-
this.markNeedsPaint();
45-
}
46-
4740

4841
public override void paint(PaintingContext context, Offset offset) {
4942
// Debug.Log($"[RenderRaycastableBox] Paint {this.widget.GetHashCode()}: {this.size}@{offset}");
50-
RaycastManager.UpdateSizeOffset(this.widget.GetHashCode(), (int) this.windowHashCode, this.size, offset);
43+
RaycastManager.UpdateSizeOffset(this.widgetHashCode, this.windowHashCode, this.size, offset);
5144

5245
base.paint(context, offset);
5346
}

0 commit comments

Comments
 (0)