Browse Source

Handle recursive event data, by pushing variant map per event

Josh Engebretson 10 years ago
parent
commit
7bfe8c38ba

+ 16 - 0
Script/AtomicEditor/editor/EditorEvents.ts

@@ -9,6 +9,22 @@ export interface ModalErrorEvent {
 
 }
 
+export const ContentFolderChanged = "ContentFolderChanged";
+export interface ContentFolderChangedEvent {
+
+  path: string;
+
+}
+
+export const ResourceFolderCreated = "ResourceFolderCreated";
+export interface ResourceFolderCreatedEvent {
+
+  path: string;
+  navigate:boolean;
+
+}
+
+
 export const LoadProject = "EditorLoadProject";
 export interface LoadProjectEvent {
 

+ 3 - 0
Script/AtomicEditor/resources/ResourceOps.ts

@@ -21,12 +21,15 @@ export function CreateNewFolder(resourcePath: string, reportError: boolean = tru
     }
 
     if (!fs.createDir(resourcePath)) {
+
         if (reportError)
             resourceOps.sendEvent(EditorEvents.ModalError, { title: title, message: "Could not create " + resourcePath });
 
         return false;
     }
 
+    resourceOps.sendEvent(EditorEvents.ResourceFolderCreated, { path: resourcePath});
+
     return true;
 
 }

+ 43 - 3
Script/AtomicEditor/ui/ProjectFrame.ts

@@ -37,6 +37,28 @@ class ProjectFrame extends ScriptWidget {
         // events
         this.subscribeToEvent("ProjectLoaded", (data) => this.handleProjectLoaded(data));
         this.subscribeToEvent("DragEnded", (data) => this.handleDragEnded(data));
+        this.subscribeToEvent(EditorEvents.ResourceFolderCreated, (ev: EditorEvents.ResourceFolderCreatedEvent) => this.handleResourceFolderCreated(ev));
+
+        // this uses FileWatcher which doesn't catch subfolder creation
+        this.subscribeToEvent("FileChanged", (data) => {
+
+            // console.log("File CHANGED! ", data.fileName);
+
+        })
+
+    }
+
+    handleResourceFolderCreated(ev: EditorEvents.ResourceFolderCreatedEvent) {
+
+      var db = ToolCore.getAssetDatabase();
+      db.scan();
+
+      this.refresh();
+
+      var asset = db.getAssetByPath(ev.path);
+
+      if (asset && ev.navigate)
+        this.selectPath(asset.path);
 
     }
 
