Quellcode durchsuchen

Added events to animation editor (WIP)

BearishSun vor 9 Jahren
Ursprung
Commit
2463571e4d

+ 1 - 0
Source/MBansheeEditor/MBansheeEditor.csproj

@@ -52,6 +52,7 @@
     <Compile Include="Windows\AboutBox.cs" />
     <Compile Include="Windows\AnimationWindow.cs" />
     <Compile Include="Windows\Animation\FieldSelectionWindow.cs" />
+    <Compile Include="Windows\Animation\GUIAnimEvents.cs" />
     <Compile Include="Windows\Animation\GUIAnimFieldDisplay.cs" />
     <Compile Include="Windows\Animation\GUICurveDrawing.cs" />
     <Compile Include="Windows\Animation\GUICurveEditor.cs" />

+ 167 - 0
Source/MBansheeEditor/Windows/Animation/GUIAnimEvents.cs

@@ -0,0 +1,167 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+using System;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /** @addtogroup AnimationEditor
+     *  @{
+     */
+
+    public class GUIAnimEvents
+    {
+        private const int EVENT_HALF_WIDTH = 2;
+
+        private float rangeLength = 60.0f;
+        private float rangeOffset = 0.0f;
+        private int fps = 1;
+
+        private GUICanvas canvas;
+        private int width;
+        private int height;
+        private int drawableWidth;
+
+        private AnimationEvent[] events = new AnimationEvent[0];
+        private bool[] selectedEvents = new bool[0];
+
+        public GUIAnimEvents(GUILayout layout, int width, int height)
+        {
+            canvas = new GUICanvas();
+            layout.AddElement(canvas);
+
+            SetSize(width, height);
+        }
+
+        public bool FindEvent(Vector2I windowCoords, out int eventIdx)
+        {
+            Rect2I bounds = canvas.Bounds;
+
+            if (windowCoords.x < (bounds.x + GUIGraphTime.PADDING) || windowCoords.x >= (bounds.x + bounds.width - GUIGraphTime.PADDING) ||
+                windowCoords.y < bounds.y || windowCoords.y >= (bounds.y + bounds.height))
+            {
+                eventIdx = -1;
+                return false;
+            }
+
+            Vector2I relativeCoords = windowCoords - new Vector2I(bounds.x + GUIGraphTime.PADDING, bounds.y);
+            for (int i = 0; i < events.Length; i++)
+            {
+                AnimationEvent evnt = events[i];
+
+                int xPos = (int)(((evnt.Time - rangeOffset) / GetRange()) * drawableWidth) + GUIGraphTime.PADDING;
+
+                if (relativeCoords.x >= (xPos - EVENT_HALF_WIDTH) || relativeCoords.y >= (xPos + EVENT_HALF_WIDTH))
+                {
+                    eventIdx = i;
+                    return true;
+                }
+            }
+
+            eventIdx = -1;
+            return false;
+        }
+
+        public void SetSize(int width, int height)
+        {
+            this.width = width;
+            this.height = height;
+
+            canvas.SetWidth(width);
+            canvas.SetHeight(height);
+
+            drawableWidth = Math.Max(0, width - GUIGraphTime.PADDING * 2);
+        }
+
+        public void SetRange(float length)
+        {
+            rangeLength = Math.Max(0.0f, length);
+        }
+
+        public void SetOffset(float offset)
+        {
+            rangeOffset = offset;
+        }
+
+        public void SetFPS(int fps)
+        {
+            this.fps = Math.Max(1, fps);
+        }
+
+        public void SetEvents(AnimationEvent[] events, bool[] selected)
+        {
+            int numEvents;
+            if (events != null)
+                numEvents = events.Length;
+            else
+                numEvents = 0;
+
+            this.events = new AnimationEvent[numEvents];
+            if(events != null)
+                Array.Copy(events, this.events, numEvents);
+
+            selectedEvents = new bool[numEvents];
+            if(selected != null)
+                Array.Copy(selected, selectedEvents, MathEx.Min(numEvents, selected.Length));
+        }
+
+        private void DrawEventMarker(float t, bool selected)
+        {
+            int xPos = (int)(((t - rangeOffset) / GetRange()) * drawableWidth) + GUIGraphTime.PADDING;
+
+            Vector2I a = new Vector2I(xPos - EVENT_HALF_WIDTH, height - 1);
+            Vector2I b = new Vector2I(xPos, 0);
+            Vector2I c = new Vector2I(xPos + EVENT_HALF_WIDTH, height - 1);
+            Vector2I d = new Vector2I(xPos, 0);
+
+            // Draw square shape
+            Vector2I[] linePoints = { a, b, c, d, a };
+            Vector2I[] trianglePoints = { b, c, a, d };
+
+            Color outerColor = selected ? Color.BansheeOrange : Color.Black;
+            canvas.DrawTriangleStrip(trianglePoints, Color.White, 101);
+            canvas.DrawPolyLine(linePoints, outerColor, 100);
+        }
+
+        private float GetRange(bool padding = false)
+        {
+            float spf = 1.0f / fps;
+
+            float range = rangeLength;
+            if (padding)
+            {
+                float lengthPerPixel = rangeLength / drawableWidth;
+                range += lengthPerPixel * GUIGraphTime.PADDING;
+            }
+
+            return ((int)range / spf) * spf;
+        }
+
+        /// <summary>
+        /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
+        /// </summary>
+        public void Rebuild()
+        {
+            canvas.Clear();
+
+            float range = GetRange();
+
+            float lengthPerPixel = rangeLength / drawableWidth;
+            float eventHalfWidth = lengthPerPixel * EVENT_HALF_WIDTH;
+            for (int i = 0; i < events.Length; i++)
+            {
+                float t = events[i].Time;
+
+                float min = t - eventHalfWidth;
+                float max = t + eventHalfWidth;
+
+                if (max < rangeOffset || min > (rangeOffset + range))
+                    continue;
+
+                DrawEventMarker(t, selectedEvents[i]);
+            }
+        }
+    }
+
+    /** @} */
+}

