dxx-rebirth/common/arch/sdl/event.cpp
daivuk 74ef8b02e6 Added axis buttons
Each axis can act as two buttons in both ways.
For example, a player might map slide left and slide right to J1 -A1 and J1 +A1 as button presses instead of the slide L/R axis.

This is mostly to fix XBox 360 Controller Left and Right triggers. But it can work on every axis if the player wishes to bind them.
2019-09-01 13:11:14 -04:00

256 lines
6.2 KiB
C++

/*
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
/*
*
* SDL Event related stuff
*
*
*/
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include "event.h"
#include "key.h"
#include "mouse.h"
#include "window.h"
#include "timer.h"
#include "config.h"
#include "inferno.h"
#include "joy.h"
#include "args.h"
namespace dcx {
#if SDL_MAJOR_VERSION == 2
extern SDL_Window *g_pRebirthSDLMainWindow;
static void windowevent_handler(const SDL_WindowEvent &windowevent)
{
switch (windowevent.event)
{
case SDL_WINDOWEVENT_SIZE_CHANGED:
{
const d_window_size_event e{windowevent.data1, windowevent.data2};
event_send(e);
break;
}
}
}
#endif
window_event_result event_poll()
{
SDL_Event event;
int clean_uniframe=1;
window *wind = window_get_front();
window_event_result highest_result(window_event_result::ignored);
// If the front window changes, exit this loop, otherwise unintended behavior can occur
// like pressing 'Return' really fast at 'Difficulty Level' causing multiple games to be started
while ((highest_result != window_event_result::deleted) && (wind == window_get_front()) && (event = {}, SDL_PollEvent(&event)))
{
switch(event.type) {
#if SDL_MAJOR_VERSION == 2
case SDL_WINDOWEVENT:
windowevent_handler(event.window);
break;
#endif
case SDL_KEYDOWN:
case SDL_KEYUP:
if (clean_uniframe)
{
clean_uniframe=0;
unicode_frame_buffer = {};
}
highest_result = std::max(key_handler(&event.key), highest_result);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (CGameArg.CtlNoMouse)
break;
highest_result = std::max(mouse_button_handler(&event.button), highest_result);
break;
case SDL_MOUSEMOTION:
if (CGameArg.CtlNoMouse)
break;
highest_result = std::max(mouse_motion_handler(&event.motion), highest_result);
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
if (CGameArg.CtlNoJoystick)
break;
highest_result = std::max(joy_button_handler(&event.jbutton), highest_result);
break;
case SDL_JOYAXISMOTION:
if (CGameArg.CtlNoJoystick)
break;
highest_result = std::max(joy_axisbutton_handler(&event.jaxis), highest_result);
highest_result = std::max(joy_axis_handler(&event.jaxis), highest_result);
break;
case SDL_JOYHATMOTION:
if (CGameArg.CtlNoJoystick)
break;
highest_result = std::max(joy_hat_handler(&event.jhat), highest_result);
break;
case SDL_JOYBALLMOTION:
break;
case SDL_QUIT: {
d_event qevent = { EVENT_QUIT };
highest_result = std::max(call_default_handler(qevent), highest_result);
} break;
}
}
// Send the idle event if there were no other events (or they were ignored)
if (highest_result == window_event_result::ignored)
{
const d_event ievent{EVENT_IDLE};
highest_result = std::max(event_send(ievent), highest_result);
}
else
{
#if DXX_USE_EDITOR
event_reset_idle_seconds();
#endif
}
mouse_cursor_autohide();
return highest_result;
}
void event_flush()
{
SDL_Event event;
while (SDL_PollEvent(&event));
}
int event_init()
{
// We should now be active and responding to events.
return 0;
}
window_event_result call_default_handler(const d_event &event)
{
return standard_handler(event);
}
window_event_result event_send(const d_event &event)
{
window *wind;
window_event_result handled = window_event_result::ignored;
for (wind = window_get_front(); wind && handled == window_event_result::ignored; wind = window_get_prev(*wind))
if (window_is_visible(wind))
{
handled = window_send_event(*wind, event);
if (handled == window_event_result::deleted) // break away if necessary: window_send_event() could have closed wind by now
break;
if (window_is_modal(*wind))
break;
}
if (handled == window_event_result::ignored)
return call_default_handler(event);
return handled;
}
// Process the first event in queue, sending to the appropriate handler
// This is the new object-oriented system
// Uses the old system for now, but this may change
window_event_result event_process(void)
{
window *wind = window_get_front();
window_event_result highest_result;
timer_update();
highest_result = event_poll(); // send input events first
cmd_queue_process();
// Doing this prevents problems when a draw event can create a newmenu,
// such as some network menus when they report a problem
// Also checking for window_event_result::deleted in case a window was created
// with the same pointer value as the deleted one
if ((highest_result == window_event_result::deleted) || (window_get_front() != wind))
return highest_result;
const d_event event{EVENT_WINDOW_DRAW}; // then draw all visible windows
for (wind = window_get_first(); wind != nullptr;)
{
if (window_is_visible(wind))
{
auto prev = window_get_prev(*wind);
auto result = window_send_event(*wind, event);
highest_result = std::max(result, highest_result);
if (result == window_event_result::deleted)
{
if (!prev)
{
wind = window_get_first();
continue;
}
wind = prev; // take the previous window and get the next one from that (if prev isn't nullptr)
}
}
wind = window_get_next(*wind);
}
gr_flip();
return highest_result;
}
template <bool activate_focus>
static void event_change_focus()
{
const auto enable_grab = activate_focus && CGameCfg.Grabinput && likely(!CGameArg.DbgForbidConsoleGrab);
#if SDL_MAJOR_VERSION == 1
SDL_WM_GrabInput(enable_grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
#elif SDL_MAJOR_VERSION == 2
SDL_SetWindowGrab(g_pRebirthSDLMainWindow, enable_grab ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode(enable_grab ? SDL_TRUE : SDL_FALSE);
#endif
if (activate_focus)
mouse_disable_cursor();
else
mouse_enable_cursor();
}
void event_enable_focus()
{
event_change_focus<true>();
}
void event_disable_focus()
{
event_change_focus<false>();
}
#if DXX_USE_EDITOR
static fix64 last_event = 0;
void event_reset_idle_seconds()
{
last_event = timer_query();
}
fix event_get_idle_seconds()
{
return (timer_query() - last_event)/F1_0;
}
#endif
}