Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- namespace Lemma.Components
- {
- public class ListContainer : UIComponent
- {
- public enum ListOrientation
- {
- Vertical, Horizontal
- }
- public Property<ListOrientation> Orientation = new Property<ListOrientation> { Value = ListOrientation.Vertical };
- public Property<float> Spacing = new Property<float> { Value = 2.0f };
- private NotifyBinding binding;
- public override void InitializeProperties()
- {
- base.InitializeProperties();
- this.Children.ItemAdded += new ListProperty<UIComponent>.ItemAddedEventHandler(
- delegate(int index, UIComponent t)
- {
- this.rebuildBinding();
- });
- this.Children.ItemChanged += new ListProperty<UIComponent>.ItemChangedEventHandler(
- delegate(int index, UIComponent old, UIComponent newValue)
- {
- this.rebuildBinding();
- });
- this.Children.ItemRemoved += new ListProperty<UIComponent>.ItemRemovedEventHandler(
- delegate(int index, UIComponent t)
- {
- this.rebuildBinding();
- });
- this.rebuildBinding();
- }
- private void rebuildBinding()
- {
- if (this.binding != null)
- this.binding.Delete();
- List<IProperty> properties = new List<IProperty> { this.Orientation, this.Spacing };
- properties.AddRange(this.Children.SelectMany(x => new IProperty[] { x.ScaledSize, x.Visible }));
- this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, properties.ToArray());
- this.layoutDirty = true;
- }
- protected override void updateLayout()
- {
- if (this.Orientation.Value == ListOrientation.Horizontal)
- {
- Vector2 pos = Vector2.Zero;
- float maxHeight = 0.0f;
- foreach (UIComponent child in this.Children)
- {
- child.AnchorPoint.Value = Vector2.Zero;
- child.Position.Value = pos;
- Vector2 size = child.Visible ? child.ScaledSize : Vector2.Zero;
- pos.X += size.X + this.Spacing;
- maxHeight = Math.Max(maxHeight, size.Y);
- }
- this.Size.Value = new Vector2(pos.X - this.Spacing, maxHeight);
- }
- else
- {
- Vector2 pos = Vector2.Zero;
- float maxWidth = 0.0f;
- foreach (UIComponent child in this.Children)
- {
- child.AnchorPoint.Value = Vector2.Zero;
- child.Position.Value = pos;
- Vector2 size = child.Visible ? child.ScaledSize : Vector2.Zero;
- pos.Y += size.Y + this.Spacing;
- maxWidth = Math.Max(maxWidth, size.X);
- }
- this.Size.Value = new Vector2(maxWidth, pos.Y - this.Spacing);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment