| | 98 | } |
| | 99 | |
| | 100 | private void HandleWarFoundryCoreArmyChanged (Army oldValue, Army newValue) |
| | 101 | { |
| | 102 | CommandStack.Reset(); |
| | 103 | loadedFilePath = null; |
| | 104 | layout.actionSaveArmy.Enabled = false; |
| | 105 | SetPointsPanelText(); |
| | 106 | SetAppTitle(); |
| | 107 | } |
| | 108 | |
| | 109 | private void SetPointsPanelText () |
| | 110 | { |
| | 111 | //TODO: implement panel and points panel |
| | 112 | } |
| | 113 | |
| | 114 | |
| | 115 | private void SetAppTitle() |
| | 116 | { |
| | 117 | string str = AppTitle; |
| | 118 | |
| | 119 | if (CurrentGameSystem!=null) |
| | 120 | { |
| | 121 | str+= " - " + CurrentGameSystem.Name; |
| | 122 | |
| | 123 | if (CurrentArmy!=null) |
| | 124 | { |
| | 125 | str+= " - " + CurrentArmy.Name; |
| | 126 | } |
| | 127 | } |
| | 128 | |
| | 129 | this.WindowTitle = str; |
| | 130 | } |
| | 131 | |
| | 132 | public CommandStack CommandStack |
| | 133 | { |
| | 134 | get |
| | 135 | { |
| | 136 | if (commandStack == null) |
| | 137 | { |
| | 138 | commandStack = new CommandStack(); |
| | 139 | } |
| | 140 | |
| | 141 | return commandStack; |
| | 142 | } |
| | 143 | } |
| | 144 | |
| | 145 | private void HandleCommandStackCommandStackUpdated () |
| | 146 | { |
| | 147 | |
| | 148 | } |
| | 149 | |
| | 150 | private void UndoAction() |
| | 151 | { |
| | 152 | if (commandStack.CanUndo()) |
| | 153 | { |
| | 154 | commandStack.Undo(); |
| | 155 | } |
| | 156 | } |
| | 157 | |
| | 158 | private void RedoAction() |
| | 159 | { |
| | 160 | if (commandStack.CanRedo()) |
| | 161 | { |
| | 162 | commandStack.Redo(); |
| | 163 | } |
| | 164 | } |
| | 165 | |
| | 166 | private bool SaveCurrentArmy() |
| | 167 | { |
| | 168 | bool saved = false; |
| | 169 | |
| | 170 | string filePath = loadedFilePath; |
| | 171 | |
| | 172 | if (filePath == null) |
| | 173 | { |
| | 174 | filePath = PromptForArmyFilePath(); |
| | 175 | } |
| | 176 | |
| | 177 | if (filePath != null) |
| | 178 | { |
| | 179 | saved = SaveCurrentArmyToFile(filePath); |
| | 180 | } |
| | 181 | |
| | 182 | return saved; |
| | 183 | } |
| | 184 | |
| | 185 | private bool SaveCurrentArmyAs() |
| | 186 | { |
| | 187 | bool saved = false; |
| | 188 | string filePath = PromptForArmyFilePath(); |
| | 189 | |
| | 190 | if (filePath != null) |
| | 191 | { |
| | 192 | saved = SaveCurrentArmyToFile(filePath); |
| | 193 | } |
| | 194 | |
| | 195 | return saved; |
| | 196 | } |
| | 197 | |
| | 198 | private bool SaveCurrentArmyToFile(string filePath) |
| | 199 | { |
| | 200 | if (WarFoundrySaver.GetSaver().Save(CurrentArmy, filePath)) |
| | 201 | { |
| | 202 | loadedFilePath = filePath; |
| | 203 | layout.actionSaveArmy.Enabled = false; |
| | 204 | CommandStack.setCleanMark(); |
| | 205 | return true; |
| | 206 | } |
| | 207 | else |
| | 208 | { |
| | 209 | QMessageBox.Critical(this, "file save failed", "file save failed - check log for details"); |
| | 210 | return false; |
| | 211 | } |
| | 212 | } |
| | 213 | |
| | 214 | private string PromptForArmyFilePath() |
| | 215 | { |
| | 216 | int result = saveArmyDialog.Exec(); |
| | 217 | |
| | 218 | if (result == (int)QDialog.DialogCode.Accepted) |
| | 219 | { |
| | 220 | return saveArmyDialog.SelectedFiles()[0]; |
| | 221 | } |
| | 222 | else |
| | 223 | { |
| | 224 | return null; |
| | 225 | } |
| | 226 | } |
| | 227 | |
| | 228 | public GameSystem CurrentGameSystem |
| | 229 | { |
| | 230 | get { return WarFoundryCore.CurrentGameSystem; } |
| | 231 | set { WarFoundryCore.CurrentGameSystem = value; } |
| | 232 | } |
| | 233 | |
| | 234 | public Army CurrentArmy |
| | 235 | { |
| | 236 | get { return WarFoundryCore.CurrentArmy; } |
| | 237 | set { SetArmy(value); } |
| | 238 | } |
| | 239 | |
| | 240 | private void SetArmy(Army newArmy) |
| | 241 | { |
| | 242 | IgnoreArmy(CurrentArmy); |
| | 243 | CloseAllUnitWindows(); |
| | 244 | |
| | 245 | if (newArmy == null) |
| | 246 | { |
| | 247 | SetNullArmyState(); |
| | 248 | } |
| | 249 | else |
| | 250 | { |
| | 251 | WarFoundryCore.CurrentGameSystem = newArmy.GameSystem; |
| | 252 | ListenToArmy(newArmy); |
| | 253 | SetNonNullArmyState(newArmy); |
| | 254 | } |
| | 255 | |
| | 256 | WarFoundryCore.CurrentArmy = newArmy; |
| | 257 | } |
| | 258 | |
| | 259 | private void IgnoreArmy(Army oldArmy) |
| | 260 | { |
| | 261 | if (oldArmy != null) |
| | 262 | { |
| | 263 | oldArmy.UnitAdded -= UnitAddedMethod; |
| | 264 | oldArmy.UnitRemoved -= UnitRemovedMethod; |
| | 265 | oldArmy.PointsValueChanged -= PointsValueChangedMethod; |
| | 266 | } |
| | 267 | } |
| | 268 | private void UnitAddedMethod(object unitObj) |
| | 269 | { |
| | 270 | if (unitObj is Unit) |
| | 271 | { |
| | 272 | Unit unit = (Unit)unitObj; |
| | 273 | //TODO set error panel |
| | 274 | //sbErrorPanel.Text = ""; |
| | 275 | } |
| | 276 | } |
| | 277 | |
| | 278 | private void UnitRemovedMethod(object unitObj) |
| | 279 | { |
| | 280 | if (unitObj is Unit) |
| | 281 | { |
| | 282 | Unit unit = (Unit)unitObj; |
| | 283 | //TODO set error panel |
| | 284 | //sbErrorPanel.Text = ""; |
| | 285 | |
| | 286 | //TODO check if window is open, and close it if it is |
| | 287 | |
| | 288 | } |
| | 289 | } |
| | 290 | |
| | 291 | private void PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal) |
| | 292 | { |
| | 293 | if (obj is Army) |
| | 294 | { |
| | 295 | SetPointsPanelText(); |
| | 296 | } |
| | 297 | } |
| | 298 | |
| | 299 | private void CloseAllUnitWindows() |
| | 300 | { |
| | 301 | // FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows); |
| | 302 | // |
| | 303 | // foreach (FrmUnit window in unitForms) |
| | 304 | // { |
| | 305 | // window.Close(); |
| | 306 | // } |
| | 307 | // |
| | 308 | // unitWindows.Clear(); |
| | 309 | } |
| | 310 | |
| | 311 | private void ListenToArmy(Army newArmy) |
| | 312 | { |
| | 313 | if (newArmy != null) |
| | 314 | { |
| | 315 | newArmy.UnitAdded += UnitAddedMethod; |
| | 316 | newArmy.UnitRemoved += UnitRemovedMethod; |
| | 317 | newArmy.PointsValueChanged += PointsValueChangedMethod; |
| | 318 | } |
| | 319 | } |
| | 320 | |
| | 321 | private void SetNullArmyState() |
| | 322 | { |
| | 323 | layout.actionSaveArmyAs.Enabled = false; |
| | 324 | layout.actionCloseArmy.Enabled = false; |
| | 325 | layout.menuExportArmyAs.Enabled = false; |
| | 326 | DisableCategoryButtons(); |
| | 327 | } |
| | 328 | |
| | 329 | void DisableCategoryButtons () |
| | 330 | { |
| | 331 | //TODO handle category buttons |
| | 332 | } |
| | 333 | |
| | 334 | |
| | 335 | private void SetNonNullArmyState(Army newArmy) |
| | 336 | { |
| | 337 | SetCategoryButtons(newArmy.Race.Categories); |
| | 338 | EnableCategoryButtons(); |
| | 339 | layout.actionSaveArmyAs.Enabled = true; |
| | 340 | layout.actionCloseArmy.Enabled = true; |
| | 341 | layout.menuExportArmyAs.Enabled = true; |
| | 342 | } |
| | 343 | |
| | 344 | void SetCategoryButtons (Category[] categories) |
| | 345 | { |
| | 346 | //TODO create category buttons |
| | 347 | } |
| | 348 | |
| | 349 | void EnableCategoryButtons () |
| | 350 | { |
| | 351 | //TODO enable category buttons |