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; namespace fs = boost::filesystem;
InputManager::InputManager(Window* window) : mWindow(window), InputManager* InputManager::mInstance = NULL;
mKeyboardInputConfig(NULL)
InputManager::InputManager() : mKeyboardInputConfig(NULL)
{ {
} }
@ -31,6 +32,14 @@ InputManager::~InputManager()
deinit(); deinit();
} }
InputManager* InputManager::getInstance()
{
if(!mInstance)
mInstance = new InputManager();
return mInstance;
}
void InputManager::init() void InputManager::init()
{ {
if(initialized()) if(initialized())
@ -155,7 +164,7 @@ InputConfig* InputManager::getInputConfigByDevice(int device)
return mInputConfigs[device]; return mInputConfigs[device];
} }
bool InputManager::parseEvent(const SDL_Event& ev) bool InputManager::parseEvent(const SDL_Event& ev, Window* window)
{ {
bool causedEvent = false; bool causedEvent = false;
switch(ev.type) switch(ev.type)
@ -173,7 +182,7 @@ bool InputManager::parseEvent(const SDL_Event& ev)
else else
normValue = -1; 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; causedEvent = true;
} }
@ -182,18 +191,17 @@ bool InputManager::parseEvent(const SDL_Event& ev)
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP: 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; return true;
case SDL_JOYHATMOTION: 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; return true;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if(ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive()) if(ev.key.keysym.sym == SDLK_BACKSPACE && SDL_IsTextInputActive())
{ {
if(mWindow->peekGui() != NULL) window->textInput("\b");
mWindow->peekGui()->textInput("\b");
} }
if(ev.key.repeat) if(ev.key.repeat)
@ -207,16 +215,15 @@ bool InputManager::parseEvent(const SDL_Event& ev)
return false; 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; return true;
case SDL_KEYUP: 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; return true;
case SDL_TEXTINPUT: case SDL_TEXTINPUT:
if(mWindow->peekGui() != NULL) window->textInput(ev.text.text);
mWindow->peekGui()->textInput(ev.text.text);
break; break;
case SDL_JOYDEVICEADDED: case SDL_JOYDEVICEADDED:

View file

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

View file

@ -13,7 +13,6 @@
Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10), Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10),
mAllowSleep(true) mAllowSleep(true)
{ {
mInputManager = new InputManager(this);
mViewController = new ViewController(this); mViewController = new ViewController(this);
mHelp = new HelpComponent(this); mHelp = new HelpComponent(this);
@ -33,7 +32,6 @@ Window::~Window()
delete peekGui(); delete peekGui();
delete mHelp; delete mHelp;
delete mInputManager;
} }
void Window::pushGui(GuiComponent* gui) void Window::pushGui(GuiComponent* gui)
@ -74,7 +72,7 @@ bool Window::init(unsigned int width, unsigned int height)
return false; return false;
} }
mInputManager->init(); InputManager::getInstance()->init();
ResourceManager::getInstance()->reloadAll(); ResourceManager::getInstance()->reloadAll();
@ -97,11 +95,17 @@ bool Window::init(unsigned int width, unsigned int height)
void Window::deinit() void Window::deinit()
{ {
mInputManager->deinit(); InputManager::getInstance()->deinit();
ResourceManager::getInstance()->unloadAll(); ResourceManager::getInstance()->unloadAll();
Renderer::deinit(); Renderer::deinit();
} }
void Window::textInput(const char* text)
{
if(peekGui())
peekGui()->textInput(text);
}
void Window::input(InputConfig* config, Input input) 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")) 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_ #define _WINDOW_H_
#include "GuiComponent.h" #include "GuiComponent.h"
#include "InputManager.h"
#include <vector> #include <vector>
#include "resources/Font.h" #include "resources/Font.h"
#include "InputManager.h"
class ViewController; class ViewController;
class HelpComponent; class HelpComponent;
@ -20,6 +20,7 @@ public:
void removeGui(GuiComponent* gui); void removeGui(GuiComponent* gui);
GuiComponent* peekGui(); GuiComponent* peekGui();
void textInput(const char* text);
void input(InputConfig* config, Input input); void input(InputConfig* config, Input input);
void update(int deltaTime); void update(int deltaTime);
void render(); void render();
@ -27,7 +28,6 @@ public:
bool init(unsigned int width = 0, unsigned int height = 0); bool init(unsigned int width = 0, unsigned int height = 0);
void deinit(); void deinit();
inline InputManager* getInputManager() { return mInputManager; }
inline ViewController* getViewController() { return mViewController; } inline ViewController* getViewController() { return mViewController; }
void normalizeNextUpdate(); void normalizeNextUpdate();
@ -40,7 +40,6 @@ public:
void setHelpPrompts(const std::vector<HelpPrompt>& prompts); void setHelpPrompts(const std::vector<HelpPrompt>& prompts);
private: private:
InputManager* mInputManager;
ViewController* mViewController; ViewController* mViewController;
HelpComponent* mHelp; HelpComponent* mHelp;
ImageComponent* mBackgroundOverlay; ImageComponent* mBackgroundOverlay;

View file

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

View file

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

View file

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