using System; using System.Collections.Generic; using BansheeEngine; namespace BansheeEditor { /// /// Renders an inspector for the resource. /// [CustomInspector(typeof(StringTable))] internal class StringTableInspector : Inspector { private GUIEnumField languageField; private GUIDictionaryField valuesField = new GUIDictionaryField(); private Dictionary strings = new Dictionary(); /// protected internal override void Initialize() { BuildGUI(); } /// protected internal override bool Refresh() { bool anythingModified = false; // Note: We're ignoring changes to the string table made externally here in order to avoid a lot of checks. if ((Language) languageField.Value != StringTables.ActiveLanguage) { languageField.Value = (ulong)StringTables.ActiveLanguage; anythingModified = true; BuildGUI(); } anythingModified |= valuesField.Refresh(); return anythingModified; } /// /// Recreates all the GUI elements used by this inspector. /// private void BuildGUI() { layout.Clear(); strings.Clear(); StringTable stringTable = referencedObject as StringTable; if(stringTable == null) return; string[] identifiers = stringTable.Identifiers; foreach (var identifier in identifiers) strings[identifier] = stringTable.GetString(identifier); languageField = new GUIEnumField(typeof (Language)); languageField.OnSelectionChanged += x => { StringTables.ActiveLanguage = (Language)x; BuildGUI(); Refresh(); }; layout.AddElement(languageField); valuesField.Update(new LocEdString("Values"), strings, layout); valuesField.OnChanged += x => { foreach (var KVP in x) { string oldValue; if (strings.TryGetValue(KVP.Key, out oldValue)) { if (oldValue != KVP.Value) stringTable.SetString(KVP.Key, KVP.Value); } else stringTable.SetString(KVP.Key, KVP.Value); } foreach (var KVP in strings) { if (!x.ContainsKey(KVP.Key)) stringTable.RemoveString(KVP.Key); } BuildGUI(); Refresh(); }; valuesField.OnValueChanged += x => { stringTable.SetString(x, strings[x]); }; layout.AddSpace(10); } /// /// Row element used for displaying GUI for string table dictionary elements. /// public class StringTableEntry : GUIDictionaryFieldRow { private GUITextField keyField; private GUITextField valueField; /// protected override GUILayoutX CreateKeyGUI(GUILayoutY layout) { GUILayoutX titleLayout = layout.AddLayoutX(); keyField = new GUITextField(new LocEdString((string)key)); titleLayout.AddElement(keyField); // TODO - Key changes are not being applied yet return titleLayout; } /// protected override void CreateValueGUI(GUILayoutY layout) { string value = GetValue(); valueField = new GUITextField(new LocEdString(value)); layout.AddElement(valueField); } /// internal protected override bool Refresh(out bool rebuildGUI) { rebuildGUI = false; // Key cannot be changed so we don't check it here string newValue = GetValue(); if (valueField.Value != newValue) { valueField.Value = newValue; return true; } return false; } } } }