Skip to content
OE_display.cpp 25.5 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>
        //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;
OE_display::OE_display(OE_document* document,OE_controller* controller)
	this->document = document;
}

OE_display::~OE_display()
{
}

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

bool OE_display::setDocument(OE_document * document)
{
	this->document = document;
	if(document)
	{
        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;
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
bool OE_display::refresh()
{
	if(document)
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();

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

		while (stitch != document->stitchs.end())
		{
			(*stitch)->refresh(zoom/2);
			stitch++;
		}
		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)
		{

			glTranslatef(min.x,min.y,0);
			glPushName(0); //id of the move tool

			glBegin(GL_QUADS);
				style.selectionBoundColor.gl();
				//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);

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

			glPopName();

			glTranslatef(-min.x,-min.y,0);

		}else
		{

			//draw selection bound
			glLineWidth(1.5);
			glBegin(GL_LINE_LOOP);
				style.selectionBoundColor.gl();
				glVertex2f(min.x,min.y);
				glVertex2f(min.x,max.y);
				glVertex2f(max.x,max.y);
				glVertex2f(max.x,min.y);
			glEnd();

			glTranslatef(min.x,min.y,0);

			//draw move cross
			glBegin(GL_TRIANGLES);
				style.transformGizmoColor.gl();
				glVertex2f(-crossSize*0.6f,-crossSize*0.1f);
				glVertex2f(-crossSize*0.6f,crossSize*0.1f);
				glVertex2f(crossSize*0.6f,crossSize*0.1f);

				glVertex2f(crossSize*0.6f,crossSize*0.1f);
				glVertex2f(-crossSize*0.6f,-crossSize*0.1f);
				glVertex2f(crossSize*0.6f,-crossSize*0.1f);

				glVertex2f(crossSize*0.6f,crossSize*0.3f);
				glVertex2f(crossSize*1.0f,0);
				glVertex2f(crossSize*0.6f,-crossSize*0.3f);

				glVertex2f(-crossSize*0.6f,crossSize*0.3f);
				glVertex2f(-crossSize*1.0f,0);
				glVertex2f(-crossSize*0.6f,-crossSize*0.3f);

				glVertex2f(-crossSize*0.1f,-crossSize*0.6f);
				glVertex2f(crossSize*0.1f,-crossSize*0.6f);
				glVertex2f(crossSize*0.1f,crossSize*0.6f);

				glVertex2f(crossSize*0.1f,crossSize*0.6f);
				glVertex2f(-crossSize*0.1f,-crossSize*0.6f);
				glVertex2f(-crossSize*0.1f,crossSize*0.6f);

				glVertex2f(crossSize*0.3f,crossSize*0.6f);
				glVertex2f(0,crossSize*1.0f);
				glVertex2f(-crossSize*0.3f,crossSize*0.6f);

				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();
				glVertex2f(-crossSize*1.5f,-crossSize*1.2f);
				glVertex2f(-crossSize*0.6f,-crossSize*0.55f);
				glVertex2f(-crossSize*0.55f,-crossSize*0.6f);

				glVertex2f(-crossSize*1.5f,-crossSize*1.2f);
				glVertex2f(-crossSize*0.5f,-crossSize*0.55f);
				glVertex2f(-crossSize*1.2f,-crossSize*1.5f);

				glVertex2f(-crossSize*0.5f,-crossSize*0.5f);
				glVertex2f(-crossSize*0.85f,-crossSize*0.5f);
				glVertex2f(-crossSize*0.5f,-crossSize*0.85f);

				glVertex2f(-crossSize*1.5f,-crossSize*1.5f);
				glVertex2f(-crossSize*1.5f,-crossSize*0.9f);
				glVertex2f(-crossSize*0.9f,-crossSize*1.5f);

			glEnd();

			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)
{

			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
 *
 */

bool OE_display::drawStitch(OE_stitchs * stitch, int curpoint)
{
	stitch->refresh(zoom/2);
	if (!stitch->check()||!style.drawStitches||stitch->getNpts()==0)
		return false;

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

	// Points
	glPointSize(style.stitchPointSize);
	glBegin(GL_POINTS);

	if (curpoint>0)
		style.stitchPointBeforeCurrentColor.gl();
	else
		style.stitchPointAfterCurrentColor.gl();
	for (i = 1; i < points.size(); i++) {
		if (curpoint==i)
			style.stitchPointAfterCurrentColor.gl();
		glVertex2f(points.at(i).x,points.at(i).y);
	}
	glEnd();

	//draw lines
	glLineWidth(style.stitchLineWidth);
	glBegin(GL_LINE_STRIP);
	OE_thread * thread = stitch->getThread();
	if (thread)
		glColor4ubv(thread->getColor());
	else
		style.stitchLineBeforeCurrentColor.gl();
		for (i = 0; i < points.size(); i++) {
			glVertex2f(points.at(i).x,points.at(i).y);
		}
	glEnd();

	// start point

	style.stitchStartPointColor.gl();
	glPointSize(style.stitchStartPointSize);
	glBegin(GL_POINTS);
	glVertex2f(points.at(0).x,points.at(0).y);
	glEnd();

	return true;
}


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

bool OE_display::drawCurve(OE_curve * curve)
{
	if (!curve->check()||!style.drawCurves)
		return false;

	if (curve->getNeedRefresh()) curve->refresh(zoom/2);

	unsigned i;

	//draw curves
	glLineWidth(style.curveWidth);
	glBegin(GL_LINE_STRIP);
		style.curveColor.gl();

		for (i = 0; i < curve->discPts.size(); i++) {
			glVertex2f(curve->discPts.at(i).v.x,curve->discPts.at(i).v.y);
		}

		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);
		glVertex2f(curve->discPts.at(curve->discPts.size()-1).v.x,curve->discPts.at(curve->discPts.size()-1).v.y);
		glEnd();
	}
/*
	glPointSize(5.0f);
	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
	if(style.drawCurvePoints)
	{
		glPointSize(style.curvePointSize);
		glBegin(GL_POINTS);
		glVertex2f(curve->discPts[0].v.x, curve->discPts[0].v.y);
		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);
		}
		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
 *
 */

bool OE_display::drawCurveControl(OE_curve * curve, bool select)
{
	if (!curve->check()||!style.drawCurves||!style.drawCurveControls)
		return false;

	if (OE_pointcurve * tmpcurve = dynamic_cast<OE_pointcurve*> (curve))
	{
		if(select)
		{

			// Points
			glPointSize(4.0f);

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

			for (unsigned i = 0; i < tmpcurve->pts.size(); i++) {

				glLoadName(i);
				//vector_2d* p = &tmpcurve->pts[i];
				glBegin(GL_POINTS);
				glVertex2f(tmpcurve->pts[i].x,tmpcurve->pts[i].y);
				glEnd();
			}

			glPopName();
			glPopName();

		}else
		{
			// Tangeant lines
			glLineWidth(1.5);
			style.curveTangeantColor.gl();
			glBegin(GL_LINES);
			for (unsigned i = 0; i < tmpcurve->pts.size()-3; i += 3) {
				vector_2d* p = &tmpcurve->pts[i];
				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);
			}
			glEnd();


			// Points
			glPointSize(style.curvePointSize);
			//glColor4ubv(controlEndPointColor);

			glBegin(GL_POINTS);

			style.curvePointColor.gl();

			for (unsigned i = 0; i < tmpcurve->pts.size()-3; i += 3) {
				vector_2d* p = &tmpcurve->pts[i];
				glVertex2f(p[0].x,p[0].y);
			}

			glEnd();

			// tangeant end points
			glPointSize(style.curvePointSize);
			style.curvePointColor.gl();
			glBegin(GL_POINTS);

			for (unsigned i = 0; i < curve->pts.size()-3; i += 3) {
				vector_2d* p = &curve->pts[i];
				glVertex2f(p[1].x,p[1].y);
				glVertex2f(p[2].x,p[2].y);
			}
			glEnd();

			// start point & direction

			/*glColor4f(1.0,0.0,0.0,1.0);
			glPointSize(2.0f);
			glBegin(GL_POINTS);
			glVertex2f(curve->pts[0].x,curve->pts[0].y);
			glEnd();*/
			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];
			glVertex2f(curve->pts[0].x,curve->pts[0].y);
			//glVertex2f(curve->pts[1].x,curve->pts[1].y);
			glVertex2f(p.x,p.y);
			glEnd();
		}

		return true;
	}

	//if this curve is a joincurve

	if (OE_joincurve * joincurve = dynamic_cast<OE_joincurve*> (curve))
	{
		std::list<OE_subcurve*>::iterator curve = joincurve->curves.begin();
		if(!select)
		{
			while (curve != joincurve->curves.end())
			{
				OE_curve * tmpcurve = (*curve);
				glLineWidth(3);
				glBegin(GL_LINE_STRIP);
				style.curveTangeantColor.gl();

				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

	if (OE_subcurve * subcurve = dynamic_cast<OE_subcurve*> (curve))
	{
		if(select)
		{
			// Points
			glPointSize(4.0f);

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

			glBegin(GL_POINTS);
			glVertex2f(subcurve->pts[0].x,subcurve->pts[0].y);
			glEnd();

			glLoadName(1);
			glBegin(GL_POINTS);
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x,subcurve->pts[subcurve->getNpts()-1].y);
			glEnd();

			glPopName();
			glPopName();

		}else
		{
			style.curvePointColor.gl();
			glPointSize(5.0f);
			glBegin(GL_POINTS);
			glVertex2f(subcurve->pts[0].x,subcurve->pts[0].y);
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x,subcurve->pts[subcurve->getNpts()-1].y);
			glEnd();
			// start point & direction
			style.curveStartPointColor.gl();
			drawTriangle(subcurve->pts[0], subcurve->pts[1], 13);
		}


		return true;
	}
}


bool OE_display::drawJoincurveControl(OE_joincurve * curve, bool select)
{
	if (select)
	{
	for (unsigned i = 0; i < curve->curves.size(); i++) {

		OE_subcurve* subcurve = curve->getCurve(i);

		glLoadName(i*2+1);
		glBegin(GL_POINTS);
		glVertex2f(subcurve->pts[0].x,subcurve->pts[0].y);
		glEnd();

		glLoadName(i*2);
		glBegin(GL_POINTS);
		glVertex2f(subcurve->pts[subcurve->getNpts()-1].x,subcurve->pts[subcurve->getNpts()-1].y);
		glEnd();
	}
	}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();

			for (unsigned i = 0; i < subcurve->discPts.size(); i++) {
					glVertex2f(subcurve->discPts.at(i).v.x,subcurve->discPts.at(i).v.y);
			}
			glEnd();


			// draw the subcurves controls
			style.curveStartPointColor.gl();
			glPointSize(6.0f);
			glBegin(GL_POINTS);
			glVertex2f(subcurve->pts[0].x,subcurve->pts[0].y);
			glVertex2f(subcurve->pts[subcurve->getNpts()-1].x,subcurve->pts[subcurve->getNpts()-1].y);

			glEnd();
			// start point & direction
			style.curveStartPointColor.gl();
			drawTriangle(subcurve->pts[0], subcurve->pts[1], 13);
		}
	}
}

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

