public PartialViewResult EditElement(int id, string key, string tab, string error, string refreshTreeSelectElement) { Logging.Log.DebugFormat("{0}: EditElement {1}", id, key); if (Session["EditorDictionary"] == null) { return(Timeout()); } Models.Element model = EditorDictionary[id].GetElementModelForView(id, key, tab, error, refreshTreeSelectElement, ModelState); return(PartialView("ElementEditor", model)); }
private void setupCheckbox(Models.Element element) { Checkbox checkBox = ContainerProvider.Scope.Resolve <Checkbox>(); Color4 color = string.IsNullOrEmpty(element.Color) ? primaryColor : colorHelper.ToTKColor(element.Color); checkBox.Setup(vectorHelper.ToTKVector2(element.Position), color); addReferenceToAController(element, checkBox); elements.Add(checkBox); }
private void setupProgressBar(Models.Element element) { ProgressBar progressBar = ContainerProvider.Scope.Resolve <ProgressBar>(); Color4 color = string.IsNullOrEmpty(element.Color) ? primaryColor : colorHelper.ToTKColor(element.Color); Color4 fillColor = string.IsNullOrEmpty(element.FillColor) ? primaryColor : colorHelper.ToTKColor(element.FillColor); progressBar.Setup(vectorHelper.ToTKVector2(element.Position), vectorHelper.ToTKVector2(element.Size), color, fillColor); addReferenceToAController(element, progressBar); elements.Add(progressBar); }
private void setupLabel(Models.Element element) { Label label = ContainerProvider.Scope.Resolve <Label>(); Color4 textColor = string.IsNullOrEmpty(element.TextColor) ? this.textColor : colorHelper.ToTKColor(element.TextColor); label.Setup(vectorHelper.ToTKVector2(element.Position), element.Text, textColor, element.FontSize, element.Clickable); addReferenceToAController(element, label); elements.Add(label); }
private void addReferenceToAController(Models.Element modelElement, Elements.Element element) { if (controllerFields.Any(x => x.Name.Equals(modelElement.Name, StringComparison.CurrentCultureIgnoreCase))) { FieldInfo field = controllerFields.FirstOrDefault(x => x.Name.Equals(modelElement.Name, StringComparison.CurrentCultureIgnoreCase)); if (field.FieldType.Name.Equals(modelElement.Type)) { field.SetValue(controller, element); } else { logger.Warning($"Field { field.Name } should be a type of { modelElement.Type }."); } } }
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); } } }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ElementSaveData result = new ElementSaveData(); result.Values = new Dictionary <string, object>(); int gameId = (int)bindingContext.ValueProvider.GetValue("_game_id").ConvertTo(typeof(int)); string key = (string)bindingContext.ValueProvider.GetValue("_key").ConvertTo(typeof(string)); string redirectToElement = (string)bindingContext.ValueProvider.GetValue("_redirectToElement").ConvertTo(typeof(string)); string additionalAction = (string)bindingContext.ValueProvider.GetValue("_additionalAction").ConvertTo(typeof(string)); string additionalActionTab = (string)bindingContext.ValueProvider.GetValue("_additionalActionTab").ConvertTo(typeof(string)); string ignoreExpression = (string)bindingContext.ValueProvider.GetValue("_ignoreExpression").ConvertTo(typeof(string)); result.GameId = gameId; result.Key = key; result.RedirectToElement = redirectToElement; result.AdditionalAction = additionalAction; result.AdditionalActionTab = additionalActionTab; var editorDictionary = controllerContext.RequestContext.HttpContext.Session["EditorDictionary"] as Dictionary <int, Services.EditorService>; if (editorDictionary == null) { result.Success = false; return(result); } if (!editorDictionary.ContainsKey(gameId)) { Logging.Log.ErrorFormat("Current Session does not contain a game id of {0}", gameId); result.Success = false; return(result); } if (editorDictionary[gameId] == null) { Logging.Log.ErrorFormat("Current Session has game id {0} = null", gameId); result.Success = false; return(false); } Models.Element originalElement = editorDictionary[gameId].GetElementModelForView(gameId, key); if (originalElement == null) { Logging.Log.ErrorFormat("BindModel failed for game {0} element '{1}' - originalElement is null", gameId, key); result.Success = false; return(false); } if (originalElement.EditorDefinition == null) { Logging.Log.ErrorFormat("BindModel failed for game {0} element '{1}' - originalElement.EditorDefinition is null", gameId, key); result.Success = false; return(false); } if (originalElement.EditorDefinition.Tabs == null) { Logging.Log.ErrorFormat("BindModel failed for game {0} element '{1}' - originalElement.EditorDefinition.Tabs is null", gameId, key); result.Success = false; return(false); } foreach (IEditorTab tab in originalElement.EditorDefinition.Tabs.Values) { if (!tab.IsTabVisible(originalElement.EditorData)) { continue; } if (tab.GetBool("desktop")) { continue; } if (editorDictionary[gameId].Controller.SimpleMode && !tab.IsTabVisibleInSimpleMode) { continue; } foreach (IEditorControl ctl in tab.Controls) { if (!ctl.IsControlVisible(originalElement.EditorData)) { continue; } if (ctl.GetBool("desktop")) { continue; } if (editorDictionary[gameId].Controller.SimpleMode && !ctl.IsControlVisibleInSimpleMode) { continue; } BindControl(bindingContext, result, gameId, ignoreExpression, editorDictionary, originalElement, ctl, ctl.ControlType); } } result.Success = true; return(result); }