-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathHelixViewModel.cs
155 lines (137 loc) · 5.1 KB
/
HelixViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Caliburn.Micro;
using Gemini.Demo.Modules.Home.Views;
using Gemini.Framework;
using Gemini.Modules.CodeCompiler;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace Gemini.Demo.Modules.Home.ViewModels
{
[Export(typeof(HelixViewModel))]
public class HelixViewModel : Document
{
private readonly ICodeCompiler _codeCompiler;
private readonly List<IDemoScript> _scripts;
private IHelixView _helixView;
private Point3D _cameraPosition;
[DisplayName("Camera Position"), Description("Position of the camera in 3D space"), Category("Camera")]
public Point3D CameraPosition
{
get { return _cameraPosition; }
set
{
_cameraPosition = value;
NotifyOfPropertyChange(() => CameraPosition);
}
}
private double _cameraFieldOfView;
[DisplayName("Field of View"), Range(1.0, 180.0), Category("Camera")]
public double CameraFieldOfView
{
get { return _cameraFieldOfView; }
set
{
_cameraFieldOfView = value;
NotifyOfPropertyChange(() => CameraFieldOfView);
}
}
private Point3D _lightPosition;
[DisplayName("Light Position")]
public Point3D LightPosition
{
get { return _lightPosition; }
set
{
_lightPosition = value;
NotifyOfPropertyChange(() => LightPosition);
}
}
private double _rotationAngle;
[DisplayName("Rotation Angle"), ReadOnly(true)]
public double RotationAngle
{
get { return _rotationAngle; }
set
{
_rotationAngle = value;
NotifyOfPropertyChange(() => RotationAngle);
}
}
[ImportingConstructor]
public HelixViewModel(ICodeCompiler codeCompiler)
{
DisplayName = "Helix";
_codeCompiler = codeCompiler;
_scripts = new List<IDemoScript>();
CameraPosition = new Point3D(6, 5, 4);
CameraFieldOfView = 45;
LightPosition = new Point3D(0, 5, 0);
RotationAngle = 0;
}
protected override void OnViewLoaded(object view)
{
_helixView = (IHelixView)view;
_helixView.TextEditor.Text = @"public class MyClass : Gemini.Demo.Modules.Home.ViewModels.IDemoScript
{
public void Execute(Gemini.Demo.Modules.Home.ViewModels.HelixViewModel viewModel)
{
viewModel.RotationAngle += 0.1;
}
}
";
_helixView.TextEditor.TextChanged += (sender, e) => CompileScripts();
CompositionTarget.Rendering += OnRendering;
CompileScripts();
base.OnViewLoaded(view);
}
private void CompileScripts()
{
lock (_scripts)
{
_scripts.Clear();
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var newAssembly = _codeCompiler.Compile(
new[] { CSharpSyntaxTree.ParseText(_helixView.TextEditor.Text) },
new[]
{
MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")),
MetadataReference.CreateFromFile(typeof(IResult).Assembly.Location),
MetadataReference.CreateFromFile(typeof(AppBootstrapper).Assembly.Location),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(Assembly.GetEntryAssembly().Location)
},
"GeminiDemoScript");
if (newAssembly != null)
{
_scripts.AddRange(newAssembly.GetTypes()
.Where(x => typeof(IDemoScript).IsAssignableFrom(x))
.Select(x => (IDemoScript)Activator.CreateInstance(x)));
}
}
}
private void OnRendering(object sender, EventArgs e)
{
lock (_scripts)
_scripts.ForEach(x => x.Execute(this));
}
protected override async Task OnDeactivateAsync(bool close, CancellationToken cancellationToken)
{
if (close)
CompositionTarget.Rendering -= OnRendering;
await base.OnDeactivateAsync(close, cancellationToken);
}
}
}