浏览代码

Fixed read only attributes in serialization.
Added flags to UIElement for compatibility.

ninjastone 9 年之前
父节点
当前提交
9c4b0b169d
共有 3 个文件被更改,包括 23 次插入22 次删除
  1. 3 3
      Source/Urho3D/Scene/Serializable.cpp
  2. 10 13
      Source/Urho3D/UI/UIElement.cpp
  3. 10 6
      Source/Urho3D/UI/UIElement.h

+ 3 - 3
Source/Urho3D/Scene/Serializable.cpp

@@ -316,7 +316,7 @@ bool Serializable::Save(Serializer& dest) const
     for (unsigned i = 0; i < attributes->Size(); ++i)
     {
         const AttributeInfo& attr = attributes->At(i);
-        if (!(attr.mode_ & AM_FILE) || (attr.mode_ & AM_FILEREADONLY))
+        if (!(attr.mode_ & AM_FILE) || (attr.mode_ & AM_FILEREADONLY) == AM_FILEREADONLY)
             continue;
 
         OnGetAttribute(attr, value);
@@ -521,7 +521,7 @@ bool Serializable::SaveXML(XMLElement& dest) const
     for (unsigned i = 0; i < attributes->Size(); ++i)
     {
         const AttributeInfo& attr = attributes->At(i);
-        if (!(attr.mode_ & AM_FILE) || attr.mode_ & AM_FILEREADONLY)
+        if (!(attr.mode_ & AM_FILE) || (attr.mode_ & AM_FILEREADONLY) == AM_FILEREADONLY)
             continue;
 
         OnGetAttribute(attr, value);
@@ -558,7 +558,7 @@ bool Serializable::SaveJSON(JSONValue& dest) const
     for (unsigned i = 0; i < attributes->Size(); ++i)
     {
         const AttributeInfo& attr = attributes->At(i);
-        if (!(attr.mode_ & AM_FILE) || attr.mode_ & AM_FILEREADONLY)
+        if (!(attr.mode_ & AM_FILE) || (attr.mode_ & AM_FILEREADONLY) == AM_FILEREADONLY)
             continue;
 
         OnGetAttribute(attr, value);

+ 10 - 13
Source/Urho3D/UI/UIElement.cpp

@@ -136,8 +136,10 @@ UIElement::UIElement(Context* context) :
     maxOffset_(IntVector2::ZERO),
     anchorMin_(0, 0),
     anchorMax_(0, 0),
-    pivot_(0, 0),
-    recalcMaxOffset_(false)
+    recalcMaxOffset_(false),
+    anchorEnable_(false),
+    pivot_(FLT_MAX, FLT_MAX),
+    pivotSet_(false)
 {
     SetEnabled(false);
 }
@@ -169,7 +171,7 @@ void UIElement::RegisterObject(Context* context)
         VA_TOP, AM_FILEREADONLY);
     URHO3D_ACCESSOR_ATTRIBUTE("Min Anchor", GetMinAnchor, SetMinAnchor, Vector2, Vector2::ZERO, AM_FILE);
     URHO3D_ACCESSOR_ATTRIBUTE("Max Anchor", GetMaxAnchor, SetMaxAnchor, Vector2, Vector2::ZERO, AM_FILE);
-    URHO3D_ACCESSOR_ATTRIBUTE("Pivot", GetPivot, SetPivot, Vector2, Vector2::ZERO, AM_FILE);
+    URHO3D_ACCESSOR_ATTRIBUTE("Pivot", GetPivot, SetPivot, Vector2, Vector2(FLT_MAX, FLT_MAX), AM_FILE);
     URHO3D_ACCESSOR_ATTRIBUTE("Clip Border", GetClipBorder, SetClipBorder, IntRect, IntRect::ZERO, AM_FILE);
     URHO3D_ACCESSOR_ATTRIBUTE("Priority", GetPriority, SetPriority, int, 0, AM_FILE);
     URHO3D_ACCESSOR_ATTRIBUTE("Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_FILE);
@@ -622,6 +624,7 @@ void UIElement::SetMaxOffset(const IntVector2& offset)
     if (maxOffset_ != offset)
     {
         maxOffset_ = offset;
+        anchorEnable_ = true;
         AdjustAnchoredSize();
         MarkDirty();
     }
@@ -631,17 +634,10 @@ void UIElement::AdjustAnchoredSize()
 {
     IntVector2 newSize = size_;
 
-    if (parent_)
+    if (parent_ && anchorEnable_)
     {
-        if (anchorMin_.x_ < anchorMax_.x_)
-        {
-            newSize.x_ = (int)(parent_->size_.x_ * (anchorMax_.x_ - anchorMin_.x_)) + maxOffset_.x_ - position_.x_;
-        }
-
-        if (anchorMin_.y_ < anchorMax_.y_)
-        {
-            newSize.y_ = (int)(parent_->size_.y_ * (anchorMax_.y_ - anchorMin_.y_)) + maxOffset_.y_ - position_.y_;
-        }
+        newSize.x_ = (int)(parent_->size_.x_ * Clamp(anchorMax_.x_ - anchorMin_.x_, 0.0f, 1.0f)) + maxOffset_.x_ - position_.x_;
+        newSize.y_ = (int)(parent_->size_.y_ * Clamp(anchorMax_.y_ - anchorMin_.y_, 0.0f, 1.0f)) + maxOffset_.y_ - position_.y_;
 
         if (size_ != newSize)
         {
@@ -893,6 +889,7 @@ void UIElement::SetPivot(const Vector2& pivot)
 {
     if (pivot != pivot_)
     {
+        pivotSet_ = true;
         pivot_ = pivot;
         MarkDirty();
     }

+ 10 - 6
Source/Urho3D/UI/UIElement.h

@@ -440,15 +440,15 @@ public:
     /// Return horizontal alignment.
     HorizontalAlignment GetHorizontalAlignment() const 
     {
-        if (anchorMin_.x_ == 0.0f && anchorMax_.x_ == 0.0f)
+        if (anchorMin_.x_ == 0.0f && anchorMax_.x_ == 0.0f && (!pivotSet_ || pivot_.x_ == 0.0f))
         {
             return HA_LEFT;
         }
-        else if (anchorMin_.x_ == 0.5f && anchorMax_.x_ == 0.5f)
+        else if (anchorMin_.x_ == 0.5f && anchorMax_.x_ == 0.5f && (!pivotSet_ || pivot_.x_ == 0.5f))
         {
             return HA_CENTER;
         }
-        else if (anchorMin_.x_ == 1.0f && anchorMax_.x_ == 1.0f)
+        else if (anchorMin_.x_ == 1.0f && anchorMax_.x_ == 1.0f && (!pivotSet_ || pivot_.x_ == 1.0f))
         {
             return HA_RIGHT;
         }
@@ -458,15 +458,15 @@ public:
     /// Return vertical alignment.
     VerticalAlignment GetVerticalAlignment() const 
     {
-        if (anchorMin_.y_ == 0.0f && anchorMax_.y_ == 0.0f)
+        if (anchorMin_.y_ == 0.0f && anchorMax_.y_ == 0.0f && (!pivotSet_ || pivot_.y_ == 0.0f))
         {
             return VA_TOP;
         }
-        else if (anchorMin_.y_ == 0.5f && anchorMax_.y_ == 0.5f)
+        else if (anchorMin_.y_ == 0.5f && anchorMax_.y_ == 0.5f && (!pivotSet_ || pivot_.y_ == 0.5f))
         {
             return VA_CENTER;
         }
-        else if (anchorMin_.y_ == 1.0f && anchorMax_.y_ == 1.0f)
+        else if (anchorMin_.y_ == 1.0f && anchorMax_.y_ == 1.0f && (!pivotSet_ || pivot_.y_ == 1.0f))
         {
             return VA_BOTTOM;
         }
@@ -787,6 +787,10 @@ private:
     IntVector2 layoutMinSize_;
     /// Relative size.
     IntVector2 maxOffset_;
+    /// Use max offset instead of size.
+    bool anchorEnable_ : 1;
+    /// Has pivot changed manually.
+    bool pivotSet_ : 1;
     /// Anchor Minimum Position
     Vector2 anchorMin_;
     /// Anchor Maximum Position