using System.Collections.Generic; using UnityEditor; using UnityEngine; using Ubisoft.Hotel.Localisation; namespace Ubisoft.Hotel.Package.Editor { [CustomEditor(typeof(AppNameProperty))] internal sealed class AppNamePropertyEditor : PackagePropertyEditor { private AppNameProperty m_appNameProperty; protected override void ExtendedOnEnable() { m_appNameProperty = target as AppNameProperty; } protected override void ExtendedOnInspectorGUI() { DrawLocalisedAppNames(); } private void DrawLocalisedAppNames() { string[] titles = { "Name", "Short name (max 16ch)" }; List localisedAppNames = m_appNameProperty.LocalisedAppNames; DrawLocalisedAppName(titles, ref localisedAppNames); bool enabled = localisedAppNames.Count < LocalisationUtils.GetLanguageIds().Count; bool isPressedAddAppName = DrawButton("+ Add new localised app name", enabled); if (isPressedAddAppName) { localisedAppNames.Add(CreateLocalisedAppName()); } } private void DrawLocalisedAppName(string[] titles, ref List localisedAppNames) { float headColumn = 160.0f; float contentColumn = 160.0f; _ = EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("", EditorStyles.label, GUILayout.Width(30)); EditorGUILayout.LabelField("Language Id", EditorStyles.label, GUILayout.Width(headColumn + 30)); foreach (string item in titles) { EditorGUILayout.LabelField(item, EditorStyles.label, GUILayout.Width(contentColumn)); } EditorGUILayout.EndHorizontal(); bool backupEnabled = GUI.enabled; string[] languageNames = LocalisationUtils.GetLanguageNamesAsStringArray(); List languageIds = LocalisationUtils.GetLanguageIds(); int index, backupIndex; AppName itemToRemove = null; foreach (AppName item in localisedAppNames) { _ = EditorGUILayout.BeginHorizontal(); bool isPressedRemoveAppNameFile = DrawSimpleButton("-"); if (isPressedRemoveAppNameFile) { itemToRemove = item; } backupIndex = languageIds.IndexOf(item.LanguageId); if (backupIndex == -1) { Debug.EditorLogError($"Language id {Debug.FormatTextInCodeContext(item.LanguageId.ToString())} is not a valid language id."); } else { index = EditorGUILayout.Popup("", backupIndex, languageNames, GUILayout.Width(headColumn)); if (index != backupIndex) { // Make sure that there's no another AppName that uses the same language id if (GetAppNameIndexByLanguageId(LocalisationUtils.NameToLanguageId(languageNames[index])) == -1) { item.LanguageId = LocalisationUtils.NameToLanguageId(languageNames[index]); } else { Debug.EditorLogError($"An app name is already defined for language id {Debug.FormatTextInCodeContext(languageNames[index])}"); } } } GUI.enabled = true; // Names item.Name = DrawSimpleTextField(item.Name, contentColumn); item.ShortName = DrawSimpleTextField(item.ShortName, contentColumn); EditorGUILayout.EndHorizontal(); } GUI.enabled = backupEnabled; if (itemToRemove != null) { _ = localisedAppNames.Remove(itemToRemove); Repaint(); } } private AppName CreateLocalisedAppName() { List localisedAppNames = m_appNameProperty.LocalisedAppNames; List appNames = new List(); int count = localisedAppNames.Count; for (int i = 0; i < count; i++) { appNames.Add(localisedAppNames[i].LanguageId); } // Searchs for the first language id that is not used in localisedAppNames yet ELanguageId languageId = GetNextLanguageIdForAppName(appNames); return new AppName(languageId, "Enter app name", ""); } private ELanguageId GetNextLanguageIdForAppName(List appNames) { ELanguageId returnValue = LocalisationUtils.DEFAULT_LANGUAGE_ID; List languageIds = LocalisationUtils.GetLanguageIds(); foreach (ELanguageId languageId in languageIds) { if (!appNames.Contains(languageId)) { returnValue = languageId; break; } } return returnValue; } private int GetAppNameIndexByLanguageId(ELanguageId languageId) { int returnValue = -1; if (m_appNameProperty.LocalisedAppNames != null) { int count = m_appNameProperty.LocalisedAppNames.Count; for (int i = 0; i < count && returnValue == -1; i++) { if (m_appNameProperty.LocalisedAppNames[i].LanguageId == languageId) { returnValue = i; } } } return returnValue; } private string DrawSimpleTextField(string content, float width) { return EditorGUILayout.TextField(content, GUILayout.Width(width)); } private bool DrawSimpleButton(string label) { return GUILayout.Button(label, GUILayout.Width(30), GUILayout.Height(15)); } private bool DrawButton(string label, bool enabled = true) { bool backup = GUI.enabled; GUI.enabled = enabled; _ = EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); bool isClicked = GUILayout.Button(label, GUILayout.Width(200)); GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); GUI.enabled = backup; return isClicked; } } }