Skip to content
OE_display.cpp 38.8 KiB
Newer Older
			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);

raoul's avatar
raoul committed
			const 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);
            glColor4fv(OE_preferences::stitchStartPointColor.rgba);

			for (auto stepPt : linkstitch->stepPts)
			{
3dsman's avatar
3dsman committed

                glColor4fv(OE_preferences::stitchControlsColor.rgba);
				glBegin(GL_POINTS);
raoul's avatar
raoul committed
				glVertex2f(stepPt.x, stepPt.y);
                //OE_preferences::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);
            glColor4fv(OE_preferences::stitchStartPointColor.rgba);
			glBegin(GL_LINES);
raoul's avatar
raoul committed
			const 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();
            glColor4fv(OE_preferences::stitchControlsColor.rgba);
			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);
}

/** \brief draw the instruction points
 *
 * \return true if all is ok
 *
 */
bool OE_display::drawCommands()
{
	float scaleRatio = document->getPulsePerMm();
	if (document->instPoints.size() == 0)
raoul's avatar
raoul committed
	{
raoul's avatar
raoul committed
	}
    glPointSize(OE_preferences::instPointSize);
        glColor4fv(OE_preferences::instLineBeforeCurrentColor.rgba);
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);
        glColor4fv(OE_preferences::instLineAfterCurrentColor.rgba);
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(OE_preferences::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)
				{
                    glColor4fv((curcolor*OE_color(1, 1, 1, OE_preferences::instLineBeforeCurrentColor.rgba[3])).rgba);
raoul's avatar
raoul committed
				}
				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();
raoul's avatar
raoul committed
				{
					glColor4fv(curcolor.rgba);
raoul's avatar
raoul committed
				}
raoul's avatar
raoul committed
				{
                    glColor4fv((curcolor*OE_color(1, 1, 1, OE_preferences::instLineBeforeCurrentColor.rgba[3])).rgba);
raoul's avatar
raoul committed
				}
			}
			OE_start* start = dynamic_cast<OE_start*> (document->instCommand.at(i));
			if (start)
			{
				curcolor = start->getThreadColor();
                glColor4fv((curcolor*OE_color(1, 1, 1, OE_preferences::instLineBeforeCurrentColor.rgba[3])).rgba);
			}
		}

		for (; pt < document->instPoints.size(); pt++)
raoul's avatar
raoul committed
			if (pt == document->curPoint)
			{
                glColor4fv((curcolor*OE_color(1, 1, 1, OE_preferences::instLineBeforeCurrentColor.rgba[3])).rgba);
raoul's avatar
raoul committed
			}
			glVertex2f(document->instPoints.at(pt).x/scaleRatio, document->instPoints.at(pt).y/scaleRatio);
    glColor4fv(OE_preferences::instStartPointColor.rgba);
    glPointSize(OE_preferences::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())
	{
        glColor4fv(OE_preferences::instCurPointColor.rgba);
        glPointSize(OE_preferences::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(OE_preferences::backgroundColor.rgba[0], OE_preferences::backgroundColor.rgba[1], OE_preferences::backgroundColor.rgba[2], 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);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glDisable(GL_DEPTH_TEST);
	glColor4ub(255,255,255,255);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

	vector_2d hoopSize = document->getHoopSize()/2;
	vector_2d zero = document->getZeroPoint();

	//draw hoop
            glColor4fv(OE_preferences::hoopColor.rgba);
raoul's avatar
raoul committed
			glVertex2f(zero.x-hoopSize.x, zero.y-hoopSize.y);
			glVertex2f(zero.x-hoopSize.x, zero.y+hoopSize.y);
raoul's avatar
raoul committed
			glVertex2f(zero.x+hoopSize.x, zero.y+hoopSize.y);
			glVertex2f(zero.x+hoopSize.x, zero.y-hoopSize.y);
			glVertex2f(zero.x-hoopSize.x, zero.y-hoopSize.y);
            glColor4fv((OE_preferences::gridColor * OE_color(1, 1, 1, std::max(0.0f, 0.2f-zoom*2))).rgba);
raoul's avatar
raoul committed
			int offset = (int)-hoopSize.x;
			while (offset<hoopSize.x)
raoul's avatar
raoul committed
					glVertex2f(zero.x+offset, zero.y-hoopSize.y);
					glVertex2f(zero.x+offset, zero.y+hoopSize.y);
raoul's avatar
raoul committed
			offset = (int)-hoopSize.y;
			while (offset<hoopSize.y)
raoul's avatar
raoul committed
					glVertex2f(zero.x-hoopSize.x, zero.y+offset);
					glVertex2f(zero.x+hoopSize.x, zero.y+offset);
            glColor4fv(OE_preferences::gridColor.rgba);
raoul's avatar
raoul committed
			offset = (int)-hoopSize.x+(int)hoopSize.x%10;
			while (offset<hoopSize.x)
raoul's avatar
raoul committed
				glVertex2f(zero.x+offset, zero.y-hoopSize.y);
				glVertex2f(zero.x+offset, zero.y+hoopSize.y);
raoul's avatar
raoul committed
			offset = (int)-hoopSize.y+(int)hoopSize.y%10;
			while (offset<hoopSize.y)
raoul's avatar
raoul committed
					glVertex2f(zero.x-hoopSize.x, zero.y+offset);
					glVertex2f(zero.x+hoopSize.x, zero.y+offset);
        float zeroSize = OE_preferences::zeroSize;
            glColor4fv(OE_preferences::zeroColor.rgba);
raoul's avatar
raoul committed
			glVertex2f(zero.x-zeroSize, zero.y);
			glVertex2f(zero.x, zero.y+zeroSize);
raoul's avatar
raoul committed
			glVertex2f(zero.x+zeroSize, zero.y);
			glVertex2f(zero.x, zero.y-zeroSize);
raoul's avatar
raoul committed
			glVertex2f(zero.x-zeroSize, zero.y);
            glColor4fv(OE_preferences::zeroColor.rgba);
raoul's avatar
raoul committed
			glVertex2f(zero.x-zeroSize, zero.y);
			glVertex2f(zero.x+zeroSize, zero.y);
raoul's avatar
raoul committed
			glVertex2f(zero.x, zero.y-zeroSize);
			glVertex2f(zero.x, zero.y+zeroSize);
raoul's avatar
raoul committed
	{
		refreshAll();
	}*/
raoul's avatar
raoul committed
	if (style->drawCurves)
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();
		while (curve != document->curves.end())
		{
raoul's avatar
raoul committed
			drawCurve(*curve);
			curve++;
		}

		curve = document->selectedCurves.begin();
		while (curve != document->selectedCurves.end())
		{
raoul's avatar
raoul committed
			drawCurveControl(*curve);
			selectionBounds += (*curve)->getBound();
			curve++;
		}
	}

raoul's avatar
raoul committed
	if (style->drawStitches)
	{
		std::list<OE_stitchs*>::iterator stitch = document->stitchs.begin();
		int curpoint = document->curPoint;
		while (stitch != document->stitchs.end())
		{
raoul's avatar
raoul committed
			drawStitch(*stitch, curpoint);
raoul's avatar
raoul committed
			curpoint -= (*stitch)->getNpts();
			stitch++;
		}

		stitch = document->selectedStitchs.begin();
		while (stitch != document->selectedStitchs.end())
		{
raoul's avatar
raoul committed
			drawStitchControl(*stitch);
			selectionBounds += (*stitch)->getBound();
			stitch++;
		}
        if(higligtedStitch)
            drawStitch(higligtedStitch, 0);
raoul's avatar
raoul committed
	if (style->drawCommands)
	{
raoul's avatar
raoul committed
	}
void OE_display::pick(std::list<OE_pointcurve*> selectedCurves, std::list<OE_stitchs*> selectedStitches)
{
	std::list<OE_pointcurve*>::iterator curve;
	std::list<OE_stitchs*>::iterator stitch;
	int index;

	glViewport(0, 0, width, height);
	glClearColor(0.85f, 0.85f, 0.85f, 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();

	glOrtho(absMouse.x-7*zoom, absMouse.x+7*zoom, absMouse.y+7*zoom, absMouse.y-7*zoom, -1, 1);


	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glDisable(GL_DEPTH_TEST);
	glColor4ub(255,255,255,255);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

	glRenderMode(GL_SELECT);
	//Init of name stack
	glInitNames();
3dsman's avatar
3dsman committed
    glPushName(TOOLSSELID); //id of the tools
3dsman's avatar
3dsman committed
/*	glPushName(ZEROID); //id of the zero tool

	//draw 0
	vector_2d zero = document->getZeroPoint();
	glPointSize(6);
	glBegin(GL_POINT);
raoul's avatar
raoul committed
		glVertex2f(zero.x, zero.y);
3dsman's avatar
3dsman committed
    glPushName(TRANSTOOLID); //id of the move tool
	// if sitches are selectables draw their controls
raoul's avatar
raoul committed
	if (style->selectStitches)
3dsman's avatar
3dsman committed
        glLoadName(STITCHSSELID); //id of the stitches

		index = 0;
		glPushName(index); //init the stitches index

		stitch = selectedStitches.begin();
		while (stitch != selectedStitches.end())
		{
raoul's avatar
raoul committed
			drawStitchControl(*stitch, true);
	// if curves are selectables draw their controls
raoul's avatar
raoul committed
	if (style->selectCurves)
3dsman's avatar
3dsman committed
        glLoadName(CURVESSELID); //id of the curves
		index = 0;
		glPushName(index); //init the curves index

		curve = selectedCurves.begin();
		while (curve != selectedCurves.end())
		{
raoul's avatar
raoul committed
			drawCurveControl(*curve, true);
	// if sitches are selectables draw the stitch
raoul's avatar
raoul committed
	if (style->selectStitches)
3dsman's avatar
3dsman committed
        glLoadName(STITCHSSELID); //id of the stitches
		index = 0;
		glPushName(index); //init the stitches index

		int curpoint = document->curPoint;

		stitch = document->stitchs.begin();
		while (stitch != document->stitchs.end())
		{
raoul's avatar
raoul committed
			drawStitch(*stitch, curpoint);
raoul's avatar
raoul committed
			curpoint -= (*stitch)->getNpts();
	// if curves are selectables draw them
raoul's avatar
raoul committed
	if (style->selectCurves)
3dsman's avatar
3dsman committed
        glLoadName(CURVESSELID); //id of the curves
		index = 0;
		glPushName(index); //init the curves index

		curve = document->curves.begin();
		while (curve != document->curves.end())
		{
raoul's avatar
raoul committed
			drawCurve(*curve);
/** \brief move and zoom to see the entire document
 */
void OE_display::showAll()
{
	if (document)
		BoundingBox bound = document->getBound();
		viewPos.x = (bound.getMax().x + bound.getMin().x)/2;
		viewPos.y = (bound.getMax().y + bound.getMin().y)/2;
raoul's avatar
raoul committed

		zoom = std::max((bound.getMax().x - bound.getMin().x)/width/1.5, (bound.getMax().y - bound.getMin().y)/height/1.5);
void OE_display::zoomSelection()
{
	if (document&&controller)
	{

		BoundingBox bound = controller->getSelectionBoundingBox();
		if (!bound.init)
		{
			vector_2d hoop = document->getHoopSize();
			bound = BoundingBox(-hoop.x/2.f,-hoop.y/2.f,hoop.x/2.f,hoop.y/2.f);
		}
		viewPos.x = (bound.getMax().x + bound.getMin().x)/2;
		viewPos.y = (bound.getMax().y + bound.getMin().y)/2;

		zoom = std::max((bound.getMax().x - bound.getMin().x)/width/1.5, (bound.getMax().y - bound.getMin().y)/height/1.5);
raoul's avatar
raoul committed
bool OE_display::mouse_Pos(double x, double y)
raoul's avatar
raoul committed
	if (pan)
	{
		viewPos = viewPos + (mouse-vector_2d(x, y))*zoom*2;
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	mouse.x = x;
	mouse.y = y;
raoul's avatar
raoul committed
	absMouse = viewPos+(mouse-vector_2d(width, height)/2)*zoom*2;
raoul's avatar
raoul committed
	return true;
raoul's avatar
raoul committed

raoul's avatar
raoul committed
bool OE_display::mouse_Button(QMouseEvent* event)
raoul's avatar
raoul committed
	bool redraw = false;
	if (event->button() == Qt::MiddleButton)
raoul's avatar
raoul committed
	{
raoul's avatar
raoul committed
		pan = event->type() == QEvent::MouseButtonPress;
		if (pan)
			redraw = true;
raoul's avatar
raoul committed
	}
raoul's avatar
raoul committed
	return redraw;
void OE_display::scroll(double /*xoffset*/, double yoffset)
raoul's avatar
raoul committed
	if (!pan)
	{
		if (yoffset>0)
		{
			zoom = zoom/1.1f;
raoul's avatar
raoul committed
		}
		else
		{
			zoom = zoom*1.1f;
raoul's avatar
raoul committed
		}
void OE_display::resize(int width, int height)
{
	this->width = width;
	this->height = height;
}

raoul's avatar
raoul committed
void OE_display::keyPressEvent(QKeyEvent* event)
{
raoul's avatar
raoul committed
		return;
	key(event);
}

void OE_display::keyReleaseEvent(QKeyEvent* event)
{
raoul's avatar
raoul committed
	key(event);
}

void OE_display::key(QKeyEvent* /*event*/)
raoul's avatar
raoul committed
{
raoul's avatar
raoul committed
	//DEBUG printf("%s \"%s\"\n", event->type()==QEvent::KeyPress?"Keypress":"Keyrelease", event->text().toStdString().c_str(), event->count()); fflush(stdout);
	update(); // TODO only when necessary
raoul's avatar
raoul committed
}

vector_2d OE_display::screenToDocument(vector_2d pos)
{
raoul's avatar
raoul committed
	return viewPos+(pos-vector_2d(width, height)/2)*zoom*2;