Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ContentControl.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.Controls
10 {
11  /// <summary>
12  /// Represents a control with a single piece of content of any type.
13  /// </summary>
14  [DebuggerDisplay("ContentControl - Name={Name}")]
15  public abstract class ContentControl : Control
16  {
17  private UIElement content;
18 
19  private UIElement visualContent;
20 
21  private ContentPresenter contentPresenter;
22 
23  /// <summary>
24  /// The key to the ContentArrangeMatrix dependency property.
25  /// </summary>
26  protected readonly static PropertyKey<Matrix> ContentArrangeMatrixPropertyKey = new PropertyKey<Matrix>("ContentArrangeMatrixKey", typeof(ContentControl), DefaultValueMetadata.Static(Matrix.Identity));
27 
28  private Matrix contentWorldMatrix;
29 
30  protected override void OnNameChanged()
31  {
32  base.OnNameChanged();
33 
34  if(ContentPresenter != null)
35  ContentPresenter.Name = "of '" + Name + "'";
36  }
37 
38  /// <summary>
39  /// Gets or sets the presenter of the <see cref="ContentControl"/>'s presenter.
40  /// </summary>
42  {
43  get { return contentPresenter; }
44  set
45  {
46  if (value == contentPresenter)
47  return;
48 
49  VisualContent = value;
50  contentPresenter = value;
51  }
52  }
53 
54  /// <summary>
55  /// Gets or sets the content of a ContentControl.
56  /// </summary>
57  /// <exception cref="InvalidOperationException">The value passed has already a parent.</exception>
58  public virtual UIElement Content
59  {
60  get { return content; }
61  set
62  {
63  if(content == value)
64  return;
65 
66  if (Content != null)
67  SetParent(Content, null);
68 
69  content = value;
70 
71  if (contentPresenter == null)
72  VisualContent = content;
73  else
74  ContentPresenter.Content = value;
75 
76  if (Content != null)
77  SetParent(Content, this);
78 
79  InvalidateMeasure();
80  }
81  }
82 
83  /// <summary>
84  /// Gets the visual content of the ContentControl.
85  /// </summary>
86  public UIElement VisualContent
87  {
88  get { return visualContent; }
89  protected set
90  {
91  if (VisualContent != null)
92  SetVisualParent(VisualContent, null);
93 
94  visualContent = value;
95 
96  if (VisualContent != null)
97  SetVisualParent(visualContent, this);
98 
99  InvalidateMeasure();
100  }
101  }
102 
103  protected override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
104  {
105  // measure size desired by the children
106  var childDesiredSizeWithMargins = Vector3.Zero;
107  if (VisualContent != null)
108  {
109  // remove space for padding in availableSizeWithoutMargins
110  var childAvailableSizeWithMargins = CalculateSizeWithoutThickness(ref availableSizeWithoutMargins, ref padding);
111 
112  VisualContent.Measure(childAvailableSizeWithMargins);
113  childDesiredSizeWithMargins = VisualContent.DesiredSizeWithMargins;
114  }
115 
116  // add the padding to the child desired size
117  var desiredSizeWithPadding = CalculateSizeWithThickness(ref childDesiredSizeWithMargins, ref padding);
118 
119  return desiredSizeWithPadding;
120  }
121 
122  protected override Vector3 ArrangeOverride(Vector3 finalSizeWithoutMargins)
123  {
124  // arrange the content
125  if (VisualContent != null)
126  {
127  // calculate the remaining space for the child after having removed the padding space.
128  var childSizeWithoutPadding = CalculateSizeWithoutThickness(ref finalSizeWithoutMargins, ref padding);
129 
130  // arrange the child
131  VisualContent.Arrange(childSizeWithoutPadding, IsCollapsed);
132 
133  // compute the rendering offsets of the child element wrt the parent origin (0,0,0)
134  var childOffsets = new Vector3(Padding.Left, Padding.Top, Padding.Back) - finalSizeWithoutMargins/2;
135 
136  // set the arrange matrix of the child.
137  VisualContent.DependencyProperties.Set(ContentArrangeMatrixPropertyKey, Matrix.Translation(childOffsets));
138  }
139 
140  return finalSizeWithoutMargins;
141  }
142 
143  protected override void UpdateWorldMatrix(ref Matrix parentWorldMatrix, bool parentWorldChanged)
144  {
145  var contentMatrixChanged = parentWorldChanged || ArrangeChanged || LocalMatrixChanged;
146 
147  base.UpdateWorldMatrix(ref parentWorldMatrix, parentWorldChanged);
148 
149  if (VisualContent != null)
150  {
151  if (contentMatrixChanged)
152  {
153  var contentMatrix = VisualContent.DependencyProperties.Get(ContentArrangeMatrixPropertyKey);
154  Matrix.Multiply(ref contentMatrix, ref WorldMatrixInternal, out contentWorldMatrix);
155  }
156 
157  ((IUIElementUpdate)VisualContent).UpdateWorldMatrix(ref contentWorldMatrix, contentMatrixChanged);
158  }
159  }
160  }
161 }
Provides a base class for all the User Interface elements in Paradox applications.
Definition: UIElement.cs:21
override Vector3 ArrangeOverride(Vector3 finalSizeWithoutMargins)
When overridden in a derived class, positions possible child elements and determines a size for a UIE...
Represents the base class for user interface (UI) controls.
Definition: Control.cs:11
Interface for the update of the UIElements.
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
override void OnNameChanged()
This method is call when the name of the UIElement changes. This method can be overridden in inherite...
Represents a control with a single piece of content of any type.
override void UpdateWorldMatrix(ref Matrix parentWorldMatrix, bool parentWorldChanged)
Method called by IUIElementUpdate.UpdateWorldMatrix. Parents are in charge of recursively calling thi...
override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
When overridden in a derived class, measures the size in layout required for possible child elements ...
A class aiming at presenting another UIElement.
Only valid for a property / field that has a class or struct type. When restored, instead of recreati...
SiliconStudio.Core.Mathematics.Vector3 Vector3
A class that represents a tag propety.
Definition: PropertyKey.cs:17
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47