| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2018 Marko Pintera ([email protected]). All rights reserved. **********************//
- using System;
- using System.Collections.Generic;
- using bs;
- namespace bs.Editor
- {
- /** @addtogroup AnimationEditor
- * @{
- */
- /// <summary>
- /// Displays a set of animation curves and events. Allows manipulation of both by adding, removing and modifying
- /// curve keyframes, and animation events.
- /// </summary>
- internal class GUICurveEditor
- {
- /// <summary>
- /// Information about currently selected set of keyframes for a specific curve.
- /// </summary>
- class SelectedKeyframes
- {
- public int curveIdx;
- public List<int> keyIndices = new List<int>();
- }
- /// <summary>
- /// Information about a keyframe that is currently being dragged.
- /// </summary>
- struct DraggedKeyframe
- {
- public DraggedKeyframe(int index, KeyFrame original)
- {
- this.index = index;
- this.original = original;
- }
- public int index;
- public KeyFrame original;
- }
- /// <summary>
- /// Information about all keyframes of a specific curve that are currently being dragged.
- /// </summary>
- class DraggedKeyframes
- {
- public int curveIdx;
- public List<DraggedKeyframe> keys = new List<DraggedKeyframe>();
- }
- /// <summary>
- /// Data about an animation event.
- /// </summary>
- class EventInfo
- {
- public AnimationEvent animEvent;
- public bool selected;
- }
- private const int TIMELINE_HEIGHT = 20;
- private const int VERT_PADDING = 2;
- private const int EVENTS_HEIGHT = 15;
- private const int SIDEBAR_WIDTH = 30;
- private const int DRAG_START_DISTANCE = 3;
- private const float DRAG_SCALE = 3.0f;
- private const float ZOOM_SCALE = 0.1f/120.0f; // One scroll step is usually 120 units, we want 1/10 of that
- private GUILayout gui;
- private GUILayout mainPanel;
- private GUIPanel drawingPanel;
- private GUIPanel eventsPanel;
- private GUITexture timelineBackground;
- private GUITexture eventsBackground;
- private GUIGraphTime guiTimeline;
- private GUIAnimEvents guiEvents;
- private GUICurves guiCurveDrawing;
- private GUIGraphValues guiSidebar;
- private int scrollBarWidth;
- private int scrollBarHeight;
- private GUIResizeableScrollBarH horzScrollBar;
- private GUIResizeableScrollBarV vertScrollBar;
- private ContextMenu blankContextMenu;
- private ContextMenu keyframeContextMenu;
- private ContextMenu blankEventContextMenu;
- private ContextMenu eventContextMenu;
- private Vector2I contextClickPosition;
- private EdCurveDrawInfo[] curveInfos = new EdCurveDrawInfo[0];
- private bool disableCurveEdit = false;
- private float xRange = 60.0f;
- private float yRange = 10.0f;
- private Vector2 offset;
- private int width;
- private int height;
- private int markedFrameIdx;
- private List<SelectedKeyframes> selectedKeyframes = new List<SelectedKeyframes>();
- private bool showEvents = true;
- private List<EventInfo> events = new List<EventInfo>();
- private SceneObject eventsSO;
- private bool isPointerHeld;
- private bool isMousePressedOverKey;
- private bool isMousePressedOverTangent;
- private bool isDragInProgress;
- private bool isModifiedDuringDrag;
- private List<DraggedKeyframes> draggedKeyframes = new List<DraggedKeyframes>();
- private TangentRef draggedTangent;
- private Vector2I dragStart;
- /// <summary>
- /// Triggers whenever user selects a new frame. Reports the index of the selected frame.
- /// </summary>
- public Action<int> OnFrameSelected;
- /// <summary>
- /// Triggered whenever a new animation event is added.
- /// </summary>
- public Action OnEventAdded;
- /// <summary>
- /// Triggered whenever values in an animation event change.
- /// </summary>
- public Action OnEventModified;
- /// <summary>
- /// Triggered whenever an animation event is deleted.
- /// </summary>
- public Action OnEventDeleted;
- /// <summary>
- /// Triggered whenever keyframe in a curve is modified (added, removed or edited).
- /// </summary>
- public Action OnCurveModified;
- /// <summary>
- /// Triggered when the user clicks anywhere on the curve editor area.
- /// </summary>
- public Action OnClicked;
- /// <summary>
- /// The displayed range of the curve, where:
- /// .x - Range of the horizontal area. Displayed area ranges from [0, x].
- /// .y - Range of the vertical area. Displayed area ranges from [-y, y].
- /// </summary>
- public Vector2 Range
- {
- get { return new Vector2(xRange, yRange); }
- set
- {
- xRange = value.x;
- yRange = value.y;
- guiTimeline.SetRange(xRange);
- guiCurveDrawing.SetRange(xRange, yRange * 2.0f);
- guiSidebar.SetRange(offset.y - yRange, offset.y + yRange);
- if(showEvents)
- guiEvents.SetRange(xRange);
- Redraw();
- }
- }
- /// <summary>
- /// Determines how much to offset the displayed curve values.
- /// </summary>
- public Vector2 Offset
- {
- get { return offset; }
- set
- {
- offset = value;
- guiTimeline.SetOffset(offset.x);
- guiCurveDrawing.SetOffset(offset);
- guiSidebar.SetRange(offset.y - yRange, offset.y + yRange);
- if(showEvents)
- guiEvents.SetOffset(offset.x);
- Redraw();
- }
- }
- /// <summary>
- /// Returns the width of the curve editor, in pixels.
- /// </summary>
- public int Width
- {
- get { return width; }
- }
- /// <summary>
- /// Returns the height of the curve editor, in pixels.
- /// </summary>
- public int Height
- {
- get { return height; }
- }
- /// <summary>
- /// Set to true if curves are not allowed to be edited.
- /// </summary>
- public bool DisableCurveEdit
- {
- set { disableCurveEdit = value; }
- }
- /// <summary>
- /// Animation events displayed on the curve editor.
- /// </summary>
- public AnimationEvent[] Events
- {
- get
- {
- AnimationEvent[] animEvents = new AnimationEvent[events.Count];
- // Note: Hidden dependency. Returned events must point to the same event class this object is using, so
- // that any modifications made in this class will be visible in the returned values.
- for (int i = 0; i < events.Count; i++)
- animEvents[i] = events[i].animEvent;
- return animEvents;
- }
- set
- {
- events.Clear();
- for (int i = 0; i < value.Length; i++)
- {
- EventInfo eventInfo = new EventInfo();
- eventInfo.animEvent = value[i];
- events.Add(eventInfo);
- }
- UpdateEventsGUI();
- }
- }
- /// <summary>
- /// Creates a new curve editor GUI elements.
- /// </summary>
- /// <param name="gui">GUI layout into which to place the GUI element.</param>
- /// <param name="width">Width in pixels.</param>
- /// <param name="height">Height in pixels.</param>
- /// <param name="showEvents">If true show events on the graph and allow their editing.</param>
- /// <param name="drawOptions">Options that control which elements to display when drawing curves.</param>
- public GUICurveEditor(GUILayout gui, int width, int height, bool showEvents,
- CurveDrawOptions drawOptions = CurveDrawOptions.DrawMarkers | CurveDrawOptions.DrawKeyframes)
- {
- this.gui = gui;
- this.showEvents = showEvents;
- blankContextMenu = new ContextMenu();
- blankContextMenu.AddItem("Add keyframe", AddKeyframeAtPosition);
- blankEventContextMenu = new ContextMenu();
- blankEventContextMenu.AddItem("Add event", AddEventAtPosition);
- keyframeContextMenu = new ContextMenu();
- keyframeContextMenu.AddItem("Delete", DeleteSelectedKeyframes);
- keyframeContextMenu.AddItem("Edit", EditSelectedKeyframe);
- keyframeContextMenu.AddItem("Tangents/Auto", () => { ChangeSelectionTangentMode(TangentMode.Auto); });
- keyframeContextMenu.AddItem("Tangents/Free", () => { ChangeSelectionTangentMode(TangentMode.Free); });
- keyframeContextMenu.AddItem("Tangents/In/Auto", () => { ChangeSelectionTangentMode(TangentMode.InAuto); });
- keyframeContextMenu.AddItem("Tangents/In/Free", () => { ChangeSelectionTangentMode(TangentMode.InFree); });
- keyframeContextMenu.AddItem("Tangents/In/Linear", () => { ChangeSelectionTangentMode(TangentMode.InLinear); });
- keyframeContextMenu.AddItem("Tangents/In/Step", () => { ChangeSelectionTangentMode(TangentMode.InStep); });
- keyframeContextMenu.AddItem("Tangents/Out/Auto", () => { ChangeSelectionTangentMode(TangentMode.OutAuto); });
- keyframeContextMenu.AddItem("Tangents/Out/Free", () => { ChangeSelectionTangentMode(TangentMode.OutFree); });
- keyframeContextMenu.AddItem("Tangents/Out/Linear", () => { ChangeSelectionTangentMode(TangentMode.OutLinear); });
- keyframeContextMenu.AddItem("Tangents/Out/Step", () => { ChangeSelectionTangentMode(TangentMode.OutStep); });
- eventContextMenu = new ContextMenu();
- eventContextMenu.AddItem("Delete", DeleteSelectedEvents);
- eventContextMenu.AddItem("Edit", EditSelectedEvent);
- horzScrollBar = new GUIResizeableScrollBarH();
- horzScrollBar.OnScrollOrResize += OnHorzScrollOrResize;
- vertScrollBar = new GUIResizeableScrollBarV();
- vertScrollBar.OnScrollOrResize += OnVertScrollOrResize;
- GUILayout mainLayout = gui.AddLayoutY();
- GUILayout curveLayoutHorz = mainLayout.AddLayoutX();
- GUILayout horzScrollBarLayout = mainLayout.AddLayoutX();
- horzScrollBarLayout.AddElement(horzScrollBar);
- horzScrollBarLayout.AddFlexibleSpace();
- mainPanel = curveLayoutHorz.AddPanel();
- curveLayoutHorz.AddElement(vertScrollBar);
- curveLayoutHorz.AddFlexibleSpace();
- scrollBarHeight = horzScrollBar.Bounds.height;
- scrollBarWidth = vertScrollBar.Bounds.width;
- this.width = Math.Max(0, width - scrollBarWidth);
- this.height = Math.Max(0, height - scrollBarHeight);
- GUIPanel timelinePanel = mainPanel.AddPanel();
- guiTimeline = new GUIGraphTime(timelinePanel, this.width, TIMELINE_HEIGHT);
- GUIPanel timelineBgPanel = mainPanel.AddPanel(1);
- timelineBackground = new GUITexture(null, EditorStyles.Header);
- timelineBackground.Bounds = new Rect2I(0, 0, this.width, TIMELINE_HEIGHT + VERT_PADDING);
- timelineBgPanel.AddElement(timelineBackground);
- int eventsHeaderHeight = 0;
- if (showEvents)
- {
- eventsPanel = mainPanel.AddPanel();
- eventsPanel.SetPosition(0, TIMELINE_HEIGHT + VERT_PADDING);
- guiEvents = new GUIAnimEvents(eventsPanel, this.width, EVENTS_HEIGHT);
- GUIPanel eventsBgPanel = eventsPanel.AddPanel(1);
- eventsBackground = new GUITexture(null, EditorStyles.Header);
- eventsBackground.Bounds = new Rect2I(0, 0, this.width, EVENTS_HEIGHT + VERT_PADDING);
- eventsBgPanel.AddElement(eventsBackground);
- eventsHeaderHeight = EVENTS_HEIGHT;
- }
- drawingPanel = mainPanel.AddPanel();
- drawingPanel.SetPosition(0, TIMELINE_HEIGHT + eventsHeaderHeight + VERT_PADDING);
- guiCurveDrawing = new GUICurves(drawOptions);
- guiCurveDrawing.SetWidth(this.width);
- guiCurveDrawing.SetHeight(this.height - TIMELINE_HEIGHT - eventsHeaderHeight - VERT_PADDING * 2);
- guiCurveDrawing.SetRange(60.0f, 20.0f);
- guiCurveDrawing.Curves = GetPlainCurveDrawInfos();
- drawingPanel.AddElement(guiCurveDrawing);
- GUIPanel sidebarPanel = mainPanel.AddPanel(-10);
- sidebarPanel.SetPosition(0, TIMELINE_HEIGHT + eventsHeaderHeight + VERT_PADDING);
- guiSidebar = new GUIGraphValues(sidebarPanel, SIDEBAR_WIDTH, this.height - TIMELINE_HEIGHT -
- eventsHeaderHeight - VERT_PADDING * 2);
- guiSidebar.SetRange(-10.0f, 10.0f);
- horzScrollBar.SetWidth(this.width);
- vertScrollBar.SetHeight(this.height);
- UpdateScrollBarSize();
- }
- /// <summary>
- /// Change the set of curves to display.
- /// </summary>
- /// <param name="curveInfos">New set of curves to draw on the GUI element.</param>
- public void SetCurves(EdCurveDrawInfo[] curveInfos)
- {
- this.curveInfos = curveInfos;
- guiCurveDrawing.Curves = GetPlainCurveDrawInfos();
- Redraw();
- }
- /// <summary>
- /// Change the physical size of the GUI element.
- /// </summary>
- /// <param name="width">Width of the element in pixels.</param>
- /// <param name="height">Height of the element in pixels.</param>
- public void SetSize(int width, int height)
- {
- this.width = Math.Max(0, width - scrollBarWidth);
- this.height = Math.Max(0, height - scrollBarHeight);
- int eventsHeaderHeight = 0;
- if (showEvents)
- {
- eventsHeaderHeight = EVENTS_HEIGHT;
- guiEvents.SetSize(this.width, EVENTS_HEIGHT);
- eventsBackground.Bounds = new Rect2I(0, 0, this.width, EVENTS_HEIGHT + VERT_PADDING);
- }
- guiTimeline.SetSize(this.width, TIMELINE_HEIGHT);
- guiCurveDrawing.SetWidth(this.width);
- guiCurveDrawing.SetHeight(this.height - TIMELINE_HEIGHT - eventsHeaderHeight);
- guiSidebar.SetSize(SIDEBAR_WIDTH, this.height - TIMELINE_HEIGHT - eventsHeaderHeight);
- timelineBackground.Bounds = new Rect2I(0, 0, this.width, TIMELINE_HEIGHT + VERT_PADDING);
- horzScrollBar.SetWidth(this.width);
- vertScrollBar.SetHeight(this.height);
- UpdateScrollBarSize();
- UpdateScrollBarPosition();
- }
- /// <summary>
- /// Number of frames per second, used for frame selection and marking.
- /// </summary>
- /// <param name="fps">Number of prames per second.</param>
- public void SetFPS(int fps)
- {
- guiTimeline.SetFPS(fps);
- guiCurveDrawing.FPS = fps;
- if(showEvents)
- guiEvents.SetFPS(fps);
- Redraw();
- }
- /// <summary>
- /// Sets a scene object that will be used for enumerating components/methods used for adding events.
- /// </summary>
- /// <param name="so">Scene object containing the animation component.</param>
- public void SetEventSceneObject(SceneObject so)
- {
- eventsSO = so;
- }
- /// <summary>
- /// Returns time for a frame with the specified index. Depends on set range and FPS.
- /// </summary>
- /// <param name="frameIdx">Index of the frame (not a key-frame) to get the time for.</param>
- /// <returns>Time of the frame with the provided index. </returns>
- public float GetTimeForFrame(int frameIdx)
- {
- return guiCurveDrawing.GetTimeForFrame(frameIdx);
- }
- /// <summary>
- /// Sets the frame at which to display the frame marker.
- /// </summary>
- /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
- public void SetMarkedFrame(int frameIdx)
- {
- markedFrameIdx = frameIdx;
- guiTimeline.SetMarkedFrame(frameIdx);
- guiCurveDrawing.MarkedFrame = frameIdx;
- if(showEvents)
- guiEvents.SetMarkedFrame(frameIdx);
- Redraw();
- }
- /// <summary>
- /// Adds a new keyframe at the currently selected frame.
- /// </summary>
- public void AddKeyFrameAtMarker()
- {
- ClearSelection();
- if (!disableCurveEdit)
- {
- foreach (var curveInfo in curveInfos)
- {
- float t = guiCurveDrawing.GetTimeForFrame(markedFrameIdx);
- float value = curveInfo.curve.Evaluate(t);
- curveInfo.curve.AddOrUpdateKeyframe(t, value);
- curveInfo.curve.Apply();
- }
- }
- else
- ShowReadOnlyMessage();
- OnCurveModified?.Invoke();
- RefreshCurveDrawing();
- UpdateEventsGUI();
- }
- /// <summary>
- /// Adds a new event at the currently selected event.
- /// </summary>
- public void AddEventAtMarker()
- {
- ClearSelection();
- if (!showEvents)
- return;
- float eventTime = guiEvents.GetTimeForFrame(markedFrameIdx);
- EventInfo eventInfo = new EventInfo();
- eventInfo.animEvent = new AnimationEvent("", eventTime);
- events.Add(eventInfo);
- OnEventAdded?.Invoke();
- UpdateEventsGUI();
- StartEventEdit(events.Count - 1);
- }
- /// <summary>
- /// Rebuilds GUI displaying the animation events list.
- /// </summary>
- private void UpdateEventsGUI()
- {
- if (!showEvents)
- return;
- AnimationEvent[] animEvents = new AnimationEvent[events.Count];
- bool[] selected = new bool[events.Count];
- for (int i = 0; i < events.Count; i++)
- {
- animEvents[i] = events[i].animEvent;
- selected[i] = events[i].selected;
- }
- guiEvents.SetEvents(animEvents, selected);
- guiEvents.Rebuild();
- }
- /// <summary>
- /// Rebuilds the entire curve editor GUI.
- /// </summary>
- public void Redraw()
- {
- guiTimeline.Rebuild();
- guiSidebar.Rebuild();
- if(showEvents)
- guiEvents.Rebuild();
- }
- /// <summary>
- /// Updates the curve drawing. Should be called after animation curves change.
- /// </summary>
- private void RefreshCurveDrawing()
- {
- guiCurveDrawing.Curves = GetPlainCurveDrawInfos();
- }
- /// <summary>
- /// Changes the tangent mode for all currently selected keyframes.
- /// </summary>
- /// <param name="mode">Tangent mode to set. If only in or out tangent mode is provided, the mode for the opposite
- /// tangent will be kept as is.</param>
- private void ChangeSelectionTangentMode(TangentMode mode)
- {
- if (disableCurveEdit)
- {
- ShowReadOnlyMessage();
- return;
- }
- foreach (var selectedEntry in selectedKeyframes)
- {
- EdAnimationCurve curve = curveInfos[selectedEntry.curveIdx].curve;
- foreach (var keyframeIdx in selectedEntry.keyIndices)
- {
- if (mode == TangentMode.Auto || mode == TangentMode.Free)
- {
- curve.SetTangentMode(keyframeIdx, mode);
- // Refresh tangent display
- guiCurveDrawing.SelectKeyframe(new KeyframeRef(selectedEntry.curveIdx, keyframeIdx), mode, true);
- }
- else
- {
- TangentMode newMode = curve.TangentModes[keyframeIdx];
- if (mode.HasFlag((TangentMode) TangentType.In))
- {
- // Replace only the in tangent mode, keeping the out tangent as is
- TangentMode inFlags = (TangentMode.InAuto | TangentMode.InFree | TangentMode.InLinear |
- TangentMode.InStep);
- newMode &= ~inFlags;
- newMode |= (mode & inFlags);
- }
- else
- {
- // Replace only the out tangent mode, keeping the in tangent as is
- TangentMode outFlags = (TangentMode.OutAuto | TangentMode.OutFree | TangentMode.OutLinear |
- TangentMode.OutStep);
- newMode &= ~outFlags;
- newMode |= (mode & outFlags);
- }
- curve.SetTangentMode(keyframeIdx, newMode);
- // Refresh tangent display
- guiCurveDrawing.SelectKeyframe(new KeyframeRef(selectedEntry.curveIdx, keyframeIdx), newMode, true);
- }
- }
- curve.Apply();
- }
- OnCurveModified?.Invoke();
- RefreshCurveDrawing();
- }
- /// <summary>
- /// Adds a new keyframe at the specified time on the provided curve.
- /// </summary>
- /// <param name="curveIdx">Index of the curve to add the keyframe to.</param>
- /// <param name="time">Time at which to add the keyframe.</param>
- private void AddKeyframe(int curveIdx, float time)
- {
- ClearSelection();
- if (!disableCurveEdit)
- {
- if (curveIdx < curveInfos.Length)
- {
- EdAnimationCurve curve = curveInfos[curveIdx].curve;
- float value = curve.Evaluate(time, false);
- curve.AddOrUpdateKeyframe(time, value);
- curve.Apply();
- }
- }
- else
- ShowReadOnlyMessage();
- OnCurveModified?.Invoke();
- RefreshCurveDrawing();
- UpdateEventsGUI();
- }
- /// <summary>
- /// Adds a new keyframe at the position the context menu was opened at.
- /// </summary>
- private void AddKeyframeAtPosition()
- {
- Vector2 curveCoord;
- if (guiCurveDrawing.PixelToCurveSpace(contextClickPosition, out curveCoord))
- {
- ClearSelection();
- if (!disableCurveEdit)
- {
- foreach (var curveInfo in curveInfos)
- {
- float t = curveCoord.x;
- float value = curveCoord.y;
- curveInfo.curve.AddOrUpdateKeyframe(t, value);
- curveInfo.curve.Apply();
- }
- }
- else
- ShowReadOnlyMessage();
- OnCurveModified?.Invoke();
- RefreshCurveDrawing();
- UpdateEventsGUI();
- }
- }
- /// <summary>
- /// Adds a new event at the position the context menu was opened at.
- /// </summary>
- private void AddEventAtPosition()
- {
- if (!showEvents)
- return;
- int frame = guiEvents.GetFrame(contextClickPosition);
- if (frame != -1)
- {
- ClearSelection();
- float time = guiEvents.GetTime(contextClickPosition.x);
- EventInfo eventInfo = new EventInfo();
- eventInfo.animEvent = new AnimationEvent("", time);
- events.Add(eventInfo);
- OnEventAdded?.Invoke();
- UpdateEventsGUI();
- StartEventEdit(events.Count - 1);
- }
- }
- /// <summary>
- /// Removes all currently selected keyframes from the curves.
- /// </summary>
- private void DeleteSelectedKeyframes()
- {
- if (!disableCurveEdit)
- {
- foreach (var selectedEntry in selectedKeyframes)
- {
- EdAnimationCurve curve = curveInfos[selectedEntry.curveIdx].curve;
- // Sort keys from highest to lowest so the indices don't change
- selectedEntry.keyIndices.Sort((x, y) =>
- {
- return y.CompareTo(x);
- });
- foreach (var keyframeIdx in selectedEntry.keyIndices)
- curve.RemoveKeyframe(keyframeIdx);
- curve.Apply();
- }
- }
- else
- ShowReadOnlyMessage();
- ClearSelection();
- OnCurveModified?.Invoke();
- RefreshCurveDrawing();
- UpdateEventsGUI();
- }
- /// <summary>
- /// Deletes all currently selected events.
- /// </summary>
- private void DeleteSelectedEvents()
- {
- List<EventInfo> newEvents = new List<EventInfo>();
- foreach (var entry in events)
- {
- if(!entry.selected)
- newEvents.Add(entry);
- }
- events = newEvents;
- OnEventDeleted?.Invoke();
- ClearSelection();
- UpdateEventsGUI();
- }
- /// <summary>
- /// Unselects any selected keyframes and events.
- /// </summary>
- private void ClearSelection()
- {
- guiCurveDrawing.ClearSelectedKeyframes();
- selectedKeyframes.Clear();
- foreach (var entry in events)
- entry.selected = false;
- }
- /// <summary>
- /// Adds the provided keyframe to the selection list (doesn't clear existing ones).
- /// </summary>
- /// <param name="keyframeRef">Keyframe to select.</param>
- private void SelectKeyframe(KeyframeRef keyframeRef)
- {
- TangentMode tangentMode = TangentMode.Auto;
- if (keyframeRef.curveIdx >= 0 && keyframeRef.curveIdx < curveInfos.Length)
- {
- EdAnimationCurve curve = curveInfos[keyframeRef.curveIdx].curve;
- if (keyframeRef.keyIdx >= 0 && keyframeRef.keyIdx < curve.TangentModes.Length)
- tangentMode = curve.TangentModes[keyframeRef.keyIdx];
- }
- guiCurveDrawing.SelectKeyframe(keyframeRef, tangentMode, true);
- if (!IsSelected(keyframeRef))
- {
- int curveIdx = selectedKeyframes.FindIndex(x =>
- {
- return x.curveIdx == keyframeRef.curveIdx;
- });
- if (curveIdx == -1)
- {
- curveIdx = selectedKeyframes.Count;
- SelectedKeyframes newKeyframes = new SelectedKeyframes();
- newKeyframes.curveIdx = keyframeRef.curveIdx;
- selectedKeyframes.Add(newKeyframes);
- }
- selectedKeyframes[curveIdx].keyIndices.Add(keyframeRef.keyIdx);
- }
- }
- /// <summary>
- /// Checks is the provided keyframe currently selected.
- /// </summary>
- /// <param name="keyframeRef">Keyframe to check.</param>
- /// <returns>True if selected, false otherwise.</returns>
- private bool IsSelected(KeyframeRef keyframeRef)
- {
- int curveIdx = selectedKeyframes.FindIndex(x =>
- {
- return x.curveIdx == keyframeRef.curveIdx;
- });
- if (curveIdx == -1)
- return false;
- int keyIdx = selectedKeyframes[curveIdx].keyIndices.FindIndex(x =>
- {
- return x == keyframeRef.keyIdx;
- });
- return keyIdx != -1;
- }
- /// <summary>
- /// Opens the edit window for the currently selected keyframe.
- /// </summary>
- private void EditSelectedKeyframe()
- {
- if (disableCurveEdit)
- {
- ShowReadOnlyMessage();
- return;
- }
- if (selectedKeyframes.Count == 0)
- return;
- EdAnimationCurve curve = curveInfos[selectedKeyframes[0].curveIdx].curve;
- KeyFrame[] keyFrames = curve.KeyFrames;
- int keyIndex = selectedKeyframes[0].keyIndices[0];
- KeyFrame keyFrame = keyFrames[keyIndex];
- Vector2I position = guiCurveDrawing.CurveToPixelSpace(new Vector2(keyFrame.time, keyFrame.value));
- Rect2I drawingBounds = drawingPanel.Bounds;
- position.x = MathEx.Clamp(position.x, 0, drawingBounds.width);
- position.y = MathEx.Clamp(position.y, 0, drawingBounds.height);
- KeyframeEditWindow editWindow = DropDownWindow.Open<KeyframeEditWindow>(drawingPanel, position);
- editWindow.Initialize(keyFrame, x =>
- {
- curve.UpdateKeyframe(keyIndex, x.time, x.value);
- curve.Apply();
- RefreshCurveDrawing();
- },
- x =>
- {
- if (x)
- OnCurveModified?.Invoke();
- });
- }
- /// <summary>
- /// Opens the edit window for the currently selected event.
- /// </summary>
- private void EditSelectedEvent()
- {
- if (!showEvents)
- return;
- for (int i = 0; i < events.Count; i++)
- {
- if (events[i].selected)
- {
- StartEventEdit(i);
- break;
- }
- }
- }
- /// <summary>
- /// Opens the event edit window for the specified event.
- /// </summary>
- /// <param name="eventIdx">Event index to open the edit window for.</param>
- private void StartEventEdit(int eventIdx)
- {
- if (!showEvents || eventsSO == null)
- return;
- AnimationEvent animEvent = events[eventIdx].animEvent;
- Vector2I position = new Vector2I();
- position.x = guiEvents.GetOffset(animEvent.time);
- position.y = EVENTS_HEIGHT/2;
- Component[] components = eventsSO.GetComponents();
- string[] componentNames = new string[components.Length];
- for (int i = 0; i < components.Length; i++)
- componentNames[i] = components[i].GetType().Name;
- EventEditWindow editWindow = DropDownWindow.Open<EventEditWindow>(eventsPanel, position);
- editWindow.Initialize(animEvent, componentNames, () =>
- {
- UpdateEventsGUI();
- },
- x =>
- {
- if(x)
- OnEventModified?.Invoke();
- });
- }
- /// <summary>
- /// Shows a dialog box that notifies the user that the animation clip is read only.
- /// </summary>
- private void ShowReadOnlyMessage()
- {
- LocEdString title = new LocEdString("Warning");
- LocEdString message =
- new LocEdString("You cannot edit keyframes on animation clips that" +
- " are imported from an external file.");
- DialogBox.Open(title, message, DialogBox.Type.OK);
- }
- #region Input
- /// <summary>
- /// Handles input. Should be called by the owning window whenever a pointer is pressed.
- /// </summary>
- /// <param name="ev">Object containing pointer press event information.</param>
- internal void OnPointerPressed(PointerEvent ev)
- {
- if (ev.IsUsed)
- return;
- Rect2I elementBounds = mainPanel.ScreenBounds;
- Vector2I pointerPos = ev.ScreenPos - new Vector2I(elementBounds.x, elementBounds.y);
- bool isOverEditor = pointerPos.x >= 0 && pointerPos.x < width && pointerPos.y >= 0 && pointerPos.y < height;
- if (!isOverEditor)
- return;
- else
- OnClicked?.Invoke();
- Rect2I drawingBounds = drawingPanel.Bounds;
- Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
- Rect2I eventBounds = new Rect2I();
- if(showEvents)
- eventBounds = eventsPanel.Bounds;
- Vector2I eventPos = pointerPos - new Vector2I(eventBounds.x, eventBounds.y);
- if (ev.Button == PointerButton.Left)
- {
- Vector2 curveCoord;
- if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out curveCoord, true))
- {
- KeyframeRef keyframeRef;
- if (!guiCurveDrawing.FindKeyFrame(drawingPos, out keyframeRef))
- {
- TangentRef tangentRef;
- if (guiCurveDrawing.FindTangent(drawingPos, out tangentRef))
- {
- isMousePressedOverTangent = true;
- dragStart = drawingPos;
- draggedTangent = tangentRef;
- }
- else
- ClearSelection();
- }
- else
- {
- if (!IsSelected(keyframeRef))
- {
- if (!Input.IsButtonHeld(ButtonCode.LeftShift) && !Input.IsButtonHeld(ButtonCode.RightShift))
- ClearSelection();
- SelectKeyframe(keyframeRef);
- }
- isMousePressedOverKey = true;
- dragStart = drawingPos;
- }
- UpdateEventsGUI();
- }
- else
- {
- int frameIdx = guiTimeline.GetFrame(pointerPos);
- if (frameIdx != -1)
- SetMarkedFrame(frameIdx);
- else
- {
- int eventIdx;
- if (showEvents && guiEvents.FindEvent(eventPos, out eventIdx))
- {
- if (!Input.IsButtonHeld(ButtonCode.LeftShift) && !Input.IsButtonHeld(ButtonCode.RightShift))
- ClearSelection();
- events[eventIdx].selected = true;
- UpdateEventsGUI();
- }
- else
- {
- ClearSelection();
- UpdateEventsGUI();
- }
- }
- OnFrameSelected?.Invoke(frameIdx);
- }
- isPointerHeld = true;
- }
- else if (ev.Button == PointerButton.Right)
- {
- Vector2 curveCoord;
- if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out curveCoord, true))
- {
- contextClickPosition = drawingPos;
- KeyframeRef keyframeRef;
- if (!guiCurveDrawing.FindKeyFrame(drawingPos, out keyframeRef))
- {
- ClearSelection();
- blankContextMenu.Open(pointerPos, mainPanel);
- UpdateEventsGUI();
- }
- else
- {
- // If clicked outside of current selection, just select the one keyframe
- if (!IsSelected(keyframeRef))
- {
- ClearSelection();
- SelectKeyframe(keyframeRef);
- UpdateEventsGUI();
- }
- keyframeContextMenu.Open(pointerPos, mainPanel);
- }
- }
- else if (showEvents && guiEvents.GetFrame(eventPos) != -1) // Clicked over events bar
- {
- contextClickPosition = eventPos;
- int eventIdx;
- if (!guiEvents.FindEvent(eventPos, out eventIdx))
- {
- ClearSelection();
- blankEventContextMenu.Open(pointerPos, mainPanel);
- UpdateEventsGUI();
- }
- else
- {
- // If clicked outside of current selection, just select the one event
- if (!events[eventIdx].selected)
- {
- ClearSelection();
- events[eventIdx].selected = true;
- UpdateEventsGUI();
- }
- eventContextMenu.Open(pointerPos, mainPanel);
- }
- }
- }
- else if (ev.button == PointerButton.Middle)
- {
- Vector2 curvePos;
- if (WindowToCurveSpace(pointerPos, out curvePos))
- {
- dragStartPos = pointerPos;
- isMiddlePointerHeld = true;
- }
- }
- }
- /// <summary>
- /// Handles input. Should be called by the owning window whenever a pointer is double-clicked.
- /// </summary>
- /// <param name="ev">Object containing pointer press event information.</param>
- internal void OnPointerDoubleClicked(PointerEvent ev)
- {
- if (ev.IsUsed)
- return;
- Rect2I elementBounds = mainPanel.ScreenBounds;
- Vector2I pointerPos = ev.ScreenPos - new Vector2I(elementBounds.x, elementBounds.y);
- bool isOverEditor = pointerPos.x >= 0 && pointerPos.x < width && pointerPos.y >= 0 && pointerPos.y < height;
- if (!isOverEditor)
- return;
- Rect2I drawingBounds = drawingPanel.Bounds;
- Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
- if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out var curveCoord, true))
- {
- int curveIdx = (int)guiCurveDrawing.FindCurve(drawingPos);
- if (curveIdx == -1)
- return;
- AddKeyframe(curveIdx, curveCoord.x);
- }
- }
- /// <summary>
- /// Handles input. Should be called by the owning window whenever a pointer is moved.
- /// </summary>
- /// <param name="ev">Object containing pointer move event information.</param>
- internal void OnPointerMoved(PointerEvent ev)
- {
- if (ev.Button != PointerButton.Left)
- return;
- if (isPointerHeld)
- {
- Rect2I elementBounds = mainPanel.ScreenBounds;
- Vector2I pointerPos = ev.ScreenPos - new Vector2I(elementBounds.x, elementBounds.y);
- if (isMousePressedOverKey || isMousePressedOverTangent)
- {
- Rect2I drawingBounds = drawingPanel.Bounds;
- Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
- if (!isDragInProgress)
- {
- int distance = Vector2I.Distance(drawingPos, dragStart);
- if (distance >= DRAG_START_DISTANCE)
- {
- if (isMousePressedOverKey && !disableCurveEdit)
- {
- draggedKeyframes.Clear();
- foreach (var selectedEntry in selectedKeyframes)
- {
- EdAnimationCurve curve = curveInfos[selectedEntry.curveIdx].curve;
- KeyFrame[] keyFrames = curve.KeyFrames;
- DraggedKeyframes newEntry = new DraggedKeyframes();
- newEntry.curveIdx = selectedEntry.curveIdx;
- draggedKeyframes.Add(newEntry);
- foreach (var keyframeIdx in selectedEntry.keyIndices)
- newEntry.keys.Add(new DraggedKeyframe(keyframeIdx, keyFrames[keyframeIdx]));
- }
- }
- isDragInProgress = true;
- }
- }
- if (isDragInProgress)
- {
- if (isMousePressedOverKey && !disableCurveEdit)
- {
- Vector2 diff = Vector2.Zero;
- Vector2 dragStartCurve;
- if (guiCurveDrawing.PixelToCurveSpace(dragStart, out dragStartCurve, true))
- {
- Vector2 currentPosCurve;
- if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out currentPosCurve, true))
- diff = currentPosCurve - dragStartCurve;
- }
- foreach (var draggedEntry in draggedKeyframes)
- {
- EdAnimationCurve curve = curveInfos[draggedEntry.curveIdx].curve;
- for (int i = 0; i < draggedEntry.keys.Count; i++)
- {
- DraggedKeyframe draggedKey = draggedEntry.keys[i];
- float newTime = Math.Max(0.0f, draggedKey.original.time + diff.x);
- float newValue = draggedKey.original.value + diff.y;
- int newIndex = curve.UpdateKeyframe(draggedKey.index, newTime, newValue);
- // It's possible key changed position due to time change, but since we're moving all
- // keys at once they cannot change position relative to one another, otherwise we would
- // need to update indices for other keys as well.
- draggedKey.index = newIndex;
- draggedEntry.keys[i] = draggedKey;
- }
- curve.Apply();
- }
- // Rebuild selected keys from dragged keys (after potential sorting)
- ClearSelection();
- foreach (var draggedEntry in draggedKeyframes)
- {
- foreach (var keyframe in draggedEntry.keys)
- SelectKeyframe(new KeyframeRef(draggedEntry.curveIdx, keyframe.index));
- }
- isModifiedDuringDrag = true;
- RefreshCurveDrawing();
- UpdateEventsGUI();
- }
- else if (isMousePressedOverTangent && !disableCurveEdit)
- {
- EdAnimationCurve curve = curveInfos[draggedTangent.keyframeRef.curveIdx].curve;
- KeyFrame keyframe = curve.KeyFrames[draggedTangent.keyframeRef.keyIdx];
- Vector2 keyframeCurveCoords = new Vector2(keyframe.time, keyframe.value);
- Vector2 currentPosCurve;
- if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out currentPosCurve, true))
- {
- Vector2 normal = currentPosCurve - keyframeCurveCoords;
- normal = normal.Normalized;
- float tangent = EdAnimationCurve.NormalToTangent(normal);
- if (draggedTangent.type == TangentType.In)
- {
- if (normal.x > 0.0f)
- tangent = float.PositiveInfinity;
- else
- tangent = -tangent;
- keyframe.inTangent = tangent;
- if(curve.TangentModes[draggedTangent.keyframeRef.keyIdx] == TangentMode.Free)
- keyframe.outTangent = tangent;
- }
- else
- {
- if (normal.x < 0.0f)
- tangent = float.PositiveInfinity;
- keyframe.outTangent = tangent;
- if (curve.TangentModes[draggedTangent.keyframeRef.keyIdx] == TangentMode.Free)
- keyframe.inTangent = tangent;
- }
- curve.KeyFrames[draggedTangent.keyframeRef.keyIdx] = keyframe;
- curve.Apply();
- isModifiedDuringDrag = true;
- RefreshCurveDrawing();
- }
- }
- }
- }
- else // Move frame marker
- {
- int frameIdx = guiTimeline.GetFrame(pointerPos);
- if (frameIdx != -1)
- SetMarkedFrame(frameIdx);
- OnFrameSelected?.Invoke(frameIdx);
- }
- }
- if (isMiddlePointerHeld)
- {
- Rect2I elementBounds = mainPanel.ScreenBounds;
- Vector2I pointerPos = ev.ScreenPos - new Vector2I(elementBounds.x, elementBounds.y);
- int distance = Vector2I.Distance(dragStartPos, pointerPos);
- if (distance >= DRAG_START_DISTANCE)
- {
- isZoomDragInProgress = true;
- Cursor.Hide();
- Rect2I clipRect;
- clipRect.x = ev.ScreenPos.x - 2;
- clipRect.y = ev.ScreenPos.y - 2;
- clipRect.width = 4;
- clipRect.height = 4;
- Cursor.ClipToRect(clipRect);
- }
- }
- }
- /// <summary>
- /// Handles input. Should be called by the owning window whenever a pointer is released.
- /// </summary>
- /// <param name="ev">Object containing pointer release event information.</param>
- internal void OnPointerReleased(PointerEvent ev)
- {
- if (isZoomDragInProgress)
- {
- Cursor.Show();
- Cursor.ClipDisable();
- }
- if (isModifiedDuringDrag)
- OnCurveModified?.Invoke();
- isMiddlePointerHeld = false;
- isZoomDragInProgress = false;
- isPointerHeld = false;
- isDragInProgress = false;
- isMousePressedOverKey = false;
- isMousePressedOverTangent = false;
- isModifiedDuringDrag = false;
- }
- /// <summary>
- /// Handles input. Should be called by the owning window whenever a button is released.
- /// </summary>
- /// <param name="ev">Object containing button release event information.</param>
- internal void OnButtonUp(ButtonEvent ev)
- {
- if(ev.Button == ButtonCode.Delete)
- DeleteSelectedKeyframes();
- }
- #endregion
- #region Scroll, drag, zoom
- private Vector2I dragStartPos;
- private bool isMiddlePointerHeld;
- private bool isZoomDragInProgress;
- private float zoomAmount;
- /// <summary>
- /// Handles mouse scroll wheel and dragging events in order to zoom or drag the displayed curve editor contents.
- /// Should be called every frame.
- /// </summary>
- internal void HandleDragAndZoomInput()
- {
- // Handle middle mouse dragging
- if (isZoomDragInProgress)
- {
- float lengthPerPixel = Range.x / Width;
- float heightPerPixel = Range.y / Height;
- float dragX = Input.GetAxisValue(InputAxis.MouseX) * DRAG_SCALE * lengthPerPixel;
- float dragY = Input.GetAxisValue(InputAxis.MouseY) * DRAG_SCALE * heightPerPixel;
- Vector2 offset = Offset;
- offset.x = Math.Max(0.0f, offset.x + dragX);
- offset.y -= dragY;
- Offset = offset;
- UpdateScrollBarSize();
- UpdateScrollBarPosition();
- }
- // Handle zoom in/out
- float scroll = Input.GetAxisValue(InputAxis.MouseZ);
- if (scroll != 0.0f)
- {
- Rect2I elementBounds = mainPanel.ScreenBounds;
- Vector2I pointerPos = Input.PointerPosition - new Vector2I(elementBounds.x, elementBounds.y);
- Vector2 curvePos;
- if (WindowToCurveSpace(pointerPos, out curvePos))
- {
- float zoom = scroll * ZOOM_SCALE;
- Zoom(curvePos, zoom);
- }
- }
- }
- /// <summary>
- /// Moves or resizes the vertical scroll bar under the curve editor.
- /// </summary>
- /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
- /// <param name="size">New size of the scrollbar handle, in range [0, 1].</param>
- private void SetVertScrollbarProperties(float position, float size)
- {
- Vector2 visibleRange = Range;
- Vector2 totalRange = GetTotalRange();
- visibleRange.y = totalRange.y*size;
- Range = visibleRange;
- float scrollableRange = totalRange.y - visibleRange.y;
- Vector2 offset = Offset;
- offset.y = -scrollableRange * (position * 2.0f - 1.0f);
- Offset = offset;
- }
- /// <summary>
- /// Moves or resizes the horizontal scroll bar under the curve editor.
- /// </summary>
- /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
- /// <param name="size">New size of the scrollbar handle, in range [0, 1].</param>
- private void SetHorzScrollbarProperties(float position, float size)
- {
- Vector2 visibleRange = Range;
- Vector2 totalRange = GetTotalRange();
- visibleRange.x = totalRange.x * size;
- Range = visibleRange;
- float scrollableRange = totalRange.x - visibleRange.x;
- Vector2 offset = Offset;
- offset.x = scrollableRange * position;
- Offset = offset;
- }
- /// <summary>
- /// Updates the size of both scrollbars depending on the currently visible curve area vs. the total curve area.
- /// </summary>
- private void UpdateScrollBarSize()
- {
- Vector2 visibleRange = Range;
- Vector2 totalRange = GetTotalRange();
- horzScrollBar.HandleSize = visibleRange.x / totalRange.x;
- vertScrollBar.HandleSize = visibleRange.y / totalRange.y;
- }
- /// <summary>
- /// Updates the position of both scrollbars depending on the offset currently applied to the visible curve area.
- /// </summary>
- private void UpdateScrollBarPosition()
- {
- Vector2 visibleRange = Range;
- Vector2 totalRange = GetTotalRange();
- Vector2 scrollableRange = totalRange - visibleRange;
- Vector2 offset = Offset;
- if (scrollableRange.x > 0.0f)
- horzScrollBar.Position = offset.x / scrollableRange.x;
- else
- horzScrollBar.Position = 0.0f;
- if (scrollableRange.y > 0.0f)
- {
- float pos = offset.y/scrollableRange.y;
- float sign = MathEx.Sign(pos);
- pos = sign*MathEx.Clamp01(MathEx.Abs(pos));
- pos = (1.0f - pos) /2.0f;
- vertScrollBar.Position = pos;
- }
- else
- vertScrollBar.Position = 0.0f;
- }
- /// <summary>
- /// Calculates the width/height of the curve area depending on the current zoom level.
- /// </summary>
- /// <returns>Width/height of the curve area, in curve space (value, time).</returns>
- private Vector2 GetZoomedRange()
- {
- float zoomLevel = MathEx.Pow(2, zoomAmount);
- GetOptimalRangeAndOffset(out _, out var optimalRange);
- return optimalRange / zoomLevel;
- }
- /// <summary>
- /// Returns the total width/height of the contents of the curve area.
- /// </summary>
- /// <returns>Width/height of the curve area, in curve space (value, time).</returns>
- private Vector2 GetTotalRange()
- {
- // Return optimal range (that covers the visible curve)
- GetOptimalRangeAndOffset(out _, out var range);
- // Increase range in case user zoomed out
- Vector2 zoomedRange = GetZoomedRange();
- return Vector2.Max(range, zoomedRange);
- }
- /// <summary>
- /// Zooms in or out at the provided position in the curve display.
- /// </summary>
- /// <param name="curvePos">Position to zoom towards, relative to the curve display area, in curve space
- /// (value, time)</param>
- /// <param name="amount">Amount to zoom in (positive), or out (negative).</param>
- private void Zoom(Vector2 curvePos, float amount)
- {
- // Increase or decrease the visible range depending on zoom level
- Vector2 oldZoomedRange = GetZoomedRange();
- zoomAmount = MathEx.Clamp(zoomAmount + amount, -10.0f, 10.0f);
- Vector2 zoomedRange = GetZoomedRange();
- Vector2 zoomedDiff = zoomedRange - oldZoomedRange;
- Vector2 currentRange = Range;
- Vector2 newRange = currentRange + zoomedDiff;
- newRange.x = Math.Max(0, newRange.x);
- Range = newRange;
- // When zooming, make sure to focus on the point provided, so adjust the offset
- Vector2 rangeScale = newRange;
- rangeScale.x /= currentRange.x;
- rangeScale.y /= currentRange.y;
- UpdateScrollBarSize();
- UpdateScrollBarPosition();
- }
- /// <summary>
- /// Updates the offset and range of the curve display to fully fit the currently selected set of curves.
- /// </summary>
- /// <param name="resetTime">If true the time offset/range will be recalculated, otherwise current time offset will
- /// be kept as is.</param>
- internal void CenterAndResize(bool resetTime)
- {
- GetOptimalRangeAndOffset(out var offset, out var range);
- if (!resetTime)
- {
- offset.x = Offset.x;
- range.x = Range.x;
- }
- Range = range;
- Offset = offset;
- UpdateScrollBarPosition();
- UpdateScrollBarSize();
- }
- /// <summary>
- /// Triggered when the user moves or resizes the horizontal scrollbar.
- /// </summary>
- /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
- /// <param name="size">New size of the scrollbar, in range [0, 1].</param>
- private void OnHorzScrollOrResize(float position, float size)
- {
- SetHorzScrollbarProperties(position, size);
- }
- /// <summary>
- /// Triggered when the user moves or resizes the vertical scrollbar.
- /// </summary>
- /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
- /// <param name="size">New size of the scrollbar, in range [0, 1].</param>
- private void OnVertScrollOrResize(float position, float size)
- {
- SetVertScrollbarProperties(position, size);
- }
- #endregion
- #region Helpers
- /// <summary>
- /// Returns width/height required to show the entire contents of the currently displayed curves.
- /// </summary>
- /// <param name="offset">Offset used for centering the curves.</param>
- /// <param name="range">Range representing the width/height in curve space (time, value). </param>
- private void GetOptimalRangeAndOffset(out Vector2 offset, out Vector2 range)
- {
- AnimationCurve[] curves = new AnimationCurve[curveInfos.Length];
- for(int i = 0; i < curveInfos.Length; i++)
- curves[i] = curveInfos[i].curve.Normal;
- float xMin, xMax;
- float yMin, yMax;
- AnimationUtility.CalculateRange(curves, out xMin, out xMax, out yMin, out yMax);
- float xRange = xMax - xMin;
- float yRange = (yMax - yMin) * 0.5f;
- float yOffset = yMin + yRange;
- // Add padding to y range
- yRange *= 1.05f;
- // Don't allow zero range
- if (xRange == 0.0f)
- xRange = 60.0f;
- if (yRange == 0.0f)
- yRange = 10.0f;
- offset = new Vector2(xMin, yOffset);
- range = new Vector2(xRange, yRange);
- }
- /// <summary>
- /// Returns a set of current curve draw infos with non-editor curves.
- /// </summary>
- /// <returns>Curve draw infos.</returns>
- private CurveDrawInfo[] GetPlainCurveDrawInfos()
- {
- CurveDrawInfo[] output = new CurveDrawInfo[curveInfos.Length];
- for(int i = 0; i < curveInfos.Length; i++)
- output[i] = new CurveDrawInfo(curveInfos[i].curve.Normal, curveInfos[i].color);
- return output;
- }
- /// <summary>
- /// Converts coordinate in curve space (time, value) into pixel coordinates relative to the curve drawing area
- /// origin.
- /// </summary>
- /// <param name="curveCoords">Time and value of the location to convert.</param>
- /// <returns>Coordinates relative to curve drawing area's origin, in pixels.</returns>
- public Vector2I CurveToPixelSpace(Vector2 curveCoords)
- {
- return guiCurveDrawing.CurveToPixelSpace(curveCoords);
- }
- /// <summary>
- /// Converts coordinates in window space (relative to the parent window origin) into coordinates in curve space.
- /// </summary>
- /// <param name="pointerPos">Coordinates relative to this GUI element, in pixels.</param>
- /// <param name="curveCoord">Curve coordinates within the range as specified by <see cref="Range"/>. Only
- /// valid when function returns true.</param>
- /// <returns>True if the coordinates are within the curve area, false otherwise.</returns>
- public bool WindowToCurveSpace(Vector2I pointerPos, out Vector2 curveCoord)
- {
- Rect2I drawingBounds = drawingPanel.Bounds;
- Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
- return guiCurveDrawing.PixelToCurveSpace(drawingPos, out curveCoord);
- }
- #endregion
- }
-
- /// <summary>
- /// Information necessary to drawing an editor curve.
- /// </summary>
- public struct EdCurveDrawInfo
- {
- public EdCurveDrawInfo(EdAnimationCurve curve, Color color)
- {
- this.curve = curve;
- this.color = color;
- }
- public EdAnimationCurve curve;
- public Color color;
- }
- /// <summary>
- /// Drop down window that displays input boxes used for editing a keyframe.
- /// </summary>
- [DefaultSize(120, 80)]
- internal class KeyframeEditWindow : DropDownWindow
- {
- private Action<bool> closeCallback;
- private bool changesMade;
- /// <summary>
- /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
- /// use.
- /// </summary>
- /// <param name="keyFrame">Keyframe whose properties to edit.</param>
- /// <param name="updateCallback">Callback triggered when event values change.</param>
- /// <param name="closeCallback">Callback triggered just before the window closes.</param>
- internal void Initialize(KeyFrame keyFrame, Action<KeyFrame> updateCallback, Action<bool> closeCallback)
- {
- GUIFloatField timeField = new GUIFloatField(new LocEdString("Time"), 40, "");
- timeField.Value = keyFrame.time;
- timeField.OnChanged += x => { keyFrame.time = x; changesMade = true; updateCallback(keyFrame); };
- GUIFloatField valueField = new GUIFloatField(new LocEdString("Value"), 40, "");
- valueField.Value = keyFrame.value;
- valueField.OnChanged += x => { keyFrame.value = x; changesMade = true; updateCallback(keyFrame); };
- GUILayoutY vertLayout = GUI.AddLayoutY();
- vertLayout.AddFlexibleSpace();
- GUILayoutX horzLayout = vertLayout.AddLayoutX();
- horzLayout.AddFlexibleSpace();
- GUILayout contentLayout = horzLayout.AddLayoutY();
- GUILayout timeLayout = contentLayout.AddLayoutX();
- timeLayout.AddSpace(5);
- timeLayout.AddElement(timeField);
- timeLayout.AddFlexibleSpace();
- GUILayout componentLayout = contentLayout.AddLayoutX();
- componentLayout.AddSpace(5);
- componentLayout.AddElement(valueField);
- componentLayout.AddFlexibleSpace();
- horzLayout.AddFlexibleSpace();
- vertLayout.AddFlexibleSpace();
- this.closeCallback = closeCallback;
- }
- private void OnDestroy()
- {
- closeCallback?.Invoke(changesMade);
- }
- }
- /// <summary>
- /// Drop down window that displays input boxes used for editing an event.
- /// </summary>
- [DefaultSize(200, 80)]
- internal class EventEditWindow : DropDownWindow
- {
- private Action<bool> closeCallback;
- private bool changesMade;
- /// <summary>
- /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
- /// use.
- /// </summary>
- /// <param name="animEvent">Event whose properties to edit.</param>
- /// <param name="componentNames">List of component names that the user can select from.</param>
- /// <param name="updateCallback">Callback triggered when event values change.</param>
- /// <param name="closeCallback">Callback triggered just before the window closes.</param>
- internal void Initialize(AnimationEvent animEvent, string[] componentNames, Action updateCallback,
- Action<bool> closeCallback)
- {
- int selectedIndex = -1;
- string methodName = "";
- if (!string.IsNullOrEmpty(animEvent.name))
- {
- string[] nameEntries = animEvent.name.Split('/');
- if (nameEntries.Length > 1)
- {
- string typeName = nameEntries[0];
- for (int i = 0; i < componentNames.Length; i++)
- {
- if (componentNames[i] == typeName)
- {
- selectedIndex = i;
- break;
- }
- }
- methodName = nameEntries[nameEntries.Length - 1];
- }
- }
- GUIFloatField timeField = new GUIFloatField(new LocEdString("Time"), 40, "");
- timeField.Value = animEvent.time;
- timeField.OnChanged += x => { animEvent.time = x; changesMade = true; updateCallback(); };
- GUIListBoxField componentField = new GUIListBoxField(componentNames, new LocEdString("Component"), 40);
- if (selectedIndex != -1)
- componentField.Index = selectedIndex;
- componentField.OnSelectionChanged += x =>
- {
- string compName = "";
- if (x != -1)
- compName = componentNames[x] + "/";
- animEvent.name = compName + x;
- changesMade = true;
- updateCallback();
- };
- GUITextField methodField = new GUITextField(new LocEdString("Method"), 40, false, "", GUIOption.FixedWidth(190));
- methodField.Value = methodName;
- methodField.OnChanged += x =>
- {
- string compName = "";
- if(componentField.Index != -1)
- compName = componentNames[componentField.Index] + "/";
- animEvent.name = compName + x;
- changesMade = true;
- updateCallback();
- };
- GUILayoutY vertLayout = GUI.AddLayoutY();
- vertLayout.AddFlexibleSpace();
- GUILayoutX horzLayout = vertLayout.AddLayoutX();
- horzLayout.AddFlexibleSpace();
- GUILayout contentLayout = horzLayout.AddLayoutY();
- GUILayout timeLayout = contentLayout.AddLayoutX();
- timeLayout.AddSpace(5);
- timeLayout.AddElement(timeField);
- timeLayout.AddFlexibleSpace();
- GUILayout componentLayout = contentLayout.AddLayoutX();
- componentLayout.AddSpace(5);
- componentLayout.AddElement(componentField);
- componentLayout.AddFlexibleSpace();
- GUILayout methodLayout = contentLayout.AddLayoutX();
- methodLayout.AddSpace(5);
- methodLayout.AddElement(methodField);
- methodLayout.AddFlexibleSpace();
- horzLayout.AddFlexibleSpace();
- vertLayout.AddFlexibleSpace();
- this.closeCallback = closeCallback;
- }
- private void OnDestroy()
- {
- closeCallback?.Invoke(changesMade);
- }
- }
- /** @} */
- }
|