-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.cpp
217 lines (181 loc) · 7 KB
/
mouse.cpp
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
#define GLFW_EXPOSE_NATIVE_WIN32
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include "deps/glfw3.h"
#include "deps/glfw3native.h"
// TODO:
// - why does exact resolution cause transparency to break? See :ExactResolution:
const double TAU = 6.283185307179586;
const double dim_amount = 0.7;
const int spotlight_segments = 60*4;
double dim_target;
double dim_current;
double spotlight_target_radius;
double spotlight_current_radius;
bool closing;
double lerp(double a, double b, double t) { return a + t * (b - a); }
void begin_closing()
{
closing = true;
dim_target = 0;
spotlight_target_radius = 1;
}
int show_spotlight()
{
// reset values
dim_target = dim_amount;
dim_current = 0;
spotlight_target_radius = 0.1;
spotlight_current_radius = 1;
closing = false;
// initialize the library
if (!glfwInit()) return 1;
// get resolution information
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
// calculate aspect ratio
int width = mode->width;
int height = mode->height + 1; // see :ExactResolution:
float ratio = (float)mode->width / (float)mode->height;
// set the window type
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); // transparent
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE); // always on top
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // borderless
// create a window and its OpenGL context
GLFWwindow *window = glfwCreateWindow(width, height, "Mouse Highlighter", NULL, NULL);
if (!window) { glfwTerminate(); return 2; }
// remove the taskbar icon - feels a little hacky, the other option is to explore
// having the main entry point be a graphical application that has no visuals except
// living in the system tray, then make that the parent of this spotlight window
HWND hwnd = glfwGetWin32Window(window);
DWORD style = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, (style & ~WS_EX_APPWINDOW) | WS_EX_TOOLWINDOW);
// listen for key press
glfwSetKeyCallback(window, [](GLFWwindow*, int, int, int action, int) {
if (action == GLFW_PRESS) begin_closing();
});
// listen for click
glfwSetMouseButtonCallback(window, [](GLFWwindow*, int, int action, int) {
if (action == GLFW_PRESS) begin_closing();
});
// listen for mouse wheel scrolling
glfwSetScrollCallback(window, [](GLFWwindow*, double, double scroll) {
if (closing) return;
spotlight_target_radius += scroll * -0.05;
if (spotlight_target_radius < 0.05) spotlight_target_radius = 0.05;
if (spotlight_target_radius > 0.60) spotlight_target_radius = 0.60;
});
// make the window's context current
glfwMakeContextCurrent(window);
// turn on vsync
glfwSwapInterval(1);
// show the window
glfwShowWindow(window);
// used to keep track of delta time
double last = glfwGetTime();
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
// calculate delta time
double dt = glfwGetTime() - last;
last = glfwGetTime();
// get mouse position
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
double y = 2 * (1 - ypos / height) - 1;
double x = 2 * (xpos / width) - 1;
// calculate spotlight radius
double rx = spotlight_current_radius;
double ry = spotlight_current_radius * ratio;
// update dim and spotlight amounts
dim_current = lerp(dim_current, dim_target, dt * 10);
spotlight_current_radius = lerp(spotlight_current_radius, spotlight_target_radius, dt * 10);
if (closing && dim_current <= 0.01) glfwSetWindowShouldClose(window, GLFW_TRUE);
// render
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor4f(0, 0, 0, dim_current);
auto draw_quad = [](double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3)
{
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
};
for (int i = 0; i < spotlight_segments; ++i)
{
double x0 = x + sin((i+0)%spotlight_segments*TAU/spotlight_segments) * rx;
double y0 = y + cos((i+0)%spotlight_segments*TAU/spotlight_segments) * ry;
double x1 = x + sin((i+1)%spotlight_segments*TAU/spotlight_segments) * rx;
double y1 = y + cos((i+1)%spotlight_segments*TAU/spotlight_segments) * ry;
if (i < spotlight_segments / 4 * 1) // top right quadrant
{
draw_quad(x0, 1, x1, 1, x1, y1, x0, y0);
draw_quad(1, y0, 1, y1, x1, y1, x0, y0);
}
else if (i < spotlight_segments / 4 * 2) // bottom right quadrant
{
draw_quad(x0, -1, x1, -1, x1, y1, x0, y0);
draw_quad(1, y0, 1, y1, x1, y1, x0, y0);
}
else if (i < spotlight_segments / 4 * 3) // bottom left quadrant
{
draw_quad(x0, -1, x1, -1, x1, y1, x0, y0);
draw_quad(-1, y0, -1, y1, x1, y1, x0, y0);
}
else // top left quadrant
{
draw_quad(x0, 1, x1, 1, x1, y1, x0, y0);
draw_quad(-1, y0, -1, y1, x1, y1, x0, y0);
}
}
auto draw_rectangle = [](double x0, double y0, double x1, double y1)
{
glVertex2f(x0, y0);
glVertex2f(x1, y0);
glVertex2f(x1, y1);
glVertex2f(x0, y1);
};
draw_rectangle( -1, 1, 1, y + ry); // top
draw_rectangle( -1, -1, 1, y - ry); // bottom
draw_rectangle(x - rx, -1, -1, 1); // left
draw_rectangle(x + rx, -1, 1, 1); // right
glEnd();
// swap front and back buffers
glfwSwapBuffers(window);
// poll for and process events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
int main()
{
if (RegisterHotKey(NULL, 1, MOD_ALT | MOD_NOREPEAT, 'S') &&
RegisterHotKey(NULL, 2, MOD_ALT | MOD_NOREPEAT, 'Q'))
{
printf("'ALT+s' registered for spotlight\n");
printf("'ALT+q' registered for quitting\n");
}
else
{
printf("ERROR: Couldn't register keyboard shortcuts.");
return 1;
}
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
if (msg.message == WM_HOTKEY && msg.wParam == 1)
{
int result = show_spotlight();
if (result) printf("ERROR: %d\n", result);
}
if (msg.message == WM_HOTKEY && msg.wParam == 2)
{
return 0;
}
}
return 0;
}