ModPropsPane.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. using static MenuFileUtility;
  7. internal class ModPropsPane : BasePane
  8. {
  9. private readonly PropManager propManager;
  10. private readonly Dropdown propCategoryDropdown;
  11. private readonly Toggle modFilterToggle;
  12. private readonly Toggle baseFilterToggle;
  13. private Vector2 propListScrollPos;
  14. private string SelectedCategory => MenuCategories[propCategoryDropdown.SelectedItemIndex];
  15. private List<ModItem> modPropList;
  16. private string currentCategory;
  17. private bool modItemsReady;
  18. private bool shouldDraw;
  19. private int categoryIndex;
  20. private bool modFilter;
  21. private bool baseFilter;
  22. private int currentListCount;
  23. private readonly bool isModsOnly = PropManager.ModItemsOnly;
  24. private enum FilterType
  25. {
  26. None, Mod, Base
  27. }
  28. public ModPropsPane(PropManager propManager)
  29. {
  30. this.propManager = propManager;
  31. modItemsReady = MenuFilesReady || PropManager.ModItemsOnly;
  32. string[] listItems = Translation.GetArray("clothing", MenuCategories);
  33. if (!modItemsReady)
  34. {
  35. listItems[0] = Translation.Get("systemMessage", "initializing");
  36. MenuFilesReadyChange += (s, a) =>
  37. {
  38. modItemsReady = true;
  39. propCategoryDropdown.SetDropdownItems(
  40. Translation.GetArray("clothing", MenuCategories)
  41. );
  42. };
  43. }
  44. propCategoryDropdown = new Dropdown(listItems);
  45. propCategoryDropdown.SelectionChange += (s, a) =>
  46. {
  47. if (!modItemsReady) return;
  48. ChangePropCategory();
  49. };
  50. if (!isModsOnly)
  51. {
  52. modFilterToggle = new Toggle(Translation.Get("background2Window", "modsToggle"));
  53. modFilterToggle.ControlEvent += (s, a) => ChangeFilter(FilterType.Mod);
  54. baseFilterToggle = new Toggle(Translation.Get("background2Window", "baseToggle"));
  55. baseFilterToggle.ControlEvent += (s, a) => ChangeFilter(FilterType.Base);
  56. }
  57. }
  58. protected override void ReloadTranslation()
  59. {
  60. string[] listItems = Translation.GetArray("clothing", MenuCategories);
  61. if (!modItemsReady) listItems[0] = Translation.Get("systemMessage", "initializing");
  62. propCategoryDropdown.SetDropdownItems(listItems);
  63. if (!isModsOnly)
  64. {
  65. modFilterToggle.Label = Translation.Get("background2Window", "modsToggle");
  66. baseFilterToggle.Label = Translation.Get("background2Window", "baseToggle");
  67. }
  68. }
  69. public override void Draw()
  70. {
  71. const float dropdownButtonHeight = 30f;
  72. float dropdownButtonWidth = isModsOnly ? 120f : 90f;
  73. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  74. GUILayout.Height(dropdownButtonHeight),
  75. GUILayout.Width(dropdownButtonWidth)
  76. };
  77. GUILayout.BeginHorizontal();
  78. if (isModsOnly)
  79. {
  80. GUILayout.FlexibleSpace();
  81. propCategoryDropdown.Draw(dropdownLayoutOptions);
  82. GUILayout.FlexibleSpace();
  83. }
  84. else
  85. {
  86. GUI.enabled = modItemsReady;
  87. propCategoryDropdown.Draw(dropdownLayoutOptions);
  88. GUI.enabled = shouldDraw;
  89. modFilterToggle.Draw();
  90. baseFilterToggle.Draw();
  91. GUI.enabled = true;
  92. }
  93. GUILayout.EndHorizontal();
  94. if (shouldDraw)
  95. {
  96. float windowHeight = Screen.height * 0.7f;
  97. const int buttonSize = 50;
  98. const int offsetLeft = 15;
  99. const int offsetTop = 85;
  100. const int columns = 4;
  101. Rect positionRect = new Rect(offsetLeft, offsetTop + dropdownButtonHeight, 220, windowHeight);
  102. Rect viewRect = new Rect(
  103. 0, 0, buttonSize * columns, (buttonSize * Mathf.Ceil(currentListCount / (float)columns)) + 5
  104. );
  105. propListScrollPos = GUI.BeginScrollView(positionRect, propListScrollPos, viewRect);
  106. int modIndex = 0;
  107. foreach (ModItem modItem in modPropList)
  108. {
  109. if ((modFilter && !modItem.IsMod) || (baseFilter && modItem.IsMod)) continue;
  110. float x = modIndex % columns * buttonSize;
  111. float y = modIndex / columns * buttonSize;
  112. Rect iconRect = new Rect(x, y, buttonSize, buttonSize);
  113. if (GUI.Button(iconRect, "")) propManager.SpawnModItemProp(modItem);
  114. GUI.DrawTexture(iconRect, modItem.Icon);
  115. modIndex++;
  116. }
  117. GUI.EndScrollView();
  118. GUILayout.Space(windowHeight);
  119. }
  120. }
  121. private void ChangeFilter(FilterType filterType)
  122. {
  123. if (updating) return;
  124. if (modFilterToggle.Value && baseFilterToggle.Value)
  125. {
  126. updating = true;
  127. modFilterToggle.Value = filterType == FilterType.Mod;
  128. baseFilterToggle.Value = filterType == FilterType.Base;
  129. updating = false;
  130. }
  131. modFilter = modFilterToggle.Value;
  132. baseFilter = baseFilterToggle.Value;
  133. SetListCount();
  134. }
  135. private void ChangePropCategory()
  136. {
  137. string category = SelectedCategory;
  138. if (currentCategory == category) return;
  139. currentCategory = category;
  140. categoryIndex = propCategoryDropdown.SelectedItemIndex;
  141. shouldDraw = categoryIndex > 0;
  142. if (!shouldDraw) return;
  143. propListScrollPos = Vector2.zero;
  144. modPropList = Constants.GetModPropList(category);
  145. SetListCount();
  146. }
  147. private void SetListCount()
  148. {
  149. if (modFilter) currentListCount = modPropList.Count(mod => mod.IsMod);
  150. else if (baseFilter) currentListCount = modPropList.Count(mod => !mod.IsMod);
  151. else currentListCount = modPropList.Count;
  152. }
  153. }
  154. }