-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
161 lines (118 loc) · 5.29 KB
/
Program.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
namespace example;
// example-0015
// raycast in orthogonal mode for snapping test
class Program
{
static void Main(string[] args)
{
var GRID_SIZE = 2f;
var GRID_STEP = .1f;
var PAD = .25f;
var SNAP = GRID_STEP / 5;
InitAvalonia();
// to avoid reprocess vertex array buffer here we use a separate vertex manager
// where to draw dynamic temp entities such as tracking horiz/vert lines.
// this will created once and registered in BuildModel and cleared during pointer moved handler
GLVertexManager? customVtxMgr = null;
var w = GLWindow.Create(
onFocusedControlChanged: (glSplit, avaloniaGLControl, isInitial) =>
{
if (!isInitial) return;
var glCtl = avaloniaGLControl.GLControl;
var glModel = glCtl.GLModel;
avaloniaGLControl.PointerMoved += (a, e) =>
{
if (customVtxMgr is null) return;
var p = e.GetPosition(avaloniaGLControl).ToVector2();
var size = glCtl.Device.Size.ToVector2();
var lcoord = glCtl.RayCastLocal(screen: p).From;
customVtxMgr.Clear();
customVtxMgr.AddFigure(
MakeRectangle(
csCenter: WCS,
w: GRID_SIZE + 2 * PAD,
h: GRID_SIZE + 2 * PAD,
Color.Blue));
var rightTop = new Vector3(GRID_SIZE / 2 + PAD, GRID_SIZE / 2 + PAD, 0);
var rightBottom = new Vector3(GRID_SIZE / 2 + PAD, -GRID_SIZE / 2 - PAD, 0);
customVtxMgr.AddFigure(glModel.MakeTextFigure(new GLText(
cs: WCS.Move(rightTop),
text: $"{lcoord}",
height: PAD / 2,
alignment: GLTextVHAlignment.TopRight)));
#region draw horizontal matching line
{
var xrest = lcoord.X % GRID_STEP;
var xMatches =
lcoord.X >= -GRID_SIZE / 2 && lcoord.X <= GRID_SIZE / 2 &&
(xrest <= SNAP || xrest >= GRID_STEP - SNAP);
var xMatchVal = 0f;
if (xMatches)
{
xMatchVal = (float)Round(lcoord.X / GRID_STEP) * GRID_STEP;
var fig = new GLLineFigure(GLLine.FromTo(
new GLVertex(new Vector3(xMatchVal, -GRID_SIZE / 2, 0)),
new GLVertex(new Vector3(xMatchVal, GRID_SIZE / 2, 0))))
{
Order = 1
};
fig.SetColor(Color.Yellow);
customVtxMgr.AddFigure(fig);
}
}
#endregion
#region draw vertical matching line
{
var yrest = lcoord.Y % GRID_STEP;
var yMatches =
lcoord.Y >= -GRID_SIZE / 2 && lcoord.Y <= GRID_SIZE / 2 &&
(yrest <= SNAP || yrest >= GRID_STEP - SNAP);
var yMatchVal = 0f;
if (yMatches)
{
yMatchVal = (float)Round(lcoord.Y / GRID_STEP) * GRID_STEP;
var fig = new GLLineFigure(GLLine.FromTo(
new GLVertex(new Vector3(-GRID_SIZE / 2, yMatchVal, 0)),
new GLVertex(new Vector3(GRID_SIZE / 2, yMatchVal, 0))))
{
Order = 1
};
fig.SetColor(Color.Yellow);
customVtxMgr.AddFigure(fig);
}
}
#endregion
glCtl.ZoomFit();
};
}
);
w.GLModel.BuildModel = (glCtl, isInitial) =>
{
if (!isInitial) return;
var glModel = glCtl.GLModel;
glModel.Clear();
customVtxMgr = new GLVertexManager();
glModel.AddCustomVertexManager(customVtxMgr);
glModel.AddFigure(MakeWCSFigure(size: GRID_SIZE / 2).Act(fig => fig.Order = 1));
{
var lines = new List<GLLine>();
float x = -GRID_SIZE / 2;
while (x.LessThanOrEqualsTol(1e-2f, 1))
{
lines.Add(new Vector3(x, -1, 0).LineV(Vector3.UnitY * 2, Color.Gray));
x += GRID_STEP;
}
float y = -GRID_SIZE / 2;
while (y.LessThanOrEqualsTol(1e-2f, 1))
{
lines.Add(new Vector3(-1, y, 0).LineV(Vector3.UnitX * 2, Color.Gray));
y += GRID_STEP;
}
glModel.AddFigure(new GLLineFigure(lines));
}
glCtl.Perspective = false;
glCtl.CameraView(CameraViewType.Top);
};
w.ShowSync();
}
}