Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.GTK
annotate FrmMainWindow.cs @ 17:7653fab9d70e
Re #58: Remove LogNotifier from API
* Remove dependency on IBBoard.Log4Net, since use in API will be removed as part of this ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 14 Jul 2009 18:33:52 +0000 |
parents | 85db2c9a1546 |
children | c4d9b1ec75ed |
rev | line source |
---|---|
5 | 1 // This file (FrmMainWindow.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2009 IBBoard. |
0 | 2 // |
5 | 3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. |
0 | 4 |
5 using System; | |
6 using System.IO; | |
7 using System.Collections.Generic; | |
8 using System.Configuration; | |
13 | 9 using Gtk; |
10 using IBBoard; | |
0 | 11 using IBBoard.Commands; |
12
685532d43a96
Re #88: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
12 using IBBoard.GtkSharp; |
0 | 13 using IBBoard.IO; |
13 | 14 using IBBoard.Lang; |
15 using IBBoard.Logging; | |
0 | 16 using IBBoard.CustomMath; |
17 using IBBoard.WarFoundry.API; | |
18 using IBBoard.WarFoundry.API.Factories; | |
13 | 19 using IBBoard.WarFoundry.API.Factories.Xml; |
20 using IBBoard.WarFoundry.API.Objects; | |
0 | 21 using IBBoard.WarFoundry.API.Commands; |
22 using IBBoard.WarFoundry.API.Savers; | |
23 using IBBoard.WarFoundry.API.Requirements; | |
6
cfc7683e73f9
Fix breakage from r33 by completing update of move to IBBoard.WarFoundry.GTK sub-packages
IBBoard <dev@ibboard.co.uk>
parents:
5
diff
changeset
|
24 using IBBoard.WarFoundry.GTK.Widgets; |
13 | 25 using IBBoard.WarFoundry.Plugin.Rollcall; |
0 | 26 using IBBoard.Xml; |
27 using log4net; | |
28 | |
13 | 29 namespace IBBoard.WarFoundry.GTK |
0 | 30 { |
31 public partial class FrmMainWindow: Gtk.Window | |
32 { | |
33 private static readonly string AppTitle = "WarFoundry"; | |
13 | 34 private const int CATEGORY_BUTTON_SEPARATOR_INDEX = 6; |
35 | |
36 private Preferences preferences; | |
37 private ILog logger = LogManager.GetLogger(typeof(FrmMainWindow)); | |
38 | |
0 | 39 private CommandStack commandStack; |
40 private Dictionary<ToolButton, Category> categoryMap = new Dictionary<ToolButton, Category>(); | |
13 | 41 private Dictionary<IBBoard.WarFoundry.API.Objects.Unit, UnitDisplayWidget> unitToWidgetMap = new Dictionary<IBBoard.WarFoundry.API.Objects.Unit,UnitDisplayWidget>(); |
42 | |
43 private ObjectAddDelegate UnitAddedMethod; | |
44 private ObjectRemoveDelegate UnitRemovedMethod; | |
45 private DoubleValChangedDelegate PointsValueChangedMethod; | |
0 | 46 private FailedUnitRequirementDelegate FailedUnitRequirementMethod; |
47 private StringValChangedDelegate UnitNameChangedMethod; | |
48 | |
49 private GameSystem system; | |
50 private string loadedArmyPath; | |
51 | |
52 private MenuToolButton undoMenuButton, redoMenuButton; | |
53 | |
54 public static void Main (string[] args) | |
13 | 55 { |
56 try | |
0 | 57 { |
58 Application.Init(); | |
59 FrmMainWindow win = new FrmMainWindow(args); | |
60 win.Show(); | |
61 Application.Run(); | |
13 | 62 LogManager.GetLogger(typeof(FrmMainWindow)).Debug("Application ended"); |
63 } | |
64 catch(Exception ex) | |
65 { | |
66 LogManager.GetLogger(typeof(FrmMainWindow)).Fatal("("+ex.GetType().Name+") "+ex.Message + Environment.NewLine + ex.StackTrace); | |
0 | 67 } |
68 } | |
69 | |
70 public FrmMainWindow() : this(new string[0]) | |
71 { | |
72 //Do nothing extra | |
73 } | |
74 | |
75 public FrmMainWindow (string[] args): base (Gtk.WindowType.Toplevel) | |
76 { | |
77 logger.Info("Opening FrmMainWindow"); | |
78 Build (); | |
79 //Replace the undo/redo buttons with menu versions, which Monodevelop's GUI editor doesn't currently support | |
80 redoMenuButton = new MenuToolButton("gtk-redo"); | |
81 redoMenuButton.Label = "Redo"; | |
10
c687bbe901f8
Re #37 - Resolve deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
82 redoMenuButton.TooltipText = "Redo"; |
0 | 83 redoMenuButton.Clicked+= redoTBButtonActivated; |
84 toolbar.Insert(redoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); | |
85 undoMenuButton = new MenuToolButton("gtk-undo"); | |
86 undoMenuButton.Label = "Undo"; | |
10
c687bbe901f8
Re #37 - Resolve deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
87 undoMenuButton.TooltipText = "Undo"; |
0 | 88 undoMenuButton.Clicked+= undoTBButtonActivated; |
89 toolbar.Insert(undoMenuButton, CATEGORY_BUTTON_SEPARATOR_INDEX); | |
90 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-1]); | |
91 toolbar.Remove(toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX-2]); | |
92 toolbar.ShowAll(); | |
93 | |
94 Title = AppTitle; | |
95 TreeViewColumn mainColumn = new TreeViewColumn (); | |
96 mainColumn.Title = "Army Categories"; | |
97 CellRendererText mainCell = new CellRendererText (); | |
98 mainColumn.PackStart (mainCell, true); | |
99 treeUnits.AppendColumn(mainColumn); | |
100 mainColumn.SetCellDataFunc(mainCell, new TreeCellDataFunc(RenderCategoryTreeObjectName)); | |
101 treeUnits.Model = new TreeStore(typeof(WarFoundryObject)); | |
102 logger.Debug("Loading preferences"); | |
103 Preferences = new Preferences("WarFoundryGTK"); | |
104 logger.Debug("Loading translations"); | |
8
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
105 |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
106 try |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
107 { |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
108 Translation.InitialiseTranslations(Constants.ExecutablePath, Preferences["language"].ToString()); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
109 } |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
110 catch (TranslationLoadException ex) |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
111 { |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
112 logger.Error(ex); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
113 MessageDialog dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, ex.Message); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
114 dialog.Title = "Translation loading failed"; |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
115 dialog.Run(); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
116 dialog.Destroy(); |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
117 } |
20c5fd0bb79d
Fixes #5 - Stop missing translations being fatal
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
118 |
13 | 119 logger.Debug("Initialising"); |
120 commandStack = new CommandStack(); | |
0 | 121 commandStack.CommandStackUpdated+=new MethodInvoker(commandStack_CommandStackUpdated); |
122 WarFoundryCore.GameSystemChanged+= new GameSystemChangedDelegate(OnGameSystemChanged); | |
123 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(OnArmyChanged); | |
124 Destroyed+= new EventHandler(OnWindowDestroyed); | |
125 //TODO: Translate and subscribe to other events | |
13 | 126 UnitAddedMethod = new ObjectAddDelegate(OnUnitAdded); |
127 UnitRemovedMethod = new ObjectRemoveDelegate(OnUnitRemoved); | |
128 PointsValueChangedMethod = new DoubleValChangedDelegate(OnPointsValueChanged); | |
0 | 129 FailedUnitRequirementMethod = new FailedUnitRequirementDelegate(OnFailedUnitRequirement); |
130 UnitNameChangedMethod = new StringValChangedDelegate(OnUnitNameChanged); | |
131 | |
132 //FIXME: Temporary hack to add paths and factories | |
133 WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + "data")); | |
134 IWarFoundryFactory factory = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(typeof(WarFoundryXmlFactory)); | |
135 | |
136 if (factory!=null && factory is WarFoundryXmlFactory) | |
137 { | |
138 WarFoundryLoader.GetDefault().RegisterFactory((WarFoundryXmlFactory)factory); | |
139 } | |
140 | |
141 factory = WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(typeof(RollcallFactory)); | |
142 | |
143 if (factory!=null && factory is RollcallFactory) | |
144 { | |
145 WarFoundryLoader.GetDefault().RegisterNonNativeFactory((INonNativeWarFoundryFactory)factory); | |
146 } | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
147 |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
148 logger.Debug("Initialising complete - seeing if we can load default army or system"); |
0 | 149 |
150 if (args.Length == 1) | |
151 { | |
152 logger.Debug("Attempting to load from file"); | |
153 FileInfo file = new FileInfo(args[0]); | |
154 | |
155 try | |
156 { | |
5 | 157 ICollection<IWarFoundryObject> objects = WarFoundryLoader.GetDefault().LoadFile(file); |
158 | |
159 if (objects.Count == 1) | |
0 | 160 { |
5 | 161 List<IWarFoundryObject> objectList = new List<IWarFoundryObject>(); |
162 objectList.AddRange(objects); | |
163 IWarFoundryObject loadedObject = objectList[0]; | |
164 | |
165 if (loadedObject is Army) | |
166 { | |
167 WarFoundryCore.CurrentArmy = (Army)loadedObject; | |
168 logger.InfoFormat("Loaded army from {0}", file.FullName); | |
169 } | |
170 else if (loadedObject is GameSystem) | |
171 { | |
172 WarFoundryCore.CurrentGameSystem = (GameSystem)loadedObject; | |
173 logger.InfoFormat("Loaded game system from {0}", file.FullName); | |
174 } | |
0 | 175 } |
176 } | |
177 catch (InvalidFileException ex) | |
178 { | |
179 //TODO: show error dialog | |
180 logger.Error(ex); | |
181 } | |
182 } | |
183 else | |
184 { | |
185 string gameSystemID = Preferences.GetStringProperty("currSystem"); | |
186 | |
187 if (gameSystemID!=null && !"".Equals(gameSystemID)) | |
188 { | |
189 logger.Debug("Attempting to load current game system from properties"); | |
190 GameSystem sys = WarFoundryLoader.GetDefault().GetGameSystem(gameSystemID); | |
191 | |
192 if (sys!=null) | |
193 { | |
194 WarFoundryCore.CurrentGameSystem = sys; | |
195 logger.InfoFormat("Loaded game system {0} from properties", gameSystemID); | |
196 } | |
197 } | |
198 } | |
199 } | |
200 | |
201 private void RenderCategoryTreeObjectName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) | |
202 { | |
203 object o = model.GetValue(iter, 0); | |
204 | |
205 if (o is ArmyCategory) | |
206 { | |
207 ArmyCategory c = (ArmyCategory)o; | |
208 string name = ""; | |
209 | |
210 if (Preferences.GetBooleanProperty("ShowCatPercentage")) | |
211 { | |
212 name = Translation.GetTranslation("categoryTreeCatName", "{0} - {1}pts", c.Name, c.PointsTotal); | |
213 } | |
214 else | |
215 { | |
216 name = Translation.GetTranslation("categoryTreeCatNamePercentage", "{0} - {1}pts ({2}%)", c.Name, c.PointsTotal, (c.ParentArmy.PointsTotal > 0 ? Math.Round((c.PointsTotal / c.ParentArmy.PointsTotal) * 100) : 0)); | |
217 } | |
218 | |
219 (cell as CellRendererText).Text = name; | |
220 } | |
221 else if (o is IBBoard.WarFoundry.API.Objects.Unit) | |
222 { | |
223 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; | |
224 string name = Translation.GetTranslation("categoryTreeCatName", "{0} - {1}pts", u.Name, u.PointsValue); | |
225 (cell as CellRendererText).Text = name; | |
226 } | |
227 } | |
228 | |
229 private void OnWindowDestroyed(object source, EventArgs args) | |
230 { | |
231 logger.Info("Exiting"); | |
232 Application.Quit(); | |
233 } | |
234 | |
235 private void OnUnitNameChanged(WarFoundryObject val, string oldValue, string newValue) | |
236 { | |
237 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val; | |
238 UnitDisplayWidget widget; | |
239 unitToWidgetMap.TryGetValue(unit, out widget); | |
240 | |
241 if (widget!=null) | |
242 { | |
243 unitsNotebook.SetTabLabelText(widget, newValue); | |
244 } | |
245 } | |
246 | |
247 private void OnUnitAdded(WarFoundryObject val) | |
248 { | |
249 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)val; | |
250 unit.NameChanged+= UnitNameChangedMethod; | |
251 AddUnitToTree(unit); | |
252 } | |
253 | |
254 private void AddUnitToTree(IBBoard.WarFoundry.API.Objects.Unit unit) | |
255 { | |
256 TreeStore model = (TreeStore)treeUnits.Model; | |
257 TreeIter iter; | |
258 model.GetIterFirst(out iter); | |
259 | |
260 do | |
261 { | |
262 object obj = model.GetValue(iter, 0); | |
263 | |
264 if (obj is ArmyCategory) | |
265 { | |
266 ArmyCategory cat = (ArmyCategory)obj; | |
267 | |
268 if (cat.Equals(unit.Category)) | |
269 { | |
270 model.AppendValues(iter, unit); | |
271 TreePath path = model.GetPath(iter); | |
272 treeUnits.ExpandToPath(path); | |
273 } | |
274 } | |
275 } | |
276 while (model.IterNext(ref iter)); | |
277 } | |
278 | |
279 private void OnUnitRemoved(WarFoundryObject obj) | |
280 { | |
281 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj; | |
282 unit.NameChanged-= UnitNameChangedMethod; | |
283 RemoveUnitFromTree(unit); | |
284 | |
285 //See if unit has a tab open and close it if it does | |
286 } | |
287 | |
288 private void RemoveUnitFromTree(IBBoard.WarFoundry.API.Objects.Unit unit) | |
289 { | |
290 TreeStore model = (TreeStore)treeUnits.Model; | |
291 TreeIter iter; | |
292 model.GetIterFirst(out iter); | |
293 bool removed = false; | |
294 | |
295 do | |
296 { | |
297 object obj = model.GetValue(iter, 0); | |
298 | |
299 if (obj is ArmyCategory) | |
300 { | |
301 ArmyCategory cat = (ArmyCategory)obj; | |
302 | |
303 if (unit.Category == null || cat.Equals(unit.Category)) | |
304 { | |
305 TreeIter innerIter; | |
306 model.IterChildren(out innerIter, iter); | |
307 | |
308 do | |
309 { | |
310 object innerObj = model.GetValue(innerIter, 0); | |
311 | |
312 if (unit.Equals(innerObj)) | |
313 { | |
314 model.Remove(ref innerIter); | |
315 removed = true; | |
316 break; | |
317 } | |
318 } | |
319 while (model.IterNext(ref innerIter)); | |
320 | |
321 if (removed) | |
322 { | |
323 break; | |
324 } | |
325 } | |
326 } | |
327 } | |
328 while (model.IterNext(ref iter)); | |
329 } | |
330 | |
331 private void OnPointsValueChanged(WarFoundryObject obj, double before, double after) | |
332 { | |
333 //Set points in panel | |
334 } | |
335 | |
336 private void OnFailedUnitRequirement(List<FailedUnitRequirement> failedRequirement) | |
337 { | |
338 //Show error message in panel | |
339 } | |
340 | |
341 public Preferences Preferences | |
342 { | |
343 get { return preferences; } | |
344 set { preferences = value; } | |
345 } | |
346 | |
347 /*public AbstractNativeWarFoundryFactory Factory | |
348 { | |
349 get { return WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(Constants.ExecutablePath, factoryType); } | |
350 }*/ | |
351 | |
352 protected void OnDeleteEvent (object sender, DeleteEventArgs a) | |
353 { | |
354 Application.Quit (); | |
355 a.RetVal = true; | |
356 } | |
357 | |
358 protected virtual void OnExitActivated(object sender, System.EventArgs e) | |
359 { | |
360 Application.Quit(); | |
361 } | |
362 | |
363 protected virtual void OnChangeGameSystemActivated(object sender, System.EventArgs e) | |
364 { | |
365 ChangeCurrentGameSystem(); | |
366 } | |
367 | |
368 protected virtual void OnCreateArmyActivated(object sender, System.EventArgs e) | |
369 { | |
370 CreateNewArmy(); | |
371 } | |
372 | |
373 protected virtual void OnReloadFilesActivated(object sender, System.EventArgs e) | |
374 { | |
375 } | |
376 | |
377 protected virtual void OnSaveArmyAsActivated(object sender, System.EventArgs e) | |
378 { | |
379 SaveCurrentArmyAs(); | |
380 } | |
381 | |
382 protected virtual void OnCloseArmyActivated(object sender, System.EventArgs e) | |
383 { | |
384 CloseCurrentArmy(); | |
385 } | |
386 | |
387 protected virtual void OnOpenArmyActivated(object sender, System.EventArgs e) | |
388 { | |
389 OpenArmy(); | |
390 } | |
391 | |
392 protected virtual void OnSaveArmyActivated(object sender, System.EventArgs e) | |
393 { | |
394 SaveCurrentArmy(); | |
395 } | |
396 | |
397 protected virtual void OnAddUnitActivated(object sender, System.EventArgs e) | |
398 { | |
399 if (sender is ToolButton) | |
400 { | |
401 Category cat = null; | |
402 categoryMap.TryGetValue((ToolButton)sender, out cat); | |
403 | |
404 if (cat!=null) | |
405 { | |
406 logger.DebugFormat("Show FrmNewUnit for {0}", cat.Name); | |
407 FrmNewUnit newUnit = new FrmNewUnit(WarFoundryCore.CurrentArmy.Race, cat, WarFoundryCore.CurrentArmy); | |
408 ResponseType response = (ResponseType)newUnit.Run(); | |
409 newUnit.Hide(); | |
410 | |
411 if (response==ResponseType.Ok) | |
412 { | |
14
abbf8a3ac431
Re #90: Stop new units showing up twice
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
413 CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, WarFoundryCore.CurrentArmy.GetCategory(cat)); |
0 | 414 commandStack.Execute(cmd); |
415 } | |
416 | |
417 newUnit.Dispose(); | |
418 } | |
419 } | |
420 } | |
13 | 421 |
422 public CommandStack CommandStack | |
423 { | |
424 get { return commandStack; } | |
0 | 425 } |
426 | |
427 private void SetAppTitle() | |
428 { | |
429 if (WarFoundryCore.CurrentArmy!=null) | |
430 { | |
431 Title = AppTitle + " - " + WarFoundryCore.CurrentGameSystem.Name + " - " + WarFoundryCore.CurrentArmy.Name; | |
432 } | |
433 else if (WarFoundryCore.CurrentGameSystem!=null) | |
434 { | |
435 Title = AppTitle + " - " + WarFoundryCore.CurrentGameSystem.Name; | |
436 } | |
437 else | |
438 { | |
439 Title = AppTitle; | |
440 } | |
441 } | |
442 | |
443 private void OnGameSystemChanged(GameSystem oldSys, GameSystem newSys) | |
444 { | |
445 system = newSys; | |
446 SetAppTitle(); | |
447 miCreateArmy.Sensitive = system!=null; | |
448 newArmyButton.Sensitive = system!=null; | |
449 RemoveCategoryButtons(); | |
450 | |
451 if (system!=null) | |
452 { | |
453 AddCategoryButtons(system.Categories); | |
454 } | |
455 } | |
456 | |
457 private void OnArmyChanged(Army oldArmy, Army newArmy) | |
458 { | |
459 loadedArmyPath = null; | |
460 SetAppTitle(); | |
461 SetArmyTree(newArmy); | |
462 | |
463 if (oldArmy!=null) | |
13 | 464 { |
465 oldArmy.UnitAdded-= UnitAddedMethod; | |
466 oldArmy.UnitRemoved-= UnitRemovedMethod; | |
467 oldArmy.PointsValueChanged-= PointsValueChangedMethod; | |
0 | 468 oldArmy.FailedRequirement-=FailedUnitRequirementMethod; |
469 } | |
470 | |
471 unitToWidgetMap.Clear(); | |
472 | |
473 while (unitsNotebook.NPages > 0) | |
474 { | |
475 unitsNotebook.RemovePage(0); | |
476 } | |
477 | |
13 | 478 if (newArmy==null) |
479 { | |
480 DisableCategoryButtons(); | |
481 } | |
482 else | |
483 { | |
484 newArmy.UnitAdded+= UnitAddedMethod; | |
485 newArmy.UnitRemoved+= UnitRemovedMethod; | |
486 newArmy.PointsValueChanged+= PointsValueChangedMethod; | |
487 newArmy.FailedRequirement+=FailedUnitRequirementMethod; | |
488 //TODO: Clear all buttons | |
489 EnableCategoryButtons(); | |
490 | |
491 if (newArmy.Race.HasCategoryOverrides()) | |
492 { | |
493 RemoveCategoryButtons(); | |
494 AddCategoryButtons(newArmy.Race.Categories); | |
495 } | |
0 | 496 } |
497 | |
498 miCloseArmy.Sensitive = newArmy!=null; | |
499 miSaveArmyAs.Sensitive = newArmy!=null; | |
500 //New army has no changes, so we can't save it | |
501 miSaveArmy.Sensitive = false; | |
13 | 502 saveArmyButton.Sensitive = false; |
503 | |
504 CommandStack.Reset(); | |
0 | 505 SetPointsPanelText(); |
506 } | |
507 | |
508 private void SetArmyTree(Army army) | |
509 { | |
510 logger.Debug("Resetting tree"); | |
511 TreeStore store = (TreeStore)treeUnits.Model; | |
512 store.Clear(); | |
513 TreeIter iter; | |
514 | |
515 if (army!=null) | |
516 { | |
517 logger.Debug("Loading in categories to tree"); | |
518 | |
519 foreach (ArmyCategory cat in army.Categories) | |
520 { | |
521 logger.DebugFormat("Append category {0}", cat.Name); | |
522 iter = store.AppendValues(cat); | |
523 | |
524 foreach (IBBoard.WarFoundry.API.Objects.Unit unit in cat.GetUnits()) | |
525 { | |
526 store.AppendValues(iter, unit); | |
527 } | |
528 } | |
529 | |
530 logger.Debug("Finished loading tree categories"); | |
531 } | |
532 } | |
533 | |
534 private void DisableCategoryButtons() | |
535 { | |
536 SetCategoryButtonsSensitive(false); | |
537 } | |
538 | |
539 private void EnableCategoryButtons() | |
540 { | |
541 SetCategoryButtonsSensitive(true); | |
542 } | |
543 | |
544 private void SetCategoryButtonsSensitive(bool state) | |
545 { | |
546 int toolbarButtonCount = toolbar.Children.Length - 1; | |
547 logger.Debug("Last button index: "+toolbarButtonCount); | |
548 | |
549 for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--) | |
550 { | |
551 logger.DebugFormat("Setting button {0} state to {1}", i, state); | |
552 toolbar.Children[i].Sensitive = state; | |
553 } | |
554 } | |
555 | |
556 private void RemoveCategoryButtons() | |
557 { | |
558 int toolbarButtonCount = toolbar.Children.Length - 1; | |
559 | |
560 for (int i = toolbarButtonCount; i > CATEGORY_BUTTON_SEPARATOR_INDEX; i--) | |
561 { | |
562 toolbar.Remove(toolbar.Children[i]); | |
563 } | |
564 | |
565 categoryMap.Clear(); | |
566 } | |
567 | |
568 private void AddCategoryButtons(Category[] cats) | |
569 { | |
570 if (cats!=null && cats.Length > 0) | |
571 { | |
572 logger.DebugFormat("Toolbar button count: {0}. Adding {1} categories.", toolbar.Children.Length, cats.Length); | |
573 | |
574 foreach (Category cat in cats) | |
575 { | |
576 ToolButton button = new ToolButton("gtk-add"); | |
577 button.Label = cat.Name; | |
10
c687bbe901f8
Re #37 - Resolve deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
578 button.TooltipText = "Add unit from "+cat.Name; |
0 | 579 //TODO: See if we can associate data in some way, the same as we can with SWF. For now we just use the map. |
580 categoryMap.Add(button, cat); | |
581 button.Clicked+= new System.EventHandler(OnAddUnitActivated); | |
582 toolbar.Insert(button, -1); | |
583 } | |
584 } | |
585 | |
586 toolbar.Children[CATEGORY_BUTTON_SEPARATOR_INDEX].Visible = cats!=null && cats.Length>0; | |
587 | |
588 toolbar.ShowAll(); | |
589 } | |
590 | |
591 private void SetPointsPanelText() | |
592 { | |
593 //TODO: Set the points value in the status bar | |
594 } | |
595 | |
13 | 596 private void commandStack_CommandStackUpdated() |
597 { | |
598 undoMenuButton.Sensitive = commandStack.CanUndo(); | |
599 miUndo.Sensitive = undoMenuButton.Sensitive; | |
600 redoMenuButton.Sensitive = commandStack.CanRedo(); | |
601 miRedo.Sensitive = redoMenuButton.Sensitive; | |
0 | 602 int redoLength = commandStack.RedoLength; |
13 | 603 //TODO: Build menus for undo/redo and find way of adding tooltips |
604 /*int maxRedo = Math.Min(10, redoLength); | |
605 MenuItem[] menuItems = null; | |
606 | |
607 if (redoLength > 0) | |
608 { | |
609 menuItems = new MenuItem[maxRedo]; | |
610 Command com; | |
611 MenuItem mi; | |
612 | |
613 for (int i = 0; i < maxRedo; i++) | |
614 { | |
615 com = commandStack.PeekRedoCommand(i+1); | |
616 | |
617 if (com == null) | |
618 { | |
619 break; | |
620 } | |
621 | |
622 mi = new MenuItem(com.Description); | |
623 mi.Click+=new EventHandler(redoMenu_Click); | |
624 menuItems[i] = mi; | |
625 } | |
626 } | |
627 | |
628 redoMenu.MenuItems.Clear(); | |
629 | |
630 if (menuItems!=null && menuItems[0]!=null) | |
631 { | |
632 bttnRedo.ToolTipText = menuItems[0].Text; | |
633 redoMenu.MenuItems.AddRange(menuItems); | |
0 | 634 }*/ |
635 //TODO: Put above code back when we have a dropdown version of the redo button | |
636 if (redoLength > 0) | |
637 { | |
638 //redoMenuButton.Tooltip = CommandStack.PeekRedoCommand().Description; | |
13 | 639 } |
640 | |
641 int undoLength = commandStack.UndoLength; | |
642 /*int maxUndo = Math.Min(10, undoLength); | |
643 MenuItem[] menuItemsUndo = null; | |
644 | |
645 if (undoLength > 0) | |
646 { | |
647 menuItemsUndo = new MenuItem[maxUndo]; | |
648 Command com; | |
649 MenuItem mi; | |
650 | |
651 for (int i = 0; i < maxUndo; i++) | |
652 { | |
653 com = commandStack.PeekUndoCommand(i+1); | |
654 | |
655 if (com == null) | |
656 { | |
657 break; | |
658 } | |
659 | |
660 mi = new MenuItem(com.UndoDescription); | |
661 mi.Click+=new EventHandler(undoMenu_Click); | |
662 menuItemsUndo[i] = mi; | |
663 } | |
664 } | |
665 | |
666 undoMenu.MenuItems.Clear(); | |
667 | |
668 if (menuItemsUndo!=null && menuItemsUndo[0]!=null) | |
669 { | |
670 bttnUndo.ToolTipText = menuItemsUndo[0].Text; | |
671 undoMenu.MenuItems.AddRange(menuItemsUndo); | |
0 | 672 }*/ |
13 | 673 //TODO: Put above code back when we have a dropdown version of the undo button |
0 | 674 if (undoLength > 0) |
675 { | |
676 //undoMenuButton.Tooltip = CommandStack.PeekUndoCommand().UndoDescription; | |
677 } | |
13 | 678 |
679 saveArmyButton.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null && CanSave(); | |
680 miSaveArmy.Sensitive = commandStack.IsDirty() && WarFoundryCore.CurrentArmy!=null && CanSave(); | |
0 | 681 } |
682 | |
683 private bool CanSave() | |
684 { | |
685 return loadedArmyPath!=null && WarFoundryCore.CurrentArmy!=null && WarFoundrySaver.GetSaver()!=null; | |
13 | 686 } |
687 | |
688 private bool SaveCurrentArmyOrSaveAs() | |
689 { | |
690 if (CanSave()) | |
691 { | |
692 return SaveCurrentArmy(); | |
693 } | |
694 else | |
695 { | |
696 return SaveCurrentArmyAs(); | |
697 } | |
0 | 698 } |
699 | |
700 private bool OpenArmy() | |
701 { | |
702 //TODO: Open dialog for file selection then open army | |
703 bool success = false; | |
704 loadedArmyPath = null;//TODO: Set loaded file path | |
705 return success; | |
13 | 706 } |
707 | |
708 private bool SaveCurrentArmy() | |
0 | 709 { |
710 bool success = false; | |
13 | 711 |
712 if (CanSave()) | |
0 | 713 { |
714 try | |
13 | 715 { |
716 if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, loadedArmyPath)) | |
0 | 717 { |
718 saveArmyButton.Sensitive = false; | |
13 | 719 miSaveArmy.Sensitive = false; |
720 CommandStack.setCleanMark(); | |
721 success = true; | |
0 | 722 } |
723 } | |
724 catch (IOException ex) | |
725 { | |
726 logger.Error("Saving army failed", ex); | |
727 MessageDialog md = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "An error occured while saving the army. Please check the logs for details on what failed"); | |
728 md.Show(); | |
13 | 729 } |
0 | 730 } |
731 | |
13 | 732 return success; |
733 } | |
734 | |
735 private bool SaveCurrentArmyAs() | |
736 { | |
737 /*if (saveArmyDialog.Filter == "") | |
738 { | |
739 string savePath = UserDataPath+Constants.DirectoryString+"armies"+Constants.DirectoryString; | |
740 | |
741 if (!Directory.Exists(savePath)) | |
742 { | |
743 Directory.CreateDirectory(savePath); | |
744 } | |
745 | |
746 saveArmyDialog.InitialDirectory = savePath; | |
747 saveArmyDialog.Filter = Translation.GetTranslation("armyFileFilter")+"|*.army"; | |
748 saveArmyDialog.Title = Translation.GetTranslation("saveArmyDialog"); | |
749 } | |
750 | |
751 DialogResult dr = saveArmyDialog.ShowDialog(this); | |
752 | |
753 if (dr == DialogResult.OK) | |
754 { | |
755 if (WarFoundrySaver.GetSaver().Save(WarFoundryCore.CurrentArmy, saveArmyDialog.FileName)) | |
756 { | |
757 miSaveArmy.Enabled = false; | |
758 bttnSaveArmy.Enabled = false; | |
0 | 759 CommandStack.setCleanMark(); |
13 | 760 loadedArmyPath = saveArmyDialog.FileName; |
761 return true; | |
762 } | |
763 else | |
764 { | |
765 MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error); | |
766 return false; | |
767 } | |
768 } | |
769 else | |
770 { | |
771 return false; | |
0 | 772 }*/ |
13 | 773 return false; |
0 | 774 } |
775 | |
13 | 776 private bool CloseCurrentArmy() |
777 { | |
778 if (WarFoundryCore.CurrentArmy!=null) | |
779 { | |
780 bool canClose = false; | |
781 | |
782 if (CommandStack.IsDirty()) | |
0 | 783 { |
13 | 784 MessageDialog dia = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo | ButtonsType.Cancel, "The army \""+WarFoundryCore.CurrentArmy.Name+"\" has been modified.\r\nSave changes before closing army?"); |
0 | 785 ResponseType dr = (ResponseType)dia.Run(); |
786 | |
13 | 787 if (dr == ResponseType.Yes) |
0 | 788 { |
789 //They want to save so try to save it or prompt for save as | |
13 | 790 //If they cancel the save as then assume they don't want to close |
791 canClose = SaveCurrentArmyOrSaveAs(); | |
792 } | |
793 else if (dr == ResponseType.No) | |
0 | 794 { |
13 | 795 //They don't care about their changes |
796 canClose = true; | |
0 | 797 } |
798 else | |
799 { | |
800 //Assume cancel or close with the X button | |
801 canClose = false; | |
802 } | |
803 | |
13 | 804 dia.Dispose(); |
805 } | |
806 else | |
807 { | |
808 //Nothing has changed so we can safely close | |
809 canClose = true; | |
810 } | |
811 | |
812 if (canClose) | |
0 | 813 { |
13 | 814 //do close |
815 WarFoundryCore.CurrentArmy = null; | |
816 return true; | |
817 } | |
818 else | |
819 { | |
820 return false; | |
821 } | |
822 } | |
823 else | |
824 { | |
825 //pretend we succeeded | |
826 return true; | |
827 } | |
828 } | |
829 | |
830 private void CreateNewArmy() | |
0 | 831 { |
832 logger.Debug("Create new army"); | |
833 FrmNewArmy newArmy = new FrmNewArmy(WarFoundryCore.CurrentGameSystem); | |
834 ResponseType type = (ResponseType)newArmy.Run(); | |
835 newArmy.Hide(); | |
836 | |
837 if (type == ResponseType.Ok) | |
13 | 838 { |
839 if (CloseCurrentArmy()) | |
0 | 840 { |
841 WarFoundryCore.CurrentArmy = new Army(newArmy.SelectedRace, newArmy.ArmyName, newArmy.ArmySize); | |
842 } | |
843 } | |
844 else | |
845 { | |
846 logger.Debug("Create new army cancelled"); | |
847 } | |
848 | |
13 | 849 newArmy.Destroy(); |
0 | 850 } |
851 | |
852 private void ChangeCurrentGameSystem() | |
853 { | |
854 logger.Debug("Changing game system"); | |
855 FrmChangeGameSystem dialog = new FrmChangeGameSystem(this); | |
856 ResponseType type = (ResponseType)dialog.Run(); | |
857 dialog.Hide(); | |
858 | |
859 if (type == ResponseType.Ok) | |
860 { | |
861 WarFoundryCore.CurrentGameSystem = dialog.SelectedSystem; | |
862 } | |
863 else | |
864 { | |
865 logger.Debug("Game system change cancelled"); | |
866 } | |
867 | |
868 dialog.Destroy(); | |
869 } | |
870 | |
871 protected virtual void undoTBButtonActivated (object sender, System.EventArgs e) | |
872 { | |
873 CommandStack.Undo(); | |
874 } | |
875 | |
876 protected virtual void redoTBButtonActivated (object sender, System.EventArgs e) | |
877 { | |
878 CommandStack.Redo(); | |
879 } | |
880 | |
881 protected virtual void saveTBButtonActivated (object sender, System.EventArgs e) | |
882 { | |
883 SaveCurrentArmy(); | |
884 } | |
885 | |
886 protected virtual void openTBButtonActivated (object sender, System.EventArgs e) | |
887 { | |
888 OpenArmy(); | |
889 } | |
890 | |
891 protected virtual void newTBButtonActivated (object sender, System.EventArgs e) | |
892 { | |
893 CreateNewArmy(); | |
894 } | |
895 | |
896 protected virtual void ArmyRowActivated (object o, Gtk.RowActivatedArgs args) | |
897 { | |
898 TreeModel model = treeUnits.Model; | |
899 TreeIter iter; | |
900 model.GetIter(out iter, args.Path); | |
901 object obj = model.GetValue(iter, 0); | |
902 | |
903 if (obj is IBBoard.WarFoundry.API.Objects.Unit) | |
904 { | |
905 IBBoard.WarFoundry.API.Objects.Unit unit = (IBBoard.WarFoundry.API.Objects.Unit)obj; | |
906 | |
907 UnitDisplayWidget widget; | |
908 unitToWidgetMap.TryGetValue(unit, out widget); | |
909 | |
910 if (widget!=null) | |
911 { | |
912 logger.DebugFormat("Selecting existing page for "+unit.Name); | |
913 unitsNotebook.Page = unitsNotebook.PageNum(widget); | |
914 } | |
915 else | |
916 { | |
917 widget = new UnitDisplayWidget(unit, CommandStack); | |
918 logger.Debug("Adding page for "+unit.Name); | |
919 unitToWidgetMap[unit] = widget; | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
920 widget.Destroyed+= new EventHandler(UnitWidgetDestroyed); |
12
685532d43a96
Re #88: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
921 int pageNum = NotebookUtil.AddPageToNotebookWithCloseButton(unitsNotebook, widget, unit.Name); |
0 | 922 logger.Debug("Page added at index "+pageNum); |
923 unitsNotebook.ShowAll(); | |
924 unitsNotebook.Page = pageNum; | |
925 } | |
926 } | |
927 } | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
928 |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
929 private void UnitWidgetDestroyed(object sender, EventArgs e) |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
930 { |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
931 if (sender is UnitDisplayWidget) |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
932 { |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
933 unitToWidgetMap.Remove(((UnitDisplayWidget)sender).Unit); |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
934 } |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
14
diff
changeset
|
935 } |
0 | 936 } |
937 } |