#include "workspace/Timestep.h" #include #include #include "workspace/WorkSpace.h" #include "common/RecourceHelper.h" #include "common/SpdLogger.h" Timestep::Timestep(const std::vector& steps, const QString& path, WorkSpace* parent /*= nullptr*/) noexcept : QObject((QObject*)parent) , steps_(steps) , path_(path) , workSpace_(parent) { maxTime_ = *steps_.rbegin(); } Timestep* Timestep::Load(const QString& path, WorkSpace* parent) { const QString filePath = QString("%1/%2").arg(RecourceHelper::Get().GetBasePath()).arg(path); LOG_INFO("Load timestep: {}", filePath.toStdString()); QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { LOG_WARN("Cannot open file for reading: {}", file.errorString().toStdString()); return nullptr; } QTextStream in(&file); std::vector numbers; while (!in.atEnd()) { QString line = in.readLine(); bool ok; double value = line.toDouble(&ok); if (ok) { numbers.push_back(value); } else { LOG_WARN("Cannot open file for reading: {}", line.toStdString()); } } file.close(); Timestep* timestep = new Timestep(numbers, path, parent); return timestep; } void Timestep::GetRange(double& minTime, double& maxTime, double& step) { minTime = *steps_.begin(); maxTime = maxTime_; step = currentStep_; } void Timestep::Update(double dt) { if (playStatus_ != PlayStatus::PS_Started) { return; } if (nullptr == workSpace_) { LOG_WARN("workSpace_ is nullptr"); return; } double deltaTime = dt * currentStep_; current_ += deltaTime; workSpace_->OnFrame(deltaTime); if (current_ >= maxTime_ && playStatus_ != PlayStatus::PS_Stoped) { current_ = maxTime_; Stop(); } emit TimeChanged(current_); } bool Timestep::IsPause() { return playStatus_ == PlayStatus::PS_Suspended && playStatus_ != PlayStatus::PS_Started; } bool Timestep::IsStoped() { return playStatus_ == PlayStatus::PS_Stoped; } void Timestep::Start() { LOG_INFO("enter"); playStatus_ = PlayStatus::PS_Started; current_ = 0.0; if (nullptr == workSpace_) { LOG_WARN("workSpace_ is nullptr"); return; } workSpace_->Begin(); emit StatusChanged((int)playStatus_); } void Timestep::Resume() { LOG_INFO("enter"); if (PlayStatus::PS_Suspended != playStatus_) { LOG_WARN("play status is not suspended"); return; } playStatus_ = PlayStatus::PS_Started; emit StatusChanged((int)playStatus_); } void Timestep::Pause() { LOG_INFO("enter"); if (PlayStatus::PS_Started == playStatus_) { playStatus_ = PlayStatus::PS_Suspended; } else if (PlayStatus::PS_Suspended == playStatus_) { playStatus_ = PlayStatus::PS_Started; } emit StatusChanged((int)playStatus_); } void Timestep::Stop() { LOG_INFO("enter"); current_ = maxTime_; playStatus_ = PlayStatus::PS_Stoped; if (nullptr == workSpace_) { LOG_WARN("workSpace_ is nullptr"); return; } workSpace_->End(); emit StatusChanged((int)playStatus_); } void Timestep::Up() { currentSpeed *= 2; currentStep_ *= currentSpeed; emit StepChanged(currentSpeed); } void Timestep::Down() { currentSpeed *= 0.5; currentStep_ *= currentSpeed; emit StepChanged(currentSpeed); }