Explorar o código

WIP GUI widgets for main and custom cameras in managed code

BearishSun %!s(int64=10) %!d(string=hai) anos
pai
achega
a689342efc

+ 93 - 0
MBansheeEditor/Inspectors/GUIWidgetInspector.cs

@@ -0,0 +1,93 @@
+using System.Collections;
+using System.Collections.Generic;
+using BansheeEngine;
+
+namespace BansheeEditor
+{
+    /// <summary>
+    /// Renders an inspector for the <see cref="GUIWidget"/> component.
+    /// </summary>
+    [CustomInspector(typeof(GUIWidget))]
+    internal class GUIWidgetInspector : Inspector
+    {
+        private GUIResourceField skinField;
+        private GUIGameObjectField cameraField;
+
+        private InspectableState modifyState;
+
+        /// <inheritdoc/>
+        protected internal override void Initialize()
+        {
+            BuildGUI();
+        }
+
+        /// <inheritdoc/>
+        protected internal override InspectableState Refresh()
+        {
+            GUIWidget guiWidget = InspectedObject as GUIWidget;
+            if (guiWidget == null)
+                return InspectableState.NotModified;
+
+            skinField.Value = guiWidget.Skin;
+            cameraField.Value = guiWidget.Camera;
+            
+            InspectableState oldState = modifyState;
+            if (modifyState.HasFlag(InspectableState.Modified))
+                modifyState = InspectableState.NotModified;
+
+            return oldState;
+        }
+
+        /// <summary>
+        /// Recreates all the GUI elements used by this inspector.
+        /// </summary>
+        private void BuildGUI()
+        {
+            Layout.Clear();
+
+            GUIWidget guiWidget = InspectedObject as GUIWidget;
+            if (guiWidget == null)
+                return;
+
+            skinField = new GUIResourceField(typeof(GUISkin), new LocEdString("Skin"));
+            cameraField = new GUIGameObjectField(typeof (Camera), new LocEdString("Camera"));
+
+            skinField.OnChanged += x =>
+            {
+                GUISkin skin = Resources.Load<GUISkin>(x);
+                guiWidget.Skin = skin;
+
+                MarkAsModified();
+                ConfirmModify();
+            };
+
+            cameraField.OnChanged += x =>
+            {
+                guiWidget.Camera = x as Camera;
+
+                MarkAsModified();
+                ConfirmModify();
+            };
+
+            Layout.AddElement(skinField);
+            Layout.AddElement(cameraField);
+        }
+
+        /// <summary>
+        /// Marks the contents of the inspector as modified.
+        /// </summary>
+        protected void MarkAsModified()
+        {
+            modifyState |= InspectableState.ModifyInProgress;
+        }
+
+        /// <summary>
+        /// Confirms any queued modifications.
+        /// </summary>
+        protected void ConfirmModify()
+        {
+            if (modifyState.HasFlag(InspectableState.ModifyInProgress))
+                modifyState |= InspectableState.Modified;
+        }
+    }
+}

+ 1 - 0
MBansheeEditor/MBansheeEditor.csproj

@@ -46,6 +46,7 @@
     <Compile Include="BuildWindow.cs" />
     <Compile Include="BuildWindow.cs" />
     <Compile Include="CodeEditor.cs" />
     <Compile Include="CodeEditor.cs" />
     <Compile Include="ColorPicker.cs" />
     <Compile Include="ColorPicker.cs" />
+    <Compile Include="Inspectors\GUIWidgetInspector.cs" />
     <Compile Include="LogWindow.cs" />
     <Compile Include="LogWindow.cs" />
     <Compile Include="DefaultSize.cs" />
     <Compile Include="DefaultSize.cs" />
     <Compile Include="DialogBox.cs" />
     <Compile Include="DialogBox.cs" />

+ 46 - 0
MBansheeEngine/GUI/GUI.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace BansheeEngine
+{
+    /// <summary>
+    /// Provides access to a global <see cref="GUIPanel"/> that renders GUI on the main viewport. Use 
+    /// <see cref="GUIWidget"/> if you need more control over the placement of GUI, or require it to be rendered to a 
+    /// different viewport.
+    /// </summary>
+    public static class GUI
+    {
+        /// <summary>
+        /// Skin used for rendering all the GUI elements.
+        /// </summary>
+        public static GUISkin Skin
+        {
+            get { return Internal_GetSkin(); }
+            set
+            {
+                IntPtr skinPtr = IntPtr.Zero;
+                if (value != null)
+                    skinPtr = value.GetCachedPtr();
+
+                Internal_SetSkin(skinPtr);
+            }
+        }
+
+        /// <summary>
+        /// Container into which all GUI elements should be placed.
+        /// </summary>
+        public static GUIPanel Panel
+        {
+            get { return Internal_GetPanel(); }
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern GUISkin Internal_GetSkin();
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetSkin(IntPtr skin);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern GUIPanel Internal_GetPanel();
+    }
+}

+ 76 - 0
MBansheeEngine/GUI/GUIWidget.cs

@@ -0,0 +1,76 @@
+namespace BansheeEngine
+{
+    /// <summary>
+    /// Container of GUI elements that can be positioned in the scene, and can output the rendered GUI to a user defined
+    /// camera.
+    /// </summary>
+    public class GUIWidget : Component
+    {
+        private NativeGUIWidget nativeGUIWidget;
+
+        [SerializeField]
+        private SerializableData serializableData = new SerializableData();
+
+        /// <summary>
+        /// Skin used for rendering all the GUI elements belonging to this widget.
+        /// </summary>
+        internal GUISkin Skin
+        {
+            get { return nativeGUIWidget.Skin; }
+            set
+            {
+                serializableData.skin = value;
+                nativeGUIWidget.Skin = value;
+            }
+        }
+
+        /// <summary>
+        /// Determines to which camera are the GUI elements belonong to this widget rendered. If null then they will be
+        /// rendered on the main camera.
+        /// </summary>
+        internal Camera Camera
+        {
+            get { return nativeGUIWidget.Camera; }
+            set
+            {
+                serializableData.camera = value;
+                nativeGUIWidget.Camera = value;
+            }
+        }
+
+        /// <summary>
+        /// Container into which all GUI elements belonging to this widget should be placed.
+        /// </summary>
+        internal GUIPanel Panel
+        {
+            get { return nativeGUIWidget.Panel; }
+        }
+
+        private void OnReset()
+        {
+            if (nativeGUIWidget != null)
+                nativeGUIWidget.Destroy();
+
+            nativeGUIWidget = new NativeGUIWidget();
+
+            // Restore saved values after reset
+            nativeGUIWidget.Skin = serializableData.skin;
+            nativeGUIWidget.Camera = serializableData.camera;
+        }
+
+        private void OnDestroy()
+        {
+            nativeGUIWidget.Destroy();
+        }
+
+        /// <summary>
+        /// Holds all data the GUIWidget component needs to persist through serialization.
+        /// </summary>
+        [SerializeObject]
+        private class SerializableData
+        {
+            public GUISkin skin;
+            public Camera camera;
+        }
+    }
+}

+ 1 - 7
MBansheeEngine/Light.cs

@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Runtime.CompilerServices;
-using System.Text;
-
-namespace BansheeEngine
+namespace BansheeEngine
 {
 {
     /// <summary>
     /// <summary>
     /// Component that illuminates a portion of the scene covered by the light.
     /// Component that illuminates a portion of the scene covered by the light.

+ 3 - 0
MBansheeEngine/MBansheeEngine.csproj

@@ -47,7 +47,9 @@
     <Compile Include="Bounds.cs" />
     <Compile Include="Bounds.cs" />
     <Compile Include="Builtin.cs" />
     <Compile Include="Builtin.cs" />
     <Compile Include="Camera.cs" />
     <Compile Include="Camera.cs" />
+    <Compile Include="GUI\GUI.cs" />
     <Compile Include="GUI\GUIListView.cs" />
     <Compile Include="GUI\GUIListView.cs" />
+    <Compile Include="GUI\GUIWidget.cs" />
     <Compile Include="Layers.cs" />
     <Compile Include="Layers.cs" />
     <Compile Include="NativeCamera.cs" />
     <Compile Include="NativeCamera.cs" />
     <Compile Include="ContextMenu.cs" />
     <Compile Include="ContextMenu.cs" />
@@ -87,6 +89,7 @@
     <Compile Include="Input.cs" />
     <Compile Include="Input.cs" />
     <Compile Include="InputConfiguration.cs" />
     <Compile Include="InputConfiguration.cs" />
     <Compile Include="Light.cs" />
     <Compile Include="Light.cs" />
+    <Compile Include="NativeGUIWidget.cs" />
     <Compile Include="NativeLight.cs" />
     <Compile Include="NativeLight.cs" />
     <Compile Include="LocString.cs" />
     <Compile Include="LocString.cs" />
     <Compile Include="ManagedResource.cs" />
     <Compile Include="ManagedResource.cs" />

+ 79 - 0
MBansheeEngine/NativeGUIWidget.cs

@@ -0,0 +1,79 @@
+using System;
+using System.Runtime.CompilerServices;
+
+namespace BansheeEngine
+{
+    /// <summary>
+    /// Wrapper around the native GUIWidget class.
+    /// <see cref="GUIWidget"/>
+    /// </summary>
+    internal class NativeGUIWidget : ScriptObject
+    {
+        /// <summary>
+        /// Skin used for rendering all the GUI elements belonging to this widget.
+        /// </summary>
+        internal GUISkin Skin
+        {
+            get { return Internal_GetSkin(mCachedPtr); }
+            set
+            {
+                IntPtr skinPtr = IntPtr.Zero;
+                if (value != null)
+                    skinPtr = value.GetCachedPtr();
+
+                Internal_SetSkin(mCachedPtr, skinPtr);
+            }
+        }
+
+        /// <summary>
+        /// Determines to which camera are the GUI elements belonong to this widget rendered. If null then they will be
+        /// rendered on the main camera.
+        /// </summary>
+        internal Camera Camera
+        {
+            get { return Internal_GetCamera(mCachedPtr); }
+            set
+            {
+                IntPtr cameraPtr = IntPtr.Zero;
+                if (value != null)
+                    cameraPtr = value.GetCachedPtr();
+
+                Internal_SetCamera(mCachedPtr, cameraPtr);
+            }
+        }
+
+        /// <summary>
+        /// Container into which all GUI elements belonging to this widget should be placed.
+        /// </summary>
+        internal GUIPanel Panel
+        {
+            get { return Internal_GetPanel(mCachedPtr); }
+        }
+
+        /// <summary>
+        /// Deletes the GUI widget and all child GUI elements.
+        /// </summary>
+        internal void Destroy()
+        {
+            Internal_Destroy(mCachedPtr);
+        }
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern GUISkin Internal_GetSkin(IntPtr instance);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetSkin(IntPtr instance, IntPtr skin);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern Camera Internal_GetCamera(IntPtr instance);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_SetCamera(IntPtr instance, IntPtr camera);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern GUIPanel Internal_GetPanel(IntPtr instance);
+
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern void Internal_Destroy(IntPtr instance);
+    }
+}

+ 0 - 3
MBansheeEngine/NativeLight.cs

@@ -1,8 +1,5 @@
 using System;
 using System;
-using System.Collections.Generic;
-using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Runtime.CompilerServices;
-using System.Text;
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {