StringTableInspector.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Renders an inspector for the <see cref="StringTable"/> resource.
  8. /// </summary>
  9. [CustomInspector(typeof(StringTable))]
  10. internal class StringTableInspector : Inspector
  11. {
  12. private GUIEnumField languageField;
  13. private GUIDictionaryField<string, string> valuesField = new GUIDictionaryField<string,string>();
  14. private Dictionary<string, string> strings = new Dictionary<string,string>();
  15. /// <inheritdoc/>
  16. protected internal override void Initialize()
  17. {
  18. BuildGUI();
  19. }
  20. /// <inheritdoc/>
  21. protected internal override void Refresh()
  22. {
  23. // Note: We're ignoring changes to the string table made externally here in order to avoid a lot of checks.
  24. if ((Language) languageField.Value != StringTables.ActiveLanguage)
  25. {
  26. languageField.Value = (ulong)StringTables.ActiveLanguage;
  27. BuildGUI();
  28. }
  29. valuesField.Refresh();
  30. }
  31. /// <summary>
  32. /// Recreates all the GUI elements used by this inspector.
  33. /// </summary>
  34. private void BuildGUI()
  35. {
  36. Layout.Clear();
  37. strings.Clear();
  38. StringTable stringTable = InspectedObject as StringTable;
  39. if(stringTable == null)
  40. return;
  41. string[] identifiers = stringTable.Identifiers;
  42. foreach (var identifier in identifiers)
  43. strings[identifier] = stringTable.GetString(identifier);
  44. languageField = new GUIEnumField(typeof (Language));
  45. languageField.OnSelectionChanged += x =>
  46. {
  47. StringTables.ActiveLanguage = (Language)x;
  48. BuildGUI();
  49. Refresh();
  50. };
  51. Layout.AddElement(languageField);
  52. valuesField.Update<StringTableEntry>(new LocEdString("Strings"), strings, Layout);
  53. valuesField.OnChanged += x =>
  54. {
  55. if (x != null)
  56. {
  57. foreach (var KVP in x)
  58. {
  59. if (stringTable.Contains(KVP.Key))
  60. {
  61. string oldValue = stringTable.GetString(KVP.Key);
  62. if (oldValue != KVP.Value)
  63. stringTable.SetString(KVP.Key, KVP.Value);
  64. }
  65. else
  66. stringTable.SetString(KVP.Key, KVP.Value);
  67. }
  68. string[] oldIdentifiers = stringTable.Identifiers;
  69. foreach (var identifier in oldIdentifiers)
  70. {
  71. if (!x.ContainsKey(identifier))
  72. stringTable.RemoveString(identifier);
  73. }
  74. }
  75. else
  76. {
  77. foreach (var KVP in strings)
  78. stringTable.RemoveString(KVP.Key);
  79. }
  80. EditorApplication.SetDirty(stringTable);
  81. BuildGUI();
  82. Refresh();
  83. };
  84. valuesField.OnValueChanged += x =>
  85. {
  86. stringTable.SetString(x, strings[x]);
  87. EditorApplication.SetDirty(stringTable);
  88. };
  89. Layout.AddSpace(10);
  90. }
  91. /// <summary>
  92. /// Row element used for displaying GUI for string table dictionary elements.
  93. /// </summary>
  94. public class StringTableEntry : GUIDictionaryFieldRow
  95. {
  96. private GUITextField keyField;
  97. private GUITextField valueField;
  98. /// <inheritdoc/>
  99. protected override GUILayoutX CreateKeyGUI(GUILayoutY layout)
  100. {
  101. string key = GetKey<string>();
  102. GUILayoutX titleLayout = layout.AddLayoutX();
  103. keyField = new GUITextField(new LocEdString(key));
  104. titleLayout.AddElement(keyField);
  105. keyField.OnChanged += SetKey;
  106. return titleLayout;
  107. }
  108. /// <inheritdoc/>
  109. protected override void CreateValueGUI(GUILayoutY layout)
  110. {
  111. string value = GetValue<string>();
  112. valueField = new GUITextField(new LocEdString(value));
  113. layout.AddElement(valueField);
  114. valueField.OnChanged += SetValue;
  115. }
  116. /// <inheritdoc/>
  117. internal protected override bool Refresh()
  118. {
  119. keyField.Value = GetKey<string>();
  120. valueField.Value = GetValue<string>();
  121. return false;
  122. }
  123. }
  124. }
  125. }