Browse Source

Rename executeCommand to exec and add log notice if nothing to undo

master
Krisjanis Rijnieks 10 years ago
parent
commit
7631497f8e
  1. 11
      src/Commands/BaseCommand.h
  2. 10
      src/Commands/CommandManager.cpp
  3. 15
      src/Commands/CommandManager.h
  4. 2
      src/Commands/TestCommand.cpp
  5. 2
      src/Commands/TestCommand.h
  6. 2
      src/Commands/TestUndoCommand.cpp
  7. 2
      src/Commands/TestUndoCommand.h
  8. 6
      src/ofxPiMapper.cpp

11
src/Commands/BaseCommand.h

@ -3,6 +3,9 @@
// Good example // Good example
// http://gameprogrammingpatterns.com/command.html // http://gameprogrammingpatterns.com/command.html
// This file contains declarations of both:
// - non-undoable command base class
// - undoable command base class
#pragma once #pragma once
@ -11,19 +14,21 @@ namespace ofx{
// Base class for all commands // Base class for all commands
class BaseCommand{ class BaseCommand{
public: public:
virtual ~BaseCommand(){}; virtual ~BaseCommand(){};
virtual void execute() = 0; virtual void exec() = 0;
// By default a command is not undo
virtual bool isUndoable(){return false;} virtual bool isUndoable(){return false;}
protected: protected:
// In order to avoid using this class directly,
// we make the constructor protected.
BaseCommand(){}; BaseCommand(){};
}; };
// Base class for all undoable commands // Base class for all undoable commands
class BaseUndoableCommand : public BaseCommand{ class BaseUndoableCommand : public BaseCommand{
public: public:
virtual void undo() = 0; virtual void undo() = 0;
virtual bool isUndoable(){return true;} virtual bool isUndoable(){return true;}

10
src/Commands/CommandManager.cpp

@ -3,8 +3,8 @@
namespace ofx{ namespace ofx{
namespace piMapper{ namespace piMapper{
void CommandManager::executeCommand(BaseCommand * cmd){ void CommandManager::exec(BaseCommand * cmd){
cmd->execute(); cmd->exec();
if (cmd->isUndoable()){ if (cmd->isUndoable()){
commandStack.push_back(static_cast<BaseUndoableCommand *>(cmd)); commandStack.push_back(static_cast<BaseUndoableCommand *>(cmd));
} }
@ -15,11 +15,11 @@ namespace ofx{
BaseUndoableCommand * cmd = commandStack.back(); BaseUndoableCommand * cmd = commandStack.back();
cmd->undo(); cmd->undo();
// Delete last command for now // Delete last command now, change this when implementing redo.
// TODO: Enable redo func and that means we do not destroy last command,
// we move the stack pointer instead.
delete commandStack.back(); delete commandStack.back();
commandStack.pop_back(); commandStack.pop_back();
} else {
ofLogNotice("CommandManager", "Nothing to undo");
} }
} }

15
src/Commands/CommandManager.h

@ -1,22 +1,19 @@
#pragma once #pragma once
#import <deque> #import <vector>
#import "BaseCommand.h" #import "BaseCommand.h"
#import "ofLog.h"
namespace ofx{ namespace ofx{
namespace piMapper{ namespace piMapper{
class CommandManager{ class CommandManager{
public: public:
void executeCommand(BaseCommand * cmd); void exec(BaseCommand * cmd);
void undo(); void undo();
// TODO: Add redo
private: private:
std::deque<BaseUndoableCommand *> commandStack; std::vector<BaseUndoableCommand *> commandStack;
// TODO: Add redo stack
// Maybe use vector instead of deque...
}; };
} // namespace piMapper } // namespace piMapper

2
src/Commands/TestCommand.cpp

@ -7,7 +7,7 @@ namespace ofx{
_application = a; _application = a;
} }
void TestCommand::execute(){ void TestCommand::exec(){
string name = "Hugo"; string name = "Hugo";
_application->testCommand(name); _application->testCommand(name);
} }

2
src/Commands/TestCommand.h

@ -16,7 +16,7 @@ namespace ofx{
public: public:
TestCommand(ofxPiMapper * a); TestCommand(ofxPiMapper * a);
void execute(); void exec();
private: private:
ofxPiMapper * _application; ofxPiMapper * _application;

2
src/Commands/TestUndoCommand.cpp

@ -7,7 +7,7 @@ namespace ofx{
_application = a; _application = a;
} }
void TestUndoCommand::execute(){ void TestUndoCommand::exec(){
increase = 2; increase = 2;
_application->testUndoableCommand(increase); _application->testUndoableCommand(increase);
} }

2
src/Commands/TestUndoCommand.h

@ -14,7 +14,7 @@ namespace ofx{
public: public:
TestUndoCommand(ofxPiMapper * a); TestUndoCommand(ofxPiMapper * a);
void execute(); void exec();
void undo(); void undo();
private: private:

6
src/ofxPiMapper.cpp

@ -104,12 +104,12 @@ void ofxPiMapper::keyPressed(ofKeyEventArgs &args){
break; break;
// TODO: Remove the following case when Command test done. // TODO: Remove the following case when Command test done.
case '9': case '9':
commandManager.executeCommand(new ofx::piMapper::TestUndoCommand((ofxPiMapper *)this)); commandManager.exec(new ofx::piMapper::TestUndoCommand((ofxPiMapper *)this));
break; break;
case '0': case '0':
commandManager.executeCommand(new ofx::piMapper::TestCommand((ofxPiMapper *)this)); commandManager.exec(new ofx::piMapper::TestCommand((ofxPiMapper *)this));
break; break;
case 'u': case 'z':
// undo // undo
commandManager.undo(); commandManager.undo();
break; break;

Loading…
Cancel
Save