I want to dock a menubar-like control to the top of the window.
For that I gave it a style which enables anchors and set the max anchor to (1|0), like this:
menubar.xml
<element type="BorderImage" style="MenuBar">
</element>
style.xml
<element type="BorderImage">
<attribute name="Blend Mode" value="Alpha" />
<attribute name="Border" value="8 8 8 8" />
<attribute name="Texture" value="Texture2D;textures/ui.png" />
</element>
<element type="MenuBar" style="BorderImage">
<attribute name="Image Rect" value="0 40 40 80" />
<attribute name="Min Size" value="40 40" />
<attribute name="Enable Anchor" value="true" />
<attribute name="Max Anchor" value="1 0" />
</element>
I then add the element to the UI.Root as follows:
// _defaultStyle has been set as DefaultStyle of UI.Root and represents "style.xml"
XmlFile layoutFile = ResourceCache.GetXmlFile("menubar.xml");
UIElement menuBar = UI.LoadLayout(layoutFile, _defaultStyle);
UI.Root.AddChild(menuBar);
However, the control is not visible when running this.
I noticed that when I remove Enable Anchor
and Max Anchor
in the menu bar style, and set it manually after adding the UIElement to the UI.Root, it works as expected and how I saw it in the Urho3D editor:
<element type="MenuBar" style="BorderImage">
<attribute name="Image Rect" value="0 40 40 80" />
<attribute name="Min Size" value="40 40" />
</element>
XmlFile layoutFile = ResourceCache.GetXmlFile("menubar.xml");
UIElement menuBar = UI.LoadLayout(layoutFile, _defaultStyle);
UI.Root.AddChild(menuBar);
// Bleh
menuBar.UIElement.EnableAnchor = true;
menuBar.UIElement.MaxAnchor = new Vector2(1, 0);
That’s a very cheesy solution; is there no way to load anchors correctly from an XML style / layout or am I doing something wrong here or in a bad order?