浏览代码

[Raycast] Update

/main
iizzaya 5 年前
当前提交
68007132
共有 2 个文件被更改,包括 79 次插入46 次删除
  1. 106
      Runtime/Plugins/Raycast/RaycastManager.cs
  2. 19
      Runtime/Plugins/Raycast/RaycastableContainer.cs

106
Runtime/Plugins/Raycast/RaycastManager.cs


using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Rect = Unity.UIWidgets.ui.Rect;
public bool isDirty;
public Rect rect;
bool _isDirty = true;
public bool isDirty {
get { return this._isDirty; }
}
public float left;
public float right;
public float top;
public float bottom;
public void MarkDirty() {
this._isDirty = true;
}
public void UnmarkDirty() {
this._isDirty = false;
}
public void UpdateRect(float left, float top, float width, float height) {
this.left = left;
this.right = left + width;
this.top = top;
this.bottom = top + height;
}
public RaycastableRect(bool isDirty) {
this.isDirty = isDirty;
public bool CheckInRect(Vector2 pos) {
return pos.x >= this.left &&
pos.x < this.right &&
pos.y >= this.top &&
pos.y < this.bottom;
}
}

}
}
public Dictionary<int, Dictionary<int, RaycastableRect>> hashCodeList =
public readonly Dictionary<int, Dictionary<int, RaycastableRect>> raycastHandlerMap =
if (!instance.hashCodeList.ContainsKey(windowHashCode)) {
// Debug.Log($"New Window: @[{windowHashCode}] ({instance.hashCodeList.Count})");
instance.hashCodeList.Add(windowHashCode, new Dictionary<int, RaycastableRect>());
if (!instance.raycastHandlerMap.ContainsKey(windowHashCode)) {
Debug.Log($"New Window: @[{windowHashCode}] ({instance.raycastHandlerMap.Count})");
instance.raycastHandlerMap.Add(windowHashCode, new Dictionary<int, RaycastableRect>());
public static void AddToList(int key, int windowHashCode) {
public static void AddToList(int widgetHashCode, int windowHashCode) {
// Debug.Log($"Add To List: [{key}]@[{windowHashCode}]");
if (!instance.hashCodeList[windowHashCode].ContainsKey(key)) {
instance.hashCodeList[windowHashCode][key] = new RaycastableRect(true);
Debug.Log($"Add To List: [{widgetHashCode}]@[{windowHashCode}]");
if (!instance.raycastHandlerMap[windowHashCode].ContainsKey(widgetHashCode)) {
instance.raycastHandlerMap[windowHashCode][widgetHashCode] = new RaycastableRect();
public static void MarkDirty(int key, int windowHashCode) {
// Debug.Log($"Mark Dirty: [{key}]@[{windowHashCode}]");
if (instance.hashCodeList[windowHashCode].ContainsKey(key)) {
instance.hashCodeList[windowHashCode][key].isDirty = true;
}
public static void MarkDirty(int widgetHashCode, int windowHashCode) {
Debug.Log($"Mark Dirty: [{widgetHashCode}]@[{windowHashCode}]");
D.assert(instance.raycastHandlerMap.ContainsKey(windowHashCode), () =>
$"Raycast Handler Map doesn't contain Window {windowHashCode}");
D.assert(instance.raycastHandlerMap[windowHashCode].ContainsKey(widgetHashCode), () =>
$"Raycast Handler Map doesn't contain Widget {widgetHashCode} at Window {windowHashCode}");
instance.raycastHandlerMap[windowHashCode][widgetHashCode].MarkDirty();
public static void UpdateSizeOffset(int key, int windowHashCode, Size size, Offset offset) {
// Debug.Log($"Update Size Offset: [{key}]@[{windowHashCode}]");
if (instance.hashCodeList[windowHashCode].ContainsKey(key)) {
if (instance.hashCodeList[windowHashCode][key].isDirty) {
instance.hashCodeList[windowHashCode][key].rect =
Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height);
instance.hashCodeList[windowHashCode][key].isDirty = false;
}
public static void UpdateSizeOffset(int widgetHashCode, int windowHashCode, Size size, Offset offset) {
D.assert(instance.raycastHandlerMap.ContainsKey(windowHashCode), () =>
$"Raycast Handler Map doesn't contain Window {windowHashCode}");
D.assert(instance.raycastHandlerMap[windowHashCode].ContainsKey(widgetHashCode), () =>
$"Raycast Handler Map doesn't contain Widget {widgetHashCode} at Window {windowHashCode}");
if (instance.raycastHandlerMap[windowHashCode][widgetHashCode].isDirty) {
Debug.Log($"Update Size Offset: [{widgetHashCode}]@[{windowHashCode}]");
instance.raycastHandlerMap[windowHashCode][widgetHashCode]
.UpdateRect(offset.dx, offset.dy, size.width, size.height);
instance.raycastHandlerMap[windowHashCode][widgetHashCode].UnmarkDirty();
public static void RemoveFromList(int key, int windowHashCode) {
// Debug.Log($"Remove From List: [{key}]@[{windowHashCode}]");
if (instance.hashCodeList[windowHashCode].ContainsKey(key)) {
instance.hashCodeList[windowHashCode].Remove(key);
}
public static void RemoveFromList(int widgetHashCode, int windowHashCode) {
Debug.Log($"Remove From List: [{widgetHashCode}]@[{windowHashCode}]");
D.assert(instance.raycastHandlerMap.ContainsKey(windowHashCode), () =>
$"Raycast Handler Map doesn't contain Window {windowHashCode}");
D.assert(instance.raycastHandlerMap[windowHashCode].ContainsKey(widgetHashCode), () =>
$"Raycast Handler Map doesn't contain Widget {widgetHashCode} at Window {windowHashCode}");
instance.raycastHandlerMap[windowHashCode].Remove(widgetHashCode);
foreach (var item in instance.hashCodeList[windowHashCode]) {
if (item.Value.rect.contains(new Offset(pos.x, pos.y))) {
D.assert(instance.raycastHandlerMap.ContainsKey(windowHashCode), () =>
$"Raycast Handler Map doesn't contain Window {windowHashCode}");
foreach (var item in instance.raycastHandlerMap[windowHashCode]) {
if (item.Value.CheckInRect(pos)) {
return false;
}
}

19
Runtime/Plugins/Raycast/RaycastableContainer.cs


}
public override Element createElement() {
return new _RaycastableBoxRenderElement(this.windowHashCode, this);
return new _RaycastableBoxRenderElement(windowHashCode: this.windowHashCode, widget: this);
}
}

RenderBox child = null,
RaycastableBox widget = null
) : base(child) {
this.widget = widget;
RaycastableBox widget
) {
this.widgetHashCode = widget.GetHashCode();
readonly int widgetHashCode;
RaycastableBox widget;
public override void detach() {
base.detach();
this.markNeedsPaint();
}
RaycastManager.UpdateSizeOffset(this.widget.GetHashCode(), (int) this.windowHashCode, this.size, offset);
RaycastManager.UpdateSizeOffset(this.widgetHashCode, this.windowHashCode, this.size, offset);
base.paint(context, offset);
}

正在加载...
取消
保存