昵称:烦夫子
类别:界面/平面设计师
年龄:38
现所在地:北京
主页浏览总数:24259
总积分:89
文章数:88
作品数:70
// Object.cpp: implementation of the Object class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SoftDraw.h"
#include "Object.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Object::Object (const Object *p)
{
num = 0;
data = NULL;
parent = (Object *) p;
linetype = PS_SOLID; //实线
linewidth = 1; //线宽为1
color = RGB(0,0,0); //背景色
plottype = 2;
}
Object::~Object()
{
Flush ();
}
//得到对象数量
int Object::GetNum () const
{
return num;
}
// 释放全部对象占用的内存
void Object::Flush ()
{
for (int i = 0; i < num; i++)
if (NULL != data[i])
{
delete data[i];
data[i] = NULL;
}
if(NULL != data)
delete [] data;
num = 0;
data = NULL;
}
// 检测对象名称和类型
int Object::IsLeaf () const
{
return (name == TObject || name == TPline|| name == TText || name == TTextArray
|| name == TPolyreg || name == TBSpline || name == TBSplineArray ||
name == TShape || name == TShapeArray || name == TRing || name == TRingArray ||
name == TPoint2D || name == TPoint2DArray || name == TLabel || name == TLabelArray
|| name == TPattern || name == TOther);
}
// 设置对像类型
void Object::SetObjType (ObjType objtype)
{
name = objtype;
}
// 获取对像类型
void Object::GetObjType (ObjType& objtype) const
{
ōbjtype = name;
}
//增加内存空间
BOOL Object::IncreaseSpace (int iIndex)
{
ASSERT (iIndex >= 0 && iIndex <= num);
if(num >= MAX_DATA)
return FALSE;
//一次分配的增量是10
long lOldNum = ((num - 1) / 10 + 1) * 10;
num++;
if(NULL != data && num <= lOldNum)
{
//缓存足够
if(num>1)
{
//移动数据
for (int i = num-1; i > iIndex; i--)
data[i] = data[i - 1];
}
}
else
{
//缓存不够,重新分配
long lNewNum = ((num - 1) / 10 + 1) * 10;
PObject *temp = NULL;
temp = new PObject[lNewNum];
if(NULL == temp)
return FALSE;
for(int m=0; m
if (num > 1)
{ //移动数据
for (int i = 0; i < num; i++)
temp[i] = (i < iIndex) ? data[i] : data[i - 1];
delete [] data;
}
data = temp;
}
return TRUE;
}
//释放内存空间, 对指定的对象进行操作
void Object::DecreaseSpace (int del)
{
ASSERT (del >= 0 && del < num);
if (num > 1)
{
for (int i = del; i < num-1; i++)
data[i] = data[i + 1];
}
num--;
}
// 对象复制
Object* Object::Duplicate (Object* aObject)
{
Object *p = NULL;
if(aObject->parent)
{
p = aObject->parent->GetNewObject ();
*p = *aObject;
}
return p;
}
//用新的对象替换现有对象
int Object::Replace (Object* oldObject, Object* newObject)
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
if (data[i] == oldObject) {
data[i] = newObject;
newObject->parent = this;
return TRUE;
}
return FALSE;
}
//从数据中去掉, 并不真正删除.由调用者删除aObject.
int Object::Del (Object* aObject)
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
if (data[i] == aObject) break;
if (i < num) {
DecreaseSpace (i);
return i;
}
else
return -1;
}
//向数据中加入, 并不复制.调用者不能删除aObject.
BOOL Object::Add (Object* aObject)
{
ASSERT (! IsLeaf ());
return Insert (aObject, num);
}
//向数据中加入, 并不复制.调用者不能删除aObject.
BOOL Object::Insert (Object* aObject, int pos)
{
ASSERT (! IsLeaf ());
if (pos < 0 || pos > num) pos = num;
if (!IncreaseSpace (pos))
return FALSE;
data[pos] = aObject;
aObject->parent = this;
return TRUE;
}
//对两个数据进行交换
void Object::Swap(int iFrom, int iTo)
{
if(num<=0) return;
if(iFrom<0 || iTo<0 || iFrom>=num || iTo>=num || iFrom==iTo) return;
Object *tmp;
tmp = data[iTo];
data[iTo] = data[iFrom];
data[iFrom] = tmp;
}
//得到最大最小对象
void Object::GetMinMax ()
{
Vector minp, maxp;
VectorRun tmpPt;
MinP = Vector (10000, 10000);
MaxP = Vector (-10000, -10000);
for (int i = 0; i < num; i++) {
data[i]->GetMinMax (minp, maxp);
MinP = tmpPt.VMin (minp, MinP);
MaxP = tmpPt.VMax (maxp, MaxP);
}
}
//得到最大最小矢量点
void Object::GetMinMax (Vector& minp, Vector& maxp)
{
GetMinMax ();
minp = MinP;
maxp = MaxP;
}
//得到新的对象
Object* Object::GetNewObject () const
{
ASSERT (! IsLeaf ());
return NULL;
}
//得到对象
Object* Object::GetObject (int index) const
{
ASSERT (! IsLeaf ());
if (index >= 0 && index < num && data)
return (data[index]);
else
return NULL;
}
//得到前一个对象
Object* Object::GetPrevious (const Object *cur) const
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
if (data[i] == cur)
return data[(i + num - 1) % num];
return NULL;
}
//得到下一个对象
Object* Object::GetNext (const Object *cur) const
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
if (data[i] == cur)
return data[(i + 1) % num];
return NULL;
}
//得到当前对象的序号
int Object::GetIndex (const Object *cur) const
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
if (*data[i] == *cur) return i;
return -1;
}
//得到对象的属性
void Object::GetProp(int& t,int& wd, COLORREF& c, int& el) const
{
t = linetype;
wd = linewidth;
c = color;
el = plottype;
}
//设置对象的属性
void Object::SetProp(int t,int wd, COLORREF c, int el)
{
linetype = t;
linewidth = wd;
color = c;
//0:忽略, 1:慢笔, 2:慢刀, 3:快笔, 4:快刀, 5:半刀
if (el >= 0 && el <= 5)
plottype = el;
else
plottype = 0;
}
//得到对象的尺寸
long Object::GetSize () const
{
long s = 0;
for (int i = 0; i < num; i++)
s += data[i]->GetSize ();
return s;
}
//重载操作符,对象加操作
Object& Object::operator += (Object& aObject)
{
ASSERT (name == aObject.name);
for (int i = 0; i < aObject.num; i++)
Add (aObject.data[i]);
for (i = aObject.num - 1; i >= 0; i--)
aObject.Del (aObject.data[i]);
return *this;
}
//对象是否相等
BOOL Object::operator == (const Object& aObject)
{
ASSERT (! IsLeaf ());
if (name != aObject.name || num != aObject.num)
return FALSE;
for (int i = 0; i < num; i++)
if (data[i] != aObject.data[i]) return FALSE;
return TRUE;
}
//对象赋值
Object& Object::operator = (const Object& aObject)
{
ASSERT (name == aObject.name && num == 0);
if (aObject.num > 0 && !IsLeaf ())
{
Object* p = NULL;
for (int i = 0; i < aObject.num; i++)
{
p = GetNewObject ();
if(NULL == p)
break;
*p = *aObject.data[i];
if (!Add (p))
delete p;
}
}
linetype = aObject.linetype;
color = aObject.color;
linewidth = aObject.linewidth;
plottype = aObject.plottype;
MinP = aObject.MinP;
MaxP = aObject.MaxP;
return *this;
}
//对象求反
BOOL Object::operator ^ (const Object& aObject)
{
ASSERT (! IsLeaf ());
if (name != aObject.name || num != aObject.num)
return FALSE;
for (int i = 0; i < num; i++)
if (! (*data[i] ^ *aObject.data[i])) return FALSE;
return TRUE;
}
//绘图模式色彩设置
COLORREF Object::GetColor (DrawMode mode)
{
switch (mode) {
case DNormal: return RGB ( 0, 160, 0); //正常模式下为绿色
case DSelected: return RGB (255, 0, 255);
case DGray: return RGB (192, 192, 192);
case DErase: return RGB (255, 255, 255);
default: return RGB (0, 255, 0);
}
}
void Object::TransformData (TransformFunc f)
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
data[i]->TransformData (f);
GetMinMax ();
}
// 画对象
void Object::Draw (CDC* dc, float scale, DrawMode mode, int dispmode) const
{
if (IsLeaf ()) {
if (dc->IsPrinting ())
FastDraw (dc, scale, mode, PrintTrans);
else
FastDraw (dc, scale, mode, NullTrans);
}
else {
for (int i = 0; i < num; i++)
data[i]->Draw (dc, scale, mode, dispmode);
}
}
void Object::FastDraw (CDC* dc, float scale, DrawMode mode, TransformFuncMode f) const
{
ASSERT (! IsLeaf ());
for (int i = 0; i < num; i++)
data[i]->FastDraw (dc, scale, mode, f);
}