Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
OptionsViewModel.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Input;
6 using System.IO;
7 using System.Windows;
9 using SiliconStudio.Presentation.ViewModel;
10 
11 namespace SiliconStudio.Paradox.ConfigEditor.ViewModels
12 {
14  {
15  public Options Options { get; private set; }
16 
18  {
19  Options = Options.Load() ?? new Options();
20 
21  ParadoxPath = Options.ParadoxPath;
22  ParadoxConfigFilename = Options.ParadoxConfigFilename;
23 
24  CheckParadoxPath();
25  CheckParadoxConfigFilename();
26 
27  BrowsePathCommand = new AnonymousCommand(BrowsePath);
28  BrowseConfigFileCommand = new AnonymousCommand(BrowseConfigFile);
29  }
30 
31  public void SetOptionsWindow(Window window)
32  {
33  CloseCommand = new AnonymousCommand(window.Close);
34  }
35 
36  public ICommand CloseCommand { get; private set; }
37  public ICommand BrowsePathCommand { get; private set; }
38  public ICommand BrowseConfigFileCommand { get; private set; }
39 
40  private void BrowsePath()
41  {
42  var dialog = new System.Windows.Forms.FolderBrowserDialog
43  {
44  Description = "Select Paradox base directory",
45  ShowNewFolderButton = true,
46  };
47 
48  if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
49  ParadoxPath = dialog.SelectedPath;
50  }
51 
52  private void BrowseConfigFile()
53  {
54  var dialog = new Microsoft.Win32.OpenFileDialog
55  {
56  Title = "Select the Paradox configuration file",
57  Filter = "Xml Files (*.xml)|*.xml|All Files (*.*)|*.*",
58  Multiselect = false,
59  CheckFileExists = true,
60  };
61 
62  if (dialog.ShowDialog() == true)
63  ParadoxConfigFilename = dialog.FileName;
64  }
65 
66  private string paradoxPath;
67  public string ParadoxPath
68  {
69  get { return paradoxPath; }
70  set
71  {
72  if (SetValue(ref paradoxPath, value, "ParadoxPath"))
73  CheckParadoxPath();
74  }
75  }
76 
77  private bool isParadoxPathValid;
78  public bool IsParadoxPathValid
79  {
80  get { return isParadoxPathValid; }
81  set { SetValue(ref isParadoxPathValid, value, "IsParadoxPathValid"); }
82  }
83 
84  private void CheckParadoxPath()
85  {
86  IsParadoxPathValid = Directory.Exists(ParadoxPath);
87  }
88 
89  private string paradoxConfigFilename;
90  public string ParadoxConfigFilename
91  {
92  get { return paradoxConfigFilename; }
93  set
94  {
95  if (SetValue(ref paradoxConfigFilename, value, "ParadoxConfigFilename"))
96  CheckParadoxConfigFilename();
97  }
98  }
99 
100  private bool isParadoxConfigFilenameValid;
101  public bool IsParadoxConfigFilenameValid
102  {
103  get { return isParadoxConfigFilenameValid; }
104  set { SetValue(ref isParadoxConfigFilenameValid, value, "IsParadoxConfigFilenameValid"); }
105  }
106 
107  private void CheckParadoxConfigFilename()
108  {
109  if (string.IsNullOrWhiteSpace(ParadoxConfigFilename))
110  {
111  IsParadoxConfigFilenameValid = true;
112  return;
113  }
114 
115  var tempFilename = ParadoxConfigFilename;
116 
117  if (Path.IsPathRooted(tempFilename) == false)
118  tempFilename = Path.Combine(ParadoxPath, ParadoxConfigFilename);
119 
120  IsParadoxConfigFilenameValid = File.Exists(tempFilename);
121  }
122 
123  private ICommand acceptCommand;
124  public ICommand AcceptCommand
125  {
126  get
127  {
128  if (acceptCommand == null)
129  acceptCommand = new AnonymousCommand(Accept);
130  return acceptCommand;
131  }
132  }
133 
134  private void Accept()
135  {
136  if (string.IsNullOrWhiteSpace(ParadoxPath))
137  {
138  MessageBox.Show("Invalid Paradox Path, this field must not be empty.", "Paradox Path Error", MessageBoxButton.OK, MessageBoxImage.Error);
139  return;
140  }
141 
142  if (Directory.Exists(ParadoxPath) == false)
143  {
144  string message = string.Format("Invalid Paradox Path, the directory '{0}' does not exit.", ParadoxPath);
145  MessageBox.Show(message, "Paradox Path Error", MessageBoxButton.OK, MessageBoxImage.Error);
146  return;
147  }
148 
149  Options.ParadoxPath = ParadoxPath;
150  Options.ParadoxConfigFilename = ParadoxConfigFilename;
151 
152  Options.Save();
153 
154  var handler = OptionsChanged;
155  if (handler != null)
156  handler();
157 
158  CloseCommand.Execute(null); // this just closes the Options window
159  }
160 
161  public event Action OptionsChanged;
162  }
163 }
An implementation of CommandBase that route Execute calls to a given anonymous method.
This abstract class represents a basic view model, implementing INotifyPropertyChanging and INotifyPr...