+ 27 - 8
Source/MBansheeEditor/Windows/Animation/GUICurveEditor.cs

@@ -37,14 +37,16 @@ namespace BansheeEditor
         }
 
         private const int TIMELINE_HEIGHT = 20;
+        private const int EVENTS_HEIGHT = 10;
         private const int SIDEBAR_WIDTH = 30;
         private const int DRAG_START_DISTANCE = 3;
 
         private EditorWindow window;
         private GUILayout gui;
         private GUIPanel drawingPanel;
-        private GUIPanel sidebarPanel;
+
         private GUIGraphTime guiTimeline;
+        private GUIAnimEvents guiEvents;
         private GUICurveDrawing guiCurveDrawing;
         private GUIGraphValues guiSidebar;
 
@@ -90,6 +92,7 @@ namespace BansheeEditor
                 yRange = value.y;
 
                 guiTimeline.SetRange(xRange);
+                guiEvents.SetRange(xRange);
                 guiCurveDrawing.SetRange(xRange, yRange * 2.0f);
                 guiSidebar.SetRange(offset.y - yRange, offset.y + yRange);
 
@@ -108,6 +111,7 @@ namespace BansheeEditor
                 offset = value;
 
                 guiTimeline.SetOffset(offset.x);
+                guiEvents.SetOffset(offset.x);
                 guiCurveDrawing.SetOffset(offset);
                 guiSidebar.SetRange(offset.y - yRange, offset.y + yRange);
 
@@ -157,16 +161,20 @@ namespace BansheeEditor
 
             guiTimeline = new GUIGraphTime(gui, width, TIMELINE_HEIGHT);
 
+            GUIPanel eventsPanel = gui.AddPanel();
+            eventsPanel.SetPosition(0, TIMELINE_HEIGHT);
+            guiEvents = new GUIAnimEvents(eventsPanel, width, EVENTS_HEIGHT);
+            
             drawingPanel = gui.AddPanel();
-            drawingPanel.SetPosition(0, TIMELINE_HEIGHT);
+            drawingPanel.SetPosition(0, TIMELINE_HEIGHT + EVENTS_HEIGHT);
 
-            guiCurveDrawing = new GUICurveDrawing(drawingPanel, width, height - TIMELINE_HEIGHT, curves);
+            guiCurveDrawing = new GUICurveDrawing(drawingPanel, width, height - TIMELINE_HEIGHT - EVENTS_HEIGHT, curves);
             guiCurveDrawing.SetRange(60.0f, 20.0f);
 
-            sidebarPanel = gui.AddPanel(-10);
-            sidebarPanel.SetPosition(0, TIMELINE_HEIGHT);
+            GUIPanel sidebarPanel = gui.AddPanel(-10);
+            sidebarPanel.SetPosition(0, TIMELINE_HEIGHT + EVENTS_HEIGHT);
 
-            guiSidebar = new GUIGraphValues(sidebarPanel, SIDEBAR_WIDTH, height - TIMELINE_HEIGHT);
+            guiSidebar = new GUIGraphValues(sidebarPanel, SIDEBAR_WIDTH, height - TIMELINE_HEIGHT - EVENTS_HEIGHT);
             guiSidebar.SetRange(-10.0f, 10.0f);
         }
 
@@ -257,6 +265,14 @@ namespace BansheeEditor
 
                     if (frameIdx != -1)
                         SetMarkedFrame(frameIdx);
+                    else
+                    {
+                        int eventIdx;
+                        if (guiEvents.FindEvent(pointerPos, out eventIdx))
+                        {
+                            // TODO - Select event
+                        }
+                    }
 
                     OnFrameSelected?.Invoke(frameIdx);
                 }
@@ -473,8 +489,9 @@ namespace BansheeEditor
             this.height = height;
 
             guiTimeline.SetSize(width, TIMELINE_HEIGHT);
-            guiCurveDrawing.SetSize(width, height - TIMELINE_HEIGHT);
-            guiSidebar.SetSize(SIDEBAR_WIDTH, height - TIMELINE_HEIGHT);
+            guiEvents.SetSize(height, EVENTS_HEIGHT);
+            guiCurveDrawing.SetSize(width, height - TIMELINE_HEIGHT - EVENTS_HEIGHT);
+            guiSidebar.SetSize(SIDEBAR_WIDTH, height - TIMELINE_HEIGHT - EVENTS_HEIGHT);
 
             Redraw();
         }
@@ -486,6 +503,7 @@ namespace BansheeEditor
         public void SetFPS(int fps)
         {
             guiTimeline.SetFPS(fps);
+            guiEvents.SetFPS(fps);
             guiCurveDrawing.SetFPS(fps);
 
             Redraw();
@@ -537,6 +555,7 @@ namespace BansheeEditor
         {
             guiCurveDrawing.Rebuild();
             guiTimeline.Rebuild();
+            guiEvents.Rebuild();
             guiSidebar.Rebuild();
         }
         

+ 0 - 2
Source/MBansheeEditor/Windows/AnimationWindow.cs

@@ -394,8 +394,6 @@ namespace BansheeEditor
 
             horzScrollBar.HandleSize = visibleRange.x / totalRange.x;
             vertScrollBar.HandleSize = visibleRange.y / totalRange.y;
-
-            Debug.Log(visibleRange.y + " - " + totalRange.y + " - " + (visibleRange.y / totalRange.y));
         }
 
         private void UpdateScrollBarPosition()