更新设计.

pull/7/head
ChenX 7 years ago
parent 6bb435ffb6
commit 533ea55973

@ -0,0 +1,44 @@
import { LineUndoType } from './FileSystem.test';
/**
* ,使.
*
* @class EentiyDrawAdapter
*/
class EentiyDrawAdapter
{
Draw()
{
return undefined;
}
}
/**
* 线
*
* @class LineAdapter
* @extends {EentiyDrawAdapter}
*/
class LineAdapter extends EentiyDrawAdapter
{
Draw()
{
return undefined;
}
Update(type: number)
{
switch (type)
{
case LineUndoType.Full:
break;
case LineUndoType.StartPoint:
break;
case LineUndoType.EndPoint:
break;
default:
break;
}
}
}

@ -32,17 +32,109 @@ db.AppendEntity(ent);
,. ,.
, ,
###
->
....
undo:, Remove
redo:. A
->
undo:. P
redo:. P
->
undo:. P
redo: P
:,()
R:.
A,P. .
A P P P A P P
| A .
.
A A A A
A.
:.
:,.()
,,.
.
A (insert new A).
A (update A.redo)
cmdName:"Line" //对象的创建命令
[
Object1:[C]//对象保留的创建的记录
]
cmdName:"Move" //对象修改命令
[
Object1:[P],
ObjectN:[P],
ObjectM:[P],
...
]
cmdName:"Ohther" //假设这个是一个复杂度很高的命令
[
Object1:[A,P,A,P,A,P,P,P],.
ObjectN:[A,P,A,P,A,P,P,P],
ObjectM:[C,P,P,P,P] ,C
...
]
:id.
,idid.
ObjectId.
id..
*/ */
//CAD对象工厂,通过注册 和暴露的创建方法,动态创建对象
class CADFactory
{
private constructor() { }
private objectNameMap = new Map<string, any>();
private static factory = new CADFactory();
static RegisterObject(C)
{
let obj = new C();
this.factory.objectNameMap.set(obj.ClassName.toUpperCase(), () => new C());
}
static CreateObject(name: string): CADObject
{
let createF = this.factory.objectNameMap.get(name.toUpperCase());
if (createF)
return createF();
}
}
//所有cad对象的基类 //所有cad对象的基类
class CADObject class CADObject
{ {
protected db: Database;
//对象是否已经被删除 //对象是否已经被删除
private isErase: boolean = false; protected isErase: boolean = false;
protected objectId: ObjectId;
get Id(): ObjectId
{
return this.objectId;
}
get IsErase(): boolean get IsErase(): boolean
{ {
return this.isErase; return this.isErase;
@ -51,6 +143,12 @@ class CADObject
//-----------------------------File----------------------------- //-----------------------------File-----------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化 //对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//类名,保证序列化时得到正确的new
get ClassName(): string
{
return "CADObject";
}
//对象从文件中读取数据,初始化自身 //对象从文件中读取数据,初始化自身
FileIn(file: CADFile) FileIn(file: CADFile)
{ {
@ -63,8 +161,15 @@ class CADObject
ApplyPartialUndo(file: CADFile) ApplyPartialUndo(file: CADFile)
{ {
} }
//-----------------------------File End-----------------------------
} }
class ObjectId
{
private id: number;
private constructor() { }
}
//cad文件数据 //cad文件数据
class CADFile class CADFile
@ -77,86 +182,100 @@ class CommandHistoricRecord extends CADObject
//命令名称 //命令名称
commandName: string; commandName: string;
//历史记录表 //历史记录表
historicCol: HistoricRecord[]; historicCol: Map<number, HistoricRecord[]>;
} }
//历史记录 //历史记录
class HistoricRecord class HistoricRecord extends CADObject
{ {
objectId: number; undoData: CADObject;
undoData: any; redoData: CADObject;
redoData: any; userData: CADObject;
} }
class HistoricManage //历史记录管理
class HistoricManage extends CADObject
{ {
m_Db: Database; private curIndex: number = -1; //当前执行位置
curIndex: number = -1; //当前执行位置 private historicRecord: CommandHistoricRecord[] = [];//历史记录
historicRecord: CommandHistoricRecord[] = [];//历史记录
constructor(db: Database)
{
super();
this.db = db;
}
get UndoData(): CommandHistoricRecord get UndoData(): CommandHistoricRecord
{ {
if (this.historicRecord.length == 0) if (this.historicRecord.length === 0)
{ {
this.startCmd(""); this.StartCmd("");
} }
return this.historicRecord[this.historicRecord.length - 1]; return this.historicRecord[this.historicRecord.length - 1];
} }
startCmd(cmdName: string) StartCmd(cmdName: string)
{ {
this.historicRecord.splice(this.curIndex, this.historicRecord.length - this.curIndex); this.historicRecord.splice(this.curIndex, this.historicRecord.length - this.curIndex);
this.historicRecord.push(new CommandHistoricRecord()); this.historicRecord.push(new CommandHistoricRecord());
this.curIndex = this.historicRecord.length - 1; this.curIndex = this.historicRecord.length - 1;
} }
Undo() Undo(): boolean
{ {
let historicRec = this.historicRecord[this.curIndex]; let historicRec = this.historicRecord[this.curIndex];
if (!historicRec) if (!historicRec)
{ {
return; return false;
} }
let recList = historicRec.historicCol; for (let [id, recList] of historicRec.historicCol)
for (let i = recList.length; i--;) {
let obj = this.db.getObject(id);
for (let rec of recList)
{ {
let data = recList[i]; obj.ApplyPartialUndo(rec.undoData);
let obj = this.m_Db.getObject(data.objectId); }
obj.ApplyPartialUndo(data.undoData);
} }
this.curIndex--; this.curIndex--;
return true;
} }
Redo(cout: number) Redo()
{ {
let undoData = this.historicRecord[this.curIndex + 1]; let historicRec = this.historicRecord[this.curIndex + 1];
if (!undoData) if (!historicRec)
{ {
return; return false;
} }
for (let rec of undoData.historicCol) for (let [id, recList] of historicRec.historicCol)
{ {
let obj = this.m_Db.getObject(rec.objectId); let obj = this.db.getObject(id);
obj.ApplyPartialUndo(rec.redoData); for (let i = recList.length; i--;)
{
obj.ApplyPartialUndo(recList[i].redoData);
}
} }
this.curIndex++; this.curIndex++;
return true;
} }
} }
class Database extends CADObject class Database
{ {
className: "Database";
hm: HistoricManage; hm: HistoricManage;
//块表记录 //块表记录
blockTableCol: BlockTableRecord[]; blockTableCol: BlockTableRecord[];
//材质字典 //模型空间
ModelSpace: BlockTableRecord = new BlockTableRecord();
//材质字典...
objectCol = new Map<number, CADObject>(); objectCol = new Map<number, CADObject>();
constructor() constructor()
{ {
super(); this.hm = new HistoricManage(this);
this.objectCol.set(0, this);
} }
getObject(id: number): CADObject getObject(id: number): CADObject
@ -168,14 +287,6 @@ class Database extends CADObject
{ {
let id = this.objectCol.size; let id = this.objectCol.size;
this.objectCol.set(id, obj); this.objectCol.set(id, obj);
this.hm.UndoData.historicCol.push(
{
objectId: 0,
undoData: { type: 1, i: id },//remove
redoData: { type: 2, i: id },//add
}
)
} }
ApplyPartialUndo(data) ApplyPartialUndo(data)
{ {
@ -192,32 +303,18 @@ class Database extends CADObject
} }
class BlockTableRecord extends CADObject class BlockTableRecord extends CADObject
{ {
objectMap = new Map<number, CADObject>(); private objectMap = new Map<number, CADObject>();
} }
enum EntityUndoType enum EntityUndoType
{ {
} }
//
interface EntityData
{
id: number;
isErase: boolean;
}
/** //所有图元的基类
*
*
*
* @class Entity
*/
class Entity extends CADObject class Entity extends CADObject
{ {
m_Db: Database; m_Db: Database;
@ -228,89 +325,62 @@ class Entity extends CADObject
} }
/** //直线撤销类型
* 线 export enum LineUndoType
*
* @enum {number}
*/
enum LineUndoType
{ {
Full = 0, Full = 0,
StartPoint = 1, StartPoint = 1,
EndPoint = 2, EndPoint = 2,
} }
// //直线对象
interface LineData extends EntityData
{
startPoint: number[];
endPoint: number[];
}
/**
* 线
*
* @class Line
*/
class Line extends Entity class Line extends Entity
{ {
constructor() constructor()
{ {
super(); super();
} }
ApplyPartialUndo(data)
{ //-----------------------------File-----------------------------
switch (data.type) //对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//类名,保证序列化时得到正确的new
get ClassName(): string
{ {
case LineUndoType.Full: return "Line";
break;
case LineUndoType.StartPoint:
break;
default:
break;
}
} }
}
/** //对象从文件中读取数据,初始化自身
* ,使. FileIn(file: CADFile)
*
* @class EentiyDrawAdapter
*/
class EentiyDrawAdapter
{
Draw()
{ {
return undefined;
} }
} //对象将自身数据写入到文件.
FileOut(file: CADFile)
/**
* 线
*
* @class LineAdapter
* @extends {EentiyDrawAdapter}
*/
class LineAdapter extends EentiyDrawAdapter
{
Draw()
{ {
return undefined;
} }
ApplyPartialUndo(data)
Update(type: number)
{ {
switch (type) switch (data.type)
{ {
case LineUndoType.Full: case LineUndoType.Full:
break; break;
case LineUndoType.StartPoint: case LineUndoType.StartPoint:
break; break;
case LineUndoType.EndPoint:
break;
default: default:
break; break;
} }
} }
//-----------------------------File End-----------------------------
} }
CADFactory.RegisterObject(Line);
let l = CADFactory.CreateObject("Line");
console.log(l);
let db = new Database();
console.log(db.hm.Undo());
db.hm.Redo();
Loading…
Cancel
Save