StringTableInspector.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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("Strings"), 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. string key = GetKey<string>();
  94. GUILayoutX titleLayout = layout.AddLayoutX();
  95. keyField = new GUITextField(new LocEdString(key));
  96. titleLayout.AddElement(keyField);
  97. keyField.OnChanged += SetKey;
  98. return titleLayout;
  99. }
  100. /// <inheritdoc/>
  101. protected override void CreateValueGUI(GUILayoutY layout)
  102. {
  103. string value = GetValue<string>();
  104. valueField = new GUITextField(new LocEdString(value));
  105. layout.AddElement(valueField);
  106. valueField.OnChanged += SetValue;
  107. }
  108. /// <inheritdoc/>
  109. internal protected override bool Refresh(out bool rebuildGUI)
  110. {
  111. rebuildGUI = false;
  112. string newKey = GetKey<string>();
  113. if (keyField.Value != newKey)
  114. {
  115. keyField.Value = newKey;
  116. return true;
  117. }
  118. string newValue = GetValue<string>();
  119. if (valueField.Value != newValue)
  120. {
  121. valueField.Value = newValue;
  122. return true;
  123. }
  124. return false;
  125. }
  126. }
  127. }
  128. }