// Constructors /// <summary> /// Basic default constructor. Must make separate method calls to initialize the properties /// </summary> public MainViewModel() { // Initialize our properties MainUnitFile = null; // UnitFile is initialized in InitMainUnitFile UnitEditor = new UnitEditorVM(); Armies = new StartingArmy(); // Subscribe to our events UnitEditor.PropertyChanged += OnUnitTabChange; Armies.PropertyChanged += OnArmiesChange; // As this is the entry point of the program, we should handle the some amount of // file directory work we need done. //System.IO.Directory.CreateDirectory("Backups"); // Sets up a folder for backup files //System.IO.Directory.CreateDirectory("Save Files"); // Sets up a folder for saving files // NOTE: On second thought, don't. We may not have access priviledges }
// Methods /// <summary> /// Listener for PropertChanged events /// </summary> /// <param name="obj">The object that fired the event</param> /// <param name="args">The arguments sent from the event</param> public void OnNameChange(object obj, PropertyChangedEventArgs args) { // First, we need to be dealing with the right stuff UnitEditorVM tempUE = obj as UnitEditorVM; if (tempUE == null) { // Not what we're looking for return; } if (tempUE.IsInitialLoad) { // We're just changing to a different unit, ignore return; } // Lastly, we're only looking to update the name here if (args.PropertyName.Equals("DispName")) { ViewName = tempUE.DispName; } }
// Event delegates protected void OnUnitTabChange(object obj, EventArgs args) { // Let's get some easier to read locals UnitEditorVM tempUE = obj as UnitEditorVM; string propChanged = (args as PropertyChangedEventArgs).PropertyName; // If obj isn't a UnitEditorVM... if (tempUE == null) { // ...Don't do anything return; } if (tempUE.IsInitialLoad) { // Ignore, no values are actually being changed return; } // Lastly, if we have errors on this property... if (!String.IsNullOrEmpty(tempUE[propChanged])) { // Don't update return; } // This switch handles the bulk of our updating to the model switch (propChanged) { case "DispName": tempUE.CurUnitName.TheUnit.DisplayName.Value = tempUE.DispName; // Also, propogate changes back to the NameList //tempUE.CurUnitName.ViewName = tempUE.DispName; break; case "Cost": // Divide by 1,000 to get the appropriate, data-file value tempUE.CurUnitName.TheUnit.Cost.Value = tempUE.Cost / 1000; break; case "Movement": tempUE.CurUnitName.TheUnit.Speed.Value = tempUE.Movement; break; case "SightRange": tempUE.CurUnitName.TheUnit.Vision = tempUE.SightRange; break; case "AirAtt": tempUE.CurUnitName.TheUnit.AirAttack.Value = tempUE.AirAtt; break; case "VehAtt": tempUE.CurUnitName.TheUnit.ArmorAttack.Value = tempUE.VehAtt; break; case "InfAtt": tempUE.CurUnitName.TheUnit.InfAttack.Value = tempUE.InfAtt; break; case "Def": tempUE.CurUnitName.TheUnit.Defense.Value = tempUE.Def; break; case "Health": tempUE.CurUnitName.TheUnit.HitPoints.Value = tempUE.Health; break; case "AttRange": tempUE.CurUnitName.TheUnit.AttackRange.Value = tempUE.AttRange; break; case "Fuel": tempUE.CurUnitName.TheUnit.GasTank.Value = tempUE.Fuel; break; case "CollDamage": tempUE.CurUnitName.TheUnit.CollateralDamage.Value = tempUE.CollDamage; break; case "IsIndirect": tempUE.CurUnitName.TheUnit.IsIndirect = tempUE.IsIndirect; break; case "IsSingleUse": tempUE.CurUnitName.TheUnit.IsSingleUse = tempUE.IsSingleUse; break; case "IsNotKept": tempUE.CurUnitName.TheUnit.IsNotKept = tempUE.IsNotKept; break; case "CanBuy": tempUE.CurUnitName.TheUnit.CanBuyFlag = tempUE.CanBuy; break; case "MoveType": tempUE.CurUnitName.TheUnit.MoveCat = tempUE.MoveType; break; case "ArmorType": tempUE.CurUnitName.TheUnit.UnitCat = tempUE.ArmorType; break; case "Faction": tempUE.CurUnitName.TheUnit.Faction = tempUE.UFaction; break; } }