-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
174 lines (169 loc) · 6.31 KB
/
MainWindow.xaml.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using HelixToolkit.Wpf.SharpDX;
using HelixToolkit.Wpf.SharpDX.Animations;
using HelixToolkit.Wpf.SharpDX.Assimp;
using HelixToolkit.Wpf.SharpDX.Controls;
using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Real_3D_Model_Viewer
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Property_Changes
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string info = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
protected bool SetValue<T>(ref T backingField, T value, [CallerMemberName]string propertyName = "")
{
if (Equals(backingField, value))
{
return false;
}
backingField = value;
OnPropertyChanged(propertyName);
return true;
}
#endregion
public EffectsManager EffectsManager { get; }
public Camera Camera { get; }
public MainWindow()//инициализация главного окна
{
InitializeComponent();
EffectsManager = new DefaultEffectsManager();
Camera = new OrthographicCamera()
{
LookDirection = new System.Windows.Media.Media3D.Vector3D(0, -10, -10),
Position = new System.Windows.Media.Media3D.Point3D(0, 10, 10),
UpDirection = new System.Windows.Media.Media3D.Vector3D(0, 1, 0),
FarPlaneDistance = 5000,
NearPlaneDistance = 0.1f
};
DataContext = this;
}
private void OpenModel_Button_Click(object sender, RoutedEventArgs e)
{
ImportTry();
}
private void ClearScene_Button_Click(object sender, RoutedEventArgs e)
{
Animations.Clear();
GroupModel.Clear();
MessageBox.Show("Сцена была очищена.");
}
public HelixToolkitScene scene;
public ObservableCollection<Animation> Animations { get; } = new ObservableCollection<Animation>();
public SceneNodeGroupModel3D GroupModel { get; } = new SceneNodeGroupModel3D();
public void ImportTry()
{
var opFile = new OpenFileDialog();
if (opFile.ShowDialog() == true)
{
try
{
var path = opFile.FileName;
var loader = new Importer();
var scene = loader.Load(path);
if (scene != null)
{
GroupModel.AddNode(scene.Root);
}
if (scene.HasAnimation)
{
foreach (var ani in scene.Animations)
{
Animations.Add(ani);
}
}
MessageBox.Show("Модель была успешно добавлена на сцену.");
}
catch
{
MessageBox.Show("Ошибка во время загрузки модели.");
}
}
}
public bool enableAnimation = false;
public bool EnableAnimation
{
set
{
if (SetValue(ref enableAnimation, value))
{
if (value)
{
StartAnimation();
}
else
{
StopAnimation();
}
}
}
get { return enableAnimation; }
}
public NodeAnimationUpdater animationUpdater;
public Animation selectedAnimation = null;
public Animation SelectedAnimation
{
set
{
if (SetValue(ref selectedAnimation, value))
{
StopAnimation();
if (value != null)
{
animationUpdater = new NodeAnimationUpdater(value);
}
else
{
animationUpdater = null;
}
if (enableAnimation)
{
StartAnimation();
}
}
}
get
{
return selectedAnimation;
}
}
private CompositionTargetEx compositeHelper = new CompositionTargetEx();
public void StartAnimation()
{
compositeHelper.Rendering += CompositeHelper_Rendering;
}
public void StopAnimation()
{
compositeHelper.Rendering -= CompositeHelper_Rendering;
}
private void CompositeHelper_Rendering(object sender, System.Windows.Media.RenderingEventArgs e)
{
if (animationUpdater != null)
{
animationUpdater.Update(Stopwatch.GetTimestamp(), Stopwatch.Frequency);
}
}
private void QuestButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Горячие клавиши:\n" +
"1)B, F, T, D, L, R - просмотр сцены с разных сторон (Back, Front, Top, Bottom, Left, Right соответственно).\n" +
"2) Прокрутка колесика мышки для масштабирования.\n" +
"3) Правая кнопка для вращения сцены/модели.\n" +
"4) Левая кнопка для перемещения модели по сцене." +
"\n Для активации выбрать анимацию из списка.");
}
}
}