From 172d4723c7405d2e748ed03267ca10be9711212b Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 9 Jul 2021 19:17:38 +0800 Subject: *misc --- Assets/ActionTool/Editor/ActionPreviewEditor.cs | 219 +++++++++++++++++++++--- 1 file changed, 194 insertions(+), 25 deletions(-) (limited to 'Assets/ActionTool/Editor/ActionPreviewEditor.cs') diff --git a/Assets/ActionTool/Editor/ActionPreviewEditor.cs b/Assets/ActionTool/Editor/ActionPreviewEditor.cs index faad376c..858b28e2 100644 --- a/Assets/ActionTool/Editor/ActionPreviewEditor.cs +++ b/Assets/ActionTool/Editor/ActionPreviewEditor.cs @@ -6,6 +6,12 @@ using UnityEditor; namespace ActionTool { + public struct BoxParam + { + public ColliderData collider; + public int frame; + } + public class ActionPreviewEditor : EditorWindow { Texture m_UITextureStop; @@ -27,6 +33,8 @@ namespace ActionTool const float kFrameWidth = 10; const float kFrameHeight = 20; + float m_GridY = 0; + ActionEditorStyles styles; ActionEditorUI ui; @@ -90,6 +98,7 @@ namespace ActionTool 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); @@ -176,28 +185,6 @@ namespace ActionTool x += kToolbarControlSize; } - 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.End(); - } - 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.End(); - } - x += kToolbarControlSize; - } - void GUI_Detail() { float y = kToolbarHeight + 5; @@ -219,7 +206,9 @@ namespace ActionTool GUI_FrameText(ref y); GUI_Slider(ref y); GUI_Grid(ref y); - GUI_Events(ref y); + GUI_Events(); + GUI_Boxes(); + GUI_FrameLine(); } void GUI_FrameText(ref float y) @@ -263,6 +252,8 @@ namespace ActionTool void GUI_Grid(ref float y) { + m_GridY = y; + ActionData action = ActionManager.actionData; int sampleCount = (int)action.totalFrame + 1; @@ -290,18 +281,161 @@ namespace ActionTool x = Mathf.Clamp(x, kTimeLineViewXOffset, kTimeLineViewXOffset + action.totalFrame * kFrameWidth); ui.DrawVerticalLineFast(x, y, y + ActionManager.eventAndBoxCount * kFrameHeight, c); } + GL.PopMatrix(); + GL.End(); + + 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); + GL.PushMatrix(); + GL.LoadPixelMatrix(); + bool bWin = Application.platform == RuntimePlatform.WindowsEditor; + if (bWin) + GL.Begin(GL.QUADS); + else + GL.Begin(GL.LINES); ui.DrawVerticalLineFast(kTimeLineViewXOffset + bgRect.width * action.curAnimTimeNormal, y, y + ActionManager.eventAndBoxCount * kFrameHeight, Color.red); GL.PopMatrix(); GL.End(); + } - y += ActionManager.eventAndBoxCount * kFrameHeight; + 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 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 GUI_Events(ref float y) + void DrawBox(int index, ColliderData box, float y, Color c) { + ActionData action = ActionManager.actionData; + Color prevColor = GUI.color; + GUI.color = c; + Rect rect = new Rect(kTimeLineViewXOffset - 18, y - 1, 17, kFrameHeight); + bool selected = ActionManager.colliderData == box; + bool select = GUI.Toggle(rect, selected, index.ToString(), styles.boxToggle); + if (select) + { + ActionManager.OnSelectBox(box); + + ui.defaultUIMaterail.SetPass(0); + GL.PushMatrix(); + GL.LoadPixelMatrix(); + bool bWin = Application.platform == RuntimePlatform.WindowsEditor; + if (bWin) + GL.Begin(GL.QUADS); + else + GL.Begin(GL.LINES); + 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); + GL.PopMatrix(); + GL.End(); + } + 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); + EditorGUI.DrawRect(region, c * 0.4f); + } + 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 menu = new GenericMenu(); + menu.AddItem(new GUIContent("New Frame"), false, ActionManager.AddNewBoxFrame, param); + menu.AddItem(new GUIContent("Delete"), false, ActionManager.DeleteBoxFrame, param); + menu.ShowAsContext(); + } + else + { + Debug.LogError("[ActionTool] 错误的点击"); + } } void GUI_DrawSeperateLine(float x, float y, float height) @@ -322,6 +456,41 @@ namespace ActionTool GL.End(); } + 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; -- cgit v1.1-26-g67d0