StringTableInspector.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 bool Refresh()
  22. {
  23. bool anythingModified = false;
  24. // Note: We're ignoring changes to the string table made externally here in order to avoid a lot of checks.
  25. if ((Language) languageField.Value != StringTables.ActiveLanguage)
  26. {
  27. languageField.Value = (ulong)StringTables.ActiveLanguage;
  28. anythingModified = true;
  29. BuildGUI();
  30. }
  31. anythingModified |= valuesField.Refresh();
  32. return anythingModified;
  33. }
  34. /// <summary>
  35. /// Recreates all the GUI elements used by this inspector.
  36. /// </summary>
  37. private void BuildGUI()
  38. {
  39. layout.Clear();
  40. strings.Clear();
  41. StringTable stringTable = referencedObject as StringTable;
  42. if(stringTable == null)
  43. return;
  44. string[] identifiers = stringTable.Identifiers;
  45. foreach (var identifier in identifiers)
  46. strings[identifier] = stringTable.GetString(identifier);
  47. languageField = new GUIEnumField(typeof (Language));
  48. languageField.OnSelectionChanged += x =>
  49. {
  50. StringTables.ActiveLanguage = (Language)x;
  51. BuildGUI();
  52. Refresh();
  53. };
  54. layout.AddElement(languageField);
  55. valuesField.Update<StringTableEntry>(new LocEdString("Values"), strings, layout);
  56. valuesField.OnChanged += x =>
  57. {
  58. foreach (var KVP in x)
  59. {
  60. string oldValue;
  61. if (strings.TryGetValue(KVP.Key, out oldValue))
  62. {
  63. if (oldValue != KVP.Value)
  64. stringTable.SetString(KVP.Key, KVP.Value);
  65. }
  66. else
  67. stringTable.SetString(KVP.Key, KVP.Value);
  68. }
  69. foreach (var KVP in strings)
  70. {
  71. if (!x.ContainsKey(KVP.Key))
  72. stringTable.RemoveString(KVP.Key);
  73. }
  74. BuildGUI();
  75. Refresh();
  76. };
  77. valuesField.OnValueChanged += x =>
  78. {
  79. stringTable.SetString(x, strings[x]);
  80. };
  81. layout.AddSpace(10);
  82. }
  83. /// <summary>
  84. /// Row element used for displaying GUI for string table dictionary elements.
  85. /// </summary>
  86. public class StringTableEntry : GUIDictionaryFieldRow
  87. {
  88. private GUITextField keyField;
  89. private GUITextField valueField;
  90. /// <inheritdoc/>
  91. protected override GUILayoutX CreateKeyGUI(GUILayoutY layout)
  92. {
  93. GUILayoutX titleLayout = layout.AddLayoutX();
  94. keyField = new GUITextField(new LocEdString((string)key));
  95. titleLayout.AddElement(keyField);
  96. // TODO - Key changes are not being applied yet
  97. return titleLayout;
  98. }
  99. /// <inheritdoc/>
  100. protected override void CreateValueGUI(GUILayoutY layout)
  101. {
  102. string value = GetValue<string>();
  103. valueField = new GUITextField(new LocEdString(value));
  104. layout.AddElement(valueField);
  105. }
  106. /// <inheritdoc/>
  107. internal protected override bool Refresh(out bool rebuildGUI)
  108. {
  109. rebuildGUI = false;
  110. // Key cannot be changed so we don't check it here
  111. string newValue = GetValue<string>();
  112. if (valueField.Value != newValue)
  113. {
  114. valueField.Value = newValue;
  115. return true;
  116. }
  117. return false;
  118. }
  119. }
  120. }
  121. }