CrabUI
Loading...
Searching...
No Matches
CUIDropDown.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10using System.Xml;
11using System.Xml.Linq;
12namespace CrabUI
13{
14 /// <summary>
15 /// Drop down list, aka Select
16 /// </summary>
18 {
19 internal class DDOption : CUIButton
20 {
21 public DDOption() : this("") { }
22 public DDOption(string text) : base(text) { }
23 }
24 private CUIButton MainButton;
25 private CUIVerticalList OptionBox;
26
27 /// <summary>
28 /// List of options
29 /// Options are just strings
30 /// </summary>
31 [CUISerializable]
32 public IEnumerable<string> Options
33 {
34 get => OptionBox.Children.Cast<DDOption>().Select(o => o.Text);
35 set
36 {
37 Clear();
38 foreach (string option in value) { Add(option); }
39 }
40 }
41 [CUISerializable]
42 public string Selected
43 {
44 get => MainButton.Text;
45 set => Select(value);
46 }
47
48 public event Action<string> OnSelect;
49 public Action<string> AddOnSelect { set { OnSelect += value; } }
50
51
52 public void Open() => OptionBox.Revealed = true;
53 public void Close() => OptionBox.Revealed = false;
54
55 public void Clear()
56 {
57 OptionBox.RemoveAllChildren();
58 Select("");
59 }
60
61 public void Add(string option)
62 {
63 OptionBox.Append(new DDOption(option)
64 {
65 AddOnMouseDown = (e) => Select(option),
66 });
67 }
68
69 public void Select(int i) => Select(Options.ElementAtOrDefault(i));
70 public void Select(string option)
71 {
72 MainButton.Text = option ?? "";
73 OptionBox.Revealed = false;
74 OnSelect?.Invoke(MainButton.Text);
75 }
76
77 public void Remove(int i) => Remove(Options.ElementAtOrDefault(i));
78 public void Remove(string option)
79 {
80 if (option == null) return;
81 if (!Options.Contains(option)) return;
82
83 DDOption ddoption = OptionBox.Children.Cast<DDOption>().FirstOrDefault(o => o.Text == option);
84 bool wasSelected = MainButton.Text == ddoption.Text;
85 OptionBox.RemoveChild(ddoption);
86 if (wasSelected) Select(0);
87 }
88
89 public CUIDropDown() : base()
90 {
91 SerializeChildren = false;
92 OptionBox = new CUIVerticalList()
93 {
94 Relative = new CUINullRect(w: 1),
95 FitContent = new CUIBool2(true, true),
96 Ghost = new CUIBool2(false, true),
97 Anchor = CUIAnchor.TopLeft,
98 ParentAnchor = CUIAnchor.BottomLeft,
99 ZIndex = 500,
100 Style = new CUIStyle(){
101 {"BackgroundColor", "CUIPalette.DDOption.Background"},
102 {"Border", "CUIPalette.DDOption.Border"},
103 },
104 };
105
106 MainButton = new CUIButton()
107 {
108 Text = "CUIDropDown",
109 Relative = new CUINullRect(w: 1, h: 1),
110 AddOnMouseDown = (e) => OptionBox.Revealed = !OptionBox.Revealed,
111 };
112
113 Append(MainButton);
114 Append(OptionBox);
115
116 FitContent = new CUIBool2(true, true);
117
118 //HACK Why this main is hardcoded?
119 //in static constructor CUI.Main is null and this won't work
120 if (CUI.Main is not null) CUI.Main.OnMouseDown += (e) => Close();
121 }
122 }
123}
A button It's derived from CUITextBlock and has all its props.
Definition CUIButton.cs:19
Base class for all components.
bool Revealed
Visible + !IgnoreEvents.
CUINullRect Relative
Relative to parent size and position, [0..1].
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
CUIStyle Style
Allows you to assing parsable string or link to CUIPalette to any prop It's indexable,...
Vector2 Anchor
this point of this component
CUIBool2 FitContent
Will resize itself to fit components with absolute size, e.g. text.
bool SerializeChildren
Is this a serialization cutoff point Parent will serialize children down to this component and stop...
Vector2? ParentAnchor
will be attached to this point of parent
CUIBool2 Ghost
Ghost components don't affect layout.
int? ZIndex
Components are drawn in order of their ZIndex Normally it's derived from component position in the ...
Drop down list, aka Select.
IEnumerable< string > Options
List of options Options are just strings.
Resizing components to it's Width and placing them sequentially.