camcontrol: release mouse when window is not focused

This commit is contained in:
Andrei Alexeyev 2021-07-17 14:49:14 +03:00
parent 20ec3e62e9
commit da5d45efc3
No known key found for this signature in database
GPG key ID: 72D26128040B9690

View file

@ -68,6 +68,16 @@ static bool wheel_event(SDL_Event *e, void *a) {
return false;
}
static bool focus_gained_event(SDL_Event *e, void *a) {
SDL_SetWindowGrab(a, true);
SDL_CaptureMouse(true);
}
static bool focus_lost_event(SDL_Event *e, void *a) {
SDL_SetWindowGrab(a, false);
SDL_CaptureMouse(false);
}
static void nodraw(EntityInterface *ent) { }
static void predraw(EntityInterface *ent, void *a) {
@ -97,6 +107,14 @@ TASK(camera_control, { Camera3D *cam; }) {
&(EventHandler) { .proc = wheel_event, .arg = &move_speed, .event_type = SDL_MOUSEWHEEL }
);
events_register_handler(
&(EventHandler) { .proc = focus_gained_event, .arg = w, .event_type = SDL_WINDOWEVENT_FOCUS_GAINED }
);
events_register_handler(
&(EventHandler) { .proc = focus_lost_event, .arg = w, .event_type = SDL_WINDOWEVENT_FOCUS_LOST }
);
ent_hook_pre_draw(predraw, NULL);
Font *fnt = res_font("monotiny");