Browse Source

Working on C# events

Josh Engebretson 10 years ago
parent
commit
3998342e88

+ 1 - 0
Build/AtomicSharp/AtomicSharp.csproj

@@ -60,6 +60,7 @@
     <Compile Include="AtomicInterop.cs" />
     <Compile Include="AtomicInterop.cs" />
     <Compile Include="NativeCore.cs" />
     <Compile Include="NativeCore.cs" />
     <Compile Include="EventCore.cs" />
     <Compile Include="EventCore.cs" />
+    <Compile Include="ScriptObject.cs" />
   </ItemGroup>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 </Project>
 </Project>

+ 21 - 4
Build/AtomicSharp/AtomicSharpTest/Program.cs

@@ -3,16 +3,28 @@ using System;
 using AtomicEngine;
 using AtomicEngine;
 using AtomicPlayer;
 using AtomicPlayer;
 
 
+class MyObject : ScriptObject
+{
+	
+}
+
 class Spinner : CSComponent
 class Spinner : CSComponent
 {
 {
 	public float Speed = 1.0f;
 	public float Speed = 1.0f;
 
 
 	override public void Start()
 	override public void Start()
 	{
 	{
-		//var renderer = Atomic.GetSubsystem<Renderer> ();
-		//SubscribeToEvent (renderer, "BeginViewUpdate", handleEvent);
+		myObject = new MyObject ();
+
+		SubscribeToEvent (myObject, "MyEvent", handleMyObjectEvent);
 
 
-		SubscribeToEvent ("BeginViewUpdate", handleEvent);
+		var renderer = Atomic.GetSubsystem<Renderer> ();
+		SubscribeToEvent (renderer, "BeginViewUpdate", handleEvent);
+	}
+
+	void handleMyObjectEvent(VariantMap eventData)
+	{
+		Console.WriteLine ("Got My Event");		
 	}
 	}
 
 
 	void handleEvent(VariantMap eventData)
 	void handleEvent(VariantMap eventData)
@@ -20,6 +32,8 @@ class Spinner : CSComponent
 		View view = eventData.Get<View> ("view");
 		View view = eventData.Get<View> ("view");
 		view.Camera.Zoom = zoom;
 		view.Camera.Zoom = zoom;
 		zoom += .01f;
 		zoom += .01f;
+
+		myObject.SendEvent ("MyEvent");
 	}
 	}
 
 
 	override public void Update(float timeStep)
 	override public void Update(float timeStep)
@@ -29,6 +43,8 @@ class Spinner : CSComponent
 
 
 	float zoom = 1.0f;
 	float zoom = 1.0f;
 
 
+	MyObject myObject;
+
 }
 }
 	
 	
 class MyGame
 class MyGame
@@ -51,11 +67,12 @@ class MyGame
 
 
 		var name = zone.Node.Name;
 		var name = zone.Node.Name;
 
 
+		/*
 		var chestNode = scene.GetChild ("Chest", true);
 		var chestNode = scene.GetChild ("Chest", true);
 		var c = chestNode.AddComponent <Spinner> ();
 		var c = chestNode.AddComponent <Spinner> ();
 		c.Speed = 10.0f;
 		c.Speed = 10.0f;
-
 		c.Destroy ();
 		c.Destroy ();
+		*/
 
 
 		zone.SetAmbientColor( new Color(1, 0, 0) );
 		zone.SetAmbientColor( new Color(1, 0, 0) );
 
 

+ 1 - 6
Build/AtomicSharp/CSComponent.cs

@@ -40,12 +40,7 @@ namespace AtomicEngine
 		{
 		{
 			EventCore.SubscribeToEvent (this, null, eventType, function);
 			EventCore.SubscribeToEvent (this, null, eventType, function);
 		}
 		}
-
-		void handleEvent(string eventType, Dictionary<uint, object> eventData)
-		{
-
-		}
-			
+						
 		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
 		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
 		private static extern IntPtr csb_Atomic_CSComponent_Constructor();
 		private static extern IntPtr csb_Atomic_CSComponent_Constructor();
 
 

+ 11 - 1
Build/AtomicSharp/EventCore.cs

@@ -67,7 +67,13 @@ namespace AtomicEngine
 		static Dictionary<uint, List<uint> > eventSubscribers = new Dictionary<uint, List<uint>>();
 		static Dictionary<uint, List<uint> > eventSubscribers = new Dictionary<uint, List<uint>>();
 
 
 		static Dictionary<uint, Dictionary<uint, Subscription> > subscriptions = new Dictionary<uint, Dictionary<uint, Subscription> >();
 		static Dictionary<uint, Dictionary<uint, Subscription> > subscriptions = new Dictionary<uint, Dictionary<uint, Subscription> >();
