#include "stdafx.h"
#include "Line.h"

#include <iostream>
using namespace std;

Line::Line(): id(++total){
	count++;
	cout << "create Line#" << id << endl;
	this->p1 = CPoint();
	this->p2 = CPoint();
	m_Color = BLACK;
	m_Pen = 1;

	buildEnclosingRectangle();
}

Line::Line(CPoint p1, CPoint p2, COLORREF aColor): id(++total){
	count++;
	cout << "create Line#" << id << endl;
	this->p1 = p1;
	this->p2 = p2;
	m_Color = aColor;
	m_Pen = 1;

	buildEnclosingRectangle();
}

Line::Line(const Line* o): id(++total){
	count++;
	cout << "create Line#" << id << " via cc" << endl;
	this->p1 = CPoint(o->p1);
	this->p2 = CPoint(o->p2);
	this->m_Color = o->m_Color;
	this->m_Pen = o->m_Pen;

	buildEnclosingRectangle();
}

Line::Line(const Line& o): id(++total){
	count++;
	cout << "create Line#" << id << " via cc" << endl;
	this->p1 = CPoint(o.p1);
	this->p2 = CPoint(o.p2);
	this->m_Color = o.m_Color;
	this->m_Pen = o.m_Pen;

	buildEnclosingRectangle();
}

Line::~Line(){
	count--;
	cout << "destroy Line#" << id << endl;
}

void Line::buildEnclosingRectangle(){
	// Define the enclosing rectangle
	m_EnclosingRect = CRect(this->p1, this->p2);
	m_EnclosingRect.NormalizeRect();
}

CPoint Line::getPoint(int n){
	if (n == 1)
		return p1;
	else
		return p2;
}

ostream& Line::print(ostream& os) const{
	return os << "Line#" << id << ": " << endl
		<< toString();
}

Line& Line::operator= (const Line& o){ 
	if(this == &o) 
		return *this;
	// call copy-assignment
	this->p1 = o.p1;
	this->p2 = o.p2;
	this->m_Color = o.m_Color;
	this->m_Pen = o.m_Pen;
	buildEnclosingRectangle();
	return *this;
}

string Line::toString() const {
	return "";//  Point1: " + p1->toString() + "  Point2: " + p2->toString();
}

int Line::operator==(const Line& o) const {
	return ((p1 == o.p1) && (p2 == o.p2)); // TODO: do we need to compare line color?
};


// Draw a Line object
void Line::Draw(CDC* pDC, CElement* pElement, string key){
	// Create a pen for this object and
	// initialize it to the object color and line width of 1 pixel
	CPen aPen;
	COLORREF aColor = (this == pElement)? SELECT_COLOR: m_Color;

	if(!aPen.CreatePen(PS_SOLID, m_Pen, aColor)){  
		// Pen creation failed. Abort the program.
		AfxMessageBox("Pen creation failed drawing a line", MB_OK);
		AfxAbort();
	}

	CPen* pOldPen = pDC->SelectObject(&aPen);  // Select the pen

	// Now draw the line
	pDC->MoveTo(p1);
	pDC->LineTo(p2);

	if (key != "")
		m_keyRectangle = drawKey(pDC, key);
	else
		m_keyRectangle = CRect(0,0,0,0);

	pDC->SelectObject(pOldPen);                // Restore the old pen
}

void Line::Move(CSize& aSize){           
	p1 += aSize;				// Move points
	p2 += aSize;
	m_EnclosingRect += aSize;	// Move the enclosing rectangle
}

// serialization
CArchive& Line::load(CArchive& ar) {
	CElement::load(ar);

	ar	>> p1 
		>> p2;

	buildEnclosingRectangle();

	return ar;
}

CArchive& Line::save(CArchive& ar) { 
	CElement::save(ar);

	ar	<< p1 
		<< p2;

	return ar;
}


// initialize static fields 
unsigned long int Line::count = 0;
unsigned long int Line::total = 0;
Соседние файлы в папке Sketcher05