public override void Setup(SerializedProperty property, FieldInfo fieldInfo, PropertyAttribute attribute) { _dictionary = PropertyHelper.GetObject <IEditableDictionary>(property); if (_dictionary == null) { Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath); } else { _dictionaryControl.Setup(property, _dictionary); if (attribute is DictionaryDisplayAttribute display) { if (display.AssetType != null) { _dictionaryControl.MakeDrawable(ListItemDisplayType.AssetPopup, display.AssetType); } else if (display.ItemDisplay != ListItemDisplayType.Normal) { _dictionaryControl.MakeDrawable(display.ItemDisplay, null); } if (display.AllowAdd) { _dictionaryControl.MakeAddable(_addButton, display.AddLabel == null ? new GUIContent("Add Item") : (display.AddLabel == "" ? GUIContent.none : new GUIContent(display.AddLabel))); } if (display.AllowRemove) { _dictionaryControl.MakeRemovable(_removeButton); } if (display.AllowCollapse) { _dictionaryControl.MakeCollapsable(GetOpenPreference(property)); } if (display.ShowEditButton) { _dictionaryControl.MakeEditable(_editButton); } if (display.EmptyText != null) { _dictionaryControl.MakeEmptyLabel(new GUIContent(display.EmptyText)); } } } }
public override void Setup(SerializedProperty property, FieldInfo fieldInfo) { var attribute = TypeHelper.GetAttribute <DictionaryDisplayAttribute>(fieldInfo); _dictionary = GetObject <IEditableDictionary>(property); if (_dictionary == null) { Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath); } else { _dictionaryControl.Setup(property, _dictionary); if (attribute != null) { if (attribute.InlineChildren) { _dictionaryControl.MakeDrawableInline(); } if (attribute.AllowAdd) { _dictionaryControl.MakeAddable(_addButton, attribute.AddLabel == null ? new GUIContent("Add Item") : (attribute.AddLabel == "" ? GUIContent.none : new GUIContent(attribute.AddLabel))); } if (attribute.AllowRemove) { _dictionaryControl.MakeRemovable(_removeButton); } if (attribute.AllowCollapse) { _dictionaryControl.MakeCollapsable(GetOpenPreference(property)); } if (attribute.ShowEditButton) { _dictionaryControl.MakeEditable(_editButton); } if (attribute.EmptyText != null) { _dictionaryControl.MakeEmptyLabel(new GUIContent(attribute.EmptyText)); } } } }
public DictionaryControl Setup(SerializedProperty property, IEditableDictionary dictionary) { _dictionary = dictionary; PrepareForEdit(); _rootProperty = property; _keysProperty = property.FindPropertyRelative("_keys"); _valuesProperty = property.FindPropertyRelative("_values"); var reorderableList = new ReorderableList(property.serializedObject, _keysProperty, false, true, false, false); Setup(reorderableList); return(this); }
public override void Setup(SerializedProperty property, FieldInfo fieldInfo, PropertyAttribute attribute) { _dictionary = PropertyHelper.GetObject <IEditableDictionary>(property); _itemDrawer = PropertyHelper.GetNextDrawer(fieldInfo, attribute); if (_dictionary == null) { Debug.LogWarningFormat(_invalidTypeWarning, property.propertyPath); } else { _dictionaryControl.Setup(property, _dictionary); if (attribute is DictionaryDisplayAttribute display) { if (_itemDrawer != null) { _dictionaryControl.MakeDrawable(DrawItem); } if (display.AllowAdd) { _dictionaryControl.MakeAddable(_addButton, display.AddLabel == null ? new GUIContent("Add Item") : (display.AddLabel == string.Empty ? GUIContent.none : new GUIContent(display.AddLabel))); } if (display.AllowRemove) { _dictionaryControl.MakeRemovable(_removeButton); } if (display.AllowCollapse) { _dictionaryControl.MakeCollapsable(); } if (display.EmptyText != null) { _dictionaryControl.MakeEmptyLabel(new GUIContent(display.EmptyText)); } } } }
private string GetDisplayString(object value) { IEditableScripts scriptValue = value as IEditableScripts; IEditableList <string> listStringValue = value as IEditableList <string>; IEditableDictionary <string> dictionaryStringValue = value as IEditableDictionary <string>; IEditableDictionary <IEditableScripts> dictionaryScriptValue = value as IEditableDictionary <IEditableScripts>; IDataWrapper wrappedValue = value as IDataWrapper; string result = null; if (scriptValue != null) { result = scriptValue.DisplayString(); } else if (listStringValue != null) { result = GetListDisplayString(listStringValue.DisplayItems); } else if (dictionaryStringValue != null) { result = GetDictionaryDisplayString(dictionaryStringValue.DisplayItems); } else if (dictionaryScriptValue != null) { result = GetDictionaryDisplayString(dictionaryScriptValue.DisplayItems); } else if (wrappedValue != null) { result = wrappedValue.DisplayString(); } else if (value == null) { result = "(null)"; } else { result = value.ToString(); } return(EditorUtility.FormatAsOneLine(result)); }
private ElementSaveData.ScriptSaveData BindStringDictionary(IValueProvider provider, EditorController controller, string ignoreExpression, IEditableDictionary<string> dictionary, string key, IEditorControl ctl) { ElementSaveData.ScriptSaveData result = new ElementSaveData.ScriptSaveData(); if (dictionary != null) { int dictionaryCount = 0; foreach (var item in dictionary.Items) { string keyValue; if (string.IsNullOrEmpty(ctl.GetString("source"))) { keyValue = GetValueProviderString(provider, string.Format("{0}-key{1}", key, dictionaryCount)); } else { // key is not editable when a source is specified keyValue = item.Key; } result.Attributes.Add(string.Format("key{0}", dictionaryCount), keyValue); string valueValue = GetValueProviderString(provider, string.Format("{0}-value{1}", key, dictionaryCount)); result.Attributes.Add(string.Format("value{0}", dictionaryCount), valueValue); dictionaryCount++; } } return result; }
private ElementSaveData.ScriptSaveData BindScriptDictionary(IValueProvider provider, EditorController controller, string ignoreExpression, IEditableDictionary<IEditableScripts> dictionary, string key) { ElementSaveData.ScriptSaveData result = new ElementSaveData.ScriptSaveData(); if (dictionary != null) { int dictionaryCount = 0; foreach (var item in dictionary.Items.Values) { string expressionValue = GetValueProviderString(provider, string.Format("{0}-key{1}", key, dictionaryCount)); result.Attributes.Add(string.Format("key{0}", dictionaryCount), expressionValue); ElementSaveData.ScriptsSaveData scriptResult = new ElementSaveData.ScriptsSaveData(); BindScriptLines(provider, string.Format("{0}-value{1}", key, dictionaryCount), controller, item.Value, scriptResult, ignoreExpression); result.Attributes.Add(string.Format("value{0}", dictionaryCount), scriptResult); dictionaryCount++; } } return result; }
public PartialViewResult EditScriptDictionaryValue(int id, string key, IEditableDictionary <IEditableScripts> value, string keyPrompt, string source, string attribute) { return(PartialView("ScriptDictionaryEditor", EditorDictionary[id].GetScriptDictionaryModel(id, key, value, keyPrompt, source, attribute, ModelState))); }
public PartialViewResult EditScriptDictionaryValue(int id, string key, IEditableDictionary<IEditableScripts> value, string keyPrompt, string source, string attribute) { return PartialView("ScriptDictionaryEditor", EditorDictionary[id].GetScriptDictionaryModel(id, key, value, keyPrompt, source, attribute, ModelState)); }
private ElementSaveData.ScriptSaveData BindStringDictionary(IValueProvider provider, EditorController controller, string ignoreExpression, IEditableDictionary <string> dictionary, string key, IEditorControl ctl) { ElementSaveData.ScriptSaveData result = new ElementSaveData.ScriptSaveData(); if (dictionary != null) { int dictionaryCount = 0; foreach (var item in dictionary.Items) { string keyValue; if (string.IsNullOrEmpty(ctl.GetString("source"))) { keyValue = GetValueProviderString(provider, string.Format("{0}-key{1}", key, dictionaryCount)); } else { // key is not editable when a source is specified keyValue = item.Key; } result.Attributes.Add(string.Format("key{0}", dictionaryCount), keyValue); string valueValue = GetValueProviderString(provider, string.Format("{0}-value{1}", key, dictionaryCount)); result.Attributes.Add(string.Format("value{0}", dictionaryCount), valueValue); dictionaryCount++; } } return(result); }
private ElementSaveData.ScriptSaveData BindScriptDictionary(IValueProvider provider, EditorController controller, string ignoreExpression, IEditableDictionary <IEditableScripts> dictionary, string key) { ElementSaveData.ScriptSaveData result = new ElementSaveData.ScriptSaveData(); if (dictionary != null) { int dictionaryCount = 0; foreach (var item in dictionary.Items.Values) { string expressionValue = GetValueProviderString(provider, string.Format("{0}-key{1}", key, dictionaryCount)); result.Attributes.Add(string.Format("key{0}", dictionaryCount), expressionValue); ElementSaveData.ScriptsSaveData scriptResult = new ElementSaveData.ScriptsSaveData(); BindScriptLines(provider, string.Format("{0}-value{1}", key, dictionaryCount), controller, item.Value, scriptResult, ignoreExpression); result.Attributes.Add(string.Format("value{0}", dictionaryCount), scriptResult); dictionaryCount++; } } return(result); }
private void BindScriptLines(IValueProvider provider, string attribute, EditorController controller, IEditableScripts originalScript, ElementSaveData.ScriptsSaveData result, string ignoreExpression) { if (originalScript == null) { return; } int count = 0; foreach (IEditableScript script in originalScript.Scripts) { ElementSaveData.ScriptSaveData scriptLine = new ElementSaveData.ScriptSaveData(); scriptLine.IsSelected = (bool)provider.GetValue(string.Format("selected-{0}-{1}", attribute, count)).ConvertTo(typeof(bool)); if (script.Type != ScriptType.If) { IEditorDefinition definition = controller.GetEditorDefinition(script); foreach (IEditorControl ctl in definition.Controls.Where(c => c.Attribute != null)) { string key = string.Format("{0}-{1}-{2}", attribute, count, ctl.Attribute); if (ctl.ControlType == "script") { IEditorData scriptEditorData = controller.GetScriptEditorData(script); IEditableScripts originalSubScript = (IEditableScripts)scriptEditorData.GetAttribute(ctl.Attribute); ElementSaveData.ScriptsSaveData scriptResult = new ElementSaveData.ScriptsSaveData(); BindScriptLines(provider, key, controller, originalSubScript, scriptResult, ignoreExpression); scriptLine.Attributes.Add(ctl.Attribute, scriptResult); } else if (ctl.ControlType == "scriptdictionary") { IEditorData dictionaryData = controller.GetScriptEditorData(script); IEditableDictionary <IEditableScripts> dictionary = (IEditableDictionary <IEditableScripts>)dictionaryData.GetAttribute(ctl.Attribute); ElementSaveData.ScriptSaveData switchResult = BindScriptDictionary(provider, controller, ignoreExpression, dictionary, key); scriptLine.Attributes.Add(ctl.Attribute, switchResult); } else if (ctl.ControlType == "list") { // do nothing } else { object value = GetScriptParameterValue( scriptLine, controller, provider, key, ctl.ControlType, ctl.GetString("simpleeditor") ?? "textbox", ctl.GetString("usetemplates"), (string)script.GetParameter(ctl.Attribute), ignoreExpression ); scriptLine.Attributes.Add(ctl.Attribute, value); } } } else { EditableIfScript ifScript = (EditableIfScript)script; object expressionValue = GetScriptParameterValue( scriptLine, controller, provider, string.Format("{0}-{1}-expression", attribute, count), "expression", null, "if", (string)ifScript.GetAttribute("expression"), ignoreExpression ); scriptLine.Attributes.Add("expression", expressionValue); ElementSaveData.ScriptsSaveData thenScriptResult = new ElementSaveData.ScriptsSaveData(); BindScriptLines(provider, string.Format("{0}-{1}-then", attribute, count), controller, ifScript.ThenScript, thenScriptResult, ignoreExpression); scriptLine.Attributes.Add("then", thenScriptResult); int elseIfCount = 0; foreach (EditableIfScript.EditableElseIf elseIf in ifScript.ElseIfScripts) { object elseIfExpressionValue = GetScriptParameterValue( scriptLine, controller, provider, string.Format("{0}-{1}-elseif{2}-expression", attribute, count, elseIfCount), "expression", null, "if", elseIf.Expression, ignoreExpression ); scriptLine.Attributes.Add(string.Format("elseif{0}-expression", elseIfCount), elseIfExpressionValue); ElementSaveData.ScriptsSaveData elseIfScriptResult = new ElementSaveData.ScriptsSaveData(); BindScriptLines(provider, string.Format("{0}-{1}-elseif{2}", attribute, count, elseIfCount), controller, elseIf.EditableScripts, elseIfScriptResult, ignoreExpression); scriptLine.Attributes.Add(string.Format("elseif{0}-then", elseIfCount), elseIfScriptResult); elseIfCount++; } if (ifScript.ElseScript != null) { ElementSaveData.ScriptsSaveData elseScriptResult = new ElementSaveData.ScriptsSaveData(); BindScriptLines(provider, string.Format("{0}-{1}-else", attribute, count), controller, ifScript.ElseScript, elseScriptResult, ignoreExpression); scriptLine.Attributes.Add("else", elseScriptResult); } } result.ScriptLines.Add(scriptLine); count++; } }
private void BindControl(ModelBindingContext bindingContext, ElementSaveData result, int gameId, string ignoreExpression, Dictionary <int, Services.EditorService> editorDictionary, Models.Element originalElement, IEditorControl ctl, string controlType, string attribute = null) { object saveValue = null; bool addSaveValueToResult = true; if (attribute == null) { attribute = ctl.Attribute; } // check to see if attribute changes from attribute editor is being passed along // if so, use the attribute editor value if (bindingContext.ValueProvider.ContainsPrefix("attr_" + attribute)) { attribute = "attr_" + attribute; } switch (controlType) { case "textbox": case "dropdown": case "file": saveValue = GetValueProviderString(bindingContext.ValueProvider, attribute); break; case "number": string stringValue = GetValueProviderString(bindingContext.ValueProvider, attribute); int intValue; int.TryParse(stringValue, out intValue); int?min = ctl.GetInt("minimum"); int?max = ctl.GetInt("maximum"); if (min.HasValue && intValue < min) { intValue = min.Value; } if (max.HasValue && intValue > max) { intValue = max.Value; } saveValue = intValue; break; case "numberdouble": string stringDoubleValue = GetValueProviderString(bindingContext.ValueProvider, attribute); double doubleValue; double.TryParse(stringDoubleValue, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowLeadingSign, System.Globalization.CultureInfo.InvariantCulture, out doubleValue); double?doubleMin = ctl.GetDouble("minimum"); double?doubleMax = ctl.GetDouble("maximum"); if (doubleMin.HasValue && doubleValue < doubleMin) { doubleValue = doubleMin.Value; } if (doubleMax.HasValue && doubleValue > doubleMax) { doubleValue = doubleMax.Value; } saveValue = doubleValue; break; case "richtext": // Replace all new line characters with a <Br/> tag here string richtextValue = GetValueProviderString(bindingContext.ValueProvider, attribute); saveValue = StripHTMLComments(HttpUtility.HtmlDecode(richtextValue.Replace(Environment.NewLine, "<br/>"))); break; case "checkbox": ValueProviderResult value = bindingContext.ValueProvider.GetValue(attribute); if (value == null) { Logging.Log.ErrorFormat("Expected true/false value for '{0}', but got null", attribute); saveValue = false; } else { saveValue = value.ConvertTo(typeof(bool)); } break; case "script": saveValue = BindScript(bindingContext.ValueProvider, attribute, originalElement.EditorData, editorDictionary[gameId].Controller, ignoreExpression); break; case "multi": string type = WebEditor.Views.Edit.ControlHelpers.GetTypeName(originalElement.EditorData.GetAttribute(attribute)); string subControlType = WebEditor.Views.Edit.ControlHelpers.GetEditorNameForType(type, ctl.GetDictionary("editors")); BindControl(bindingContext, result, gameId, ignoreExpression, editorDictionary, originalElement, ctl, subControlType); addSaveValueToResult = false; break; case "objects": saveValue = new ElementSaveData.ObjectReferenceSaveData { Reference = GetValueProviderString(bindingContext.ValueProvider, "dropdown-" + attribute) }; break; case "verbs": IEditorDataExtendedAttributeInfo extendedData = (IEditorDataExtendedAttributeInfo)originalElement.EditorData; foreach (IEditorAttributeData attr in extendedData.GetAttributeData().Where(a => !a.IsInherited)) { if (editorDictionary[gameId].Controller.IsVerbAttribute(attr.AttributeName)) { object attrValue = extendedData.GetAttribute(attr.AttributeName); string attrStringValue = attrValue as string; IEditableScripts attrScriptValue = attrValue as IEditableScripts; IEditableDictionary <IEditableScripts> attrDictionaryValue = attrValue as IEditableDictionary <IEditableScripts>; if (attrStringValue != null) { BindControl(bindingContext, result, gameId, ignoreExpression, editorDictionary, originalElement, ctl, "textbox", attr.AttributeName); } else if (attrScriptValue != null) { BindControl(bindingContext, result, gameId, ignoreExpression, editorDictionary, originalElement, ctl, "script", attr.AttributeName); } else if (attrDictionaryValue != null) { BindControl(bindingContext, result, gameId, ignoreExpression, editorDictionary, originalElement, ctl, "scriptdictionary", attr.AttributeName); } } } addSaveValueToResult = false; break; case "list": addSaveValueToResult = false; break; case "pattern": saveValue = new ElementSaveData.PatternSaveData { Pattern = GetValueProviderString(bindingContext.ValueProvider, attribute) }; break; case "scriptdictionary": var originalDictionary = originalElement.EditorData.GetAttribute(attribute) as IEditableDictionary <IEditableScripts>; saveValue = BindScriptDictionary(bindingContext.ValueProvider, editorDictionary[gameId].Controller, ignoreExpression, originalDictionary, attribute); break; case "stringdictionary": case "gamebookoptions": var originalStringDictionary = originalElement.EditorData.GetAttribute(attribute) as IEditableDictionary <string>; saveValue = BindStringDictionary(bindingContext.ValueProvider, editorDictionary[gameId].Controller, ignoreExpression, originalStringDictionary, attribute, ctl); break; default: if (attribute == null || controlType == null) { addSaveValueToResult = false; } else { throw new ArgumentException(string.Format("Save data model binder not implemented for control type {0}", controlType)); } break; } if (addSaveValueToResult) { if (result.Values.ContainsKey(attribute)) { Logging.Log.ErrorFormat("SaveData already contains attribute \"{0}\" - saveValue (\"{1}\") discarded", attribute, saveValue); } else { // remove attr prefix if it is in the attribute name: if (attribute.StartsWith("attr_")) { attribute = attribute.Substring(5); } result.Values.Add(attribute, saveValue); } } }