ModPropsPane.cs 6.6 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. public 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 float buttonSize = 54f;
  70. public override void Draw()
  71. {
  72. const float dropdownButtonHeight = 30f;
  73. float dropdownButtonWidth = isModsOnly ? 120f : 90f;
  74. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  75. GUILayout.Height(dropdownButtonHeight),
  76. GUILayout.Width(dropdownButtonWidth)
  77. };
  78. GUILayout.BeginHorizontal();
  79. if (isModsOnly)
  80. {
  81. GUILayout.FlexibleSpace();
  82. propCategoryDropdown.Draw(dropdownLayoutOptions);
  83. GUILayout.FlexibleSpace();
  84. }
  85. else
  86. {
  87. GUI.enabled = modItemsReady;
  88. propCategoryDropdown.Draw(dropdownLayoutOptions);
  89. GUI.enabled = shouldDraw;
  90. modFilterToggle.Draw();
  91. baseFilterToggle.Draw();
  92. GUI.enabled = true;
  93. }
  94. GUILayout.EndHorizontal();
  95. if (shouldDraw)
  96. {
  97. Rect windowRect = parent.WindowRect;
  98. float windowHeight = windowRect.height;
  99. float windowWidth = windowRect.width;
  100. // const float buttonSize = 50f;
  101. const float offsetTop = 80f;
  102. const int columns = 4;
  103. float buttonSize = (windowWidth / columns) - 10f;
  104. Rect positionRect = new Rect(
  105. 5f, offsetTop + dropdownButtonHeight, windowWidth - 10f, windowHeight - 145f
  106. );
  107. Rect viewRect = new Rect(
  108. 0f, 0f, buttonSize * columns, (buttonSize * Mathf.Ceil(currentListCount / (float)columns)) + 5
  109. );
  110. propListScrollPos = GUI.BeginScrollView(positionRect, propListScrollPos, viewRect);
  111. int modIndex = 0;
  112. foreach (ModItem modItem in modPropList)
  113. {
  114. if ((modFilter && !modItem.IsMod) || (baseFilter && modItem.IsMod)) continue;
  115. float x = modIndex % columns * buttonSize;
  116. float y = modIndex / columns * buttonSize;
  117. Rect iconRect = new Rect(x, y, buttonSize, buttonSize);
  118. if (GUI.Button(iconRect, "")) propManager.SpawnModItemProp(modItem);
  119. GUI.DrawTexture(iconRect, modItem.Icon);
  120. modIndex++;
  121. }
  122. GUI.EndScrollView();
  123. }
  124. }
  125. private void ChangeFilter(FilterType filterType)
  126. {
  127. if (updating) return;
  128. if (modFilterToggle.Value && baseFilterToggle.Value)
  129. {
  130. updating = true;
  131. modFilterToggle.Value = filterType == FilterType.Mod;
  132. baseFilterToggle.Value = filterType == FilterType.Base;
  133. updating = false;
  134. }
  135. modFilter = modFilterToggle.Value;
  136. baseFilter = baseFilterToggle.Value;
  137. SetListCount();
  138. }
  139. private void ChangePropCategory()
  140. {
  141. string category = SelectedCategory;
  142. if (currentCategory == category) return;
  143. currentCategory = category;
  144. categoryIndex = propCategoryDropdown.SelectedItemIndex;
  145. shouldDraw = categoryIndex > 0;
  146. if (!shouldDraw) return;
  147. propListScrollPos = Vector2.zero;
  148. modPropList = Constants.GetModPropList(category);
  149. SetListCount();
  150. }
  151. private void SetListCount()
  152. {
  153. if (modFilter) currentListCount = modPropList.Count(mod => mod.IsMod);
  154. else if (baseFilter) currentListCount = modPropList.Count(mod => !mod.IsMod);
  155. else currentListCount = modPropList.Count;
  156. }
  157. }
  158. }