Browse Source

Force elements in layout to left/top alignment, warn if has different alignment. Closes #34. Implement editor accelerator key texts a bit differently as the right alignment in a layout -hack can no longer be relied upon.

Lasse Öörni 12 năm trước cách đây
mục cha
commit
f73ee0c730

+ 6 - 6
Bin/Data/Scripts/Editor/EditorUI.as

@@ -629,10 +629,11 @@ Menu@ CreateMenuItem(const String&in title, MENU_CALLBACK@ callback = null, int
 
     if (accelKey != 0)
     {
-        int minWidth = menuText.minWidth;
-        menuText.layoutMode = LM_HORIZONTAL;
-        menuText.AddChild(CreateAccelKeyText(accelKey, accelQual));
-        menuText.minWidth = minWidth;
+        UIElement@ spacer = UIElement();
+        spacer.minWidth = menuText.indentSpacing;
+        spacer.height = menuText.height;
+        menu.AddChild(spacer);
+        menu.AddChild(CreateAccelKeyText(accelKey, accelQual));
     }
 
     return menu;
@@ -722,8 +723,7 @@ Text@ CreateAccelKeyText(int accelKey, int accelQual)
     Text@ accelKeyText = Text();
     accelKeyText.defaultStyle = uiStyle;
     accelKeyText.style = "EditorMenuText";
-    accelKeyText.horizontalAlignment = HA_RIGHT;
-    accelKeyText.indent = 1;
+    accelKeyText.textAlignment = HA_RIGHT;
 
     String text;
     if (accelKey == KEY_DELETE)

+ 38 - 13
Source/Engine/UI/UIElement.cpp

@@ -682,21 +682,38 @@ void UIElement::SetFixedHeight(int height)
 
 void UIElement::SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign)
 {
-    horizontalAlignment_ = hAlign;
-    verticalAlignment_ = vAlign;
-    MarkDirty();
+    SetHorizontalAlignment(hAlign);
+    SetVerticalAlignment(vAlign);
 }
 
 void UIElement::SetHorizontalAlignment(HorizontalAlignment align)
 {
-    horizontalAlignment_ = align;
-    MarkDirty();
+    if (align != HA_LEFT && parent_ && parent_->GetLayoutMode() == LM_HORIZONTAL)
+    {
+        LOGWARNING("Forcing left alignment because parent element has horizontal layout");
+        align = HA_LEFT;
+    }
+
+    if (horizontalAlignment_ != align)
+    {
+        horizontalAlignment_ = align;
+        MarkDirty();
+    }
 }
 
 void UIElement::SetVerticalAlignment(VerticalAlignment align)
 {
-    verticalAlignment_ = align;
-    MarkDirty();
+    if (align != VA_TOP && parent_ && parent_->GetLayoutMode() == LM_VERTICAL)
+    {
+        LOGWARNING("Forcing top alignment because parent element has vertical layout");
+        align = VA_TOP;
+    }
+
+    if (verticalAlignment_ != align)
+    {
+        verticalAlignment_ = align;
+        MarkDirty();
+    }
 }
 
 void UIElement::SetClipBorder(const IntRect& rect)
@@ -884,12 +901,14 @@ void UIElement::SetLayout(LayoutMode mode, int spacing, const IntRect& border)
     layoutMode_ = mode;
     layoutSpacing_ = Max(spacing, 0);
     layoutBorder_ = IntRect(Max(border.left_, 0), Max(border.top_, 0), Max(border.right_, 0), Max(border.bottom_, 0));
+    VerifyChildAlignment();
     UpdateLayout();
 }
 
 void UIElement::SetLayoutMode(LayoutMode mode)
 {
     layoutMode_ = mode;
+    VerifyChildAlignment();
     UpdateLayout();
 }
 
@@ -973,10 +992,7 @@ void UIElement::UpdateLayout()
         {
             if (!children_[i]->IsVisible())
                 continue;
-            HorizontalAlignment horizontalAlignment = children_[i]->horizontalAlignment_;
-            children_[i]->horizontalAlignment_ = HA_LEFT;
             children_[i]->SetPosition(positions[j], GetLayoutChildPosition(children_[i]).y_);
-            children_[i]->horizontalAlignment_ = horizontalAlignment;
             children_[i]->SetSize(sizes[j], height - layoutBorder_.top_ - layoutBorder_.bottom_);
             ++j;
         }
@@ -1015,10 +1031,7 @@ void UIElement::UpdateLayout()
         {
             if (!children_[i]->IsVisible())
                 continue;
-            VerticalAlignment verticalAlignment = children_[i]->verticalAlignment_;
-            children_[i]->verticalAlignment_ = VA_TOP;
             children_[i]->SetPosition(GetLayoutChildPosition(children_[i]).x_ + baseIndentWidth, positions[j]);
-            children_[i]->verticalAlignment_ = verticalAlignment;
             children_[i]->SetSize(width - layoutBorder_.left_ - layoutBorder_.right_, sizes[j]);
             ++j;
         }
@@ -1150,6 +1163,7 @@ void UIElement::InsertChild(unsigned index, UIElement* element)
     if (!previousStyleFile && !element->appliedStyle_.Empty() && GetDefaultStyle())
         element->SetStyle(element->appliedStyle_);
 
+    VerifyChildAlignment();
     UpdateLayout();
 
     // Send change event
@@ -1835,4 +1849,15 @@ void UIElement::Detach()
     MarkDirty();
 }
 
+void UIElement::VerifyChildAlignment()
+{
+    for (Vector<SharedPtr<UIElement> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
+    {
+        // Reapply child alignments. If they are illegal compared to layout, they will be set left/top as neded
+        (*i)->SetHorizontalAlignment((*i)->GetHorizontalAlignment());
+        (*i)->SetVerticalAlignment((*i)->GetVerticalAlignment());
+    }
 }
+
+}
+

+ 2 - 0
Source/Engine/UI/UIElement.h

@@ -553,6 +553,8 @@ private:
     IntVector2 GetLayoutChildPosition(UIElement* child);
     /// Detach from parent.
     void Detach();
+    /// Verify that child elements have proper alignment for layout mode.
+    void VerifyChildAlignment();
 
     /// Size.
     IntVector2 size_;