2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using Microsoft.Xna.Framework.Input;
5 using System.Collections.Generic;
34 private List<RectTransform> parentHierarchy =
new List<RectTransform>();
36 private readonly
bool selectMultiple;
46 if (listBox.SelectedComponent ==
null)
return null;
47 return listBox.SelectedComponent.UserData;
53 get {
return listBox.Enabled; }
54 set { listBox.Enabled = value; }
59 get {
return button.Enabled; }
62 button.Enabled = value;
63 if (icon !=
null) { icon.Enabled = value; }
69 get {
return listBox.SelectedComponent; }
86 get {
return listBox; }
93 return listBox.SelectedComponent?.UserData;
101 if (listBox.SelectedComponent ==
null)
return -1;
102 return listBox.Content.GetChildIndex(listBox.SelectedComponent);
108 get {
return button.TextColor; }
109 set { button.TextColor = value; }
114 get {
return button?.Font ?? base.Font; }
117 if (button !=
null) { button.Font = value; }
123 GUI.KeyboardDispatcher.Subscriber =
null;
135 listBox.ReceiveSpecialInput(key);
136 GUI.KeyboardDispatcher.Subscriber =
this;
139 GUI.KeyboardDispatcher.Subscriber =
null;
144 private readonly List<object> selectedDataMultiple =
new List<object>();
147 get {
return selectedDataMultiple; }
150 private readonly List<int> selectedIndexMultiple =
new List<int>();
153 get {
return selectedIndexMultiple; }
160 get {
return button.Text; }
161 set { button.Text = value; }
172 base.ToolTip = value;
173 button.ToolTip = value;
174 listBox.ToolTip = value;
180 public Vector4
Padding => button.TextBlock.Padding;
182 public GUIDropDown(
RectTransform rectT,
LocalizedString text =
null,
int elementCount = 4,
string style =
"",
bool selectMultiple =
false,
bool dropAbove =
false, Alignment textAlignment = Alignment.CenterLeft,
float listBoxScale = 1) : base(style, rectT)
189 this.selectMultiple = selectMultiple;
193 OnClicked = OnClicked,
194 TextBlock = { OverflowClip =
true }
196 GUIStyle.Apply(button,
"",
this);
197 button.TextBlock.SetTextPos();
201 listBox =
new GUIListBox(
new RectTransform(
new Point((
int)(
Rect.Width * listBoxScale),
Rect.Height * MathHelper.Clamp(elementCount, 2, 10)), rectT, listAnchor, listPivot)
202 { IsFixedSize = false }, style:
null)
209 listBox.AfterSelected = (component, obj) =>
211 SelectItem(component, obj);
216 GUIStyle.Apply(listBox,
"GUIListBox",
this);
217 GUIStyle.Apply(listBox.ContentBackground,
"GUIListBox",
this);
219 if (button.Style.ChildStyles.ContainsKey(
"dropdownicon".ToIdentifier()))
221 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);
222 icon.ApplyStyle(button.Style.ChildStyles[
"dropdownicon".ToIdentifier()]);
224 button.TextBlock.Padding +=
new Vector4(0, 0, icon.Rect.Width, 0);
227 currentHighestParent = FindHighestParent();
240 parentHierarchy.Clear();
245 while (parent?.
Parent !=
null)
247 parentHierarchy.Add(parent.Parent);
253 for (
int i = parentHierarchy.Count - 1; i > 0; i--)
255 if (parentHierarchy[i] is GUICanvas ||
258 parentHierarchy[i].GUIComponent == Screen.Selected?.Frame)
260 parentHierarchy.RemoveAt(i);
267 return parentHierarchy.Last();
275 var frame =
new GUIFrame(
new RectTransform(
new Point(listBox.Content.Rect.Width, button.Rect.Height), listBox.Content.RectTransform) { IsFixedSize = false }, style:
"ListBoxElement",
color:
color)
280 var tickBox =
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)
297 List<LocalizedString> texts =
new List<LocalizedString>();
298 selectedDataMultiple.Clear();
299 selectedIndexMultiple.Clear();
306 selectedDataMultiple.Add(child.
UserData);
307 selectedIndexMultiple.Add(i);
308 texts.Add(tickBox.Text);
321 return new GUITextBlock(
new RectTransform(
new Point(listBox.Content.Rect.Width, button.Rect.Height), listBox.Content.RectTransform) { IsFixedSize = false }, text, style:
"ListBoxElement",
color:
color, textColor: textColor)
331 listBox.ClearChildren();
336 return listBox.Content.Children;
339 private bool SelectItem(
GUIComponent component,
object obj)
351 if (component is not GUITextBlock textBlock)
353 textBlock = component.
GetChild<GUITextBlock>();
354 if (textBlock is
null && !
AllowNonText) {
return false; }
356 button.Text = textBlock?.Text ??
"";
367 SelectItem(listBox.Content.FindChild(userData), userData);
371 listBox.Select(userData);
380 var child = listBox.Content.
GetChild(index);
383 SelectItem(
null, child.UserData);
388 listBox.Select(index);
393 private bool wasOpened;
395 private bool OnClicked(
GUIComponent component,
object obj)
397 if (wasOpened)
return false;
404 listBox.UpdateScrollBarSize();
405 listBox.UpdateDimensions();
407 GUI.KeyboardDispatcher.Subscriber =
this;
409 else if (GUI.KeyboardDispatcher.Subscriber ==
this)
411 GUI.KeyboardDispatcher.Subscriber =
null;
421 currentHighestParent = FindHighestParent();
425 private void AddListBoxToGUIUpdateList(
GUIComponent parent)
430 for (
int i = 1; i < parentHierarchy.Count; i++)
432 if (parentHierarchy[i].
IsParentOf(parentHierarchy[i - 1], recursive:
false))
443 listBox.AddToGUIUpdateList(
false, 1);
447 public override void DrawManually(SpriteBatch spriteBatch,
bool alsoChildren =
false,
bool recursive =
true)
455 button.DrawManually(spriteBatch, alsoChildren, recursive);
461 base.AddToGUIUpdateList(
true, order);
464 button.AddToGUIUpdateList(
false, order);
468 protected override void Update(
float deltaTime)
472 base.Update(deltaTime);
479 if (GUI.KeyboardDispatcher.Subscriber ==
this)
481 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
OnSelectedHandler AfterSelected
Triggers after an item has been selected from the dropdown, all validation has been done and the new ...
void SelectItem(object userData)
GUIComponent AddItem(LocalizedString text, object userData=null, LocalizedString toolTip=null, Color? color=null, Color? textColor=null)
void ReceiveTextInput(string text)
void ReceiveSpecialInput(Keys key)
GUIDropDown(RectTransform rectT, LocalizedString text=null, int elementCount=4, string style="", bool selectMultiple=false, bool dropAbove=false, Alignment textAlignment=Alignment.CenterLeft, float listBoxScale=1)
void ReceiveTextInput(char inputChar)
IEnumerable< GUIComponent > GetChildren()
OnSelectedHandler OnSelected
Triggers when some item is cliecked from the dropdown. Note that SelectedData is not set yet when thi...
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)