summaryrefslogtreecommitdiff
path: root/Assets/ActionTool/Editor/ActionPreviewEditor.cs
blob: 8c9d8f05f29ab700fddf2fa1ece383b4e5180199 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace ActionTool
{

    public struct BoxParam
    {
        public ColliderData collider;
        public int frame;
    }

    public class ActionPreviewEditor : EditorWindow
    {
        Texture m_UITextureStop;
        Texture m_UITexturePause;
        Texture m_UITexturePlay;
        Texture m_UITextureNext;
        Texture m_UITextureEnd;
        Texture m_UITexturePrevious;
        Texture m_UITextureStart;
        Texture m_UITextureNewHurtBox;
        Texture m_UITextureNewHitBox;

        GUIStyle m_StyleBold;

        const float kToolbarControlMargin = 5;
        const float kToolbarHeight = 50;
        const float kToolbarControlSize = kToolbarHeight - kToolbarControlMargin * 2;
        const float kTimeLineViewXOffset = 30;
        const float kFrameWidth = 10;
        const float kFrameHeight = 20;

        float m_GridY = 0;

        ActionEditorStyles styles;
        ActionEditorUI ui;

        private void OnEnable()
        {
            titleContent = new GUIContent("Action Preview");

            m_UITextureStop = (Texture)Resources.Load("button_control_stop");
            m_UITexturePause = (Texture)Resources.Load("button_control_pause");
            m_UITexturePlay = (Texture)Resources.Load("button_control_play");
            m_UITextureNext = (Texture)Resources.Load("button_control_next");
            m_UITextureEnd = (Texture)Resources.Load("button_control_end"); 
            m_UITexturePrevious = (Texture)Resources.Load("button_control_previous");
            m_UITextureStart = (Texture)Resources.Load("button_control_start");
            m_UITextureNewHurtBox = (Texture)Resources.Load("hurtbox");
            m_UITextureNewHitBox = (Texture)Resources.Load("hitbox");

        }

        void Update()
        {
            ActionManager.UpdateFrame();
        }

        private void OnDisable()
        {
            ActionManager.PreviewWindow = null;
        }

        private void OnGUI()
        {
			styles = ActionEditorStyles.Get();

			if (ActionManager.CurrentAnimationName == null || ActionManager.CurrentAnimationName == "")
            {
                EditorGUILayout.HelpBox("选择动画", MessageType.Warning);
                return;
            }

            if (styles == null) styles = ActionEditorStyles.Get();
            if (ui == null) ui = ActionEditorUI.Get();

            GUI_Toolbar();
            GUI_Detail();
            GUI_TimeLineView();
        }

        void GUI_Toolbar()
        {
            float x = 0, y = kToolbarControlMargin;
            GUI_Toolbar_BG();
            GUI_Toolbar_Start(ref x, ref y);
            GUI_Toolbar_Previous(ref x, ref y);
            GUI_Toolbar_Stop(ref x, ref y);
            GUI_Toolbar_Pause(ref x, ref y);
            GUI_Toolbar_Next(ref x, ref y);
            GUI_Toolbar_End(ref x, ref y);

            GUI_DrawSeperateLine(x + 10 + kToolbarControlMargin, 0, kToolbarHeight);
            x += 20;

            GUI.enabled = ActionManager.animationData != null;
            GUI_Toolbar_NewHurtBox(ref x, ref y);
            GUI_Toolbar_NewHitBox(ref x, ref y);
            GUI_Toolbar_Delete(ref x, ref y);
            GUI.enabled = true;

            GUI_DrawSeperateLine(x + 10 + kToolbarControlMargin, 0, kToolbarHeight);
            x += 20;

            GUI.enabled = ActionManager.animationData == null;
            GUI_Toolbar_NewAnimationData(ref x, ref y);
            GUI.enabled = true;

            GUI.enabled = ActionManager.animationData != null;
            GUI_Toolbar_Save(ref x, ref y);
            GUI.enabled = true;
        }

        void GUI_Toolbar_BG()
        {
            GUI.DrawTexture(new Rect(0, 0, position.width, 50), EditorStyles.toolbar.normal.background);
        }

        void GUI_Toolbar_Start(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if(GUI.Button(rect, m_UITextureStart))
            {
                ActionManager.Start();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_Previous(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, m_UITexturePrevious))
            {
                ActionManager.Previous();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_Stop(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, m_UITextureStop))
            {
                ActionManager.Stop();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_Pause(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            Texture tex = ActionManager.IsPlay ? m_UITexturePlay : m_UITexturePause;
            if (GUI.Button(rect, tex))
            {
                ActionManager.Pause();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_Next(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, m_UITextureNext))
            {
                ActionManager.Next();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_End(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, m_UITextureEnd))
            {
                ActionManager.End();
            }
            x += kToolbarControlSize;
        }

        void GUI_Detail()
        {
            float y = kToolbarHeight + 5;
            float x = 5;

            GUI.Label(new Rect(x, y, 105, 20), "Animation Name:");
            x += 105;
            GUI.Label(new Rect(x, y, 210, 20), ActionManager.CurrentAnimationName, styles.textBold);
            x += 200;
        }

        void GUI_TimeLineView()
        {
            if (ActionManager.actionData == null)
                return;

            float y = 80;

            GUI_FrameText(ref y);
            GUI_Slider(ref y);
            GUI_Grid(ref y);
            GUI_Events();
            GUI_Boxes();
            GUI_FrameLine();
        }

        void GUI_FrameText(ref float y)
        {
            ActionData action = ActionManager.actionData;
            int sampleCount = (int)action.totalFrame + 1;
            Rect rect = new Rect(0, y, 20, 15);
            for(int i = 0; i < sampleCount; i++)
            {
                rect.x = kTimeLineViewXOffset + i * kFrameWidth - ((i >= 10) ? 7 : 5);
                if(i % 5 == 0)
                {
                    Color c = GUI.color;
                    GUI.color = i % 10 == 0 ? Color.yellow : GUI.color;
                    GUI.Label(rect, i.ToString(), styles.textSmall);
                    GUI.color = c;
                }
            }
            y += 11;
        }

        void GUI_Slider( ref float y)
        {
            ActionData action = ActionManager.actionData;
            Rect rect = new Rect(kTimeLineViewXOffset - 4, y, action.totalFrame * kFrameWidth + 7, 15);
            float t = GUI.HorizontalSlider(rect,action.curAnimTimeNormal, 0, 1);
            if(t != action.curAnimTimeNormal)
            {
                if(ActionManager.IsPlay)
                    ActionManager.Pause();
                action.curAnimTimeNormal = t;
            }

            if(ActionManager.IsPlay)
            {
                this.Repaint();
            }

            y += 20;
        }

        void GUI_Grid(ref float y)
        {
            m_GridY = y;

            ActionData action = ActionManager.actionData;
            int sampleCount = (int)action.totalFrame + 1;

            Rect bgRect = new Rect(kTimeLineViewXOffset, y, action.totalFrame * kFrameWidth, ActionManager.eventAndBoxCount * kFrameHeight);
            GUI.Box(bgRect, "");

            Color lineColor = new Color(0.3f, 0.3f, 0.3f);
            Color lineColor2 = new Color(0.5f, 0.5f, 0.5f);
            for (int i = 0; i < ActionManager.eventAndBoxCount + 1; i++)
            {
                ui.DrawHorizontalLineFast(y + i * kFrameHeight, kTimeLineViewXOffset, kTimeLineViewXOffset + action.totalFrame * kFrameWidth, lineColor);
            }
            for(int i = 0; i <= sampleCount; ++i)
            {
                Color c = i % 5 == 0 ? lineColor2 : lineColor;
                float x = kTimeLineViewXOffset + i * kFrameWidth;
                x = Mathf.Clamp(x, kTimeLineViewXOffset, kTimeLineViewXOffset + action.totalFrame * kFrameWidth);
                ui.DrawVerticalLineFast(x, y, y + ActionManager.eventAndBoxCount * kFrameHeight, c);
            }

            y += ActionManager.eventAndBoxCount * kFrameHeight;
        }

        void GUI_FrameLine()
        {
            float y = m_GridY;
            ActionData action = ActionManager.actionData;
            Rect bgRect = new Rect(kTimeLineViewXOffset, y, action.totalFrame * kFrameWidth, ActionManager.eventAndBoxCount * kFrameHeight);
            ui.defaultUIMaterail.SetPass(0);

            ui.DrawVerticalLineFast(kTimeLineViewXOffset + bgRect.width * action.curAnimTimeNormal, y, y + ActionManager.eventAndBoxCount * kFrameHeight, Color.red);
        }

        void GUI_Events()
        {

        }

        void GUI_Boxes()
        {
            float y = m_GridY + ActionManager.kMaxEventsPerFrame * kFrameHeight;
            AnimationData animData = ActionManager.animationData;
            if (animData == null)
                return;
            DrawBoxList(animData.hurtBoxes, ref y, Color.green);
            DrawBoxList(animData.hitBoxes, ref y, Color.red);
            DrawBoxFrameMenuItem();
        }

        void DrawBoxList(List<ColliderData> boxes, ref float y, Color c)
        {
            if (boxes == null || boxes.Count == 0)
                return;
            int count = boxes.Count;
            for(int i = 0; i < boxes.Count; ++i)
            {
                DrawBox(i, boxes[i], y + i * kFrameHeight, c);
            }
            y += count * kFrameHeight;
        }

        void DrawBox(int index, ColliderData box, float y, Color c)
        {
            ActionData action = ActionManager.actionData;

            Color prevColor = GUI.backgroundColor;
			GUI.backgroundColor = c; 
			Rect rect = new Rect(kTimeLineViewXOffset - 17, y - 1, 17, kFrameHeight);
            bool selected = ActionManager.colliderData == box;
			bool select = GUI.Toggle(rect, selected, index.ToString(), styles.boxToggle);
			GUI.backgroundColor = prevColor;

			prevColor = GUI.color;
			GUI.color = c;
			if (select)
            {
                ActionManager.OnSelectBox(box);

                //float length = action.totalFrame * kFrameWidth;
                //ui.DrawHorizontalLineFast(y, kTimeLineViewXOffset, kTimeLineViewXOffset + length, c * 0.7f);
                //ui.DrawHorizontalLineFast(y + kFrameHeight, kTimeLineViewXOffset, kTimeLineViewXOffset + length, c * 0.7f);
                //ui.DrawVerticalLineFast(kTimeLineViewXOffset + length + 1, y, y + kFrameHeight, c * 0.7f);
            }
            else if(selected && !select)
            {
                ActionManager.OnSelectBox(null);
            }

            if(box.frames != null && box.frames.Count > 0)
            {
                int prevIndex = -1;
                for(int i = 0; i < box.frames.Count; ++i)
                {
                    ColliderData.ColliderFrame frame = box.frames[i];
                    int frameIndex = frame.frame;
                    Vector2 pos = new Vector2(kTimeLineViewXOffset + frameIndex * kFrameWidth, y);
                    Rect frameRect = new Rect(pos.x, pos.y, kFrameWidth, kFrameHeight);
                    bool frameSelected = frame == ActionManager.editColliderFrame; 
                    bool frameSelect = GUI.Toggle(frameRect, frameSelected, "",styles.keyFrameButton);
                    if(!frameSelected && frameSelect)
                    {
                        ActionManager.OnSelectBox(box);
                        ActionManager.OnSelectColliderFrame(frame);
                    }
                    else if(frameSelect && !frameSelect)
                    {
                        ActionManager.OnSelectColliderFrame(null);
                    }
                    if(prevIndex != -1)
                    {
                        float length = (frameIndex - prevIndex - 1) * kFrameWidth;
                        Rect region = new Rect(kTimeLineViewXOffset + (prevIndex + 1) * kFrameWidth, y, length, kFrameHeight);
						float animFrame = action.curAnimFrame;
						Color col = c * 0.4f;
						//if (ActionManager.IsPlay)
						//{
							bool highlight = action.curAnimFrame >= prevIndex && action.curAnimFrame < frameIndex;
							col = highlight ? c * 0.6f : c * 0.4f;
						//}
						EditorGUI.DrawRect(region, col);
					}
					if (frame.active)
                        prevIndex = frameIndex;
                    else
                        prevIndex = -1;
                }
            }

            GUI.color = prevColor;
        }

		void DrawBoxFrameMenuItem()
        {
            Event e = Event.current;
            if (e.button != 1  || !e.isMouse || e.type != EventType.MouseDown)
                return;
            ActionData action = ActionManager.actionData;
            float y = m_GridY + ActionManager.kMaxEventsPerFrame * kFrameHeight;
            Vector2 position = e.mousePosition;
            int boxCount = ActionManager.animationData.GetBoxesCount();
            Rect boxRegion = new Rect(kTimeLineViewXOffset, y, action.totalFrame * kFrameWidth, boxCount * kFrameHeight);
            if (!boxRegion.Contains(position))
                return;
            // 找到对应的box和帧
            Vector2 pos = new Vector2(position.x - boxRegion.x, position.y - boxRegion.y);
            int index = (int)(pos.y / kFrameHeight);
            int frame = (int)(pos.x / kFrameWidth);
            ColliderData box = ActionManager.animationData.GetColliderByIndex(index);
            if(box != null)
            {
                BoxParam param = new BoxParam();
                param.collider = box;
                param.frame = frame;
                if(ActionManager.colliderData != box)
                    ActionManager.OnSelectBox(box);
				GenericMenu _newFrameMenu = new GenericMenu();
				_newFrameMenu.AddItem(new GUIContent("New Frame"), false, ActionManager.AddNewBoxFrame, param);
				_newFrameMenu.AddItem(new GUIContent("Delete"), false, ActionManager.DeleteBoxFrame, param);
				_newFrameMenu.ShowAsContext();
            }
            else
            {
                Debug.LogError("[ActionTool] 错误的点击");
            }
        } 

        void GUI_DrawSeperateLine(float x, float y, float height)
        {
            ui.defaultUIMaterail.SetPass(0);
            Color lineColor = new Color(0.3f, 0.3f, 0.3f);
            Color lineColor2 = new Color(0.1f, 0.1f, 0.1f);
            ui.DrawVerticalLineFast(x, y, y +height, lineColor2);
            ui.DrawVerticalLineFast(x+1, y, y +height, lineColor);
        }

        void GUI_Toolbar_NewHurtBox(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, new GUIContent("New", m_UITextureNewHurtBox)))
            {
                ActionManager.NewHurtBox();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_NewHitBox(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, new GUIContent("New", m_UITextureNewHitBox)))
            {
                ActionManager.NewHitBox();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_Delete(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            GUI.enabled = ActionManager.colliderData != null;
            if (GUI.Button(rect, "Delete"))
            {
                ActionManager.DeleteCurBox();
            }
            GUI.enabled = true;
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_NewAnimationData(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, new GUIContent("New")))
            {
                ActionManager.CreateAnimationData();
            }
            x += kToolbarControlSize;
        }

        void GUI_Toolbar_Save(ref float x, ref float y)
        {
            x += kToolbarControlMargin;
            Rect rect = new Rect(x, y, kToolbarControlSize, kToolbarControlSize);
            if (GUI.Button(rect, new GUIContent("Save")))
            {
                ActionManager.SaveAnimationData();
            }
            x += kToolbarControlSize;
        }

    }

}