Browse Source

Working on data binding

Josh Engebretson 10 years ago
parent
commit
4ba3608a07

+ 8 - 1
Source/Atomic/UI/TBUI.cpp

@@ -245,7 +245,8 @@ TBUI::TBUI(Context* context) :
     fadeAlpha_(1.0f),
     fadeTarget_(1.0f),
     currentFadeTime_(0.0f),
-    fadeTime_(0.0f)
+    fadeTime_(0.0f),
+    shuttingDown_(false)
 {
     SubscribeToEvent(E_SCREENMODE, HANDLER(TBUI, HandleScreenMode));
 }
@@ -330,6 +331,12 @@ void TBUI::Initialize()
     initialized_ = true;
 }
 
+void TBUI::Shutdown()
+{
+    shuttingDown_ = true;
+    SetInputDisabled(true);
+}
+
 void TBUI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
 {
     if (!initialized_)

+ 4 - 1
Source/Atomic/UI/TBUI.h

@@ -57,9 +57,10 @@ public:
     float GetFadeAlpha() { return fadeAlpha_; }
     void SetFadeAlpha(float fadeAlpha) { fadeAlpha_ = fadeAlpha; }
 
-
     void Initialize();
 
+    void Shutdown();
+
 private:
 
     static WeakPtr<Context> readerContext_;
@@ -83,6 +84,8 @@ private:
 
     WeakPtr<Graphics> graphics_;
 
+    bool shuttingDown_;
+
 
 
 };

+ 3 - 0
Source/AtomicEditor/Source/AEEditor.cpp

@@ -308,6 +308,9 @@ void Editor::HandleExitRequested(StringHash eventType, VariantMap& eventData)
         aepreferences_->Write();
     }
 
+    TBUI* tbui = GetSubsystem<TBUI>();
+    tbui->Shutdown();
+
     mainframe_ = 0;
     player_ = 0;
     project_ = 0;

+ 261 - 0
Source/AtomicEditor/Source/UI/UIInspectorDataBinding.cpp

