From b88564b9b2b6c6c16f1a4948cb08957f4f7cd996 Mon Sep 17 00:00:00 2001 From: Torsten Paul Date: Sat, 21 Jun 2014 01:28:34 +0200 Subject: [PATCH] Allow setting $vpt/$vpr/$vpd at top-level to set the viewport camera. --- src/MainWindow.h | 1 + src/mainwin.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/MainWindow.h b/src/MainWindow.h index b6eab9f8..71eadc15 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -86,6 +86,7 @@ private: void openFile(const QString &filename); void handleFileDrop(const QString &filename); void refreshDocument(); + void updateCamera(); void updateTemporalVariables(); bool fileChangedOnDisk(); void compileTopLevelDocument(); diff --git a/src/mainwin.cc b/src/mainwin.cc index e9a3b1b8..d56177fa 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -36,6 +36,7 @@ #include "highlighter.h" #include "export.h" #include "builtin.h" +#include "expression.h" #include "progress.h" #include "dxfdim.h" #include "AboutDialog.h" @@ -1367,6 +1368,66 @@ void MainWindow::updateTemporalVariables() top_ctx.set_variable("$vpd", Value(qglview->cam.viewer_distance)); } + +/*! + * Update the viewport camera by evaluating the special variables. If they + * are assigned on top-level, the values are used to change the camera + * rotation, translation and distance. + */ +void MainWindow::updateCamera() +{ + if (!root_module) + return; + + bool camera_set = false; + double tx = qglview->cam.object_trans.x(); + double ty = qglview->cam.object_trans.y(); + double tz = qglview->cam.object_trans.z(); + double rx = qglview->cam.object_rot.x(); + double ry = qglview->cam.object_rot.y(); + double rz = qglview->cam.object_rot.z(); + double d = qglview->cam.viewer_distance; + BOOST_FOREACH(const Assignment &a, root_module->scope.assignments) { + double x, y, z; + if ("$vpr" == a.first) { + const Value vpr = a.second.get()->evaluate(&top_ctx); + if (vpr.getVec3(x, y, z)) { + rx = x; + ry = y; + rz = z; + camera_set = true; + } + } else if ("$vpt" == a.first) { + const Value vpt = a.second.get()->evaluate(&top_ctx); + if (vpt.getVec3(x, y, z)) { + tx = x; + ty = y; + tz = z; + camera_set = true; + } + } else if ("$vpd" == a.first) { + const Value vpd = a.second.get()->evaluate(&top_ctx); + if (vpd.type() == Value::NUMBER) { + d = vpd.toDouble(); + camera_set = true; + } + } + } + + if (camera_set) { + std::vector params; + params.push_back(tx); + params.push_back(ty); + params.push_back(tz); + params.push_back(rx); + params.push_back(ry); + params.push_back(rz); + params.push_back(d); + qglview->cam.setup(params); + qglview->updateGL(); + } +} + /*! Returns true if the current document is a file on disk and that file has new content. Returns false if a file on disk has disappeared or if we haven't yet saved. @@ -1412,6 +1473,7 @@ void MainWindow::compileTopLevelDocument() QFileInfo(this->fileName).absolutePath().toLocal8Bit(), false); + updateCamera(); } void MainWindow::checkAutoReload()