2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using Microsoft.Xna.Framework.Input;
5 using System.Collections.Generic;
21 private List<RectTransform> parentHierarchy =
new List<RectTransform>();
23 private readonly
bool selectMultiple;
33 if (listBox.SelectedComponent ==
null)
return null;
34 return listBox.SelectedComponent.UserData;
40 get {
return listBox.Enabled; }
41 set { listBox.Enabled = value; }
46 get {
return button.Enabled; }
49 button.Enabled = value;
50 if (icon !=
null) { icon.Enabled = value; }
56 get {
return listBox.SelectedComponent; }
73 get {
return listBox; }
80 return listBox.SelectedComponent?.UserData;
88 if (listBox.SelectedComponent ==
null)
return -1;
89 return listBox.Content.GetChildIndex(listBox.SelectedComponent);
95 get {
return button.TextColor; }
96 set { button.TextColor = value; }
101 get {
return button?.Font ?? base.Font; }
104 if (button !=
null) { button.Font = value; }
110 GUI.KeyboardDispatcher.Subscriber =
null;
122 listBox.ReceiveSpecialInput(key);
123 GUI.KeyboardDispatcher.Subscriber =
this;
126 GUI.KeyboardDispatcher.Subscriber =
null;
131 private readonly List<object> selectedDataMultiple =
new List<object>();
134 get {
return selectedDataMultiple; }
137 private readonly List<int> selectedIndexMultiple =
new List<int>();
140 get {
return selectedIndexMultiple; }
147 get {
return button.Text; }
148 set { button.Text = value; }
159 base.ToolTip = value;
160 button.ToolTip = value;
161 listBox.ToolTip = value;
167 public Vector4
Padding => button.TextBlock.Padding;
169 public GUIDropDown(
RectTransform rectT,
LocalizedString text =
null,
int elementCount = 4,
string style =
"",
bool selectMultiple =
false,
bool dropAbove =
false, Alignment textAlignment = Alignment.CenterLeft) : base(style, rectT)
176 this.selectMultiple = selectMultiple;
180 OnClicked = OnClicked,
181 TextBlock = { OverflowClip =
true }
183 GUIStyle.Apply(button,
"",
this);
184 button.TextBlock.SetTextPos();
189 { IsFixedSize = false }, style:
null)
194 if (!selectMultiple) { listBox.OnSelected = SelectItem; }
195 GUIStyle.Apply(listBox,
"GUIListBox",
this);
196 GUIStyle.Apply(listBox.ContentBackground,
"GUIListBox",
this);
198 if (button.Style.ChildStyles.ContainsKey(
"dropdownicon".ToIdentifier()))
200 icon =
new GUIImage(
new RectTransform(
new Vector2(0.6f, 0.6f), button.RectTransform,
Anchor.CenterRight, scaleBasis:
ScaleBasis.BothHeight) { AbsoluteOffset = new Point(5, 0) },
null, scaleToFit:
true);
201 icon.ApplyStyle(button.Style.ChildStyles[
"dropdownicon".ToIdentifier()]);
204 currentHighestParent = FindHighestParent();
217 parentHierarchy.Clear();
222 while (parent?.
Parent !=
null)
224 parentHierarchy.Add(parent.Parent);
230 for (
int i = parentHierarchy.Count - 1; i > 0; i--)
232 if (parentHierarchy[i] is GUICanvas ||
235 parentHierarchy[i].GUIComponent == Screen.Selected?.Frame)
237 parentHierarchy.RemoveAt(i);
244 return parentHierarchy.Last();
252 var frame =
new GUIFrame(
new RectTransform(
new Point(button.Rect.Width, button.Rect.Height), listBox.Content.RectTransform) { IsFixedSize = false }, style:
"ListBoxElement",
color:
color)
257 new GUITickBox(
new RectTransform(
new Vector2(1.0f, 0.8f), frame.RectTransform, anchor:
Anchor.CenterLeft) { MaxSize = new Point(int.MaxValue, (int)(button.Rect.Height * 0.8f)) }, text)
269 List<LocalizedString> texts =
new List<LocalizedString>();
270 selectedDataMultiple.Clear();
271 selectedIndexMultiple.Clear();
278 selectedDataMultiple.Add(child.
UserData);
279 selectedIndexMultiple.Add(i);
280 texts.Add(tickBox.Text);
286 OnSelected?.Invoke(tb.Parent, tb.Parent.UserData);
294 return new GUITextBlock(
new RectTransform(
new Point(button.Rect.Width, button.Rect.Height), listBox.Content.RectTransform) { IsFixedSize = false }, text, style:
"ListBoxElement",
color:
color, textColor: textColor)
304 listBox.ClearChildren();
309 return listBox.Content.Children;
312 private bool SelectItem(
GUIComponent component,
object obj)
324 if (component is not GUITextBlock textBlock)
326 textBlock = component.
GetChild<GUITextBlock>();
327 if (textBlock is
null && !
AllowNonText) {
return false; }
329 button.Text = textBlock?.Text ??
"";
341 SelectItem(listBox.Content.FindChild(userData), userData);
345 listBox.Select(userData);
353 var child = listBox.Content.
GetChild(index);
356 SelectItem(
null, child.UserData);
361 listBox.Select(index);
365 private bool wasOpened;
367 private bool OnClicked(
GUIComponent component,
object obj)
369 if (wasOpened)
return false;
376 listBox.UpdateScrollBarSize();
377 listBox.UpdateDimensions();
379 GUI.KeyboardDispatcher.Subscriber =
this;
381 else if (GUI.KeyboardDispatcher.Subscriber ==
this)
383 GUI.KeyboardDispatcher.Subscriber =
null;
393 currentHighestParent = FindHighestParent();
397 private void AddListBoxToGUIUpdateList(
GUIComponent parent)
402 for (
int i = 1; i < parentHierarchy.Count; i++)
404 if (parentHierarchy[i].
IsParentOf(parentHierarchy[i - 1], recursive:
false))
415 listBox.AddToGUIUpdateList(
false, 1);
419 public override void DrawManually(SpriteBatch spriteBatch,
bool alsoChildren =
false,
bool recursive =
true)
427 button.DrawManually(spriteBatch, alsoChildren, recursive);
433 base.AddToGUIUpdateList(
true, order);
436 button.AddToGUIUpdateList(
false, order);
440 protected override void Update(
float deltaTime)
444 base.Update(deltaTime);
451 if (GUI.KeyboardDispatcher.Subscriber ==
this)
453 GUI.KeyboardDispatcher.Subscriber =
null;
GUIComponent(string style, RectTransform rectT)
This is the new constructor.
GUIComponent GetChild(int index)
bool IsParentOf(GUIComponent component, bool recursive=true)
virtual bool PlaySoundOnSelect
Action< GUIComponent > OnAddedToGUIUpdateList
virtual void Draw(SpriteBatch spriteBatch)
RectTransform RectTransform
IEnumerable< GUIComponent > Children
override void Update(float deltaTime)
bool MustSelectAtLeastOne
void SelectItem(object userData)
GUIComponent AddItem(LocalizedString text, object userData=null, LocalizedString toolTip=null, Color? color=null, Color? textColor=null)
void ReceiveTextInput(string text)
GUIDropDown(RectTransform rectT, LocalizedString text=null, int elementCount=4, string style="", bool selectMultiple=false, bool dropAbove=false, Alignment textAlignment=Alignment.CenterLeft)
void ReceiveSpecialInput(Keys key)
void ReceiveTextInput(char inputChar)
IEnumerable< GUIComponent > GetChildren()
OnSelectedHandler OnSelected
IEnumerable< int > SelectedIndexMultiple
void RefreshListBoxParent()
void ReceiveEditingInput(string text, int start, int length)
OnSelectedHandler OnDropped
override void DrawManually(SpriteBatch spriteBatch, bool alsoChildren=false, bool recursive=true)
By default, all the gui elements are drawn automatically in the same order they appear on the update ...
override void ClearChildren()
override void AddToGUIUpdateList(bool ignoreChildren=false, int order=0)
override RichString ToolTip
delegate bool OnSelectedHandler(GUIComponent selected, object obj=null)
IEnumerable< object > SelectedDataMultiple
GUIComponent SelectedComponent
void ReceiveCommandInput(char command)
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
static readonly RawLString EmptyString
static LocalizedString Join(string separator, params LocalizedString[] subStrs)