Sfoglia il codice sorgente

Handling Default Parameters, including structs which can't be const and thus can't be assigned in the parameter block

Josh Engebretson 10 anni fa
parent
commit
f8f4a3af2c

+ 2 - 0
Build/AtomicSharp/AtomicEngine.cs

@@ -105,6 +105,8 @@ namespace AtomicEngine
 	public static partial class Constants
 	{
 		public const string LIBNAME = "/Users/josh/Dev/atomic/AtomicGameEngineSharp-build/Source/AtomicSharp/AtomicSharp";
+
+		public const uint M_MAX_UNSIGNED = 0xffffffff;
 	}
 		
 	public partial class RefCounted

+ 11 - 2
Build/AtomicSharp/AtomicSharpTest/Program.cs

@@ -20,11 +20,20 @@ class Spinner : CSComponent
 
 		var renderer = Atomic.GetSubsystem<Renderer> ();
 		SubscribeToEvent (renderer, "BeginViewUpdate", handleEvent);
+
+		SubscribeToEvent (this, "SelfEvent", handleSelfEvent);
+
+		SendEvent ("SelfEvent");
 	}
 
+	void handleSelfEvent(VariantMap eventData)
+	{
+		Console.WriteLine ("Got Self Event");		
+	}
+		
 	void handleMyObjectEvent(VariantMap eventData)
 	{
-		Console.WriteLine ("Got My Event");		
+		//Console.WriteLine ("Got My Event");		
 	}
 
 	void handleEvent(VariantMap eventData)
@@ -38,7 +47,7 @@ class Spinner : CSComponent
 
 	override public void Update(float timeStep)
 	{
-		Node.Yaw (timeStep * 75 * Speed, TransformSpace.TS_LOCAL);
+		Node.Yaw (timeStep * 75 * Speed);
 	}
 
 	float zoom = 1.0f;

+ 1 - 1
Build/AtomicSharp/CSComponent.cs

@@ -28,7 +28,7 @@ namespace AtomicEngine
 			
 		public void SendEvent(string eventType, Dictionary<string, object> eventData = null)
 		{
-
+			EventCore.SendEvent (this, eventType);
 		}
 
 		public void SubscribeToEvent(AObject sender, string eventType, AtomicEventDelegate function)

+ 19 - 0
Build/AtomicSharp/Math.cs

@@ -21,6 +21,25 @@ namespace AtomicEngine
 		public float x;
 		public float y;
 		public float z;
+
+
+		/// Zero vector.
+		static public readonly Vector3 Zero = new Vector3(0, 0, 0);
+		/// (-1,0,0) vector.
+		static public readonly Vector3 Left = new Vector3(-1, 0, 0);
+		/// (1,0,0) vector.
+		static public readonly Vector3 Right = new Vector3(1, 0, 0);
+		/// (0,1,0) vector.
+		static public readonly Vector3 Up = new Vector3(0, 1, 0);
+		/// (0,-1,0) vector.
+		static public readonly Vector3 Down = new Vector3(0, -1, 0);
+		/// (0,0,1) vector.
+		static public readonly Vector3 Forward = new Vector3(0, 0, 1);
+		/// (0,0,-1) vector.
+		static public readonly Vector3 Back = new Vector3(0, 0, -1);
+		/// (1,1,1) vector.
+		static public readonly Vector3 One = new Vector3(1, 1, 1);
+
 	}
 
 	[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]

+ 1 - 1
Source/Atomic/Graphics/Drawable.h

@@ -106,7 +106,7 @@ class ATOMIC_API Drawable : public Component
 
 public:
     /// Construct.
-    Drawable(Context* context, unsigned char drawableFlags);
+    Drawable(Context* context, unsigned char drawableFlags = 0);
     /// Destruct.
     virtual ~Drawable();
     /// Register object attributes. Drawable must be registered first.

+ 2 - 2
Source/ToolCore/JSBind/CSharp/CSClassWriter.cpp

@@ -94,8 +94,8 @@ void CSClassWriter::WriteManagedProperties(String& sourceOut)
 
                 if (!fType)
                     fType = setType;
-                //else if (fType->type_ != setType->type_)
-                //    continue;
+                else if (fType->type_->ToString() != setType->type_->ToString())
+                    continue;
             }
 
             if (!fType)

+ 129 - 3
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.cpp

@@ -337,6 +337,23 @@ void CSFunctionWriter::GenerateNativeSource(String& sourceOut)
 
 // MANAGED----------------------------------------------------------------------------------------
 
+void CSFunctionWriter::WriteDefaultStructParameters(String& source)
+{
+
+    for (unsigned i = 0; i < defaultStructParameters_.Size(); i++)
+    {
+        const DefaultStructParameter& dparm = defaultStructParameters_[i];
+
+        String line = ToString("if (default(%s).Equals(%s)) %s = %s;\n",
+                               dparm.type.CString(), dparm.parameterName.CString(), dparm.parameterName.CString(),
+                               dparm.assignment.CString());
+
+        source += IndentLine(line);
+
+    }
+
+}
+
 void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
 {
     source += "\n";
@@ -426,6 +443,7 @@ void CSFunctionWriter::WriteManagedPInvokeFunctionSignature(String& source)
 }
 
 
