-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainWindow.axaml.cs
125 lines (96 loc) · 3.74 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
using Avalonia.Controls;
namespace example.Views;
// example-0002
// random lines ( avalonia AXAML program )
//
// - click added desktop button to invalidate with rebuild of the scene
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
public MainWindow()
{
InitializeComponent();
Title = AppDomain.CurrentDomain.FriendlyName;
Width = DEFAULT_WINDOW_WIDTH;
Height = DEFAULT_WINDOW_HEIGHT;
var glCtx = new GLContext();
glModel = new GLModel(glCtx);
glModel.BuildModel = (glCtl, isInitial) =>
{
var glModel = glCtl.GLModel;
glModel.Clear();
var L = 100;
var rnd = new Random();
var N = 100;
{
var xLine = GLLine.PointV(Vector3.Zero, new Vector3(L, 0, 0), Color.White, Color.Red);
var yLine = GLLine.PointV(Vector3.Zero, new Vector3(0, L, 0), Color.White, Color.Green);
var zLine = GLLine.PointV(Vector3.Zero, new Vector3(0, 0, L), Color.White, Color.Blue);
glModel.AddFigure(new GLLineFigure(xLine, yLine, zLine));
}
var lines = new List<GLLine>();
for (int i = 0; i < N; ++i)
{
var rnds = Enumerable.Range(0, 6).Select(r => rnd.NextSingle()).ToList();
var color = Color.FromArgb((byte)(rnds[0] * 255), (byte)(rnds[1] * 255), (byte)(rnds[2] * 255));
var line = GLLine.FromTo(
new Vector3(rnds[0] * L, rnds[1] * L, rnds[2] * L),
new Vector3(rnds[3] * L, rnds[4] * L, rnds[5] * L),
color);
lines.Add(line);
}
glModel.AddFigure(new GLLineFigure(lines));
if (isInitial) glCtl.CameraView(CameraViewType.Top);
};
// AvaloniaGLControlSplit is the root control that holds one or more GLControl
// in the mvvm gl model there is the need to attach this control as a child of a given Grid
this.AttachGLControlSplit(
// grid where to place split ctl as a child
grGL,
// the GL Model
glModel,
// bind gl split to a local property for further usage
setGLControlSplit: x => GLControlSplit = x);
this.btnRandomLines.Click += (a, b) =>
{
// through gl split access to the focused gl control and call the invalidate with model rebuild
GLControlSplit?.Invalidate(InvalidateEnum.RebuildModelAndRedraw);
};
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (GLControlSplit?.FocusedControl is GLView glView)
glView.AvaloniaGLControl.HandleKeyDown(e);
}
}