Browse Source

Working on JS event subscriptions

Josh Engebretson 10 years ago
parent
commit
4e5f480f93

+ 20 - 0
Data/AtomicEditor/Resources/EditorData/AtomicEditor/javascript/ui/mainframe.js

@@ -0,0 +1,20 @@
+var UI = Atomic.UI;
+var UIWidget = Atomic.UIWidget;
+var graphics = Atomic.getGraphics();
+
+var view = new Atomic.UIView();
+
+var mainframe = new UIWidget();
+
+mainframe.load("AtomicEditor/editor/ui/mainframe.tb.txt");
+mainframe.setSize(graphics.width, graphics.height);
+
+var handleEvent = function() {
+
+  print("Got Event");
+
+}
+
+mainframe.subscribeToEvent("ScreenMode", handleEvent);
+
+view.addChild(mainframe);

+ 3 - 2
Source/Atomic/UI/UI.cpp

@@ -68,6 +68,9 @@ UI::~UI()
 {
     if (initialized_)
     {
+        TBFile::SetReaderFunction(0);
+        TBID::tbidRegisterCallback = 0;
+
         tb::TBWidgetsAnimationManager::Shutdown();
 
         widgetWrap_.Clear();
@@ -78,8 +81,6 @@ UI::~UI()
     }
 
     uiContext_ = 0;
-    TBFile::SetReaderFunction(0);
-    TBID::tbidRegisterCallback = 0;
 }
 
 void UI::Shutdown()

+ 1 - 1
Source/AtomicJS/JSBind/modules/Javascript.json

@@ -1,5 +1,5 @@
 {
 	"name" : "Javascript",
 	"sources" : ["Javascript"],
-	"classes" : ["JSVM", "JSComponent", "JSMetrics"]
+	"classes" : ["JSVM", "JSComponent", "JSMetrics", "JSEventHelper"]
 }

+ 2 - 0
Source/AtomicJS/Javascript/JSAtomic.cpp

@@ -14,6 +14,7 @@
 #include "JSEvents.h"
 #include "JSVM.h"
 #include "JSComponent.h"
+#include "JSCore.h"
 #include "JSGraphics.h"
 #include "JSIO.h"
 #include "JSUIAPI.h"
@@ -246,6 +247,7 @@ void jsapi_init_atomic(JSVM* vm)
     jsb_modules_init(vm);
 
     // extensions
+    jsapi_init_core(vm);
     jsapi_init_io(vm);
     jsapi_init_graphics(vm);
     jsapi_init_ui(vm);

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

@@ -0,0 +1,65 @@
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// Please see LICENSE.md in repository root for license information
+// https://github.com/AtomicGameEngine/AtomicGameEngine
+
+#include "JSCore.h"
+#include "JSEventHelper.h"
+#include "JSVM.h"
+
+namespace Atomic
+{
+
+static int Object_SubscribeToEvent(duk_context* ctx)
+{
+    duk_push_this(ctx); // stack 2
+
+    Object* object = js_to_class_instance<Object>(ctx, -1, 0);
+
+    duk_get_prop_string(ctx, -1, "__eventHelper");
+
+    if (duk_is_null_or_undefined(ctx, -1))
+    {
+        duk_pop(ctx); // pop null or undefined
+
+        // construct a new event helper
+        js_push_class_object_instance(ctx, new JSEventHelper(object->GetContext()));
+
+        duk_push_object(ctx);
+        duk_put_prop_string(ctx, -2, "__eventHelperFunctions");
+
+        // dup so when we set the helper is left on stack
+        duk_dup_top(ctx);
+
+        duk_put_prop_string(ctx, 2, "__eventHelper");
+    }
+
+    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);
+        helper->AddEventHandler(eventType);
+    }
+
+    return 0;
+}
+
+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_put_prop_string(ctx, -2, "subscribeToEvent");
+
+    duk_pop(ctx);
+}
+
+}

+ 16 - 0
Source/AtomicJS/Javascript/JSCore.h

@@ -0,0 +1,16 @@
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+// Please see LICENSE.md in repository root for license information
+// https://github.com/AtomicGameEngine/AtomicGameEngine
+
+#pragma once
+
+#include "JSAPI.h"
+
+namespace Atomic
+{
+
+class JSVM;
+
+void jsapi_init_core(JSVM* vm);
+
+}

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

