Client LuaCsForBarotrauma
MutableWorkshopMenu.cs
1 #nullable enable
3 using Microsoft.Xna.Framework;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Threading;
7 
8 namespace Barotrauma.Steam
9 {
10  sealed partial class MutableWorkshopMenu : WorkshopMenu
11  {
12  public enum Tab
13  {
14  InstalledMods,
15  //Overrides, //TODO: implement later
16  PopularMods,
17  Publish
18  }
19 
20  private enum Filter
21  {
22  ShowLocal,
23  ShowWorkshop,
24  ShowPublished,
25  ShowOnlySubs,
26  ShowOnlyItemAssemblies
27  }
28 
29  public Tab CurrentTab { get; private set; }
30 
31  private readonly GUILayoutGroup tabber;
32  private readonly Dictionary<Tab, (GUIButton Button, GUIFrame Content)> tabContents;
33 
34  private readonly GUIFrame contentFrame;
35 
36  private CancellationTokenSource taskCancelSrc = new CancellationTokenSource();
37  private readonly HashSet<SteamManager.Workshop.ItemThumbnail> itemThumbnails = new HashSet<SteamManager.Workshop.ItemThumbnail>();
38 
39  private readonly Option<GUIListBox> popularModsListOption;
40  private readonly Option<GUIListBox> selfModsListOption;
41 
42  private uint memSubscribedModCount = 0;
43 
44  private static bool EnableWorkshopSupport => SteamManager.IsInitialized;
45 
46  public MutableWorkshopMenu(GUIFrame parent) : base(parent)
47  {
48  var mainLayout
49  = new GUILayoutGroup(new RectTransform(Vector2.One, parent.RectTransform), isHorizontal: false)
50  {
51  Stretch = true,
52  AbsoluteSpacing = GUI.IntScale(4)
53  };
54 
55  Vector2 tabberSize = EnableWorkshopSupport ? (1.0f, 0.05f) : Vector2.Zero;
56 
57  tabber = new GUILayoutGroup(new RectTransform(tabberSize, mainLayout.RectTransform), isHorizontal: true)
58  { Stretch = true };
59  tabContents = new Dictionary<Tab, (GUIButton Button, GUIFrame Content)>();
60 
61  if (EnableWorkshopSupport)
62  {
63  new GUIButton(new RectTransform((1.0f, 0.05f), mainLayout.RectTransform, Anchor.BottomLeft),
64  style: "GUIButtonSmall", text: TextManager.Get("FindModsButton"))
65  {
66  OnClicked = (button, o) =>
67  {
68  SteamManager.OverlayCustomUrl($"https://steamcommunity.com/app/{SteamManager.AppID}/workshop/");
69  return false;
70  }
71  };
72  }
73  else
74  {
75  tabber.Visible = false;
76  }
77 
78  contentFrame = new GUIFrame(new RectTransform((1.0f, 0.95f), mainLayout.RectTransform), style: null);
79 
80  new GUICustomComponent(new RectTransform(Vector2.Zero, mainLayout.RectTransform),
81  onUpdate: (f, component) => UpdateSubscribedModInstalls());
82 
83  CreateInstalledModsTab(
84  out enabledCoreDropdown,
85  out enabledRegularModsList,
86  out disabledRegularModsList,
87  out onInstalledInfoButtonHit,
88  out modsListFilter,
89  out modsListFilterTickboxes,
90  out bulkUpdateButtonOption);
91 
92  if (EnableWorkshopSupport)
93  {
94  CreatePopularModsTab(out GUIListBox popularModList);
95  CreatePublishTab(out GUIListBox selfModsList);
96 
97  popularModsListOption = Option<GUIListBox>.Some(popularModList);
98  selfModsListOption = Option<GUIListBox>.Some(selfModsList);
99  }
100  else
101  {
102  popularModsListOption = Option.None;
103  selfModsListOption = Option.None;
104  }
105 
106  SelectTab(Tab.InstalledMods);
107  }
108 
109  private void SwitchContent(GUIFrame newContent)
110  {
111  contentFrame.Children.ForEach(c => c.Visible = false);
112  newContent.Visible = true;
113  }
114 
115  public void SelectTab(Tab tab)
116  {
117  CurrentTab = tab;
118  SwitchContent(tabContents[tab].Content);
119  tabber.Children.ForEach(c =>
120  {
121  if (c is GUIButton btn) { btn.Selected = btn == tabContents[tab].Button; }
122  });
123  if (!taskCancelSrc.IsCancellationRequested) { taskCancelSrc.Cancel(); }
124  itemThumbnails.ForEach(t => t.Dispose());
125  itemThumbnails.Clear();
126  switch (tab)
127  {
128  case Tab.InstalledMods:
130  break;
131  case Tab.PopularMods when popularModsListOption.TryUnwrap(out var popularModsList):
132  PopulateItemList(popularModsList, SteamManager.Workshop.GetPopularItems(), includeSubscribeButton: true);
133  break;
134  case Tab.Publish when selfModsListOption.TryUnwrap(out var selfModsList):
135  PopulateItemList(selfModsList, SteamManager.Workshop.GetPublishedItems(), includeSubscribeButton: false, onFill: AddUnpublishedMods);
136  break;
137  }
138  }
139 
140  private void AddButtonToTabber(Tab tab, GUIFrame content)
141  {
142  var button = new GUIButton(new RectTransform(Vector2.One, tabber.RectTransform, Anchor.BottomCenter, Pivot.BottomCenter), TextManager.Get($"workshopmenutab.{tab}"), style: "GUITabButton")
143  {
144  OnClicked = (b, _) =>
145  {
146  if (tab != CurrentTab)
147  {
148  SelectTab(tab);
149  }
150  return false;
151  }
152  };
153  button.RectTransform.MaxSize = RectTransform.MaxPoint;
154  button.Children.ForEach(c => c.RectTransform.MaxSize = RectTransform.MaxPoint);
155 
156  tabContents.Add(tab, (button, content));
157  }
158 
159  private GUIFrame CreateNewContentFrame(Tab tab)
160  {
161  var content = new GUIFrame(new RectTransform(Vector2.One * 0.98f, contentFrame.RectTransform, Anchor.Center, Pivot.Center), style: null);
162  AddButtonToTabber(tab, content);
163  return content;
164  }
165 
166  private void CreatePopularModsTab(out GUIListBox popularModsList)
167  {
168  GUIFrame content = CreateNewContentFrame(Tab.PopularMods);
169  if (!SteamManager.IsInitialized)
170  {
171  tabContents[Tab.PopularMods].Button.Enabled = false;
172  }
173  GUIFrame listFrame = new GUIFrame(new RectTransform(Vector2.One, content.RectTransform), style: null);
174  CreateWorkshopItemList(listFrame, out _, out popularModsList, onSelected: PopulateFrameWithItemInfo);
175  }
176 
177  private void CreatePublishTab(out GUIListBox selfModsList)
178  {
179  GUIFrame content = CreateNewContentFrame(Tab.Publish);
180  if (!SteamManager.IsInitialized)
181  {
182  tabContents[Tab.Publish].Button.Enabled = false;
183  }
184  CreateWorkshopItemOrPackageList(content, out _, out selfModsList, onSelected: PopulatePublishTab);
185  }
186 
187  public void Apply()
188  {
189  ContentPackageManager.EnabledPackages.SetCore(EnabledCorePackage);
190  ContentPackageManager.EnabledPackages.SetRegular(enabledRegularModsList.Content.Children
191  .Select(c => c.UserData as RegularPackage).OfType<RegularPackage>().ToArray());
192  PopulateInstalledModLists(forceRefreshEnabled: true, refreshDisabled: true);
193  ContentPackageManager.LogEnabledRegularPackageErrors();
194  enabledCoreDropdown.ButtonTextColor =
195  EnabledCorePackage.HasAnyErrors
196  ? GUIStyle.Red
197  : GUIStyle.TextColorNormal;
198  }
199  }
200 }
RectTransform RectTransform
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
GUIComponent that can be used to render custom content on the UI
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
Definition: GUIListBox.cs:42
void PopulateInstalledModLists(bool forceRefreshEnabled=false, bool refreshDisabled=true)