@@ -0,0 +1,261 @@
+
+#include <TurboBadger/tb_widgets_common.h>
+#include <TurboBadger/tb_inline_select.h>
+
+#include "AtomicEditor.h"
+
+#include <Atomic/Core/StringUtils.h>
+
+#include "UIInspectorDataBinding.h"
+
+namespace AtomicEditor
+{
+
+void InspectorDataBinding::SetObjectValueFromWidget(TBWidget *srcWidget)
+{
+    if (objectLocked_)
+        return;
+
+    objectLocked_ = true;
+
+    VariantType type = attrInfo_->type_;
+
+    if (type == VAR_BOOL)
+    {
+        TBCheckBox* box = (TBCheckBox *) widget_;
+        Variant v(box->GetValue() ? true : false);
+        object_->SetAttribute(attrInfo_->name_, v);
+
+        // refresh widget
+        SetWidgetValueFromObject();
+    }
+    else if (type == VAR_VECTOR3 && srcWidget)
+    {
+        Vector3 value = object_->GetAttribute(attrInfo_->name_).GetVector3();
+
+        if (srcWidget->GetID() == TBID(unsigned(1)))
+            value.x_ = srcWidget->GetValue();
+        else if (srcWidget->GetID() == TBID(unsigned(2)))
+            value.y_ = srcWidget->GetValue();
+        else if (srcWidget->GetID() == TBID(unsigned(3)))
+            value.z_ = srcWidget->GetValue();
+
+        object_->SetAttribute(attrInfo_->name_, value);
+    }
+    else if (type == VAR_QUATERNION && srcWidget)
+    {
+        Quaternion q = object_->GetAttribute(attrInfo_->name_).GetQuaternion();
+
+        Vector3 value = q.EulerAngles();
+
+        if (srcWidget->GetID() == TBID(unsigned(1)))
+            value.x_ = srcWidget->GetValue();
+        else if (srcWidget->GetID() == TBID(unsigned(2)))
+            value.y_ = srcWidget->GetValue();
+        else if (srcWidget->GetID() == TBID(unsigned(3)))
+            value.z_ = srcWidget->GetValue();
+
+        q.FromEulerAngles(value.x_, value.y_, value.z_);
+
+        object_->SetAttribute(attrInfo_->name_, q);
+    }
+
+
+    objectLocked_ = false;
+
+}
+
+void InspectorDataBinding::SetWidgetValueFromObject()
+{
+    if (widgetLocked_)
+        return;
+
+    widgetLocked_ = true;
+
+    if (attrInfo_->type_ == VAR_BOOL)
+    {
+        bool value = object_->GetAttribute(attrInfo_->name_).GetBool();
+        widget_->SetValue(value ? 1 : 0);
+    }
+    else if (attrInfo_->type_ == VAR_VECTOR3)
+    {
+        Vector3 value = object_->GetAttribute(attrInfo_->name_).GetVector3();
+
+        TBInlineSelect* select = widget_->GetWidgetByIDAndType<TBInlineSelect>(TBID(unsigned(1)));
+        if (select)
+            select->SetValue(value.x_);
+        select = widget_->GetWidgetByIDAndType<TBInlineSelect>(TBID(unsigned(2)));
+        if (select)
+            select->SetValue(value.y_);
+        select = widget_->GetWidgetByIDAndType<TBInlineSelect>(TBID(unsigned(3)));
+        if (select)
+            select->SetValue(value.z_);
+    }
+    else if (attrInfo_->type_ == VAR_QUATERNION)
+    {
+        Vector3 value = object_->GetAttribute(attrInfo_->name_).GetQuaternion().EulerAngles();
+
+        TBInlineSelect* select = widget_->GetWidgetByIDAndType<TBInlineSelect>(TBID(unsigned(1)));
+        if (select)
+            select->SetValue(value.x_);
+        select = widget_->GetWidgetByIDAndType<TBInlineSelect>(TBID(unsigned(2)));
+        if (select)
+            select->SetValue(value.y_);
+        select = widget_->GetWidgetByIDAndType<TBInlineSelect>(TBID(unsigned(3)));
+        if (select)
+            select->SetValue(value.z_);
+    }
+    else if (attrInfo_->type_ == VAR_STRING)
+    {
+        String value = object_->GetAttribute(attrInfo_->name_).GetString();
+        widget_->SetText(value.CString());
+    }
+    else if (attrInfo_->type_ == VAR_FLOAT)
+    {
+        float value = object_->GetAttribute(attrInfo_->name_).GetFloat();
+        widget_->SetText(ToString("%f", value).CString());
+    }
+    else if (attrInfo_->type_ == VAR_INT)
+    {
+        int value = object_->GetAttribute(attrInfo_->name_).GetInt();
+
+        if (attrInfo_->enumNames_)
+        {
+            widget_->SetText(attrInfo_->enumNames_[value]);
+        }
+        else
+        {
+            widget_->SetText(ToString("%i", value).CString());
+        }
+
+    }
+
+    widgetLocked_ = false;
+
+}
+
+bool InspectorDataBinding::OnEvent(const TBWidgetEvent &ev)
+{
+    if (ev.type == EVENT_TYPE_CLICK)
+    {
+        if (objectLocked_)
+            return false;
+
+        if (widget_ == ev.target || widget_->IsAncestorOf(ev.target))
+        {
+            SetObjectValueFromWidget(ev.target);
+        }
+    }
+
+    if (ev.type == EVENT_TYPE_CHANGED)
+    {
+        if (objectLocked_)
+            return false;
+
+        if (widget_ == ev.target || widget_->IsAncestorOf(ev.target))
+        {
+            SetObjectValueFromWidget(ev.target);
+        }
+    }
+
+    return false;
+}
+
+InspectorDataBinding* InspectorDataBinding::Create(Serializable* object, const AttributeInfo* attrInfo)
+{
+
+    if (attrInfo->mode_ & AM_NOEDIT)
+        return NULL;
+
+    TBWidget* widget = NULL;
+    InspectorDataBinding* binding = NULL;
+
+    TBFontDescription fd;
+    fd.SetID(TBIDC("Vera"));
+    fd.SetSize(11);
+
+    if (attrInfo->type_ == VAR_BOOL)
+    {
+        TBCheckBox* box = new TBCheckBox();
+        widget = box;
+    }
+    else if (attrInfo->type_ == VAR_STRING)
+    {
+        TBEditField* field = new TBEditField();
+        field->SetFontDescription(fd);
+        widget = field;
+    }
+    else if (attrInfo->type_ == VAR_FLOAT)
+    {
+        TBEditField* field = new TBEditField();
+        field->SetFontDescription(fd);
+        widget = field;
+    }
+    else if (attrInfo->type_ == VAR_COLOR)
+    {
+        TBLayout* layout = new TBLayout();
+        widget = layout;
+        layout->SetSpacing(0);
+
+        LayoutParams lp;
+        lp.SetWidth(70);
+
+        for (unsigned i = 0; i < 4; i++)
+        {
+            TBInlineSelect* select = new TBInlineSelect();
+            select->SetID(TBID(i + 1));
+            select->SetFontDescription(fd);
+            select->SetLimits(-10000000, 10000000);
+            select->SetLayoutParams(lp);
+            layout->AddChild(select);
+        }
+
+    }
+    else if (attrInfo->type_ == VAR_INT)
+    {
+        if (attrInfo->enumNames_)
+        {
+            TBButton* field = new TBButton();
+            field->SetFontDescription(fd);
+            widget = field;
+        }
+        else
+        {
+            TBEditField* field = new TBEditField();
+            field->SetFontDescription(fd);
+            widget = field;
+        }
+    }
+    else if (attrInfo->type_ == VAR_VECTOR3 || attrInfo->type_ == VAR_QUATERNION)
+    {
+        TBLayout* layout = new TBLayout();
+        widget = layout;
+        layout->SetSpacing(0);
+
+        LayoutParams lp;
+        lp.SetWidth(90);
+
+        for (unsigned i = 0; i < 3; i++)
+        {
+            TBInlineSelect* select = new TBInlineSelect();
+            select->SetID(TBID(i + 1));
+            select->SetFontDescription(fd);
+            select->SetLimits(-10000000, 10000000);
+            select->SetLayoutParams(lp);
+            layout->AddChild(select);
+        }
+
+    }
+
+    if (widget)
+    {
+        binding = new InspectorDataBinding();
+        binding->object_ = object;
+        binding->widget_ = widget;
+        binding->attrInfo_ = attrInfo;
+    }
+
+    return binding;
+}
+
+}

+ 47 - 0
Source/AtomicEditor/Source/UI/UIInspectorDataBinding.h

@@ -0,0 +1,47 @@
+
+#pragma once
+
+#include <TurboBadger/tb_widgets.h>
+#include <Atomic/Scene/Scene.h>
+
+namespace tb
+{
+    class TBWidget;
+    class TBMenuWindow;
+    class TBInlineSelect;
+}
+
+using namespace Atomic;
+using namespace tb;
+
+namespace AtomicEditor
+{
+
+class InspectorDataBinding
+{
+
+public:
+
+    static InspectorDataBinding* Create(Serializable* object, const AttributeInfo* attrInfo);
+
+    void SetWidgetValueFromObject();
+    void SetObjectValueFromWidget(TBWidget* srcWidget = NULL);
+
+    TBWidget* GetWidget() { return widget_; }
+
+    bool OnEvent(const TBWidgetEvent &ev);
+
+private:
+
+    InspectorDataBinding() : widget_(0), object_(0), attrInfo_(0), objectLocked_(false), widgetLocked_(false) {}
+
+    TBWidget* widget_;
+    Serializable* object_;
+    const AttributeInfo* attrInfo_;
+
+    bool objectLocked_;
+    bool widgetLocked_;
+
+};
+
+}

+ 133 - 50
Source/AtomicEditor/Source/UI/UIInspectorFrame.cpp

@@ -4,10 +4,7 @@
 
 #include "AtomicEditor.h"
 
-#include <TurboBadger/tb_select.h>
 #include <TurboBadger/tb_editfield.h>
-#include <TurboBadger/tb_inline_select.h>
-
 #include <Atomic/Core/Context.h>
 #include <Atomic/IO/Log.h>
 #include <Atomic/IO/FileSystem.h>
@@ -20,9 +17,10 @@
 #include "AEEditor.h"
 #include "AEEvents.h"
 
-#include "UIListView.h"
+#include "UIInspectorDataBinding.h"
 #include "UIInspectorFrame.h"
 
+
 #include "UI/Modal/UIModalOps.h"
 
 using namespace tb;
@@ -32,7 +30,6 @@ namespace AtomicEditor
 
 InspectorFrame::InspectorFrame(Context* context) :
     AEWidget(context)
-  , refreshing_(false)
 {
     TBUI* tbui = GetSubsystem<TBUI>();
     tbui->LoadResourceFile(delegate_, "AtomicEditor/editor/ui/inspectorframe.tb.txt");
@@ -46,24 +43,14 @@ InspectorFrame::InspectorFrame(Context* context) :
 
     SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(InspectorFrame, HandleEditorActiveNodeChange));
 
-    CreateTransformLayout();
-
 }
 
 InspectorFrame::~InspectorFrame()
-{
-    if (transformLayout_->GetParent())
-        transformLayout_->GetParent()->RemoveChild(transformLayout_);
-
-    transformLayout_->Die();
-
-}
-
-void InspectorFrame::Clear()
 {
 
 }
 
+/*
 TBLayout* InspectorFrame::CreateComponentLayout(Component* component)
 {
     TBLayout* componentLayout = new TBLayout(AXIS_Y);
@@ -187,8 +174,14 @@ TBLayout* InspectorFrame::CreateComponentLayout(Component* component)
     return componentLayout;
 }
 
+*/
+
 bool InspectorFrame::OnEvent(const TBWidgetEvent &ev)
 {
+    for (unsigned i = 0; i < dataBindings_.Size(); i++)
+        dataBindings_[i]->OnEvent(ev);
+
+    /*
     if (ev.type == EVENT_TYPE_KEY_DOWN)
     {
         for (unsigned i = 0; i < dataBindings_.Size(); i++)
@@ -305,10 +298,12 @@ bool InspectorFrame::OnEvent(const TBWidgetEvent &ev)
         }
 
     }
+    */
 
     return false;
 }
 
+/*
 void InspectorFrame::CreateTransformLayout()
 {
     // transform
@@ -399,50 +394,24 @@ void InspectorFrame::CreateTransformLayout()
     transformLayout_->AddChild(scaleLayout);
 
 }
-
-void InspectorFrame::RefreshTransform()
-{
-    if (node_.Null())
-        return;
-
-    refreshing_ = true;
-
-    Vector3 pos = node_->GetPosition();
-    Vector3 scale = node_->GetScale();
-    Vector3 rot = node_->GetRotation().EulerAngles();
-
-    posXSelect_->SetValue(pos.x_);
-    posYSelect_->SetValue(pos.y_);
-    posZSelect_->SetValue(pos.z_);
-    rotXSelect_->SetValue(rot.x_);
-    rotYSelect_->SetValue(rot.y_);
-    rotZSelect_->SetValue(rot.z_);
-    scaleXSelect_->SetValue(scale.x_);
-    scaleYSelect_->SetValue(scale.y_);
-    scaleZSelect_->SetValue(scale.x_);
-
-    refreshing_ = false;
-
-}
+*/
 
 void InspectorFrame::InspectNode(Node* node)
 {
     if (node_ == node)
         return;
 
-    refreshing_ = true;
-
     node_ = node;
 
-    if (transformLayout_->GetParent())
-        transformLayout_->GetParent()->RemoveChild(transformLayout_);
-
     inspectorContainer_->DeleteAllChildren();
+
+    for (unsigned i = 0; i < dataBindings_.Size(); i++)
+        delete dataBindings_[i];
+
     dataBindings_.Clear();
 
     if (!node_)
     {
-        refreshing_ = false;
         return;
     }
     else
@@ -451,8 +420,118 @@ void InspectorFrame::InspectNode(Node* node)
         fd.SetID(TBIDC("Vera"));
         fd.SetSize(11);
 
+        LayoutParams nlp;
+        nlp.SetWidth(304);
         TBLayout* nodeLayout = new TBLayout(AXIS_Y);
+
+        nodeLayout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
         nodeLayout->SetLayoutPosition(LAYOUT_POSITION_LEFT_TOP);
+        nodeLayout->SetLayoutParams(nlp);
+
+        TBContainer* nodeContainer = new TBContainer();
+        nodeContainer->SetGravity(WIDGET_GRAVITY_ALL);
+
+        TBLayout* attrsVerticalLayout = new TBLayout(AXIS_Y);
+        attrsVerticalLayout->SetGravity(WIDGET_GRAVITY_ALL);
+        attrsVerticalLayout->SetLayoutPosition(LAYOUT_POSITION_LEFT_TOP);
+        nodeContainer->AddChild(attrsVerticalLayout);
+
+        const Vector<AttributeInfo>* attrs = node->GetAttributes();
+
+        for (unsigned i = 0; i < attrs->Size(); i++)
+        {
+            const AttributeInfo* attr = &attrs->At(i);
+
+            InspectorDataBinding*  binding = InspectorDataBinding::Create(node, attr);
+
+            if (binding)
+            {
+                dataBindings_.Push(binding);
+
+                TBLayout* attrLayout = new TBLayout();
+
+                attrLayout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
+                TBTextField* name = new TBTextField();
+
+                String bname = attr->name_;
+                if (bname == "Is Enabled")
+                    bname = "Enabled";
+
+                name->SetText(bname.CString());
+                name->SetFontDescription(fd);
+
+                attrLayout->AddChild(name);
+                TBWidget* bwidget = binding->GetWidget();
+                attrLayout->AddChild(bwidget);
+
+                attrsVerticalLayout->AddChild(attrLayout);
+            }
+
+        }
+
+        nodeLayout->AddChild(nodeContainer);
+
+        const Vector<SharedPtr<Component> > components = node->GetComponents();
+        for (unsigned i = 0; i < components.Size(); i++)
+        {
+            Component* c = components[i];
+
+            TBContainer* componentContainer = new TBContainer();
+            componentContainer->SetGravity(WIDGET_GRAVITY_ALL);
+
+            TBLayout* attrsVerticalLayout = new TBLayout(AXIS_Y);
+            attrsVerticalLayout->SetGravity(WIDGET_GRAVITY_ALL);
+            attrsVerticalLayout->SetLayoutPosition(LAYOUT_POSITION_LEFT_TOP);
+
+            TBTextField* cnameField = new TBTextField();
+            cnameField->SetText(c->GetTypeName().CString());
+            //cnameField->SetFontDescription(fd);
+            attrsVerticalLayout->AddChild(cnameField);
+
+
+            componentContainer->AddChild(attrsVerticalLayout);
+
+            const Vector<AttributeInfo>* attrs = c->GetAttributes();
+
+            if (attrs)
+                for (unsigned i = 0; i < attrs->Size(); i++)
+                {
+                    const AttributeInfo* attr = &attrs->At(i);
+
+                    InspectorDataBinding*  binding = InspectorDataBinding::Create(c, attr);
+
+                    if (binding)
+                    {
+                        dataBindings_.Push(binding);
+
+                        TBLayout* attrLayout = new TBLayout();
+
+                        attrLayout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
+                        TBTextField* name = new TBTextField();
+
+                        String bname = attr->name_;
+                        if (bname == "Is Enabled")
+                            bname = "Enabled";
+
+                        name->SetText(bname.CString());
+                        name->SetFontDescription(fd);
+
+                        attrLayout->AddChild(name);
+                        TBWidget* bwidget = binding->GetWidget();
+                        attrLayout->AddChild(bwidget);
+
+                        attrsVerticalLayout->AddChild(attrLayout);
+                    }
+
+                }
+
+            nodeLayout->AddChild(componentContainer);
+
+        }
+
+
+        /*
+
 
         // enabled and name
         TBLayout* nameLayout = new TBLayout();
@@ -488,12 +567,16 @@ void InspectorFrame::InspectNode(Node* node)
             nodeLayout->AddChild(sep);
         }
 
-        inspectorContainer_->AddChild(nodeLayout);
+
 
         RefreshTransform();
-    }
+        */
+
+        inspectorContainer_->AddChild(nodeLayout);
 
-    refreshing_ = false;
+        for (unsigned i = 0; i < dataBindings_.Size(); i++)
+            dataBindings_[i]->SetWidgetValueFromObject();
+    }
 
 }
 

+ 2 - 29
Source/AtomicEditor/Source/UI/UIInspectorFrame.h

@@ -5,7 +5,6 @@
 #pragma once
 
 #include "AEWidget.h"
-#include "UIMenubar.h"
 
 #include <Atomic/Scene/Scene.h>
 
@@ -23,6 +22,7 @@ namespace AtomicEditor
 
 class ListView;
 class ListViewItem;
+class InspectorDataBinding;
 
 class InspectorFrame : public AEWidget
 {
@@ -39,40 +39,13 @@ public:
 
 private:
 
-    struct DataBinding
-    {
-        TBWidget* widget;
-        Component* component;
-        unsigned attrIndex;
-    };
-
-    void Clear();
-
     void InspectNode(Node* node);
 
-    void CreateTransformLayout();
-
-    TBLayout* CreateComponentLayout(Component *component);
-    void RefreshTransform();
-
     void HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData);
 
     TBLayout* inspectorContainer_;
 
-    TBLayout* transformLayout_;
-    TBInlineSelect* posXSelect_;
-    TBInlineSelect* posYSelect_;
-    TBInlineSelect* posZSelect_;
-    TBInlineSelect* rotXSelect_;
-    TBInlineSelect* rotYSelect_;
-    TBInlineSelect* rotZSelect_;
-    TBInlineSelect* scaleXSelect_;
-    TBInlineSelect* scaleYSelect_;
-    TBInlineSelect* scaleZSelect_;
-
-    Vector<DataBinding> dataBindings_;
-
-    bool refreshing_;
+    Vector<InspectorDataBinding*> dataBindings_;
 
     SharedPtr<Node> node_;