This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
TestCursor.cs
282 lines (224 loc) · 8.87 KB
/
TestCursor.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
namespace Glfw3.Tests
{
using OpenGL;
using System;
/// <summary>
/// This test provides an interface to the cursor image and cursor mode parts of the API. Custom
/// cursor image generation by urraka.
/// </summary>
/// <remarks>
/// Ported from <c>cursor.c</c>.
/// </remarks>
class TestCursor : TestBase
{
const int CURSOR_FRAME_COUNT = 60;
static double cursorX;
static double cursorY;
static int swapInterval = 1;
static bool waitEvents = true;
static bool animateCursor = false;
static bool trackCursor = false;
static Glfw.Cursor[] standardCursors;
static float Star(int x, int y, float t)
{
const float c = 64 / 2f;
float i = (0.25f * (float)Math.Sin(2f * Math.PI * t) + 0.75f);
float k = 64 * 0.046875f * i;
float dist = (float)Math.Sqrt((x - c) * (x - c) + (y - c) * (y - c));
float salpha = 1f - dist / c;
float xalpha = (float)x == c ? c : k / Math.Abs(x - c);
float yalpha = (float)y == c ? c : k / Math.Abs(y - c);
return Math.Max(0f, Math.Min(1f, i * salpha * 0.2f + salpha * xalpha * yalpha));
}
static Glfw.Cursor CreateCursorFrame(float t)
{
int i = 0, x, y;
var buffer = new byte[64 * 64 * 4];
for (y = 0; y < 64; y++)
{
for (x = 0; x < 64; x++)
{
buffer[i++] = 255;
buffer[i++] = 255;
buffer[i++] = 255;
buffer[i++] = (byte)(255 * Star(x, y, t));
}
}
var image = new Glfw.Image
{
Width = 64,
Height = 64,
Pixels = buffer
};
return Glfw.CreateCursor(image, image.Width / 2, image.Height / 2);
}
static void CursorPositionCallback(Glfw.Window window, double x, double y)
{
Log("{0}: Cursor position: {1} {2} ({3} {4})",
Glfw.GetTime(),
x, y, x - cursorX, y - cursorY);
cursorX = x;
cursorY = y;
}
static void KeyCallback(Glfw.Window window, Glfw.KeyCode key, int scancode, Glfw.InputState state, Glfw.KeyMods mods)
{
if (state != Glfw.InputState.Press)
return;
switch (key)
{
case Glfw.KeyCode.A:
{
animateCursor = !animateCursor;
if (!animateCursor)
Glfw.SetCursor(window, Glfw.Cursor.None);
break;
}
case Glfw.KeyCode.Escape:
{
Glfw.SetWindowShouldClose(window, true);
break;
}
case Glfw.KeyCode.N:
Glfw.SetInputMode(window, Glfw.InputMode.Cursor, Glfw.CursorMode.Normal);
Log("(( cursor is normal ))");
break;
case Glfw.KeyCode.D:
Glfw.SetInputMode(window, Glfw.InputMode.Cursor, Glfw.CursorMode.Disabled);
Log("(( cursor is disabled ))");
break;
case Glfw.KeyCode.H:
Glfw.SetInputMode(window, Glfw.InputMode.Cursor, Glfw.CursorMode.Hidden);
Log("(( cursor is hidden ))");
break;
case Glfw.KeyCode.Space:
swapInterval = 1 - swapInterval;
Log("(( swap interval: {0} ))", swapInterval);
Glfw.SwapInterval(swapInterval);
break;
case Glfw.KeyCode.W:
waitEvents = !waitEvents;
Log("(( {0}ing for events ))", waitEvents ? "wait" : "poll");
break;
case Glfw.KeyCode.T:
trackCursor = !trackCursor;
break;
case Glfw.KeyCode.Alpha0:
Glfw.SetCursor(window, Glfw.Cursor.None);
break;
case Glfw.KeyCode.Alpha1:
Glfw.SetCursor(window, standardCursors[0]);
break;
case Glfw.KeyCode.Alpha2:
Glfw.SetCursor(window, standardCursors[1]);
break;
case Glfw.KeyCode.Alpha3:
Glfw.SetCursor(window, standardCursors[2]);
break;
case Glfw.KeyCode.Alpha4:
Glfw.SetCursor(window, standardCursors[3]);
break;
case Glfw.KeyCode.Alpha5:
Glfw.SetCursor(window, standardCursors[4]);
break;
case Glfw.KeyCode.Alpha6:
Glfw.SetCursor(window, standardCursors[5]);
break;
}
}
static void Main(string[] args)
{
Init();
Glfw.Window window;
var starCursors = new Glfw.Cursor[CURSOR_FRAME_COUNT];
Glfw.Cursor currentFrame = Glfw.Cursor.None;
if (!Glfw.Init())
Environment.Exit(1);
for (int i = 0; i < CURSOR_FRAME_COUNT; i++)
{
starCursors[i] = CreateCursorFrame(i / (float)CURSOR_FRAME_COUNT);
if (!starCursors[i])
{
Glfw.Terminate();
Environment.Exit(1);
}
}
Glfw.CursorType[] shapes =
{
Glfw.CursorType.Arrow,
Glfw.CursorType.Beam,
Glfw.CursorType.Crosshair,
Glfw.CursorType.Hand,
Glfw.CursorType.ResizeX,
Glfw.CursorType.ResizeY
};
standardCursors = new Glfw.Cursor[6];
for (int i = 0; i < standardCursors.Length; i++)
{
standardCursors[i] = Glfw.CreateStandardCursor(shapes[i]);
if (!standardCursors[i])
{
Glfw.Terminate();
Environment.Exit(1);
}
}
window = Glfw.CreateWindow(640, 480, "Cursor Test");
if (!window)
{
Glfw.Terminate();
Environment.Exit(1);
}
Glfw.MakeContextCurrent(window);
Glfw.GetCursorPos(window, out cursorX, out cursorY);
Log("Cursor position: {0} {1}", cursorX, cursorY);
Glfw.SetCursorPosCallback(window, CursorPositionCallback);
Glfw.SetKeyCallback(window, KeyCallback);
while (!Glfw.WindowShouldClose(window))
{
Gl.Clear(ClearBufferMask.ColorBufferBit);
if (trackCursor)
{
int wnd_width, wnd_height, fb_width, fb_height;
float scale;
Glfw.GetWindowSize(window, out wnd_width, out wnd_height);
Glfw.GetFramebufferSize(window, out fb_width, out fb_height);
scale = (float)fb_width / (float)wnd_width;
Gl.Viewport(0, 0, fb_width, fb_height);
Gl.MatrixMode(MatrixMode.Projection);
Gl.LoadIdentity();
Gl.Ortho(0f, fb_width, 0f, fb_height, 0f, 1f);
Gl.Begin(PrimitiveType.Lines);
Gl.Vertex2(0f, (float)(fb_height - cursorY * scale));
Gl.Vertex2((float)fb_width, (float)(fb_height - cursorY * scale));
Gl.Vertex2((float)cursorX * scale, 0f);
Gl.Vertex2((float)cursorX * scale, (float)fb_height);
Gl.End();
}
Glfw.SwapBuffers(window);
if (animateCursor)
{
int i = (int)(Glfw.GetTime() * 30.0) % CURSOR_FRAME_COUNT;
if (currentFrame != starCursors[i])
{
Glfw.SetCursor(window, starCursors[i]);
currentFrame = starCursors[i];
}
}
else currentFrame = Glfw.Cursor.None;
if (waitEvents)
{
if (animateCursor)
Glfw.WaitEventsTimeout(1.0 / 30.0);
else
Glfw.WaitEvents();
}
else Glfw.PollEvents();
}
Glfw.DestroyWindow(window);
for (int i = 0; i < CURSOR_FRAME_COUNT; i++)
Glfw.DestroyCursor(starCursors[i]);
for (int i = 0; i < standardCursors.Length; i++)
Glfw.DestroyCursor(standardCursors[i]);
Glfw.Terminate();
}
}
}