/Hetymine.Executioner/Controls/CommandsConfigurator.xaml.cs

http://executioner.googlecode.com/ · C# · 321 lines · 253 code · 51 blank · 17 comment · 54 complexity · 65c300128594f543a2eca2f7a2eaaa58 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Media;
  7. using Perseus;
  8. using Perseus.Data;
  9. using Perseus.Plugins;
  10. using Hetymine.Executioner.Commands;
  11. using Hetymine.Executioner.Data;
  12. using Hetymine.Executioner.Plugins;
  13. namespace Hetymine.Executioner.Controls {
  14. /// <summary>
  15. /// Interaction logic for CommandsConfigurator.xaml
  16. /// </summary>
  17. public partial class CommandsConfigurator : UserControl, IConfigurator<Dictionary<string, List<ICommand>>> {
  18. private bool _IsApply = true;
  19. private Dictionary<string, List<ICommand>> _Commands;
  20. private Dictionary<string, int> _SelectedCommandIndexes;
  21. private string _CommandType = string.Empty;
  22. private ICommand _SelectedCommand;
  23. private CommandNameEventHandler _CommandNameChanged;
  24. private ScrollBarVisibilityEventHandler _ScrollBarVisibilityChanged;
  25. public CommandsConfigurator() {
  26. InitializeComponent();
  27. this.cCommandList.SelectionChanged += new SelectionChangedEventHandler(CommandList_SelectionChanged);
  28. this.cAddCommand.Click += new RoutedEventHandler(AddCommand_Click);
  29. this.cRemoveCommand.Click += new RoutedEventHandler(RemoveCommand_Click);
  30. this.cConfigurator.ScrollChanged += new ScrollChangedEventHandler(Configurator_ScrollChanged);
  31. this._CommandNameChanged = new CommandNameEventHandler(CommandNameChanged);
  32. this._ScrollBarVisibilityChanged = new ScrollBarVisibilityEventHandler(ScrollBarVisibilityChanged);
  33. }
  34. private void Configurator_ScrollChanged(object sender, ScrollChangedEventArgs e) {
  35. if (this.cConfigurator.ComputedVerticalScrollBarVisibility == Visibility.Visible) {
  36. this.cConfigurator.Padding = new Thickness(0, 0, 5, 0);
  37. }
  38. else {
  39. this.cConfigurator.Padding = new Thickness(0);
  40. }
  41. }
  42. #region IConfigurator<Dictionary<string, List<ICommand>>> Members
  43. public void InitializeConfig(Dictionary<string, List<ICommand>> config) {
  44. this._Commands = config;
  45. this._SelectedCommandIndexes = new Dictionary<string, int>();
  46. }
  47. public UserControl ConfigPanel { get { return this; } }
  48. #endregion
  49. private void CommandList_SelectionChanged(object sender, SelectionChangedEventArgs e) {
  50. if (!this._IsApply) { return; }
  51. int index = this.cCommandList.SelectedIndex;
  52. this._SelectedCommandIndexes[this._CommandType] = index;
  53. if (this.cConfigurator.Content != null && this.cConfigurator.Content is ILoader) {
  54. ((ILoader)this.cConfigurator.Content).Unload();
  55. }
  56. if (index < 0) {
  57. this.cEmptyCommand.Visibility = Visibility.Visible;
  58. this.cConfigurator.Content = null;
  59. // Reset scrollbar visibility to default
  60. this.cConfigurator.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  61. this.cConfigurator.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  62. }
  63. else {
  64. this.cEmptyCommand.Visibility = Visibility.Collapsed;
  65. // Remove previous event handlers
  66. if (this._SelectedCommand != null) {
  67. this._SelectedCommand.Configurator.CommandNameChanged -= this._CommandNameChanged;
  68. if (this._SelectedCommand.Configurator is IScrollable) {
  69. ((IScrollable)this._SelectedCommand.Configurator).ScrollBarVisibilityChanged -= this._ScrollBarVisibilityChanged;
  70. }
  71. }
  72. this._SelectedCommand = this.SelectedCommand();
  73. this._SelectedCommand.Configurator.CommandNameChanged += this._CommandNameChanged;
  74. if (this._SelectedCommand.Configurator is IScrollable) {
  75. var scrollable = this._SelectedCommand.Configurator as IScrollable;
  76. scrollable.ScrollBarVisibilityChanged += this._ScrollBarVisibilityChanged;
  77. this.cConfigurator.HorizontalScrollBarVisibility = scrollable.HorizontalScrollBarVisibility;
  78. this.cConfigurator.VerticalScrollBarVisibility = scrollable.VerticalScrollBarVisibility;
  79. }
  80. else {
  81. // Reset scrollbar visibility to default
  82. this.cConfigurator.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
  83. this.cConfigurator.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  84. }
  85. this.cConfigurator.Content = this._SelectedCommand.Configurator.ConfigPanel;
  86. if (this.cConfigurator.Content is ILoader) {
  87. ((ILoader)this.cConfigurator.Content).Load();
  88. }
  89. }
  90. }
  91. private void CommandNameChanged(object sender, EventArgs e) {
  92. // ListBox item's tag is set to the commands type
  93. ListBoxItem lbi = this.cCommandList.SelectedItem as ListBoxItem;
  94. string type = lbi.Tag.ToString();
  95. ICommand command = this.SelectedCommand();
  96. bool prefix = (this._CommandType.IsEmpty());
  97. TextBlock name = this.CommandListItemName(type, command, prefix);
  98. ((ListBoxItem)this.cCommandList.SelectedItem).Content = name;
  99. }
  100. private ICommand SelectedCommand() {
  101. int index = this.cCommandList.SelectedIndex;
  102. if (!this._CommandType.IsEmpty()) {
  103. return this._Commands[this._CommandType][index];
  104. }
  105. else {
  106. int count = 0;
  107. foreach (List<ICommand> cs in this._Commands.Values) {
  108. foreach (ICommand c in cs) {
  109. if (count == index) {
  110. return c;
  111. }
  112. ++count;
  113. }
  114. }
  115. }
  116. return null;
  117. }
  118. private void AddCommand_Click(object sender, RoutedEventArgs e) {
  119. this.cRemoveCommand.IsEnabled = true;
  120. this._IsApply = false;
  121. // Remove all empty HotStrings
  122. for (int i = this._Commands[this._CommandType].Count - 1; i >= 0; i--) {
  123. if (this._Commands[this._CommandType][i].IsEmpty) {
  124. this._Commands[this._CommandType].RemoveAt(i);
  125. }
  126. }
  127. this._IsApply = true;
  128. ICommand command = null;
  129. if (this._CommandType == "Trigger") {
  130. command = new TriggerCommand();
  131. }
  132. else {
  133. PluginInstance<Plugin> plugin = Executioner.PluginService[typeof(CommandPlugin), this._CommandType];
  134. if (plugin == null) { return; }
  135. CommandPlugin cp = plugin.Instance as CommandPlugin;
  136. if (cp.CanInitialize) {
  137. command = cp.InitializeCommand();
  138. }
  139. }
  140. if (command != null) {
  141. this._Commands[this._CommandType].Add(command);
  142. this.cCommandList.Items.Add(this.CommandListItem(this._CommandType, command, false));
  143. this.cCommandList.SelectedIndex = this.cCommandList.Items.Count - 1;
  144. this.cCommandList.ScrollIntoView(this.cCommandList.SelectedItem);
  145. }
  146. else {
  147. Sounds.Error();
  148. }
  149. }
  150. private void RemoveCommand_Click(object sender, RoutedEventArgs e) {
  151. int index = this.cCommandList.SelectedIndex;
  152. if (index < 0) { return; }
  153. this._Commands[this._CommandType].RemoveAt(index);
  154. this.cCommandList.Items.RemoveAt(index);
  155. if (this.cCommandList.Items.Count == 0) {
  156. this.cRemoveCommand.IsEnabled = false;
  157. }
  158. else if (this.cCommandList.Items.Count <= index) {
  159. this.cCommandList.SelectedIndex = index - 1;
  160. }
  161. else {
  162. this.cCommandList.SelectedIndex = index;
  163. }
  164. }
  165. public void CommandTypeFilter(string type) {
  166. // Prevent selection change event from being triggered and updating
  167. // selected command value before it's used
  168. this._IsApply = false;
  169. this._CommandType = type;
  170. this.cCommandList.Items.Clear();
  171. if (!this._Commands.ContainsKey(type)) {
  172. this._Commands.Add(type, new List<ICommand>());
  173. }
  174. // All command types
  175. if (type.IsEmpty()) {
  176. // Hide add and remove buttons, edit only
  177. this.cButtons.Visibility = Visibility.Collapsed;
  178. foreach (string key in this._Commands.Keys) {
  179. foreach (ICommand command in this._Commands[key]) {
  180. this.cCommandList.Items.Add(this.CommandListItem(key, command, true));
  181. }
  182. }
  183. }
  184. else { // Specific command type
  185. if (type == "Unknown") { // Can't add unknown types
  186. this.cButtons.Visibility = Visibility.Collapsed;
  187. }
  188. else {
  189. this.cButtons.Visibility = Visibility.Visible;
  190. }
  191. foreach (ICommand command in this._Commands[type]) {
  192. this.cCommandList.Items.Add(this.CommandListItem(type, command, false));
  193. }
  194. }
  195. this._IsApply = true;
  196. // If at least one item then enable remove command
  197. this.cRemoveCommand.IsEnabled = (this.cCommandList.Items.Count > 0);
  198. if (!this._SelectedCommandIndexes.ContainsKey(type)) {
  199. this._SelectedCommandIndexes.Add(type, -1);
  200. }
  201. // If no command is selected, then show empty command message
  202. if (this._SelectedCommandIndexes[type] < 0) {
  203. if (this.cCommandList.Items.Count > 0) {
  204. this._SelectedCommandIndexes[type] = 0;
  205. this.cCommandList.SelectedIndex = this._SelectedCommandIndexes[type];
  206. this.cCommandList.ScrollIntoView(this.cCommandList.SelectedItem);
  207. }
  208. else {
  209. this.cEmptyCommand.Visibility = Visibility.Visible;
  210. this.cConfigurator.Content = null;
  211. }
  212. }
  213. // If a command is selected, show the commands configurator
  214. else if (this._SelectedCommandIndexes[type] >= this.cCommandList.Items.Count) {
  215. this.cEmptyCommand.Visibility = Visibility.Visible;
  216. this.cConfigurator.Content = null;
  217. this._SelectedCommandIndexes[type] = -1;
  218. this.cCommandList.ScrollIntoView(this.cCommandList.SelectedItem);
  219. }
  220. else {
  221. this.cCommandList.SelectedIndex = this._SelectedCommandIndexes[type];
  222. this.cCommandList.ScrollIntoView(this.cCommandList.SelectedItem);
  223. }
  224. }
  225. private ListBoxItem CommandListItem(string type, ICommand command, bool prefix) {
  226. ListBoxItem lbi = new ListBoxItem();
  227. lbi.Tag = type;
  228. lbi.Content = this.CommandListItemName(type, command, prefix);
  229. return lbi;
  230. }
  231. private TextBlock CommandListItemName(string type, ICommand command, bool prefix) {
  232. TextBlock tb = new TextBlock();
  233. // Empty command
  234. if (command.IsEmpty) {
  235. tb.Inlines.Add(new Run("<empty " + type.ToLower() + " command>"));
  236. return tb;
  237. }
  238. // Useable command
  239. if (prefix) {
  240. Bold b = new Bold(new Run("[ "));
  241. b.Foreground = Brushes.DarkGray;
  242. tb.Inlines.Add(b);
  243. tb.Inlines.Add(new Run(type.ToLower()));
  244. b = new Bold(new Run(" ] "));
  245. b.Foreground = Brushes.DarkGray;
  246. tb.Inlines.Add(b);
  247. }
  248. if (command.Name.IsEmpty()) {
  249. tb.Inlines.Add(new Run("<no name set>"));
  250. }
  251. else {
  252. tb.Inlines.Add(new Run(command.Name));
  253. }
  254. if (command.CallStrings.Count > 0) {
  255. Bold b = new Bold(new Run(" ( "));
  256. b.Foreground = Brushes.DarkGray;
  257. tb.Inlines.Add(b);
  258. tb.Inlines.Add(new Italic(new Run(String.Join(", ", command.CallStrings.ToArray()))));
  259. b = new Bold(new Run(" )"));
  260. b.Foreground = Brushes.DarkGray;
  261. tb.Inlines.Add(b);
  262. }
  263. return tb;
  264. }
  265. private void ScrollBarVisibilityChanged(object sender, ScrollBarVisibilityEventArgs e) {
  266. this.cConfigurator.HorizontalScrollBarVisibility = e.HorizontalScrollBarVisibility;
  267. this.cConfigurator.VerticalScrollBarVisibility = e.VerticalScrollBarVisibility;
  268. }
  269. }
  270. }