54 lines
1.2 KiB
C++
54 lines
1.2 KiB
C++
#include "utils/TimeAction.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include "common/SpdLogger.h"
|
|
#include "common/RecourceHelper.h"
|
|
|
|
TimeAction::TimeAction(QObject* parent /*= nullptr*/)
|
|
: QObject(parent){
|
|
|
|
}
|
|
|
|
TimeAction::TimeAction(std::vector<std::pair<double, int>> data, const QString& path, QObject* parent /*= nullptr*/)
|
|
: QObject(parent)
|
|
, data_(std::move(data))
|
|
, path_(path) {
|
|
|
|
}
|
|
|
|
TimeAction::~TimeAction() {
|
|
|
|
}
|
|
|
|
TimeAction* TimeAction::LoadFromFile(const QString& path, QObject* parent /*= nullptr*/) {
|
|
const QString filePath = QString("%1/%2").arg(RecourceHelper::Get().GetBasePath()).arg(path);
|
|
LOG_INFO("load file: {}", filePath.toStdString());
|
|
|
|
std::ifstream file(filePath.toStdString());
|
|
if (!file.is_open()) {
|
|
LOG_WARN("open file failed");
|
|
return nullptr;
|
|
}
|
|
|
|
std::vector<std::pair<double, int>> data;
|
|
double num;
|
|
int state;
|
|
|
|
while (file >> num >> state) {
|
|
data.emplace_back(num, state);
|
|
}
|
|
file.close();
|
|
|
|
return new TimeAction(std::move(data), path, parent);
|
|
}
|
|
|
|
int TimeAction::GetValue(double t) const {
|
|
int sec = static_cast<int>(t);
|
|
if (sec >= data_.size()) {
|
|
return -1;
|
|
}
|
|
|
|
return data_[sec].second;
|
|
}
|