CrabUI
Loading...
Searching...
No Matches
CUIMultiButton.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;
10
11namespace CrabUI
12{
13 /// <summary>
14 /// Button with multiple options
15 /// which are rotating when you click
16 /// </summary>
18 {
19 private List<string> options = new List<string>();
20 /// <summary>
21 /// Options are just strings
22 /// </summary>
23 [CUISerializable]
24 public IEnumerable<string> Options
25 {
26 get => options;
27 set => options = value.ToList();
28 }
29 public event Action<string> OnSelect;
30 public Action<string> AddOnSelect { set { OnSelect += value; } }
31
32 [CUISerializable] public bool CycleOnClick { get; set; } = true;
33 public int SelectedIndex
34 {
35 get => options.IndexOf(Selected);
36 set
37 {
38 if (options.Count == 0)
39 {
40 Selected = "";
41 }
42 else
43 {
44 Selected = options.ElementAtOrDefault(value % options.Count) ?? "";
45 }
46 }
47 }
48 [CUISerializable]
49 public string Selected
50 {
51 get => Text;
52 set
53 {
54 Text = value;
55 OnSelect?.Invoke(value);
56 }
57 }
58
59 public void Add(string option) => options.Add(option);
60 public void Remove(string option)
61 {
62 int i = options.IndexOf(option);
63 options.Remove(option);
64 if (option == Selected) Select(i);
65 }
66 public void Select(int i) => SelectedIndex = i;
67 public void Select(string option) => Selected = option;
68 public void SelectNext() => SelectedIndex++;
69 public void SelectPrev() => SelectedIndex--;
70
71 public CUIMultiButton() : base()
72 {
73 Text = "MultiButton";
74 OnMouseDown += (e) =>
75 {
76 if (CycleOnClick)
77 {
78 SelectNext();
79 if (Command != null) DispatchUp(new CUICommand(Command, Selected));
80 }
81 };
82 }
83
84
85 /// <summary>
86 /// CUITextBlock DoWrapFor but for all text
87 /// </summary>
88 /// <param name="size"></param>
89 /// <returns></returns>
90 protected override Vector2 DoWrapFor(Vector2 size)
91 {
92 if ((!WrappedForThisSize.HasValue || size == WrappedForThisSize.Value) && !TextPropChanged) return WrappedSize;
93
94 TextPropChanged = false;
95 WrappedForThisSize = size;
96
97 if (Vertical) size = new Vector2(0, size.Y);
98
99
100 IEnumerable<string> WrappedTexts;
101 if (Wrap || Vertical)
102 {
103 WrappedText = Font.WrapText(Text, size.X / TextScale - Padding.X * 2).Trim('\n');
104 WrappedTexts = options.Select(o => Font.WrapText(o, size.X / TextScale - Padding.X * 2).Trim('\n'));
105 }
106 else
107 {
108 WrappedText = Text;
109 WrappedTexts = options;
110 }
111
112 IEnumerable<Vector2> RealTextSizes = WrappedTexts.Select(t => Font.MeasureString(t) * TextScale);
113
114 float maxX = 0;
115 float maxY = 0;
116 foreach (Vector2 s in RealTextSizes)
117 {
118 if (s.X > maxX) maxX = s.X;
119 if (s.Y > maxY) maxY = s.Y;
120 }
121
122 Vector2 MaxTextSize = new Vector2(maxX, maxY);
123
124 RealTextSize = Font.MeasureString(WrappedText) * TextScale;
125
126 if (WrappedText == "") RealTextSize = new Vector2(0, 0);
127 RealTextSize = new Vector2((float)Math.Round(RealTextSize.X), (float)Math.Round(RealTextSize.Y));
128
129 Vector2 minSize = MaxTextSize + Padding * 2;
130
131 if (!Wrap || Vertical)
132 {
133 SetForcedMinSize(new CUINullVector2(minSize));
134 }
135
136 WrappedSize = new Vector2(Math.Max(size.X, minSize.X), Math.Max(size.Y, minSize.Y));
137
138 return WrappedSize;
139 }
140
141 internal override Vector2 AmIOkWithThisSize(Vector2 size)
142 {
143 return DoWrapFor(size);
144 }
145 }
146}
A button It's derived from CUITextBlock and has all its props.
Definition CUIButton.cs:19
Vector2 Padding
Used for text, should be in CUITextBlock really.
string Command
This command will be dispatched up when some component specific event happens.
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
Button with multiple options which are rotating when you click.
override Vector2 DoWrapFor(Vector2 size)
CUITextBlock DoWrapFor but for all text.
IEnumerable< string > Options
Options are just strings.
record CUICommand(string Name, object Data=null)
Can be dispatched up the component tree to notify parent about something add pass some event data wit...
Vector2, but with float?