Sunday, July 02, 2006

ClassEdit Finished

I finally have a saveable CharClass Editor. Users can drag and drop skills, feats, and special abilities into their respective ListViews. These ListViews are custom controls by Glacial Components(ListView v1.3) available here . These allow embedded controls. Currently I am using this functionality to embed NumericUpDowns in the skills view to edit any bonuses. I am considering not haveing this option and simplying allowing the user to choose the Class's class skills.

In addition to this, I have built generic methods for saving and loading the resource Xml files.
They are here:
public static void SaveToFile ( T obj, string fName )
{
try
{
TextWriter w = new StreamWriter(fName);
XmlSerializer s = new XmlSerializer(typeof(T));
s.Serialize(w, obj);
w.Close();
}
catch ( Exception ex )
{
MessageBox.Show("Problem saving file: " + fName + " " + ex.Message);
}
}

public static void LoadFromFile ( T obj, string fName )
{
StreamReader str = null;
try
{
str = new StreamReader(fName);
XmlSerializer s = new XmlSerializer(typeof(T));
obj = (T)s.Deserialize(str);
str.Close();
}
catch ( Exception ex )
{
MessageBox.Show("Problem loading file: " + fName + " " + ex.Message);
}
}

These methods will serialize or deserialize any serializable object. In order to be serializeable, the data you want must be a public property, and you have to have a parameterless constructor. There are probably more restrictions, but I don't know them.

In order to use drag and drop, three event handlers are needed: ItemDrag, DragEnter, and DragDrop. For some controls the MouseDown event may need to be used instead of ItemDrag. The ItemDrag event is placed in the source control, and the DragEnter and DragDrop in the target. DragEnter checks to make sure the dragged object is the right type, and then changes the effect to Copy. The DragDrop is where the control creates a new object and places it in.

When a user drags one of these objects onto the Class, a value copy is made. This means that the new item is referenced to a separate piece of memory. Changes can be made independently to the object in the main list.

The rest of today, I will probably work on the RaceEdit form.

0 Comments:

Post a Comment

<< Home