summaryrefslogtreecommitdiff
path: root/Assets/Tools/ActionTool/Editor/ActionRootMotionEditor.cs
blob: eb076fc3bf19b7403113f75b2bc2fb6f85332de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace ActionTool
{
    public class ActionRootMotionEditor : EditorWindow
    {
        ActionEditorStyles styles;

        // isrecord的时候playbackFrame是整数
        public static bool IsRecord { get; private set; }

        const float kToolbarControlMargin = 5;
        const float kToolbarHeight = 50;
        const float kToolbarControlSize = kToolbarHeight - kToolbarControlMargin * 2;
        float m_ToolbarOffset = 0; // <= 0

        Texture m_UITextureRecord;
        Texture m_UITextureTakeRecord;
        Texture m_UITextureTrashCan;

        Texture2D tex;

        private void OnEnable()
        {
            maxSize = new Vector2(300, 90);
            minSize = maxSize;
            this.titleContent = new GUIContent("RootMotion Editor");

            m_UITextureRecord = (Texture)Resources.Load("button_control_record");
            m_UITextureTakeRecord = (Texture)Resources.Load("button_control_takerecord");
            m_UITextureTrashCan = EditorGUIUtility.FindTexture("d_TreeEditor.Trash");

            tex = new Texture2D(1, 1, TextureFormat.RGBA32, false);
            tex.SetPixel(0, 0, new Color(1f, 0, 0) * 0.5f);
            tex.Apply();

            IsRecord = false;

            ActionManager.gizmos.ShowRootMotionGizmos(true);
        }

        private void OnDisable()
        {
            IsRecord = false;

            ActionManager.gizmos.ShowRootMotionGizmos(false);
        }

        private void Update()
        {

        }

        private void OnGUI()
        {
            if(ActionManager.animationData == null || ActionManager.animationData.overrideRootMotion == false)
            {
                this.Close();
                return;
            }
            if (IsRecord)
            {
                GUI.DrawTexture(new Rect(0, 0, maxSize.x, maxSize.y), tex, ScaleMode.StretchToFill);
            }
            float x = m_ToolbarOffset, y = kToolbarControlMargin;
            GUI_Record(ref x, ref y);
            GUI_TakeRecord(ref x, ref y);
            GUI_Delete(ref x, ref y);
            GUI.enabled = false;
            EditorGUI.Vector3Field(new Rect(0, kToolbarHeight, position.width, 20), "Position: ", ActionManager.unitRoot.transform.position);
            GUI.enabled = true;
        }

        void GUI_Record(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            IsRecord = GUI.Toggle(rect, IsRecord, EditorGUIUtility.IconContent("d_Animation.Record", "Record"), GUI.skin.button);
            x += kToolbarControlSize;
        }

        void GUI_TakeRecord(ref float x, ref float y)
        {
            if (!IsRecord)
                GUI.enabled = false;
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, EditorGUIUtility.IconContent("Animation.AddKeyframe", "Key Frame")))
            {
                Vector3 pos = ActionManager.unitRoot.transform.position;
                int frame = (int)ActionManager.actionData.curAnimFrame;
                ActionManager.animationData.rootMotionOverrideData.SetPosition(frame, pos);
                ActionManager.PreviewWindow.Repaint();
                EditorWindow.GetWindow<SceneView>()?.Repaint();
                SceneView.RepaintAll();
            }
            GUI.enabled = true;
            x += kToolbarControlSize;
        }
         
        void GUI_Delete(ref float x, ref float y)
        {
            if (!IsRecord)
                GUI.enabled = false;
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, m_UITextureTrashCan))
            {
                int frame = (int)ActionManager.actionData.curAnimFrame;
                ActionManager.animationData.rootMotionOverrideData.RemovePositionAtFrame(frame);
                ActionManager.PreviewWindow.Repaint();
            }
            GUI.enabled = true;
            x += kToolbarControlSize;
        }

    }
}