Merge pull request #841 from openscad/viewport-variables

Add $vpd and make the viewport variables writeable
master
Marius Kintel 2014-06-21 15:35:56 -04:00
commit 4097aa887d
4 changed files with 67 additions and 1 deletions

View File

@ -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();

View File

@ -106,6 +106,7 @@ Builtins::Builtins()
Value zero3val(zero3);
this->globalscope.assignments.push_back(Assignment("$vpt", boost::shared_ptr<Expression>(new Expression(zero3val))));
this->globalscope.assignments.push_back(Assignment("$vpr", boost::shared_ptr<Expression>(new Expression(zero3val))));
this->globalscope.assignments.push_back(Assignment("$vpd", boost::shared_ptr<Expression>(new Expression(500))));
}
Builtins::~Builtins()

View File

@ -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"] << "{" << "}";

View File

@ -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<double> 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()