bool OE_display::drawStitchControl(OE_stitchs * stitch, bool select)
{

	if (!stitch->check()||!style.drawStitches||!style.drawStitchControl)
		return false;

	if (OE_linestitch * linestitch = dynamic_cast<OE_linestitch*> (stitch))
	{
		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];

		if(select)
		{

			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index
			if (linestitch->getJoincurve())
				drawJoincurveControl(linestitch->getJoincurve(),true);
			glPopName();
			glLoadName(1);
			glPushName(0); //init the controller points index
				glBegin(GL_TRIANGLES);
				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);

				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);
				glEnd();
			glPopName();
			glPopName();
		}else
		{
			glLineWidth(2);
			glBegin(GL_LINE_STRIP);
			style.stitchLineBeforeCurrentColor.gl();
				glVertex2f(joinCurve->pts[0].x,joinCurve->pts[0].y);
				glVertex2f(dir.x,dir.y);
			glEnd();

			drawJoincurveControl(joinCurve,false);

			glBegin(GL_TRIANGLES);
			style.stitchStartPointColor.gl();
			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);

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

		}
		return true;
	}

	if (OE_birailstitch * birailstitch = dynamic_cast<OE_birailstitch*> (stitch))
	{
		if(select)
		{

			// Points
			glPointSize(4.0f);

			glPushName(0); //init the controller type index
			glPushName(0); //init the controller points index
			drawJoincurveControl(birailstitch->getJoincurve1(),true);
			glPopName();
			glLoadName(1);
			glPushName(0); //init the controller points index
			drawJoincurveControl(birailstitch->getJoincurve2(),true);
			glPopName();
			glPopName();
		}else
		{
			drawJoincurveControl(birailstitch->getJoincurve1(),false);
			drawJoincurveControl(birailstitch->getJoincurve2(),false);
		}
		return true;
	}

	if (OE_linkstitch * linkstitch = dynamic_cast<OE_linkstitch*> (stitch))
	{
		if (!select)
		{
			// step stitchs
			glPointSize(3.0f);
			style.stitchPointBeforeCurrentColor.gl();
			glBegin(GL_POINTS);

			for (unsigned i = 0; i < linkstitch->stepPts.size(); i++) {
				vector_2d* p = &linkstitch->stepPts[i];
				glVertex2f(p[1].x,p[1].y);
			}
			glEnd();
		}
		return true;
	}
}

