Browse Source

When attaching a UIElement as child, apply potentially changed style recursively. Remove default size from Button's style (generally style shouldn't change element size.) Closes #1103.

Lasse Öörni 10 years ago
parent
commit
b593380632
3 changed files with 26 additions and 12 deletions
  1. 16 5
      Source/Urho3D/UI/UIElement.cpp
  2. 4 0
      Source/Urho3D/UI/UIElement.h
  3. 6 7
      bin/Data/UI/DefaultStyle.xml

+ 16 - 5
Source/Urho3D/UI/UIElement.cpp

@@ -969,6 +969,9 @@ bool UIElement::SetStyle(const String& styleName, XMLFile* file)
         defaultStyle_ = file;
     }
 
+    // Remember the effectively applied style file, either custom or default
+    appliedStyleFile_ = file;
+
     styleXPathQuery_.SetVariable("typeName", actualStyleName);
     XMLElement styleElem = file->GetRoot().SelectSinglePrepared(styleXPathQuery_);
     return styleElem && SetStyle(styleElem);
@@ -1253,8 +1256,6 @@ void UIElement::InsertChild(unsigned index, UIElement* element)
     else
         children_.Insert(children_.Begin() + index, SharedPtr<UIElement>(element));
 
-    XMLFile* previousStyleFile = element->GetDefaultStyle();
-
     element->Remove();
 
     if (sortChildren_)
@@ -1263,9 +1264,8 @@ void UIElement::InsertChild(unsigned index, UIElement* element)
     element->parent_ = this;
     element->MarkDirty();
 
-    // If child element did not already have a style file, but has specified a style name, apply it now
-    if (!previousStyleFile && !element->appliedStyle_.Empty() && GetDefaultStyle())
-        element->SetStyle(element->appliedStyle_);
+    // Apply style now if child element (and its children) has it defined
+    ApplyStyleRecursive(element);
 
     VerifyChildAlignment();
     UpdateLayout();
@@ -1874,6 +1874,17 @@ void UIElement::GetChildrenRecursive(PODVector<UIElement*>& dest) const
     }
 }
 
+void UIElement::ApplyStyleRecursive(UIElement* element)
+{
+    // If child element style file changes as result of being (re)parented and it has a defined style, apply it now
+    if (!element->appliedStyle_.Empty() && element->appliedStyleFile_.Get() != element->GetDefaultStyle())
+    {
+        element->SetStyle(element->appliedStyle_);
+        for (Vector<SharedPtr<UIElement> >::ConstIterator i = element->children_.Begin(); i != element->children_.End(); ++i)
+            element->ApplyStyleRecursive(*i);
+    }
+}
+
 int UIElement::CalculateLayoutParentSize(const PODVector<int>& sizes, int begin, int end, int spacing)
 {
     int width = begin + end;

+ 4 - 0
Source/Urho3D/UI/UIElement.h

@@ -653,6 +653,8 @@ protected:
 private:
     /// Return child elements recursively.
     void GetChildrenRecursive(PODVector<UIElement*>& dest) const;
+    /// Recursively apply style to a child element hierarchy when adding to an element.
+    void ApplyStyleRecursive(UIElement* element);
     /// Calculate layout width for resizing the parent element.
     int CalculateLayoutParentSize(const PODVector<int>& sizes, int begin, int end, int spacing);
     /// Calculate child widths/positions in the layout.
@@ -698,6 +700,8 @@ private:
     bool colorGradient_;
     /// Default style file.
     SharedPtr<XMLFile> defaultStyle_;
+    /// Last applied style file.
+    WeakPtr<XMLFile> appliedStyleFile_;
     /// Traversal mode for rendering.
     TraversalMode traversalMode_;
     /// Flag whether node should send child added / removed events by itself.

+ 6 - 7
bin/Data/UI/DefaultStyle.xml

@@ -3,7 +3,6 @@
         <attribute name="Texture" value="Texture2D;Textures/UI.png" />
     </element>
     <element type="Button" style="BorderImage">
-        <attribute name="Size" value="16 16" />
         <attribute name="Image Rect" value="16 0 32 16" />
         <attribute name="Border" value="4 4 4 4" />
         <attribute name="Pressed Image Offset" value="16 0" />
@@ -34,14 +33,14 @@
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="0 0 12 24" />
                 <variant type="IntVector2" value="0 0" />
-            </variant>           
+            </variant>
             <variant type="VariantVector" >
                 <variant type="String" value="ResizeVertical" />
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="0 64 20 84" />
                 <variant type="IntVector2" value="9 9" />
             </variant>
-            <variant type="VariantVector" >     
+            <variant type="VariantVector" >
                 <variant type="String" value="ResizeDiagonalTopRight" />
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="20 64 40 84" />
@@ -53,19 +52,19 @@
                 <variant type="IntRect" value="40 64 60 84" />
                 <variant type="IntVector2" value="9 9" />
             </variant>
-            <variant type="VariantVector" >             
+            <variant type="VariantVector" >
                 <variant type="String" value="ResizeDiagonalTopLeft" />
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="60 64 80 84" />
                 <variant type="IntVector2" value="9 9" />
             </variant>
-            <variant type="VariantVector" >        
+            <variant type="VariantVector" >
                 <variant type="String" value="RejectDrop" />
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="80 64 100 84" />
                 <variant type="IntVector2" value="9 9" />
             </variant>
-            <variant type="VariantVector" >         
+            <variant type="VariantVector" >
                 <variant type="String" value="AcceptDrop" />
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="100 64 128 90" />
@@ -76,7 +75,7 @@
                 <variant type="ResourceRef" value="Image;Textures/UI.png" />
                 <variant type="IntRect" value="128 64 148 85" />
                 <variant type="IntVector2" value="9 9" />
-            </variant>              
+            </variant>
         </attribute>
     </element>
     <element type="DropDownList" style="BorderImage">