DFOptionControl.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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)
  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, false);
  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)
  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.Checked += Button_Checked;
  151. button.Unchecked += Button_Checked;
  152. Children.Add(button);
  153. }
  154. }
  155. private void Button_Checked(object sender, RoutedEventArgs e)
  156. {
  157. OnChanged();
  158. }
  159. public string? GetValue()
  160. {
  161. foreach (var child in Children)
  162. {
  163. if (child is RadioButton radio)
  164. {
  165. if (radio.IsChecked == true)
  166. {
  167. return radio.Tag as string;
  168. }
  169. }
  170. }
  171. return null;
  172. }
  173. public void SetValue(string value)
  174. {
  175. var found = false;
  176. foreach (var child in Children)
  177. {
  178. if (child is RadioButton radio)
  179. {
  180. if ((string)radio.Tag == value)
  181. {
  182. radio.IsChecked = true;
  183. found = true;
  184. }
  185. }
  186. }
  187. if (!found)
  188. {
  189. foreach (var child in Children)
  190. {
  191. if (child is RadioButton radio)
  192. {
  193. radio.IsChecked = false;
  194. }
  195. }
  196. }
  197. }
  198. public bool IsEmpty() => GetValue() == null;
  199. }
  200. public class ComboBoxOptionControl : ComboBox, IOptionControl
  201. {
  202. private readonly Action OnChanged;
  203. public ComboBoxOptionControl(string[] options, Action onChanged)
  204. {
  205. SetResourceReference(StyleProperty, typeof(ComboBox));
  206. OnChanged = onChanged;
  207. foreach (var option in options)
  208. Items.Add(option);
  209. VerticalContentAlignment = VerticalAlignment.Center;
  210. SelectionChanged += ComboBoxOptionControl_SelectionChanged;
  211. }
  212. private void ComboBoxOptionControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  213. {
  214. OnChanged();
  215. }
  216. public string? GetValue()
  217. {
  218. return SelectedValue as string;
  219. }
  220. public void SetValue(string value)
  221. {
  222. SelectedValue = value;
  223. }
  224. public bool IsEmpty() => GetValue() == null;
  225. }
  226. public class CheckBoxOptionControl : CheckBox, IOptionControl
  227. {
  228. private readonly Action OnChanged;
  229. private readonly string TrueValue;
  230. private readonly string FalseValue;
  231. public CheckBoxOptionControl(string trueValue, string falseValue, Action onChanged)
  232. {
  233. OnChanged = onChanged;
  234. TrueValue = trueValue;
  235. FalseValue = falseValue;
  236. IsThreeState = false;
  237. HorizontalContentAlignment = HorizontalAlignment.Center;
  238. VerticalContentAlignment = VerticalAlignment.Center;
  239. Checked += CheckBoxOptionControl_Checked;
  240. Unchecked += CheckBoxOptionControl_Checked;
  241. }
  242. private void CheckBoxOptionControl_Checked(object sender, RoutedEventArgs e)
  243. {
  244. OnChanged();
  245. }
  246. public string? GetValue()
  247. {
  248. return IsChecked == true
  249. ? TrueValue
  250. : FalseValue;
  251. }
  252. public void SetValue(string value)
  253. {
  254. IsChecked = value == TrueValue;
  255. }
  256. public bool IsEmpty() => false;
  257. }
  258. public class DFOptionControl : DynamicFormFieldControl<DFLayoutOptionField, DFLayoutOptionFieldProperties, string, string?>
  259. {
  260. private IOptionControl OptionControl = null!; // Late-initialised
  261. protected override FrameworkElement Create()
  262. {
  263. var options = string.IsNullOrWhiteSpace(Field.Properties.Options)
  264. ? new string[] { }
  265. : Field.Properties.Options.Replace(",","\n").Split('\n').Select(x=>x.Trim()).ToArray();
  266. switch (Field.Properties.OptionType)
  267. {
  268. case DFLayoutOptionType.Buttons:
  269. return SetControl(new ButtonsOptionControl(options, ChangeField));
  270. case DFLayoutOptionType.Radio:
  271. return SetControl(new RadioOptionControl(options, ChangeField));
  272. }
  273. return SetControl(new ComboBoxOptionControl(options, ChangeField));
  274. }
  275. private T SetControl<T>(T value)
  276. where T : FrameworkElement, IOptionControl
  277. {
  278. OptionControl = value;
  279. return value;
  280. }
  281. public override void SetSerializedValue(string? value)
  282. {
  283. SetValue(value);
  284. }
  285. public override string? GetSerializedValue()
  286. {
  287. return OptionControl.GetValue();
  288. }
  289. public override string GetValue() => OptionControl.GetValue() ?? "";
  290. public override void SetValue(string? value) => OptionControl.SetValue(value ?? "");
  291. protected override bool IsEmpty() => OptionControl.IsEmpty();
  292. }