ModPropsPane.cs 6.7 KB

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