Saturday, May 10, 2008

Refactoring

I have been looking at my code lately and it's time for another refactor. I've noticed that almost all of my SceneObject types require transformations. There are only a select few that do not yet I have implemented the transformation system through the generic property/command system. This worked well at the start but as I implement more complex, non-primitive, types for the editor, I'm realizing that it can be a pain to re-implement the same functionality over and over again.

I am currently working on a BoxTrigger which is composed of a SceneNode and a wireframe box (technically it doesn't require the SceneNode but I need it for later). I want to be able to translate, rotate, and scale this object but in order to do that, I have to forward the methods on to the SceneNode. I'd have to write all sorts of code to handle the transformations again when I really shouldn't have to.

My proposed solution, after looking at Sinbads wxOgreMVC code, is to implement the transforms on the base SceneObject class and then declare a way for each derived type to receive a notification when the transform changes. This will simply allow me to add the 'position', 'scale', and 'orientation' properties to my class and everything else should work.

The tools will need a very small rewrite to handle this but that shouldn't take more than 10 minutes. In order to transform a SceneObject, the tool should first check to see if the any of the 3 properties above are present. If they are, then the associated methods should be relevant. For instance, the translate tool should see if the 'position' property is available. If it is, it can then call 'translate', 'setPosition', 'getWorldPosition', etc knowing that they are fully implemented.

When it comes time to save objects, the transform information may require special handling. I believe that the 'position' property should be all that is required to be written out.

I'm doing some testing at the BoxTrigger level before I implement this at the base level but so far, everything has worked accordingly. I believe this transition should make all future objects a lot easier to implement and work with. Considering that most of the editor objects are concerned with transformation information, having it available at the base level should be a major feature.