Changed InputManager to be a singleton.

Considering it has global state I don't know why it was being kept as part of the Window class.
This commit is contained in:
Aloshi 2014-04-18 13:07:32 -05:00
parent b968349864
commit 43bc4f5fe2
7 changed files with 41 additions and 28 deletions

View file

@ -21,8 +21,9 @@
namespace fs = boost::filesystem;
InputManager::InputManager(Window* window) : mWindow(window),
mKeyboardInputConfig(NULL)
InputManager* InputManager::mInstance = NULL;
InputManager::InputManager() : mKeyboardInputConfig(NULL)
{
}
@ -31,6 +32,14 @@ InputManager::~InputManager()
deinit();
}
InputManager* InputManager::getInstance()
{
if(!mInstance)
mInstance = new InputManager();
return mInstance;
}
void InputManager::init()
{
if(initialized())
@ -155,7 +164,7 @@ InputConfig* InputManager::getInputConfigByDevice(int device)
return mInputConfigs[device];
}
bool InputManager::parseEvent(const SDL_Event& ev)
bool InputManager::parseEvent(const SDL_Event& ev, Window* window)
{
bool causedEvent = false;
switch(ev.type)
@ -173,7 +182,7 @@ bool InputManager::parseEvent(const SDL_Event& ev)
else
normValue = -1;
mWindow->input(getInputConfigByDevice(ev.jaxis.which), Input(ev.jaxis.which, TYPE_AXIS, ev.jaxis.axis, normValue, false));
window->input(getInputConfigByDevice(ev.jaxis.which), Input(ev.jaxis.which, TYPE_AXIS, ev.jaxis.axis, normValue, false));
causedEvent = true;
}
@ -182,18 +191,17 @@ bool InputManager::parseEvent(const SDL_Event& ev)
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
mWindow->input(getInputConfigByDevice(ev.jbutton.which), Input(ev.jbutton.which, TYPE_BUTTON, ev.jbutton.button, ev.jbutton.state == SDL_PRESSED, false));
window->input(getInputConfigByDevice(ev.jbutton.which), Input(ev.jbutton.which, TYPE_BUTTON, ev.jbutton.button, ev.jbutton.state == SDL_PRESSED, false));
return true;
case SDL_JOYHATMOTION:
mWindow->input(getInputConfigByDevice(ev.jhat.which), Input(ev.jhat.which, TYPE_HAT, ev.jhat.hat, ev.jhat.value, false));
window->input(getInputConfigByDevice(ev.jhat.which), Input(ev.jhat.which, TYPE_HAT, ev.jhat.hat, ev.jhat.value, false));
return true;
case SDL_KEYDOWN:
if(ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive())
{
if(mWindow->peekGui() != NULL)
mWindow->peekGui()->textInput("\b");
window->textInput("\b");
}
if(ev.key.repeat)
@ -207,16 +215,15 @@ bool InputManager::parseEvent(const SDL_Event& ev)
return false;
}
mWindow->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 1, false));
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 1, false));
return true;
case SDL_KEYUP:
mWindow->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 0, false));
window->input(getInputConfigByDevice(DEVICE_KEYBOARD), Input(DEVICE_KEYBOARD, TYPE_KEY, ev.key.keysym.sym, 0, false));
return true;
case SDL_TEXTINPUT:
if(mWindow->peekGui() != NULL)
mWindow->peekGui()->textInput(ev.text.text);
window->textInput(ev.text.text);
break;
case SDL_JOYDEVICEADDED:

View file

@ -13,9 +13,11 @@ class Window;
class InputManager
{
private:
static const int DEADZONE = 23000;
InputManager();
Window* mWindow;
static InputManager* mInstance;
static const int DEADZONE = 23000;
void loadDefaultKBConfig();
@ -32,8 +34,9 @@ private:
bool loadInputConfig(InputConfig* config); // returns true if successfully loaded, false if not (or didn't exist)
public:
InputManager(Window* window);
~InputManager();
virtual ~InputManager();
static InputManager* getInstance();
void writeDeviceConfig(InputConfig* config);
static std::string getConfigPath();
@ -49,7 +52,7 @@ public:
InputConfig* getInputConfigByDevice(int deviceId);
bool parseEvent(const SDL_Event& ev);
bool parseEvent(const SDL_Event& ev, Window* window);
};
#endif

View file

@ -13,7 +13,6 @@
Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10),
mAllowSleep(true)
{
mInputManager = new InputManager(this);
mViewController = new ViewController(this);
mHelp = new HelpComponent(this);
@ -33,7 +32,6 @@ Window::~Window()
delete peekGui();
delete mHelp;
delete mInputManager;
}
void Window::pushGui(GuiComponent* gui)
@ -74,7 +72,7 @@ bool Window::init(unsigned int width, unsigned int height)
return false;
}
mInputManager->init();
InputManager::getInstance()->init();
ResourceManager::getInstance()->reloadAll();
@ -97,11 +95,17 @@ bool Window::init(unsigned int width, unsigned int height)
void Window::deinit()
{
mInputManager->deinit();
InputManager::getInstance()->deinit();
ResourceManager::getInstance()->unloadAll();
Renderer::deinit();
}
void Window::textInput(const char* text)
{
if(peekGui())
peekGui()->textInput(text);
}
void Window::input(InputConfig* config, Input input)
{
if(config->getDeviceId() == DEVICE_KEYBOARD && input.value && input.id == SDLK_g && SDL_GetModState() & KMOD_LCTRL && Settings::getInstance()->getBool("Debug"))

View file

@ -2,9 +2,9 @@
#define _WINDOW_H_
#include "GuiComponent.h"
#include "InputManager.h"
#include <vector>
#include "resources/Font.h"
#include "InputManager.h"
class ViewController;
class HelpComponent;
@ -20,6 +20,7 @@ public:
void removeGui(GuiComponent* gui);
GuiComponent* peekGui();
void textInput(const char* text);
void input(InputConfig* config, Input input);
void update(int deltaTime);
void render();
@ -27,7 +28,6 @@ public:
bool init(unsigned int width = 0, unsigned int height = 0);
void deinit();
inline InputManager* getInputManager() { return mInputManager; }
inline ViewController* getViewController() { return mViewController; }
void normalizeNextUpdate();
@ -40,7 +40,6 @@ public:
void setHelpPrompts(const std::vector<HelpPrompt>& prompts);
private:
InputManager* mInputManager;
ViewController* mViewController;
HelpComponent* mHelp;
ImageComponent* mBackgroundOverlay;

View file

@ -37,7 +37,7 @@ GuiDetectDevice::GuiDetectDevice(Window* window, bool firstRun) : GuiComponent(w
// device info
std::stringstream deviceInfo;
int numDevices = mWindow->getInputManager()->getNumJoysticks();
int numDevices = InputManager::getInstance()->getNumJoysticks();
if(numDevices > 0)
deviceInfo << numDevices << " GAMEPAD" << (numDevices > 1 ? "S" : "") << " DETECTED";

View file

@ -147,7 +147,7 @@ GuiInputConfig::GuiInputConfig(Window* window, InputConfig* target, bool reconfi
// buttons
std::vector< std::shared_ptr<ButtonComponent> > buttons;
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "OK", "ok", [this, okCallback] {
mWindow->getInputManager()->writeDeviceConfig(mTargetConfig); // save
InputManager::getInstance()->writeDeviceConfig(mTargetConfig); // save
if(okCallback)
okCallback();
delete this;

View file

@ -189,7 +189,7 @@ int main(int argc, char* argv[])
window.getViewController()->preload();
//choose which GUI to open depending on if an input configuration already exists
if(fs::exists(InputManager::getConfigPath()) && window.getInputManager()->getNumConfiguredDevices() > 0)
if(fs::exists(InputManager::getConfigPath()) && InputManager::getInstance()->getNumConfiguredDevices() > 0)
{
window.getViewController()->goToStart();
}else{
@ -221,7 +221,7 @@ int main(int argc, char* argv[])
case SDL_TEXTEDITING:
case SDL_JOYDEVICEADDED:
case SDL_JOYDEVICEREMOVED:
if(window.getInputManager()->parseEvent(event))
if(InputManager::getInstance()->parseEvent(event, &window))
{
sleeping = false;
timeSinceLastEvent = 0;