You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
17 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace TMPro.EditorUtilities
  4. {
  5. /// <summary>Base class for TextMesh Pro shader GUIs.</summary>
  6. public abstract class TMP_BaseShaderGUI : ShaderGUI
  7. {
  8. /// <summary>Representation of a #pragma shader_feature.</summary>
  9. /// <description>It is assumed that the first feature option is for no keyword (underscores).</description>
  10. protected class ShaderFeature
  11. {
  12. public string undoLabel;
  13. public GUIContent label;
  14. /// <summary>The keyword labels, for display. Include the no-keyword as the first option.</summary>
  15. public GUIContent[] keywordLabels;
  16. /// <summary>The shader keywords. Exclude the no-keyword option.</summary>
  17. public string[] keywords;
  18. int m_State;
  19. public bool Active
  20. {
  21. get { return m_State >= 0; }
  22. }
  23. public int State
  24. {
  25. get { return m_State; }
  26. }
  27. public void ReadState(Material material)
  28. {
  29. for (int i = 0; i < keywords.Length; i++)
  30. {
  31. if (material.IsKeywordEnabled(keywords[i]))
  32. {
  33. m_State = i;
  34. return;
  35. }
  36. }
  37. m_State = -1;
  38. }
  39. public void SetActive(bool active, Material material)
  40. {
  41. m_State = active ? 0 : -1;
  42. SetStateKeywords(material);
  43. }
  44. public void DoPopup(MaterialEditor editor, Material material)
  45. {
  46. EditorGUI.BeginChangeCheck();
  47. int selection = EditorGUILayout.Popup(label, m_State + 1, keywordLabels);
  48. if (EditorGUI.EndChangeCheck())
  49. {
  50. m_State = selection - 1;
  51. editor.RegisterPropertyChangeUndo(undoLabel);
  52. SetStateKeywords(material);
  53. }
  54. }
  55. void SetStateKeywords(Material material)
  56. {
  57. for (int i = 0; i < keywords.Length; i++)
  58. {
  59. if (i == m_State)
  60. {
  61. material.EnableKeyword(keywords[i]);
  62. }
  63. else
  64. {
  65. material.DisableKeyword(keywords[i]);
  66. }
  67. }
  68. }
  69. }
  70. static GUIContent s_TempLabel = new GUIContent();
  71. protected static bool s_DebugExtended;
  72. static int s_UndoRedoCount, s_LastSeenUndoRedoCount;
  73. static float[][] s_TempFloats =
  74. {
  75. null, new float[1], new float[2], new float[3], new float[4]
  76. };
  77. protected static GUIContent[] s_XywhVectorLabels =
  78. {
  79. new GUIContent("X"),
  80. new GUIContent("Y"),
  81. new GUIContent("W", "Width"),
  82. new GUIContent("H", "Height")
  83. };
  84. protected static GUIContent[] s_LbrtVectorLabels =
  85. {
  86. new GUIContent("L", "Left"),
  87. new GUIContent("B", "Bottom"),
  88. new GUIContent("R", "Right"),
  89. new GUIContent("T", "Top")
  90. };
  91. static TMP_BaseShaderGUI()
  92. {
  93. // Keep track of how many undo/redo events happened.
  94. Undo.undoRedoPerformed += () => s_UndoRedoCount += 1;
  95. }
  96. bool m_IsNewGUI = true;
  97. float m_DragAndDropMinY;
  98. protected MaterialEditor m_Editor;
  99. protected Material m_Material;
  100. protected MaterialProperty[] m_Properties;
  101. void PrepareGUI()
  102. {
  103. m_IsNewGUI = false;
  104. ShaderUtilities.GetShaderPropertyIDs();
  105. // New GUI just got constructed. This happens in response to a selection,
  106. // but also after undo/redo events.
  107. if (s_LastSeenUndoRedoCount != s_UndoRedoCount)
  108. {
  109. // There's been at least one undo/redo since the last time this GUI got constructed.
  110. // Maybe the undo/redo was for this material? Assume that is was.
  111. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, m_Material as Material);
  112. }
  113. s_LastSeenUndoRedoCount = s_UndoRedoCount;
  114. }
  115. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
  116. {
  117. m_Editor = materialEditor;
  118. m_Material = materialEditor.target as Material;
  119. this.m_Properties = properties;
  120. if (m_IsNewGUI)
  121. {
  122. PrepareGUI();
  123. }
  124. DoDragAndDropBegin();
  125. EditorGUI.BeginChangeCheck();
  126. DoGUI();
  127. if (EditorGUI.EndChangeCheck())
  128. {
  129. TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, m_Material);
  130. }
  131. DoDragAndDropEnd();
  132. }
  133. /// <summary>Override this method to create the specific shader GUI.</summary>
  134. protected abstract void DoGUI();
  135. protected bool BeginPanel(string panel, bool expanded)
  136. {
  137. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  138. Rect r = EditorGUI.IndentedRect(GUILayoutUtility.GetRect(20, 18));
  139. r.x += 20;
  140. r.width += 6;
  141. bool enabled = GUI.enabled;
  142. GUI.enabled = true;
  143. expanded = TMP_EditorUtility.EditorToggle(r, expanded, new GUIContent(panel), TMP_UIStyleManager.panelTitle);
  144. GUI.enabled = enabled;
  145. EditorGUI.indentLevel += 1;
  146. EditorGUI.BeginDisabledGroup(false);
  147. return expanded;
  148. }
  149. protected bool BeginPanel(string panel, ShaderFeature feature, bool expanded, bool readState = true)
  150. {
  151. if (readState)
  152. {
  153. feature.ReadState(m_Material);
  154. }
  155. EditorGUI.BeginChangeCheck();
  156. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  157. GUILayout.BeginHorizontal();
  158. Rect r = EditorGUI.IndentedRect(GUILayoutUtility.GetRect(20, 20, GUILayout.Width(20f)));
  159. bool active = EditorGUI.Toggle(r, feature.Active);
  160. if (EditorGUI.EndChangeCheck())
  161. {
  162. m_Editor.RegisterPropertyChangeUndo(feature.undoLabel);
  163. feature.SetActive(active, m_Material);
  164. }
  165. r = EditorGUI.IndentedRect(GUILayoutUtility.GetRect(20, 18));
  166. r.width += 6;
  167. bool enabled = GUI.enabled;
  168. GUI.enabled = true;
  169. expanded = TMP_EditorUtility.EditorToggle(r, expanded, new GUIContent(panel), TMP_UIStyleManager.panelTitle);
  170. GUI.enabled = enabled;
  171. GUILayout.EndHorizontal();
  172. EditorGUI.indentLevel += 1;
  173. EditorGUI.BeginDisabledGroup(!active);
  174. return expanded;
  175. }
  176. public void EndPanel()
  177. {
  178. EditorGUI.EndDisabledGroup();
  179. EditorGUI.indentLevel -= 1;
  180. EditorGUILayout.EndVertical();
  181. }
  182. MaterialProperty BeginProperty(string name)
  183. {
  184. MaterialProperty property = FindProperty(name, m_Properties);
  185. EditorGUI.BeginChangeCheck();
  186. EditorGUI.showMixedValue = property.hasMixedValue;
  187. m_Editor.BeginAnimatedCheck(Rect.zero, property);
  188. return property;
  189. }
  190. bool EndProperty()
  191. {
  192. m_Editor.EndAnimatedCheck();
  193. EditorGUI.showMixedValue = false;
  194. return EditorGUI.EndChangeCheck();
  195. }
  196. protected void DoPopup(string name, string label, GUIContent[] options)
  197. {
  198. MaterialProperty property = BeginProperty(name);
  199. s_TempLabel.text = label;
  200. int index = EditorGUILayout.Popup(s_TempLabel, (int)property.floatValue, options);
  201. if (EndProperty())
  202. {
  203. property.floatValue = index;
  204. }
  205. }
  206. protected void DoCubeMap(string name, string label)
  207. {
  208. DoTexture(name, label, typeof(Cubemap));
  209. }
  210. protected void DoTexture2D(string name, string label, bool withTilingOffset = false, string[] speedNames = null)
  211. {
  212. DoTexture(name, label, typeof(Texture2D), withTilingOffset, speedNames);
  213. }
  214. void DoTexture(string name, string label, System.Type type, bool withTilingOffset = false, string[] speedNames = null)
  215. {
  216. MaterialProperty property = BeginProperty(name);
  217. Rect rect = EditorGUILayout.GetControlRect(true, 60f);
  218. float totalWidth = rect.width;
  219. rect.width = EditorGUIUtility.labelWidth + 60f;
  220. s_TempLabel.text = label;
  221. Object tex = EditorGUI.ObjectField(rect, s_TempLabel, property.textureValue, type, false);
  222. if (EndProperty())
  223. {
  224. property.textureValue = tex as Texture;
  225. }
  226. rect.x += rect.width + 4f;
  227. rect.width = totalWidth - rect.width - 4f;
  228. rect.height = EditorGUIUtility.singleLineHeight;
  229. if (withTilingOffset)
  230. {
  231. DoTilingOffset(rect, property);
  232. rect.y += (rect.height + 2f) * 2f;
  233. }
  234. if (speedNames != null)
  235. {
  236. DoUVSpeed(rect, speedNames);
  237. }
  238. }
  239. void DoTilingOffset(Rect rect, MaterialProperty property)
  240. {
  241. float labelWidth = EditorGUIUtility.labelWidth;
  242. int indentLevel = EditorGUI.indentLevel;
  243. EditorGUI.indentLevel = 0;
  244. EditorGUIUtility.labelWidth = Mathf.Min(40f, rect.width * 0.20f);
  245. Vector4 vector = property.textureScaleAndOffset;
  246. bool changed = false;
  247. float[] values = s_TempFloats[2];
  248. s_TempLabel.text = "Tiling";
  249. Rect vectorRect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  250. values[0] = vector.x;
  251. values[1] = vector.y;
  252. EditorGUI.BeginChangeCheck();
  253. EditorGUI.MultiFloatField(vectorRect, s_XywhVectorLabels, values);
  254. if (EndProperty())
  255. {
  256. vector.x = values[0];
  257. vector.y = values[1];
  258. changed = true;
  259. }
  260. rect.y += rect.height + 2f;
  261. s_TempLabel.text = "Offset";
  262. vectorRect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  263. values[0] = vector.z;
  264. values[1] = vector.w;
  265. EditorGUI.BeginChangeCheck();
  266. EditorGUI.MultiFloatField(vectorRect, s_XywhVectorLabels, values);
  267. if (EndProperty())
  268. {
  269. vector.z = values[0];
  270. vector.w = values[1];
  271. changed = true;
  272. }
  273. if (changed)
  274. {
  275. property.textureScaleAndOffset = vector;
  276. }
  277. EditorGUIUtility.labelWidth = labelWidth;
  278. EditorGUI.indentLevel = indentLevel;
  279. }
  280. protected void DoUVSpeed(Rect rect, string[] names)
  281. {
  282. float labelWidth = EditorGUIUtility.labelWidth;
  283. int indentLevel = EditorGUI.indentLevel;
  284. EditorGUI.indentLevel = 0;
  285. EditorGUIUtility.labelWidth = Mathf.Min(40f, rect.width * 0.20f);
  286. s_TempLabel.text = "Speed";
  287. rect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  288. EditorGUIUtility.labelWidth = 13f;
  289. rect.width = rect.width * 0.5f - 1f;
  290. DoFloat(rect, names[0], "X");
  291. rect.x += rect.width + 2f;
  292. DoFloat(rect, names[1], "Y");
  293. EditorGUIUtility.labelWidth = labelWidth;
  294. EditorGUI.indentLevel = indentLevel;
  295. }
  296. protected void DoToggle(string name, string label)
  297. {
  298. MaterialProperty property = BeginProperty(name);
  299. s_TempLabel.text = label;
  300. bool value = EditorGUILayout.Toggle(s_TempLabel, property.floatValue == 1f);
  301. if (EndProperty())
  302. {
  303. property.floatValue = value ? 1f : 0f;
  304. }
  305. }
  306. protected void DoFloat(string name, string label)
  307. {
  308. MaterialProperty property = BeginProperty(name);
  309. Rect rect = EditorGUILayout.GetControlRect();
  310. rect.width = EditorGUIUtility.labelWidth + 55f;
  311. s_TempLabel.text = label;
  312. float value = EditorGUI.FloatField(rect, s_TempLabel, property.floatValue);
  313. if (EndProperty())
  314. {
  315. property.floatValue = value;
  316. }
  317. }
  318. protected void DoColor(string name, string label)
  319. {
  320. MaterialProperty property = BeginProperty(name);
  321. s_TempLabel.text = label;
  322. Color value = EditorGUI.ColorField(EditorGUILayout.GetControlRect(), s_TempLabel, property.colorValue);
  323. if (EndProperty())
  324. {
  325. property.colorValue = value;
  326. }
  327. }
  328. void DoFloat(Rect rect, string name, string label)
  329. {
  330. MaterialProperty property = BeginProperty(name);
  331. s_TempLabel.text = label;
  332. float value = EditorGUI.FloatField(rect, s_TempLabel, property.floatValue);
  333. if (EndProperty())
  334. {
  335. property.floatValue = value;
  336. }
  337. }
  338. protected void DoSlider(string name, string label)
  339. {
  340. MaterialProperty property = BeginProperty(name);
  341. Vector2 range = property.rangeLimits;
  342. s_TempLabel.text = label;
  343. float value = EditorGUI.Slider(
  344. EditorGUILayout.GetControlRect(), s_TempLabel, property.floatValue, range.x, range.y
  345. );
  346. if (EndProperty())
  347. {
  348. property.floatValue = value;
  349. }
  350. }
  351. protected void DoVector3(string name, string label)
  352. {
  353. MaterialProperty property = BeginProperty(name);
  354. s_TempLabel.text = label;
  355. Vector4 value = EditorGUILayout.Vector3Field(s_TempLabel, property.vectorValue);
  356. if (EndProperty())
  357. {
  358. property.vectorValue = value;
  359. }
  360. }
  361. protected void DoVector(string name, string label, GUIContent[] subLabels)
  362. {
  363. MaterialProperty property = BeginProperty(name);
  364. Rect rect = EditorGUILayout.GetControlRect();
  365. s_TempLabel.text = label;
  366. rect = EditorGUI.PrefixLabel(rect, s_TempLabel);
  367. Vector4 vector = property.vectorValue;
  368. float[] values = s_TempFloats[subLabels.Length];
  369. for (int i = 0; i < subLabels.Length; i++)
  370. {
  371. values[i] = vector[i];
  372. }
  373. EditorGUI.MultiFloatField(rect, subLabels, values);
  374. if (EndProperty())
  375. {
  376. for (int i = 0; i < subLabels.Length; i++)
  377. {
  378. vector[i] = values[i];
  379. }
  380. property.vectorValue = vector;
  381. }
  382. }
  383. void DoDragAndDropBegin()
  384. {
  385. m_DragAndDropMinY = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true)).y;
  386. }
  387. void DoDragAndDropEnd()
  388. {
  389. Rect rect = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true));
  390. Event evt = Event.current;
  391. if (evt.type == EventType.DragUpdated)
  392. {
  393. DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
  394. evt.Use();
  395. }
  396. else if (
  397. evt.type == EventType.DragPerform &&
  398. Rect.MinMaxRect(rect.xMin, m_DragAndDropMinY, rect.xMax, rect.yMax).Contains(evt.mousePosition)
  399. )
  400. {
  401. DragAndDrop.AcceptDrag();
  402. evt.Use();
  403. Material droppedMaterial = DragAndDrop.objectReferences[0] as Material;
  404. if (droppedMaterial && droppedMaterial != m_Material)
  405. {
  406. PerformDrop(droppedMaterial);
  407. }
  408. }
  409. }
  410. void PerformDrop(Material droppedMaterial)
  411. {
  412. Texture droppedTex = droppedMaterial.GetTexture(ShaderUtilities.ID_MainTex);
  413. if (!droppedTex)
  414. {
  415. return;
  416. }
  417. Texture currentTex = m_Material.GetTexture(ShaderUtilities.ID_MainTex);
  418. TMP_FontAsset requiredFontAsset = null;
  419. if (droppedTex != currentTex)
  420. {
  421. requiredFontAsset = TMP_EditorUtility.FindMatchingFontAsset(droppedMaterial);
  422. if (!requiredFontAsset)
  423. {
  424. return;
  425. }
  426. }
  427. foreach (GameObject o in Selection.gameObjects)
  428. {
  429. if (requiredFontAsset)
  430. {
  431. TMP_Text textComponent = o.GetComponent<TMP_Text>();
  432. if (textComponent)
  433. {
  434. Undo.RecordObject(textComponent, "Font Asset Change");
  435. textComponent.font = requiredFontAsset;
  436. }
  437. }
  438. TMPro_EventManager.ON_DRAG_AND_DROP_MATERIAL_CHANGED(o, m_Material, droppedMaterial);
  439. EditorUtility.SetDirty(o);
  440. }
  441. }
  442. }
  443. }