| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- using System;
- using System.Collections.Generic;
- using BansheeEngine;
- namespace BansheeEditor
- {
- /** @addtogroup Windows
- * @{
- */
- /// <summary>
- /// Displays animation curve editor window.
- /// </summary>
- [DefaultSize(900, 500)]
- internal class AnimationWindow : EditorWindow
- {
- /// <summary>
- /// A set of animation curves for a field of a certain type.
- /// </summary>
- private struct FieldCurves
- {
- public SerializableProperty.FieldType type;
- public EdAnimationCurve[] curves;
- }
- private const int FIELD_DISPLAY_WIDTH = 200;
- private bool isInitialized;
- private GUIButton playButton;
- private GUIButton recordButton;
- private GUIButton prevFrameButton;
- private GUIIntField frameInputField;
- private GUIButton nextFrameButton;
- private GUIButton addKeyframeButton;
- private GUIButton addEventButton;
- private GUIButton optionsButton;
- private GUIButton addPropertyBtn;
- private GUIButton delPropertyBtn;
- private GUILayout buttonLayout;
- private int buttonLayoutHeight;
- private GUIPanel editorPanel;
- private GUIAnimFieldDisplay guiFieldDisplay;
- private GUICurveEditor guiCurveEditor;
- private SceneObject selectedSO;
- private int currentFrameIdx;
- private int fps = 1;
- private Dictionary<string, FieldCurves> curves = new Dictionary<string, FieldCurves>();
- private List<string> selectedFields = new List<string>();
- internal int FPS
- {
- get { return fps; }
- set { guiCurveEditor.SetFPS(value); fps = MathEx.Max(value, 1); }
- }
- /// <summary>
- /// Opens the animation window.
- /// </summary>
- [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
- private static void OpenGameWindow()
- {
- OpenWindow<AnimationWindow>();
- }
- /// <inheritdoc/>
- protected override LocString GetDisplayName()
- {
- return new LocEdString("Animation");
- }
- private void OnInitialize()
- {
- Selection.OnSelectionChanged += OnSelectionChanged;
- EditorInput.OnPointerPressed += OnPointerPressed;
- EditorInput.OnPointerMoved += OnPointerMoved;
- EditorInput.OnPointerReleased += OnPointerReleased;
- EditorInput.OnButtonUp += OnButtonUp;
- Rebuild();
- }
- private void OnEditorUpdate()
- {
- }
- private void OnDestroy()
- {
- Selection.OnSelectionChanged -= OnSelectionChanged;
- EditorInput.OnPointerPressed -= OnPointerPressed;
- EditorInput.OnPointerMoved -= OnPointerMoved;
- EditorInput.OnPointerReleased -= OnPointerReleased;
- EditorInput.OnButtonUp -= OnButtonUp;
- }
- protected override void WindowResized(int width, int height)
- {
- if (!isInitialized)
- return;
- guiFieldDisplay.SetSize(FIELD_DISPLAY_WIDTH, height - buttonLayoutHeight*2);
- int curveEditorWidth = Math.Max(0, width - FIELD_DISPLAY_WIDTH);
- guiCurveEditor.SetSize(curveEditorWidth, height - buttonLayoutHeight);
- guiCurveEditor.Redraw();
- }
- private void Rebuild()
- {
- GUI.Clear();
- selectedFields.Clear();
- curves.Clear();
- isInitialized = false;
- selectedSO = Selection.SceneObject;
- if (selectedSO == null)
- {
- GUILabel warningLbl = new GUILabel(new LocEdString("Select an object to animate in the Hierarchy or Scene windows."));
- GUILayoutY vertLayout = GUI.AddLayoutY();
- vertLayout.AddFlexibleSpace();
- GUILayoutX horzLayout = vertLayout.AddLayoutX();
- vertLayout.AddFlexibleSpace();
- horzLayout.AddFlexibleSpace();
- horzLayout.AddElement(warningLbl);
- horzLayout.AddFlexibleSpace();
-
- return;
- }
- // TODO - Retrieve Animation & AnimationClip from the selected object, fill curves dictionary
- // - If not available, show a button to create new animation clip
- // Top button row
- GUIContent playIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Play),
- new LocEdString("Play"));
- GUIContent recordIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Record),
- new LocEdString("Record"));
- GUIContent prevFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameBack),
- new LocEdString("Previous frame"));
- GUIContent nextFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameForward),
- new LocEdString("Next frame"));
- GUIContent addKeyframeIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddKeyframe),
- new LocEdString("Add keyframe"));
- GUIContent addEventIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddEvent),
- new LocEdString("Add event"));
- GUIContent optionsIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Options),
- new LocEdString("Options"));
- playButton = new GUIButton(playIcon);
- recordButton = new GUIButton(recordIcon);
- prevFrameButton = new GUIButton(prevFrameIcon);
- frameInputField = new GUIIntField();
- nextFrameButton = new GUIButton(nextFrameIcon);
- addKeyframeButton = new GUIButton(addKeyframeIcon);
- addEventButton = new GUIButton(addEventIcon);
- optionsButton = new GUIButton(optionsIcon);
- playButton.OnClick += () =>
- {
- // TODO
- // - Record current state of the scene object hierarchy
- // - Evaluate all curves manually and update them
- // - On end, restore original values of the scene object hierarchy
- };
- recordButton.OnClick += () =>
- {
- // TODO
- // - Every frame read back current values of all the current curve's properties and assign it to the current frame
- };
- prevFrameButton.OnClick += () =>
- {
- SetCurrentFrame(currentFrameIdx - 1);
- };
- frameInputField.OnChanged += SetCurrentFrame;
- nextFrameButton.OnClick += () =>
- {
- SetCurrentFrame(currentFrameIdx + 1);
- };
- addKeyframeButton.OnClick += () =>
- {
- guiCurveEditor.AddKeyFrameAtMarker();
- // TODO - Update local curves?
- };
- addEventButton.OnClick += () =>
- {
- // TODO - Add event
- };
- optionsButton.OnClick += () =>
- {
- Vector2I openPosition = ScreenToWindowPos(Input.PointerPosition);
- AnimationOptions dropDown = DropDownWindow.Open<AnimationOptions>(this, openPosition);
- dropDown.Initialize(this);
- };
- // Property buttons
- addPropertyBtn = new GUIButton(new LocEdString("Add property"));
- delPropertyBtn = new GUIButton(new LocEdString("Delete selected"));
- addPropertyBtn.OnClick += () =>
- {
- Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
- FieldSelectionWindow fieldSelection = DropDownWindow.Open<FieldSelectionWindow>(this, windowPos);
- fieldSelection.OnFieldSelected += OnFieldAdded;
- };
- delPropertyBtn.OnClick += () =>
- {
- LocEdString title = new LocEdString("Warning");
- LocEdString message = new LocEdString("Are you sure you want to remove all selected fields?");
- DialogBox.Open(title, message, DialogBox.Type.YesNo, x =>
- {
- if (x == DialogBox.ResultType.Yes)
- {
- RemoveSelectedFields();
- }
- });
- };
- GUILayout mainLayout = GUI.AddLayoutY();
- buttonLayout = mainLayout.AddLayoutX();
- buttonLayout.AddSpace(5);
- buttonLayout.AddElement(playButton);
- buttonLayout.AddElement(recordButton);
- buttonLayout.AddSpace(5);
- buttonLayout.AddElement(prevFrameButton);
- buttonLayout.AddElement(frameInputField);
- buttonLayout.AddElement(nextFrameButton);
- buttonLayout.AddSpace(5);
- buttonLayout.AddElement(addKeyframeButton);
- buttonLayout.AddElement(addEventButton);
- buttonLayout.AddSpace(5);
- buttonLayout.AddElement(optionsButton);
- buttonLayout.AddFlexibleSpace();
- buttonLayoutHeight = playButton.Bounds.height;
- GUILayout contentLayout = mainLayout.AddLayoutX();
- GUILayout fieldDisplayLayout = contentLayout.AddLayoutY(GUIOption.FixedWidth(FIELD_DISPLAY_WIDTH));
- guiFieldDisplay = new GUIAnimFieldDisplay(fieldDisplayLayout, FIELD_DISPLAY_WIDTH,
- Height - buttonLayoutHeight * 2, selectedSO);
- guiFieldDisplay.OnEntrySelected += OnFieldSelected;
- GUILayout bottomButtonLayout = fieldDisplayLayout.AddLayoutX();
- bottomButtonLayout.AddElement(addPropertyBtn);
- bottomButtonLayout.AddElement(delPropertyBtn);
- GUILayout curveLayout = contentLayout.AddLayoutY();
- editorPanel = curveLayout.AddPanel();
- int curveEditorWidth = Math.Max(0, Width - FIELD_DISPLAY_WIDTH);
- guiCurveEditor = new GUICurveEditor(this, editorPanel, curveEditorWidth, Height - buttonLayoutHeight);
- guiCurveEditor.OnFrameSelected += OnFrameSelected;
- guiCurveEditor.Redraw();
- SetCurrentFrame(currentFrameIdx);
- isInitialized = true;
- }
- private void SetCurrentFrame(int frameIdx)
- {
- currentFrameIdx = Math.Max(0, frameIdx);
- frameInputField.Value = currentFrameIdx;
- guiCurveEditor.SetMarkedFrame(currentFrameIdx);
- float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
- List<GUIAnimFieldPathValue> values = new List<GUIAnimFieldPathValue>();
- foreach (var kvp in curves)
- {
- SerializableProperty property = GUIAnimFieldDisplay.FindProperty(selectedSO, kvp.Key);
- if (property != null)
- {
- GUIAnimFieldPathValue fieldValue = new GUIAnimFieldPathValue();
- fieldValue.path = kvp.Key;
- switch (kvp.Value.type)
- {
- case SerializableProperty.FieldType.Vector2:
- {
- Vector2 value = new Vector2();
- for(int i = 0; i < 2; i++)
- value[i] = kvp.Value.curves[i].Evaluate(time, false);
- fieldValue.value = value;
- }
- break;
- case SerializableProperty.FieldType.Vector3:
- {
- Vector3 value = new Vector3();
- for (int i = 0; i < 3; i++)
- value[i] = kvp.Value.curves[i].Evaluate(time, false);
- fieldValue.value = value;
- }
- break;
- case SerializableProperty.FieldType.Vector4:
- {
- Vector4 value = new Vector4();
- for (int i = 0; i < 4; i++)
- value[i] = kvp.Value.curves[i].Evaluate(time, false);
- fieldValue.value = value;
- }
- break;
- case SerializableProperty.FieldType.Color:
- {
- Color value = new Color();
- for (int i = 0; i < 4; i++)
- value[i] = kvp.Value.curves[i].Evaluate(time, false);
- fieldValue.value = value;
- }
- break;
- case SerializableProperty.FieldType.Bool:
- case SerializableProperty.FieldType.Int:
- case SerializableProperty.FieldType.Float:
- fieldValue.value = kvp.Value.curves[0].Evaluate(time, false); ;
- break;
- }
- values.Add(fieldValue);
- }
- }
- guiFieldDisplay.SetDisplayValues(values.ToArray());
- }
- private void OnPointerPressed(PointerEvent ev)
- {
- if (!isInitialized)
- return;
- guiCurveEditor.OnPointerPressed(ev);
- }
- private void OnPointerMoved(PointerEvent ev)
- {
- if (!isInitialized)
- return;
- guiCurveEditor.OnPointerMoved(ev);
- }
- private void OnPointerReleased(PointerEvent ev)
- {
- if (!isInitialized)
- return;
- guiCurveEditor.OnPointerReleased(ev);
- }
- private void OnButtonUp(ButtonEvent ev)
- {
- if (!isInitialized)
- return;
- guiCurveEditor.OnButtonUp(ev);
- }
- private void UpdateDisplayedCurves()
- {
- List<EdAnimationCurve> curvesToDisplay = new List<EdAnimationCurve>();
- for (int i = 0; i < selectedFields.Count; i++)
- {
- EdAnimationCurve curve;
- if(TryGetCurve(selectedFields[i], out curve))
- curvesToDisplay.Add(curve);
- }
- guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
- float xRange;
- float yRange;
- CalculateRange(curvesToDisplay, out xRange, out yRange);
- // Don't allow zero range
- if (xRange == 0.0f)
- xRange = 60.0f;
- if (yRange == 0.0f)
- yRange = 10.0f;
- // Add padding to y range
- yRange *= 1.05f;
- // Don't reduce visible range
- xRange = Math.Max(xRange, guiCurveEditor.XRange);
- yRange = Math.Max(yRange, guiCurveEditor.YRange);
- guiCurveEditor.SetRange(xRange, yRange);
- guiCurveEditor.Redraw();
- }
- private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
- {
- xRange = 0.0f;
- yRange = 0.0f;
- foreach (var curve in curves)
- {
- KeyFrame[] keyframes = curve.KeyFrames;
- foreach (var key in keyframes)
- {
- xRange = Math.Max(xRange, key.time);
- yRange = Math.Max(yRange, Math.Abs(key.value));
- }
- }
- }
- private bool TryGetCurve(string path, out EdAnimationCurve curve)
- {
- int index = path.LastIndexOf(".");
- string parentPath;
- string subPathSuffix = null;
- if (index == -1)
- {
- parentPath = path;
- }
- else
- {
- parentPath = path.Substring(0, index);
- subPathSuffix = path.Substring(index, path.Length - index);
- }
- FieldCurves fieldCurves;
- if (curves.TryGetValue(parentPath, out fieldCurves))
- {
- if (!string.IsNullOrEmpty(subPathSuffix))
- {
- if (subPathSuffix == ".x" || subPathSuffix == ".r")
- {
- curve = fieldCurves.curves[0];
- return true;
- }
- else if (subPathSuffix == ".y" || subPathSuffix == ".g")
- {
- curve = fieldCurves.curves[1];
- return true;
- }
- else if (subPathSuffix == ".z" || subPathSuffix == ".b")
- {
- curve = fieldCurves.curves[2];
- return true;
- }
- else if (subPathSuffix == ".w" || subPathSuffix == ".a")
- {
- curve = fieldCurves.curves[3];
- return true;
- }
- }
- else
- {
- curve = fieldCurves.curves[0];
- return true;
- }
- }
- curve = null;
- return false;
- }
- private void OnFieldAdded(string path, SerializableProperty.FieldType type)
- {
- guiFieldDisplay.AddField(path);
- switch (type)
- {
- case SerializableProperty.FieldType.Vector4:
- {
- FieldCurves fieldCurves = new FieldCurves();
- fieldCurves.type = type;
- fieldCurves.curves = new EdAnimationCurve[4];
- string[] subPaths = { ".x", ".y", ".z", ".w" };
- for (int i = 0; i < subPaths.Length; i++)
- {
- string subFieldPath = path + subPaths[i];
- fieldCurves.curves[i] = new EdAnimationCurve();
- selectedFields.Add(subFieldPath);
- }
- curves[path] = fieldCurves;
- }
- break;
- case SerializableProperty.FieldType.Vector3:
- {
- FieldCurves fieldCurves = new FieldCurves();
- fieldCurves.type = type;
- fieldCurves.curves = new EdAnimationCurve[3];
- string[] subPaths = { ".x", ".y", ".z" };
- for (int i = 0; i < subPaths.Length; i++)
- {
- string subFieldPath = path + subPaths[i];
- fieldCurves.curves[i] = new EdAnimationCurve();
- selectedFields.Add(subFieldPath);
- }
- curves[path] = fieldCurves;
- }
- break;
- case SerializableProperty.FieldType.Vector2:
- {
- FieldCurves fieldCurves = new FieldCurves();
- fieldCurves.type = type;
- fieldCurves.curves = new EdAnimationCurve[2];
- string[] subPaths = { ".x", ".y" };
- for (int i = 0; i < subPaths.Length; i++)
- {
- string subFieldPath = path + subPaths[i];
- fieldCurves.curves[i] = new EdAnimationCurve();
- selectedFields.Add(subFieldPath);
- }
- curves[path] = fieldCurves;
- }
- break;
- case SerializableProperty.FieldType.Color:
- {
- FieldCurves fieldCurves = new FieldCurves();
- fieldCurves.type = type;
- fieldCurves.curves = new EdAnimationCurve[4];
- string[] subPaths = { ".r", ".g", ".b", ".a" };
- for (int i = 0; i < subPaths.Length; i++)
- {
- string subFieldPath = path + subPaths[i];
- fieldCurves.curves[i] = new EdAnimationCurve();
- selectedFields.Add(subFieldPath);
- }
- curves[path] = fieldCurves;
- }
- break;
- default: // Primitive type
- {
- FieldCurves fieldCurves = new FieldCurves();
- fieldCurves.type = type;
- fieldCurves.curves = new EdAnimationCurve[1];
- fieldCurves.curves[0] = new EdAnimationCurve();
- selectedFields.Add(path);
- curves[path] = fieldCurves;
- }
- break;
- }
- UpdateDisplayedCurves();
- }
- private bool IsPathParent(string child, string parent)
- {
- string[] childEntries = child.Split('/', '.');
- string[] parentEntries = parent.Split('/', '.');
- if (parentEntries.Length >= child.Length)
- return false;
- int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
- for (int i = 0; i < compareLength; i++)
- {
- if (childEntries[i] != parentEntries[i])
- return false;
- }
- return true;
- }
- private string GetSubPathParent(string path)
- {
- int index = path.LastIndexOf(".");
- if (index == -1)
- return path;
- return path.Substring(0, index);
- }
- private void OnFieldSelected(string path)
- {
- if (!Input.IsButtonHeld(ButtonCode.LeftShift) && !Input.IsButtonHeld(ButtonCode.RightShift))
- selectedFields.Clear();
- if (!string.IsNullOrEmpty(path))
- {
- selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
- selectedFields.Add(path);
- }
- guiFieldDisplay.SetSelection(selectedFields.ToArray());
- UpdateDisplayedCurves();
- }
- private void RemoveSelectedFields()
- {
- for (int i = 0; i < selectedFields.Count; i++)
- {
- selectedFields.Remove(selectedFields[i]);
- curves.Remove(GetSubPathParent(selectedFields[i]));
- }
- List<string> existingFields = new List<string>();
- foreach(var KVP in curves)
- existingFields.Add(KVP.Key);
- guiFieldDisplay.SetFields(existingFields.ToArray());
- selectedFields.Clear();
- UpdateDisplayedCurves();
- }
- private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
- {
- Rebuild();
- }
- private void OnFrameSelected(int frameIdx)
- {
- SetCurrentFrame(frameIdx);
- }
- }
- /// <summary>
- /// Drop down window that displays options used by the animation window.
- /// </summary>
- [DefaultSize(100, 50)]
- internal class AnimationOptions : DropDownWindow
- {
- private AnimationWindow parent;
- /// <summary>
- /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
- /// use.
- /// </summary>
- /// <param name="parent">Animation window that this drop down window is a part of.</param>
- internal void Initialize(AnimationWindow parent)
- {
- this.parent = parent;
- GUIIntField fpsField = new GUIIntField(new LocEdString("FPS"), 40);
- fpsField.Value = parent.FPS;
- fpsField.OnChanged += x => { parent.FPS = x; };
-
- GUILayoutY vertLayout = GUI.AddLayoutY();
- vertLayout.AddFlexibleSpace();
- GUILayoutX contentLayout = vertLayout.AddLayoutX();
- contentLayout.AddFlexibleSpace();
- contentLayout.AddElement(fpsField);
- contentLayout.AddFlexibleSpace();
- vertLayout.AddFlexibleSpace();
- }
- }
- /** @} */
- }
|