ModPropsPane.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 = Configuration.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 || Configuration.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. this.modFilterToggle.Label = Translation.Get("background2Window", "modsToggle");
  67. this.baseFilterToggle.Label = Translation.Get("background2Window", "baseToggle");
  68. }
  69. public override void Draw()
  70. {
  71. 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. this.propCategoryDropdown.Draw(dropdownLayoutOptions);
  82. GUILayout.FlexibleSpace();
  83. }
  84. else
  85. {
  86. GUI.enabled = this.modItemsReady;
  87. this.propCategoryDropdown.Draw(dropdownLayoutOptions);
  88. GUI.enabled = this.shouldDraw;
  89. this.modFilterToggle.Draw();
  90. this.baseFilterToggle.Draw();
  91. GUI.enabled = true;
  92. }
  93. GUILayout.EndHorizontal();
  94. if (this.shouldDraw)
  95. {
  96. float windowHeight = Screen.height * 0.7f;
  97. int buttonSize = 50;
  98. int offsetLeft = 15;
  99. int offsetTop = 85;
  100. 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 (this.updating) return;
  124. if (modFilterToggle.Value && baseFilterToggle.Value)
  125. {
  126. this.updating = true;
  127. modFilterToggle.Value = filterType == FilterType.Mod;
  128. baseFilterToggle.Value = filterType == FilterType.Base;
  129. this.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. }