Reading Programming

Jun 30, 2006 14:20 # 43207

xavierenigma *** posts about...

Infrastructure Design for 2D editing program

I'm currently developing a program with a 2D editing component which falls somewhere between a geometry visualization toolkit and a scene graph. There are several types of objects which may be used to construct the scenegraph. Some types of objects may be constructed using other types of objects. Editing occurs when an object is transformed or when a related object is transformed.

The infrastructure of objects is essentially a parent-child structure. Each parent contains an array [Actually an Java Vector for dynamic growth] of it's child objects. Because objectes may consist of multiple other objects, the strucutre is a bit more complex. Rather than creating a messy system with multiple parents, every object contains a second array [Vector] which references all of the object's relatives [Objects which already have parents but will be affected this object].

To demonstrate this, lets use some code (Though this is Java, it should be pretty straight forward)

public class MyObject {
MyObject parent;
Vector children;
Vector relatives;

//Constructor here

public void addChild(MyObject){ children.addElement(MyObect);
relatives.addElement(MyObject);}

public void addRelative(MyObect){ relatives.addElement(MyObject);}

public void render(){
for(Enumeration e=children.elements(); e.hasMoreElements(); )
((MyObject)e.nextElement()).render();
}//render this object then render children

public void update(){
for(Enumeration e=relatives.elements(); e.hasMoreElements(); )
((MyObject)e.nextElement()).notify(this);

for(Enumeration e=children.elements(); e.hasMoreElements(); )
((MyObject)e.nextElement()).update();

}//update this object, notify relatives, continue iteration through tree by updating child nodes

public void notify(MyObject modrelative){
//If necessary, notify relatives
}

}

This exemplifies a generic node. Specific nodes might be a point and line. They would extend the MyObject class inheriting the generic methods. A line will have a point as a parent. That point and another point will have the line as a relative. A point may potentially be the child of a line (When the point lies on the line) and hence, the line calls the point a relative. When render or update is called on the master node, every element will be effected and a family reunion ensues.

A word on editors:
In order to allow multiple editors, the system must share the mouse and other resources with editors. My current approach to sharing simply notifies an editor when it is active or not (Editors are activated when the objects they are associated with are selected. If multiple objects are selected, the editor for the object highest in the hiearchy is used). In actuality, all editors receive events through the conventions of the Java API's event pump (even when an editor is inactive).

I realize this is a very messy design. I'm curious to hear the ideas of the rest of the community. Feel free to provide examples using any programming language.


Small text Large text

Netalive Amp (Skin for Winamp)