PropsPane.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. public class PropsPane : BasePane
  7. {
  8. private readonly PropManager propManager;
  9. private string currentCategory;
  10. private string SelectedCategory => Constants.DoguCategories[doguCategoryDropdown.SelectedItemIndex];
  11. private readonly Dropdown doguCategoryDropdown;
  12. private readonly Dropdown doguDropdown;
  13. private readonly Button addDoguButton;
  14. private readonly Button nextDoguButton;
  15. private readonly Button prevDoguButton;
  16. private readonly Button nextDoguCategoryButton;
  17. private readonly Button prevDoguCategoryButton;
  18. private static bool handItemsReady;
  19. private bool itemSelectorEnabled = true;
  20. public PropsPane(PropManager propManager)
  21. {
  22. this.propManager = propManager;
  23. handItemsReady = Constants.HandItemsInitialized;
  24. if (!handItemsReady) Constants.MenuFilesChange += InitializeHandItems;
  25. doguCategoryDropdown = new Dropdown(Translation.GetArray("doguCategories", Constants.DoguCategories));
  26. doguCategoryDropdown.SelectionChange += (s, a) => ChangeDoguCategory(SelectedCategory);
  27. doguDropdown = new Dropdown(new[] { string.Empty });
  28. addDoguButton = new Button("+");
  29. addDoguButton.ControlEvent += (s, a) => SpawnObject();
  30. nextDoguButton = new Button(">");
  31. nextDoguButton.ControlEvent += (s, a) => doguDropdown.Step(1);
  32. prevDoguButton = new Button("<");
  33. prevDoguButton.ControlEvent += (s, a) => doguDropdown.Step(-1);
  34. nextDoguCategoryButton = new Button(">");
  35. nextDoguCategoryButton.ControlEvent += (s, a) => doguCategoryDropdown.Step(1);
  36. prevDoguCategoryButton = new Button("<");
  37. prevDoguCategoryButton.ControlEvent += (s, a) => doguCategoryDropdown.Step(-1);
  38. ChangeDoguCategory(SelectedCategory);
  39. }
  40. protected override void ReloadTranslation()
  41. {
  42. doguCategoryDropdown.SetDropdownItems(
  43. Translation.GetArray("doguCategories", Constants.DoguCategories)
  44. );
  45. string category = SelectedCategory;
  46. string[] translationArray;
  47. if (category == Constants.customDoguCategories[Constants.DoguCategory.HandItem] && !handItemsReady)
  48. {
  49. translationArray = new[] { Translation.Get("systemMessage", "initializing") };
  50. }
  51. else translationArray = GetTranslations(category);
  52. doguDropdown.SetDropdownItems(translationArray);
  53. }
  54. public override void Draw()
  55. {
  56. const float buttonHeight = 30;
  57. GUILayoutOption[] arrowLayoutOptions = {
  58. GUILayout.Width(buttonHeight),
  59. GUILayout.Height(buttonHeight)
  60. };
  61. const float dropdownButtonWidth = 120f;
  62. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  63. GUILayout.Height(buttonHeight),
  64. GUILayout.Width(dropdownButtonWidth)
  65. };
  66. GUILayout.BeginHorizontal();
  67. prevDoguCategoryButton.Draw(arrowLayoutOptions);
  68. doguCategoryDropdown.Draw(dropdownLayoutOptions);
  69. nextDoguCategoryButton.Draw(arrowLayoutOptions);
  70. GUILayout.EndHorizontal();
  71. GUI.enabled = itemSelectorEnabled;
  72. GUILayout.BeginHorizontal();
  73. doguDropdown.Draw(dropdownLayoutOptions);
  74. prevDoguButton.Draw(arrowLayoutOptions);
  75. nextDoguButton.Draw(arrowLayoutOptions);
  76. addDoguButton.Draw(arrowLayoutOptions);
  77. GUILayout.EndHorizontal();
  78. GUI.enabled = true;
  79. }
  80. private void InitializeHandItems(object sender, MenuFilesEventArgs args)
  81. {
  82. if (args.Type == MenuFilesEventArgs.EventType.HandItems)
  83. {
  84. handItemsReady = true;
  85. string selectedCategory = SelectedCategory;
  86. if (selectedCategory == Constants.customDoguCategories[Constants.DoguCategory.HandItem])
  87. {
  88. ChangeDoguCategory(selectedCategory, true);
  89. }
  90. }
  91. }
  92. private void ChangeDoguCategory(string category, bool force = false)
  93. {
  94. if (category != currentCategory || force)
  95. {
  96. currentCategory = category;
  97. string[] translationArray;
  98. if (category == Constants.customDoguCategories[Constants.DoguCategory.HandItem] && !handItemsReady)
  99. {
  100. translationArray = new[] { Translation.Get("systemMessage", "initializing") };
  101. itemSelectorEnabled = false;
  102. }
  103. else
  104. {
  105. translationArray = GetTranslations(category);
  106. itemSelectorEnabled = true;
  107. }
  108. doguDropdown.SetDropdownItems(translationArray, 0);
  109. }
  110. }
  111. private string[] GetTranslations(string category)
  112. {
  113. IEnumerable<string> itemList = Constants.DoguDict[category];
  114. if (category == Constants.customDoguCategories[Constants.DoguCategory.HandItem])
  115. {
  116. itemList = itemList.Select(item =>
  117. {
  118. string handItemAsOdogu = Utility.HandItemToOdogu(item);
  119. return Translation.Has("propNames", handItemAsOdogu) ? handItemAsOdogu : item;
  120. });
  121. }
  122. string translationCategory = category == Constants.customDoguCategories[Constants.DoguCategory.BGSmall]
  123. ? "bgNames"
  124. : "propNames";
  125. return Translation.GetArray(translationCategory, itemList);
  126. }
  127. private void SpawnObject()
  128. {
  129. string assetName = Constants.DoguDict[SelectedCategory][doguDropdown.SelectedItemIndex];
  130. if (SelectedCategory == Constants.customDoguCategories[Constants.DoguCategory.BGSmall])
  131. {
  132. propManager.AddBgProp(assetName);
  133. }
  134. else
  135. {
  136. propManager.AddGameProp(assetName);
  137. }
  138. }
  139. }
  140. }