I am trying to get my Clone method to work. This method is basically meant to return a clone of a passed in Object2D. It is currently meant to support PixmapShape and Group2D.
I have been having trouble with this for a long time and what I cannot find anything that suits my needs for drawing vector graphics so I have to sort out all out of stuff by myself. Any suggestions would be nice, definitely preferably that I don’t have to make another overhaul though.
Here is what I got:
Function Clone(extends o as Object2D) As Object2D
dim clone as Object2D
//clone original object
select case o
case isa PixmapShape
clone = new PixmapShape(PixmapShape(o).Image)
case isa Group2D
dim g as Group2D = Group2D(o)
clone = new Group2D
dim gclone as Group2D = Group2D(clone)
for i as integer = 0 to g.Count - 1
gclone.Append(g.Item(i).Clone)
next
case else
raise new UnsupportedFormatException
end select
clone.x = o.x
clone.y = o.y
clone.Rotation = o.Rotation
return clone
End Function
So the rotation and position is sadly off and it acts funny. Seems to be all right with PixmapShape on its own but cloning a group is not nice with this method and doesn’t work properly.
I’m a little confused by your “case isa Group2D” code. You append the g.Items to gclone…but then later you set the x,y, and rotation of clone. Could you just append the g.Items to clone itself? (note, I know nothing of Group2Ds, but the code seems funny to me).
Are you aware that Object2Ds work in absolute coordinates? Meaning when you modify a group2D it modifies the transform of all its children.
dim group As new Group2D
dim shape As new RectShape
shape.X = 200
//shape at <200, 0>, rotation 0, scale 1
group.Append(shape) //now shape is modified by group
group.X = 100
//shape at <300, 0>, rotation 0, scale 1
group.Rotation = 0.5
//shape at <275.5165, 95.88511>, rotation 0.5, scale 1
group.Scale = 1.5
//shape at <363.2748, 143.8277>, rotation 0.5, scale 1.5
You can even attach a shape to multiple groups and both will modify the shape. Anyways, when you clone the group you should apply the groups transform first then append the cloned shapes to it.
The other way to handle coordinates is relative, where shape would keep it’s transform of <200, 0> rotation 0, scale 1 throughout any group modifications. I find this much easier to work with because you don’t have to worry about coordinates changing underneath you and you can use the same shape in multiple places, but both coodinate systems have their uses.
Right. Translation and rotation do not happen at drawing time. They happen immediately when you set the property. All the child shapes are modified. So as Will says, you must set up the group before you add any children to it…
select case o
case isa PixmapShape
clone = new PixmapShape(PixmapShape(o).Image)
clone.x = o.x
clone.y = o.y
clone.Rotation = o.Rotation
case isa Group2D
dim g as Group2D = Group2D(o)
clone = new Group2D
clone.x = o.x
clone.y = o.y
clone.Rotation = o.Rotation
dim gclone as Group2D = Group2D(clone)
for i as integer = 0 to g.Count - 1
gclone.Append(g.Item(i).Clone)
next
case else
raise new UnsupportedFormatException
end select
return clone
Thanks for the help people. I will give this a look and see what I can do. I missed the posts thinking that noone had replied but obviously I am wrong. Help much appreciated!