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
// 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;}

10
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<BaseUndoableCommand *>(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");
}
}

15
src/Commands/CommandManager.h

@ -1,22 +1,19 @@
#pragma once
#import <deque>
#import <vector>
#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<BaseUndoableCommand *> commandStack;
// TODO: Add redo stack
// Maybe use vector instead of deque...
std::vector<BaseUndoableCommand *> commandStack;
};
} // namespace piMapper

2
src/Commands/TestCommand.cpp

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

2
src/Commands/TestCommand.h

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

2
src/Commands/TestUndoCommand.cpp

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

2
src/Commands/TestUndoCommand.h

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

6
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;

Loading…
Cancel
Save