@@ -0,0 +1,50 @@
+
+#include "JSVM.h"
+#include "JSEventHelper.h"
+
+namespace Atomic
+{
+
+JSEventHelper::JSEventHelper(Context* context) :
+    Object(context)
+{
+}
+
+JSEventHelper::~JSEventHelper()
+{
+    LOGINFO("Boom");
+}
+
+void JSEventHelper::AddEventHandler(StringHash eventType)
+{
+    SubscribeToEvent(eventType, HANDLER(JSEventHelper, HandleEvent));
+}
+
+void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
+{
+    JSVM* vm = JSVM::GetJSVM(context_);
+    duk_context* ctx = vm->GetJSContext();
+
+    duk_idx_t top = duk_get_top(ctx);
+
+    js_push_class_object_instance(ctx, this);
+
+    duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
+
+    assert(duk_is_object(ctx, -1));
+
+    duk_get_prop_string(ctx, -1, eventType.ToString().CString());
+
+    if (duk_is_function(ctx, -1))
+    {
+        if (duk_pcall(ctx, 0) != 0)
+        {
+            vm->SendJSErrorEvent();
+        }
+    }
+
+    duk_set_top(ctx, top);
+
+}
+
+}

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

@@ -0,0 +1,28 @@
+
+#pragma once
+
+#include <Atomic/Core/Object.h>
+
+namespace Atomic
+{
+
+class ATOMIC_API JSEventHelper : public Object
+{
+    OBJECT(JSEventHelper);
+
+public:
+    /// Construct.
+    JSEventHelper(Context* context);
+    /// Destruct.
+    virtual ~JSEventHelper();
+
+    void AddEventHandler(StringHash eventType);
+
+private:
+
+    void HandleEvent(StringHash eventType, VariantMap& eventData);
+
+};
+
+
+}

+ 0 - 2
Source/AtomicJS/Javascript/JSGraphics.cpp

@@ -61,8 +61,6 @@ void jsapi_init_graphics(JSVM* vm)
     duk_push_c_function(ctx, Light_SetShadowBias, 2);
     duk_put_prop_string(ctx, -2, "setShadowBias");
     duk_pop(ctx);
-
-
 }
 
 }

+ 58 - 1
Source/AtomicJS/Javascript/JSRequire.cpp

@@ -4,13 +4,70 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceCache.h>
 
+/*
+// subsystem requires
+
+#include <Atomic/Engine/Engine.h>
+#include <Atomic/Graphics/Graphics.h>
+#include <Atomic/Graphics/Renderer.h>
+#include <Atomic/Input/Input.h>
+#include <Atomic/Resource/ResourceCache.h>
+*/
+
 #include "JSVM.h"
 #include "JSRequire.h"
 #include "JSPlugin.h"
 
 namespace Atomic
 {
-    
+
+/*
+    static bool js_require_subsystem(const String& name, JSVM* vm)
+    {
+        duk_context* ctx = vm->GetJSContext();
+
+        String lowername = name.ToLower();
+
+        if (lowername.ToLower() == "input")
+        {
+            js_push_class_object_instance(ctx, vm->GetSubsystem<Input>());
+            return true;
+        }
+
+        if (lowername.ToLower() == "resourcecache")
+        {
+            js_push_class_object_instance(ctx, vm->GetSubsystem<ResourceCache>());
+            return true;
+        }
+
+        if (lowername.ToLower() == "engine")
+        {
+            js_push_class_object_instance(ctx, vm->GetSubsystem<Engine>());
+            return true;
+        }
+
+        if (lowername.ToLower() == "renderer")
+        {
+            js_push_class_object_instance(ctx, vm->GetSubsystem<Renderer>());
+            return true;
+        }
+
+        if (lowername == "graphics")
+        {
+            js_push_class_object_instance(ctx, vm->GetSubsystem<Graphics>());
+            return true;
+        }
+
+        if (lowername.ToLower() == "vm")
+        {
+            js_push_class_object_instance(ctx, vm);
+            return true;
+        }
+
+        return false;
+    }
+*/
+
     // see http://duktape.org/guide.html#modules   
     static int js_module_search(duk_context* ctx)
     {