DFOptionControl.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. namespace InABox.DynamicGrid;
  11. public interface IOptionControl
  12. {
  13. public string? GetValue();
  14. public void SetValue(string? value);
  15. public bool IsEmpty();
  16. }
  17. public class ButtonsOptionControl : Grid, IOptionControl
  18. {
  19. private Action OnChanged;
  20. private class OptionButton : Border
  21. {
  22. public string Option { get; set; }
  23. private bool _selected { get; set; }
  24. public bool Selected
  25. {
  26. get => _selected;
  27. set
  28. {
  29. _selected = value;
  30. if (value == true)
  31. {
  32. Background = new SolidColorBrush(Colors.LightGray);
  33. }
  34. else if (value == false)
  35. {
  36. Background = new SolidColorBrush(Colors.DarkGray);
  37. }
  38. }
  39. }
  40. public OptionButton(string option, bool selected)
  41. {
  42. Option = option;
  43. Selected = selected;
  44. }
  45. }
  46. public ButtonsOptionControl(string[] options, Action onChanged, string? defValue)
  47. {
  48. OnChanged = onChanged;
  49. foreach (var option in options)
  50. {
  51. ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  52. var button = new OptionButton(option, option == defValue);
  53. var label = new Label()
  54. {
  55. Content = option.Replace("\r", "").Replace("\n", "")
  56. };
  57. label.HorizontalContentAlignment = HorizontalAlignment.Center;
  58. label.VerticalContentAlignment = VerticalAlignment.Center;
  59. button.Child = label;
  60. button.Margin = new Thickness(
  61. Children.Count == 0 ? 0.0F : 2.5F,
  62. 0.0F,
  63. Children.Count == options.Length - 1 ? 0.0F : 2.5F,
  64. 0.0F
  65. );
  66. button.Padding = new Thickness(5.0F, 0.0F, 5.0F, 0.0F);
  67. button.MouseUp += Button_MouseUp;
  68. button.SetValue(ColumnProperty, Children.Count);
  69. Children.Add(button);
  70. }
  71. }
  72. private void Button_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
  73. {
  74. SelectButton((OptionButton)sender);
  75. OnChanged();
  76. }
  77. private void DeselectButtons()
  78. {
  79. foreach (var child in Children)
  80. {
  81. if (child is OptionButton childButton)
  82. {
  83. childButton.Selected = false;
  84. }
  85. }
  86. }
  87. private void SelectButton(OptionButton button)
  88. {
  89. foreach (var child in Children)
  90. {
  91. if (child is OptionButton childButton && childButton != button)
  92. {
  93. childButton.Selected = false;
  94. }
  95. }
  96. button.Selected = true;
  97. }
  98. public string? GetValue()
  99. {
  100. foreach (var child in Children)
  101. {
  102. if (child is OptionButton button)
  103. {
  104. if (button.Selected)
  105. {
  106. return button.Option;
  107. }
  108. }
  109. }
  110. return null;
  111. }
  112. public void SetValue(string? value)
  113. {
  114. var found = false;
  115. foreach (var child in Children)
  116. {
  117. if (child is OptionButton button)
  118. {
  119. if (button.Option == value)
  120. {
  121. SelectButton(button);
  122. found = true;
  123. }
  124. }
  125. }
  126. if (!found)
  127. {
  128. DeselectButtons();
  129. }
  130. }
  131. public bool IsEmpty() => GetValue() == null;
  132. }
  133. public class RadioOptionControl : Grid, IOptionControl
  134. {
  135. private Action OnChanged;
  136. public RadioOptionControl(string[] options, Action onChanged, string? defValue)
  137. {
  138. Margin = new Thickness(4,2,4,2);
  139. OnChanged = onChanged;
  140. foreach (var option in options)
  141. {
  142. RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  143. var button = new RadioButton();
  144. button.Margin = new Thickness(0, 2, 0, 2);
  145. button.Content = option.Replace("\r", "").Replace("\n", "");
  146. button.VerticalContentAlignment = VerticalAlignment.Center;
  147. button.Padding = new Thickness(5.0F, 0.0F, 5.0F, 0.0F);
  148. button.SetValue(RowProperty, Children.Count);
  149. button.Tag = option;
  150. button.IsChecked = option == defValue;
  151. button.Checked += Button_Checked;
  152. button.Unchecked += Button_Checked;
  153. Children.Add(button);
  154. }
  155. }
  156. private void Button_Checked(object sender, RoutedEventArgs e)
  157. {
  158. OnChanged();
  159. }
  160. public string? GetValue()
  161. {
  162. foreach (var child in Children)
  163. {
  164. if (child is RadioButton radio)
  165. {
  166. if (radio.IsChecked == true)
  167. {
  168. return radio.Tag as string;
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174. public void SetValue(string? value)
  175. {
  176. var found = false;
  177. foreach (var child in Children)
  178. {
  179. if (child is RadioButton radio)
  180. {
  181. if ((string)radio.Tag == value)
  182. {
  183. radio.IsChecked = true;
  184. found = true;
  185. }
  186. }
  187. }
  188. if (!found)
  189. {
  190. foreach (var child in Children)
  191. {
  192. if (child is RadioButton radio)
  193. {
  194. radio.IsChecked = false;
  195. }
  196. }
  197. }
  198. }
  199. public bool IsEmpty() => GetValue() == null;
  200. }
  201. public class ComboBoxOptionControl : ComboBox, IOptionControl
  202. {
  203. private readonly Action OnChanged;
  204. public ComboBoxOptionControl(string[] options, Action onChanged, string? defValue)
  205. {
  206. SetResourceReference(StyleProperty, typeof(ComboBox));
  207. OnChanged = onChanged;
  208. foreach (var option in options)
  209. Items.Add(option);
  210. SelectedValue = defValue;
  211. VerticalContentAlignment = VerticalAlignment.Center;
  212. SelectionChanged += ComboBoxOptionControl_SelectionChanged;
  213. }
  214. private void ComboBoxOptionControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  215. {
  216. OnChanged();
  217. }
  218. public string? GetValue()
  219. {
  220. return SelectedValue as string;
  221. }
  222. public void SetValue(string? value)
  223. {
  224. SelectedValue = value;
  225. }
  226. public bool IsEmpty() => GetValue() == null;
  227. }
  228. public class CheckBoxOptionControl : CheckBox, IOptionControl
  229. {
  230. private readonly Action OnChanged;
  231. private readonly string TrueValue;
  232. private readonly string FalseValue;
  233. private bool? _value;
  234. private bool Required;
  235. public CheckBoxOptionControl(string trueValue, string falseValue, Action onChanged, bool required, bool? defValue)
  236. {
  237. OnChanged = onChanged;
  238. TrueValue = trueValue;
  239. FalseValue = falseValue;
  240. Required = required;
  241. IsChecked = defValue ?? (required ? null : false);
  242. HorizontalContentAlignment = HorizontalAlignment.Center;
  243. VerticalContentAlignment = VerticalAlignment.Center;
  244. Checked += CheckBoxOptionControl_Checked;
  245. Unchecked += CheckBoxOptionControl_Checked;
  246. }
  247. private void CheckBoxOptionControl_Checked(object sender, RoutedEventArgs e)
  248. {
  249. _value = IsChecked;
  250. OnChanged();
  251. }
  252. public string? GetValue()
  253. {
  254. return _value switch
  255. {
  256. true => TrueValue,
  257. false => FalseValue,
  258. null => null
  259. };
  260. }
  261. public void SetValue(string? value)
  262. {
  263. _value = value == TrueValue ? true
  264. : value == FalseValue ? false
  265. : null;
  266. IsChecked = Required ? _value : (_value ?? false);
  267. }
  268. public bool IsEmpty() => IsChecked is null;
  269. }
  270. public class DFOptionControl : DynamicFormFieldControl<DFLayoutOptionField, DFLayoutOptionFieldProperties, string, string?>
  271. {
  272. private IOptionControl OptionControl = null!; // Late-initialised
  273. protected override FrameworkElement Create()
  274. {
  275. var options = string.IsNullOrWhiteSpace(Field.Properties.Options)
  276. ? []
  277. : Field.Properties.Options.Replace(",","\n").Split('\n').ToArray(x => x.Trim());
  278. return Field.Properties.OptionType switch
  279. {
  280. DFLayoutOptionType.Buttons => SetControl(new ButtonsOptionControl(options, ChangeField, Field.Properties.Default)),
  281. DFLayoutOptionType.Radio => SetControl(new RadioOptionControl(options, ChangeField, Field.Properties.Default)),
  282. _ => SetControl(new ComboBoxOptionControl(options, ChangeField, Field.Properties.Default)),
  283. };
  284. }
  285. private T SetControl<T>(T value)
  286. where T : FrameworkElement, IOptionControl
  287. {
  288. OptionControl = value;
  289. return value;
  290. }
  291. public override void SetSerializedValue(string? value)
  292. {
  293. SetValue(value);
  294. }
  295. public override string? GetSerializedValue()
  296. {
  297. return OptionControl.GetValue();
  298. }
  299. public override string GetValue() => OptionControl.GetValue() ?? "";
  300. public override void SetValue(string? value) => OptionControl.SetValue(value);
  301. protected override bool IsEmpty() => OptionControl.IsEmpty();
  302. }