Browse Source

Toggling node enabled field from script

Josh Engebretson 10 years ago
parent
commit
3fdca13a93

+ 59 - 2
Data/AtomicEditor/Resources/EditorData/AtomicEditor/typescript/ui/inspector/NodeInspector.ts

@@ -62,8 +62,6 @@ class DataBinding {
 
         }
 
-
-
         if (widget) {
 
             var binding = new DataBinding(object, attrInfo, widget);
@@ -75,7 +73,45 @@ class DataBinding {
 
     }
 
+    setObjectValueFromWidget(srcWidget:Atomic.UIWidget) {
+
+      if (this.objectLocked)
+          return;
+
+      this.objectLocked = true;
+
+      var type = this.attrInfo.type;
+
+      if (type == Atomic.VAR_BOOL)
+      {
+          var box = <Atomic.UICheckBox> this.widget;
+
+          this.object.setAttribute(this.attrInfo.name, box.value ? true : false);
+      }
+
+
+
+      this.objectLocked = false;
+
+    }
+
+    handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
+
+      if (this.objectLocked)
+          return false;
+
+      if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED)
+      {
+          if (this.widget == ev.target || this.widget.isAncestorOf(ev.target))
+          {
+              this.setObjectValueFromWidget(ev.target);
+          }
+      }
+
+    }
+
     object: Atomic.Serializable;
+    objectLocked:boolean = false;
     attrInfo: Atomic.AttributeInfo;
     widget: Atomic.UIWidget;
 
@@ -87,8 +123,24 @@ class NodeInspector extends ScriptWidget {
 
         super();
 
+        this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
+
     }
 
+    handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
+
+        for (var i = 0; i < this.bindings.length; i++) {
+
+          this.bindings[i].handleWidgetEvent(ev);
+
+        }
+
+        // return handled
+        return true;
+
+    }
+
+
     inspect(node: Atomic.Node) {
 
         var fd = new Atomic.UIFontDescription();
@@ -152,6 +204,7 @@ class NodeInspector extends ScriptWidget {
 
 
             var bname = attr.name;
+
             if (bname == "Is Enabled")
                 bname = "Enabled";
 
@@ -164,12 +217,16 @@ class NodeInspector extends ScriptWidget {
 
             attrsVerticalLayout.addChild(attrLayout);
 
+            this.bindings.push(binding);
+
         }
 
         this.addChild(nodeLayout);
 
     }
 
+    bindings:Array<DataBinding> = new Array();
+
 
 }
 

+ 9 - 0
Source/Atomic/UI/UIWidget.cpp

@@ -154,6 +154,15 @@ void UIWidget::SetGravity(UI_GRAVITY gravity)
 
 }
 
