comparison FrmMain.cs @ 0:7dd160dacb60

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 42cf06b8f897
comparison
equal deleted inserted replaced
-1:000000000000 0:7dd160dacb60
1 using System;
2 using System.Drawing;
3 using System.Drawing.Drawing2D;
4 using System.Collections;
5 using System.ComponentModel;
6 using System.Windows.Forms;
7 using System.Data;
8 using System.IO;
9 using System.Threading;
10 using IBBoard;
11 using IBBoard.CustomMath;
12 using IBBoard.Commands;
13 using IBBoard.IO;
14 using IBBoard.Lang;
15 using IBBoard.WarFoundry.API;
16 using IBBoard.WarFoundry.API.Commands;
17 using IBBoard.Windows.Forms;
18 using IBBoard.Xml;
19 using log4net;
20
21 namespace IBBoard.WarFoundry
22 {
23 /// <summary>
24 /// Summary description for Form1.
25 /// </summary>
26 public class FrmMain : System.Windows.Forms.Form
27 {
28 private static readonly string AppTitle = "WarFoundry";
29
30 private Preferences preferences;
31 protected readonly ILog logger = LogManager.GetLogger(typeof(WarFoundryFactory));
32
33 private CommandStack commandStack;
34 private ToolBarButton[] categoryButtons;
35 private Type factoryType = typeof(WarFoundryXmlFactory);
36
37 public ObjectAddDelegate UnitAddedMethod;
38 public ObjectRemoveDelegate UnitRemovedMethod;
39 public DoubleValChangedDelegate PointsValueChangedMethod;
40 //public FailedUnitRequirementDelegate FailedUnitRequirementMethod;
41
42 private FrmArmyTree armyTree;
43 private FrmDebugOutput debugWindow;
44
45 private IBBoard.Windows.Forms.ColorableStatusBar statusBar;
46 private System.Windows.Forms.ToolBar toolBar;
47 private System.Windows.Forms.MainMenu mainMenu;
48 private IBBoard.Windows.Forms.IBBMenuItem menuFile;
49 private IBBoard.Windows.Forms.IBBMenuItem menuHelp;
50 private IBBoard.Windows.Forms.IBBToolBarButton bttnNewArmy;
51 private System.Windows.Forms.ImageList buttonIcons;
52 private IBBoard.Windows.Forms.IBBToolBarButton bttnSaveArmy;
53 private System.Windows.Forms.OpenFileDialog openArmyDialog;
54 private System.Windows.Forms.SaveFileDialog saveArmyDialog;
55 private IBBoard.Windows.Forms.IBBMenuItem miExit;
56 private IBBoard.Windows.Forms.IBBToolBarButton bttnOpenArmy;
57 private IBBoard.Windows.Forms.IBBMenuItem miAbout;
58 private System.ComponentModel.IContainer components;
59 private IBBoard.Windows.Forms.IBBMenuItem miNewArmy;
60 private IBBoard.Windows.Forms.IBBMenuItem miOpenArmy;
61 private IBBoard.Windows.Forms.IBBMenuItem miCloseArmy;
62 private IBBoard.Windows.Forms.IBBMenuItem miSaveArmy;
63 private IBBoard.Windows.Forms.IBBMenuItem miSaveArmyAs;
64 private IBBoard.Windows.Forms.IBBMenuItem miChangeSystem;
65 private IBBoard.Windows.Forms.IBBToolBarButton bttnSep1;
66 private IBBoard.Windows.Forms.IBBToolBarButton bttnUndo;
67 private IBBoard.Windows.Forms.IBBToolBarButton bttnRedo;
68 private IBBoard.Windows.Forms.IBBMenuItem menuEdit;
69 private IBBoard.Windows.Forms.IBBMenuItem miUndo;
70 private IBBoard.Windows.Forms.IBBMenuItem miRedo;
71 private IBBoard.Windows.Forms.IBBMenuItem miDebugWindow;
72 private System.Windows.Forms.MenuItem miSep1;
73 private System.Windows.Forms.MenuItem miSep2;
74 private System.Windows.Forms.MenuItem miSep3;
75 private IBBoard.Windows.Forms.ColorableStatusBarPanel sbMainPanel;
76 private IBBoard.Windows.Forms.ColorableStatusBarPanel sbErrorPanel;
77 private IBBoard.Windows.Forms.ColorableStatusBarPanel sbPointsPanel;
78 private System.Windows.Forms.ContextMenu undoMenu;
79 private System.Windows.Forms.ContextMenu redoMenu;
80 private IBBoard.Windows.Forms.IBBMenuItem miReloadFiles;
81 private System.Windows.Forms.Timer statusBarTimer;
82 private System.Windows.Forms.Panel pnlRight;
83
84 public FrmMain(string[] args)
85 {
86 this.Closing+=new CancelEventHandler(FrmMain_Closing);
87 CommandStack.CommandStackUpdated+=new MethodInvoker(commandStack_CommandStackUpdated);
88
89 InitializeComponent();
90
91 Preferences = new Preferences("WarFoundry");
92 Translation.InitialiseTranslations(Constants.ExecutablePath, Preferences["language"].ToString());
93
94 //pnlRight.Left = ClientSize.Width - pnlRight.Width - 2;
95 //pnlRight.Top = toolBar.Height + 5;
96 //pnlRight.Height = ClientRectangle.Bottom - statusBar.Height - pnlRight.Top - 3;
97
98 foreach (Control ctrl in Controls)
99 {
100 Translation.TranslateControl(ctrl);
101 }
102
103 foreach(Component comp in components.Components)
104 {
105 Translation.TranslateComponent(comp);
106 }
107
108 foreach (IBBMenuItem mi in Menu.MenuItems)
109 {
110 Translation.TranslateComponent(mi);
111 }
112
113 Translation.TranslateComponent(openArmyDialog);
114 Translation.TranslateComponent(saveArmyDialog);
115
116 WarFoundryCore.GameSystemChanged+= new GameSystemChangedDelegate(FrmMain_GameSystemChanged);
117 WarFoundryCore.ArmyChanged += new ArmyChangedDelegate(FrmMain_ArmyChanged);
118 UnitAddedMethod = new ObjectAddDelegate(FrmMain_UnitAddedMethod);
119 UnitRemovedMethod = new ObjectRemoveDelegate(FrmMain_UnitRemovedMethod);
120 PointsValueChangedMethod = new DoubleValChangedDelegate(FrmMain_PointsValueChangedMethod);
121 //FailedUnitRequirementMethod = new FailedUnitRequirementDelegate(FrmMain_FailedUnitRequirement);
122
123 sbErrorPanel.Color = Color.Red;
124
125 armyTree = new FrmArmyTree(CommandStack);
126 armyTree.MdiParent = this;
127 armyTree.Show();
128 armyTree.StartPosition = FormStartPosition.Manual;
129 armyTree.Location = new Point(this.DisplayRectangle.Width - armyTree.Width - 10, 10);
130 Translation.TranslateControl(armyTree);
131
132 if (args.Length == 1)
133 {
134 logger.Debug("Attempting to load from file");
135 FileInfo file = new FileInfo(args[0]);
136
137 try
138 {
139 if (file.Extension.Equals("."+Factory.GetArmyFileExtension()))
140 {
141 WarFoundryCore.CurrentArmy = Factory.CreateArmyFromFile(file);
142 logger.InfoFormat("Loaded army from {0}", file.FullName);
143 }
144 else if (file.Extension.Equals("."+Factory.GetSystemFileExtension()))
145 {
146 WarFoundryCore.CurrentGameSystem = Factory.CreateGameSystemFromFile(file);
147 logger.InfoFormat("Loaded game system from {0}", file.FullName);
148 }
149 }
150 catch (InvalidFileException ex)
151 {
152 MessageBox.Show(Translation.GetTranslation("InvalidFileLoadError", "The file loaded ({0}) was not a valid WarFoundry file", file.FullName), Translation.GetTranslation("InvalidFileLoadTitle", "Invalid data file", null), MessageBoxButtons.OK, MessageBoxIcon.Error);
153 logger.Error(ex);
154 }
155 }
156 else
157 {
158 string gameSystemID = Preferences.GetStringProperty("currSystem");
159
160 if (gameSystemID!=null && !"".Equals(gameSystemID))
161 {
162 logger.Debug("Attempting to load current game system from properties");
163 GameSystem sys = Factory.GetGameSystem(gameSystemID);
164
165 if (sys!=null)
166 {
167 WarFoundryCore.CurrentGameSystem = sys;
168 logger.InfoFormat("Loaded game system {0} from properties", gameSystemID);
169 }
170 }
171 }
172 }
173
174 public static string DataPath
175 {
176 get { return Constants.ExecutablePath+Constants.DirectoryChar+"data"; }
177 }
178
179 public static String ArmiesPath
180 {
181 get { return Constants.UserDataPath+Constants.DirectoryChar+"armies"; }
182 }
183
184 public Preferences Preferences
185 {
186 get { return preferences; }
187 set { preferences = value; }
188 }
189
190 public CommandStack CommandStack
191 {
192 get
193 {
194 if (commandStack == null)
195 {
196 commandStack = new CommandStack();
197 }
198
199 return commandStack;
200 }
201 }
202
203 /// <summary>
204 /// Clean up any resources being used.
205 /// </summary>
206 protected override void Dispose( bool disposing )
207 {
208 if( disposing )
209 {
210 if (components != null)
211 {
212 components.Dispose();
213 }
214 }
215 base.Dispose( disposing );
216 }
217
218 #region Windows Form Designer generated code
219 /// <summary>
220 /// Required method for Designer support - do not modify
221 /// the contents of this method with the code editor.
222 /// </summary>
223 private void InitializeComponent()
224 {
225 this.components = new System.ComponentModel.Container();
226 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain));
227 this.statusBar = new IBBoard.Windows.Forms.ColorableStatusBar();
228 this.sbMainPanel = new IBBoard.Windows.Forms.ColorableStatusBarPanel();
229 this.sbErrorPanel = new IBBoard.Windows.Forms.ColorableStatusBarPanel();
230 this.sbPointsPanel = new IBBoard.Windows.Forms.ColorableStatusBarPanel();
231 this.toolBar = new System.Windows.Forms.ToolBar();
232 this.bttnNewArmy = new IBBoard.Windows.Forms.IBBToolBarButton();
233 this.bttnOpenArmy = new IBBoard.Windows.Forms.IBBToolBarButton();
234 this.bttnSaveArmy = new IBBoard.Windows.Forms.IBBToolBarButton();
235 this.bttnSep1 = new IBBoard.Windows.Forms.IBBToolBarButton();
236 this.bttnUndo = new IBBoard.Windows.Forms.IBBToolBarButton();
237 this.undoMenu = new System.Windows.Forms.ContextMenu();
238 this.bttnRedo = new IBBoard.Windows.Forms.IBBToolBarButton();
239 this.redoMenu = new System.Windows.Forms.ContextMenu();
240 this.buttonIcons = new System.Windows.Forms.ImageList(this.components);
241 this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
242 this.menuFile = new IBBoard.Windows.Forms.IBBMenuItem();
243 this.miNewArmy = new IBBoard.Windows.Forms.IBBMenuItem();
244 this.miOpenArmy = new IBBoard.Windows.Forms.IBBMenuItem();
245 this.miSaveArmy = new IBBoard.Windows.Forms.IBBMenuItem();
246 this.miSaveArmyAs = new IBBoard.Windows.Forms.IBBMenuItem();
247 this.miCloseArmy = new IBBoard.Windows.Forms.IBBMenuItem();
248 this.miSep1 = new System.Windows.Forms.MenuItem();
249 this.miChangeSystem = new IBBoard.Windows.Forms.IBBMenuItem();
250 this.miSep2 = new System.Windows.Forms.MenuItem();
251 this.miReloadFiles = new IBBoard.Windows.Forms.IBBMenuItem();
252 this.miSep3 = new System.Windows.Forms.MenuItem();
253 this.miExit = new IBBoard.Windows.Forms.IBBMenuItem();
254 this.menuEdit = new IBBoard.Windows.Forms.IBBMenuItem();
255 this.miUndo = new IBBoard.Windows.Forms.IBBMenuItem();
256 this.miRedo = new IBBoard.Windows.Forms.IBBMenuItem();
257 this.menuHelp = new IBBoard.Windows.Forms.IBBMenuItem();
258 this.miAbout = new IBBoard.Windows.Forms.IBBMenuItem();
259 this.miDebugWindow = new IBBoard.Windows.Forms.IBBMenuItem();
260 this.openArmyDialog = new System.Windows.Forms.OpenFileDialog();
261 this.saveArmyDialog = new System.Windows.Forms.SaveFileDialog();
262 this.pnlRight = new System.Windows.Forms.Panel();
263 this.statusBarTimer = new System.Windows.Forms.Timer(this.components);
264 ((System.ComponentModel.ISupportInitialize)(this.sbMainPanel)).BeginInit();
265 ((System.ComponentModel.ISupportInitialize)(this.sbErrorPanel)).BeginInit();
266 ((System.ComponentModel.ISupportInitialize)(this.sbPointsPanel)).BeginInit();
267 this.SuspendLayout();
268 //
269 // statusBar
270 //
271 this.statusBar.Location = new System.Drawing.Point(0, 548);
272 this.statusBar.Name = "statusBar";
273 this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
274 this.sbMainPanel,
275 this.sbErrorPanel,
276 this.sbPointsPanel});
277 this.statusBar.ShowPanels = true;
278 this.statusBar.Size = new System.Drawing.Size(792, 22);
279 this.statusBar.TabIndex = 1;
280 this.statusBar.PanelClick += new System.Windows.Forms.StatusBarPanelClickEventHandler(this.statusBar_PanelClick);
281 this.statusBar.DrawItem += new System.Windows.Forms.StatusBarDrawItemEventHandler(this.statusBar_DrawItem);
282 //
283 // sbMainPanel
284 //
285 this.sbMainPanel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
286 this.sbMainPanel.Color = System.Drawing.SystemColors.WindowText;
287 this.sbMainPanel.Name = "sbMainPanel";
288 this.sbMainPanel.Style = System.Windows.Forms.StatusBarPanelStyle.OwnerDraw;
289 this.sbMainPanel.Width = 475;
290 //
291 // sbErrorPanel
292 //
293 this.sbErrorPanel.Color = System.Drawing.SystemColors.WindowText;
294 this.sbErrorPanel.Name = "sbErrorPanel";
295 this.sbErrorPanel.Style = System.Windows.Forms.StatusBarPanelStyle.OwnerDraw;
296 this.sbErrorPanel.Width = 150;
297 //
298 // sbPointsPanel
299 //
300 this.sbPointsPanel.Color = System.Drawing.SystemColors.WindowText;
301 this.sbPointsPanel.Name = "sbPointsPanel";
302 this.sbPointsPanel.Style = System.Windows.Forms.StatusBarPanelStyle.OwnerDraw;
303 this.sbPointsPanel.ToolTipText = "Current Points Total";
304 this.sbPointsPanel.Width = 150;
305 //
306 // toolBar
307 //
308 this.toolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
309 this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
310 this.bttnNewArmy,
311 this.bttnOpenArmy,
312 this.bttnSaveArmy,
313 this.bttnSep1,
314 this.bttnUndo,
315 this.bttnRedo});
316 this.toolBar.ButtonSize = new System.Drawing.Size(16, 16);
317 this.toolBar.DropDownArrows = true;
318 this.toolBar.ImageList = this.buttonIcons;
319 this.toolBar.Location = new System.Drawing.Point(0, 0);
320 this.toolBar.Name = "toolBar";
321 this.toolBar.ShowToolTips = true;
322 this.toolBar.Size = new System.Drawing.Size(792, 28);
323 this.toolBar.TabIndex = 2;
324 this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
325 //
326 // bttnNewArmy
327 //
328 this.bttnNewArmy.Enabled = false;
329 this.bttnNewArmy.ImageIndex = 0;
330 this.bttnNewArmy.Name = "";
331 //
332 // bttnOpenArmy
333 //
334 this.bttnOpenArmy.ImageIndex = 2;
335 this.bttnOpenArmy.Name = "";
336 //
337 // bttnSaveArmy
338 //
339 this.bttnSaveArmy.Enabled = false;
340 this.bttnSaveArmy.ImageIndex = 1;
341 this.bttnSaveArmy.Name = "";
342 //
343 // bttnSep1
344 //
345 this.bttnSep1.Name = "";
346 this.bttnSep1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
347 //
348 // bttnUndo
349 //
350 this.bttnUndo.DropDownMenu = this.undoMenu;
351 this.bttnUndo.Enabled = false;
352 this.bttnUndo.ImageIndex = 3;
353 this.bttnUndo.Name = "";
354 this.bttnUndo.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
355 //
356 // bttnRedo
357 //
358 this.bttnRedo.DropDownMenu = this.redoMenu;
359 this.bttnRedo.Enabled = false;
360 this.bttnRedo.ImageIndex = 4;
361 this.bttnRedo.Name = "";
362 this.bttnRedo.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
363 //
364 // buttonIcons
365 //
366 this.buttonIcons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("buttonIcons.ImageStream")));
367 this.buttonIcons.TransparentColor = System.Drawing.Color.Transparent;
368 this.buttonIcons.Images.SetKeyName(0, "");
369 this.buttonIcons.Images.SetKeyName(1, "");
370 this.buttonIcons.Images.SetKeyName(2, "");
371 this.buttonIcons.Images.SetKeyName(3, "");
372 this.buttonIcons.Images.SetKeyName(4, "");
373 this.buttonIcons.Images.SetKeyName(5, "");
374 this.buttonIcons.Images.SetKeyName(6, "");
375 //
376 // mainMenu
377 //
378 this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
379 this.menuFile,
380 this.menuEdit,
381 this.menuHelp});
382 //
383 // menuFile
384 //
385 this.menuFile.Index = 0;
386 this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
387 this.miNewArmy,
388 this.miOpenArmy,
389 this.miSaveArmy,
390 this.miSaveArmyAs,
391 this.miCloseArmy,
392 this.miSep1,
393 this.miChangeSystem,
394 this.miSep2,
395 this.miReloadFiles,
396 this.miSep3,
397 this.miExit});
398 this.menuFile.Text = "&file";
399 //
400 // miNewArmy
401 //
402 this.miNewArmy.Index = 0;
403 this.miNewArmy.Text = "&new army";
404 this.miNewArmy.Click += new System.EventHandler(this.miNewArmy_Click);
405 //
406 // miOpenArmy
407 //
408 this.miOpenArmy.Index = 1;
409 this.miOpenArmy.Text = "&open army";
410 this.miOpenArmy.Click += new System.EventHandler(this.miOpenArmy_Click);
411 //
412 // miSaveArmy
413 //
414 this.miSaveArmy.Enabled = false;
415 this.miSaveArmy.Index = 2;
416 this.miSaveArmy.Text = "&save army";
417 this.miSaveArmy.Click += new System.EventHandler(this.miSaveArmy_Click);
418 //
419 // miSaveArmyAs
420 //
421 this.miSaveArmyAs.Enabled = false;
422 this.miSaveArmyAs.Index = 3;
423 this.miSaveArmyAs.Text = "save army &as...";
424 this.miSaveArmyAs.Click += new System.EventHandler(this.miSaveArmyAs_Click);
425 //
426 // miCloseArmy
427 //
428 this.miCloseArmy.Enabled = false;
429 this.miCloseArmy.Index = 4;
430 this.miCloseArmy.Text = "&close army";
431 this.miCloseArmy.Click += new System.EventHandler(this.miCloseArmy_Click);
432 //
433 // miSep1
434 //
435 this.miSep1.Index = 5;
436 this.miSep1.Text = "-";
437 //
438 // miChangeSystem
439 //
440 this.miChangeSystem.Index = 6;
441 this.miChangeSystem.Text = "change &game system";
442 this.miChangeSystem.Click += new System.EventHandler(this.miChangeSystem_Click);
443 //
444 // miSep2
445 //
446 this.miSep2.Index = 7;
447 this.miSep2.Text = "-";
448 //
449 // miReloadFiles
450 //
451 this.miReloadFiles.Index = 8;
452 this.miReloadFiles.Text = "&reload files";
453 this.miReloadFiles.Click += new System.EventHandler(this.miReloadFiles_Click);
454 //
455 // miSep3
456 //
457 this.miSep3.Index = 9;
458 this.miSep3.Text = "-";
459 //
460 // miExit
461 //
462 this.miExit.Index = 10;
463 this.miExit.Text = "e&xit";
464 this.miExit.Click += new System.EventHandler(this.miExit_Click);
465 //
466 // menuEdit
467 //
468 this.menuEdit.Index = 1;
469 this.menuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
470 this.miUndo,
471 this.miRedo});
472 this.menuEdit.Text = "&edit";
473 //
474 // miUndo
475 //
476 this.miUndo.Enabled = false;
477 this.miUndo.Index = 0;
478 this.miUndo.Text = "&undo";
479 this.miUndo.Click += new System.EventHandler(this.miUndo_Click);
480 //
481 // miRedo
482 //
483 this.miRedo.Enabled = false;
484 this.miRedo.Index = 1;
485 this.miRedo.Text = "&redo";
486 this.miRedo.Click += new System.EventHandler(this.miRedo_Click);
487 //
488 // menuHelp
489 //
490 this.menuHelp.Index = 2;
491 this.menuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
492 this.miAbout,
493 this.miDebugWindow});
494 this.menuHelp.Text = "&help";
495 //
496 // miAbout
497 //
498 this.miAbout.Enabled = false;
499 this.miAbout.Index = 0;
500 this.miAbout.Text = "&about";
501 //
502 // miDebugWindow
503 //
504 this.miDebugWindow.Index = 1;
505 this.miDebugWindow.Text = "&debug";
506 this.miDebugWindow.Click += new System.EventHandler(this.miDebugWindow_Click);
507 //
508 // saveArmyDialog
509 //
510 this.saveArmyDialog.Title = "Translatable:saveArmyDialog";
511 //
512 // pnlRight
513 //
514 this.pnlRight.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
515 | System.Windows.Forms.AnchorStyles.Right)));
516 this.pnlRight.BackColor = System.Drawing.SystemColors.AppWorkspace;
517 this.pnlRight.Location = new System.Drawing.Point(726, 30);
518 this.pnlRight.Name = "pnlRight";
519 this.pnlRight.Size = new System.Drawing.Size(64, 516);
520 this.pnlRight.TabIndex = 4;
521 this.pnlRight.Visible = false;
522 this.pnlRight.Paint += new System.Windows.Forms.PaintEventHandler(this.pnlRight_Paint);
523 //
524 // statusBarTimer
525 //
526 this.statusBarTimer.Interval = 5000;
527 this.statusBarTimer.Tick += new System.EventHandler(this.statusBarTimer_Tick);
528 //
529 // FrmMain
530 //
531 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
532 this.ClientSize = new System.Drawing.Size(792, 570);
533 this.Controls.Add(this.pnlRight);
534 this.Controls.Add(this.toolBar);
535 this.Controls.Add(this.statusBar);
536 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
537 this.IsMdiContainer = true;
538 this.Menu = this.mainMenu;
539 this.Name = "FrmMain";
540 this.Text = "WarFoundry";
541 ((System.ComponentModel.ISupportInitialize)(this.sbMainPanel)).EndInit();
542 ((System.ComponentModel.ISupportInitialize)(this.sbErrorPanel)).EndInit();
543 ((System.ComponentModel.ISupportInitialize)(this.sbPointsPanel)).EndInit();
544 this.ResumeLayout(false);
545 this.PerformLayout();
546
547 }
548 #endregion
549
550 /// <summary>
551 /// The main entry point for the application.
552 /// </summary>
553 [STAThread]
554 static void Main(string[] args)
555 {
556 try
557 {
558 ///TODO: Work out how to auto-embed the Manifest file so that we can have visual styles without having a .manifest file floating around
559 //and without using the line below, which causes the toolbar buttons to lose their icons
560 //Application.EnableVisualStyles();
561 Application.Run(new FrmMain(args));
562 }
563 catch(Exception ex)
564 {
565 LogManager.GetLogger(typeof(FrmMain)).Fatal(ex);
566 MessageBox.Show(null, "A major, unexpected and fatal error ocurred while starting the application: \r\n\r\n"+ex.Message+"\r\n\r\n"+ex.StackTrace, "Fatal error", MessageBoxButtons.OK, MessageBoxIcon.Error);
567 }
568 }
569
570 public WarFoundryFactory Factory
571 {
572 get { return WarFoundryFactoryFactory.GetFactoryFactory().GetFactory(Constants.ExecutablePath, factoryType); }
573 }
574
575 private void miExit_Click(object sender, System.EventArgs e)
576 {
577 Application.Exit();
578 }
579
580 private void miNewArmy_Click(object sender, System.EventArgs e)
581 {
582 createNewArmy();
583 }
584
585 private void createNewArmy()
586 {
587 if (closeCurrentArmy())
588 {
589 FrmNewArmy newArmy = new FrmNewArmy(CurrentGameSystem, Factory);
590 DialogResult dr = newArmy.ShowDialog();
591
592 if (dr == DialogResult.OK)
593 {
594 CurrentArmy = new Army(newArmy.SelectedRace, newArmy.ArmyName, newArmy.ArmySize, Factory);
595 }
596 }
597 }
598
599 private bool openArmy()
600 {
601 if (closeCurrentArmy())
602 {
603 if (openArmyDialog.Filter=="")
604 {
605 string savePath = ArmiesPath;
606
607 if (!Directory.Exists(savePath))
608 {
609 Directory.CreateDirectory(savePath);
610 }
611
612 openArmyDialog.InitialDirectory = savePath;
613 openArmyDialog.Filter = Translation.GetTranslation("armyFileFilter")+"|*.army";
614 openArmyDialog.Title = Translation.GetTranslation("openArmyDialog");
615
616 }
617
618 DialogResult dr = openArmyDialog.ShowDialog(this);
619
620 if (dr == DialogResult.OK)
621 {
622 try
623 {
624 CurrentArmy = Factory.LoadArmy(openArmyDialog.FileName);
625 return true;
626 }
627 catch (InvalidFileException ex)
628 {
629 logger.Error(ex);
630 MessageBox.Show(this, ex.Message, Translation.GetTranslation("InvalidFileBoxTitle", "Invalid data file"), MessageBoxButtons.OK, MessageBoxIcon.Error);
631 return false;
632 }
633 }
634 else
635 {
636 return false;
637 }
638 }
639 else
640 {
641 return false;
642 }
643 }
644
645 private bool closeCurrentArmy()
646 {
647 if (CurrentArmy!=null)
648 {
649 bool canClose = false;
650
651 if (CommandStack.IsDirty())
652 {
653 DialogResult dr = MessageBox.Show(this, "The army \""+CurrentArmy.Name+"\" has been modified.\r\nSave changes before closing army?", "Unsaved changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3);
654
655 if (dr == DialogResult.Yes)
656 {
657 canClose = saveCurrentArmyOrSaveAs();
658 }
659 else if (dr == DialogResult.No)
660 {
661 canClose = true;
662 }
663 //else they said cancel and we default to "canClose = false" so do nothing
664 }
665 else
666 {
667 canClose = true;
668 }
669
670 if (canClose)
671 {
672 //do close
673 disableCategoryButtons();
674 CurrentArmy = null;
675 return true;
676 }
677 else
678 {
679 return false;
680 }
681 }
682 else
683 {
684 disableCategoryButtons();
685 //pretend we succeeded
686 return true;
687 }
688 }
689
690 private void undoLastAction()
691 {
692 if (commandStack.CanUndo())
693 {
694 commandStack.Undo();
695 }
696 }
697
698 private void redoAction()
699 {
700 if (commandStack.CanRedo())
701 {
702 commandStack.Redo();
703 }
704 }
705
706 private bool saveCurrentArmyOrSaveAs()
707 {
708 if (WarFoundrySaver.GetSaver().CanSave(CurrentArmy))
709 {
710 return saveCurrentArmy();
711 }
712 else
713 {
714 return saveCurrentArmyAs();
715 }
716 }
717
718 private bool saveCurrentArmy()
719 {
720 if (WarFoundrySaver.GetSaver().CanSave(CurrentArmy))
721 {
722 if (WarFoundrySaver.GetSaver().Save(CurrentArmy))
723 {
724 miSaveArmy.Enabled = false;
725 bttnSaveArmy.Enabled = false;
726 CommandStack.setCleanMark();
727 return true;
728 }
729 else
730 {
731 MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
732 return false;
733 }
734 }
735 else
736 {
737 return false;
738 }
739 }
740
741 private bool saveCurrentArmyAs()
742 {
743 if (saveArmyDialog.Filter == "")
744 {
745 string savePath = ArmiesPath;
746
747 if (!Directory.Exists(savePath))
748 {
749 Directory.CreateDirectory(savePath);
750 }
751
752 saveArmyDialog.InitialDirectory = savePath;
753 saveArmyDialog.Filter = Translation.GetTranslation("armyFileFilter")+"|*.army";
754 saveArmyDialog.Title = Translation.GetTranslation("saveArmyDialog");
755 }
756
757 DialogResult dr = saveArmyDialog.ShowDialog(this);
758
759 if (dr == DialogResult.OK)
760 {
761 if (WarFoundrySaver.GetSaver().SaveAs(CurrentArmy, saveArmyDialog.FileName))
762 {
763 miSaveArmy.Enabled = false;
764 bttnSaveArmy.Enabled = false;
765 CommandStack.setCleanMark();
766 return true;
767 }
768 else
769 {
770 MessageBox.Show(this, Translation.GetTranslation("SaveFailed"), Translation.GetTranslation("SaveFailedTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
771 return false;
772 }
773 }
774 else
775 {
776 return false;
777 }
778 }
779
780 private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
781 {
782 if (e.Button == bttnUndo)
783 {
784 undoLastAction();
785 }
786 else if (e.Button == bttnRedo)
787 {
788 redoAction();
789 }
790 else if (e.Button == bttnNewArmy)
791 {
792 createNewArmy();
793 }
794 else if (e.Button == bttnOpenArmy)
795 {
796 openArmy();
797 }
798 else if (e.Button == bttnSaveArmy)
799 {
800 saveCurrentArmyOrSaveAs();
801 }
802 else
803 {
804 //it must be one of our extra buttons for the categories
805 addUnitFromCategory((Category)e.Button.Tag);
806 }
807 }
808
809 private GameSystem CurrentGameSystem
810 {
811 get { return WarFoundryCore.CurrentGameSystem; }
812 set { WarFoundryCore.CurrentGameSystem = value; }
813 }
814
815 private static Army CurrentArmy
816 {
817 get { return WarFoundryCore.CurrentArmy; }
818 set { WarFoundryCore.CurrentArmy = value; }
819 }
820
821 private void SelectGameSystem()
822 {
823 FrmSelectSystem selectSystem = new FrmSelectSystem(Factory);
824 DialogResult dr = selectSystem.ShowDialog(this);
825
826 if (dr==DialogResult.OK)
827 {
828 closeCurrentArmy();
829 CurrentGameSystem = selectSystem.GameSystem;
830 }
831 }
832
833 private void miChangeSystem_Click(object sender, System.EventArgs e)
834 {
835 SelectGameSystem();
836 }
837
838 private void FrmMain_GameSystemChanged(GameSystem oldSystem, GameSystem newSystem)
839 {
840 miNewArmy.Enabled = newSystem != null;
841 bttnNewArmy.Enabled = newSystem != null;
842 setAppTitle();
843 removeCategoryButtons();
844 addCategoryButtons();
845 }
846
847 private void FrmMain_ArmyChanged(Army oldArmy, Army newArmy)
848 {
849 setAppTitle();
850
851 if (oldArmy != null)
852 {
853 oldArmy.UnitAdded += UnitAddedMethod;
854 oldArmy.UnitRemoved += UnitRemovedMethod;
855 oldArmy.PointsValueChanged += PointsValueChangedMethod;
856 }
857
858 if (CurrentArmy==null)
859 {
860 miSaveArmyAs.Enabled = false;
861 miCloseArmy.Enabled = false;
862 disableCategoryButtons();
863 }
864 else
865 {
866 newArmy.UnitAdded += UnitAddedMethod;
867 newArmy.UnitRemoved += UnitRemovedMethod;
868 newArmy.PointsValueChanged += PointsValueChangedMethod;
869 //TODO: Clear all buttons
870 miSaveArmyAs.Enabled = true;
871 miCloseArmy.Enabled = true;
872 enableCategoryButtons();
873
874 if (newArmy.Race.HasCategoryOverrides())
875 {
876 removeCategoryButtons();
877 addCategoryButtons(newArmy.Race.Categories);
878 }
879 }
880
881 CommandStack.Reset();
882
883 miSaveArmy.Enabled = false;
884 bttnSaveArmy.Enabled = false;
885
886 setPointsPanelText();
887 }
888
889 private void addCategoryButtons()
890 {
891 if (CurrentGameSystem!=null)
892 {
893 addCategoryButtons(CurrentGameSystem.Categories);
894 }
895 }
896
897 private void addCategoryButtons(Category[] cats)
898 {
899 int catCount = cats.Length;
900 Category cat;
901 categoryButtons = new ToolBarButton[catCount+1];
902
903 ToolBarButton sep = new ToolBarButton();
904 sep.Style = ToolBarButtonStyle.Separator;
905 categoryButtons[0] = sep;
906
907 IBBToolBarButton button;
908
909 for (int i = 0; i<catCount; i++)
910 {
911 cat = cats[i];
912 button = new IBBToolBarButton();
913 ///button.Name = "bttnAddCategory" + cat.Name[0].ToString();
914 button.Text = cat.Name.ToString();//String.Format(Translation.GetTranslation("bttnAddCategory"), cat.Name);
915 button.Tag = cat;
916 button.ImageIndex = 6;
917 button.Enabled = false;
918 categoryButtons[i+1] = button;
919 }
920
921 this.Invoke(new ToolBarButtonRangeDelegate(toolBar.Buttons.AddRange), new object[]{categoryButtons});
922 }
923
924 private void removeCategoryButtons()
925 {
926 if (categoryButtons!=null)
927 {
928 for (int i = 0; i<categoryButtons.Length; i++)
929 {
930 this.Invoke(new ToolBarButtonDelegate(toolBar.Buttons.Remove), new object[]{categoryButtons[i]});
931 }
932 }
933 }
934
935 private void enableCategoryButtons()
936 {
937 setCategoryButtonState(true);
938 }
939
940 private void disableCategoryButtons()
941 {
942 setCategoryButtonState(false);
943 }
944
945 private void setCategoryButtonState(bool state)
946 {
947 if (categoryButtons!=null)
948 {
949 for (int i = 0; i<categoryButtons.Length; i++)
950 {
951 categoryButtons[i].Enabled = state;
952 }
953 }
954 }
955
956 private void miSaveArmyAs_Click(object sender, System.EventArgs e)
957 {
958 saveCurrentArmyAs();
959 }
960
961 private void commandStack_CommandStackUpdated()
962 {
963 bttnUndo.Enabled = commandStack.CanUndo();
964 miUndo.Enabled = bttnUndo.Enabled;
965 bttnRedo.Enabled = commandStack.CanRedo();
966 miRedo.Enabled = bttnRedo.Enabled;
967 MenuItem[] menuItems = null;
968 int redoLength = commandStack.RedoLength;
969 int maxRedo = Math.Min(10, redoLength);
970
971 if (redoLength > 0)
972 {
973 menuItems = new MenuItem[maxRedo];
974 Command com;
975 MenuItem mi;
976
977 for (int i = 0; i < maxRedo; i++)
978 {
979 com = commandStack.PeekRedoCommand(i+1);
980
981 if (com == null)
982 {
983 break;
984 }
985
986 mi = new MenuItem(com.Description);
987 mi.Click+=new EventHandler(redoMenu_Click);
988 menuItems[i] = mi;
989 }
990 }
991
992 redoMenu.MenuItems.Clear();
993
994 if (menuItems!=null && menuItems[0]!=null)
995 {
996 bttnRedo.ToolTipText = menuItems[0].Text;
997 redoMenu.MenuItems.AddRange(menuItems);
998 }
999
1000 int undoLength = commandStack.UndoLength;
1001 int maxUndo = Math.Min(10, undoLength);
1002 MenuItem[] menuItemsUndo = null;
1003
1004 if (undoLength > 0)
1005 {
1006 menuItemsUndo = new MenuItem[maxUndo];
1007 Command com;
1008 MenuItem mi;
1009
1010 for (int i = 0; i < maxUndo; i++)
1011 {
1012 com = commandStack.PeekUndoCommand(i+1);
1013
1014 if (com == null)
1015 {
1016 break;
1017 }
1018
1019 mi = new MenuItem(com.UndoDescription);
1020 mi.Click+=new EventHandler(undoMenu_Click);
1021 menuItemsUndo[i] = mi;
1022 }
1023 }
1024
1025 undoMenu.MenuItems.Clear();
1026
1027 if (menuItemsUndo!=null && menuItemsUndo[0]!=null)
1028 {
1029 bttnUndo.ToolTipText = menuItemsUndo[0].Text;
1030 undoMenu.MenuItems.AddRange(menuItemsUndo);
1031 }
1032
1033 bool canSave = WarFoundrySaver.GetSaver().CanSave(CurrentArmy);
1034 bttnSaveArmy.Enabled = commandStack.IsDirty() && CurrentArmy!=null && canSave;
1035 miSaveArmy.Enabled = commandStack.IsDirty() && CurrentArmy!=null && canSave;
1036 }
1037
1038 private void miSaveArmy_Click(object sender, System.EventArgs e)
1039 {
1040 //we should only be saving when the army can be saved (i.e. it has been opened from a file or Save As-ed)
1041 //so use the "just save" version
1042 saveCurrentArmy();
1043 }
1044
1045 private void setAppTitle()
1046 {
1047 string str = AppTitle;
1048
1049 if (CurrentGameSystem!=null)
1050 {
1051 str+= " - "+CurrentGameSystem.Name;
1052 }
1053
1054 if (CurrentArmy!=null)
1055 {
1056 str+= " - "+CurrentArmy.Name;
1057 }
1058
1059 this.Text = str;
1060 }
1061
1062 private void addUnitFromCategory(Category cat)
1063 {
1064 FrmNewUnit newUnit = new FrmNewUnit(CurrentArmy.Race, cat, CurrentArmy);
1065 DialogResult dr = newUnit.ShowDialog(this);
1066
1067 if (dr == DialogResult.OK)
1068 {
1069 CreateAndAddUnitCommand cmd = new CreateAndAddUnitCommand(newUnit.SelectedUnit, cat, CurrentArmy, Factory);
1070 commandStack.Execute(cmd);
1071 }
1072 }
1073
1074 private void FrmMain_UnitAddedMethod(object unitObj)
1075 {
1076 if (unitObj is Unit)
1077 {
1078 Unit unit = (Unit)unitObj;
1079 sbErrorPanel.Text = "";
1080 }
1081 }
1082
1083 private void FrmMain_UnitRemovedMethod(object unitObj)
1084 {
1085 if (unitObj is Unit)
1086 {
1087 Unit unit = (Unit)unitObj;
1088 sbErrorPanel.Text = "";
1089
1090 //check if window is open, and close it if it is
1091 foreach (Form frm in this.MdiChildren)
1092 {
1093 if (frm is FrmUnit)
1094 {
1095 if (((FrmUnit)frm).Unit == unit)
1096 {
1097 frm.Close();
1098 break;
1099 }
1100 }
1101 }
1102 }
1103 }
1104
1105 /*private void FrmMain_FailedUnitRequirement(FailedUnitRequirement failedRequirement)
1106 {
1107 sbErrorPanel.Text = Translation.GetTranslation("UnitRequirementFailed", "Unit Requirement Failed");
1108 sbErrorPanel.Tag = failedRequirement.Description;
1109 }*/
1110
1111 /*public void MdiChildMoved()
1112 {
1113 Point mouseAt = PointToClient(ActiveMdiChild.Location);
1114
1115 if (Comparisons.ValueWithinAmount(pnlRight.Right, ActiveMdiChild.Right, 10))
1116 {
1117 pnlRight.Visible = true;
1118 //pnlRight.Container.Add(ActiveMdiChild);
1119 }
1120 else
1121 {
1122 pnlRight.Visible = false;
1123 }
1124 }*/
1125
1126 public void pnlRight_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
1127 {
1128 HatchBrush dockCueBrush = new HatchBrush(HatchStyle.LightDownwardDiagonal, Color.White, Color.Gray);
1129 Pen dockCuePen = new Pen(dockCueBrush, 10);
1130 e.Graphics.DrawRectangle(dockCuePen, new Rectangle(pnlRight.Left, pnlRight.Top, pnlRight.Width, pnlRight.Height));
1131 }
1132
1133 private void miUndo_Click(object sender, System.EventArgs e)
1134 {
1135 undoLastAction();
1136 }
1137
1138 private void miRedo_Click(object sender, System.EventArgs e)
1139 {
1140 redoAction();
1141 }
1142
1143 private void miCloseArmy_Click(object sender, EventArgs e)
1144 {
1145 closeCurrentArmy();
1146 }
1147
1148 private void miOpenArmy_Click(object sender, EventArgs e)
1149 {
1150 openArmy();
1151 }
1152
1153 private void FrmMain_PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal)
1154 {
1155 if (obj is Army)
1156 {
1157 setPointsPanelText();
1158 }
1159 }
1160
1161 private void setPointsPanelText()
1162 {
1163 if (CurrentArmy==null)
1164 {
1165 sbPointsPanel.Text = "";
1166 sbPointsPanel.ResetColor();
1167 }
1168 else
1169 {
1170 sbPointsPanel.Text = String.Format(Translation.GetTranslation("statusPanelPoints"), CurrentArmy.PointsTotal, CurrentArmy.MaxPoints);
1171
1172 if (CurrentArmy.PointsTotal>CurrentArmy.MaxPoints)
1173 {
1174 sbPointsPanel.Color = Color.Red;
1175 }
1176 else
1177 {
1178 sbPointsPanel.ResetColor();
1179 }
1180 }
1181 }
1182
1183 private void redoMenu_Click(object sender, EventArgs e)
1184 {
1185 if (sender is MenuItem)
1186 {
1187 MenuItem mi = (MenuItem)sender;
1188
1189 if (mi.Parent == redoMenu)
1190 {
1191 //we know it's an redo menu item so find it's index and redo everything
1192 int max = mi.Index;
1193
1194 for (int i = 0; i <= max; i++)
1195 {
1196 commandStack.Redo();
1197 }
1198 }
1199 }
1200 }
1201
1202 private void undoMenu_Click(object sender, EventArgs e)
1203 {
1204 if (sender is MenuItem)
1205 {
1206 MenuItem mi = (MenuItem)sender;
1207
1208 if (mi.Parent == undoMenu)
1209 {
1210 //we know it's an undo menu item so find it's index and undo everything
1211 int max = mi.Index;
1212 for (int i = 0; i <= max; i++)
1213 {
1214 commandStack.Undo();
1215 }
1216 }
1217 }
1218 }
1219
1220 private void statusBar_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
1221 {
1222 statusBar.ColorableStatusBarDrawItem(sender, sbdevent);
1223 }
1224
1225 private void miDebugWindow_Click(object sender, EventArgs e)
1226 {
1227 if (debugWindow == null || debugWindow.IsDisposed)
1228 {
1229 debugWindow = new FrmDebugOutput();
1230 }
1231
1232 debugWindow.Show();
1233 debugWindow.Focus();
1234 }
1235
1236 private void FrmMain_Closing(object sender, CancelEventArgs e)
1237 {
1238 if (!closeCurrentArmy())
1239 {
1240 e.Cancel = true;
1241 }
1242 }
1243
1244 private void miReloadFiles_Click(object sender, System.EventArgs e)
1245 {
1246 Factory.LoadAllGameSystems();
1247 sbMainPanel.Text = Translation.GetTranslation("GameSystemFilesReloaded", "Game system and race files reloaded");
1248 statusBarTimer.Enabled = true;
1249 }
1250
1251 private void statusBarTimer_Tick(object sender, System.EventArgs e)
1252 {
1253 sbMainPanel.Text = "";
1254 statusBarTimer.Enabled = false;
1255 }
1256
1257 private void statusBar_PanelClick(object sender, StatusBarPanelClickEventArgs e)
1258 {
1259 if (e.StatusBarPanel == sbErrorPanel && sbErrorPanel.Text!="")
1260 {
1261 MessageBox.Show(this, sbErrorPanel.TagString, Translation.GetTranslation("FailedRequirementMessage"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
1262 }
1263 }
1264 }
1265 }