Browse Source

Merge pull request #877 from AtomicGameEngine/TSH-TS-ENHANCEMENTS

TypeScript enhancements
JoshEngebretson 9 years ago
parent
commit
6eea8be63c

+ 1 - 1
Data/AtomicEditor/ProjectTemplates/Project2D/TypeScript/Resources/Components/Star.js

@@ -28,4 +28,4 @@ var Star = (function (_super) {
     };
     };
     return Star;
     return Star;
 })(Atomic.JSComponent);
 })(Atomic.JSComponent);
-module.exports = Star;
+exports.default = Star;

+ 1 - 3
Data/AtomicEditor/ProjectTemplates/Project2D/TypeScript/Resources/Components/Star.ts

@@ -3,7 +3,7 @@
 /**
 /**
  * Component that will rotate a node at a configurable speed.
  * Component that will rotate a node at a configurable speed.
  */
  */
-class Star extends Atomic.JSComponent {
+export default class Star extends Atomic.JSComponent {
 
 
     /**
     /**
      * Fields witihin the inspectorFields object will be exposed to the editor
      * Fields witihin the inspectorFields object will be exposed to the editor
@@ -28,5 +28,3 @@ class Star extends Atomic.JSComponent {
 
 
     }
     }
 }
 }
-
-export = Star;

+ 1 - 1
Data/AtomicEditor/ProjectTemplates/Project3D/TypeScript/Resources/Components/Spinner.js

@@ -28,4 +28,4 @@ var Spinner = (function (_super) {
     };
     };
     return Spinner;
     return Spinner;
 })(Atomic.JSComponent);
 })(Atomic.JSComponent);
-module.exports = Spinner;
+exports.default = Spinner;

+ 1 - 3
Data/AtomicEditor/ProjectTemplates/Project3D/TypeScript/Resources/Components/Spinner.ts

@@ -3,7 +3,7 @@
 /**
 /**
  * Component that will rotate a node at a configurable speed.
  * Component that will rotate a node at a configurable speed.
  */
  */
-class Spinner extends Atomic.JSComponent {
+export default class Spinner extends Atomic.JSComponent {
 
 
     /**
     /**
      * Fields witihin the inspectorFields object will be exposed to the editor
      * Fields witihin the inspectorFields object will be exposed to the editor
@@ -28,5 +28,3 @@ class Spinner extends Atomic.JSComponent {
 
 
     }
     }
 }
 }
-
-export = Spinner;

+ 1 - 3
Resources/EditorData/AtomicEditor/templates/template_ts_component.ts

@@ -3,7 +3,7 @@
 /**
 /**
  * A new component
  * A new component
  */
  */
-class Component extends Atomic.JSComponent {
+export default class Component extends Atomic.JSComponent {
 
 
     /**
     /**
      * Fields witihin the inspectorFields object will be exposed to the editor
      * Fields witihin the inspectorFields object will be exposed to the editor
@@ -26,5 +26,3 @@ class Component extends Atomic.JSComponent {
 
 
     }
     }
 }
 }
-
-export = Component;

+ 7 - 0
Script/Packages/Atomic/Resource.json

@@ -7,5 +7,12 @@
 			"GetPixel": ["int", "int"],
 			"GetPixel": ["int", "int"],
 			"SetSize": ["int", "int", "int", "unsigned"]
 			"SetSize": ["int", "int", "int", "unsigned"]
 		}
 		}
+	},
+	"typescript_decl" : {
+		"ResourceCache" : [
+			"getResource<T extends Resource>(type: string, name: string, sendEventOnFailure?: boolean): T;",
+			"getTempResource<T extends Resource>(type: string, name: string, sendEventOnFailure?: boolean): T;",
+	        "getExistingResource<T extends Resource>(type: string, name: string): T;"
+		]
 	}
 	}
 }
 }

+ 6 - 0
Script/Packages/Atomic/UI.json

@@ -16,6 +16,12 @@
 
 
 		"UIButton" : [
 		"UIButton" : [
 			"onClick: () => void;"
 			"onClick: () => void;"
+		],
+		"UI": [
+		    "getWidgetAt<T extends UIWidget>(x: number, y: number, include_children: boolean): T;"
+		],
+		"UIWidget": [
+		    "getWidget<T extends UIWidget>(id: string): T;"
 		]
 		]
 	}
 	}
 
 

+ 12 - 3
Source/AtomicJS/Javascript/JSComponent.cpp

@@ -280,12 +280,21 @@ void JSComponent::InitInstance(bool hasArgs, int argIdx)
             return;
             return;
         }
         }
 
 
-        duk_get_prop_string(ctx, -1, "component");
+        // Check for "default" constructor which is used by TypeScript and ES2015
+        duk_get_prop_string(ctx, -1, "default");
 
 
         if (!duk_is_function(ctx, -1))
         if (!duk_is_function(ctx, -1))
         {
         {
-            duk_set_top(ctx, top);
-            return;
+            duk_pop(ctx);
+
+            // If "default" doesn't exist, look for component
+            duk_get_prop_string(ctx, -1, "component");
+
+            if (!duk_is_function(ctx, -1))
+            {
+                duk_set_top(ctx, top);
+                return;
+            }
         }
         }
 
 
         // call with self
         // call with self

+ 24 - 6
Source/AtomicJS/Javascript/JSComponentFile.cpp

@@ -98,6 +98,14 @@ JSComponent* JSComponentFile::CreateJSComponent()
 
 
         PushModule();
         PushModule();
 
 
+        // Check to see if the module exposes a "default" constructor which is used by TS and ES2015
+        duk_get_prop_string(ctx, -1, "default");
+        if (!duk_is_function(ctx, -1))
+        {
+            // We are not a "default" style object, so reset the stack
+            duk_pop(ctx);
+        }
+
         duk_new(ctx, 0);
         duk_new(ctx, 0);
 
 
         if (duk_is_object(ctx, -1))
         if (duk_is_object(ctx, -1))
@@ -145,16 +153,26 @@ bool JSComponentFile::InitModule()
     }
     }
     else if (duk_is_object(ctx, -1))
     else if (duk_is_object(ctx, -1))
     {
     {
-        duk_get_prop_string(ctx, -1, "component");
-
+        // Look for "default" constructor which is used by TypeScript and ES2015
+        duk_get_prop_string(ctx, -1, "default");
         if (!duk_is_function(ctx, -1))
         if (!duk_is_function(ctx, -1))
         {
         {
-            LOGERRORF("Component file export object does not export a key \"component\" function: %s", GetName().CString());
-            duk_set_top(ctx, top);
-            return false;
+            duk_pop(ctx);
+            duk_get_prop_string(ctx, -1, "component");
+
+            if (!duk_is_function(ctx, -1))
+            {
+                LOGERRORF("Component file export object does not export a key \"component\" function: %s", GetName().CString());
+                duk_set_top(ctx, top);
+                return false;
+            }
+
+            scriptClass_ = false;
+        } else {
+            // this is a TS or ES2015 default class
+            scriptClass_ = true;
         }
         }
 
 
-        scriptClass_ = false;
     }
     }
 
 
     duk_set_top(ctx, top);
     duk_set_top(ctx, top);