et1337

ListContainer.cs

Dec 9th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace Lemma.Components
  8. {
  9.     public class ListContainer : UIComponent
  10.     {
  11.         public enum ListOrientation
  12.         {
  13.             Vertical, Horizontal
  14.         }
  15.         public Property<ListOrientation> Orientation = new Property<ListOrientation> { Value = ListOrientation.Vertical };
  16.         public Property<float> Spacing = new Property<float> { Value = 2.0f };
  17.         private NotifyBinding binding;
  18.  
  19.         public override void InitializeProperties()
  20.         {
  21.             base.InitializeProperties();
  22.             this.Children.ItemAdded += new ListProperty<UIComponent>.ItemAddedEventHandler(
  23.                 delegate(int index, UIComponent t)
  24.                 {
  25.                     this.rebuildBinding();
  26.                 });
  27.             this.Children.ItemChanged += new ListProperty<UIComponent>.ItemChangedEventHandler(
  28.                 delegate(int index, UIComponent old, UIComponent newValue)
  29.                 {
  30.                     this.rebuildBinding();
  31.                 });
  32.             this.Children.ItemRemoved += new ListProperty<UIComponent>.ItemRemovedEventHandler(
  33.                 delegate(int index, UIComponent t)
  34.                 {
  35.                     this.rebuildBinding();
  36.                 });
  37.             this.rebuildBinding();
  38.         }
  39.  
  40.         private void rebuildBinding()
  41.         {
  42.             if (this.binding != null)
  43.                 this.binding.Delete();
  44.             List<IProperty> properties = new List<IProperty> { this.Orientation, this.Spacing };
  45.             properties.AddRange(this.Children.SelectMany(x => new IProperty[] { x.ScaledSize, x.Visible }));
  46.             this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, properties.ToArray());
  47.             this.layoutDirty = true;
  48.         }
  49.  
  50.         protected override void updateLayout()
  51.         {
  52.             if (this.Orientation.Value == ListOrientation.Horizontal)
  53.             {
  54.                 Vector2 pos = Vector2.Zero;
  55.                 float maxHeight = 0.0f;
  56.                 foreach (UIComponent child in this.Children)
  57.                 {
  58.                     child.AnchorPoint.Value = Vector2.Zero;
  59.                     child.Position.Value = pos;
  60.                     Vector2 size = child.Visible ? child.ScaledSize : Vector2.Zero;
  61.                     pos.X += size.X + this.Spacing;
  62.                     maxHeight = Math.Max(maxHeight, size.Y);
  63.                 }
  64.                 this.Size.Value = new Vector2(pos.X - this.Spacing, maxHeight);
  65.             }
  66.             else
  67.             {
  68.                 Vector2 pos = Vector2.Zero;
  69.                 float maxWidth = 0.0f;
  70.                 foreach (UIComponent child in this.Children)
  71.                 {
  72.                     child.AnchorPoint.Value = Vector2.Zero;
  73.                     child.Position.Value = pos;
  74.                     Vector2 size = child.Visible ? child.ScaledSize : Vector2.Zero;
  75.                     pos.Y += size.Y + this.Spacing;
  76.                     maxWidth = Math.Max(maxWidth, size.X);
  77.                 }
  78.                 this.Size.Value = new Vector2(maxWidth, pos.Y - this.Spacing);
  79.             }
  80.         }
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment