Skip to content
OE_display.cpp 30.6 KiB
Newer Older
/*
 * Copyright (c) 2015 Tricoire Sebastien 3dsman@free.fr
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 */

#include "OE_display.h"
#include "OE_utils.h"

#include "OE_controller.h"

#include <iostream>

#include <GL/gl.h>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
raoul's avatar
raoul committed

//float OE_display::cx, OE_display::cy = 0.0;
vector_2d OE_display::viewPos;
float OE_display::zoom = 1.0;
//int OE_display::mouseX, OE_display::mouseY, OE_display::mouseXInit, OE_display::mouseYInit = -1;
bool OE_display::pan = false;
int OE_display::width, OE_display::height = 0;
raoul's avatar
raoul committed
OE_display::OE_display(OE_document* document, OE_controller* controller, OE_displayStyle* style)
	this->document = document;
}

OE_display::~OE_display()
{
}

/** \brief draw the document on screen
 *
 * \return true if all is ok
 *
 */

raoul's avatar
raoul committed
bool OE_display::setDocument(OE_document* document)
{
	this->document = document;
raoul's avatar
raoul committed
/*	if (document)
raoul's avatar
raoul committed
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();

		while (curve != document->curves.end())
			(*curve)->refresh(zoom/2);
			curve++;
bool OE_display::setController(OE_controller* controller)
	this->controller = controller;
	return true;
}

bool OE_display::setDisplayStyle(OE_displayStyle* style)
{
	this->style = style;
	return true;
}

bool OE_display::refreshAll()
raoul's avatar
raoul committed
	if (document)
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();

		while (curve != document->curves.end())
		{
			(*curve)->refresh(zoom/2, !changeDpi);
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();

		while (stitch != document->stitchs.end())
		{
			(*stitch)->refresh(zoom/2, !changeDpi);

		if (controller) controller->generateInstructions();

		changeDpi = false;
		return true;
	}
	return false;
}

/** \brief draw the move gizmo
 *
 * \return true if all is ok
 *
 */
bool OE_display::drawSelectionTools(bool select)
{
	vector_2d min = selectionBounds.getMin();
	vector_2d max = selectionBounds.getMax();

	float crossSize = zoom*style->moveCrossSize;
	float boxOffset = zoom*style->moveCrossSize;

	min.x -= boxOffset;
	min.y -= boxOffset;
	max.x += boxOffset;
	max.y += boxOffset;

	if (selectionBounds.init)
	{
		if (select)
		{
raoul's avatar
raoul committed
			glTranslatef(min.x, min.y, 0);
			glPushName(0); //id of the move tool

			glBegin(GL_QUADS);
				style->selectionBoundColor.gl();
raoul's avatar
raoul committed
				//glColor4d(1, 0, 0, 1);
				glVertex2f(crossSize*1.0f, 0);
				glVertex2f(0, crossSize*1.0f);
				glVertex2f(-crossSize*1.0f, 0);
				glVertex2f(0, -crossSize*1.0f);
			glEnd();

			glPopName();
			glPushName(1); //id of the scale tool

			glBegin(GL_QUADS);

raoul's avatar
raoul committed
			glVertex2f(-crossSize*0.5f, -crossSize*0.5f);
			glVertex2f(-crossSize*0.5f, -crossSize*1.5f);
			glVertex2f(-crossSize*1.5f, -crossSize*1.5f);
			glVertex2f(-crossSize*1.5f, -crossSize*0.5f);
raoul's avatar
raoul committed
			glTranslatef(-min.x, -min.y, 0);
		}
		else
		{
			//draw selection bound
			glLineWidth(1.5);
			glBegin(GL_LINE_LOOP);
				style->selectionBoundColor.gl();
raoul's avatar
raoul committed
				glVertex2f(min.x, min.y);
				glVertex2f(min.x, max.y);
				glVertex2f(max.x, max.y);
				glVertex2f(max.x, min.y);
raoul's avatar
raoul committed
			glTranslatef(min.x, min.y, 0);

			//draw move cross
			glBegin(GL_TRIANGLES);
				style->transformGizmoColor.gl();
raoul's avatar
raoul committed
				glVertex2f(-crossSize*0.6f, -crossSize*0.1f);
				glVertex2f(-crossSize*0.6f, crossSize*0.1f);
				glVertex2f(crossSize*0.6f, crossSize*0.1f);
raoul's avatar
raoul committed
				glVertex2f(crossSize*0.6f, crossSize*0.1f);
				glVertex2f(-crossSize*0.6f, -crossSize*0.1f);
				glVertex2f(crossSize*0.6f, -crossSize*0.1f);
raoul's avatar
raoul committed
				glVertex2f(crossSize*0.6f, crossSize*0.3f);
				glVertex2f(crossSize*1.0f, 0);
				glVertex2f(crossSize*0.6f, -crossSize*0.3f);
raoul's avatar
raoul committed
				glVertex2f(-crossSize*0.6f, crossSize*0.3f);
				glVertex2f(-crossSize*1.0f, 0);
				glVertex2f(-crossSize*0.6f, -crossSize*0.3f);
raoul's avatar
raoul committed
				glVertex2f(-crossSize*0.1f, -crossSize*0.6f);
				glVertex2f(crossSize*0.1f, -crossSize*0.6f);
				glVertex2f(crossSize*0.1f, crossSize*0.6f);
raoul's avatar
raoul committed
				glVertex2f(crossSize*0.1f, crossSize*0.6f);
				glVertex2f(-crossSize*0.1f, -crossSize*0.6f);
				glVertex2f(-crossSize*0.1f, crossSize*0.6f);
raoul's avatar
raoul committed
				glVertex2f(crossSize*0.3f, crossSize*0.6f);
				glVertex2f(0, crossSize*1.0f);
				glVertex2f(-crossSize*0.3f, crossSize*0.6f);
raoul's avatar
raoul committed
				glVertex2f(crossSize*0.3f, -crossSize*0.6f);
				glVertex2f(0, -crossSize*1.0f);
				glVertex2f(-crossSize*0.3f, -crossSize*0.6f);
			glEnd();
			//draw scale icon
			glBegin(GL_TRIANGLES);

				style->transformGizmoColor.gl();
raoul's avatar
raoul committed
				glVertex2f(-crossSize*1.5f, -crossSize*1.2f);
				glVertex2f(-crossSize*0.6f, -crossSize*0.55f);
				glVertex2f(-crossSize*0.55f, -crossSize*0.6f);
raoul's avatar
raoul committed
				glVertex2f(-crossSize*1.5f, -crossSize*1.2f);
				glVertex2f(-crossSize*0.5f, -crossSize*0.55f);
				glVertex2f(-crossSize*1.2f, -crossSize*1.5f);
raoul's avatar
raoul committed
				glVertex2f(-crossSize*0.5f, -crossSize*0.5f);
				glVertex2f(-crossSize*0.85f, -crossSize*0.5f);
				glVertex2f(-crossSize*0.5f, -crossSize*0.85f);
raoul's avatar
raoul committed
				glVertex2f(-crossSize*1.5f, -crossSize*1.5f);
				glVertex2f(-crossSize*1.5f, -crossSize*0.9f);
				glVertex2f(-crossSize*0.9f, -crossSize*1.5f);
raoul's avatar
raoul committed
			glTranslatef(-min.x, -min.y, 0);
		}
		return true;
	}
	return false;
}

/** \brief draw a triangle */
void OE_display::drawTriangle(vector_2d point1, vector_2d point2, float size)
{
raoul's avatar
raoul committed
	glBegin(GL_TRIANGLE_FAN);
	vector_2d dir  = point2 - point1;
	dir.normalize();
	dir = dir*zoom*size;

	vector_2d lat  = dir;
	lat.turnRight();

	point1 = point1 - dir;
	vector_2d p1 = dir*2+point1;
	glVertex2f(p1.x, p1.y);
	p1 = point1+lat;
	glVertex2f(p1.x, p1.y);
	p1 = point1+dir/2;
	glVertex2f(p1.x, p1.y);
	p1 = point1-lat;
	glVertex2f(p1.x, p1.y);
	glEnd();
}


/** \brief draw a stitch on screen
 *
 * \return true if all is ok
 *
 */
raoul's avatar
raoul committed
bool OE_display::drawStitch(OE_stitchs* stitch, int curpoint)
raoul's avatar
raoul committed
	if (!stitch->check() || !style->drawStitches || stitch->getNpts()==0)
	{
raoul's avatar
raoul committed
	}

	std::vector<vector_2d> points = stitch->getPoints();
	unsigned i;

	// Points
	glPointSize(style->stitchPointSize);
raoul's avatar
raoul committed
	{
		style->stitchPointBeforeCurrentColor.gl();
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	{
		style->stitchPointAfterCurrentColor.gl();
raoul's avatar
raoul committed
	}
	for (i = 1; i < points.size(); i++)
	{
raoul's avatar
raoul committed
		{
			style->stitchPointAfterCurrentColor.gl();
raoul's avatar
raoul committed
		}
		glVertex2f(points.at(i).x, points.at(i).y);
	glLineWidth(style->stitchLineWidth);
raoul's avatar
raoul committed
	OE_thread* thread = stitch->getThread();
raoul's avatar
raoul committed
	{
		thread->getColor().gl();
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	{
		style->stitchLineBeforeCurrentColor.gl();
raoul's avatar
raoul committed
	}
	for (i = 0; i < points.size(); i++)
	{
		glVertex2f(points.at(i).x, points.at(i).y);
	}
	style->stitchStartPointColor.gl();
	glPointSize(style->stitchStartPointSize);
raoul's avatar
raoul committed
	glVertex2f(points.at(0).x, points.at(0).y);
	glEnd();

	return true;
}

/** \brief draw a curve on screen
 *
 * \return true if all is ok
 *
 */
raoul's avatar
raoul committed
bool OE_display::drawCurve(OE_curve* curve)
raoul's avatar
raoul committed
	if (!curve->check() || !style->drawCurves)
	{
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	/*if (curve->getNeedRefresh())
	{
		curve->refresh(zoom/2);
	}*/
	glLineWidth(style->curveWidth);
raoul's avatar
raoul committed
		for (i = 0; i < curve->discPts.size(); i++)
		{
			glVertex2f(curve->discPts.at(i).v.x, curve->discPts.at(i).v.y);
raoul's avatar
raoul committed
		if (curve->getClosed())
		{
			glVertex2f(curve->discPts.at(0).v.x, curve->discPts.at(0).v.y);
		}
	glEnd();
	if (!curve->getClosed())
	{
		glPointSize(style->curveStartPointSize);
		glBegin(GL_POINTS);
		glVertex2f(curve->discPts.at(0).v.x, curve->discPts.at(0).v.y);
raoul's avatar
raoul committed
		glVertex2f(curve->discPts.at(curve->discPts.size()-1).v.x, curve->discPts.at(curve->discPts.size()-1).v.y);
raoul's avatar
raoul committed
	glColor3d(1.0f, 0.0f, 0.0f);
	glBegin(GL_POINTS);
	vector_2dt point = curve->closestPoint(vector_2d (absMouseX, absMouseY));
	glVertex2f(point.v.x, point.v.y);
	glEnd();
*/
	//draw curve points
raoul's avatar
raoul committed
	if (style->drawCurvePoints)
		glPointSize(style->curvePointSize);
		glBegin(GL_POINTS);
		glVertex2f(curve->discPts[0].v.x, curve->discPts[0].v.y);
raoul's avatar
raoul committed
		for (i = 0; i < curve->discPts.size(); i++)
		{
			glColor3d(curve->discPts.at(i).t/((double)curve->pts.size()/3.0f), 0.0f, 0.0f);
				glVertex2f(curve->discPts.at(i).v.x, curve->discPts.at(i).v.y);
		}
raoul's avatar
raoul committed
		if (curve->getClosed())
		{
			glVertex2f(curve->discPts[0].v.x, curve->discPts[0].v.y);
		}
		glEnd();
	}
	return true;
}


/** \brief draw the curve controls on screen
 *
 * \return true if all is ok
 *
 */

raoul's avatar
raoul committed
bool OE_display::drawCurveControl(OE_curve* curve, bool select)
raoul's avatar
raoul committed
	if (!curve->check() || !style->drawCurves || !style->drawCurveControls)
	{
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	OE_pointcurve* tmpcurve = dynamic_cast<OE_pointcurve*> (curve);
	if (tmpcurve)
raoul's avatar
raoul committed
		if (select)
		{
			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index

raoul's avatar
raoul committed
			for (unsigned i = 0; i < tmpcurve->pts.size(); i++)
			{
				glLoadName(i);
				//vector_2d* p = &tmpcurve->pts[i];
				glBegin(GL_POINTS);
raoul's avatar
raoul committed
				glVertex2f(tmpcurve->pts[i].x, tmpcurve->pts[i].y);
raoul's avatar
raoul committed
		}
		else
			style->curveTangeantColor.gl();
raoul's avatar
raoul committed
			for (unsigned i = 0; i < tmpcurve->pts.size()-3; i += 3)
			{
				vector_2d* p = &tmpcurve->pts[i];
raoul's avatar
raoul committed
				glVertex2f(p[0].x, p[0].y);
				glVertex2f(p[1].x, p[1].y);
				glVertex2f(p[2].x, p[2].y);
				glVertex2f(p[3].x, p[3].y);
			glPointSize(style->curvePointSize);
			//glColor4ubv(controlEndPointColor);

			glBegin(GL_POINTS);

			style->curvePointColor.gl();
raoul's avatar
raoul committed
			for (unsigned i = 0; i < tmpcurve->pts.size()-3; i += 3)
			{
				vector_2d* p = &tmpcurve->pts[i];
raoul's avatar
raoul committed
				glVertex2f(p[0].x, p[0].y);
			glPointSize(style->curvePointSize);
			style->curvePointColor.gl();
raoul's avatar
raoul committed
			for (unsigned i = 0; i < curve->pts.size()-3; i += 3)
			{
raoul's avatar
raoul committed
				glVertex2f(p[1].x, p[1].y);
				glVertex2f(p[2].x, p[2].y);
raoul's avatar
raoul committed
			/*glColor4f(1.0, 0.0, 0.0, 1.0);
			glPointSize(2.0f);
			glBegin(GL_POINTS);
raoul's avatar
raoul committed
			glVertex2f(curve->pts[0].x, curve->pts[0].y);
			style->curveStartPointColor.gl();
			drawTriangle(curve->pts[0], curve->pts[1], 13);

			glLineWidth(2);
			glBegin(GL_LINES);
			vector_2d p  = curve->pts[1] - curve->pts[0];
			p.normalize();
			p = p+curve->pts[0];
raoul's avatar
raoul committed
			glVertex2f(curve->pts[0].x, curve->pts[0].y);
			//glVertex2f(curve->pts[1].x, curve->pts[1].y);
			glVertex2f(p.x, p.y);
raoul's avatar
raoul committed
	OE_joincurve* joincurve = dynamic_cast<OE_joincurve*> (curve);
	if (joincurve)
	{
		std::list<OE_subcurve*>::iterator curve = joincurve->curves.begin();
raoul's avatar
raoul committed
		if (!select)
		{
			while (curve != joincurve->curves.end())
			{
raoul's avatar
raoul committed
				OE_curve* tmpcurve = (*curve);
				glLineWidth(3);
				glBegin(GL_LINE_STRIP);
				style->curveTangeantColor.gl();
raoul's avatar
raoul committed
				for (unsigned i = 0; i < tmpcurve->discPts.size(); i++)
				{
					glVertex2f(tmpcurve->discPts.at(i).v.x, tmpcurve->discPts.at(i).v.y);
				}
				glEnd();
				if (tmpcurve->discPts.size()>1)
				{
					style->curveStartPointColor.gl();
					drawTriangle(tmpcurve->pts[0], tmpcurve->pts[1], 13);
				}
				curve++;
			}
		}
		return true;
	}

	//if this curve is a subcurve
raoul's avatar
raoul committed
	OE_subcurve* subcurve = dynamic_cast<OE_subcurve*> (curve);
	if (subcurve)
raoul's avatar
raoul committed
		if (select)
		{
			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index

			glBegin(GL_POINTS);
raoul's avatar
raoul committed
			glVertex2f(subcurve->pts[0].x, subcurve->pts[0].y);
raoul's avatar
raoul committed
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x, subcurve->pts[subcurve->getNpts()-1].y);
raoul's avatar
raoul committed
		}
		else
			style->curvePointColor.gl();
			glPointSize(5.0f);
			glBegin(GL_POINTS);
raoul's avatar
raoul committed
			glVertex2f(subcurve->pts[0].x, subcurve->pts[0].y);
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x, subcurve->pts[subcurve->getNpts()-1].y);
			style->curveStartPointColor.gl();
			drawTriangle(subcurve->pts[0], subcurve->pts[1], 13);
		}
		return true;
	}
}


raoul's avatar
raoul committed
bool OE_display::drawJoincurveControl(OE_joincurve* curve, bool select)
raoul's avatar
raoul committed
		for (unsigned i = 0; i < curve->curves.size(); i++)
		{
			OE_subcurve* subcurve = curve->getCurve(i);
raoul's avatar
raoul committed
			glLoadName(i*2+1);
			glBegin(GL_POINTS);
			glVertex2f(subcurve->pts[0].x, subcurve->pts[0].y);
			glEnd();
raoul's avatar
raoul committed
			glLoadName(i*2);
			glBegin(GL_POINTS);
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x, subcurve->pts[subcurve->getNpts()-1].y);
			glEnd();
		}
raoul's avatar
raoul committed
	else
	{
		for (unsigned i = 0; i < curve->curves.size(); i++)
		{
			OE_subcurve* subcurve = curve->getCurve(i);

			// draw the subcurves
			glLineWidth(2);
			glBegin(GL_LINE_STRIP);
			style->curveTangeantColor.gl();
raoul's avatar
raoul committed
			for (unsigned i = 0; i < subcurve->discPts.size(); i++)
			{
				glVertex2f(subcurve->discPts.at(i).v.x, subcurve->discPts.at(i).v.y);
			style->curveStartPointColor.gl();
			glPointSize(6.0f);
			glBegin(GL_POINTS);
raoul's avatar
raoul committed
			glVertex2f(subcurve->pts[0].x, subcurve->pts[0].y);
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x, subcurve->pts[subcurve->getNpts()-1].y);
			style->curveStartPointColor.gl();
			drawTriangle(subcurve->pts[0], subcurve->pts[1], 13);
		}
	}
}

/** \brief draw the stitchs controls on screen
 *
 * \return true if all is ok
 *
 */
raoul's avatar
raoul committed
bool OE_display::drawStitchControl(OE_stitchs* stitch, bool select)
raoul's avatar
raoul committed
	if (!stitch->check() || !style->drawStitches || !style->drawStitchControl)
	{
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	OE_linestitch* linestitch = dynamic_cast<OE_linestitch*> (stitch);
	if (linestitch)
	{
		OE_joincurve* joinCurve  = linestitch->getJoincurve();
		vector_2d dir = joinCurve->pts[1] - joinCurve->pts[0];
		dir.normalize();
		dir.turnRight();
		vector_2d scaleDir = dir*zoom*10;
		dir = scaleDir * 10+ joinCurve->pts[0];

raoul's avatar
raoul committed
		if (select)
		{

			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index
			if (linestitch->getJoincurve())
raoul's avatar
raoul committed
			{
				drawJoincurveControl(linestitch->getJoincurve(), true);
			}
			glPopName();
			glLoadName(1);
			glPushName(0); //init the controller points index
				glBegin(GL_TRIANGLES);
raoul's avatar
raoul committed
				glVertex2f(dir.x-scaleDir.y+scaleDir.x/3, dir.y+scaleDir.x+scaleDir.y/3);
				glVertex2f(dir.x+scaleDir.x*2, dir.y+scaleDir.y*2);
				glVertex2f(dir.x+scaleDir.y+scaleDir.x/3, dir.y-scaleDir.x+scaleDir.y/3);
raoul's avatar
raoul committed
				glVertex2f(dir.x+scaleDir.y-scaleDir.x/3, dir.y-scaleDir.x-scaleDir.y/3);
				glVertex2f(dir.x-scaleDir.x*2, dir.y-scaleDir.y*2);
				glVertex2f(dir.x-scaleDir.y-scaleDir.x/3, dir.y+scaleDir.x-scaleDir.y/3);
raoul's avatar
raoul committed
		}
		else
		{
			glLineWidth(2);
			glBegin(GL_LINE_STRIP);
			style->stitchLineBeforeCurrentColor.gl();
raoul's avatar
raoul committed
				glVertex2f(joinCurve->pts[0].x, joinCurve->pts[0].y);
				glVertex2f(dir.x, dir.y);
raoul's avatar
raoul committed
			drawJoincurveControl(joinCurve, false);
			style->stitchStartPointColor.gl();
raoul's avatar
raoul committed
			glVertex2f(dir.x-scaleDir.y+scaleDir.x/3, dir.y+scaleDir.x+scaleDir.y/3);
			glVertex2f(dir.x+scaleDir.x*2, dir.y+scaleDir.y*2);
			glVertex2f(dir.x+scaleDir.y+scaleDir.x/3, dir.y-scaleDir.x+scaleDir.y/3);
raoul's avatar
raoul committed
			glVertex2f(dir.x+scaleDir.y-scaleDir.x/3, dir.y-scaleDir.x-scaleDir.y/3);
			glVertex2f(dir.x-scaleDir.x*2, dir.y-scaleDir.y*2);
			glVertex2f(dir.x-scaleDir.y-scaleDir.x/3, dir.y+scaleDir.x-scaleDir.y/3);
raoul's avatar
raoul committed
	OE_birailstitch* birailstitch = dynamic_cast<OE_birailstitch*> (stitch);
	if (birailstitch)
raoul's avatar
raoul committed
		if (select)
		{

			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index
raoul's avatar
raoul committed
			drawJoincurveControl(birailstitch->getJoincurve1(), true);
			glPopName();
			glLoadName(1);
			glPushName(0); //init the controller points index
raoul's avatar
raoul committed
			drawJoincurveControl(birailstitch->getJoincurve2(), true);
raoul's avatar
raoul committed
		}
		else
raoul's avatar
raoul committed
			drawJoincurveControl(birailstitch->getJoincurve1(), false);
			drawJoincurveControl(birailstitch->getJoincurve2(), false);
	OE_fillstitch* fillstitch = dynamic_cast<OE_fillstitch*> (stitch);
	if (fillstitch)
	{
		if (select)
		{

			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index
			drawJoincurveControl(fillstitch->getJoincurve(), true);
			glPopName();
			glPopName();
		}
		else
		{
			drawJoincurveControl(fillstitch->getJoincurve(), false);
		}
		return true;
	}

raoul's avatar
raoul committed
	OE_linkstitch* linkstitch = dynamic_cast<OE_linkstitch*> (stitch);
	if (linkstitch)
		const float uiSize = zoom * 7;
		const float uiDistance = zoom * 17;
raoul's avatar
raoul committed
		if (select)
			glPushName(0); //init the controller type index as points
			glPushName(0); //init the controller points index
			glPointSize(4.0f);
			int index = 0;
			for (auto stepPt : linkstitch->stepPts)
			{
				glBegin(GL_POINTS);
raoul's avatar
raoul committed
					glVertex2f(stepPt.x, stepPt.y);
				glEnd();
				glLoadName(++index);
			}
			glPopName();
			glLoadName(1); //init the controller type index as add
			glPushName(0); //init the controller points index
			glPointSize(4.0f);

			std::vector<vector_2d> points = linkstitch->getPoints();

			vector_2d prevVert = points.at(0);
			vector_2d p;
			index = 0;
			std::list<vector_2d>::iterator stepPt = linkstitch->stepPts.begin();
			while (stepPt != linkstitch->stepPts.end())
			{
				p = (*stepPt + prevVert)/2;

				glBegin(GL_POINTS);
raoul's avatar
raoul committed
					glVertex2f(p.x, p.y);
				glEnd();

				prevVert = *stepPt;
				stepPt++;
				glLoadName(++index);
			}

			glLoadName(index);
			p = (points.at(points.size()-1) + prevVert)/2;
raoul's avatar
raoul committed
				glVertex2f(p.x, p.y);
			glPopName();
			glLoadName(2); //init the controller type index as delete
			glPushName(0); //init the controller points index
			index = 0;
			for (auto stepPt : linkstitch->stepPts)
			{
				glBegin(GL_LINES);
raoul's avatar
raoul committed
				glVertex2f(stepPt.x-uiSize+uiDistance, stepPt.y-uiDistance);
				glVertex2f(stepPt.x+uiSize+uiDistance, stepPt.y-uiDistance);
				glEnd();
				glLoadName(++index);

			glPopName();
			glPopName();
raoul's avatar
raoul committed
		}
		else
		{
			glLineWidth(2.5);
			glPointSize(5.0f);
			style->stitchStartPointColor.gl();

			for (auto stepPt : linkstitch->stepPts)
			{
				glBegin(GL_POINTS);
raoul's avatar
raoul committed
				glVertex2f(stepPt.x, stepPt.y);
				glEnd();
				style->stitchStartPointColor.gl();
				glBegin(GL_LINES);
raoul's avatar
raoul committed
				glVertex2f(stepPt.x-uiSize+uiDistance, stepPt.y-uiDistance);
				glVertex2f(stepPt.x+uiSize+uiDistance, stepPt.y-uiDistance);
				glEnd();
			}
			style->stitchStartPointColor.gl();
			glBegin(GL_LINES);
			std::vector<vector_2d> points = linkstitch->getPoints();

			vector_2d prevVert = points.at(0);
			vector_2d p;
			std::list<vector_2d>::iterator stepPt = linkstitch->stepPts.begin();
			while (stepPt != linkstitch->stepPts.end())
			{
				p = (*stepPt + prevVert)/2;
raoul's avatar
raoul committed
				glVertex2f(p.x-uiSize, p.y);
				glVertex2f(p.x+uiSize, p.y);
				glVertex2f(p.x, p.y+uiSize);
				glVertex2f(p.x, p.y-uiSize);
				prevVert = *stepPt;
				stepPt++;
			}
			p = (points.at(points.size()-1) + prevVert)/2;
raoul's avatar
raoul committed
			glVertex2f(p.x-uiSize, p.y);
			glVertex2f(p.x+uiSize, p.y);
			glVertex2f(p.x, p.y+uiSize);
			glVertex2f(p.x, p.y-uiSize);
			glEnd();
		}
		return true;
	}
}

/** \brief draw the instruction points
 *
 * \return true if all is ok
 *
 */
bool OE_display::drawCommands()
{
	float scaleRatio = document->getPulseByMm();
	if (document->instPoints.size() == 0)
raoul's avatar
raoul committed
	{
raoul's avatar
raoul committed
	}
	glPointSize(style->instPointSize);
		style->instLineBeforeCurrentColor.gl();
raoul's avatar
raoul committed
		for (i = 1; i < document->curPoint; i++)
		{
			glVertex2f(document->instPoints.at(i).x/scaleRatio, document->instPoints.at(i).y/scaleRatio);
		style->instLineAfterCurrentColor.gl();
raoul's avatar
raoul committed
		for (i = document->curPoint; i < document->instPoints.size(); i++)
		{
			glVertex2f(document->instPoints.at(i).x/scaleRatio, document->instPoints.at(i).y/scaleRatio);
	OE_color curcolor;
	glLineWidth(style->instLineWidth);
raoul's avatar
raoul committed
		for (i = 0; i < document->instCommand.size(); i++)
		{
			for (; pt < document->instCommand.at(i)->getIdPoint(); pt++)
raoul's avatar
raoul committed
				if (pt == document->curPoint)
				{
					(curcolor*OE_color(1, 1, 1, style->instLineBeforeCurrentColor.a)).gl();
				}
				glVertex2f(document->instPoints.at(pt).x/scaleRatio, document->instPoints.at(pt).y/scaleRatio);

			OE_waitcolor* color = dynamic_cast<OE_waitcolor*> (document->instCommand.at(i));
			if (color)
			{
				curcolor = color->getThreadColor();
				if (pt > document->curPoint)
raoul's avatar
raoul committed
				{
raoul's avatar
raoul committed
				}
raoul's avatar
raoul committed
				{
					(curcolor*OE_color(1, 1, 1, style->instLineBeforeCurrentColor.a)).gl();
				}
			}
			OE_start* start = dynamic_cast<OE_start*> (document->instCommand.at(i));
			if (start)
			{
				curcolor = start->getThreadColor();
raoul's avatar
raoul committed
				(curcolor*OE_color(1, 1, 1, style->instLineBeforeCurrentColor.a)).gl();
			}
		}

		for (; pt < document->instPoints.size(); pt++)
raoul's avatar
raoul committed
			if (pt == document->curPoint)
			{
				(curcolor*OE_color(1, 1, 1, style->instLineBeforeCurrentColor.a)).gl();
			}
			glVertex2f(document->instPoints.at(pt).x/scaleRatio, document->instPoints.at(pt).y/scaleRatio);
	style->instStartPointColor.gl();
	glPointSize(style->instStartPointSize);
raoul's avatar
raoul committed
		glVertex2f(document->instPoints.at(0).x/scaleRatio, document->instPoints.at(0).y/scaleRatio);
	if (document->curPoint < document->instPoints.size())
	{
		style->instCurPointColor.gl();
		glPointSize(style->instCurPointSize);
raoul's avatar
raoul committed
			glVertex2f(document->instPoints.at(document->curPoint).x/scaleRatio, document->instPoints.at(document->curPoint).y/scaleRatio);
		glEnd();
	}

	return true;
}


/** \brief draw the document on screen
 *
 * \return true if all is ok
 *
 */
bool OE_display::draw()
{
	if (!document)
raoul's avatar
raoul committed
	{
raoul's avatar
raoul committed
	}
	glViewport(0, 0, width, height);
	glClearColor(style->backgroundColor.r, style->backgroundColor.g, style->backgroundColor.b, 1.0f);


	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDisable(GL_TEXTURE_2D);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glEnd();

	wzoom = (float)width*zoom;
	hzoom = (float)height*zoom;

	glOrtho(viewPos.x-wzoom, viewPos.x+wzoom, viewPos.y+hzoom, viewPos.y-hzoom, -1, 1);