Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GridBase.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Diagnostics;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Mathematics;
8 
9 namespace SiliconStudio.Paradox.UI.Panels
10 {
11  /// <summary>
12  /// Represents the base primitive for all the grid-like controls
13  /// </summary>
14  [DebuggerDisplay("GridBase - Name={Name}")]
15  public abstract class GridBase : Panel
16  {
17  /// <summary>
18  /// The key to the Column attached dependency property. This defines the column an item is inserted into.
19  /// </summary>
20  /// <remarks>First column has 0 as index</remarks>
21  public readonly static PropertyKey<int> ColumnPropertyKey = new PropertyKey<int>("ColumnKey", typeof(GridBase), DefaultValueMetadata.Static(0), ObjectInvalidationMetadata.New<int>(InvalidateParentGridMeasure));
22 
23  /// <summary>
24  /// The key to the Row attached dependency property. This defines the row an item is inserted into.
25  /// </summary>
26  /// <remarks>First row has 0 as index</remarks>
27  public readonly static PropertyKey<int> RowPropertyKey = new PropertyKey<int>("RowKey", typeof(GridBase), DefaultValueMetadata.Static(0), ObjectInvalidationMetadata.New<int>(InvalidateParentGridMeasure));
28 
29  /// <summary>
30  /// The key to the Layer attached dependency property. This defines the layer an item is inserted into.
31  /// </summary>
32  /// <remarks>First layer has 0 as index</remarks>
33  public readonly static PropertyKey<int> LayerPropertyKey = new PropertyKey<int>("LayerKey", typeof(GridBase), DefaultValueMetadata.Static(0), ObjectInvalidationMetadata.New<int>(InvalidateParentGridMeasure));
34 
35  /// <summary>
36  /// The key to the ColumnSpan attached dependency property. This defines the number of columns an item takes.
37  /// </summary>
38  /// <exception cref="ArgumentOutOfRangeException">The value must be strictly positive</exception>
39  public readonly static PropertyKey<int> ColumnSpanPropertyKey = new PropertyKey<int>("ColumnSpanKey", typeof(GridBase), DefaultValueMetadata.Static(1), ValidateValueMetadata.New<int>(SpanValidator), ObjectInvalidationMetadata.New<int>(InvalidateParentGridMeasure));
40 
41  /// <summary>
42  /// The key to the RowSpan attached dependency property. This defines the number of rows an item takes.
43  /// </summary>
44  /// <exception cref="ArgumentOutOfRangeException">The value must be strictly positive</exception>
45  public readonly static PropertyKey<int> RowSpanPropertyKey = new PropertyKey<int>("RowSpanKey", typeof(GridBase), DefaultValueMetadata.Static(1), ValidateValueMetadata.New<int>(SpanValidator), ObjectInvalidationMetadata.New<int>(InvalidateParentGridMeasure));
46 
47  /// <summary>
48  /// The key to the LayerSpan attached dependency property. This defines the number of layers an item takes.
49  /// </summary>
50  /// <exception cref="ArgumentOutOfRangeException">The value must be strictly positive</exception>
51  public readonly static PropertyKey<int> LayerSpanPropertyKey = new PropertyKey<int>("LayerSpanKey", typeof(GridBase), DefaultValueMetadata.Static(1), ValidateValueMetadata.New<int>(SpanValidator), ObjectInvalidationMetadata.New<int>(InvalidateParentGridMeasure));
52 
53  private static void InvalidateParentGridMeasure(object propertyowner, PropertyKey<int> propertykey, int propertyoldvalue)
54  {
55  var element = (UIElement)propertyowner;
56  var parentGridBase = element.Parent as GridBase;
57 
58  if(parentGridBase != null)
59  parentGridBase.InvalidateMeasure();
60  }
61 
62  private static void SpanValidator(ref int value)
63  {
64  if (value < 1)
65  throw new ArgumentOutOfRangeException("value");
66  }
67 
68  /// <summary>
69  /// Get an element span values as an <see cref="Int3"/>.
70  /// </summary>
71  /// <param name="element">The element from which extract the span values</param>
72  /// <returns>The span values of the element</returns>
73  protected Int3 GetElementSpanValues(UIElement element)
74  {
75  return new Int3(
76  element.DependencyProperties.Get(ColumnSpanPropertyKey),
77  element.DependencyProperties.Get(RowSpanPropertyKey),
78  element.DependencyProperties.Get(LayerSpanPropertyKey));
79  }
80 
81  /// <summary>
82  /// Get the positions of an element in the grid as an <see cref="Int3"/>.
83  /// </summary>
84  /// <param name="element">The element from which extract the position values</param>
85  /// <returns>The position of the element</returns>
87  {
88  return new Int3(
89  element.DependencyProperties.Get(ColumnPropertyKey),
90  element.DependencyProperties.Get(RowPropertyKey),
91  element.DependencyProperties.Get(LayerPropertyKey));
92  }
93  /// <summary>
94  /// Get an element span values as an <see cref="Vector3"/>.
95  /// </summary>
96  /// <param name="element">The element from which extract the span values</param>
97  /// <returns>The span values of the element</returns>
99  {
100  var intValues = GetElementSpanValues(element);
101 
102  return new Vector3(intValues.X, intValues.Y, intValues.Z);
103  }
104 
105  /// <summary>
106  /// Get the positions of an element in the grid as an <see cref="Vector3"/>.
107  /// </summary>
108  /// <param name="element">The element from which extract the position values</param>
109  /// <returns>The position of the element</returns>
111  {
112  var intValues = GetElementGridPositions(element);
113 
114  return new Vector3(intValues.X, intValues.Y, intValues.Z);
115  }
116  }
117 }
Provides a base class for all the User Interface elements in Paradox applications.
Definition: UIElement.cs:21
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
Represents a three dimensional mathematical vector.
Definition: Int3.cs:41
Represents the base primitive for all the grid-like controls
Definition: GridBase.cs:15
Vector3 GetElementSpanValuesAsFloat(UIElement element)
Get an element span values as an Vector3.
Definition: GridBase.cs:98
Int3 GetElementGridPositions(UIElement element)
Get the positions of an element in the grid as an Int3.
Definition: GridBase.cs:86
SiliconStudio.Core.Mathematics.Vector3 Vector3
Int3 GetElementSpanValues(UIElement element)
Get an element span values as an Int3.
Definition: GridBase.cs:73
Vector3 GetElementGridPositionsAsFloat(UIElement element)
Get the positions of an element in the grid as an Vector3.
Definition: GridBase.cs:110
A class that represents a tag propety.
Definition: PropertyKey.cs:17
object Get(PropertyKey propertyKey)
Gets the specified tag value.
PropertyContainer DependencyProperties
List of the dependency properties attached to the object.
Definition: UIElement.cs:198
Provides a base class for all Panel elements. Use Panel elements to position and arrange child object...
Definition: Panel.cs:19