/** \brief draw the instruction points
 *
 * \return true if all is ok
 *
 */

bool OE_display::drawCommands()
{
	if (document->instPoints.size() == 0)
		return false;

	unsigned i;

	// Points
	glPointSize(style.instPointSize);
	glBegin(GL_POINTS);
		style.instLineBeforeCurrentColor.gl();
		for (i = 1; i < document->curPoint; i++) {
			glVertex2f(document->instPoints.at(i).x,document->instPoints.at(i).y);
		}
		style.instLineAfterCurrentColor.gl();
		for (i = document->curPoint; i < document->instPoints.size(); i++) {
			glVertex2f(document->instPoints.at(i).x,document->instPoints.at(i).y);
		}
	glEnd();


	//draw lines

	unsigned pt = 0;
	glLineWidth(style.instLineWidth);
	glBegin(GL_LINE_STRIP);

		for (i = 0; i < document->instCommand.size(); i++) {

			for (; pt < document->instCommand.at(i)->getIdPoint(); pt++)
				glVertex2f(document->instPoints.at(pt).x,document->instPoints.at(pt).y);

			OE_waitcolor* color = dynamic_cast<OE_waitcolor*> (document->instCommand.at(i));
			if (color)
			{
				glColor4ubv(color->getThreadColor());
			}
			OE_start* start = dynamic_cast<OE_start*> (document->instCommand.at(i));
			if (start)
			{
				glColor4ubv(start->getThreadColor());
			}
		}

		for (; pt < document->instPoints.size(); pt++)
			glVertex2f(document->instPoints.at(pt).x,document->instPoints.at(pt).y);

	glEnd();

	// start point
	style.instStartPointColor.gl();
	glPointSize(style.instStartPointSize);
	glBegin(GL_POINTS);
		glVertex2f(document->instPoints.at(0).x,document->instPoints.at(0).y);
	glEnd();

	if (document->curPoint < document->instPoints.size())
	{
		style.instCurPointColor.gl();
		glPointSize(style.instCurPointSize);
		glBegin(GL_POINTS);
			glVertex2f(document->instPoints.at(document->curPoint).x,document->instPoints.at(document->curPoint).y);
		glEnd();
	}

	return true;
}


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

