feat: implemented pause functionality on space press

This commit is contained in:
Djairo Hougee 2024-05-05 21:54:52 +02:00
parent 0e10881d9c
commit 11f4cd4451
3 changed files with 16 additions and 1 deletions

View File

@ -37,6 +37,7 @@ void Program::setupTimer() {
auto callback = vtkSmartPointer<TimerCallbackCommand>::New(this); auto callback = vtkSmartPointer<TimerCallbackCommand>::New(this);
callback->SetClientData(this); callback->SetClientData(this);
this->interact->AddObserver(vtkCommand::TimerEvent, callback); this->interact->AddObserver(vtkCommand::TimerEvent, callback);
this->interact->AddObserver(vtkCommand::KeyPressEvent, callback);
this->interact->CreateRepeatingTimer(17); // 60 fps == 1000 / 60 == 16.7 ms per frame this->interact->CreateRepeatingTimer(17); // 60 fps == 1000 / 60 == 16.7 ms per frame
} }

View File

@ -7,10 +7,16 @@ TimerCallbackCommand::TimerCallbackCommand() : dt(3600), maxTime(3600*24*365), t
TimerCallbackCommand* TimerCallbackCommand::New(Program *program) { TimerCallbackCommand* TimerCallbackCommand::New(Program *program) {
TimerCallbackCommand *cb = new TimerCallbackCommand(); TimerCallbackCommand *cb = new TimerCallbackCommand();
cb->setProgram(program); cb->setProgram(program);
cb->setPaused(false);
return cb; return cb;
} }
void TimerCallbackCommand::Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) { void TimerCallbackCommand::Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) {
auto intr = reinterpret_cast<vtkRenderWindowInteractor *>(caller);
if (eventId == vtkCommand::KeyPressEvent and not strcmp("space", intr->GetKeySym())) {
this->paused = ! this->paused;
} else if (eventId == vtkCommand::TimerEvent and not this->paused) {
this->time += this->dt; this->time += this->dt;
if (this->time >= this->maxTime) { if (this->time >= this->maxTime) {
@ -21,8 +27,14 @@ void TimerCallbackCommand::Execute(vtkObject* caller, unsigned long eventId, voi
this->program->updateData(this->time); this->program->updateData(this->time);
} }
}
void TimerCallbackCommand::setProgram(Program *program) { void TimerCallbackCommand::setProgram(Program *program) {
this->program = program; this->program = program;
} }
void TimerCallbackCommand::setPaused(const bool val) {
this->paused = val;
}

View File

@ -11,11 +11,13 @@ public:
void Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) override; void Execute(vtkObject* caller, unsigned long eventId, void* vtkNotUsed(callData)) override;
void setProgram(Program *program); void setProgram(Program *program);
void setPaused(const bool val);
private: private:
int time; int time;
int dt; int dt;
int maxTime; int maxTime;
bool paused;
Program *program; Program *program;
}; };