Browse Source

Adding ability to listen to specific object sender for events

Josh Engebretson 10 years ago
parent
commit
3fcfddcefd

+ 9 - 1
Data/AtomicEditor/Resources/EditorData/AtomicEditor/javascript/ui/mainframe.js

@@ -8,7 +8,8 @@ var mainframe = new UIWidget();
 
 mainframe.setSize(graphics.width, graphics.height);
 
-mainframe.subscribeToEvent("ScreenMode", function(data) {
+// Subscribe to graphics subsystems screen mode switching
+mainframe.subscribeToEvent(graphics, "ScreenMode", function(data) {
 
   mainframe.setSize(data.width, data.height);
 
@@ -20,6 +21,13 @@ mainframe.subscribeToEvent("WidgetLoaded", function(data) {
 
 });
 
+mainframe.subscribeToEvent("BeginFrame", function(data) {
+
+  print("Update", data.frameNumber);
+
+});
+
+
 mainframe.load("AtomicEditor/editor/ui/mainframe.tb.txt");
 
 view.addChild(mainframe);

+ 43 - 12
Source/AtomicJS/Javascript/JSCore.cpp

@@ -38,12 +38,44 @@ static int EventHandler_Property_Get(duk_context* ctx)
 
 static int Object_SubscribeToEvent(duk_context* ctx)
 {
+
+    int top = duk_get_top(ctx);
+
+    Object* sender = NULL;
+
+    StringHash eventType = StringHash::ZERO;
+
+
+    if ( top == 2 ) // General notification: subscribeToEvent("ScreenMode", function() {});
+    {
+        if (duk_is_string(ctx, 0) && duk_is_function(ctx, 1))
+        {
+            eventType = duk_to_string(ctx, 0);
+        }
+    }
+    else if (top == 3) // Listen to specific object sender subscribeToEvent(graphics, "ScreenMode", function() {});
+    {
+        if (duk_is_object(ctx, 0) && duk_is_string(ctx, 1) && duk_is_function(ctx, 2))
+        {
+            sender = js_to_class_instance<Object>(ctx, 0, 0);
+            eventType = duk_to_string(ctx, 1);
+        }
+    }
+
+    if ( eventType == StringHash::ZERO)
+    {
+        duk_push_string(ctx, "Object.subscribeToEvent() - Bad Arguments");
+        duk_throw(ctx);
+    }
+
     duk_push_this(ctx); // stack 2
 
+    // event receiver
     Object* object = js_to_class_instance<Object>(ctx, -1, 0);
 
     duk_get_prop_string(ctx, -1, "__eventHelper");
 
+    // need to setup the handler
     if (duk_is_null_or_undefined(ctx, -1))
     {
         duk_pop(ctx); // pop null or undefined
@@ -74,18 +106,17 @@ static int Object_SubscribeToEvent(duk_context* ctx)
 
     JSEventHelper* helper = js_to_class_instance<JSEventHelper>(ctx, -1, 0);
 
-    if (duk_is_string(ctx, 0))
-    {
-        StringHash eventType = duk_to_string(ctx, 0);
-
-        duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
-        assert(duk_is_object(ctx, -1));
-        assert(duk_is_function(ctx, 1));
-        duk_dup(ctx, 1);
-        duk_put_prop_string(ctx, -2,  eventType.ToString().CString());
-        duk_pop(ctx);
+    duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
+    assert(duk_is_object(ctx, -1));
+    assert(duk_is_function(ctx, sender ? 2 : 1));
+    duk_dup(ctx, sender ? 2 : 1);
+    duk_put_prop_string(ctx, -2,  eventType.ToString().CString());
+    duk_pop(ctx);
+
+    if (sender)
+        helper->AddEventHandler(sender, eventType);
+    else
         helper->AddEventHandler(eventType);
-    }
 
     return 0;
 }
@@ -95,7 +126,7 @@ void jsapi_init_core(JSVM* vm)
     duk_context* ctx = vm->GetJSContext();
 
     js_class_get_prototype(ctx, "AObject");
-    duk_push_c_function(ctx, Object_SubscribeToEvent, 2);
+    duk_push_c_function(ctx, Object_SubscribeToEvent, DUK_VARARGS);
     duk_put_prop_string(ctx, -2, "subscribeToEvent");
     duk_pop(ctx);
 }

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

@@ -21,6 +21,11 @@ void JSEventHelper::AddEventHandler(StringHash eventType)
     SubscribeToEvent(eventType, HANDLER(JSEventHelper, HandleEvent));
 }
 
+void JSEventHelper::AddEventHandler(Object* sender, StringHash eventType)
+{
+    SubscribeToEvent(sender, eventType, HANDLER(JSEventHelper, HandleEvent));
+}
+
 void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
 {
     JSVM* vm = JSVM::GetJSVM(context_);

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

@@ -18,6 +18,8 @@ public:
 
     void AddEventHandler(StringHash eventType);
 
+    void AddEventHandler(Object* sender, StringHash eventType);
+
     VariantMap& GetCurrentData() { return currentData_; }
 
 private: