Browse Source

Renamed the UserData VariantMap in UIElement to Vars, to distinguish from low-level userdata that is typically a pointer. Added same VariantMap to Node.

Lasse Öörni 14 years ago
parent
commit
fe56f0118b

+ 2 - 1
Engine/Engine/APITemplates.h

@@ -499,6 +499,7 @@ template <class T> void RegisterNode(asIScriptEngine* engine, const char* classN
     engine->RegisterObjectMethod(className, "const String& get_name() const", asMETHOD(T, GetName), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "const String& get_name() const", asMETHOD(T, GetName), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_parent(Node@+)", asMETHOD(T, SetParent), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void set_parent(Node@+)", asMETHOD(T, SetParent), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Node@+ get_parent() const", asMETHOD(T, GetParent), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Node@+ get_parent() const", asMETHOD(T, GetParent), asCALL_THISCALL);
+    engine->RegisterObjectProperty(className, "VariantMap vars", offsetof(T, vars_));
 }
 }
 
 
 static bool ResourceLoad(File* file, XMLFile* ptr)
 static bool ResourceLoad(File* file, XMLFile* ptr)
@@ -724,7 +725,7 @@ template <class T> void RegisterUIElement(asIScriptEngine* engine, const char* c
     engine->RegisterObjectMethod(className, "IntVector2 get_screenPosition()", asMETHOD(T, GetScreenPosition), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "IntVector2 get_screenPosition()", asMETHOD(T, GetScreenPosition), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "float get_derivedOpacity()", asMETHOD(T, GetDerivedOpacity), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "float get_derivedOpacity()", asMETHOD(T, GetDerivedOpacity), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "IntRect get_combinedScreenRect()", asMETHOD(T, GetCombinedScreenRect), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "IntRect get_combinedScreenRect()", asMETHOD(T, GetCombinedScreenRect), asCALL_THISCALL);
-    engine->RegisterObjectProperty(className, "VariantMap userData", offsetof(T, userData_));
+    engine->RegisterObjectProperty(className, "VariantMap vars", offsetof(T, vars_));
 }
 }
 
 
 /// Template function for registering a class derived from BorderImage
 /// Template function for registering a class derived from BorderImage

+ 1 - 1
Engine/Input/Input.cpp

@@ -435,7 +435,7 @@ void Input::HandleWindowMessage(StringHash eventType, VariantMap& eventData)
         break;
         break;
         
         
     case WM_NCLBUTTONUP:
     case WM_NCLBUTTONUP:
-        case WM_LBUTTONUP:
+    case WM_LBUTTONUP:
         SetMouseButton(MOUSEB_LEFT, false);
         SetMouseButton(MOUSEB_LEFT, false);
         eventData[P_HANDLED] = true;
         eventData[P_HANDLED] = true;
         break;
         break;

+ 1 - 0
Engine/Scene/Node.cpp

@@ -69,6 +69,7 @@ void Node::RegisterObject(Context* context)
     ATTRIBUTE(Node, VAR_VECTOR3, "Position", position_, Vector3::ZERO);
     ATTRIBUTE(Node, VAR_VECTOR3, "Position", position_, Vector3::ZERO);
     ATTRIBUTE(Node, VAR_QUATERNION, "Rotation", rotation_, Quaternion::IDENTITY);
     ATTRIBUTE(Node, VAR_QUATERNION, "Rotation", rotation_, Quaternion::IDENTITY);
     ATTRIBUTE(Node, VAR_VECTOR3, "Scale", scale_, Vector3::UNITY);
     ATTRIBUTE(Node, VAR_VECTOR3, "Scale", scale_, Vector3::UNITY);
+    ATTRIBUTE_MODE(Node, VAR_VARIANTMAP, "Variables", vars_, VariantMap(), AM_SERIALIZATION);
 }
 }
 
 
 void Node::OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData)
 void Node::OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData)

+ 5 - 0
Engine/Scene/Node.h

@@ -222,6 +222,8 @@ public:
     bool HasComponent(ShortStringHash type) const;
     bool HasComponent(ShortStringHash type) const;
     /// Return listener components
     /// Return listener components
     const std::vector<WeakPtr<Component> > GetListeners() const { return listeners_; }
     const std::vector<WeakPtr<Component> > GetListeners() const { return listeners_; }
+    /// Return user variables
+    VariantMap& GetVars() { return vars_; }
     /// Template version of returning child nodes with a specific component
     /// Template version of returning child nodes with a specific component
     template <class T> void GetChildrenWithComponent(std::vector<Node*>& dest, bool recursive = false) const;
     template <class T> void GetChildrenWithComponent(std::vector<Node*>& dest, bool recursive = false) const;
     /// Template version of returning a component by type
     /// Template version of returning a component by type
@@ -231,6 +233,9 @@ public:
     /// Template version of checking whether has a specific component
     /// Template version of checking whether has a specific component
     template <class T> bool HasComponent() const;
     template <class T> bool HasComponent() const;
     
     
+    /// User variables
+    VariantMap vars_;
+    
 protected:
 protected:
     /// Create a component with specific ID. Used internally
     /// Create a component with specific ID. Used internally
     Component* CreateComponent(ShortStringHash type, unsigned id);
     Component* CreateComponent(ShortStringHash type, unsigned id);

+ 2 - 2
Engine/UI/ListView.cpp

@@ -45,7 +45,7 @@ int GetItemIndent(UIElement* item)
 {
 {
     if (!item)
     if (!item)
         return 0;
         return 0;
-    return item->GetUserData()[indentHash].GetInt();
+    return item->vars_[indentHash].GetInt();
 }
 }
 
 
 OBJECTTYPESTATIC(ListView);
 OBJECTTYPESTATIC(ListView);
@@ -92,7 +92,7 @@ void ListView::SetStyle(const XMLElement& element)
                 UIElement* item = root->GetChild(itemElem.GetString("name"), true);
                 UIElement* item = root->GetChild(itemElem.GetString("name"), true);
                 AddItem(item);
                 AddItem(item);
                 if (itemElem.HasAttribute("indent"))
                 if (itemElem.HasAttribute("indent"))
-                    item->GetUserData()[indentHash] = itemElem.GetInt("indent");
+                    item->vars_[indentHash] = itemElem.GetInt("indent");
                 itemElem = itemElem.GetNextElement("listitem");
                 itemElem = itemElem.GetNextElement("listitem");
             }
             }
         }
         }

+ 4 - 4
Engine/UI/Menu.cpp

@@ -56,7 +56,7 @@ Menu::~Menu()
         UIElement* parent = popup_->GetParent();
         UIElement* parent = popup_->GetParent();
         if (parent)
         if (parent)
         {
         {
-            popup_->GetUserData()[originHash].Clear();
+            popup_->vars_[originHash].Clear();
             parent->RemoveChild(popup_);
             parent->RemoveChild(popup_);
         }
         }
     }
     }
@@ -132,7 +132,7 @@ void Menu::ShowPopup(bool enable)
         
         
         popup_->SetPosition(GetScreenPosition() + popupOffset_);
         popup_->SetPosition(GetScreenPosition() + popupOffset_);
         popup_->SetVisible(true);
         popup_->SetVisible(true);
-        popup_->GetUserData()[originHash] = (void*)this;
+        popup_->vars_[originHash] = (void*)this;
         root->AddChild(popup_);
         root->AddChild(popup_);
         
         
         // Set fixed high priority
         // Set fixed high priority
@@ -142,7 +142,7 @@ void Menu::ShowPopup(bool enable)
     }
     }
     else
     else
     {
     {
-        popup_->GetUserData()[originHash].Clear();
+        popup_->vars_[originHash].Clear();
         root->RemoveChild(popup_);
         root->RemoveChild(popup_);
     }
     }
     
     
@@ -217,7 +217,7 @@ void Menu::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
         if ((element == this) || (element == popup_))
         if ((element == this) || (element == popup_))
             return;
             return;
         if (element->GetParent() == root)
         if (element->GetParent() == root)
-            element = static_cast<UIElement*>(element->GetUserData()[originHash].GetPtr());
+            element = static_cast<UIElement*>(element->vars_[originHash].GetPtr());
         else
         else
             element = element->GetParent();
             element = element->GetParent();
     }
     }

+ 2 - 7
Engine/UI/UIElement.cpp

@@ -228,8 +228,8 @@ void UIElement::SetStyle(const XMLElement& element)
         else
         else
             UpdateLayout();
             UpdateLayout();
     }
     }