+
 void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
 {
     // generate args
@@ -450,9 +468,15 @@ void CSFunctionWriter::GenManagedFunctionParameters(String& sig)
 
             sig += CSTypeHelper::GetManagedTypeString(ptype);
 
-            // hack for Drawable as this causes a compilation error (need to add default params)
-            if (sig.EndsWith("drawableFlags"))
-                sig += " = '\\0'";
+            String init = ptype->initializer_;
+
+            if (init.Length())
+            {
+                init = MapDefaultParameter(ptype);
+                if (init.Length())
+                    sig += " = " + init;
+
+            }
 
             if (i + 1 != parameters.Size())
                 sig += ", ";
@@ -488,6 +512,8 @@ void CSFunctionWriter::WriteManagedConstructor(String& source)
 
     Indent();
 
+    WriteDefaultStructParameters(source);
+
     line = ToString("if (typeof(%s) == this.GetType()", klass->GetName().CString());
     line += ToString(" || (this.GetType().BaseType == typeof(%s) && !NativeCore.GetNativeType(this.GetType())))\n", klass->GetName().CString());
 
@@ -598,6 +624,8 @@ void CSFunctionWriter::WriteManagedFunction(String& source)
 
     Indent();
 
+    WriteDefaultStructParameters(source);
+
     line.Clear();
 
     if (function_->GetReturnType())
@@ -716,4 +744,102 @@ void CSFunctionWriter::GenerateSource(String& sourceOut)
 
 }
 
+String CSFunctionWriter::MapDefaultParameter(JSBFunctionType* parameter)
+{
+
+    String init = parameter->initializer_;
+
+    if (!init.Length())
+        return init;
+
+    if (parameter->type_->asClassType())
+    {
+        if (init == "0")
+            return "null";
+    }
+
+    if (parameter->type_->asEnumType())
+    {
+        return parameter->type_->asEnumType()->enum_->GetName() + "." + init;
+    }
+
+    if (function_->class_->GetPackage()->ContainsConstant(init))
+        return "Constants." + init;
+
+    if (init == "true" || init == "false")
+        return init;
+
+    if (init == "0.0f")
+        return init;
+
+    if (init == "1.0f")
+        return init;
+
+    if (init == "0.1f")
+        return init;
+
+    if (init == "0")
+        return init;
+
+    if (init == "-1")
+        return init;
+
+    if (init == "\"\\t\"")
+        return init;
+
+    if (init == "NULL")
+        return "null";
+
+    if (init == "M_MAX_UNSIGNED")
+        return "0xffffffff";
+
+    if (init == "String::EMPTY")
+        return "\"\"";
+
+    // this kind of sucks, can't define const structs
+    // and default parameters need to be const :/
+
+    DefaultStructParameter dparm;
+
+    dparm.parameterName = parameter->name_;
+
+    if (init == "Vector3::ZERO")
+    {
+        dparm.type = "Vector3";
+        dparm.assignment = "Vector3.Zero";
+        defaultStructParameters_.Push(dparm);
+
+        return "default(Vector3)";
+    }
+
+    if (init == "Vector3::ONE")
+    {
+        dparm.type = "Vector3";
+        dparm.assignment = "Vector3.One";
+        defaultStructParameters_.Push(dparm);
+
+        return "default(Vector3)";
+    }
+
+    if (init == "Vector3::UP")
+    {
+        dparm.type = "Vector3";
+        dparm.assignment = "Vector3.Up";
+        defaultStructParameters_.Push(dparm);
+
+        return "default(Vector3)";
+    }
+
+    if (init == "IntVector2::ZERO")
+        return "IntVector2.Zero";
+
+    if (init == "Quaternion::IDENTITY")
+        return "Quaternion.Identity";
+
+
+    LOGINFOF("HEY! %s", init.CString());
+
+    return String::EMPTY;
+}
+
 }

+ 13 - 0
Source/ToolCore/JSBind/CSharp/CSFunctionWriter.h

@@ -34,6 +34,17 @@ public:
 
 private:
 
+    struct DefaultStructParameter
+    {
+        String type;
+        String parameterName;
+        String assignment;
+    };
+
+    Vector<DefaultStructParameter> defaultStructParameters_;
+
+    void WriteDefaultStructParameters(String& source);
+
     void GenNativeCallParameters(String& sig);
     void GenNativeFunctionSignature(String& sig);
 
@@ -42,6 +53,8 @@ private:
     void WriteNativeParameterMarshal(String& source);
 
 
+    String MapDefaultParameter(JSBFunctionType* parameter);
+
     void GenManagedFunctionParameters(String& sig);
 
     void GenPInvokeCallParameters(String& sig);

+ 3 - 1
Source/ToolCore/JSBind/CSharp/CSTypeHelper.cpp

@@ -15,7 +15,9 @@ String CSTypeHelper::GetManagedPrimitiveType(JSBPrimitiveType* ptype)
         return "int";
     if (ptype->kind_ == JSBPrimitiveType::Float)
         return "float";
-    if (ptype->kind_ == JSBPrimitiveType::Char)
+    if (ptype->kind_ == JSBPrimitiveType::Char && ptype->isUnsigned_)
+        return "byte";
+    else if (ptype->kind_ == JSBPrimitiveType::Char)
         return "char";
     if (ptype->kind_ == JSBPrimitiveType::Short)
         return "short";

+ 5 - 0
Source/ToolCore/JSBind/JSBHeaderVisitor.h

@@ -184,7 +184,12 @@ public:
 
 
         if (!jtype)
+        {
             jtype = processTypeConversion(type);
+            if (fst.isUnsigned() && jtype->asPrimitiveType())
+                jtype->asPrimitiveType()->isUnsigned_ = true;
+
+        }
 
         if (!jtype)
             return NULL;