commit 7245c721339885d062567befc052a592391b3b4a Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sun Mar 10 15:22:31 2019 +0200 Fixed StatusEffects only applying afflictions to one limb even if the target is "Character" instead of "Limb", added a subtle screen distortion effect to heavy radiation sickness. Closes #1256 commit e0db27e62ec9546fd4b182a0cc97f7e5830645ae Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sat Mar 9 21:53:51 2019 +0200 Fixed WrapText adding unnecessary spaces after every line break. Closes #1215 commit 988bc58d51c195ad9265b84a1e97e0101cd3f808 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sat Mar 9 21:12:50 2019 +0200 Fixed crashing when attempting to create a body for a wall section that's less than 1 unit long (e.g. if a wall that's just slightly longer than the wall section size receives damage). commit 8c31157425a9e2ec02312618d1bfa359ab3ee87d Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sat Mar 9 20:30:44 2019 +0200 Fixed clients being unable to toggle the respawn shuttle on/off commit a4ccb039219830efe9cd305c26942dda1bd04e9c Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sat Mar 9 19:33:22 2019 +0200 Fixed inability to select the respawn shuttle as a client host commit b89b2d2c282d8c74d7ccd37b3f29dcab51eff680 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sat Mar 9 19:32:41 2019 +0200 Made it possible to edit the style of the ListBox under GUIDropDowns, increased the opacity of the listbox to make the contents more readable when there's text behind it commit 8f6d9aef3d637fe37a18c78f4b15ef8fd266374e Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Sat Mar 9 18:11:23 2019 +0200 Fixed NetLobbyScreen not showing the names of the submarines the client doesn't have
344 lines
11 KiB
C#
344 lines
11 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Barotrauma
|
|
{
|
|
public class GUIDropDown : GUIComponent
|
|
{
|
|
public delegate bool OnSelectedHandler(GUIComponent selected, object obj = null);
|
|
public OnSelectedHandler OnSelected;
|
|
public OnSelectedHandler OnDropped;
|
|
|
|
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.SelectedComponent == null) return null;
|
|
return listBox.SelectedComponent.UserData;
|
|
}
|
|
}
|
|
|
|
public override bool Enabled
|
|
{
|
|
get { return listBox.Enabled; }
|
|
set { listBox.Enabled = value; }
|
|
}
|
|
|
|
public bool ButtonEnabled
|
|
{
|
|
get { return button.Enabled; }
|
|
set { button.Enabled = value; }
|
|
}
|
|
|
|
public GUIComponent Selected
|
|
{
|
|
get { return listBox.SelectedComponent; }
|
|
}
|
|
|
|
public GUIListBox ListBox
|
|
{
|
|
get { return listBox; }
|
|
}
|
|
|
|
public object SelectedData
|
|
{
|
|
get
|
|
{
|
|
return listBox.SelectedComponent?.UserData;
|
|
}
|
|
}
|
|
|
|
public int SelectedIndex
|
|
{
|
|
get
|
|
{
|
|
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
|
|
{
|
|
return base.ToolTip;
|
|
}
|
|
set
|
|
{
|
|
base.ToolTip = value;
|
|
button.ToolTip = value;
|
|
listBox.ToolTip = value;
|
|
}
|
|
}
|
|
|
|
public GUIDropDown(RectTransform rectT, string text = "", int elementCount = 4, string style = "", bool selectMultiple = false) : base(style, rectT)
|
|
{
|
|
this.selectMultiple = selectMultiple;
|
|
|
|
button = new GUIButton(new RectTransform(Vector2.One, rectT), text, Alignment.CenterLeft, style: "GUIDropDown")
|
|
{
|
|
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: null)
|
|
{
|
|
Enabled = !selectMultiple,
|
|
OnSelected = SelectItem
|
|
};
|
|
GUI.Style.Apply(listBox.Content, "GUIListBox", this);
|
|
|
|
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 void AddItem(string text, object userData = null, string 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()
|
|
{
|
|
listBox.ClearChildren();
|
|
}
|
|
|
|
public IEnumerable<GUIComponent> GetChildren()
|
|
{
|
|
return listBox.Content.Children;
|
|
}
|
|
|
|
private bool SelectItem(GUIComponent component, object obj)
|
|
{
|
|
if (selectMultiple)
|
|
{
|
|
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;
|
|
}
|
|
Dropped = false;
|
|
OnSelected?.Invoke(component, component.UserData);
|
|
return true;
|
|
}
|
|
|
|
public void SelectItem(object userData)
|
|
{
|
|
if (selectMultiple)
|
|
{
|
|
SelectItem(listBox.Content.FindChild(userData), userData);
|
|
}
|
|
else
|
|
{
|
|
listBox.Select(userData);
|
|
}
|
|
}
|
|
|
|
public void Select(int index)
|
|
{
|
|
if (selectMultiple)
|
|
{
|
|
var child = listBox.Content.GetChild(index);
|
|
if (child != null)
|
|
{
|
|
SelectItem(null, child.UserData);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
listBox.Select(index);
|
|
}
|
|
}
|
|
|
|
private bool wasOpened;
|
|
|
|
private bool OnClicked(GUIComponent component, object obj)
|
|
{
|
|
if (wasOpened) return false;
|
|
|
|
wasOpened = true;
|
|
Dropped = !Dropped;
|
|
if (Dropped && Enabled)
|
|
{
|
|
OnDropped?.Invoke(this, userData);
|
|
listBox.UpdateScrollBarSize();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void AddListBoxToGUIUpdateList(GUIComponent parent)
|
|
{
|
|
//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 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;
|
|
if (!listBoxRect.Contains(PlayerInput.MousePosition) && !button.Rect.Contains(PlayerInput.MousePosition))
|
|
{
|
|
Dropped = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|