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