Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PropertyGridTest2Control.xaml.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using SiliconStudio.Paradox.Framework.ViewModel;
15 using System.Reflection;
16 using System.Globalization;
17 using System.Diagnostics;
18 using SiliconStudio.Paradox.Framework.Diagnostics;
19 using SiliconStudio.Paradox.EntityModel;
20 using System.ComponentModel;
21 using SiliconStudio.Paradox.DebugTools.ViewModels;
22 using System.Collections;
23 using SiliconStudio.Presentation.Extensions;
24 
25 namespace SiliconStudio.Paradox.DebugTools
26 {
27  /// <summary>
28  /// Interaction logic for PropertyGridTest2Control.xaml
29  /// </summary>
30  public partial class PropertyGridTest2Control : UserControl
31  {
33  {
34  InitializeComponent();
35 
36  MyDateTime now = MyDateTime.FromDateTime(DateTime.Now);
37 
38  Components = new Component[]
39  {
40  CreateComponent("DateTime", now),
41  CreateComponent("Matrix", TestMatrix.CreateIdentity()),
42  };
43 
44  this.DataContext = this;
45  }
46 
47  public object Components { get; private set; }
48 
49  private IViewModelNode CreateProperty(object obj)
50  {
51  var context = new ViewModelContext();
52  var contextUI = new ViewModelContext();
53 
54  context.ChildrenPropertyEnumerators.Add(new ChildrenPropertyInfoEnumerator());
55  // add some more here...
56 
57  var testModel = new ViewModelNode("Root", obj);
58 
59  var view = ObservableViewModelNode.CreateObservableViewModel(contextUI, testModel);
60 
61  ObservableViewModelNode.Refresh(contextUI, context, new ViewModelState());
62 
63  return view;
64  }
65 
66  private Component CreateComponent(string name, object content)
67  {
68  return new Component
69  {
70  Name = name,
71  Properties = new IViewModelNode[] { CreateProperty(content) },
72  };
73  }
74  }
75 
76 
78  {
79  private static DataTemplate errorTemplate;
80  private static DataTemplate componentTemplate;
81  private static DataTemplate viewModelTemplate;
82  private static DataTemplate textTemplate;
83 
84  public override DataTemplate SelectTemplate(object item, DependencyObject container)
85  {
86  DataTemplate template = null;
87 
88  var element = container as FrameworkElement;
89  if (element == null)
90  throw new Exception("Container must be of type FrameworkElement");
91 
92  if (item is ViewModelReference && ((ViewModelReference)item).ViewModel.PropertyName == "EntityComponent")
93  {
94  if (componentTemplate == null)
95  componentTemplate = (DataTemplate)element.FindResource("EntityComponentView");
96  template = componentTemplate;
97  }
98  else if (item is ViewModelReference)
99  {
100  return (DataTemplate)element.FindResource("ViewModelReference");
101  }
102  else if (item is IViewModelNode)
103  {
104  var viewModel = (IViewModelNode)item;
105 
106  if (viewModel.Children.Count > 0)
107  {
108  if (viewModelTemplate == null)
109  viewModelTemplate = (DataTemplate)element.FindResource("IViewModelNode");
110  template = viewModelTemplate;
111  }
112  else if (viewModel.Type == typeof(ViewModelReference))
113  {
114  if (viewModel.PropertyName == "ObjectRef")
115  template = (DataTemplate)element.FindResource("ViewModelReferenceNode");
116  else
117  template = (DataTemplate)element.FindResource("ViewModelReferenceGuid");
118  }
119  else if (viewModel.Type == typeof(IList<ViewModelReference>))
120  {
121  template = (DataTemplate)element.FindResource("ListViewModelReference");
122  }
123  else
124  {
125  if (textTemplate == null)
126  textTemplate = (DataTemplate)element.FindResource("TextBox");
127  template = textTemplate;
128  }
129  }
130 
131  if (template == null)
132  {
133  if (errorTemplate == null)
134  errorTemplate = (DataTemplate)element.FindResource("Error");
135  template = errorTemplate;
136  }
137 
138  return template;
139  }
140  }
141 
142  public class Component
143  {
144  public string Name { get; set; }
145  public IEnumerable<Component> Components { get; set; }
146  public IEnumerable<IViewModelNode> Properties { get; set; }
147  }
148 
149  public struct TestMatrix
150  {
151  public float M11 { get; set; }
152  public float M12 { get; set; }
153  public float M13 { get; set; }
154  public float M14 { get; set; }
155 
156  public float M21 { get; set; }
157  public float M22 { get; set; }
158  public float M23 { get; set; }
159  public float M24 { get; set; }
160 
161  public float M31 { get; set; }
162  public float M32 { get; set; }
163  public float M33 { get; set; }
164  public float M34 { get; set; }
165 
166  public float M41 { get; set; }
167  public float M42 { get; set; }
168  public float M43 { get; set; }
169  public float M44 { get; set; }
170 
171  public static TestMatrix CreateIdentity()
172  {
173  return new TestMatrix
174  {
175  M11 = 1.0f,
176  M12 = 0.0f,
177  M13 = 0.0f,
178  M14 = 0.0f,
179  M21 = 0.0f,
180  M22 = 1.0f,
181  M23 = 0.0f,
182  M24 = 0.0f,
183  M31 = 0.0f,
184  M32 = 0.0f,
185  M33 = 1.0f,
186  M34 = 0.0f,
187  M41 = 0.0f,
188  M42 = 0.0f,
189  M43 = 0.0f,
190  M44 = 1.0f,
191  };
192  }
193  }
194 }
Interaction logic for PropertyGridTest2Control.xaml
override DataTemplate SelectTemplate(object item, DependencyObject container)