2008-03-26 09:18:12 +00:00
|
|
|
#include <stdarg.h>
|
2008-03-27 09:53:51 +00:00
|
|
|
|
|
|
|
#include "solvespace.h"
|
2008-03-26 09:18:12 +00:00
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
#define mView (&GraphicsWindow::MenuView)
|
|
|
|
#define mEdit (&GraphicsWindow::MenuEdit)
|
2008-03-26 09:18:12 +00:00
|
|
|
const GraphicsWindow::MenuEntry GraphicsWindow::menu[] = {
|
2008-04-12 15:17:58 +00:00
|
|
|
{ 0, "&File", 0, NULL },
|
|
|
|
{ 1, "&New\tCtrl+N", 0, NULL },
|
|
|
|
{ 1, "&Open...\tCtrl+O", 0, NULL },
|
|
|
|
{ 1, "&Save\tCtrl+S", 0, NULL },
|
|
|
|
{ 1, "Save &As...", 0, NULL },
|
|
|
|
{ 1, NULL, 0, NULL },
|
|
|
|
{ 1, "E&xit", 0, NULL },
|
|
|
|
|
|
|
|
{ 0, "&Edit", 0, NULL },
|
|
|
|
{ 1, "&Undo\tCtrl+Z", 0, NULL },
|
|
|
|
{ 1, "&Redo\tCtrl+Y", 0, NULL },
|
|
|
|
{ 1, NULL, 0, NULL },
|
|
|
|
{ 1, "&Unselect All\tEsc", MNU_UNSELECT_ALL, 27, mEdit },
|
|
|
|
|
|
|
|
{ 0, "&View", 0, NULL },
|
|
|
|
{ 1, "Zoom &In\t+", MNU_ZOOM_IN, '+', mView },
|
|
|
|
{ 1, "Zoom &Out\t-", MNU_ZOOM_OUT, '-', mView },
|
|
|
|
{ 1, "Zoom To &Fit\tF", MNU_ZOOM_TO_FIT, 'F', mView },
|
|
|
|
{ 1, NULL, 0, NULL },
|
|
|
|
{ 1, "Onto Plane/Csys\tO", MNU_ORIENT_ONTO, 'O', mView },
|
|
|
|
{ 1, NULL, 0, NULL },
|
|
|
|
{ 1, "Dimensions in &Inches", 0, NULL },
|
|
|
|
{ 1, "Dimensions in &Millimeters", 0, NULL },
|
|
|
|
|
|
|
|
{ 0, "&Sketch", 0, NULL },
|
|
|
|
{ 1, NULL, 0, NULL },
|
|
|
|
{ 1, "To&ggle Construction\tG", 0, NULL },
|
|
|
|
|
|
|
|
{ 0, "&Constrain", 0, NULL },
|
|
|
|
{ 1, "S&ymmetric\tY", 0, NULL },
|
|
|
|
|
|
|
|
{ 0, "&Help", 0, NULL },
|
|
|
|
{ 1, "&About\t", 0, NULL },
|
|
|
|
{ -1 },
|
2008-03-26 09:18:12 +00:00
|
|
|
};
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
void GraphicsWindow::Init(void) {
|
2008-04-12 14:12:26 +00:00
|
|
|
memset(this, 0, sizeof(*this));
|
|
|
|
|
|
|
|
offset.x = offset.y = offset.z = 0;
|
2008-03-27 09:53:51 +00:00
|
|
|
scale = 1;
|
|
|
|
projRight.x = 1; projRight.y = projRight.z = 0;
|
2008-04-12 15:17:58 +00:00
|
|
|
projUp.y = 1; projUp.z = projUp.x = 0;
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
show2dCsyss = true;
|
|
|
|
showAxes = true;
|
|
|
|
showPoints = true;
|
|
|
|
showAllGroups = true;
|
|
|
|
showConstraints = true;
|
2008-03-27 09:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::NormalizeProjectionVectors(void) {
|
2008-04-12 15:17:58 +00:00
|
|
|
Vector norm = projRight.Cross(projUp);
|
|
|
|
projUp = norm.Cross(projRight);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
projUp = projUp.ScaledBy(1/projUp.Magnitude());
|
2008-03-27 09:53:51 +00:00
|
|
|
projRight = projRight.ScaledBy(1/projRight.Magnitude());
|
|
|
|
}
|
|
|
|
|
2008-04-12 14:12:26 +00:00
|
|
|
Point2d GraphicsWindow::ProjectPoint(Vector p) {
|
|
|
|
p = p.Plus(offset);
|
|
|
|
|
|
|
|
Point2d r;
|
|
|
|
r.x = p.Dot(projRight) * scale;
|
2008-04-12 15:17:58 +00:00
|
|
|
r.y = p.Dot(projUp) * scale;
|
2008-04-12 14:12:26 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
void GraphicsWindow::MenuView(MenuId id) {
|
|
|
|
switch(id) {
|
|
|
|
case MNU_ZOOM_IN:
|
|
|
|
SS.GW.scale *= 1.2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MNU_ZOOM_OUT:
|
|
|
|
SS.GW.scale /= 1.2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MNU_ZOOM_TO_FIT:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MNU_ORIENT_ONTO:
|
|
|
|
SS.GW.GroupSelection();
|
|
|
|
if(SS.GW.gs.n == 1 && SS.GW.gs.csyss == 1) {
|
|
|
|
Entity *e = SS.entity.FindById(SS.GW.gs.entity[0]);
|
|
|
|
double q[4];
|
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
q[i] = SS.param.FindById(e->param(i))->val;
|
|
|
|
}
|
|
|
|
SS.GW.projRight = Vector::RotationU(q[0], q[1], q[2], q[3]);
|
|
|
|
SS.GW.projUp = Vector::RotationV(q[0], q[1], q[2], q[3]);
|
|
|
|
SS.GW.offset = SS.point.FindById(e->point(16))->GetCoords();
|
|
|
|
SS.GW.ClearSelection();
|
|
|
|
InvalidateGraphics();
|
|
|
|
} else {
|
|
|
|
Error("Select plane or coordinate system before orienting.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
InvalidateGraphics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::MenuEdit(MenuId id) {
|
|
|
|
switch(id) {
|
|
|
|
case MNU_UNSELECT_ALL:
|
|
|
|
SS.GW.ClearSelection();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: oops();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
void GraphicsWindow::MouseMoved(double x, double y, bool leftDown,
|
|
|
|
bool middleDown, bool rightDown, bool shiftDown, bool ctrlDown)
|
|
|
|
{
|
2008-04-12 14:12:26 +00:00
|
|
|
Point2d mp = { x, y };
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
if(middleDown) {
|
2008-04-12 14:12:26 +00:00
|
|
|
hover.Clear();
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
double dx = (x - orig.mouse.x) / scale;
|
|
|
|
double dy = (y - orig.mouse.y) / scale;
|
|
|
|
|
|
|
|
if(shiftDown) {
|
2008-04-12 15:17:58 +00:00
|
|
|
offset.x = orig.offset.x + dx*projRight.x + dy*projUp.x;
|
|
|
|
offset.y = orig.offset.y + dx*projRight.y + dy*projUp.y;
|
|
|
|
offset.z = orig.offset.z + dx*projRight.z + dy*projUp.z;
|
2008-03-27 09:53:51 +00:00
|
|
|
} else if(ctrlDown) {
|
|
|
|
double theta = atan2(orig.mouse.y, orig.mouse.x);
|
|
|
|
theta -= atan2(y, x);
|
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
Vector normal = orig.projRight.Cross(orig.projUp);
|
2008-03-27 09:53:51 +00:00
|
|
|
projRight = orig.projRight.RotatedAbout(normal, theta);
|
2008-04-12 15:17:58 +00:00
|
|
|
projUp = orig.projUp.RotatedAbout(normal, theta);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
|
|
|
NormalizeProjectionVectors();
|
|
|
|
} else {
|
|
|
|
double s = 0.3*(PI/180); // degrees per pixel
|
2008-04-12 15:17:58 +00:00
|
|
|
projRight = orig.projRight.RotatedAbout(orig.projUp, -s*dx);
|
|
|
|
projUp = orig.projUp.RotatedAbout(orig.projRight, s*dy);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
|
|
|
NormalizeProjectionVectors();
|
|
|
|
}
|
|
|
|
|
2008-04-11 11:13:47 +00:00
|
|
|
orig.projRight = projRight;
|
2008-04-12 15:17:58 +00:00
|
|
|
orig.projUp = projUp;
|
2008-04-11 11:13:47 +00:00
|
|
|
orig.offset = offset;
|
|
|
|
orig.mouse.x = x;
|
|
|
|
orig.mouse.y = y;
|
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
InvalidateGraphics();
|
2008-04-12 14:12:26 +00:00
|
|
|
} else {
|
|
|
|
// No mouse buttons are pressed. We just need to do our usual hit
|
|
|
|
// testing, to see if anything ought to be hovered.
|
|
|
|
Selection s;
|
|
|
|
HitTestMakeSelection(mp, &s);
|
|
|
|
if(!s.Equals(&hover)) {
|
|
|
|
hover = s;
|
|
|
|
InvalidateGraphics();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GraphicsWindow::Selection::Equals(Selection *b) {
|
|
|
|
if(point.v != b->point.v) return false;
|
|
|
|
if(entity.v != b->entity.v) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool GraphicsWindow::Selection::IsEmpty(void) {
|
|
|
|
if(point.v) return false;
|
|
|
|
if(entity.v) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void GraphicsWindow::Selection::Clear(void) {
|
|
|
|
point.v = entity.v = 0;
|
|
|
|
}
|
|
|
|
void GraphicsWindow::Selection::Draw(void) {
|
|
|
|
if(point.v) SS.point. FindById(point )->Draw();
|
|
|
|
if(entity.v) SS.entity.FindById(entity)->Draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::HitTestMakeSelection(Point2d mp, Selection *dest) {
|
|
|
|
int i;
|
|
|
|
double d, dmin = 1e12;
|
|
|
|
|
|
|
|
dest->point.v = 0;
|
|
|
|
dest->entity.v = 0;
|
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
// Do the points
|
2008-04-12 14:12:26 +00:00
|
|
|
for(i = 0; i < SS.entity.elems; i++) {
|
|
|
|
d = SS.entity.elem[i].t.GetDistance(mp);
|
|
|
|
if(d < 10 && d < dmin) {
|
|
|
|
dest->point.v = 0;
|
|
|
|
dest->entity = SS.entity.elem[i].t.h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
// Entities
|
2008-04-12 14:12:26 +00:00
|
|
|
for(i = 0; i < SS.point.elems; i++) {
|
|
|
|
d = SS.point.elem[i].t.GetDistance(mp);
|
|
|
|
if(d < 10 && d < dmin) {
|
|
|
|
dest->entity.v = 0;
|
|
|
|
dest->point = SS.point.elem[i].t.h;
|
|
|
|
}
|
2008-03-27 09:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:17:58 +00:00
|
|
|
void GraphicsWindow::ClearSelection(void) {
|
|
|
|
for(int i = 0; i < MAX_SELECTED; i++) {
|
|
|
|
selection[i].Clear();
|
|
|
|
}
|
|
|
|
InvalidateGraphics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::GroupSelection(void) {
|
|
|
|
gs.points = gs.entities = 0;
|
|
|
|
gs.csyss = 0;
|
|
|
|
gs.n = 0;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < MAX_SELECTED; i++) {
|
|
|
|
Selection *s = &(selection[i]);
|
|
|
|
if(s->point.v) {
|
|
|
|
gs.point[(gs.points)++] = s->point;
|
|
|
|
(gs.n)++;
|
|
|
|
}
|
|
|
|
if(s->entity.v) {
|
|
|
|
gs.entity[(gs.entities)++] = s->entity;
|
|
|
|
(gs.n)++;
|
|
|
|
|
|
|
|
Entity *e = SS.entity.FindById(s->entity);
|
|
|
|
if(e->type == Entity::CSYS_2D) {
|
|
|
|
(gs.csyss)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
void GraphicsWindow::MouseMiddleDown(double x, double y) {
|
|
|
|
orig.offset = offset;
|
2008-04-12 15:17:58 +00:00
|
|
|
orig.projUp = projUp;
|
2008-03-27 09:53:51 +00:00
|
|
|
orig.projRight = projRight;
|
|
|
|
orig.mouse.x = x;
|
|
|
|
orig.mouse.y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::MouseLeftDown(double x, double y) {
|
2008-04-12 14:12:26 +00:00
|
|
|
// Make sure the hover is up to date.
|
|
|
|
MouseMoved(x, y, false, false, false, false, false);
|
|
|
|
|
|
|
|
if(!hover.IsEmpty()) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < MAX_SELECTED; i++) {
|
|
|
|
if(selection[i].Equals(&hover)) {
|
|
|
|
selection[i].Clear();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < MAX_SELECTED; i++) {
|
|
|
|
if(selection[i].IsEmpty()) {
|
|
|
|
selection[i] = hover;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// I guess we ran out of slots, oh well.
|
|
|
|
done:
|
|
|
|
InvalidateGraphics();
|
|
|
|
}
|
2008-03-27 09:53:51 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 10:48:44 +00:00
|
|
|
void GraphicsWindow::MouseScroll(double x, double y, int delta) {
|
|
|
|
double offsetRight = offset.Dot(projRight);
|
2008-04-12 15:17:58 +00:00
|
|
|
double offsetUp = offset.Dot(projUp);
|
2008-04-01 10:48:44 +00:00
|
|
|
|
|
|
|
double righti = x/scale - offsetRight;
|
2008-04-12 15:17:58 +00:00
|
|
|
double upi = y/scale - offsetUp;
|
2008-04-01 10:48:44 +00:00
|
|
|
|
|
|
|
if(delta > 0) {
|
2008-04-12 15:17:58 +00:00
|
|
|
scale *= 1.2;
|
2008-04-01 10:48:44 +00:00
|
|
|
} else {
|
2008-04-12 15:17:58 +00:00
|
|
|
scale /= 1.2;
|
2008-04-01 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double rightf = x/scale - offsetRight;
|
2008-04-12 15:17:58 +00:00
|
|
|
double upf = y/scale - offsetUp;
|
2008-04-01 10:48:44 +00:00
|
|
|
|
|
|
|
offset = offset.Plus(projRight.ScaledBy(rightf - righti));
|
2008-04-12 15:17:58 +00:00
|
|
|
offset = offset.Plus(projUp.ScaledBy(upf - upi));
|
2008-04-01 10:48:44 +00:00
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
InvalidateGraphics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::ToggleBool(int link, DWORD v) {
|
|
|
|
bool *vb = (bool *)v;
|
|
|
|
*vb = !*vb;
|
|
|
|
|
|
|
|
InvalidateGraphics();
|
|
|
|
SS.TW.Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GraphicsWindow::ToggleAnyDatumShown(int link, DWORD v) {
|
|
|
|
bool t = !(SS.GW.show2dCsyss && SS.GW.showAxes && SS.GW.showPoints);
|
|
|
|
SS.GW.show2dCsyss = t;
|
|
|
|
SS.GW.showAxes = t;
|
|
|
|
SS.GW.showPoints = t;
|
|
|
|
|
|
|
|
InvalidateGraphics();
|
|
|
|
SS.TW.Show();
|
2008-04-01 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
void GraphicsWindow::Paint(int w, int h) {
|
2008-04-01 10:48:44 +00:00
|
|
|
width = w; height = h;
|
|
|
|
|
2008-03-27 09:53:51 +00:00
|
|
|
glViewport(0, 0, w, h);
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glScaled(scale*2.0/w, scale*2.0/h, 0);
|
|
|
|
|
|
|
|
double tx = projRight.Dot(offset);
|
2008-04-12 15:17:58 +00:00
|
|
|
double ty = projUp.Dot(offset);
|
2008-03-27 09:53:51 +00:00
|
|
|
double mat[16];
|
|
|
|
MakeMatrix(mat, projRight.x, projRight.y, projRight.z, tx,
|
2008-04-12 15:17:58 +00:00
|
|
|
projUp.x, projUp.y, projUp.z, ty,
|
2008-03-27 09:53:51 +00:00
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
glMultMatrixd(mat);
|
|
|
|
|
2008-04-11 11:13:47 +00:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glEnable(GL_LINE_SMOOTH);
|
2008-03-27 09:53:51 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2008-04-11 11:13:47 +00:00
|
|
|
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
2008-03-27 09:53:51 +00:00
|
|
|
|
|
|
|
glClearIndex((GLfloat)0);
|
|
|
|
glClearDepth(1.0);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2008-04-11 12:47:14 +00:00
|
|
|
|
2008-04-09 08:39:01 +00:00
|
|
|
int i;
|
2008-04-12 14:12:26 +00:00
|
|
|
|
|
|
|
// First, draw the entire scene.
|
|
|
|
glColor3f(1, 1, 1);
|
2008-04-09 08:39:01 +00:00
|
|
|
for(i = 0; i < SS.entity.elems; i++) {
|
|
|
|
SS.entity.elem[i].t.Draw();
|
|
|
|
}
|
2008-04-12 14:12:26 +00:00
|
|
|
glColor3f(0, 0.8f, 0);
|
|
|
|
for(i = 0; i < SS.point.elems; i++) {
|
|
|
|
SS.point.elem[i].t.Draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then redraw whatever the mouse is hovering over, highlighted. Have
|
|
|
|
// to disable the depth test, so that we can overdraw.
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glColor3f(1, 1, 0);
|
|
|
|
hover.Draw();
|
|
|
|
|
|
|
|
// And finally draw the selection, same mechanism.
|
|
|
|
glColor3f(1, 0, 0);
|
|
|
|
for(i = 0; i < MAX_SELECTED; i++) {
|
|
|
|
selection[i].Draw();
|
|
|
|
}
|
2008-03-27 09:53:51 +00:00
|
|
|
}
|
|
|
|
|