DFOptionControl.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 SelectButton(OptionButton button)
  78. {
  79. foreach (var child in Children)
  80. {
  81. if (child is OptionButton childButton && childButton != button)
  82. {
  83. childButton.Selected = false;
  84. }
  85. }
  86. button.Selected = true;
  87. }
  88. public string? GetValue()
  89. {
  90. foreach (var child in Children)
  91. {
  92. if (child is OptionButton button)
  93. {
  94. if (button.Selected)
  95. {
  96. return button.Option;
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. public void SetValue(string value)
  103. {
  104. foreach (var child in Children)
  105. {
  106. if (child is OptionButton button)
  107. {
  108. if (button.Option == value)
  109. {
  110. SelectButton(button);
  111. }
  112. }
  113. }
  114. }
  115. public bool IsEmpty() => GetValue() == null;
  116. }
  117. public class RadioOptionControl : Grid, IOptionControl
  118. {
  119. private Action OnChanged;
  120. public RadioOptionControl(string[] options, Action onChanged)
  121. {
  122. Margin = new Thickness(4,2,4,2);
  123. OnChanged = onChanged;
  124. foreach (var option in options)
  125. {
  126. RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  127. var button = new RadioButton();
  128. button.Margin = new Thickness(0, 2, 0, 2);
  129. button.Content = option.Replace("\r", "").Replace("\n", "");
  130. button.VerticalContentAlignment = VerticalAlignment.Center;
  131. button.Padding = new Thickness(5.0F, 0.0F, 5.0F, 0.0F);
  132. button.SetValue(RowProperty, Children.Count);
  133. button.Tag = option;
  134. button.Checked += Button_Checked;
  135. button.Unchecked += Button_Checked;
  136. Children.Add(button);
  137. }
  138. }
  139. private void Button_Checked(object sender, RoutedEventArgs e)
  140. {
  141. OnChanged();
  142. }
  143. public string? GetValue()
  144. {
  145. foreach (var child in Children)
  146. {
  147. if (child is RadioButton radio)
  148. {
  149. if (radio.IsChecked == true)
  150. {
  151. return radio.Tag as string;
  152. }
  153. }
  154. }
  155. return null;
  156. }
  157. public void SetValue(string value)
  158. {
  159. foreach (var child in Children)
  160. {
  161. if (child is RadioButton radio)
  162. {
  163. if ((string)radio.Tag == value)
  164. {
  165. radio.IsChecked = true;
  166. }
  167. }
  168. }
  169. }
  170. public bool IsEmpty() => GetValue() == null;
  171. }
  172. public class ComboBoxOptionControl : ComboBox, IOptionControl
  173. {
  174. private readonly Action OnChanged;
  175. public ComboBoxOptionControl(string[] options, Action onChanged)
  176. {
  177. SetResourceReference(StyleProperty, typeof(ComboBox));
  178. OnChanged = onChanged;
  179. foreach (var option in options)
  180. Items.Add(option);
  181. VerticalContentAlignment = VerticalAlignment.Center;
  182. SelectionChanged += ComboBoxOptionControl_SelectionChanged;
  183. }
  184. private void ComboBoxOptionControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  185. {
  186. OnChanged();
  187. }
  188. public string? GetValue()
  189. {
  190. return SelectedValue as string;
  191. }
  192. public void SetValue(string value)
  193. {
  194. SelectedValue = value;
  195. }
  196. public bool IsEmpty() => GetValue() == null;
  197. }
  198. public class CheckBoxOptionControl : CheckBox, IOptionControl
  199. {
  200. private readonly Action OnChanged;
  201. private readonly string TrueValue;
  202. private readonly string FalseValue;
  203. public CheckBoxOptionControl(string trueValue, string falseValue, Action onChanged)
  204. {
  205. OnChanged = onChanged;
  206. TrueValue = trueValue;
  207. FalseValue = falseValue;
  208. IsThreeState = false;
  209. HorizontalContentAlignment = HorizontalAlignment.Center;
  210. VerticalContentAlignment = VerticalAlignment.Center;
  211. Checked += CheckBoxOptionControl_Checked;
  212. Unchecked += CheckBoxOptionControl_Checked;
  213. }
  214. private void CheckBoxOptionControl_Checked(object sender, RoutedEventArgs e)
  215. {
  216. OnChanged();
  217. }
  218. public string? GetValue()
  219. {
  220. return IsChecked == true
  221. ? TrueValue
  222. : FalseValue;
  223. }
  224. public void SetValue(string value)
  225. {
  226. IsChecked = value == TrueValue;
  227. }
  228. public bool IsEmpty() => false;
  229. }
  230. public class DFOptionControl : DynamicFormFieldControl<DFLayoutOptionField, DFLayoutOptionFieldProperties, string, string?>
  231. {
  232. private IOptionControl OptionControl = null!; // Late-initialised
  233. protected override FrameworkElement Create()
  234. {
  235. var options = string.IsNullOrWhiteSpace(Field.Properties.Options)
  236. ? new string[] { }
  237. : Field.Properties.Options.Replace(",","\n").Split('\n').Select(x=>x.Trim()).ToArray();
  238. switch (Field.Properties.OptionType)
  239. {
  240. case DFLayoutOptionType.Buttons:
  241. return SetControl(new ButtonsOptionControl(options, ChangeField));
  242. case DFLayoutOptionType.Radio:
  243. return SetControl(new RadioOptionControl(options, ChangeField));
  244. }
  245. return SetControl(new ComboBoxOptionControl(options, ChangeField));
  246. }
  247. private T SetControl<T>(T value)
  248. where T : FrameworkElement, IOptionControl
  249. {
  250. OptionControl = value;
  251. return value;
  252. }
  253. public override void SetSerializedValue(string? value)
  254. {
  255. SetValue(value);
  256. }
  257. public override string? GetSerializedValue()
  258. {
  259. return OptionControl.GetValue();
  260. }
  261. public override string GetValue() => OptionControl.GetValue() ?? "";
  262. public override void SetValue(string? value) => OptionControl.SetValue(value ?? "");
  263. protected override bool IsEmpty() => OptionControl.IsEmpty();
  264. }