LocalizeInspector.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. //#define UGUI
  2. //#define NGUI
  3. //#define DFGUI
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using UnityEditor;
  9. using UnityEngine;
  10. using Object = UnityEngine.Object;
  11. namespace I2.Loc
  12. {
  13. [CustomEditor(typeof(Localize))]
  14. [CanEditMultipleObjects]
  15. public class LocalizeInspector : Editor
  16. {
  17. #region Variables
  18. Localize mLocalize;
  19. SerializedProperty mProp_mTerm, mProp_mTermSecondary,
  20. mProp_TranslatedObjects, mProp_LocalizeOnAwake, mProp_AlwaysForceLocalize, mProp_AllowLocalizedParameters, mProp_AllowParameters,
  21. mProp_IgnoreRTL, mProp_MaxCharactersInRTL, mProp_CorrectAlignmentForRTL, mProp_IgnoreNumbersInRTL, mProp_TermSuffix, mProp_TermPrefix, mProp_SeparateWords,
  22. mProp_CallbackEvent;
  23. bool mAllowEditKeyName;
  24. string mNewKeyName = "";
  25. string[] mTermsArray;
  26. public static string HelpURL_forum = "http://goo.gl/Uiyu8C";//http://www.inter-illusion.com/forum/i2-localization";
  27. public static string HelpURL_Documentation = "http://www.inter-illusion.com/assets/I2LocalizationManual/I2LocalizationManual.html";
  28. public static string HelpURL_Tutorials = "http://inter-illusion.com/tools/i2-localization";
  29. public static string HelpURL_ReleaseNotes = "http://inter-illusion.com/forum/i2-localization/26-release-notes";
  30. public static string HelpURL_AssetStore = "https://www.assetstore.unity3d.com/#!/content/14884";
  31. public static LocalizeInspector mLocalizeInspector;
  32. #endregion
  33. #region Inspector
  34. void OnEnable()
  35. {
  36. mLocalize = (Localize)target;
  37. mLocalizeInspector = this;
  38. LocalizationEditor.mCurrentInspector = this;
  39. mProp_mTerm = serializedObject.FindProperty("mTerm");
  40. mProp_mTermSecondary = serializedObject.FindProperty("mTermSecondary");
  41. mProp_TranslatedObjects = serializedObject.FindProperty("TranslatedObjects");
  42. mProp_IgnoreRTL = serializedObject.FindProperty("IgnoreRTL");
  43. mProp_SeparateWords = serializedObject.FindProperty("AddSpacesToJoinedLanguages");
  44. mProp_MaxCharactersInRTL = serializedObject.FindProperty ("MaxCharactersInRTL");
  45. mProp_IgnoreNumbersInRTL = serializedObject.FindProperty("IgnoreNumbersInRTL");
  46. mProp_CorrectAlignmentForRTL = serializedObject.FindProperty ("CorrectAlignmentForRTL");
  47. mProp_LocalizeOnAwake = serializedObject.FindProperty("LocalizeOnAwake");
  48. mProp_AlwaysForceLocalize = serializedObject.FindProperty("AlwaysForceLocalize");
  49. mProp_TermSuffix = serializedObject.FindProperty("TermSuffix");
  50. mProp_TermPrefix = serializedObject.FindProperty("TermPrefix");
  51. mProp_CallbackEvent = serializedObject.FindProperty("LocalizeEvent");
  52. mProp_AllowLocalizedParameters = serializedObject.FindProperty("AllowLocalizedParameters");
  53. mProp_AllowParameters = serializedObject.FindProperty("AllowParameters");
  54. if (LocalizationManager.Sources.Count==0)
  55. LocalizationManager.UpdateSources();
  56. //LocalizationEditor.ParseTerms (true);
  57. //mGUI_ShowReferences = (mLocalize.TranslatedObjects!=null && mLocalize.TranslatedObjects.Length>0);
  58. //mGUI_ShowCallback = (mLocalize.LocalizeCallBack.Target!=null);
  59. //mGUI_ShowTems = true;
  60. LocalizationEditor.mKeysDesc_AllowEdit = false;
  61. GUI_SelectedTerm = 0;
  62. mNewKeyName = mLocalize.Term;
  63. if (mLocalize.Source != null)
  64. LocalizationEditor.mLanguageSource = mLocalize.Source.SourceData;
  65. else
  66. {
  67. if (LocalizationManager.Sources.Count==0)
  68. LocalizationManager.UpdateSources();
  69. LocalizationEditor.mLanguageSource = LocalizationManager.GetSourceContaining( mLocalize.Term );
  70. }
  71. //UpgradeManager.EnablePlugins();
  72. LocalizationEditor.ApplyInferredTerm (mLocalize);
  73. RemoveUnusedReferences(mLocalize);
  74. }
  75. void OnDisable()
  76. {
  77. mLocalizeInspector = null;
  78. if (LocalizationEditor.mCurrentInspector == this) LocalizationEditor.mCurrentInspector = null;
  79. if (mLocalize == null)
  80. return;
  81. //#if TextMeshPro
  82. //string previous = null;
  83. //if (!Application.isPlaying && !string.IsNullOrEmpty(mLocalize.TMP_previewLanguage))
  84. //{
  85. // previous = LocalizationManager.CurrentLanguage;
  86. // LocalizationManager.PreviewLanguage( mLocalize.TMP_previewLanguage );
  87. //}
  88. //#endif
  89. //mLocalize.OnLocalize();
  90. // Revert the preview language
  91. // except when in TMPro and not changing to another GameObject (TMPro has a bug where any change causes the inspector to Disable and Enable)
  92. if (!mLocalize.mLocalizeTargetName.Contains("LocalizeTarget_TextMeshPro") || Selection.activeGameObject==null || !Selection.gameObjects.Contains(mLocalize.gameObject))
  93. {
  94. LocalizationManager.LocalizeAll();
  95. }
  96. //#if TextMeshPro
  97. //if (!string.IsNullOrEmpty(previous))
  98. //{
  99. // LocalizationManager.PreviewLanguage(previous);
  100. // mLocalize.TMP_previewLanguage = null;
  101. //}
  102. //#endif
  103. RemoveUnusedReferences(mLocalize);
  104. }
  105. #endregion
  106. #region GUI
  107. public override void OnInspectorGUI()
  108. {
  109. Undo.RecordObject(target, "Localize");
  110. //GUI.backgroundColor = Color.Lerp (Color.black, Color.gray, 1);
  111. //GUILayout.BeginVertical(GUIStyle_Background, GUILayout.Height(1));
  112. //GUI.backgroundColor = Color.white;
  113. if (GUILayout.Button("Localize", GUIStyle_Header))
  114. {
  115. //Application.OpenURL(HelpURL_Documentation);
  116. }
  117. GUILayout.Space(-10);
  118. LocalizationManager.UpdateSources();
  119. if (LocalizationManager.Sources.Count==0)
  120. {
  121. EditorGUILayout.HelpBox("Unable to find a Language Source.", MessageType.Warning);
  122. }
  123. else
  124. {
  125. GUILayout.Space(10);
  126. OnGUI_Target ();
  127. GUILayout.Space(10);
  128. OnGUI_Terms();
  129. //if (mGUI_ShowTems || mGUI_ShowReferences) GUILayout.Space(5);
  130. OnGUI_References();
  131. if (mLocalize.mGUI_ShowReferences || mLocalize.mGUI_ShowCallback) GUILayout.Space(10);
  132. //Localize loc = target as Localize;
  133. //--[ Localize Callback ]----------------------
  134. EditorGUILayout.PropertyField(mProp_CallbackEvent, new GUIContent("On Localize Callback"));
  135. //string HeaderTitle = "On Localize Call:";
  136. //if (!mLocalize.mGUI_ShowCallback && loc.LocalizeCallBack.Target!=null && !string.IsNullOrEmpty(loc.LocalizeCallBack.MethodName))
  137. // HeaderTitle = string.Concat(HeaderTitle, " <b>",loc.LocalizeCallBack.Target.name, ".</b><i>", loc.LocalizeCallBack.MethodName, "</i>");
  138. //mLocalize.mGUI_ShowCallback = GUITools.DrawHeader(HeaderTitle, mLocalize.mGUI_ShowCallback);
  139. //if (mLocalize.mGUI_ShowCallback)
  140. //{
  141. // GUITools.BeginContents();
  142. // DrawEventCallBack( loc.LocalizeCallBack, loc );
  143. // GUITools.EndContents();
  144. //}
  145. }
  146. OnGUI_Source ();
  147. GUILayout.Space (10);
  148. GUITools.OnGUI_Footer("I2 Localization", LocalizationManager.GetVersion(), HelpURL_forum, HelpURL_Documentation, HelpURL_AssetStore);
  149. //GUILayout.EndVertical();
  150. serializedObject.ApplyModifiedProperties();
  151. if (Event.current.type == EventType.Repaint)
  152. {
  153. LocalizationEditor.mTestAction = LocalizationEditor.eTest_ActionType.None;
  154. LocalizationEditor.mTestActionArg = null;
  155. LocalizationEditor.mTestActionArg2 = null;
  156. }
  157. }
  158. #endregion
  159. #region References
  160. void OnGUI_References()
  161. {
  162. if (mLocalize.mGUI_ShowReferences = GUITools.DrawHeader ("References", mLocalize.mGUI_ShowReferences))
  163. {
  164. GUITools.BeginContents();
  165. bool canTest = Event.current.type == EventType.Repaint;
  166. var testAddObj = canTest && LocalizationEditor.mTestAction == LocalizationEditor.eTest_ActionType.Button_Assets_Add ? (Object)LocalizationEditor.mTestActionArg : null;
  167. var testReplaceIndx = canTest && LocalizationEditor.mTestAction == LocalizationEditor.eTest_ActionType.Button_Assets_Replace ? (int)LocalizationEditor.mTestActionArg : -1;
  168. var testReplaceObj = canTest && LocalizationEditor.mTestAction == LocalizationEditor.eTest_ActionType.Button_Assets_Replace ? (Object)LocalizationEditor.mTestActionArg2 : null;
  169. var testDeleteIndx = canTest && LocalizationEditor.mTestAction == LocalizationEditor.eTest_ActionType.Button_Assets_Delete ? (int)LocalizationEditor.mTestActionArg : -1;
  170. bool changed = GUITools.DrawObjectsArray( mProp_TranslatedObjects, false, false, true, testAddObj, testReplaceObj, testReplaceIndx, testDeleteIndx);
  171. if (changed)
  172. {
  173. serializedObject.ApplyModifiedProperties();
  174. foreach (var obj in serializedObject.targetObjects)
  175. (obj as Localize).UpdateAssetDictionary();
  176. }
  177. GUITools.EndContents();
  178. }
  179. }
  180. void RemoveUnusedReferences(Localize cmp)
  181. {
  182. cmp.TranslatedObjects.RemoveAll(x => !IsUsingReference(LocalizationManager.GetTermData(cmp.Term), x) && !IsUsingReference(LocalizationManager.GetTermData(cmp.SecondaryTerm), x));
  183. if (cmp.TranslatedObjects.Count != cmp.mAssetDictionary.Count)
  184. cmp.UpdateAssetDictionary();
  185. }
  186. bool IsUsingReference(TermData termData, Object obj )
  187. {
  188. if (obj == null || termData==null) return false;
  189. string objName = obj.name;
  190. foreach (string translation in termData.Languages)
  191. {
  192. if (translation != null && translation.Contains(objName))
  193. return true;
  194. }
  195. return false;
  196. }
  197. #endregion
  198. #region Terms
  199. int GUI_SelectedTerm;
  200. void OnGUI_Terms()
  201. {
  202. if ((mLocalize.mGUI_ShowTems=GUITools.DrawHeader ("Terms", mLocalize.mGUI_ShowTems)))
  203. {
  204. //--[ tabs: Main and Secondary Terms ]----------------
  205. int oldTab = GUI_SelectedTerm;
  206. if (mLocalize.mLocalizeTarget!=null && mLocalize.mLocalizeTarget.CanUseSecondaryTerm())
  207. {
  208. GUI_SelectedTerm = GUITools.DrawTabs (GUI_SelectedTerm, new[]{"Main", "Secondary"});
  209. }
  210. else
  211. {
  212. GUI_SelectedTerm = 0;
  213. GUITools.DrawTabs (GUI_SelectedTerm, new[]{"Main", ""});
  214. }
  215. GUITools.BeginContents();
  216. TermData termData = null;
  217. if (GUI_SelectedTerm==0) termData = OnGUI_PrimaryTerm( oldTab!=GUI_SelectedTerm );
  218. else termData = OnGUI_SecondaryTerm(oldTab!=GUI_SelectedTerm);
  219. GUITools.EndContents();
  220. //--[ Modifier ]-------------
  221. if (mLocalize.Term != "-" && termData!=null && termData.TermType==eTermType.Text)
  222. {
  223. EditorGUI.BeginChangeCheck();
  224. GUILayout.BeginHorizontal();
  225. GUILayout.Label("Prefix:");
  226. EditorGUILayout.PropertyField(mProp_TermPrefix, GUITools.EmptyContent);
  227. GUILayout.Label("Suffix:");
  228. EditorGUILayout.PropertyField(mProp_TermSuffix, GUITools.EmptyContent);
  229. GUILayout.EndHorizontal();
  230. if (EditorGUI.EndChangeCheck())
  231. {
  232. EditorApplication.delayCall += () =>
  233. {
  234. if (targets != null)
  235. {
  236. foreach (var t in targets)
  237. if (t as Localize != null)
  238. (t as Localize).OnLocalize(true);
  239. }
  240. };
  241. }
  242. EditorGUI.BeginChangeCheck();
  243. int val = EditorGUILayout.Popup("Modifier", GUI_SelectedTerm == 0 ? (int)mLocalize.PrimaryTermModifier : (int)mLocalize.SecondaryTermModifier, Enum.GetNames(typeof(Localize.TermModification)));
  244. if (EditorGUI.EndChangeCheck())
  245. {
  246. serializedObject.FindProperty(GUI_SelectedTerm == 0 ? "PrimaryTermModifier" : "SecondaryTermModifier").enumValueIndex = val;
  247. GUI.changed = false;
  248. }
  249. }
  250. OnGUI_Options();
  251. //--[ OnAwake vs OnEnable ]-------------
  252. //GUILayout.BeginHorizontal();
  253. //mProp_LocalizeOnAwake.boolValue = GUILayout.Toggle(mProp_LocalizeOnAwake.boolValue, new GUIContent(" Pre-Localize on Awake", "Localizing on Awake could result in a lag when the level is loaded but faster later when objects are enabled. If false, it will Localize OnEnable, so will yield faster level load but could have a lag when screens are enabled"));
  254. //GUILayout.FlexibleSpace();
  255. //if (mLocalize.HasCallback())
  256. //{
  257. // GUI.enabled = false;
  258. // GUILayout.Toggle(true, new GUIContent(" Force Localize", "Enable this when the translations have parameters (e.g. Thew winner is {[WINNER}]) to prevent any optimization that could prevent updating the translation when the object is enabled"));
  259. // GUI.enabled = true;
  260. //}
  261. //else
  262. //{
  263. // mProp_AlwaysForceLocalize.boolValue = GUILayout.Toggle(mProp_AlwaysForceLocalize.boolValue, new GUIContent(" Force Localize", "Enable this when the translations have parameters (e.g. Thew winner is {[WINNER}]) to prevent any optimization that could prevent updating the translation when the object is enabled"));
  264. //}
  265. //GUILayout.EndHorizontal();
  266. //--[ Right To Left ]-------------
  267. if (!mLocalize.IgnoreRTL && mLocalize.Term!="-" && termData != null && termData.TermType == eTermType.Text)
  268. {
  269. GUILayout.BeginVertical("Box");
  270. //GUILayout.BeginHorizontal();
  271. // mProp_IgnoreRTL.boolValue = GUILayout.Toggle(mProp_IgnoreRTL.boolValue, new GUIContent(" Ignore Right To Left", "Arabic and other RTL languages require processing them so they render correctly, this toogle allows ignoring that processing (in case you are doing it manually during a callback)"));
  272. // GUILayout.FlexibleSpace();
  273. // mProp_SeparateWords.boolValue = GUILayout.Toggle(mProp_SeparateWords.boolValue, new GUIContent(" Separate Words", " Some languages (e.g. Chinese, Japanese and Thai) don't add spaces to their words (all characters are placed toguether), enabling this checkbox, will add spaces to all characters to allow wrapping long texts into multiple lines."));
  274. //GUILayout.EndHorizontal();
  275. {
  276. mProp_MaxCharactersInRTL.intValue = EditorGUILayout.IntField( new GUIContent("Max line length", "If the language is Right To Left, long lines will be split at this length and the RTL fix will be applied to each line, this should be set to the maximum number of characters that fit in this text width. 0 disables the per line fix"), mProp_MaxCharactersInRTL.intValue );
  277. GUILayout.BeginHorizontal();
  278. mProp_CorrectAlignmentForRTL.boolValue = GUILayout.Toggle(mProp_CorrectAlignmentForRTL.boolValue, new GUIContent(" Adjust Alignment", "Right-align when Right-To-Left Language, and Left-Align otherwise") );
  279. GUILayout.FlexibleSpace();
  280. mProp_IgnoreNumbersInRTL.boolValue = GUILayout.Toggle(mProp_IgnoreNumbersInRTL.boolValue, new GUIContent(" Ignore Numbers", "Preserve numbers as latin characters instead of converting them"));
  281. GUILayout.EndHorizontal();
  282. }
  283. GUILayout.EndVertical();
  284. }
  285. ////GUILayout.EndHorizontal();
  286. }
  287. }
  288. void OnGUI_Options()
  289. {
  290. int mask = 0;
  291. if (mProp_LocalizeOnAwake.boolValue) mask |= 1 << 0;
  292. if (mProp_AlwaysForceLocalize.boolValue) mask |= 1 << 1;
  293. if (mProp_AllowParameters.boolValue) mask |= 1 << 2;
  294. if (mProp_AllowLocalizedParameters.boolValue) mask |= 1 << 3;
  295. if (mProp_SeparateWords.boolValue) mask |= 1 << 4;
  296. if (mProp_IgnoreRTL.boolValue) mask |= 1 << 5;
  297. EditorGUI.BeginChangeCheck();
  298. mask = EditorGUILayout.MaskField(new GUIContent("Options"), mask, new []{
  299. "Localize On Awake",
  300. "Force Localize",
  301. "Allow Parameters",
  302. "Allow Localized Parameters",
  303. "Separate Words",
  304. "Ignore RTL"
  305. });
  306. if (EditorGUI.EndChangeCheck())
  307. {
  308. mProp_LocalizeOnAwake.boolValue = (mask & (1 << 0))> 0;
  309. mProp_AlwaysForceLocalize.boolValue = (mask & (1 << 1))> 0;
  310. mProp_AllowParameters.boolValue = (mask & (1 << 2))> 0;
  311. mProp_AllowLocalizedParameters.boolValue = (mask & (1 << 3))> 0;
  312. mProp_SeparateWords.boolValue = (mask & (1 << 4))> 0;
  313. mProp_IgnoreRTL.boolValue = (mask & (1 << 5))> 0;
  314. }
  315. }
  316. TermData OnGUI_PrimaryTerm( bool OnOpen )
  317. {
  318. string Key = mLocalize.mTerm;
  319. if (string.IsNullOrEmpty(Key))
  320. {
  321. string SecondaryTerm;
  322. mLocalize.GetFinalTerms( out Key, out SecondaryTerm );
  323. }
  324. if (OnOpen) mNewKeyName = Key;
  325. if ( OnGUI_SelectKey( ref Key, string.IsNullOrEmpty(mLocalize.mTerm)))
  326. mProp_mTerm.stringValue = Key;
  327. return LocalizationEditor.OnGUI_Keys_Languages( Key, mLocalize );
  328. }
  329. TermData OnGUI_SecondaryTerm( bool OnOpen )
  330. {
  331. string Key = mLocalize.mTermSecondary;
  332. if (string.IsNullOrEmpty(Key))
  333. {
  334. string ss;
  335. mLocalize.GetFinalTerms( out ss, out Key );
  336. }
  337. if (OnOpen) mNewKeyName = Key;
  338. if ( OnGUI_SelectKey( ref Key, string.IsNullOrEmpty(mLocalize.mTermSecondary)))
  339. mProp_mTermSecondary.stringValue = Key;
  340. return LocalizationEditor.OnGUI_Keys_Languages( Key, mLocalize, false );
  341. }
  342. bool OnGUI_SelectKey( ref string Term, bool Inherited ) // Inherited==true means that the mTerm is empty and we are using the Label.text instead
  343. {
  344. GUILayout.Space (5);
  345. GUILayout.BeginHorizontal();
  346. GUI.changed = false;
  347. mAllowEditKeyName = GUILayout.Toggle(mAllowEditKeyName, "Term:", EditorStyles.foldout, GUILayout.ExpandWidth(false));
  348. if (GUI.changed && mAllowEditKeyName) {
  349. mNewKeyName = Term;
  350. mTermsArray = null;
  351. }
  352. bool bChanged = false;
  353. if (mTermsArray==null || Term!="-" && Array.IndexOf(mTermsArray, Term)<0)
  354. UpdateTermsList(Term);
  355. if (Inherited)
  356. GUI.contentColor = Color.Lerp (Color.gray, Color.yellow, 0.1f);
  357. int Index = Term=="-" || Term=="" ? mTermsArray.Length-1 : Array.IndexOf( mTermsArray, Term );
  358. GUI.changed = false;
  359. int newIndex = EditorGUILayout.Popup( Index, mTermsArray);
  360. GUI.contentColor = Color.white;
  361. if (/*newIndex != Index && newIndex>=0*/GUI.changed)
  362. {
  363. GUI.changed = false;
  364. if (mLocalize.Source != null && newIndex == mTermsArray.Length - 4) //< show terms from all sources >
  365. {
  366. mLocalize.Source = null;
  367. mTermsArray = null;
  368. }
  369. else
  370. if (newIndex == mTermsArray.Length - 2) //<inferred from text>
  371. mNewKeyName = Term = string.Empty;
  372. else
  373. if (newIndex == mTermsArray.Length - 1) //<none>
  374. mNewKeyName = Term = "-";
  375. else
  376. mNewKeyName = Term = mTermsArray[newIndex];
  377. if (GUI_SelectedTerm==0)
  378. mLocalize.SetTerm (mNewKeyName);
  379. else
  380. mLocalize.SetTerm (null, mNewKeyName);
  381. mAllowEditKeyName = false;
  382. bChanged = true;
  383. }
  384. LanguageSourceData source = LocalizationManager.GetSourceContaining(Term);
  385. TermData termData = source.GetTermData(Term);
  386. if (termData!=null)
  387. {
  388. if (Inherited)
  389. bChanged = true; // if the term its inferred and a matching term its found, then use that
  390. eTermType NewType = (eTermType)EditorGUILayout.EnumPopup(termData.TermType, GUILayout.Width(90));
  391. if (termData.TermType != NewType)
  392. termData.TermType = NewType;
  393. }
  394. GUILayout.EndHorizontal();
  395. if (mAllowEditKeyName)
  396. {
  397. GUILayout.BeginHorizontal(GUILayout.Height (1));
  398. GUILayout.BeginHorizontal(EditorStyles.toolbar);
  399. if(mNewKeyName==null) mNewKeyName = string.Empty;
  400. GUI.changed = false;
  401. mNewKeyName = EditorGUILayout.TextField(mNewKeyName, new GUIStyle("ToolbarSeachTextField"), GUILayout.ExpandWidth(true));
  402. if (GUI.changed)
  403. {
  404. mTermsArray = null; // regenerate this array to apply filtering
  405. GUI.changed = false;
  406. }
  407. if (GUILayout.Button (string.Empty, string.IsNullOrEmpty(mNewKeyName) ? "ToolbarSeachCancelButtonEmpty" : "ToolbarSeachCancelButton", GUILayout.ExpandWidth(false)))
  408. {
  409. mTermsArray = null; // regenerate this array to apply filtering
  410. mNewKeyName = string.Empty;
  411. }
  412. GUILayout.EndHorizontal();
  413. string ValidatedName = mNewKeyName;
  414. LanguageSourceData.ValidateFullTerm( ref ValidatedName );
  415. bool CanUseNewName = source.GetTermData(ValidatedName)==null;
  416. GUI.enabled = !string.IsNullOrEmpty(mNewKeyName) && CanUseNewName;
  417. if (GUILayout.Button ("Create", EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
  418. {
  419. mNewKeyName = ValidatedName;
  420. mTermsArray=null; // this recreates that terms array
  421. LanguageSourceData Source = null;
  422. #if UNITY_EDITOR
  423. if (mLocalize.Source!=null)
  424. Source = mLocalize.Source.SourceData;
  425. #endif
  426. if (Source==null)
  427. Source = LocalizationManager.Sources[0];
  428. Term = mNewKeyName;
  429. var data = Source.AddTerm( mNewKeyName, eTermType.Text, false );
  430. if (data.Languages.Length > 0)
  431. data.Languages[0] = mLocalize.GetMainTargetsText();
  432. Source.Editor_SetDirty();
  433. AssetDatabase.SaveAssets();
  434. mAllowEditKeyName = false;
  435. bChanged = true;
  436. GUIUtility.keyboardControl = 0;
  437. }
  438. GUI.enabled = termData!=null && !string.IsNullOrEmpty(mNewKeyName) && CanUseNewName;
  439. if (GUILayout.Button (new GUIContent("Rename","Renames the term in the source and updates every object using it in the current scene"), EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
  440. {
  441. mNewKeyName = ValidatedName;
  442. Term = mNewKeyName;
  443. mTermsArray=null; // this recreates that terms array
  444. mAllowEditKeyName = false;
  445. bChanged = true;
  446. LocalizationEditor.TermReplacements = new Dictionary<string, string>(StringComparer.Ordinal);
  447. LocalizationEditor.TermReplacements[ termData.Term ] = mNewKeyName;
  448. termData.Term = mNewKeyName;
  449. source.UpdateDictionary(true);
  450. LocalizationEditor.ReplaceTermsInCurrentScene();
  451. GUIUtility.keyboardControl = 0;
  452. EditorApplication.update += LocalizationEditor.DoParseTermsInCurrentScene;
  453. }
  454. GUI.enabled = true;
  455. GUILayout.EndHorizontal();
  456. bChanged |= OnGUI_SelectKey_PreviewTerms ( ref Term);
  457. }
  458. GUILayout.Space (5);
  459. return bChanged;
  460. }
  461. void UpdateTermsList( string currentTerm )
  462. {
  463. List<string> Terms = mLocalize.Source==null ? LocalizationManager.GetTermsList() : mLocalize.Source.SourceData.GetTermsList();
  464. // If there is a filter, remove all terms not matching that filter
  465. if (mAllowEditKeyName && !string.IsNullOrEmpty(mNewKeyName))
  466. {
  467. string Filter = mNewKeyName.ToUpper();
  468. for (int i=Terms.Count-1; i>=0; --i)
  469. if (!Terms[i].ToUpper().Contains(Filter) && Terms[i]!=currentTerm)
  470. Terms.RemoveAt(i);
  471. }
  472. if (!string.IsNullOrEmpty(currentTerm) && currentTerm!="-" && !Terms.Contains(currentTerm))
  473. Terms.Add (currentTerm);
  474. Terms.Sort(StringComparer.OrdinalIgnoreCase);
  475. Terms.Add("");
  476. if (mLocalize.Source != null)
  477. {
  478. Terms.Add("< Show Terms from all sources >");
  479. Terms.Add("");
  480. }
  481. Terms.Add("<inferred from text>");
  482. Terms.Add("<none>");
  483. mTermsArray = Terms.ToArray();
  484. }
  485. bool OnGUI_SelectKey_PreviewTerms ( ref string Term)
  486. {
  487. if (mTermsArray==null)
  488. UpdateTermsList(Term);
  489. int nTerms = mTermsArray.Length;
  490. if (nTerms<=0)
  491. return false;
  492. if (nTerms==1 && mTermsArray[0]==Term)
  493. return false;
  494. bool bChanged = false;
  495. GUI.backgroundColor = Color.gray;
  496. GUILayout.BeginVertical (GUIStyle_OldTextArea);
  497. for (int i = 0, imax = Mathf.Min (nTerms, 3); i < imax; ++i)
  498. {
  499. ParsedTerm parsedTerm;
  500. int nUses = -1;
  501. if (LocalizationEditor.mParsedTerms.TryGetValue (mTermsArray [i], out parsedTerm))
  502. nUses = parsedTerm.Usage;
  503. string FoundText = mTermsArray [i];
  504. if (nUses > 0)
  505. FoundText = string.Concat ("(", nUses, ") ", FoundText);
  506. if (GUILayout.Button (FoundText, EditorStyles.miniLabel, GUILayout.MaxWidth(EditorGUIUtility.currentViewWidth - 70)))
  507. {
  508. if (mTermsArray[i] == "<inferred from text>")
  509. mNewKeyName = Term = string.Empty;
  510. else
  511. if (mTermsArray[i] == "<none>")
  512. mNewKeyName = Term = "-";
  513. else
  514. if (mTermsArray[i] != "< Show Terms from all sources >")
  515. mNewKeyName = Term = mTermsArray[i];
  516. //mNewKeyName = Term = (mTermsArray [i]=="<inferred from text>" ? string.Empty : mTermsArray [i]);
  517. GUIUtility.keyboardControl = 0;
  518. mAllowEditKeyName = false;
  519. bChanged = true;
  520. }
  521. }
  522. if (nTerms > 3)
  523. GUILayout.Label ("...");
  524. GUILayout.EndVertical ();
  525. GUI.backgroundColor = Color.white;
  526. return bChanged;
  527. }
  528. #endregion
  529. #region Target
  530. void OnGUI_Target()
  531. {
  532. List<string> TargetTypes = new List<string>();
  533. int CurrentTarget = -1;
  534. mLocalize.FindTarget();
  535. foreach (var desc in LocalizationManager.mLocalizeTargets)
  536. {
  537. if (desc.CanLocalize(mLocalize))
  538. {
  539. TargetTypes.Add(desc.Name);
  540. if (mLocalize.mLocalizeTarget!=null && desc.GetTargetType() == mLocalize.mLocalizeTarget.GetType())
  541. CurrentTarget = TargetTypes.Count - 1;
  542. }
  543. }
  544. if (CurrentTarget==-1)
  545. {
  546. CurrentTarget = TargetTypes.Count;
  547. TargetTypes.Add("None");
  548. }
  549. GUILayout.BeginHorizontal();
  550. GUILayout.Label ("Target:", GUILayout.Width (60));
  551. EditorGUI.BeginChangeCheck();
  552. int index = EditorGUILayout.Popup(CurrentTarget, TargetTypes.ToArray());
  553. if (EditorGUI.EndChangeCheck())
  554. {
  555. serializedObject.ApplyModifiedProperties();
  556. foreach (var obj in serializedObject.targetObjects)
  557. {
  558. var cmp = obj as Localize;
  559. if (cmp == null)
  560. continue;
  561. if (cmp.mLocalizeTarget != null)
  562. DestroyImmediate(cmp.mLocalizeTarget);
  563. cmp.mLocalizeTarget = null;
  564. foreach (var desc in LocalizationManager.mLocalizeTargets)
  565. {
  566. if (desc.Name == TargetTypes[index])
  567. {
  568. cmp.mLocalizeTarget = desc.CreateTarget(cmp);
  569. cmp.mLocalizeTargetName = desc.GetTargetType().ToString();
  570. break;
  571. }
  572. }
  573. }
  574. serializedObject.Update();
  575. }
  576. GUILayout.EndHorizontal();
  577. }
  578. #endregion
  579. #region Source
  580. void OnGUI_Source()
  581. {
  582. GUILayout.BeginHorizontal();
  583. ILanguageSource currentSource = mLocalize.Source;
  584. if (currentSource==null)
  585. {
  586. LanguageSourceData source = LocalizationManager.GetSourceContaining(mLocalize.Term);
  587. currentSource = source==null ? null : source.owner;
  588. }
  589. if (GUILayout.Button("Open Source", EditorStyles.toolbarButton, GUILayout.Width (100)))
  590. {
  591. Selection.activeObject = currentSource as Object;
  592. string sTerm, sSecondary;
  593. mLocalize.GetFinalTerms( out sTerm, out sSecondary );
  594. if (GUI_SelectedTerm==1) sTerm = sSecondary;
  595. LocalizationEditor.mKeyToExplore = sTerm;
  596. }
  597. GUILayout.Space (2);
  598. GUILayout.BeginHorizontal(EditorStyles.toolbar);
  599. EditorGUI.BeginChangeCheck ();
  600. if (mLocalize.Source == null)
  601. {
  602. GUI.contentColor = Color.Lerp (Color.gray, Color.yellow, 0.1f);
  603. }
  604. Object obj = EditorGUILayout.ObjectField(currentSource as Object, typeof(Object), true);
  605. GUI.contentColor = Color.white;
  606. if (EditorGUI.EndChangeCheck())
  607. {
  608. ILanguageSource NewSource = obj as ILanguageSource;
  609. if (NewSource == null && obj as GameObject != null)
  610. {
  611. NewSource = (obj as GameObject).GetComponent<LanguageSource>();
  612. }
  613. mLocalize.Source = NewSource;
  614. string sTerm, sSecondary;
  615. mLocalize.GetFinalTerms(out sTerm, out sSecondary);
  616. if (GUI_SelectedTerm == 1) sTerm = sSecondary;
  617. UpdateTermsList(sTerm);
  618. }
  619. if (GUILayout.Button(new GUIContent("Detect", "Finds the LanguageSource containing the selected term, the term list will now only show terms inside that source."), EditorStyles.toolbarButton, GUILayout.ExpandWidth(false)))
  620. {
  621. string sTerm, sSecondary;
  622. mLocalize.GetFinalTerms(out sTerm, out sSecondary);
  623. if (GUI_SelectedTerm == 1) sTerm = sSecondary;
  624. var data = LocalizationManager.GetSourceContaining(sTerm, false);
  625. mLocalize.Source = data==null ? null : data.owner;
  626. mTermsArray = null;
  627. }
  628. GUILayout.EndHorizontal();
  629. GUILayout.EndHorizontal();
  630. }
  631. #endregion
  632. #region Event CallBack
  633. //public void DrawEventCallBack( EventCallback CallBack, Localize loc )
  634. //{
  635. //if (CallBack==null)
  636. // return;
  637. //GUI.changed = false;
  638. //GUILayout.BeginHorizontal();
  639. //GUILayout.Label("Target:", GUILayout.ExpandWidth(false));
  640. //CallBack.Target = EditorGUILayout.ObjectField( CallBack.Target, typeof(MonoBehaviour), true) as MonoBehaviour;
  641. //GUILayout.EndHorizontal();
  642. //if (CallBack.Target!=null)
  643. //{
  644. // GameObject GO = CallBack.Target.gameObject;
  645. // List<MethodInfo> Infos = new List<MethodInfo>();
  646. // var targets = GO.GetComponents(typeof(MonoBehaviour));
  647. // foreach (var behavior in targets)
  648. // Infos.AddRange( behavior.GetType().GetMethods() );
  649. // List<string> Methods = new List<string>();
  650. // for (int i = 0, imax=Infos.Count; i<imax; ++i)
  651. // {
  652. // MethodInfo mi = Infos[i];
  653. // if (IsValidMethod(mi))
  654. // Methods.Add (mi.Name);
  655. // }
  656. // int Index = Methods.IndexOf(CallBack.MethodName);
  657. // int NewIndex = EditorGUILayout.Popup(Index, Methods.ToArray(), GUILayout.ExpandWidth(true));
  658. // if (NewIndex!=Index)
  659. // CallBack.MethodName = Methods[ NewIndex ];
  660. //}
  661. //if (GUI.changed)
  662. //{
  663. // GUI.changed = false;
  664. // EditorUtility.SetDirty(loc);
  665. // //UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty() EditorApplication.MakeSceneDirty();
  666. //}
  667. //}
  668. static bool IsValidMethod( MethodInfo mi )
  669. {
  670. if (mi.DeclaringType == typeof(MonoBehaviour) || mi.ReturnType != typeof(void))
  671. return false;
  672. ParameterInfo[] Params = mi.GetParameters ();
  673. if (Params.Length == 0) return true;
  674. if (Params.Length > 1) return false;
  675. if (Params [0].ParameterType.IsSubclassOf (typeof(Object))) return true;
  676. if (Params [0].ParameterType == typeof(Object)) return true;
  677. return false;
  678. }
  679. #endregion
  680. #region Styles
  681. public static GUIStyle GUIStyle_Header {
  682. get{
  683. if (mGUIStyle_Header==null)
  684. {
  685. mGUIStyle_Header = new GUIStyle("HeaderLabel");
  686. mGUIStyle_Header.fontSize = 25;
  687. mGUIStyle_Header.normal.textColor = Color.Lerp(Color.white, Color.gray, 0.5f);
  688. mGUIStyle_Header.fontStyle = FontStyle.BoldAndItalic;
  689. mGUIStyle_Header.alignment = TextAnchor.UpperCenter;
  690. }
  691. return mGUIStyle_Header;
  692. }
  693. }
  694. static GUIStyle mGUIStyle_Header;
  695. public static GUIStyle GUIStyle_SubHeader {
  696. get{
  697. if (mGUIStyle_SubHeader==null)
  698. {
  699. mGUIStyle_SubHeader = new GUIStyle("HeaderLabel");
  700. mGUIStyle_SubHeader.fontSize = 13;
  701. mGUIStyle_SubHeader.fontStyle = FontStyle.Normal;
  702. mGUIStyle_SubHeader.margin.top = -50;
  703. mGUIStyle_SubHeader.alignment = TextAnchor.UpperCenter;
  704. }
  705. return mGUIStyle_SubHeader;
  706. }
  707. }
  708. static GUIStyle mGUIStyle_SubHeader;
  709. public static GUIStyle GUIStyle_Background {
  710. get{
  711. if (mGUIStyle_Background==null)
  712. {
  713. mGUIStyle_Background = new GUIStyle(EditorStyles.textArea);
  714. mGUIStyle_Background.fixedHeight = 0;
  715. mGUIStyle_Background.overflow.left = 50;
  716. mGUIStyle_Background.overflow.right = 50;
  717. mGUIStyle_Background.overflow.top = -5;
  718. mGUIStyle_Background.overflow.bottom = 0;
  719. }
  720. return mGUIStyle_Background;
  721. }
  722. }
  723. static GUIStyle mGUIStyle_Background;
  724. public static GUIStyle GUIStyle_OldTextArea
  725. {
  726. get
  727. {
  728. if (mGUIStyle_OldTextArea == null)
  729. {
  730. mGUIStyle_OldTextArea = new GUIStyle(EditorStyles.textArea);
  731. mGUIStyle_OldTextArea.fixedHeight = 0;
  732. }
  733. return mGUIStyle_OldTextArea;
  734. }
  735. }
  736. static GUIStyle mGUIStyle_OldTextArea;
  737. #endregion
  738. }
  739. }