From 7631497f8e824df8fbaba29e6c744f33af5b11bd Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Thu, 14 May 2015 17:20:57 +0300 Subject: [PATCH] Rename executeCommand to exec and add log notice if nothing to undo --- src/Commands/BaseCommand.h | 11 ++++++++--- src/Commands/CommandManager.cpp | 10 +++++----- src/Commands/CommandManager.h | 15 ++++++--------- src/Commands/TestCommand.cpp | 2 +- src/Commands/TestCommand.h | 2 +- src/Commands/TestUndoCommand.cpp | 2 +- src/Commands/TestUndoCommand.h | 2 +- src/ofxPiMapper.cpp | 6 +++--- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/Commands/BaseCommand.h b/src/Commands/BaseCommand.h index a539754..59a1255 100644 --- a/src/Commands/BaseCommand.h +++ b/src/Commands/BaseCommand.h @@ -3,6 +3,9 @@ // Good example // http://gameprogrammingpatterns.com/command.html +// This file contains declarations of both: +// - non-undoable command base class +// - undoable command base class #pragma once @@ -11,19 +14,21 @@ namespace ofx{ // Base class for all commands class BaseCommand{ - public: virtual ~BaseCommand(){}; - virtual void execute() = 0; + virtual void exec() = 0; + + // By default a command is not undo virtual bool isUndoable(){return false;} protected: + // In order to avoid using this class directly, + // we make the constructor protected. BaseCommand(){}; }; // Base class for all undoable commands class BaseUndoableCommand : public BaseCommand{ - public: virtual void undo() = 0; virtual bool isUndoable(){return true;} diff --git a/src/Commands/CommandManager.cpp b/src/Commands/CommandManager.cpp index 4bb60b9..b4a510a 100644 --- a/src/Commands/CommandManager.cpp +++ b/src/Commands/CommandManager.cpp @@ -3,8 +3,8 @@ namespace ofx{ namespace piMapper{ - void CommandManager::executeCommand(BaseCommand * cmd){ - cmd->execute(); + void CommandManager::exec(BaseCommand * cmd){ + cmd->exec(); if (cmd->isUndoable()){ commandStack.push_back(static_cast(cmd)); } @@ -15,11 +15,11 @@ namespace ofx{ BaseUndoableCommand * cmd = commandStack.back(); cmd->undo(); - // Delete last command for now - // TODO: Enable redo func and that means we do not destroy last command, - // we move the stack pointer instead. + // Delete last command now, change this when implementing redo. delete commandStack.back(); commandStack.pop_back(); + } else { + ofLogNotice("CommandManager", "Nothing to undo"); } } diff --git a/src/Commands/CommandManager.h b/src/Commands/CommandManager.h index 7a22d21..56512d9 100644 --- a/src/Commands/CommandManager.h +++ b/src/Commands/CommandManager.h @@ -1,22 +1,19 @@ #pragma once -#import +#import #import "BaseCommand.h" +#import "ofLog.h" namespace ofx{ namespace piMapper{ - + class CommandManager{ - public: - void executeCommand(BaseCommand * cmd); + void exec(BaseCommand * cmd); void undo(); - // TODO: Add redo - + private: - std::deque commandStack; - // TODO: Add redo stack - // Maybe use vector instead of deque... + std::vector commandStack; }; } // namespace piMapper diff --git a/src/Commands/TestCommand.cpp b/src/Commands/TestCommand.cpp index b523303..98ec752 100644 --- a/src/Commands/TestCommand.cpp +++ b/src/Commands/TestCommand.cpp @@ -7,7 +7,7 @@ namespace ofx{ _application = a; } - void TestCommand::execute(){ + void TestCommand::exec(){ string name = "Hugo"; _application->testCommand(name); } diff --git a/src/Commands/TestCommand.h b/src/Commands/TestCommand.h index 7e05c7e..747e062 100644 --- a/src/Commands/TestCommand.h +++ b/src/Commands/TestCommand.h @@ -16,7 +16,7 @@ namespace ofx{ public: TestCommand(ofxPiMapper * a); - void execute(); + void exec(); private: ofxPiMapper * _application; diff --git a/src/Commands/TestUndoCommand.cpp b/src/Commands/TestUndoCommand.cpp index fea4e74..6434719 100644 --- a/src/Commands/TestUndoCommand.cpp +++ b/src/Commands/TestUndoCommand.cpp @@ -7,7 +7,7 @@ namespace ofx{ _application = a; } - void TestUndoCommand::execute(){ + void TestUndoCommand::exec(){ increase = 2; _application->testUndoableCommand(increase); } diff --git a/src/Commands/TestUndoCommand.h b/src/Commands/TestUndoCommand.h index 08d64a7..283a4ad 100644 --- a/src/Commands/TestUndoCommand.h +++ b/src/Commands/TestUndoCommand.h @@ -14,7 +14,7 @@ namespace ofx{ public: TestUndoCommand(ofxPiMapper * a); - void execute(); + void exec(); void undo(); private: diff --git a/src/ofxPiMapper.cpp b/src/ofxPiMapper.cpp index 2198786..fe9d013 100644 --- a/src/ofxPiMapper.cpp +++ b/src/ofxPiMapper.cpp @@ -104,12 +104,12 @@ void ofxPiMapper::keyPressed(ofKeyEventArgs &args){ break; // TODO: Remove the following case when Command test done. case '9': - commandManager.executeCommand(new ofx::piMapper::TestUndoCommand((ofxPiMapper *)this)); + commandManager.exec(new ofx::piMapper::TestUndoCommand((ofxPiMapper *)this)); break; case '0': - commandManager.executeCommand(new ofx::piMapper::TestCommand((ofxPiMapper *)this)); + commandManager.exec(new ofx::piMapper::TestCommand((ofxPiMapper *)this)); break; - case 'u': + case 'z': // undo commandManager.undo(); break;