RenderableInspector.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Inspectors
  9. * @{
  10. */
  11. /// <summary>
  12. /// Renders an inspector for the <see cref="Renderable"/> component.
  13. /// </summary>
  14. [CustomInspector(typeof (Renderable))]
  15. internal class RenderableInspector : Inspector
  16. {
  17. private List<MaterialParamGUI[]> materialParams = new List<MaterialParamGUI[]>();
  18. private List<MaterialVariationGUI> materialVariations = new List<MaterialVariationGUI>();
  19. private RRef<Material>[] materials;
  20. private GUILayout materialsLayout;
  21. /// <inheritdoc/>
  22. protected internal override void Initialize()
  23. {
  24. Renderable renderable = (Renderable)InspectedObject;
  25. drawer.AddDefault(renderable);
  26. materials = renderable.Materials;
  27. materialsLayout = Layout.AddLayoutY();
  28. BuildMaterialsGUI();
  29. }
  30. /// <inheritdoc/>
  31. protected internal override InspectableState Refresh(bool force = false)
  32. {
  33. Renderable renderable = InspectedObject as Renderable;
  34. if (renderable == null)
  35. return InspectableState.NotModified;
  36. bool rebuildMaterialsGUI = false;
  37. RRef<Material>[] newMaterials = renderable.Materials;
  38. if (newMaterials == null)
  39. rebuildMaterialsGUI = materials != null;
  40. else
  41. {
  42. if (materials == null)
  43. rebuildMaterialsGUI = true;
  44. else
  45. {
  46. if (materials.Length != newMaterials.Length)
  47. rebuildMaterialsGUI = true;
  48. else
  49. {
  50. for (int i = 0; i < materials.Length; i++)
  51. {
  52. if (materials[i] != newMaterials[i])
  53. {
  54. rebuildMaterialsGUI = true;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. if (rebuildMaterialsGUI)
  62. BuildMaterialsGUI();
  63. if (materials != null)
  64. {
  65. for (int i = 0; i < materialParams.Count; i++)
  66. {
  67. Material material = materials[i].Value;
  68. if (material != null && materialParams[i] != null)
  69. {
  70. foreach (var param in materialParams[i])
  71. param.Refresh(material);
  72. }
  73. }
  74. }
  75. return base.Refresh(force);
  76. }
  77. /// <inheritdoc />
  78. internal override void FocusOnField(string path)
  79. {
  80. if (path.StartsWith("materialParams"))
  81. {
  82. string subPath = InspectableField.GetSubPath(path, 2);
  83. string[] subPathParts = subPath.Split('/');
  84. if (subPathParts.Length < 2)
  85. return;
  86. int lastLeftIdx = subPathParts[0].LastIndexOf('[');
  87. int lastRightIdx = subPathParts[0].LastIndexOf(']', lastLeftIdx);
  88. if (lastLeftIdx == -1 || lastRightIdx == -1)
  89. return;
  90. int count = lastRightIdx - 1 - lastLeftIdx;
  91. if (count <= 0)
  92. return;
  93. string arrayIdxStr = subPath.Substring(lastLeftIdx, count);
  94. if (!int.TryParse(arrayIdxStr, out int idx))
  95. return;
  96. if (idx >= materialParams.Count)
  97. return;
  98. MaterialParamGUI[] entries = materialParams[idx];
  99. foreach (var entry in entries)
  100. {
  101. string fieldSubPath = subPathParts.Length > 2 ? subPathParts[2] : null;
  102. if (entry.Param.name == subPathParts[1])
  103. {
  104. entry.SetHasFocus(fieldSubPath);
  105. break;
  106. }
  107. }
  108. }
  109. base.FocusOnField(path);
  110. }
  111. /// <summary>
  112. /// Builds the portion of the GUI that displays details about individual materials.
  113. /// </summary>
  114. private void BuildMaterialsGUI()
  115. {
  116. materialsLayout.Clear();
  117. materialParams.Clear();
  118. materialVariations.Clear();
  119. if (materials != null && materials.Length > 0)
  120. {
  121. for (int i = 0; i < materials.Length; i++)
  122. {
  123. string suffix = "";
  124. if (materials.Length > 1)
  125. suffix = " (" + i + ")";
  126. materialsLayout.AddSpace(10);
  127. GUIToggle foldout = new GUIToggle(new LocEdString("Material parameters" + suffix), EditorStyles.Foldout);
  128. materialsLayout.AddElement(foldout);
  129. GUILayoutY materialLayout = materialsLayout.AddLayoutY();
  130. string tag = "Material" + i + "_Expanded";
  131. foldout.OnToggled += x =>
  132. {
  133. materialLayout.Active = x;
  134. Persistent.SetBool(tag, x);
  135. };
  136. materialLayout.Active = Persistent.GetBool(tag);
  137. Material material = materials[i].Value;
  138. if (material == null)
  139. {
  140. materialParams.Add(new MaterialParamGUI[0]);
  141. continue;
  142. }
  143. MaterialVariationGUI variationGUI = new MaterialVariationGUI(material, materialLayout);
  144. materialVariations.Add(variationGUI);
  145. MaterialParamGUI[] matParams = MaterialInspector.CreateMaterialGUI(material,
  146. "materialParams[" + i + "]", null, materialLayout);
  147. materialParams.Add(matParams);
  148. }
  149. }
  150. }
  151. }
  152. /** @} */
  153. }