TermsPopup_Drawer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace I2.Loc
  6. {
  7. [CustomPropertyDrawer (typeof (TermsPopup))]
  8. public class TermsPopup_Drawer : PropertyDrawer
  9. {
  10. GUIContent[] mTerms_Context;
  11. int nFramesLeftBeforeUpdate;
  12. string mPrevFilter;
  13. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  14. {
  15. var filter = ((TermsPopup)attribute).Filter;
  16. ShowGUICached(position, property, label, null, filter, ref mTerms_Context, ref nFramesLeftBeforeUpdate, ref mPrevFilter);
  17. }
  18. public static bool ShowGUI(Rect position, SerializedProperty property, GUIContent label, LanguageSourceData source, string filter = "")
  19. {
  20. GUIContent[] terms=null;
  21. int framesLeftBeforeUpdate=0;
  22. string prevFilter = null;
  23. return ShowGUICached(position, property, label, source, filter, ref terms, ref framesLeftBeforeUpdate, ref prevFilter);
  24. }
  25. public static bool ShowGUICached(Rect position, SerializedProperty property, GUIContent label, LanguageSourceData source, string filter, ref GUIContent[] terms_Contexts, ref int framesBeforeUpdating, ref string prevFilter)
  26. {
  27. UpdateTermsCache(source, filter, ref terms_Contexts, ref framesBeforeUpdating, ref prevFilter);
  28. label = EditorGUI.BeginProperty(position, label, property);
  29. EditorGUI.BeginChangeCheck ();
  30. var index = property.stringValue == "-" || property.stringValue == "" ? terms_Contexts.Length - 1 :
  31. property.stringValue == " " ? terms_Contexts.Length - 2 :
  32. GetTermIndex(terms_Contexts, property.stringValue);
  33. var newIndex = EditorGUI.Popup(position, label, index, terms_Contexts);
  34. if (EditorGUI.EndChangeCheck())
  35. {
  36. property.stringValue = newIndex < 0 || newIndex == terms_Contexts.Length - 1 ? string.Empty : terms_Contexts[newIndex].text;
  37. if (newIndex == terms_Contexts.Length - 1)
  38. property.stringValue = "-";
  39. else
  40. if (newIndex < 0 || newIndex == terms_Contexts.Length - 2)
  41. property.stringValue = string.Empty;
  42. else
  43. property.stringValue = terms_Contexts[newIndex].text;
  44. EditorGUI.EndProperty();
  45. return true;
  46. }
  47. EditorGUI.EndProperty();
  48. return false;
  49. }
  50. static int GetTermIndex(GUIContent[] terms_Contexts, string term )
  51. {
  52. for (int i = 0; i < terms_Contexts.Length; ++i)
  53. if (terms_Contexts[i].text == term)
  54. return i;
  55. return -1;
  56. }
  57. static void UpdateTermsCache(LanguageSourceData source, string filter, ref GUIContent[] terms_Contexts, ref int framesBeforeUpdating, ref string prevFilter)
  58. {
  59. framesBeforeUpdating--;
  60. if (terms_Contexts!=null && framesBeforeUpdating>0 && filter==prevFilter)
  61. {
  62. return;
  63. }
  64. framesBeforeUpdating = 60;
  65. prevFilter = filter;
  66. var Terms = source == null ? LocalizationManager.GetTermsList() : source.GetTermsList();
  67. if (string.IsNullOrEmpty(filter) == false)
  68. {
  69. Terms = Filter(Terms, filter);
  70. }
  71. Terms.Sort(StringComparer.OrdinalIgnoreCase);
  72. Terms.Add("");
  73. Terms.Add("<inferred from text>");
  74. Terms.Add("<none>");
  75. terms_Contexts = DisplayOptions(Terms);
  76. }
  77. private static List<string> Filter(List<string> terms, string filter)
  78. {
  79. var filtered = new List<string>();
  80. for (var i = 0; i < terms.Count; i++)
  81. {
  82. var term = terms[i];
  83. if (term.Contains(filter))
  84. {
  85. filtered.Add(term);
  86. }
  87. }
  88. return filtered;
  89. }
  90. private static GUIContent[] DisplayOptions(IList<string> terms)
  91. {
  92. var options = new GUIContent[terms.Count];
  93. for (var i = 0; i < terms.Count; i++)
  94. {
  95. options[i] = new GUIContent(terms[i]);
  96. }
  97. return options;
  98. }
  99. }
  100. [CustomPropertyDrawer(typeof(LocalizedString))]
  101. public class LocalizedStringDrawer : PropertyDrawer
  102. {
  103. GUIContent[] mTerms_Context;
  104. int nFramesLeftBeforeUpdate;
  105. string mPrevFilter;
  106. public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
  107. {
  108. var termRect = rect; termRect.xMax -= 50;
  109. var termProp = property.FindPropertyRelative("mTerm");
  110. TermsPopup_Drawer.ShowGUICached(termRect, termProp, label, null, "", ref mTerms_Context, ref nFramesLeftBeforeUpdate, ref mPrevFilter);
  111. var maskRect = rect; maskRect.xMin = maskRect.xMax - 30;
  112. var termIgnoreRTL = property.FindPropertyRelative("mRTL_IgnoreArabicFix");
  113. var termConvertNumbers = property.FindPropertyRelative("mRTL_ConvertNumbers");
  114. var termDontLocalizeParams = property.FindPropertyRelative("m_DontLocalizeParameters");
  115. int mask = (termIgnoreRTL.boolValue ? 0 : 1) +
  116. (termConvertNumbers.boolValue ? 0 : 2) +
  117. (termDontLocalizeParams.boolValue ? 0 : 4);
  118. int newMask = EditorGUI.MaskField(maskRect, mask, new[] { "Arabic Fix", "Ignore Numbers in RTL", "Localize Parameters" });
  119. if (newMask != mask)
  120. {
  121. termIgnoreRTL.boolValue = (newMask & 1) == 0;
  122. termConvertNumbers.boolValue = (newMask & 2) == 0;
  123. termDontLocalizeParams.boolValue = (newMask & 4) == 0;
  124. }
  125. var showRect = rect; showRect.xMin = termRect.xMax; showRect.xMax=maskRect.xMin;
  126. bool enabled = GUI.enabled;
  127. GUI.enabled = enabled & (!string.IsNullOrEmpty (termProp.stringValue) && termProp.stringValue!="-");
  128. if (GUI.Button (showRect, "?"))
  129. {
  130. var source = LocalizationManager.GetSourceContaining(termProp.stringValue);
  131. LocalizationEditor.mKeyToExplore = termProp.stringValue;
  132. Selection.activeObject = source.ownerObject;
  133. }
  134. GUI.enabled = enabled;
  135. }
  136. }
  137. }