From 5fd8c2bbd52fb518f8ce61ffc1ae4c2ce4ff5efd Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Thu, 17 Sep 2015 21:59:15 +0200 Subject: [PATCH] Add keyboard input handler and move the view switching functionality there --- example/example.xcodeproj/project.pbxproj | 6 +++ src/Input/Keyboard.cpp | 64 +++++++++++++++++++++++ src/Input/Keyboard.h | 31 +++++++++++ src/ofxPiMapper.cpp | 13 ++++- src/ofxPiMapper.h | 13 +++++ 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/Input/Keyboard.cpp create mode 100644 src/Input/Keyboard.h diff --git a/example/example.xcodeproj/project.pbxproj b/example/example.xcodeproj/project.pbxproj index dbcabb9..5cb6294 100644 --- a/example/example.xcodeproj/project.pbxproj +++ b/example/example.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ 396427961BAB3DC900A06716 /* ProjectionMappingViewState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 396427941BAB3DC900A06716 /* ProjectionMappingViewState.cpp */; }; 396427991BAB3EA600A06716 /* SourceSelectionViewState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 396427971BAB3EA600A06716 /* SourceSelectionViewState.cpp */; }; 3964279C1BAB3F2F00A06716 /* TextureMappingViewState.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3964279A1BAB3F2F00A06716 /* TextureMappingViewState.cpp */; }; + 3964279F1BAB446A00A06716 /* Keyboard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3964279D1BAB446A00A06716 /* Keyboard.cpp */; }; 397EFC7C1A08E7680009286E /* ofxPiMapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 397EFC7B1A08E7680009286E /* ofxPiMapper.cpp */; }; 397EFC7F1A08FE720009286E /* FboSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 397EFC7D1A08FE720009286E /* FboSource.cpp */; }; 397EFC821A09047C0009286E /* CustomSource.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 397EFC801A09047C0009286E /* CustomSource.cpp */; }; @@ -168,6 +169,8 @@ 396427981BAB3EA600A06716 /* SourceSelectionViewState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SourceSelectionViewState.h; sourceTree = ""; }; 3964279A1BAB3F2F00A06716 /* TextureMappingViewState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextureMappingViewState.cpp; sourceTree = ""; }; 3964279B1BAB3F2F00A06716 /* TextureMappingViewState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextureMappingViewState.h; sourceTree = ""; }; + 3964279D1BAB446A00A06716 /* Keyboard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Keyboard.cpp; path = Input/Keyboard.cpp; sourceTree = ""; }; + 3964279E1BAB446A00A06716 /* Keyboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Keyboard.h; path = Input/Keyboard.h; sourceTree = ""; }; 397EFC7B1A08E7680009286E /* ofxPiMapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ofxPiMapper.cpp; sourceTree = ""; }; 397EFC7D1A08FE720009286E /* FboSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FboSource.cpp; sourceTree = ""; }; 397EFC7E1A08FE720009286E /* FboSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FboSource.h; sourceTree = ""; }; @@ -410,6 +413,8 @@ 396427931BAB3B4A00A06716 /* Input */ = { isa = PBXGroup; children = ( + 3964279E1BAB446A00A06716 /* Keyboard.h */, + 3964279D1BAB446A00A06716 /* Keyboard.cpp */, ); name = Input; sourceTree = ""; @@ -921,6 +926,7 @@ 39C1243C19EE9589005DF557 /* DirectoryFilter.cpp in Sources */, 3933D5D919BB87BD000ACA55 /* ofxSliderGroup.cpp in Sources */, 39C1243619EE9589005DF557 /* ByteBufferReader.cpp in Sources */, + 3964279F1BAB446A00A06716 /* Keyboard.cpp in Sources */, 39C1246A19F0AB96005DF557 /* QuadSurface.cpp in Sources */, 39C1247F19F187D5005DF557 /* RadioList.cpp in Sources */, 39C1243419EE9589005DF557 /* Base64Encoding.cpp in Sources */, diff --git a/src/Input/Keyboard.cpp b/src/Input/Keyboard.cpp new file mode 100644 index 0000000..dfb595f --- /dev/null +++ b/src/Input/Keyboard.cpp @@ -0,0 +1,64 @@ +#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 new file mode 100644 index 0000000..5335f4a --- /dev/null +++ b/src/Input/Keyboard.h @@ -0,0 +1,31 @@ +// 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 1a579ea..c4d3880 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -33,6 +33,7 @@ void ofxPiMapper::setup(){ ofLogNotice("ofxPiMapper") << "Done setting up"; _mainView = new ofx::piMapper::MainView(); + _keyboard = new ofx::piMapper::Keyboard(this); } void ofxPiMapper::stateSetup() { @@ -75,10 +76,11 @@ void ofxPiMapper::draw(){ } // draw void ofxPiMapper::keyPressed(ofKeyEventArgs &args){ - ofLogNotice("ofxPiMapper") << "Key pressed: " << static_cast(args.key); + //ofLogNotice("ofxPiMapper") << "Key pressed: " << static_cast(args.key); switch (args.key) { + /* case '1': if (gui.getMode() != ofx::piMapper::GuiMode::NONE) { cmdManager.exec(new ofx::piMapper::SetGuiModeCmd(&gui, @@ -103,6 +105,7 @@ void ofxPiMapper::keyPressed(ofKeyEventArgs &args){ ofx::piMapper::GuiMode::SOURCE_SELECTION)); } break; + */ case 'i': bShowInfo = !bShowInfo; break; @@ -178,6 +181,14 @@ void ofxPiMapper::addQuadSurface(){ } // addQuadSurface +ofx::piMapper::CmdManager & ofxPiMapper::getCmdManager() { + return cmdManager; +} + +ofx::piMapper::SurfaceManagerGui & ofxPiMapper::getGui() { + return gui; +} + ofx::piMapper::MediaServer& ofxPiMapper::getMediaServer(){ return mediaServer; } diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index d6233d4..180bf60 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -20,10 +20,17 @@ // Main view with state design pattern #include "MainView.h" +#include "Keyboard.h" #define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml" #define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml" +namespace ofx { + namespace piMapper { + class Keyboard; + } +} + class ofxPiMapper{ public: @@ -47,6 +54,10 @@ class ofxPiMapper{ // Toggle help / info void showInfo() { bShowInfo = true; }; void hideInfo() { bShowInfo = false; }; + + // Getters + ofx::piMapper::CmdManager & getCmdManager(); + ofx::piMapper::SurfaceManagerGui & getGui(); // Discussion: // Maybe these should be static as this would allow to access them @@ -66,5 +77,7 @@ class ofxPiMapper{ // maybe it should be as a separate layer? ofx::piMapper::SurfaceManagerGui gui; + ofx::piMapper::MainView * _mainView; + ofx::piMapper::Keyboard * _keyboard; }; \ No newline at end of file