Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddio/lnxmouse.cpp: Code compression using lambdas #653

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
264 changes: 98 additions & 166 deletions ddio/lnxmouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,195 +192,127 @@
int sdlMouseButtonDownFilter(SDL_Event const *event) {
ASSERT(event->type == SDL_MOUSEBUTTONDOWN);

const SDL_MouseButtonEvent *ev = &event->button;
t_mse_event mevt;

if (ev->button == 1) {
DDIO_mouse_state.btn_mask |= MOUSE_LB;
DIM_buttons.down_count[0]++;
DIM_buttons.time_down[0] = timer_GetTime();
DIM_buttons.is_down[0] = true;
mevt.btn = 0;
mevt.state = true;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 0: Down\n");
} else if (ev->button == 2) {
DDIO_mouse_state.btn_mask |= MOUSE_RB;
DIM_buttons.down_count[1]++;
DIM_buttons.time_down[1] = timer_GetTime();
DIM_buttons.is_down[1] = true;
mevt.btn = 1;
mevt.state = true;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 1: Down\n");
}
else if (ev->button == 3) {
DDIO_mouse_state.btn_mask |= MOUSE_CB;
DIM_buttons.down_count[2]++;
DIM_buttons.time_down[2] = timer_GetTime();
DIM_buttons.is_down[2] = true;
mevt.btn = 2;
mevt.state = true;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 2: Down\n");
}

auto register_press = [](int button_bits, std::size_t button_index) {
DDIO_mouse_state.btn_mask |= button_bits;
DIM_buttons.down_count[button_index]++;
DIM_buttons.time_down[button_index] = timer_GetTime();
DIM_buttons.is_down[button_index] = true;
{
t_mse_event mevt;
mevt.btn = button_index;
mevt.state = true;
MB_queue.send(mevt);
}
};

switch (event->button.button) {
case 1:
register_press(MOUSE_LB, 0);
break;
case 2:
register_press(MOUSE_RB, 1);
break;
case 3:
register_press(MOUSE_CB, 2);
break;
// buttons 4 and 5 are reserved for the mouse wheel...that's how the engine works...adjust in here.

else if (ev->button == 4) {
DDIO_mouse_state.btn_mask |= MOUSE_B6;
DIM_buttons.down_count[5]++;
DIM_buttons.time_down[5] = timer_GetTime();
DIM_buttons.is_down[5] = true;
mevt.btn = 5;
mevt.state = true;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 5: Down\n");
}
else if (ev->button == 5) {
DDIO_mouse_state.btn_mask |= MOUSE_B7;
DIM_buttons.down_count[6]++;
DIM_buttons.time_down[6] = timer_GetTime();
DIM_buttons.is_down[6] = true;
mevt.btn = 6;
mevt.state = true;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 6: Down\n");
}
else if (ev->button == 6) {
DDIO_mouse_state.btn_mask |= MOUSE_B8;
DIM_buttons.down_count[7]++;
DIM_buttons.time_down[7] = timer_GetTime();
DIM_buttons.is_down[7] = true;
mevt.btn = 7;
mevt.state = true;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 7: Down\n");
case 4:
register_press(MOUSE_B6, 5);
break;
case 5:
register_press(MOUSE_B7, 6);
break;
case 6:
register_press(MOUSE_B8, 7);
break;
default:
break;
}

return (0);
return 0;
}

int sdlMouseButtonUpFilter(SDL_Event const *event) {
ASSERT(event->type == SDL_MOUSEBUTTONUP);

const SDL_MouseButtonEvent *ev = &event->button;
t_mse_event mevt;

if (ev->button == 1) {
DDIO_mouse_state.btn_mask &= (~MOUSE_LB);
DIM_buttons.up_count[0]++;
DIM_buttons.is_down[0] = false;
DIM_buttons.time_up[0] = timer_GetTime();
mevt.btn = 0;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 0: Up\n");
}
else if (ev->button == 2) {
DDIO_mouse_state.btn_mask &= (~MOUSE_RB);
DIM_buttons.up_count[1]++;
DIM_buttons.is_down[1] = false;
DIM_buttons.time_up[1] = timer_GetTime();
mevt.btn = 1;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 1: Up\n");
}
else if (ev->button == 3) {
DDIO_mouse_state.btn_mask &= (~MOUSE_CB);
DIM_buttons.up_count[2]++;
DIM_buttons.is_down[2] = false;
DIM_buttons.time_up[2] = timer_GetTime();
mevt.btn = 2;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 2: Up\n");

}

auto register_release = [](int button_bits, std::size_t button_index) {
DDIO_mouse_state.btn_mask &= (~button_bits);
DIM_buttons.up_count[button_index]++;
DIM_buttons.is_down[button_index] = false;
DIM_buttons.time_up[button_index] = timer_GetTime();
{
t_mse_event mevt;
mevt.btn = button_index;
mevt.state = false;
MB_queue.send(mevt);
}
};

switch (event->button.button) {
case 1:
register_release(MOUSE_LB, 0);
break;
case 2:
register_release(MOUSE_RB, 1);
break;
case 3:
register_release(MOUSE_CB, 2);
break;
// buttons 4 and 5 are reserved for the mouse wheel...that's how the engine works...adjust in here.

else if (ev->button == 4) {
DDIO_mouse_state.btn_mask &= (~MOUSE_B6);
DIM_buttons.up_count[5]++;
DIM_buttons.is_down[5] = false;
DIM_buttons.time_up[5] = timer_GetTime();
mevt.btn = 5;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 5: Up\n");
}
else if (ev->button == 5) {
DDIO_mouse_state.btn_mask &= (~MOUSE_B7);
DIM_buttons.up_count[6]++;
DIM_buttons.is_down[6] = false;
DIM_buttons.time_up[6] = timer_GetTime();
mevt.btn = 6;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 6: Up\n");
}
else if (ev->button == 6) {
DDIO_mouse_state.btn_mask &= (~MOUSE_B8);
DIM_buttons.up_count[7]++;
DIM_buttons.is_down[7] = false;
DIM_buttons.time_up[7] = timer_GetTime();
mevt.btn = 7;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Button 7: Up\n");
case 4:
register_release(MOUSE_B6, 5);
break;
case 5:
register_release(MOUSE_B7, 6);
break;
case 6:
register_release(MOUSE_B8, 7);
break;
default:
break;
}

return (0);
return 0;
}

int sdlMouseWheelFilter(SDL_Event const *event) {
ASSERT(event->type == SDL_MOUSEWHEEL);

const SDL_MouseWheelEvent *ev = &event->wheel;
t_mse_event mevt;

// !!! FIXME: this ignores horizontal wheels for now, since Descent3 doesn't currently have a concept of them
// !!! FIXME: (vertical mouse wheels are represented as mouse buttons 4 and 5, incorrectly, on all platforms).
// !!! FIXME: this will require improvements to the engine before this changes here, though.

if (ev->y > 0) { /* Mouse scroll up */
DDIO_mouse_state.btn_mask |= MOUSE_B5;
DIM_buttons.down_count[4]++;
DIM_buttons.time_down[4] = timer_GetTime();
DIM_buttons.is_down[4] = true;
mevt.btn = 4;
mevt.state = true;
MB_queue.send(mevt);

// send an immediate release event, as if the "button" was clicked. !!! FIXME: this also needs improvements in the engine.
// don't remove from btn_mask
DIM_buttons.up_count[4]++;
DIM_buttons.is_down[4] = false;
DIM_buttons.time_up[4] = timer_GetTime();
mevt.btn = 4;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Scrollwheel: Rolled Up\n");
} else if (ev->y < 0) { /* Mouse scroll down */
DDIO_mouse_state.btn_mask |= MOUSE_B6;
DIM_buttons.down_count[5]++;
DIM_buttons.time_down[5] = timer_GetTime();
DIM_buttons.is_down[5] = true;
mevt.btn = 5;
mevt.state = true;
MB_queue.send(mevt);
auto register_clicked = [](int button_bits, std::size_t button_index) {
DDIO_mouse_state.btn_mask |= button_bits;
DIM_buttons.down_count[button_index]++;
DIM_buttons.time_down[button_index] = timer_GetTime();
DIM_buttons.is_down[button_index] = true;
{
t_mse_event mevt;
mevt.btn = button_index;
mevt.state = true;
MB_queue.send(mevt);
}

// send an immediate release event, as if the "button" was clicked. !!! FIXME: this also needs improvements in the engine.
// don't remove from btn_mask
DIM_buttons.up_count[5]++;
DIM_buttons.is_down[5] = false;
DIM_buttons.time_up[5] = timer_GetTime();
mevt.btn = 5;
mevt.state = false;
MB_queue.send(mevt);
// mprintf(0, "MOUSE Scrollwheel: Rolled Down\n");
DIM_buttons.up_count[button_index]++;
DIM_buttons.is_down[button_index] = false;
DIM_buttons.time_up[button_index] = timer_GetTime();
{
t_mse_event mevt;
mevt.btn = button_index;
mevt.state = false;
MB_queue.send(mevt);
}
};

if (event->wheel.y > 0) {
// Mouse scroll up
register_clicked(MOUSE_B5, 4);
} else if (event->wheel.y < 0) {
// Mouse scroll down
register_clicked(MOUSE_B6, 5);
}

return 0;
Expand Down Expand Up @@ -422,7 +354,7 @@

// This function will handle all mouse events.
void ddio_InternalMouseFrame(void) {
static unsigned frame_count = 0;

Check warning on line 357 in ddio/lnxmouse.cpp

View workflow job for this annotation

GitHub Actions / macOS-ARM, Debug

variable 'frame_count' set but not used [-Wunused-but-set-variable]

Check warning on line 357 in ddio/lnxmouse.cpp

View workflow job for this annotation

GitHub Actions / macOS-ARM, Release

variable 'frame_count' set but not used [-Wunused-but-set-variable]

Check warning on line 357 in ddio/lnxmouse.cpp

View workflow job for this annotation

GitHub Actions / macOS-Intel, Release

variable 'frame_count' set but not used [-Wunused-but-set-variable]

Check warning on line 357 in ddio/lnxmouse.cpp

View workflow job for this annotation

GitHub Actions / macOS-Intel, Debug

variable 'frame_count' set but not used [-Wunused-but-set-variable]
SDL_PumpEvents();
frame_count++;
}
Expand Down
Loading