123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using System;
- using UnityEngine;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- using static CustomGizmo;
- // TODO: Finalize dragpopint scaling
- internal abstract class BaseDrag : MonoBehaviour
- {
- private const float doubleClickSensitivity = 0.3f;
- protected const int upperArm = 0;
- protected const int foreArm = 1;
- protected const int hand = 2;
- protected const int upperArmRot = 0;
- protected const int handRot = 1;
- private GameObject gizmoGo;
- protected Maid maid;
- protected Meido meido;
- protected Func<Vector3> position;
- protected Func<Vector3> rotation;
- protected Renderer dragPointRenderer;
- protected Collider dragPointCollider;
- protected Vector3 worldPoint;
- protected Vector3 mousePos;
- private DragType dragType = DragType.None;
- protected DragType CurrentDragType
- {
- get => dragType;
- set
- {
- dragType = value;
- reInitDrag = dragType != dragTypeOld;
- dragTypeOld = dragType;
- }
- }
- protected DragType dragTypeOld;
- protected float doubleClickStart = 0f;
- protected bool reInitDrag = false;
- protected bool isPlaying;
- protected CustomGizmo gizmo;
- protected GizmoType CurrentGizmoType
- {
- get => gizmo?.CurrentGizmoType ?? GizmoType.None;
- set
- {
- if (gizmo != null)
- {
- if (GizmoActive) gizmo.CurrentGizmoType = value;
- }
- }
- }
- public Vector3 BaseScale { get; private set; }
- public Vector3 DragPointScale
- {
- get => transform.localScale;
- set
- {
- transform.localScale = value;
- }
- }
- public bool IsBone { get; set; }
- public bool GizmoVisible
- {
- get => gizmo?.Visible ?? false;
- set
- {
- if (gizmo != null) gizmo.Visible = value;
- }
- }
- private bool gizmoActive = false;
- public bool GizmoActive
- {
- get => gizmoActive;
- set
- {
- if (gizmoGo != null)
- {
- gizmoActive = value;
- gizmoGo.SetActive(gizmoActive);
- GizmoVisible = gizmoActive;
- }
- }
- }
- private bool dragPointVisible;
- public bool DragPointVisible
- {
- get => dragPointVisible;
- set
- {
- dragPointVisible = value;
- dragPointRenderer.enabled = dragPointVisible;
- }
- }
- private bool dragPointActive;
- public bool DragPointActive
- {
- get => dragPointActive;
- set
- {
- dragPointActive = value;
- dragPointCollider.enabled = dragPointActive;
- }
- }
- public GizmoMode CurrentGizmoMode
- {
- get => gizmo?.gizmoMode ?? GizmoMode.Local;
- set
- {
- if (gizmo != null)
- {
- if (GizmoActive) gizmo.gizmoMode = value;
- }
- }
- }
- public event EventHandler DragEvent;
- protected enum DragType
- {
- None, Select, Delete,
- MoveXZ, MoveY, RotLocalXZ, RotY, RotLocalY,
- Scale
- }
- public static Material LightBlue = new Material(Shader.Find("Transparent/Diffuse"))
- {
- color = new Color(0.4f, 0.4f, 1f, 0.3f)
- };
- public static Material Blue = new Material(Shader.Find("Transparent/Diffuse"))
- {
- color = new Color(0.5f, 0.5f, 1f, 0.8f)
- };
- public static GameObject MakeDragPoint(PrimitiveType primitiveType, Vector3 scale, Material material)
- {
- GameObject dragPoint = GameObject.CreatePrimitive(primitiveType);
- dragPoint.transform.localScale = scale;
- dragPoint.GetComponent<Renderer>().material = material;
- dragPoint.layer = 8;
- return dragPoint;
- }
- public static T MakeDragPoint<T>(
- PrimitiveType primitiveType, Vector3 scale, Material material
- ) where T : BaseDrag
- {
- GameObject dragPoint = MakeDragPoint(primitiveType, scale, material);
- return dragPoint.AddComponent<T>();
- }
- public BaseDrag Initialize(Meido meido, Func<Vector3> position, Func<Vector3> rotation)
- {
- this.InitializeDragPoint(position, rotation);
- this.meido = meido;
- this.maid = meido.Maid;
- isPlaying = !meido.IsStop;
- return this;
- }
- protected void InitializeDragPoint(Func<Vector3> position, Func<Vector3> rotation)
- {
- this.BaseScale = transform.localScale;
- this.position = position;
- this.rotation = rotation;
- this.dragPointRenderer = GetComponent<Renderer>();
- this.dragPointCollider = GetComponent<Collider>();
- this.DragPointVisible = true;
- }
- protected void InitializeGizmo(Transform target, float scale = 0.25f, GizmoMode mode = GizmoMode.Local)
- {
- gizmoGo = CustomGizmo.MakeGizmo(target, scale, mode);
- gizmo = gizmoGo.GetComponent<CustomGizmo>();
- if (meido != null)
- {
- gizmo.GizmoDrag += (s, a) =>
- {
- meido.IsStop = true;
- isPlaying = false;
- };
- }
- GizmoActive = false;
- GizmoVisible = false;
- }
- public void SetDragProp(bool gizmoActive, bool dragPointActive, bool dragPointVisible)
- {
- this.GizmoActive = gizmoActive;
- this.DragPointActive = dragPointActive;
- this.DragPointVisible = dragPointVisible;
- }
- public void SetDragProp(bool gizmoActive, bool dragPointActive, bool dragPointVisible, GizmoMode mode)
- {
- SetDragProp(gizmoActive, dragPointActive, dragPointVisible);
- this.CurrentGizmoMode = mode;
- }
- protected virtual void InitializeDrag()
- {
- worldPoint = Camera.main.WorldToScreenPoint(transform.position);
- mousePos = Input.mousePosition;
- isPlaying = !meido?.IsStop ?? false;
- }
- protected virtual void DoubleClick() { }
- protected abstract void Drag();
- protected abstract void GetDragType();
- protected virtual void OnMouseUp()
- {
- if ((Time.time - doubleClickStart) < doubleClickSensitivity)
- {
- doubleClickStart = -1;
- DoubleClick();
- }
- else
- {
- doubleClickStart = Time.time;
- }
- }
- protected virtual void Update()
- {
- GetDragType();
- transform.position = position();
- transform.eulerAngles = rotation();
- }
- protected virtual void OnMouseDown()
- {
- InitializeDrag();
- }
- protected virtual void OnMouseDrag()
- {
- if (CurrentDragType == DragType.Select) return;
- if (reInitDrag)
- {
- reInitDrag = false;
- InitializeDrag();
- }
- if (mousePos != Input.mousePosition) Drag();
- }
- private void OnEnable()
- {
- if (position != null)
- {
- transform.position = position();
- transform.eulerAngles = rotation();
- }
- }
- private void OnDestroy()
- {
- GameObject.Destroy(gizmo);
- }
- protected void OnDragEvent()
- {
- DragEvent?.Invoke(null, EventArgs.Empty);
- }
- }
- }
|