@@ -50,7 +72,7 @@ class ProjectFrame extends ScriptWidget {
             var project = system.project;
 
 
-            if (this.menu.handlePopupMenu(data.target, data.refid, this.currentFolder ? this.currentFolder.path : project.resourcePath))
+            if (this.menu.handlePopupMenu(data.target, data.refid))
                 return true;
 
             // create
@@ -118,10 +140,22 @@ class ProjectFrame extends ScriptWidget {
     rescan(asset: ToolCore.Asset) {
 
         var db = ToolCore.getAssetDatabase();
-
         db.scan();
         this.refresh();
-        this.refreshContent(asset);
+    }
+
+    selectPath(path:string) {
+
+      var db = ToolCore.getAssetDatabase();
+
+      var asset = db.getAssetByPath(path);
+
+      console.log("Select Path: ", path, " ", asset);
+
+      if (!asset)
+        return;
+
+      this.folderList.selectItemByID(asset.guid);
 
     }
 
@@ -192,6 +226,12 @@ class ProjectFrame extends ScriptWidget {
 
     private refreshContent(folder: ToolCore.Asset) {
 
+        if (this.currentFolder != folder) {
+
+            this.sendEvent(EditorEvents.ContentFolderChanged, { path: folder.path });
+
+        }
+
         this.currentFolder = folder;
 
         var db = ToolCore.getAssetDatabase();

+ 8 - 2
Script/AtomicEditor/ui/ProjectFrameMenu.ts

@@ -12,9 +12,13 @@ class ProjectFrameMenus extends Atomic.ScriptObject {
 
         MenuItemSources.createMenuItemSource("project create items", createItems);
 
+        this.subscribeToEvent(EditorEvents.ContentFolderChanged, (ev:EditorEvents.ContentFolderChangedEvent) => {
+          this.contentFolder = ev.path;
+        })
+
     }
 
-    handlePopupMenu(target: Atomic.UIWidget, refid: string, currentContentFolder:string): boolean {
+    handlePopupMenu(target: Atomic.UIWidget, refid: string): boolean {
 
         if (!target || !refid) return;
 
@@ -22,7 +26,7 @@ class ProjectFrameMenus extends Atomic.ScriptObject {
 
             if (refid == "create_folder") {
 
-                EditorUI.getModelOps().showCreateFolder(currentContentFolder);
+                EditorUI.getModelOps().showCreateFolder(this.contentFolder);
 
                 return true;
 
@@ -40,6 +44,8 @@ class ProjectFrameMenus extends Atomic.ScriptObject {
 
     }
 
+    contentFolder:string;
+
 }
 
 export = ProjectFrameMenus;

+ 59 - 0
Source/AtomicJS/Javascript/JSAPI.cpp

@@ -290,6 +290,65 @@ void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
 
 }
 
+
+// variant map Proxy getter, so we can convert access to string based
+// member lookup, to string hash on the fly
+
+static int variantmap_property_get(duk_context* ctx)
+{
+    // targ, key, recv
+
+    if (duk_is_string(ctx, 1))
+    {
+        StringHash key = duk_to_string(ctx, 1);
+        duk_get_prop_index(ctx, 0, (unsigned) key.Value());
+        return 1;
+    }
+
+    duk_push_undefined(ctx);
+    return 1;
+
+}
+
+
+void js_push_variantmap(duk_context* ctx, const VariantMap &vmap)
+{
+
+    // setup proxy so we can map string
+    duk_get_global_string(ctx, "Proxy");
+
+    duk_push_object(ctx);
+
+    VariantMap::ConstIterator itr = vmap.Begin();
+
+    while (itr != vmap.End()) {
+
+        js_push_variant(ctx, itr->second_);
+
+
+        if (duk_is_undefined(ctx, -1)) {
+
+            duk_pop(ctx);
+        }
+        else
+        {
+            duk_put_prop_index(ctx, -2, (unsigned) itr->first_.Value());
+        }
+
+        itr++;
+
+    }
+
+    // setup property handler
+    duk_push_object(ctx);
+    duk_push_c_function(ctx, variantmap_property_get, 3);
+    duk_put_prop_string(ctx, -2, "get");
+
+    duk_new(ctx, 2);
+
+
+}
+
 void js_push_variant(duk_context *ctx, const Variant& v)
 {
     VariantType type = v.GetType();

+ 1 - 0
Source/AtomicJS/Javascript/JSAPI.h

@@ -39,6 +39,7 @@ void js_class_get_constructor(duk_context* ctx, const char* package, const char
 
 /// Pushes variant value or undefined if can't be pushed
 void js_push_variant(duk_context* ctx, const Variant &v);
+void js_push_variantmap(duk_context* ctx, const VariantMap &vmap);
 
 void js_to_variant(duk_context* ctx, int variantIdx, Variant &v);
 

+ 0 - 39
Source/AtomicJS/Javascript/JSCore.cpp

@@ -11,33 +11,6 @@
 namespace Atomic
 {
 
-static int EventHandler_Property_Get(duk_context* ctx)
-{
-    // targ, key, recv
-
-    if (duk_is_string(ctx, 1))
-    {
-        const char* cstr = duk_to_string(ctx, 1);
-        StringHash key = cstr;
-
-        JSEventHelper* helper = js_to_class_instance<JSEventHelper>(ctx, 0, 0);
-        VariantMap& data = helper->GetCurrentData();
-
-        if (!data.Contains(key))
-        {
-            duk_push_undefined(ctx);
-            return 1;
-        }
-
-        js_push_variant(ctx, data[key]);
-        return 1;
-    }
-
-    duk_push_undefined(ctx);
-    return 1;
-
-}
-
 static int Object_SubscribeToEvent(duk_context* ctx)
 {
 
@@ -84,18 +57,6 @@ static int Object_SubscribeToEvent(duk_context* ctx)
         // construct a new event helper
         js_push_class_object_instance(ctx, new JSEventHelper(object->GetContext()));
 
-        // proxy support
-        duk_get_global_string(ctx, "Proxy");
-
-        duk_dup(ctx, -2);
-
-        // setup property handler
-        duk_push_object(ctx);
-        duk_push_c_function(ctx, EventHandler_Property_Get, 3);
-        duk_put_prop_string(ctx, -2, "get");
-        duk_new(ctx, 2);
-        duk_put_prop_string(ctx, -2, "__eventHelperProxy");
-
         duk_push_object(ctx);
         duk_put_prop_string(ctx, -2, "__eventHelperFunctions");
 

+ 8 - 7
Source/AtomicJS/Javascript/JSEventHelper.cpp

@@ -8,8 +8,7 @@ namespace Atomic
 {
 
 JSEventHelper::JSEventHelper(Context* context) :
-    Object(context),
-    currentData_((VariantMap&) Variant::emptyVariantMap)
+    Object(context)
 {
 }
 
@@ -29,7 +28,8 @@ void JSEventHelper::AddEventHandler(Object* sender, StringHash eventType)
 }
 
 void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
-{
+{    
+
     JSVM* vm = JSVM::GetJSVM(0);
     duk_context* ctx = vm->GetJSContext();
 
@@ -45,12 +45,13 @@ void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
 
     if (duk_is_function(ctx, -1))
     {
-        currentData_ = (const VariantMap&) eventData;
-
-        // pass in event helper proxy
-        duk_get_prop_string(ctx, -3, "__eventHelperProxy");
         assert(duk_is_object(ctx, -1));
 
+        //TODO: this is expensive, we should be caching these in VM
+        // and reusing, instead of creating new variant map object
+        // per event handler
+        js_push_variantmap(ctx, eventData);
+
         if (duk_pcall(ctx, 1) != 0)
         {
             vm->SendJSErrorEvent();

+ 0 - 5
Source/AtomicJS/Javascript/JSEventHelper.h

@@ -17,17 +17,12 @@ public:
     virtual ~JSEventHelper();
 
     void AddEventHandler(StringHash eventType);
-
     void AddEventHandler(Object* sender, StringHash eventType);
 
-    VariantMap& GetCurrentData() { return currentData_; }
-
 private:
 
     void HandleEvent(StringHash eventType, VariantMap& eventData);
 
-    VariantMap& currentData_;
-
 };