| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850 |
- //********************************** 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 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 EditorWindow window;
- 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 GUICurveDrawing 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 CurveDrawInfo[] curveInfos = new CurveDrawInfo[0];
- private bool disableCurveEdit = false;
- private bool drawCurveRange = 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="window">Parent window of the GUI element.</param>
- /// <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>
- public GUICurveEditor(EditorWindow window, GUILayout gui, int width, int height, bool showEvents)
- {
- this.window = window;
- 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 GUICurveDrawing(drawingPanel, this.width, this.height - TIMELINE_HEIGHT -
- eventsHeaderHeight - VERT_PADDING * 2, curveInfos);
- guiCurveDrawing.SetRange(60.0f, 20.0f);
- 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(CurveDrawInfo[] curveInfos)
- {
- this.curveInfos = curveInfos;
- guiCurveDrawing.SetCurves(curveInfos);
- Redraw();
- }
- /// <summary>
- /// Changes curve rendering mode. Normally the curves are drawn individually, but when range rendering is enabled
- /// the area between the first two curves is drawn instead. This setting is ignored if less than two curves are
- /// present. More than two curves are also ignored.
- /// </summary>
- /// <param name="drawRange">True to enable range rendering mode, false to enable individual curve rendering.</param>
- public void SetDrawRange(bool drawRange)
- {
- drawCurveRange = drawRange;
- guiCurveDrawing.SetDrawRange(drawRange);
- 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.SetSize(this.width, 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.SetFPS(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.SetMarkedFrame(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();
- guiCurveDrawing.Rebuild();
- 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();
- guiCurveDrawing.Rebuild();
- 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()
- {
- guiCurveDrawing.Rebuild();
- guiTimeline.Rebuild();
- guiSidebar.Rebuild();
- if(showEvents)
- guiEvents.Rebuild();
- }
- /// <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);
- 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);
- }
- }
- curve.Apply();
- }
- OnCurveModified?.Invoke();
- guiCurveDrawing.Rebuild();
- }
- /// <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();
- guiCurveDrawing.Rebuild();
- 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();
- guiCurveDrawing.Rebuild();
- 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();
- guiCurveDrawing.Rebuild();
- 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();
- guiCurveDrawing.Rebuild();
- 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();
- guiCurveDrawing.Rebuild();
- 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)
- {
- guiCurveDrawing.SelectKeyframe(keyframeRef, 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 = GUIUtility.CalculateBounds(drawingPanel, window.GUI);
- position.x = MathEx.Clamp(position.x, 0, drawingBounds.width);
- position.y = MathEx.Clamp(position.y, 0, drawingBounds.height);
- Vector2I windowPos = position + new Vector2I(drawingBounds.x, drawingBounds.y);
-
- KeyframeEditWindow editWindow = DropDownWindow.Open<KeyframeEditWindow>(window, windowPos);
- editWindow.Initialize(keyFrame, x =>
- {
- curve.UpdateKeyframe(keyIndex, x.time, x.value);
- curve.Apply();
- guiCurveDrawing.Rebuild();
- },
- 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)
- {
- AnimationEvent animEvent = events[eventIdx].animEvent;
- Vector2I position = new Vector2I();
- position.x = guiEvents.GetOffset(animEvent.time);
- position.y = EVENTS_HEIGHT/2;
- Rect2I eventBounds = GUIUtility.CalculateBounds(eventsPanel, window.GUI);
- Vector2I windowPos = position + new Vector2I(eventBounds.x, eventBounds.y);
- if (eventsSO == null)
- return;
- 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>(window, windowPos);
- 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;
- Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
- Rect2I elementBounds = GUIUtility.CalculateBounds(mainPanel, window.GUI);
- Vector2I pointerPos = windowPos - 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 = 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;
- }
- guiCurveDrawing.Rebuild();
- 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();
- guiCurveDrawing.Rebuild();
- 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);
- guiCurveDrawing.Rebuild();
- UpdateEventsGUI();
- }
- else
- {
- // If clicked outside of current selection, just select the one keyframe
- if (!IsSelected(keyframeRef))
- {
- ClearSelection();
- SelectKeyframe(keyframeRef);
- guiCurveDrawing.Rebuild();
- 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);
- guiCurveDrawing.Rebuild();
- UpdateEventsGUI();
- }
- else
- {
- // If clicked outside of current selection, just select the one event
- if (!events[eventIdx].selected)
- {
- ClearSelection();
- events[eventIdx].selected = true;
- guiCurveDrawing.Rebuild();
- UpdateEventsGUI();
- }
- eventContextMenu.Open(pointerPos, mainPanel);
- }
- }
- }
- else if (ev.button == PointerButton.Middle)
- {
- Vector2 curvePos;
- if (WindowToCurveSpace(windowPos, out curvePos))
- {
- dragStartPos = windowPos;
- 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;
- Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
- Rect2I elementBounds = GUIUtility.CalculateBounds(mainPanel, window.GUI);
- Vector2I pointerPos = windowPos - 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 = 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)
- {
- Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
- Rect2I elementBounds = GUIUtility.CalculateBounds(mainPanel, window.GUI);
- Vector2I pointerPos = windowPos - 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;
- guiCurveDrawing.Rebuild();
- 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;
- 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;
- guiCurveDrawing.Rebuild();
- }
- }
- }
- }
- else // Move frame marker
- {
- int frameIdx = guiTimeline.GetFrame(pointerPos);
- if (frameIdx != -1)
- SetMarkedFrame(frameIdx);
- OnFrameSelected?.Invoke(frameIdx);
- }
- }
- if (isMiddlePointerHeld)
- {
- Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
- int distance = Vector2I.Distance(dragStartPos, windowPos);
- 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)
- {
- Vector2I windowPos = window.ScreenToWindowPos(Input.PointerPosition);
- Vector2 curvePos;
- if (WindowToCurveSpace(windowPos, 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);
- Vector2 optimalRange = GetOptimalRange();
- 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)
- Vector2 optimalRange = GetOptimalRange();
- // Increase range in case user zoomed out
- Vector2 zoomedRange = GetZoomedRange();
- return Vector2.Max(optimalRange, 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;
- 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;
- Vector2 relativeCurvePos = curvePos - Offset;
- Vector2 newCurvePos = relativeCurvePos * rangeScale;
- Vector2 diff = newCurvePos - relativeCurvePos;
- Offset -= diff;
- 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)
- {
- Vector2 offset, range;
- GetOptimalRangeAndOffset(out offset, out 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>
- /// <returns>Width/height of the curve area, in curve space (time, value).</returns>
- private Vector2 GetOptimalRange()
- {
- float xMin, xMax;
- float yMin, yMax;
- CalculateRange(curveInfos, out xMin, out xMax, out yMin, out yMax);
- float xRange = xMax;
- float yRange = Math.Max(Math.Abs(yMin), Math.Abs(yMax));
- // 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;
- return new Vector2(xRange, yRange);
- }
- /// <summary>
- /// Returns the offset and range required for fully displaying the currently selected set of 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)
- {
- float xMin, xMax;
- float yMin, yMax;
- CalculateRange(curveInfos, 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>
- /// Calculates the total range covered by a set of curves.
- /// </summary>
- /// <param name="curveInfos">Curves to calculate range for.</param>
- /// <param name="xMin">Minimum time value present in the curves.</param>
- /// <param name="xMax">Maximum time value present in the curves.</param>
- /// <param name="yMin">Minimum curve value present in the curves.</param>
- /// <param name="yMax">Maximum curve value present in the curves.</param>
- private static void CalculateRange(CurveDrawInfo[] curveInfos, out float xMin, out float xMax, out float yMin,
- out float yMax)
- {
- // Note: This only evaluates at keyframes, we should also evaluate in-between in order to account for steep
- // tangents
- xMin = float.PositiveInfinity;
- xMax = float.NegativeInfinity;
- yMin = float.PositiveInfinity;
- yMax = float.NegativeInfinity;
- foreach (var curveInfo in curveInfos)
- {
- KeyFrame[] keyframes = curveInfo.curve.KeyFrames;
- foreach (var key in keyframes)
- {
- xMin = Math.Min(xMin, key.time);
- xMax = Math.Max(xMax, key.time);
- yMin = Math.Min(yMin, key.value);
- yMax = Math.Max(yMax, key.value);
- }
- }
- if (xMin == float.PositiveInfinity)
- xMin = 0.0f;
- if (xMax == float.NegativeInfinity)
- xMax = 0.0f;
- if (yMin == float.PositiveInfinity)
- yMin = 0.0f;
- if (yMax == float.NegativeInfinity)
- yMax = 0.0f;
- }
- /// <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="windowPos">Coordinates relative to parent editor window, 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 windowPos, out Vector2 curveCoord)
- {
- Rect2I elementBounds = GUIUtility.CalculateBounds(mainPanel, window.GUI);
- Vector2I pointerPos = windowPos - new Vector2I(elementBounds.x, elementBounds.y);
- Rect2I drawingBounds = drawingPanel.Bounds;
- Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
- return guiCurveDrawing.PixelToCurveSpace(drawingPos, out curveCoord);
- }
- #endregion
- }
- /// <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);
- }
- }
- /** @} */
- }
|