diff options
author | ccolin | 2020-12-22 13:15:23 +0100 |
---|---|---|
committer | ccolin | 2020-12-22 13:15:23 +0100 |
commit | 7fbe0814d52ba861a02b0560d4e6872845ef241e (patch) | |
tree | 1a8f6fd67b5d1606a58c0df00fbab285f1206d36 /src/drone_controller.cc |
initial commit
Diffstat (limited to 'src/drone_controller.cc')
-rw-r--r-- | src/drone_controller.cc | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/drone_controller.cc b/src/drone_controller.cc new file mode 100644 index 0000000..46acc7e --- /dev/null +++ b/src/drone_controller.cc @@ -0,0 +1,102 @@ +#include "drone_controller.hh" + +#include <QJsonArray> +#include <QDebug> + + +Waypoint::Waypoint(unsigned frame, QVector3D pos) + :frame(frame), + pos(pos) {} + + +Waypoint::Waypoint(const QJsonObject &json) + :Waypoint(json["frame"].toInt(), + QVector3D(json["position"]["lng_X"].toInt(), + json["position"]["alt_Y"].toInt(), + json["position"]["lat_Z"].toInt())) {} + + +Drone::Drone(const QJsonObject &json) { + QJsonArray ja = json["waypoints"].toArray(); + waypoints.reserve(ja.size()); + for (const QJsonValue &o : ja) { + waypoints.append(Waypoint(o.toObject())); + } +} + + +const QVector<Waypoint> Drone::getWaypoints() const { + return waypoints; +} + + +DroneController::DroneController(const QJsonObject &json) + :framerate(json["framerate"].toInt()) { + + // TODO: REMOVE!! + framerate = 1; + + QJsonArray ja = json["drones"].toArray(); + drones.reserve(ja.size()); + for (const QJsonValue &o : ja) { + drones.append(Drone(o.toObject())); + } + for (const Drone &d : drones) { + for (const Waypoint &wp : d.getWaypoints()) { + if (wp.frame > duration) duration = wp.frame; + } + } + + connect(&timer, &QTimer::timeout, this, &DroneController::step); + pause(); +} + + +DroneController::~DroneController() { +} + + +int DroneController::getDuration() const { + return duration; +} + + +void DroneController::step() { + qDebug() << "frame " << frame << "/" << duration + << " (" << (double) frame / duration * 100 << "%)"; + emit frameChanged(frame); + frame++; +} + + +void DroneController::play() { + paused = false; + timer.start(1000. / framerate); + qDebug() << "playing"; +} + + +void DroneController::pause() { + paused = true; + timer.stop(); + qDebug() << "pausing"; +} + + +void DroneController::suspend() { + bool old_paused = paused; + pause(); + paused = old_paused; +} + + +void DroneController::resume() { + if (!paused) play(); +} + + +void DroneController::seek(int frame) { + if (this->frame == frame) return; + this->frame = frame; + step(); +} |