-
Notifications
You must be signed in to change notification settings - Fork 4
/
MainWindow.axaml.cs
225 lines (173 loc) · 5.62 KB
/
MainWindow.axaml.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Avalonia.Controls;
namespace example.Views;
// example-0016
// reflect vertex change to control invalidation
//
// - move slider xyz to change vertex coord
// or use keyboard (1(x), 2(y), 3(z)) with shift to decrease
// - change selected vertex using numeric updown
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region property changed
public new event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// invoke this method to forward propertchanged event notification.
/// note: not needed to specify propertyName set by compiler service to called property.
/// </summary>
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
GLModel glModel;
#region GLControlSplit
private AvaloniaGLControlSplit? _GLControlSplit = null;
/// <summary>
/// GLControlSplit
/// </summary>
public AvaloniaGLControlSplit? GLControlSplit
{
get => _GLControlSplit;
set
{
var changed = value != _GLControlSplit;
if (changed)
{
_GLControlSplit = value;
OnPropertyChanged();
}
}
}
#endregion
#region FocusedControl
private GLControl? _FocusedControl = null;
/// <summary>
/// FocusedControl
/// </summary>
public GLControl? FocusedControl
{
get => _FocusedControl;
set
{
var changed = value != _FocusedControl;
if (changed)
{
_FocusedControl = value;
OnPropertyChanged();
}
}
}
#endregion
// property used to bind VertexEdit control gui to selected vertex
#region VertexEdit
private VertexEdit? _VertexEdit = null;
/// <summary>
/// VertexEdit
/// </summary>
public VertexEdit? VertexEdit
{
get => _VertexEdit;
set
{
var changed = value != _VertexEdit;
if (changed)
{
_VertexEdit = value;
OnPropertyChanged();
}
}
}
#endregion
GLTriangle? tri = null;
// create a point (10pixel [screen]) which will be moved afterward
// to highlight selected vertex
GLPointFigure selectedVertexFig = new GLPointFigure(Vector3.Zero)
{
PointSize = 10,
Order = 1
};
public MainWindow()
{
InitializeComponent();
var q = this.Resources;
Title = AppDomain.CurrentDomain.FriendlyName;
// light[0] : -1700,0,1100
var glCtx = new GLContext();
glModel = new GLModel(glCtx);
glModel.BuildModel = (glCtl, isInitial) =>
{
if (!isInitial) return;
var glModel = glCtl.GLModel;
glModel.Clear();
var a = new Vector3(-50, -50, 0);
var b = new Vector3(50, -50, 0);
var c = new Vector3(0, 50, 0);
var va = new GLVertex(a, Color.Red);
var vb = new GLVertex(b, Color.Green);
var vc = new GLVertex(c, Color.Blue);
tri = new GLTriangle(va, vb, vc);
var fig = new GLTriangleFigure(tri);
glModel.AddFigure(fig);
glModel.AddFigure(selectedVertexFig);
glCtl.CameraView(CameraViewType.Top);
glModel.ViewInvalidated += (model) => glCtl.Invalidate();
BindVtxMgr(0);
};
this.AttachGLControlSplit(grGL, glModel,
x => GLControlSplit = x,
onFocusedControlChanged: (glSplit, avaloniaGlControl, isInitial) =>
FocusedControl = avaloniaGlControl?.GLControl);
VertexIndexSelector.ValueChanged += VertexIndexSelector_ValueChanged;
this.KeyDown += (sender, e) =>
{
if (VertexEdit is null) return;
var val = 1f;
if (e.KeyModifiers == KeyModifiers.Shift) val *= -1f;
switch (e.Key)
{
case Key.D1: VertexEdit.X += val; break;
case Key.D2: VertexEdit.Y += val; break;
case Key.D3: VertexEdit.Z += val; break;
}
};
}
/// <summary>
/// connect a VertexEdit gui editor ( 3 slider coord xyz )
/// to model vertex of given index
/// </summary>
void BindVtxMgr(int idx)
{
if (tri is not null)
{
var v = idx switch
{
0 => tri.V1,
1 => tri.V2,
2 => tri.V3,
_ => throw new ArgumentException($"invalid vertex index")
};
// pass getter, setter methods to vertex edit gui ctrl
// to retrieve Vector3 from GLVertex and to set Vector3 Position of GLVertex
VertexEdit = new VertexEdit(
() => v.Position,
(vv) =>
{
// update application point figure position
selectedVertexFig.Move(vv, relative: false);
v.Position = vv;
});
}
}
private void VertexIndexSelector_ValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
{
if (VertexIndexSelector.Value is null) return;
var idx = (int)VertexIndexSelector.Value;
BindVtxMgr(idx);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (GLControlSplit?.FocusedControl is GLView glView)
glView.AvaloniaGLControl.HandleKeyDown(e);
}
}