From 43bc4f5fe239692f0bced0cc80e3924650314e10 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Fri, 18 Apr 2014 13:07:32 -0500 Subject: [PATCH] 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. --- src/InputManager.cpp | 31 +++++++++++++++++++------------ src/InputManager.h | 13 ++++++++----- src/Window.cpp | 12 ++++++++---- src/Window.h | 5 ++--- src/guis/GuiDetectDevice.cpp | 2 +- src/guis/GuiInputConfig.cpp | 2 +- src/main.cpp | 4 ++-- 7 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/InputManager.cpp b/src/InputManager.cpp index de676e988..e1816ed57 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -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: diff --git a/src/InputManager.h b/src/InputManager.h index a3fc0101d..5ec8d8848 100644 --- a/src/InputManager.h +++ b/src/InputManager.h @@ -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 diff --git a/src/Window.cpp b/src/Window.cpp index 279bf29a7..dc38877b7 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -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")) diff --git a/src/Window.h b/src/Window.h index c71b8d742..5c87e9e75 100644 --- a/src/Window.h +++ b/src/Window.h @@ -2,9 +2,9 @@ #define _WINDOW_H_ #include "GuiComponent.h" -#include "InputManager.h" #include #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& prompts); private: - InputManager* mInputManager; ViewController* mViewController; HelpComponent* mHelp; ImageComponent* mBackgroundOverlay; diff --git a/src/guis/GuiDetectDevice.cpp b/src/guis/GuiDetectDevice.cpp index 1bcb09db5..7825270fa 100644 --- a/src/guis/GuiDetectDevice.cpp +++ b/src/guis/GuiDetectDevice.cpp @@ -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"; diff --git a/src/guis/GuiInputConfig.cpp b/src/guis/GuiInputConfig.cpp index 14f0e6e26..10c56bdf7 100644 --- a/src/guis/GuiInputConfig.cpp +++ b/src/guis/GuiInputConfig.cpp @@ -147,7 +147,7 @@ GuiInputConfig::GuiInputConfig(Window* window, InputConfig* target, bool reconfi // buttons std::vector< std::shared_ptr > buttons; buttons.push_back(std::make_shared(mWindow, "OK", "ok", [this, okCallback] { - mWindow->getInputManager()->writeDeviceConfig(mTargetConfig); // save + InputManager::getInstance()->writeDeviceConfig(mTargetConfig); // save if(okCallback) okCallback(); delete this; diff --git a/src/main.cpp b/src/main.cpp index 96df4d39a..d8a78ac0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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;