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/builtin.cc b/src/builtin.cc index fc43b5e4..c20f9f03 100644 --- a/src/builtin.cc +++ b/src/builtin.cc @@ -106,6 +106,7 @@ Builtins::Builtins() Value zero3val(zero3); this->globalscope.assignments.push_back(Assignment("$vpt", boost::shared_ptr(new Expression(zero3val)))); this->globalscope.assignments.push_back(Assignment("$vpr", boost::shared_ptr(new Expression(zero3val)))); + this->globalscope.assignments.push_back(Assignment("$vpd", boost::shared_ptr(new Expression(500)))); } Builtins::~Builtins() diff --git a/src/highlighter.cc b/src/highlighter.cc index 955911d5..67c343e2 100644 --- a/src/highlighter.cc +++ b/src/highlighter.cc @@ -225,7 +225,7 @@ Highlighter::Highlighter(QTextDocument *parent) tokentypes["prim3d"] << "cube" << "cylinder" << "sphere" << "polyhedron"; tokentypes["prim2d"] << "square" << "polygon" << "circle"; tokentypes["import"] << "include" << "use" << "import_stl" << "import" << "import_dxf" << "dxf_dim" << "dxf_cross" << "surface"; - tokentypes["special"] << "$children" << "child" << "children" << "$fn" << "$fa" << "$fs" << "$t" << "$vpt" << "$vpr"; + tokentypes["special"] << "$children" << "child" << "children" << "$fn" << "$fa" << "$fs" << "$t" << "$vpt" << "$vpr" << "$vpd"; tokentypes["extrude"] << "linear_extrude" << "rotate_extrude"; tokentypes["bracket"] << "[" << "]" << "(" << ")"; tokentypes["curlies"] << "{" << "}"; diff --git a/src/mainwin.cc b/src/mainwin.cc index b028cb8f..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" @@ -1363,6 +1364,68 @@ void MainWindow::updateTemporalVariables() vpr.push_back(Value(fmodf(360 - qglview->cam.object_rot.y(), 360))); vpr.push_back(Value(fmodf(360 - qglview->cam.object_rot.z(), 360))); top_ctx.set_variable("$vpr", Value(vpr)); + + 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(); + } } /*! @@ -1410,6 +1473,7 @@ void MainWindow::compileTopLevelDocument() QFileInfo(this->fileName).absolutePath().toLocal8Bit(), false); + updateCamera(); } void MainWindow::checkAutoReload()