Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
UniformGrid.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 grid where all the rows and columns have an uniform size.
13  /// </summary>
14  [DebuggerDisplay("UniformGrid - Name={Name}")]
15  public class UniformGrid : GridBase
16  {
17  /// <summary>
18  /// The key to the Columns dependency property.
19  /// </summary>
20  public readonly static PropertyKey<int> ColumnsPropertyKey = new PropertyKey<int>("ColumnsKey", typeof(UniformGrid), DefaultValueMetadata.Static(1), ValidateValueMetadata.New<int>(GridSizeValidator), ObjectInvalidationMetadata.New<int>(InvalidateGridMeasure));
21 
22  /// <summary>
23  /// The key to the Rows dependency property.
24  /// </summary>
25  public readonly static PropertyKey<int> RowsPropertyKey = new PropertyKey<int>("RowsKey", typeof(UniformGrid), DefaultValueMetadata.Static(1), ValidateValueMetadata.New<int>(GridSizeValidator), ObjectInvalidationMetadata.New<int>(InvalidateGridMeasure));
26 
27  /// <summary>
28  /// The key to the Layers dependency property.
29  /// </summary>
30  public readonly static PropertyKey<int> LayersPropertyKey = new PropertyKey<int>("LayersKey", typeof(UniformGrid), DefaultValueMetadata.Static(1), ValidateValueMetadata.New<int>(GridSizeValidator), ObjectInvalidationMetadata.New<int>(InvalidateGridMeasure));
31 
32  /// <summary>
33  /// The final size of one cell
34  /// </summary>
35  private Vector3 finalForOneCell;
36 
37  private static void InvalidateGridMeasure(object propertyOwner, PropertyKey<int> propertyKey, int propertyOldValue)
38  {
39  var element = (UIElement)propertyOwner;
40  element.InvalidateMeasure();
41  }
42 
43  private static void GridSizeValidator(ref int value)
44  {
45  if (value < 1)
46  throw new ArgumentOutOfRangeException("value");
47  }
48 
49  /// <summary>
50  /// Gets or sets the number of Columns that the <see cref="UniformGrid"/> has.
51  /// </summary>
52  /// <exception cref="ArgumentOutOfRangeException">The value must be strictly positive</exception>
53  public int Columns
54  {
55  get { return DependencyProperties.Get(ColumnsPropertyKey); }
56  set { DependencyProperties.Set(ColumnsPropertyKey, value); }
57  }
58 
59  /// <summary>
60  /// Gets or sets the number of Rows that the <see cref="UniformGrid"/> has.
61  /// </summary>
62  /// <exception cref="ArgumentOutOfRangeException">The value must be strictly positive</exception>
63  public int Rows
64  {
65  get { return DependencyProperties.Get(RowsPropertyKey); }
66  set { DependencyProperties.Set(RowsPropertyKey, value); }
67  }
68 
69  /// <summary>
70  /// Gets or sets the number of Layers that the <see cref="UniformGrid"/> has.
71  /// </summary>
72  /// <exception cref="ArgumentOutOfRangeException">The value must be strictly positive</exception>
73  public int Layers
74  {
75  get { return DependencyProperties.Get(LayersPropertyKey); }
76  set { DependencyProperties.Set(LayersPropertyKey, value); }
77  }
78 
79  protected override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
80  {
81  // compute the size available for one cell
82  var gridSize = new Vector3(Columns, Rows, Layers);
83  var availableForOneCell = new Vector3(availableSizeWithoutMargins.X / gridSize.X, availableSizeWithoutMargins.Y / gridSize.Y, availableSizeWithoutMargins.Z / gridSize.Z);
84 
85  // measure all the children
86  var neededForOneCell = Vector3.Zero;
87  foreach (var child in VisualChildrenCollection)
88  {
89  // compute the size available for the child depending on its spans values
90  var childSpans = GetElementSpanValuesAsFloat(child);
91  var availableForChildWithMargin = Vector3.Modulate(childSpans, availableForOneCell);
92 
93  child.Measure(availableForChildWithMargin);
94 
95  neededForOneCell = new Vector3(
96  Math.Max(neededForOneCell.X, child.DesiredSizeWithMargins.X / childSpans.X),
97  Math.Max(neededForOneCell.Y, child.DesiredSizeWithMargins.Y / childSpans.Y),
98  Math.Max(neededForOneCell.Z, child.DesiredSizeWithMargins.Z / childSpans.Z));
99  }
100 
101  return Vector3.Modulate(gridSize, neededForOneCell);
102  }
103 
104  protected override Vector3 ArrangeOverride(Vector3 finalSizeWithoutMargins)
105  {
106  // compute the size available for one cell
107  var gridSize = new Vector3(Columns, Rows, Layers);
108  finalForOneCell = new Vector3(finalSizeWithoutMargins.X / gridSize.X, finalSizeWithoutMargins.Y / gridSize.Y, finalSizeWithoutMargins.Z / gridSize.Z);
109 
110  // arrange all the children
111  foreach (var child in VisualChildrenCollection)
112  {
113  // compute the final size of the child depending on its spans values
114  var childSpans = GetElementSpanValuesAsFloat(child);
115  var finalForChildWithMargin = Vector3.Modulate(childSpans, finalForOneCell);
116 
117  // set the arrange matrix of the child
118  var childOffsets = GetElementGridPositionsAsFloat(child);
119  child.DependencyProperties.Set(PanelArrangeMatrixPropertyKey, Matrix.Translation(Vector3.Modulate(childOffsets, finalForOneCell) - finalSizeWithoutMargins / 2));
120 
121  // arrange the child
122  child.Arrange(finalForChildWithMargin, IsCollapsed);
123  }
124 
125  return finalSizeWithoutMargins;
126  }
127 
128  private void CalculateDistanceToSurroundingModulo(float position, float modulo, float elementCount, out Vector2 distances)
129  {
130  if (modulo <= 0)
131  {
132  distances = Vector2.Zero;
133  return;
134  }
135 
136  var validPosition = Math.Max(0, Math.Min(position, elementCount * modulo));
137  var inferiorQuotient = Math.Min(elementCount - 1, (float)Math.Floor(validPosition / modulo));
138 
139  distances.X = (inferiorQuotient+0) * modulo - validPosition;
140  distances.Y = (inferiorQuotient+1) * modulo - validPosition;
141  }
142 
143  public override Vector2 GetSurroudingAnchorDistances(Orientation direction, float position)
144  {
145  Vector2 distances;
146  var gridElements = new Vector3(Columns, Rows, Layers);
147 
148  CalculateDistanceToSurroundingModulo(position, finalForOneCell[(int)direction], gridElements[(int)direction], out distances);
149 
150  return distances;
151  }
152  }
153 }
Provides a base class for all the User Interface elements in Paradox applications.
Definition: UIElement.cs:21
float Y
The Y component of the vector.
Definition: Vector3.cs:84
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
Orientation
Defines the different orientations that a control or layout can have.
Definition: Orientation.cs:8
float X
The X component of the vector.
Definition: Vector3.cs:78
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
override Vector2 GetSurroudingAnchorDistances(Orientation direction, float position)
Get the distances to the previous and next anchors in the provided direction and from given position...
Definition: UniformGrid.cs:143
Represents the base primitive for all the grid-like controls
Definition: GridBase.cs:15
override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
When overridden in a derived class, measures the size in layout required for possible child elements ...
Definition: UniformGrid.cs:79
SiliconStudio.Core.Mathematics.Vector3 Vector3
Represents the grid where all the rows and columns have an uniform size.
Definition: UniformGrid.cs:15
float Z
The Z component of the vector.
Definition: Vector3.cs:90
A class that represents a tag propety.
Definition: PropertyKey.cs:17
override Vector3 ArrangeOverride(Vector3 finalSizeWithoutMargins)
When overridden in a derived class, positions possible child elements and determines a size for a UIE...
Definition: UniformGrid.cs:104