From 05396890ebdc948eef2734061c04538cd94fcb61 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sat, 10 Oct 2015 14:26:11 +0300 Subject: [PATCH] Move state change keyboard mappings to Application to handle --- src/Application/Application.cpp | 50 +++++++++++++++++++- src/Application/Application.h | 9 +++- src/Application/ApplicationBaseState.h | 3 ++ src/Input/Keyboard.cpp | 64 -------------------------- src/Input/Keyboard.h | 31 ------------- src/ofxPiMapper.cpp | 3 +- src/ofxPiMapper.h | 2 - 7 files changed, 61 insertions(+), 101 deletions(-) delete mode 100644 src/Input/Keyboard.cpp delete mode 100644 src/Input/Keyboard.h diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp index 8f169a8..456647f 100644 --- a/src/Application/Application.cpp +++ b/src/Application/Application.cpp @@ -4,14 +4,62 @@ namespace ofx { namespace piMapper { - Application::Application(){ + Application::Application(ofxPiMapper * opm) { + _ofxPiMapper = opm; setState(PresentationState::instance()); + ofAddListener(ofEvents().keyPressed, this, &Application::onKeyPressed); + } + + Application::~Application() { + _ofxPiMapper = 0; + setState(0); + ofRemoveListener(ofEvents().keyPressed, this, &Application::onKeyPressed); } void Application::draw(){ _state->draw(this); } + // Here we handle application state changes only + void Application::onKeyPressed(ofKeyEventArgs & args) { + switch (args.key) { + case '1': + ofLogNotice("Application::onKeyPressed()") << "Switching to Presentation State"; + if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::NONE) { + _ofxPiMapper->getCmdManager().exec( + new ofx::piMapper::SetGuiModeCmd(&_ofxPiMapper->getGui(), + ofx::piMapper::GuiMode::NONE)); + } + break; + case '2': + ofLogNotice("Application::onKeyPressed()") << "Switching to Projection Mapping State"; + if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::PROJECTION_MAPPING) { + _ofxPiMapper->getCmdManager().exec( + new ofx::piMapper::SetGuiModeCmd(&_ofxPiMapper->getGui(), + ofx::piMapper::GuiMode::PROJECTION_MAPPING)); + } + break; + case '3': + ofLogNotice("Application::onKeyPressed()") << "Switching to Source Selection State"; + if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::SOURCE_SELECTION) { + _ofxPiMapper->getCmdManager().exec( + new ofx::piMapper::SetGuiModeCmd(&_ofxPiMapper->getGui(), + ofx::piMapper::GuiMode::SOURCE_SELECTION)); + } + break; + case '4': + ofLogNotice("Application::onKeyPressed()") << "Switching to Texture Mapping State"; + if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::TEXTURE_MAPPING) { + _ofxPiMapper->getCmdManager().exec( + new ofx::piMapper::SetGuiModeCmd(&_ofxPiMapper->getGui(), + ofx::piMapper::GuiMode::TEXTURE_MAPPING)); + } + break; + default: + break; + } + } + void Application::setState(ApplicationBaseState * st){ _state = st; } diff --git a/src/Application/Application.h b/src/Application/Application.h index 70a3f5f..916b3a1 100644 --- a/src/Application/Application.h +++ b/src/Application/Application.h @@ -3,6 +3,9 @@ #include "ofEvents.h" #include "ofLog.h" #include "ApplicationBaseState.h" +#include "ofxPiMapper.h" + +class ofxPiMapper; namespace ofx { namespace piMapper { @@ -11,8 +14,11 @@ namespace ofx { class Application { public: - Application(); + Application(ofxPiMapper * opm); + ~Application(); + void draw(); + void onKeyPressed(ofKeyEventArgs & args); protected: void setState(ApplicationBaseState * st); @@ -20,6 +26,7 @@ namespace ofx { private: friend class ApplicationBaseState; ApplicationBaseState * _state; + ofxPiMapper * _ofxPiMapper; }; } // namespace piMapper diff --git a/src/Application/ApplicationBaseState.h b/src/Application/ApplicationBaseState.h index c396390..c30d5f3 100644 --- a/src/Application/ApplicationBaseState.h +++ b/src/Application/ApplicationBaseState.h @@ -12,6 +12,9 @@ namespace ofx { public: virtual void draw(Application * app){}; virtual void setState(Application * app, ApplicationBaseState * st); + + // Event handler virtual methods + virtual void keyPressed(){}; }; } // namespace piMapper diff --git a/src/Input/Keyboard.cpp b/src/Input/Keyboard.cpp deleted file mode 100644 index dfb595f..0000000 --- a/src/Input/Keyboard.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "Keyboard.h" - -namespace ofx { - namespace piMapper { - - Keyboard::Keyboard(ofxPiMapper * opm) { - _ofxPiMapper = opm; - ofLogNotice("Keyboard event handler"); - ofAddListener(ofEvents().keyPressed, this, &Keyboard::onKeyPressed); - } - - Keyboard::~Keyboard() { - _ofxPiMapper = 0; - ofRemoveListener(ofEvents().keyPressed, this, &Keyboard::onKeyPressed); - } - - void Keyboard::onKeyPressed(ofKeyEventArgs & args) { - ofLogNotice("Keyboard::onKeyPressed"); - ofLog() << args.keycode; - - // Choose what command to create here - // depending on the key pressed - switch (args.key) { - case '1': - ofLogNotice("Keyboard::onKeyPressed") << "1"; - if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::NONE) { - _ofxPiMapper->getCmdManager().exec( - new ofx::piMapper::SetGuiModeCmd(&_ofxPiMapper->getGui(), - ofx::piMapper::GuiMode::NONE)); - } - break; - case '2': - ofLogNotice("Keyboard::onKeyPressed") << "2"; - if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::PROJECTION_MAPPING) { - _ofxPiMapper->getCmdManager().exec(new ofx::piMapper::SetGuiModeCmd( - &_ofxPiMapper->getGui(), - ofx::piMapper::GuiMode::PROJECTION_MAPPING)); - } - break; - case '3': - ofLogNotice("Keyboard::onKeyPressed") << "3"; - if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::SOURCE_SELECTION) { - _ofxPiMapper->getCmdManager().exec(new ofx::piMapper::SetGuiModeCmd( - &_ofxPiMapper->getGui(), - ofx::piMapper::GuiMode::SOURCE_SELECTION)); - } - break; - case '4': - ofLogNotice("Keyboard::onKeyPressed") << "4"; - if (_ofxPiMapper->getGui().getMode() != ofx::piMapper::GuiMode::TEXTURE_MAPPING) { - _ofxPiMapper->getCmdManager().exec(new ofx::piMapper::SetGuiModeCmd( - &_ofxPiMapper->getGui(), - ofx::piMapper::GuiMode::TEXTURE_MAPPING)); - } - break; - default: - // TODO: Forward it to the application state for it to decide. - ofLogNotice("Keyboard::onKeyPressed") << "default case"; - break; - } - } - - } -} \ No newline at end of file diff --git a/src/Input/Keyboard.h b/src/Input/Keyboard.h deleted file mode 100644 index 5335f4a..0000000 --- a/src/Input/Keyboard.h +++ /dev/null @@ -1,31 +0,0 @@ -// Keyboard -// Keyboard input handler -// Creates commands on keyboard input and forwards them to the CommandManager -// Created by Krisjanis Rijnieks on 2015-09-17 -#pragma once - -#include "ofEvents.h" -#include "ofEventUtils.h" -#include "ofLog.h" -#include "ofxPiMapper.h" -#include "SetGuiModeCmd.h" -#include "GuiMode.h" - -class ofxPiMapper; - -namespace ofx { - namespace piMapper { - - class Keyboard { - public: - Keyboard(ofxPiMapper * opm); - ~Keyboard(); - - void onKeyPressed(ofKeyEventArgs & args); - - private: - ofxPiMapper * _ofxPiMapper; - }; - - } -} \ No newline at end of file diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index 329ebed..1ef3828 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -32,8 +32,7 @@ void ofxPiMapper::setup(){ ofLogNotice("ofxPiMapper") << "Done setting up"; - _application = new ofx::piMapper::Application(); - _keyboard = new ofx::piMapper::Keyboard(this); + _application = new ofx::piMapper::Application(this); } void ofxPiMapper::stateSetup() { diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index 9a560e1..4179cf8 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -14,7 +14,6 @@ // Main view with state design pattern #include "Application.h" // Main application entry point -#include "Keyboard.h" #define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml" #define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml" @@ -73,5 +72,4 @@ class ofxPiMapper{ ofx::piMapper::Application * _application; - ofx::piMapper::Keyboard * _keyboard; }; \ No newline at end of file