38f1ddb...178a853: v0.8.9.1, removed content folder
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -13,18 +14,23 @@ namespace Barotrauma
|
||||
private GUIButton button;
|
||||
private GUIListBox listBox;
|
||||
|
||||
private RectTransform currentListBoxParent;
|
||||
private List<RectTransform> parentHierarchy = new List<RectTransform>();
|
||||
|
||||
private bool selectMultiple;
|
||||
|
||||
public bool Dropped { get; set; }
|
||||
|
||||
public object SelectedItemData
|
||||
{
|
||||
get
|
||||
{
|
||||
if (listBox.Selected == null) return null;
|
||||
return listBox.Selected.UserData;
|
||||
if (listBox.SelectedComponent == null) return null;
|
||||
return listBox.SelectedComponent.UserData;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Enabled
|
||||
public override bool Enabled
|
||||
{
|
||||
get { return listBox.Enabled; }
|
||||
set { listBox.Enabled = value; }
|
||||
@@ -32,7 +38,7 @@ namespace Barotrauma
|
||||
|
||||
public GUIComponent Selected
|
||||
{
|
||||
get { return listBox.Selected; }
|
||||
get { return listBox.SelectedComponent; }
|
||||
}
|
||||
|
||||
public GUIListBox ListBox
|
||||
@@ -44,7 +50,7 @@ namespace Barotrauma
|
||||
{
|
||||
get
|
||||
{
|
||||
return (listBox.Selected == null) ? null : listBox.Selected.UserData;
|
||||
return listBox.SelectedComponent?.UserData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,11 +58,29 @@ namespace Barotrauma
|
||||
{
|
||||
get
|
||||
{
|
||||
if (listBox.Selected == null) return -1;
|
||||
return listBox.children.FindIndex(x => x == listBox.Selected);
|
||||
if (listBox.SelectedComponent == null) return -1;
|
||||
return listBox.Content.GetChildIndex(listBox.SelectedComponent);
|
||||
}
|
||||
}
|
||||
|
||||
private List<object> selectedDataMultiple = new List<object>();
|
||||
public IEnumerable<object> SelectedDataMultiple
|
||||
{
|
||||
get { return selectedDataMultiple; }
|
||||
}
|
||||
|
||||
private List<int> selectedIndexMultiple = new List<int>();
|
||||
public IEnumerable<int> SelectedIndexMultiple
|
||||
{
|
||||
get { return selectedIndexMultiple; }
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get { return button.Text; }
|
||||
set { button.Text = value; }
|
||||
}
|
||||
|
||||
public override string ToolTip
|
||||
{
|
||||
get
|
||||
@@ -70,56 +94,108 @@ namespace Barotrauma
|
||||
listBox.ToolTip = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public override Rectangle Rect
|
||||
|
||||
public GUIDropDown(RectTransform rectT, string text = "", int elementCount = 4, string style = "", bool selectMultiple = false) : base(style, rectT)
|
||||
{
|
||||
get
|
||||
this.selectMultiple = selectMultiple;
|
||||
|
||||
button = new GUIButton(new RectTransform(Vector2.One, rectT), text, Alignment.CenterLeft, style: "GUIDropDown")
|
||||
{
|
||||
return base.Rect;
|
||||
}
|
||||
|
||||
set
|
||||
OnClicked = OnClicked
|
||||
};
|
||||
GUI.Style.Apply(button, "", this);
|
||||
|
||||
listBox = new GUIListBox(new RectTransform(new Point(Rect.Width, Rect.Height * MathHelper.Clamp(elementCount, 2, 10)), rectT, Anchor.BottomLeft, Pivot.TopLeft)
|
||||
{ IsFixedSize = false }, style: style)
|
||||
{
|
||||
Point moveAmount = value.Location - rect.Location;
|
||||
base.Rect = value;
|
||||
Enabled = !selectMultiple,
|
||||
OnSelected = SelectItem
|
||||
};
|
||||
|
||||
button.Rect = new Rectangle(button.Rect.Location + moveAmount, button.Rect.Size);
|
||||
listBox.Rect = new Rectangle(listBox.Rect.Location + moveAmount, listBox.Rect.Size);
|
||||
currentListBoxParent = FindListBoxParent();
|
||||
currentListBoxParent.GUIComponent.OnAddedToGUIUpdateList += AddListBoxToGUIUpdateList;
|
||||
rectT.ParentChanged += (RectTransform newParent) =>
|
||||
{
|
||||
currentListBoxParent.GUIComponent.OnAddedToGUIUpdateList -= AddListBoxToGUIUpdateList;
|
||||
if (newParent != null)
|
||||
{
|
||||
currentListBoxParent = FindListBoxParent();
|
||||
currentListBoxParent.GUIComponent.OnAddedToGUIUpdateList += AddListBoxToGUIUpdateList;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Finds the component after which the listbox should be drawn. Usually the parent of the dropdown, but if the dropdown
|
||||
/// is the child of another GUIListBox, we need to draw our listbox after that because listboxes clip everything outside their rect.
|
||||
/// </summary>
|
||||
private RectTransform FindListBoxParent()
|
||||
{
|
||||
parentHierarchy.Clear();
|
||||
parentHierarchy = new List<RectTransform>() { RectTransform.Parent };
|
||||
while (parentHierarchy.Last().Parent != null)
|
||||
{
|
||||
parentHierarchy.Add(parentHierarchy.Last().Parent);
|
||||
}
|
||||
//find the parent GUIListBox highest in the hierarchy
|
||||
for (int i = parentHierarchy.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (parentHierarchy[i].GUIComponent is GUIListBox) return parentHierarchy[i];
|
||||
}
|
||||
//or just go with the direct parent if there are no listboxes in the hierarchy
|
||||
parentHierarchy.Clear();
|
||||
parentHierarchy.Add(RectTransform.Parent);
|
||||
return RectTransform.Parent;
|
||||
}
|
||||
|
||||
public GUIDropDown(Rectangle rect, string text, string style, GUIComponent parent = null)
|
||||
: this(rect, text, style, Alignment.TopLeft, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public GUIDropDown(Rectangle rect, string text, string style, Alignment alignment, GUIComponent parent = null)
|
||||
: base(style)
|
||||
{
|
||||
this.rect = rect;
|
||||
|
||||
if (parent != null) parent.AddChild(this);
|
||||
|
||||
button = new GUIButton(this.rect, text, Color.White, alignment, Alignment.CenterLeft, "GUIDropDown", null);
|
||||
GUI.Style.Apply(button, style, this);
|
||||
|
||||
button.OnClicked = OnClicked;
|
||||
|
||||
listBox = new GUIListBox(new Rectangle(this.rect.X, this.rect.Bottom, this.rect.Width, 200), style, null);
|
||||
listBox.OnSelected = SelectItem;
|
||||
}
|
||||
|
||||
public override void AddChild(GUIComponent child)
|
||||
{
|
||||
listBox.AddChild(child);
|
||||
}
|
||||
|
||||
|
||||
public void AddItem(string text, object userData = null, string toolTip = "")
|
||||
{
|
||||
GUITextBlock textBlock = new GUITextBlock(new Rectangle(0,0,0,20), text, "ListBoxElement", Alignment.TopLeft, Alignment.CenterLeft, listBox);
|
||||
textBlock.UserData = userData;
|
||||
textBlock.ToolTip = toolTip;
|
||||
if (selectMultiple)
|
||||
{
|
||||
var frame = new GUIFrame(new RectTransform(new Point(button.Rect.Width, button.Rect.Height), listBox.Content.RectTransform)
|
||||
{ IsFixedSize = false }, style: "ListBoxElement")
|
||||
{
|
||||
UserData = userData,
|
||||
ToolTip = toolTip
|
||||
};
|
||||
|
||||
new GUITickBox(new RectTransform(new Point((int)(button.Rect.Height * 0.8f)), frame.RectTransform, anchor: Anchor.CenterLeft), text)
|
||||
{
|
||||
UserData = userData,
|
||||
ToolTip = toolTip,
|
||||
OnSelected = (GUITickBox tb) =>
|
||||
{
|
||||
List<string> texts = new List<string>();
|
||||
selectedDataMultiple.Clear();
|
||||
selectedIndexMultiple.Clear();
|
||||
int i = 0;
|
||||
foreach (GUIComponent child in ListBox.Content.Children)
|
||||
{
|
||||
var tickBox = child.GetChild<GUITickBox>();
|
||||
if (tickBox.Selected)
|
||||
{
|
||||
selectedDataMultiple.Add(child.UserData);
|
||||
selectedIndexMultiple.Add(i);
|
||||
texts.Add(tickBox.Text);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
button.Text = string.Join(", ", texts);
|
||||
OnSelected?.Invoke(tb.Parent, tb.Parent.UserData);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
new GUITextBlock(new RectTransform(new Point(button.Rect.Width, button.Rect.Height), listBox.Content.RectTransform)
|
||||
{ IsFixedSize = false }, text, style: "ListBoxElement")
|
||||
{
|
||||
UserData = userData,
|
||||
ToolTip = toolTip
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public override void ClearChildren()
|
||||
@@ -127,44 +203,63 @@ namespace Barotrauma
|
||||
listBox.ClearChildren();
|
||||
}
|
||||
|
||||
public List<GUIComponent> GetChildren()
|
||||
public IEnumerable<GUIComponent> GetChildren()
|
||||
{
|
||||
return listBox.children;
|
||||
return listBox.Content.Children;
|
||||
}
|
||||
|
||||
private bool SelectItem(GUIComponent component, object obj)
|
||||
{
|
||||
GUITextBlock textBlock = component as GUITextBlock;
|
||||
if (textBlock == null)
|
||||
if (selectMultiple)
|
||||
{
|
||||
textBlock = component.GetChild<GUITextBlock>();
|
||||
if (textBlock == null) return false;
|
||||
foreach (GUIComponent child in ListBox.Content.Children)
|
||||
{
|
||||
var tickBox = child.GetChild<GUITickBox>();
|
||||
if (obj == child.UserData) { tickBox.Selected = true; }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GUITextBlock textBlock = component as GUITextBlock;
|
||||
if (textBlock == null)
|
||||
{
|
||||
textBlock = component.GetChild<GUITextBlock>();
|
||||
if (textBlock == null) return false;
|
||||
}
|
||||
button.Text = textBlock.Text;
|
||||
}
|
||||
|
||||
button.Text = textBlock.Text;
|
||||
Dropped = false;
|
||||
|
||||
if (OnSelected != null) OnSelected(component, component.UserData);
|
||||
|
||||
OnSelected?.Invoke(component, component.UserData);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SelectItem(object userData)
|
||||
{
|
||||
//GUIComponent child = listBox.children.FirstOrDefault(c => c.UserData == userData);
|
||||
|
||||
//if (child == null) return;
|
||||
|
||||
listBox.Select(userData);
|
||||
|
||||
//SelectItem(child, userData);
|
||||
if (selectMultiple)
|
||||
{
|
||||
SelectItem(listBox.Content.FindChild(userData), userData);
|
||||
}
|
||||
else
|
||||
{
|
||||
listBox.Select(userData);
|
||||
}
|
||||
}
|
||||
|
||||
public void Select(int index)
|
||||
{
|
||||
listBox.Select(index);
|
||||
if (selectMultiple)
|
||||
{
|
||||
var child = listBox.Content.GetChild(index);
|
||||
if (child != null)
|
||||
{
|
||||
SelectItem(null, child.UserData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
listBox.Select(index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private bool wasOpened;
|
||||
|
||||
@@ -174,60 +269,68 @@ namespace Barotrauma
|
||||
|
||||
wasOpened = true;
|
||||
Dropped = !Dropped;
|
||||
|
||||
if (Dropped)
|
||||
if (Dropped && Enabled)
|
||||
{
|
||||
if (Enabled) OnDropped?.Invoke(this, userData);
|
||||
if (parent.children[parent.children.Count - 1] != this)
|
||||
{
|
||||
parent.children.Remove(this);
|
||||
parent.children.Add(this);
|
||||
}
|
||||
OnDropped?.Invoke(this, userData);
|
||||
listBox.UpdateScrollBarSize();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void AddToGUIUpdateList()
|
||||
private void AddListBoxToGUIUpdateList(GUIComponent parent)
|
||||
{
|
||||
base.AddToGUIUpdateList();
|
||||
button.AddToGUIUpdateList();
|
||||
if (Dropped) listBox.AddToGUIUpdateList();
|
||||
//the parent is not our parent anymore :(
|
||||
//can happen when subscribed to a parent higher in the hierarchy (instead of the direct parent),
|
||||
//and somewhere between this component and the higher parent a component was removed
|
||||
for (int i = 1; i < parentHierarchy.Count; i++)
|
||||
{
|
||||
if (!parentHierarchy[i].IsParentOf(parentHierarchy[i - 1], recursive: false))
|
||||
{
|
||||
parent.OnAddedToGUIUpdateList -= AddListBoxToGUIUpdateList;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (Dropped)
|
||||
{
|
||||
listBox.AddToGUIUpdateList(false, UpdateOrder);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
public override void DrawManually(SpriteBatch spriteBatch, bool alsoChildren = false, bool recursive = true)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
AutoDraw = false;
|
||||
Draw(spriteBatch);
|
||||
if (alsoChildren)
|
||||
{
|
||||
button.DrawManually(spriteBatch, alsoChildren, recursive);
|
||||
}
|
||||
}
|
||||
|
||||
public override void AddToGUIUpdateList(bool ignoreChildren = false, int order = 0)
|
||||
{
|
||||
base.AddToGUIUpdateList(true, order);
|
||||
if (!ignoreChildren)
|
||||
{
|
||||
button.AddToGUIUpdateList(false, order);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Update(float deltaTime)
|
||||
{
|
||||
if (!Visible) return;
|
||||
wasOpened = false;
|
||||
|
||||
base.Update(deltaTime);
|
||||
|
||||
if (Dropped && PlayerInput.LeftButtonClicked())
|
||||
{
|
||||
Rectangle listBoxRect = listBox.Rect;
|
||||
listBoxRect.Width += 20;
|
||||
if (!listBoxRect.Contains(PlayerInput.MousePosition) && !button.Rect.Contains(PlayerInput.MousePosition))
|
||||
{
|
||||
Dropped = false;
|
||||
}
|
||||
}
|
||||
|
||||
button.Update(deltaTime);
|
||||
|
||||
if (Dropped) listBox.Update(deltaTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch spriteBatch)
|
||||
{
|
||||
if (!Visible) return;
|
||||
|
||||
base.Draw(spriteBatch);
|
||||
|
||||
button.Draw(spriteBatch);
|
||||
|
||||
if (!Dropped) return;
|
||||
listBox.Draw(spriteBatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user