Client LuaCsForBarotrauma
UiUtil.cs
1 #nullable enable
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
6 using Microsoft.Xna.Framework;
7 
8 namespace Barotrauma.Steam
9 {
10  abstract partial class WorkshopMenu
11  {
12  protected static RectTransform NewItemRectT(GUILayoutGroup parent, float heightScale = 1.0f)
13  => new RectTransform((1.0f, 0.06f * heightScale), parent.RectTransform, Anchor.CenterLeft);
14 
15  protected static void Spacer(GUILayoutGroup parent, float height = 0.03f)
16  {
17  new GUIFrame(new RectTransform((1.0f, height), parent.RectTransform, Anchor.CenterLeft), style: null);
18  }
19 
20  protected static GUITextBlock Label(GUILayoutGroup parent, LocalizedString str, GUIFont font, float heightScale = 1.0f)
21  {
22  return new GUITextBlock(NewItemRectT(parent, heightScale), str, font: font);
23  }
24 
25  protected static GUITextBox ScrollableTextBox(GUILayoutGroup parent, float heightScale, string text)
26  {
27  var containingListBox = new GUIListBox(NewItemRectT(parent, heightScale));
28  var textBox = new GUITextBox(
29  new RectTransform(Vector2.One, containingListBox.Content.RectTransform),
30  "", style: "GUITextBoxNoBorder", wrap: true,
31  textAlignment: Alignment.TopLeft);
32  textBox.OnTextChanged += (textBox, text) =>
33  {
34  string wrappedText = textBox.TextBlock.WrappedText.Value;
35  int measuredHeight = (int)textBox.Font.MeasureString(wrappedText).Y;
36  textBox.RectTransform.NonScaledSize =
37  (containingListBox.Content.Rect.Width,
38  Math.Max(measuredHeight, containingListBox.Content.Rect.Height));
39  containingListBox.UpdateScrollBarSize();
40 
41  return true;
42  };
43  textBox.OnEnterPressed += (textBox, text) =>
44  {
45  string str = textBox.Text;
46  int cursorPos = textBox.CaretIndex;
47  textBox.Text = $"{str[..cursorPos]}\n{str[cursorPos..]}";
48  textBox.CaretIndex = cursorPos + 1;
49 
50  return true;
51  };
52  textBox.Text = text;
53  return textBox;
54  }
55 
56  protected static GUIDropDown DropdownEnum<T>(
57  GUILayoutGroup parent, Func<T, LocalizedString> textFunc, T currentValue,
58  Action<T> setter) where T : Enum
59  => Dropdown(parent, textFunc, (T[])Enum.GetValues(typeof(T)), currentValue, setter);
60 
61  protected static GUIDropDown Dropdown<T>(
62  GUILayoutGroup parent, Func<T, LocalizedString> textFunc, IReadOnlyList<T> values, T currentValue,
63  Action<T> setter, float heightScale = 1.0f)
64  {
65  var dropdown = new GUIDropDown(NewItemRectT(parent, heightScale));
66  SwapDropdownValues(dropdown, textFunc, values, currentValue, setter);
67  return dropdown;
68  }
69 
70  protected static void SwapDropdownValues<T>(
71  GUIDropDown dropdown,
72  Func<T, LocalizedString> textFunc,
73  IReadOnlyList<T> values,
74  T currentValue,
75  Action<T> setter)
76  {
77  if (dropdown.ListBox.Content.Children.Any(c => c.UserData is not T))
78  {
79  throw new Exception("SwapValues must preserve the type of the dropdown's userdata");
80  }
81 
82  dropdown.OnSelected = null;
83  dropdown.ClearChildren();
84 
85  values.ForEach(v => dropdown.AddItem(text: textFunc(v), userData: v));
86  dropdown.Select(values.IndexOf(currentValue));
87  dropdown.OnSelected = (dd, obj) =>
88  {
89  setter((T)obj);
90  return true;
91  };
92  }
93 
94  protected static int Round(float v) => (int)MathF.Round(v);
95  protected static string Percentage(float v) => $"{Round(v * 100)}";
96 
97  protected struct ActionCarrier
98  {
99  public readonly Identifier Id;
100  public readonly Action Action;
101  public ActionCarrier(Identifier id, Action action)
102  {
103  Id = id;
104  Action = action;
105  }
106  }
107 
108  protected GUIComponent CreateActionCarrier(GUIComponent parent, Identifier id, Action action)
109  => new GUIFrame(new RectTransform(Vector2.Zero, parent.RectTransform), style: null)
110  { UserData = new ActionCarrier(id, action) };
111 
112  protected GUITextBox CreateSearchBox(RectTransform searchRectT)
113  {
114  var searchHolder = new GUIFrame(searchRectT, style: null);
115  var searchBox = new GUITextBox(new RectTransform(Vector2.One, searchHolder.RectTransform), "", createClearButton: true);
116  var searchTitle = new GUITextBlock(new RectTransform(Vector2.One, searchHolder.RectTransform) {Anchor = Anchor.TopLeft},
117  textColor: Color.DarkGray * 0.6f,
118  text: TextManager.Get("Search") + TextManager.Get("ellipsis"),
119  textAlignment: Alignment.CenterLeft)
120  {
121  CanBeFocused = false
122  };
123  new GUICustomComponent(new RectTransform(Vector2.Zero, searchHolder.RectTransform), onUpdate:
124  (f, component) =>
125  {
126  searchTitle.RectTransform.NonScaledSize = searchBox.Frame.RectTransform.NonScaledSize;
127  });
128  searchBox.OnSelected += (sender, userdata) => { searchTitle.Visible = false; };
129  searchBox.OnDeselected += (sender, userdata) => { searchTitle.Visible = searchBox.Text.IsNullOrWhiteSpace(); };
130 
131  searchBox.OnTextChanged += (sender, str) =>
132  {
134  return true;
135  };
136  return searchBox;
137  }
138 
139  protected static void CreateModErrorInfo(ContentPackage mod, GUIComponent uiElement, GUITextBlock nameText)
140  {
141  uiElement.ToolTip = "";
142  if (mod.FatalLoadErrors.Any())
143  {
144  const int maxErrorsToShow = 5;
145  nameText.TextColor = GUIStyle.Red;
146  uiElement.ToolTip =
147  TextManager.GetWithVariable("ContentPackageHasFatalErrors", "[packagename]", mod.Name)
148  + '\n' + string.Join('\n', mod.FatalLoadErrors.Take(maxErrorsToShow).Select(e => e.Message));
149  if (mod.FatalLoadErrors.Length > maxErrorsToShow)
150  {
151  uiElement.ToolTip += '\n' + TextManager.GetWithVariable("workshopitemdownloadprompttruncated", "[number]", (mod.FatalLoadErrors.Count() - maxErrorsToShow).ToString());
152  }
153  }
154 
155  if (mod.EnableError.IsSome())
156  {
157  nameText.TextColor = GUIStyle.Red;
158  if (!uiElement.ToolTip.IsNullOrWhiteSpace()) { uiElement.ToolTip += "\n"; }
159  uiElement.ToolTip += TextManager.GetWithVariable(
160  "ContentPackageEnableError", "[packagename]", mod.Name);
161  }
162  }
163  }
164 }
ImmutableArray< LoadError > FatalLoadErrors
Errors that occurred when loading this content package. Currently, all errors are considered fatal an...
Option< ContentPackageManager.LoadProgress.Error > EnableError
An error that occurred when trying to enable this mod. This field doesn't directly affect whether or ...
virtual RichString ToolTip
RectTransform RectTransform
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
GUIComponent that can be used to render custom content on the UI
GUIComponent AddItem(LocalizedString text, object userData=null, LocalizedString toolTip=null, Color? color=null, Color? textColor=null)
Definition: GUIDropDown.cs:247
OnSelectedHandler OnSelected
Definition: GUIDropDown.cs:13
void Select(int index)
Definition: GUIDropDown.cs:349
override void ClearChildren()
Definition: GUIDropDown.cs:302
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
Definition: GUIListBox.cs:33
static GUITextBox ScrollableTextBox(GUILayoutGroup parent, float heightScale, string text)
Definition: UiUtil.cs:25
GUIComponent CreateActionCarrier(GUIComponent parent, Identifier id, Action action)
static void Spacer(GUILayoutGroup parent, float height=0.03f)
Definition: UiUtil.cs:15
static GUIDropDown Dropdown< T >(GUILayoutGroup parent, Func< T, LocalizedString > textFunc, IReadOnlyList< T > values, T currentValue, Action< T > setter, float heightScale=1.0f)
Definition: UiUtil.cs:61
static void CreateModErrorInfo(ContentPackage mod, GUIComponent uiElement, GUITextBlock nameText)
Definition: UiUtil.cs:139
abstract void UpdateModListItemVisibility()
static string Percentage(float v)
static GUITextBlock Label(GUILayoutGroup parent, LocalizedString str, GUIFont font, float heightScale=1.0f)
Definition: UiUtil.cs:20
static RectTransform NewItemRectT(GUILayoutGroup parent, float heightScale=1.0f)
static GUIDropDown DropdownEnum< T >(GUILayoutGroup parent, Func< T, LocalizedString > textFunc, T currentValue, Action< T > setter)
GUITextBox CreateSearchBox(RectTransform searchRectT)
Definition: UiUtil.cs:112
static int Round(float v)
static void SwapDropdownValues< T >(GUIDropDown dropdown, Func< T, LocalizedString > textFunc, IReadOnlyList< T > values, T currentValue, Action< T > setter)
Definition: UiUtil.cs:70
ActionCarrier(Identifier id, Action action)
Definition: UiUtil.cs:101