+bool UIWidget::IsAncestorOf(UIWidget* widget)
+{
+    if (!widget_ || !widget || !widget->widget_)
+        return false;
+
+    return widget_->IsAncestorOf(widget->widget_);
+
+}
+
 void UIWidget::SetPosition(int x, int y)
 {
     if (!widget_)

+ 58 - 0
Source/Atomic/UI/UIWidget.h

@@ -39,6 +39,62 @@ enum UI_GRAVITY {
     UI_GRAVITY_DEFAULT		= tb::WIDGET_GRAVITY_DEFAULT
 };
 
+enum UI_EVENT_TYPE {
+    /** Click event is what should be used to trig actions in almost all cases.
+
+        It is invoked on a widget after POINTER_UP if the pointer is still inside
+        its hit area. It can also be invoked by keyboard on some clickable widgets
+        (see TBWidget::SetClickByKey).
+
+        If panning of scrollable widgets start while the pointer is down, CLICK
+        won't be invoked when releasing the pointer (since that should stop panning). */
+    UI_EVENT_TYPE_CLICK = tb::EVENT_TYPE_CLICK,
+
+    /** Long click event is sent when the pointer has been down for some time
+        without moving much.
+
+        It is invoked on a widget that has enabled it (TBWidget::SetWantLongClick
+        If this event isn't handled, the widget will invoke a CONTEXT_MENU event.
+        If any of those are handled, the CLICK event that would normally be
+        invoked after the pending POINTER_UP will be suppressed. */
+    UI_EVENT_TYPE_LONG_CLICK = tb::EVENT_TYPE_LONG_CLICK,
+    UI_EVENT_TYPE_POINTER_DOWN = tb::EVENT_TYPE_POINTER_DOWN,
+    UI_EVENT_TYPE_POINTER_UP = tb::EVENT_TYPE_POINTER_UP,
+    UI_EVENT_TYPE_POINTER_MOVE = tb::EVENT_TYPE_POINTER_MOVE,
+    UI_EVENT_TYPE_RIGHT_POINTER_DOWN = tb::EVENT_TYPE_RIGHT_POINTER_DOWN,
+    UI_EVENT_TYPE_RIGHT_POINTER_UP = tb::EVENT_TYPE_RIGHT_POINTER_UP,
+    UI_EVENT_TYPE_WHEEL = tb::EVENT_TYPE_WHEEL,
+
+    /** Invoked after changing text in a TBTextField, changing selected item
+        in a TBSelectList etc. Invoking this event trigs synchronization with
+        connected TBWidgetValue and other widgets connected to it. */
+    UI_EVENT_TYPE_CHANGED = tb::EVENT_TYPE_CHANGED,
+    UI_EVENT_TYPE_KEY_DOWN = tb::EVENT_TYPE_KEY_DOWN,
+    UI_EVENT_TYPE_KEY_UP = tb::EVENT_TYPE_KEY_UP,
+
+    /** Invoked by the platform when a standard keyboard shortcut is pressed.
+        It's called before InvokeKeyDown (EVENT_TYPE_KEY_DOWN) and if the event
+        is handled (returns true), the KeyDown is canceled.
+        The ref_id will be set to one of the following:
+            "cut", "copy", "paste", "selectall", "undo", "redo", "new", "open", "save". */
+    UI_EVENT_TYPE_SHORTCUT = tb::EVENT_TYPE_SHORTCUT,
+
+    /** Invoked when a context menu should be opened at the event x and y coordinates.
+        It may be invoked automatically for a widget on long click, if nothing handles
+        the long click event. */
+    UI_EVENT_TYPE_CONTEXT_MENU = tb::EVENT_TYPE_CONTEXT_MENU,
+
+    /** Invoked by the platform when one or multiple files has been dropped on
+        the widget. The event is guaranteed to be a TBWidgetEventFileDrop. */
+    UI_EVENT_TYPE_FILE_DROP = tb::EVENT_TYPE_FILE_DROP,
+
+    /** Invoked by the platform when a tab container's tab changed */
+    UI_EVENT_TYPE_TAB_CHANGED = tb::EVENT_TYPE_TAB_CHANGED,
+
+    /** Custom event. Not used internally. ref_id may be used for additional type info. */
+    UI_EVENT_TYPE_CUSTOM = tb::EVENT_TYPE_CUSTOM
+};
+
 
 class UILayoutParams;
 class UIFontDescription;
@@ -106,6 +162,8 @@ public:
     UIWidget* GetFirstChild();
     UIWidget* GetNext();
 
+    bool IsAncestorOf(UIWidget* widget);
+
     void SetIsFocusable(bool value);
 
 

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

@@ -202,6 +202,35 @@ void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)
 
 }
 
+void js_to_variant(duk_context* ctx, int variantIdx, Variant &v)
+{
+    v.Clear();
+
+    if (duk_is_boolean(ctx, variantIdx))
+    {
+        v = duk_to_boolean(ctx, variantIdx) ? true : false;
+        return;
+    }
+
+    if (duk_is_string(ctx, variantIdx))
+    {
+        v = duk_to_string(ctx, variantIdx);
+        return;
+    }
+
+    if (duk_is_number(ctx, variantIdx))
+    {
+        v = (float) duk_to_number(ctx, variantIdx);
+        return;
+    }
+
+    if (duk_is_pointer(ctx, variantIdx))
+    {
+        v = (RefCounted*) duk_get_pointer(ctx, variantIdx);
+        return;
+    }
+}
+
 void js_push_variant(duk_context *ctx, const Variant& v)
 {
     VariantType type = v.GetType();

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

@@ -39,6 +39,8 @@ void js_class_get_prototype(duk_context* ctx, const char* package, const char *c
 /// Pushes variant value or undefined if can't be pushed
 void js_push_variant(duk_context* ctx, const Variant &v);
 
+void js_to_variant(duk_context* ctx, int variantIdx, Variant &v);
+
 void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v);
 
 }

+ 17 - 0
Source/AtomicJS/Javascript/JSSceneSerializable.cpp

@@ -33,6 +33,21 @@ namespace Atomic
     void* ptr_;
 
 */
+
+static int Serializable_SetAttribute(duk_context* ctx)
+{
+    const char* name = duk_to_string(ctx, 0);
+    Variant v;
+    js_to_variant(ctx, 1, v);
+
+    duk_push_this(ctx);
+    Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
+
+    serial->SetAttribute(name, v);
+
+    return 0;
+}
+
 static int Serializable_GetAttributes(duk_context* ctx)
 {
     duk_push_this(ctx);
@@ -104,6 +119,8 @@ void jsapi_init_scene_serializable(JSVM* vm)
     js_class_get_prototype(ctx, "Atomic", "Serializable");
     duk_push_c_function(ctx, Serializable_GetAttributes, 0);
     duk_put_prop_string(ctx, -2, "getAttributes");
+    duk_push_c_function(ctx, Serializable_SetAttribute, 2);
+    duk_put_prop_string(ctx, -2, "setAttribute");
     duk_pop(ctx);
 
 }