-    if (element.HasChildElement("userdata"))
-        SetUserData(element.GetChildElement("userdat").GetVariantMap());
+    if (element.HasChildElement("vars"))
+        vars_ = element.GetChildElement("vars").GetVariantMap();
 }
 }
 
 
 void UIElement::Update(float timeStep)
 void UIElement::Update(float timeStep)
@@ -617,11 +617,6 @@ void UIElement::SetLayoutBorder(const IntRect& border)
     UpdateLayout();
     UpdateLayout();
 }
 }
 
 
-void UIElement::SetUserData(const VariantMap& userData)
-{
-    userData_ = userData;
-}
-
 void UIElement::UpdateLayout()
 void UIElement::UpdateLayout()
 {
 {
     if ((layoutMode_ == LM_FREE) || (layoutNestingLevel_))
     if ((layoutMode_ == LM_FREE) || (layoutNestingLevel_))

+ 5 - 8
Engine/UI/UIElement.h

@@ -103,8 +103,6 @@ class UIElement : public Object
 {
 {
     OBJECT(Element);
     OBJECT(Element);
     
     
-    template <class T> friend void RegisterUIElement(asIScriptEngine* engine, const char* className);
-    
 public:
 public:
     /// Construct
     /// Construct
     UIElement(Context* context);
     UIElement(Context* context);
@@ -231,8 +229,6 @@ public:
     void SetLayoutSpacing(int spacing);
     void SetLayoutSpacing(int spacing);
     /// Set layout border
     /// Set layout border
     void SetLayoutBorder(const IntRect& border);
     void SetLayoutBorder(const IntRect& border);
-    /// Set userdata
-    void SetUserData(const VariantMap& userData);
     /// Manually update layout. Should not be necessary in most cases, but is provided for completeness
     /// Manually update layout. Should not be necessary in most cases, but is provided for completeness
     void UpdateLayout();
     void UpdateLayout();
     /// Disable automatic layout update. Should only be used if there are performance problems
     /// Disable automatic layout update. Should only be used if there are performance problems
@@ -322,8 +318,6 @@ public:
     int GetLayoutSpacing() const { return layoutSpacing_; }
     int GetLayoutSpacing() const { return layoutSpacing_; }
     /// Return layout border
     /// Return layout border
     const IntRect& GetLayoutBorder() const { return layoutBorder_; }
     const IntRect& GetLayoutBorder() const { return layoutBorder_; }
-    /// Return userdata
-    VariantMap& GetUserData() { return userData_; }
     /// Return number of child elements
     /// Return number of child elements
     unsigned GetNumChildren(bool recursive = false) const;
     unsigned GetNumChildren(bool recursive = false) const;
     /// Return child element by index
     /// Return child element by index
@@ -338,6 +332,8 @@ public:
     UIElement* GetRootElement() const;
     UIElement* GetRootElement() const;
     /// Return precalculated 32-bit color. Only valid when no gradient
     /// Return precalculated 32-bit color. Only valid when no gradient
     unsigned GetUIntColor();
     unsigned GetUIntColor();
+    /// Return user variables
+    VariantMap& GetVars() { return vars_; }
     
     
     /// Convert screen coordinates to element coordinates
     /// Convert screen coordinates to element coordinates
     IntVector2 ScreenToElement(const IntVector2& screenPosition);
     IntVector2 ScreenToElement(const IntVector2& screenPosition);
@@ -360,6 +356,9 @@ public:
     void GetBatchesWithOffset(IntVector2& offset, std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, IntRect
     void GetBatchesWithOffset(IntVector2& offset, std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, IntRect
         currentScissor);
         currentScissor);
     
     
+    /// User variables
+    VariantMap vars_;
+    
 protected:
 protected:
     /// Mark screen position as needing an update
     /// Mark screen position as needing an update
     void MarkDirty();
     void MarkDirty();
@@ -396,8 +395,6 @@ protected:
     FocusMode focusMode_;
     FocusMode focusMode_;
     /// Drag and drop flags
     /// Drag and drop flags
     unsigned dragDropMode_;
     unsigned dragDropMode_;
-    /// Userdata
-    VariantMap userData_;
     /// Layout mode
     /// Layout mode
     LayoutMode layoutMode_;
     LayoutMode layoutMode_;
     /// Layout spacing
     /// Layout spacing