Пример #1
0
        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);
                }
            }
        }
Пример #2
0
        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);
                }
            }
        }