-		
+
+
+		public static void SendEvent(AObject sender, string eventType)
+		{
+			csb_Atomic_AObject_SendEvent (sender.nativeInstance, eventType);	
+		}
+
 		public static void SubscribeToEvent(AObject subscriber, AObject sender, string eventType, AtomicEventDelegate function)
 		public static void SubscribeToEvent(AObject subscriber, AObject sender, string eventType, AtomicEventDelegate function)
 		{
 		{
 			var eventTypeID = Atomic.StringToStringHash (eventType);			
 			var eventTypeID = Atomic.StringToStringHash (eventType);			
@@ -146,6 +152,10 @@ namespace AtomicEngine
 			vmap.Invalidate ();
 			vmap.Invalidate ();
 
 
 		}
 		}
+
+		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
+		private static extern void csb_Atomic_AObject_SendEvent(IntPtr self, string eventType);
+
 	}
 	}
 }
 }
 
 

+ 33 - 0
Build/AtomicSharp/ScriptObject.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+
+namespace AtomicEngine
+{
+	public class ScriptObject : AObject
+	{
+		public ScriptObject ()
+		{
+			nativeInstance = NativeCore.RegisterNative (csb_Atomic_CSScriptObject_Constructor(), this);	
+		}
+
+		public void SendEvent(string eventType, Dictionary<string, object> eventData = null)
+		{
+			EventCore.SendEvent (this, eventType);
+		}
+
+		public void SubscribeToEvent(AObject sender, string eventType, AtomicEventDelegate function)
+		{
+			EventCore.SubscribeToEvent (this, sender, eventType, function);
+		}
+
+		public void SubscribeToEvent(string eventType, AtomicEventDelegate function)
+		{
+			EventCore.SubscribeToEvent (this, null, eventType, function);
+		}
+
+		[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
+		private static extern IntPtr csb_Atomic_CSScriptObject_Constructor();
+	}
+}
+

+ 12 - 0
Source/AtomicSharp/AtomicSharpAPI.cpp

@@ -13,6 +13,7 @@
 
 
 #include "AtomicSharp.h"
 #include "AtomicSharp.h"
 #include "AtomicSharpAPI.h"
 #include "AtomicSharpAPI.h"
+#include "CSScriptObject.h"
 
 
 #ifdef ATOMIC_PLATFORM_WINDOWS
 #ifdef ATOMIC_PLATFORM_WINDOWS
 #pragma warning(disable: 4244) // possible loss of data
 #pragma warning(disable: 4244) // possible loss of data
@@ -75,6 +76,17 @@ ATOMIC_EXPORT_API RefCounted* csb_Atomic_CSComponent_Constructor()
    return new CSComponent(AtomicSharp::GetContext());
    return new CSComponent(AtomicSharp::GetContext());
 }
 }
 
 
+ATOMIC_EXPORT_API RefCounted* csb_Atomic_CSScriptObject_Constructor()
+{
+   return new CSScriptObject(AtomicSharp::GetContext());
+}
+
+ATOMIC_EXPORT_API void csb_Atomic_AObject_SendEvent(Object* object, const char* eventType)
+{
+   object->SendEvent(eventType);
+}
+
+
 ATOMIC_EXPORT_API ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
 ATOMIC_EXPORT_API ClassID csb_Atomic_RefCounted_GetClassID(RefCounted* refCounted)
 {
 {
     if (!refCounted)
     if (!refCounted)

+ 38 - 0
Source/AtomicSharp/CSScriptObject.cpp

@@ -0,0 +1,38 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "CSScriptObject.h"
+
+namespace Atomic
+{
+
+CSScriptObject::CSScriptObject(Context* context) : Object(context)
+{
+
+}
+
+CSScriptObject::~CSScriptObject()
+{
+
+}
+
+}

+ 45 - 0
Source/AtomicSharp/CSScriptObject.h

@@ -0,0 +1,45 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include <Atomic/Core/Object.h>
+
+namespace Atomic
+{
+
+/// ScriptObject makes it possible for script classes to inherit "directly" from
+/// Object (which is abstract) and use the event system, etc
+class CSScriptObject : public Object
+{
+
+    OBJECT(CSScriptObject);
+
+public:
+    /// Construct.
+    CSScriptObject(Context* context);
+    /// Destruct.
+    virtual ~CSScriptObject();
+
+};
+
+}