Skip to content

Commit

Permalink
Refactor|InputSystem|Client: Moved InputDevices and ddevent_t queue i…
Browse files Browse the repository at this point in the history
…nto InputSystem
  • Loading branch information
danij-deng committed Nov 2, 2014
1 parent 1526ba0 commit 3cc8fae
Show file tree
Hide file tree
Showing 21 changed files with 1,151 additions and 1,133 deletions.
1 change: 1 addition & 0 deletions doomsday/client/include/ui/b_util.h
Expand Up @@ -24,6 +24,7 @@
#include "dd_input.h"

struct bcontext_t;
class InputDevice;

// Event Binding Toggle State
enum ebstate_t
Expand Down
98 changes: 0 additions & 98 deletions doomsday/client/include/ui/dd_input.h
Expand Up @@ -24,8 +24,6 @@
#include <de/String>
#include "api_event.h"

class InputDevice;

// Input device identifiers:
enum
{
Expand Down Expand Up @@ -113,102 +111,6 @@ struct ddevent_t

void I_ConsoleRegister();

/**
* Initialize the virtual input devices.
*
* @note There need not be actual physical devices available in order to use
* these state tables.
*/
void I_InitAllDevices();

/**
* Free the memory allocated for the input devices.
*/
void I_ShutdownAllDevices();

/**
* Lookup an InputDevice by it's unique @a id.
*/
InputDevice &I_Device(int id);

/**
* Lookup an InputDevice by it's unique @a id.
*
* @return Pointer to the associated InputDevice; otherwise @c nullptr.
*/
InputDevice *I_DevicePtr(int id);

/**
* Iterate through all the InputDevices.
*/
de::LoopResult I_ForAllDevices(std::function<de::LoopResult (InputDevice &)> func);

/**
* Initializes the key mappings to the default values.
*/
void I_InitKeyMappings();

/**
* Checks the current keyboard state, generates input events based on pressed/held
* keys and posts them.
*/
void I_ReadKeyboard();

/**
* Checks the current mouse state (axis, buttons and wheel).
* Generates events and mickeys and posts them.
*/
void I_ReadMouse();

/**
* Checks the current joystick state (axis, sliders, hat and buttons).
* Generates events and posts them. Axis clamps and dead zone is done
* here.
*/
void I_ReadJoystick();

void I_ReadHeadTracker();

/**
* Clear the input event queue.
*/
void I_ClearEvents();

bool I_IgnoreEvents(bool yes = true);

/**
* Process all incoming input for the given timestamp.
* This is called only in the main thread, and also from the busy loop.
*
* This gets called at least 35 times per second. Usually more frequently
* than that.
*/
void I_ProcessEvents(timespan_t ticLength);

void I_ProcessSharpEvents(timespan_t ticLength);

/**
* @param ev A copy is made.
*/
void I_PostEvent(ddevent_t *ev);

bool I_ConvertEvent(ddevent_t const *ddEvent, event_t *ev);

/**
* Converts a libcore Event into an old-fashioned ddevent_t.
*
* @param event Event instance.
* @param ddEvent ddevent_t instance.
*/
void I_ConvertEvent(de::Event const &event, ddevent_t *ddEvent);

/**
* Update the input device state table.
*/
void I_TrackInput(ddevent_t *ev);

bool I_ShiftDown();

#ifdef DENG2_DEBUG
/**
* Render a visual representation of the current state of all input devices.
Expand Down
68 changes: 68 additions & 0 deletions doomsday/client/include/ui/inputsystem.h
Expand Up @@ -20,9 +20,14 @@
#ifndef CLIENT_INPUTSYSTEM_H
#define CLIENT_INPUTSYSTEM_H

#include <functional>
#include <de/types.h>
#include <de/System>
#include "dd_input.h" // ddevent_t
#include "SettingsRegister"

class InputDevice;

/**
* Input devices and events. @ingroup ui
*
Expand All @@ -38,6 +43,69 @@ class InputSystem : public de::System
// System.
void timeChanged(de::Clock const &);

/**
* Lookup an InputDevice by it's unique @a id.
*/
InputDevice &device(int id) const;

/**
* Lookup an InputDevice by it's unique @a id.
*
* @return Pointer to the associated InputDevice; otherwise @c nullptr.
*/
InputDevice *devicePtr(int id) const;

/**
* Iterate through all the InputDevices.
*/
de::LoopResult forAllDevices(std::function<de::LoopResult (InputDevice &)> func) const;

/**
* Returns @c true if the shift key of the keyboard is thought to be down.
* @todo: Refactor away
*/
bool shiftDown() const;

public: /// Event processing --------------------------------------------------
/**
* Clear the input event queue.
*/
void clearEvents();

bool ignoreEvents(bool yes = true);

/**
* @param ev A copy is made.
*/
void postEvent(ddevent_t *ev);

/**
* Process all incoming input for the given timestamp.
* This is called only in the main thread, and also from the busy loop.
*
* This gets called at least 35 times per second. Usually more frequently
* than that.
*/
void processEvents(timespan_t ticLength);

void processSharpEvents(timespan_t ticLength);

/**
* Update the input devices.
*/
void trackEvent(ddevent_t *ev);

public:
static bool convertEvent(ddevent_t const *ddEvent, event_t *ev);

/**
* Converts a libcore Event into an old-fashioned ddevent_t.
*
* @param event Event instance.
* @param ddEvent ddevent_t instance.
*/
static void convertEvent(de::Event const &event, ddevent_t *ddEvent);

public:
/**
* Register the console commands and variables of this module.
Expand Down
8 changes: 4 additions & 4 deletions doomsday/client/src/busymode.cpp
Expand Up @@ -267,7 +267,7 @@ static void preBusySetup(int initialMode)
Con_TransitionConfigure();
}

busyWasIgnoringInput = I_IgnoreEvents();
busyWasIgnoringInput = ClientApp::inputSystem().ignoreEvents();

// Load any resources needed beforehand.
//BusyVisual_PrepareResources();
Expand All @@ -290,7 +290,7 @@ static void postBusyCleanup()
{
#ifdef __CLIENT__
// Discard input events so that any and all accumulated input events are ignored.
I_IgnoreEvents(busyWasIgnoringInput);
ClientApp::inputSystem().ignoreEvents(busyWasIgnoringInput);
DD_ResetTimer();

//BusyVisual_ReleaseTextures();
Expand Down Expand Up @@ -471,8 +471,8 @@ void BusyMode_Loop(void)
timespan_t oldTime;

// Post and discard all input events.
I_ProcessEvents(0);
I_ProcessSharpEvents(0);
ClientApp::inputSystem().processEvents(0);
ClientApp::inputSystem().processSharpEvents(0);

if(canUpload)
{
Expand Down
7 changes: 4 additions & 3 deletions doomsday/client/src/dd_loop.cpp
Expand Up @@ -38,6 +38,7 @@
#endif

#ifdef __CLIENT__
# include "clientapp.h"
# include "ui/busyvisual.h"
# include "ui/clientwindow.h"
#endif
Expand Down Expand Up @@ -407,11 +408,11 @@ void Loop_RunTics(void)

#ifdef __CLIENT__
// Process input events.
I_ProcessEvents(ticLength);
ClientApp::inputSystem().processEvents(ticLength);
if(!processSharpEventsAfterTickers)
{
// We are allowed to process sharp events before tickers.
I_ProcessSharpEvents(ticLength);
ClientApp::inputSystem().processSharpEvents(ticLength);
}
#endif

Expand All @@ -422,7 +423,7 @@ void Loop_RunTics(void)
if(processSharpEventsAfterTickers)
{
// This is done after tickers for compatibility with ye olde game logic.
I_ProcessSharpEvents(ticLength);
ClientApp::inputSystem().processSharpEvents(ticLength);
}
#endif

Expand Down
8 changes: 2 additions & 6 deletions doomsday/client/src/dd_main.cpp
Expand Up @@ -1549,10 +1549,6 @@ bool App_ChangeGame(Game &game, bool allowReload)
Con_InitDatabases();
consoleRegister();

#ifdef __CLIENT__
I_InitAllDevices();
#endif

R_InitSvgs();

#ifdef __CLIENT__
Expand Down Expand Up @@ -1699,7 +1695,7 @@ bool App_ChangeGame(Game &game, bool allowReload)
* @note Only necessary here because we might not have been able to use
* busy mode (which would normally do this for us on end).
*/
I_ClearEvents();
ClientApp::inputSystem().clearEvents();

if(!App_GameLoaded())
{
Expand Down Expand Up @@ -2468,7 +2464,7 @@ int DD_GetInteger(int ddvalue)
{
#ifdef __CLIENT__
case DD_SHIFT_DOWN:
return int(I_ShiftDown());
return int(ClientApp::inputSystem().shiftDown());

case DD_WINDOW_WIDTH:
return DENG_GAMEVIEW_WIDTH;
Expand Down
5 changes: 4 additions & 1 deletion doomsday/client/src/sys_system.cpp
Expand Up @@ -34,6 +34,9 @@
#include "de_system.h"
#include "de_graphics.h"
#include "de_misc.h"
#ifdef __CLIENT__
# include "clientapp.h"
#endif

#include "dd_main.h"
#include "dd_loop.h"
Expand Down Expand Up @@ -132,7 +135,7 @@ void Sys_Shutdown(void)
S_Shutdown();
#ifdef __CLIENT__
GL_Shutdown();
I_ClearEvents();
ClientApp::inputSystem().clearEvents();
#endif

DD_DestroyGames();
Expand Down
14 changes: 10 additions & 4 deletions doomsday/client/src/ui/b_command.cpp
Expand Up @@ -22,6 +22,7 @@

#include <de/memory.h>
#include <de/vector1.h>
#include "clientapp.h"
#include "world/p_players.h" // P_ConsoleToLocal
#include "ui/b_main.h"
#include "ui/inputdevice.h"
Expand All @@ -34,6 +35,11 @@

using namespace de;

static inline InputSystem &inputSys()
{
return ClientApp::inputSystem();
}

void B_InitCommandBindingList(evbinding_t *listRoot)
{
DENG2_ASSERT(listRoot);
Expand Down Expand Up @@ -141,7 +147,7 @@ static dd_bool B_ParseEvent(evbinding_t *eb, char const *desc)

// Next part defined button, axis, or hat.
desc = Str_CopyDelim(str, desc, '-');
if(!B_ParseJoystickTypeAndId(I_Device(eb->device), Str_Text(str), &eb->type, &eb->id))
if(!B_ParseJoystickTypeAndId(inputSys().device(eb->device), Str_Text(str), &eb->type, &eb->id))
{
return false;
}
Expand Down Expand Up @@ -354,7 +360,7 @@ Action *EventBinding_ActionForEvent(evbinding_t *eb, ddevent_t const *event,
InputDevice *dev = nullptr;
if(event->type != E_SYMBOLIC)
{
dev = I_DevicePtr(eb->device);
dev = inputSys().devicePtr(eb->device);
if(!dev || !dev->isActive())
{
// The device is not active, there is no way this could get executed.
Expand Down Expand Up @@ -420,7 +426,7 @@ Action *EventBinding_ActionForEvent(evbinding_t *eb, ddevent_t const *event,

// Is the position as required?
if(!B_CheckAxisPos(eb->state, eb->pos,
I_Device(event->device).axis(event->axis.id)
inputSys().device(event->device).axis(event->axis.id)
.translateRealPosition(event->axis.pos)))
return nullptr;
break;
Expand Down Expand Up @@ -468,7 +474,7 @@ void B_EventBindingToString(evbinding_t const *eb, ddstring_t *str)
DENG2_ASSERT(eb && str);

Str_Clear(str);
B_AppendDeviceDescToString(I_Device(eb->device), eb->type, eb->id, str);
B_AppendDeviceDescToString(inputSys().device(eb->device), eb->type, eb->id, str);

switch(eb->type)
{
Expand Down

0 comments on commit 3cc8fae

Please sign in to comment.