140 lines
4.0 KiB
C#
140 lines
4.0 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
|
|
namespace Wing.Gizmos
|
|
{
|
|
public class GizmosScale : GizmosBase
|
|
{
|
|
public delegate void ScaleEventHandler(float dx, float dy, float dz);
|
|
|
|
public event ScaleEventHandler OnScale;
|
|
public event Action OnScaleEnd;
|
|
|
|
private Vector3 mTotal = Vector3.zero;
|
|
|
|
private Transform _X;
|
|
private Transform _Y;
|
|
private Transform _Z;
|
|
|
|
private float originOffset;
|
|
// Use this for initialization
|
|
|
|
protected override void OnInit()
|
|
{
|
|
ConstructAxis("X", EGizmosAxisType.X);
|
|
ConstructAxis("Y", EGizmosAxisType.Y);
|
|
ConstructAxis("Z", EGizmosAxisType.Z);
|
|
ConstructAxis("Center", EGizmosAxisType.CENTER);
|
|
|
|
_X = transform.Find("X");
|
|
_Y = transform.Find("Y");
|
|
_Z = transform.Find("Z");
|
|
|
|
originOffset = _X.Find("Cube").transform.localPosition.z;
|
|
}
|
|
|
|
protected override void OnDragDelta(GameObject go, float dx, float dy, float dz)
|
|
{
|
|
//精度捕捉
|
|
Vector3 delta = new Vector3(dx, dy, dz);
|
|
Vector3 newPos = Vector3.zero;
|
|
ISnap snap = SnapFactory.Instance.Get(ESnapType.Scale);
|
|
if (snap.Snap(mTotal + delta, ref newPos))
|
|
{
|
|
dx = newPos.x - mTotal.x;
|
|
dy = newPos.y - mTotal.y;
|
|
dz = newPos.z - mTotal.z;
|
|
}
|
|
mTotal += new Vector3(dx, dy, dz);
|
|
///////
|
|
|
|
base.OnDragDelta(go, dx, dy, dz);
|
|
SetLengthOfAxis(go, dx, dy, dz, true);
|
|
//transform.Translate(dx, dy, dz);
|
|
if (OnScale != null)
|
|
{
|
|
OnScale(dx, dy, dz);
|
|
}
|
|
}
|
|
|
|
public void Scale(float dx,float dy,float dz)
|
|
{
|
|
if (OnScale != null)
|
|
{
|
|
OnScale(dx, dy, dz);
|
|
}
|
|
|
|
if (OnScaleEnd != null)
|
|
{
|
|
OnScaleEnd();
|
|
}
|
|
}
|
|
|
|
protected override void OnDragEnd(GameObject go)
|
|
{
|
|
base.OnDragEnd(go);
|
|
SetLengthOfAxis(go, originOffset, originOffset, originOffset, false);
|
|
if (OnScaleEnd != null)
|
|
{
|
|
OnScaleEnd();
|
|
}
|
|
|
|
mTotal = Vector3.zero;
|
|
}
|
|
|
|
protected override float ComputeScaleFactor(float d)
|
|
{
|
|
float magic = (float)Math.Log10(d + 10) * 1.4f;
|
|
return magic;
|
|
}
|
|
|
|
private void SetLengthOfAxis(GameObject go, float dx, float dy, float dz, bool delta)
|
|
{
|
|
EGizmosAxisType type = _axisMap[go];
|
|
if (type == EGizmosAxisType.X)
|
|
{
|
|
SetLengthOfAxis(go.transform.parent, dx, delta);
|
|
}
|
|
else if (type == EGizmosAxisType.Y)
|
|
{
|
|
SetLengthOfAxis(go.transform.parent, dy, delta);
|
|
}
|
|
else if (type == EGizmosAxisType.Z)
|
|
{
|
|
SetLengthOfAxis(go.transform.parent, dz, delta);
|
|
}
|
|
else if (type == EGizmosAxisType.CENTER)
|
|
{
|
|
SetLengthOfAxis(_X, dx, delta);
|
|
SetLengthOfAxis(_Y, dx, delta);
|
|
SetLengthOfAxis(_Z, dx, delta);
|
|
}
|
|
}
|
|
|
|
private void SetLengthOfAxis(Transform axisRoot, float scale,bool delta)
|
|
{
|
|
Transform axis = axisRoot.Find("Cylinder");
|
|
if (axis != null)
|
|
{
|
|
Vector3 localScale = axis.localScale;
|
|
Transform cube = axisRoot.Find("Cube");
|
|
Vector3 pos = cube.localPosition;
|
|
if (delta)
|
|
{
|
|
pos.z += scale;
|
|
}
|
|
else
|
|
{
|
|
pos.z = scale;
|
|
}
|
|
|
|
localScale.z = pos.z / originOffset;
|
|
axis.localScale = localScale;
|
|
cube.localPosition = pos;
|
|
}
|
|
}
|
|
}
|
|
}
|