StringTableInspector.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, StringTableEntry> valuesField;
  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 = GUIDictionaryField<string, string, StringTableEntry>.Create(
  53. new LocEdString("Strings"), strings, Layout);
  54. valuesField.OnChanged += x =>
  55. {
  56. if (x != null)
  57. {
  58. foreach (var KVP in x)
  59. {
  60. if (stringTable.Contains(KVP.Key))
  61. {
  62. string oldValue = stringTable.GetString(KVP.Key);
  63. if (oldValue != KVP.Value)
  64. stringTable.SetString(KVP.Key, KVP.Value);
  65. }
  66. else
  67. stringTable.SetString(KVP.Key, KVP.Value);
  68. }
  69. string[] oldIdentifiers = stringTable.Identifiers;
  70. foreach (var identifier in oldIdentifiers)
  71. {
  72. if (!x.ContainsKey(identifier))
  73. stringTable.RemoveString(identifier);
  74. }
  75. }
  76. else
  77. {
  78. foreach (var KVP in strings)
  79. stringTable.RemoveString(KVP.Key);
  80. }
  81. EditorApplication.SetDirty(stringTable);
  82. };
  83. valuesField.OnValueChanged += x =>
  84. {
  85. stringTable.SetString(x, strings[x]);
  86. EditorApplication.SetDirty(stringTable);
  87. };
  88. valuesField.OnValueRemoved += x =>
  89. {
  90. stringTable.RemoveString(x);
  91. EditorApplication.SetDirty(stringTable);
  92. };
  93. Layout.AddSpace(10);
  94. }
  95. /// <summary>
  96. /// Row element used for displaying GUI for string table dictionary elements.
  97. /// </summary>
  98. public class StringTableEntry : GUIDictionaryFieldRow
  99. {
  100. private GUITextField keyField;
  101. private GUITextField valueField;
  102. /// <inheritdoc/>
  103. protected override GUILayoutX CreateKeyGUI(GUILayoutY layout)
  104. {
  105. GUILayoutX titleLayout = layout.AddLayoutX();
  106. keyField = new GUITextField(new LocEdString("Identifier"));
  107. titleLayout.AddElement(keyField);
  108. keyField.OnChanged += SetKey;
  109. return titleLayout;
  110. }
  111. /// <inheritdoc/>
  112. protected override void CreateValueGUI(GUILayoutY layout)
  113. {
  114. string value = GetValue<string>();
  115. valueField = new GUITextField(new LocEdString(value));
  116. layout.AddElement(valueField);
  117. valueField.OnChanged += SetValue;
  118. }
  119. /// <inheritdoc/>
  120. internal protected override bool Refresh()
  121. {
  122. keyField.Value = GetKey<string>();
  123. valueField.Value = GetValue<string>();
  124. return false;
  125. }
  126. }
  127. }
  128. }