bool OE_display::draw()
{
	if (!document)
		return false;

	bool select = false;

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

	//draw hoop
	if (style.drawHoop)
	{
		glLineWidth(3);
		glBegin(GL_LINE_STRIP);
			style.hoopColor.gl();
			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);
			glVertex2f(zero.x+hoopSize.x,zero.y-hoopSize.y);
			glVertex2f(zero.x-hoopSize.x,zero.y-hoopSize.y);
		glEnd();
	}

	//draw grid
	if (style.drawGrid)
	{
		glLineWidth(1.5);
		glBegin(GL_LINES);
			(style.gridColor * OE_Color(1,1,1, maxf(0.0f,0.2f-zoom*2))).gl();
			//glColor4d(style.gridColor.r, style.gridColor.g, style.gridColor.b, maxf(0.0f,0.2f-zoom*2));

			int offset = ( int)-hoopSize.x;
			while(offset<hoopSize.x)
			{
					glVertex2f(zero.x+offset,zero.y-hoopSize.y);
					glVertex2f(zero.x+offset,zero.y+hoopSize.y);
				offset += 1;
			}

			offset = ( int)-hoopSize.y;
			while(offset<hoopSize.y)
			{
					glVertex2f(zero.x-hoopSize.x,zero.y+offset);
					glVertex2f(zero.x+hoopSize.x,zero.y+offset);
				offset += 1;
			}

			//glColor4d(0.0f, 0.0f, 0.0f, 0.1f);
			style.gridColor.gl();

			offset = ( int)-hoopSize.x+( int)hoopSize.x%10;
			while(offset<hoopSize.x)
			{
				glVertex2f(zero.x+offset,zero.y-hoopSize.y);
				glVertex2f(zero.x+offset,zero.y+hoopSize.y);

				offset += 10;
			}

			offset = ( int)-hoopSize.y+( int)hoopSize.y%10;
			while(offset<hoopSize.y)
			{
					glVertex2f(zero.x-hoopSize.x,zero.y+offset);
					glVertex2f(zero.x+hoopSize.x,zero.y+offset);
				offset += 10;
			}
		glEnd();
	}

	//draw 0
	if (style.drawZero)
	{
		float zeroSize = style.zeroSize;

		glLineWidth(1.5);
		glBegin(GL_LINE_STRIP);
			style.zeroColor.gl();
			glVertex2f(zero.x-zeroSize,zero.y);
			glVertex2f(zero.x,zero.y+zeroSize);

			glVertex2f(zero.x+zeroSize,zero.y);
			glVertex2f(zero.x,zero.y-zeroSize);

			glVertex2f(zero.x-zeroSize,zero.y);
		glEnd();
		glBegin(GL_LINES);
			style.zeroColor.gl();
			glVertex2f(zero.x-zeroSize,zero.y);
			glVertex2f(zero.x+zeroSize,zero.y);

			glVertex2f(zero.x,zero.y-zeroSize);
			glVertex2f(zero.x,zero.y+zeroSize);
		glEnd();
	}


	document->lock();

	if (changeDpi)
		refresh();

	selectionBounds.init = false;
	if(style.drawCurves)
	{
		std::list<OE_pointcurve*>::iterator curve = document->curves.begin();
		while (curve != document->curves.end())
		{
			drawCurve((*curve));
			curve++;
		}

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

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

		stitch = document->selectedStitchs.begin();
		while (stitch != document->selectedStitchs.end())
		{
			drawStitchControl((*stitch));
			selectionBounds += (*stitch)->getBound();
			stitch++;
		}
	}
	if(style.drawCommands)
		drawCommands();

	document->unlock();

	drawSelectionTools();

	return true;
}


bool OE_display::select(std::list<OE_pointcurve*> selectedCurves, std::list<OE_stitchs*> selectedStitches)
{
	std::list<OE_pointcurve*>::iterator curve;
	std::list<OE_stitchs*>::iterator stitch;
	int index;

	glLoadName(0); //id of the tools
/*	glPushName(0); //id of the zero tool

	//draw 0
	vector_2d zero = document->getZeroPoint();
	glPointSize(6);
	glBegin(GL_POINT);
		glVertex2f(zero.x,zero.y);
	glEnd();
	glPopName();
*/

	glPushName(1); //id of the move tool
	drawSelectionTools(true);
	glPopName();

	if (changeDpi)
		refresh();

	if(style.selectStitches)
	{
		glLoadName(1); //id of the stitches

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

		stitch = selectedStitches.begin();
		while (stitch != selectedStitches.end())
		{
			drawStitchControl((*stitch), true);
			stitch++;
			index++;
			glLoadName(index);
		}
		glPopName();
	}

	if(style.selectCurves)