2024-10-23 17:30:59 +00:00
|
|
|
|
#if UNITY_EDITOR
|
2024-12-10 10:14:38 +00:00
|
|
|
|
using Sirenix.OdinInspector;
|
2024-11-06 17:45:57 +00:00
|
|
|
|
using System;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
using UnityEngine;
|
2024-10-29 17:51:06 +00:00
|
|
|
|
using UnityEngine.UIElements;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
|
|
|
|
|
namespace UguiToolkit.Editor
|
|
|
|
|
{
|
|
|
|
|
public interface IEntity
|
|
|
|
|
{
|
2024-12-10 10:14:38 +00:00
|
|
|
|
void SetOriginalMatrix(Transform tf);
|
2024-10-29 17:51:06 +00:00
|
|
|
|
void ApplyTransform(Transform tf);
|
2024-12-10 10:14:38 +00:00
|
|
|
|
void InternalApplyTransform(Transform tf);
|
|
|
|
|
void ApplyTransformByParent(Transform parentTf, Transform tf);
|
2024-10-29 17:51:06 +00:00
|
|
|
|
bool IsInside(Transform tf);
|
2024-12-03 05:05:13 +00:00
|
|
|
|
void ApplyData<T>(T ui) where T: Component;
|
2024-12-01 16:33:27 +00:00
|
|
|
|
void InitPreview();
|
|
|
|
|
void ShowSelectionImg(bool show);
|
|
|
|
|
GameObject gameObject { get; }
|
2024-10-21 16:20:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-06 10:46:00 +00:00
|
|
|
|
|
|
|
|
|
public abstract class BaseEntity<T1,T2> : MonoBehaviour, IEntity where T1 : Component where T2 : LayoutInfo.ElementInfo
|
2024-10-21 16:20:39 +00:00
|
|
|
|
{
|
|
|
|
|
// ElementInfo
|
|
|
|
|
private T2 m_elementInfo;
|
2024-10-29 17:51:06 +00:00
|
|
|
|
private UnityEngine.UI.Image m_selectionImg;
|
2024-12-10 10:14:38 +00:00
|
|
|
|
private Matrix4x4 lastMoveMatrix;
|
|
|
|
|
private Matrix4x4 oldTransformMatrix;
|
2024-10-21 16:20:39 +00:00
|
|
|
|
|
2024-10-29 17:51:06 +00:00
|
|
|
|
public T2 ElementInfo => m_elementInfo;
|
|
|
|
|
public void ShowSelectionImg(bool show)
|
|
|
|
|
{
|
|
|
|
|
if (m_selectionImg)
|
|
|
|
|
{
|
|
|
|
|
m_selectionImg.gameObject.SetActive(show);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 16:33:27 +00:00
|
|
|
|
public abstract void InitPreview();
|
2024-12-10 10:14:38 +00:00
|
|
|
|
protected abstract void OnApplyTransform(Transform tf);
|
2024-12-01 16:33:27 +00:00
|
|
|
|
protected abstract void OnApplyData(T1 ui);
|
|
|
|
|
|
2024-12-10 10:14:38 +00:00
|
|
|
|
public virtual void ApplyTransformByParent(Transform parentTf, Transform tf)
|
|
|
|
|
{
|
|
|
|
|
var position = lastMoveMatrix.GetColumn(3); // 提取位置
|
|
|
|
|
var rotation = Quaternion.LookRotation(lastMoveMatrix.GetColumn(2), lastMoveMatrix.GetColumn(1)); // 提取旋转
|
|
|
|
|
Debug.Log($"[I] ApplyTransformByParent {position}, {rotation.eulerAngles}");
|
|
|
|
|
parentTf.position += new Vector3(position.x, position.y, position.z);
|
|
|
|
|
parentTf.rotation *= rotation;
|
|
|
|
|
|
|
|
|
|
if (tf)
|
|
|
|
|
{
|
|
|
|
|
var lastTransformMatrixInverse = lastMoveMatrix.inverse;
|
|
|
|
|
position = lastTransformMatrixInverse.GetColumn(3); // 提取位置
|
|
|
|
|
rotation = Quaternion.LookRotation(lastTransformMatrixInverse.GetColumn(2), lastTransformMatrixInverse.GetColumn(1)); // 提取旋转
|
|
|
|
|
tf.position += new Vector3(position.x, position.y, position.z);
|
|
|
|
|
tf.rotation *= rotation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetOriginalMatrix(Transform tf)
|
|
|
|
|
{
|
|
|
|
|
oldTransformMatrix = Matrix4x4.TRS(tf.position, tf.rotation, tf.localScale);
|
|
|
|
|
}
|
2024-12-09 11:18:10 +00:00
|
|
|
|
|
2024-12-10 10:14:38 +00:00
|
|
|
|
public void InternalApplyTransform(Transform tf)
|
|
|
|
|
{
|
|
|
|
|
OnApplyTransform(tf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ApplyTransform(Transform tf)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[I] ApplyTransform {tf.name}");
|
|
|
|
|
OnApplyTransform(tf);
|
|
|
|
|
Matrix4x4 newTransformMatrix = Matrix4x4.TRS(tf.position, tf.rotation, tf.localScale);
|
|
|
|
|
lastMoveMatrix = newTransformMatrix * oldTransformMatrix.inverse;
|
|
|
|
|
}
|
2024-12-01 16:33:27 +00:00
|
|
|
|
|
2024-10-29 17:51:06 +00:00
|
|
|
|
public bool IsInside(Transform tf)
|
|
|
|
|
{
|
|
|
|
|
var rect = ElementInfo.Rect;
|
|
|
|
|
var pos = StageManager.Instance.PrefabContentsRoot.transform.InverseTransformPoint(new Vector3(tf.position.x, tf.position.y, 0));
|
|
|
|
|
return rect.Contains(pos);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 16:20:39 +00:00
|
|
|
|
// 创建时调用
|
|
|
|
|
public void SetData(T2 elementInfo)
|
|
|
|
|
{
|
|
|
|
|
this.m_elementInfo = elementInfo;
|
2024-10-29 17:51:06 +00:00
|
|
|
|
CreateSelectionImg();
|
2024-10-21 16:20:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 05:05:13 +00:00
|
|
|
|
public void ApplyData<T>(T ui) where T : Component
|
2024-10-21 16:20:39 +00:00
|
|
|
|
{
|
2024-11-06 17:45:57 +00:00
|
|
|
|
OnApplyData(ui as T1);
|
2024-10-21 16:20:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 17:51:06 +00:00
|
|
|
|
private void CreateSelectionImg()
|
|
|
|
|
{
|
|
|
|
|
var go = new GameObject("_selectionImg_", typeof(RectTransform));
|
|
|
|
|
EntityHelper.UpdateHierarchyAndSceneVisibilityOfEntity(false, go);
|
|
|
|
|
var tf = go.transform;
|
|
|
|
|
tf.SetParent(transform);
|
|
|
|
|
tf.localPosition = Vector3.zero;
|
|
|
|
|
tf.localRotation = Quaternion.identity;
|
|
|
|
|
tf.localScale = Vector3.one;
|
|
|
|
|
|
|
|
|
|
var rtf = go.GetComponent<RectTransform>();
|
|
|
|
|
rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h);
|
|
|
|
|
|
|
|
|
|
m_selectionImg = go.AddComponent<UnityEngine.UI.Image>();
|
2024-12-10 10:14:38 +00:00
|
|
|
|
m_selectionImg.color = new Color(0, 1, 0, 0.4f);
|
2024-10-29 17:51:06 +00:00
|
|
|
|
}
|
2024-10-21 16:20:39 +00:00
|
|
|
|
}
|
2024-10-23 17:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|