/// <summary> /// 入力値をプロパティに設定する /// </summary> /// <param name="childTransform">値を取得対象ゲームオブジェクトのトランスフォーム</param> /// <param name="propertyInfo">プロパティ</param> /// <param name="component">プロパティが宣言されている親クラス</param> private static void SetComponentToProperty(Transform childTransform, PropertyInfo propertyInfo, object component) { object value = null; if (childTransform.GetComponent <DropdownBox>() != null) { DropdownBox dropdownBox = childTransform.GetComponent <DropdownBox>(); value = dropdownBox.SelectedItem.Value; } else if (childTransform.GetComponent <InputField>() != null) { InputField inputField = childTransform.GetComponent <InputField>(); value = inputField.text; } else if (childTransform.GetComponent <Toggle>() != null) { value = childTransform.GetComponent <Toggle>().isOn; } DataBinding <object> .SetPropertyValue(propertyInfo, component, value); }
public void ParseWidgets(IContainer ParentWidget, Dictionary <string, object> Data) { foreach (string name in Data.Keys) { if (!(Data[name] is JObject)) { throw new Exception($"Expected an Object, but got {Data[name].GetType().Name} in key '{name}' inside 'widgets'"); } Dictionary <string, object> config = ((JObject)Data[name]).ToObject <Dictionary <string, object> >(); if (!config.ContainsKey("type")) { throw new Exception($"Widget definition must contain a 'type' key in widget '{name}'"); } if (!(config["type"] is string)) { throw new Exception($"Widget type must be a string."); } Widget w = null; string type = (string)config["type"]; switch (type) { case "container": w = new Container(ParentWidget); break; case "label": w = new DynamicLabel(ParentWidget); break; case "numeric": w = new NumericBox(ParentWidget); break; case "textbox": w = new TextBox(ParentWidget); break; case "button": w = new Button(ParentWidget); break; case "dropdown": w = new DropdownBox(ParentWidget); break; case "checkbox": w = new CheckBox(ParentWidget); break; case "radiobox": w = new RadioBox(ParentWidget); break; case "switch_picker": w = new GameSwitchBox(ParentWidget); break; case "variable_picker": w = new GameVariableBox(ParentWidget); break; case "multitextbox": w = new MultilineTextBox(ParentWidget); break; case "multilabel": w = new MultilineDynamicLabel(ParentWidget); break; default: throw new Exception($"Unknown widget type '{type}' in widget '{name}'"); } if (WidgetLookup.ContainsKey(name)) { throw new Exception($"Two or more widgets with the same were found: '{name}'"); } WidgetLookup.Add(name, w); int X = 0; int Y = 0; int Width = -1; int Height = -1; string text = null; int wvalue = 0; int? min_value = null; int? max_value = null; Font font = null; List <ListItem> items = null; int idx = -1; bool enabled = true; List <ClickAction> clickactions = new List <ClickAction>(); foreach (string key in config.Keys) { if (key == "type") { continue; } object value = config[key]; switch (key) { case "x": EnsureType(typeof(long), value, "x"); X = Convert.ToInt32(value); break; case "y": EnsureType(typeof(long), value, "y"); Y = Convert.ToInt32(value); break; case "width": EnsureType(typeof(long), value, "width"); Width = Convert.ToInt32(value); if (Width < 1) { throw new Exception($"Widget width ({Width}) must be greater than 0."); } break; case "height": EnsureType(typeof(long), value, "height"); Height = Convert.ToInt32(value); if (Height < 1) { throw new Exception($"Widget height ({Height}) must be greater than 0."); } break; case "font": if (type != "label" && type != "textbox" && type != "button" && type != "dropdown" && type != "checkbox" && type != "radiobox" && type != "multitextbox" && type != "multilabel") { throw new Exception($"Widget type ({type}) can not contain a 'font' field."); } if (value is string) { if (!Fonts.ContainsKey((string)value)) { throw new Exception($"Undefined font name '{(string) value}"); } font = Fonts[(string)value]; } else if (value is JObject) { font = ParseFont(((JObject)value).ToObject <Dictionary <string, object> >()); } else { throw new Exception($"Expected a String or Object, but got {value.GetType().Name} in 'font'"); } break; case "value": if (type != "numeric") { throw new Exception($"Widget type ({type}) can not contain a 'value' field."); } EnsureType(typeof(long), value, "value"); wvalue = Convert.ToInt32(value); break; case "min_value": if (type != "numeric") { throw new Exception($"Widget type ({type}) can not contain a 'min_value' field."); } EnsureType(typeof(long), value, "min_value"); min_value = Convert.ToInt32(value); break; case "max_value": if (type != "numeric") { throw new Exception($"Widget type ({type}) can not contain a 'max_value' field."); } EnsureType(typeof(long), value, "max_value"); max_value = Convert.ToInt32(value); break; case "text": if (type != "label" && type != "textbox" && type != "button" && type != "checkbox" && type != "radiobox" && type != "multitextbox" && type != "multilabel") { throw new Exception($"Widget type ({type}) can not contain a 'text' field."); } EnsureType(typeof(string), value, "text"); text = (string)value; break; case "items": if (type != "dropdown") { throw new Exception($"Widget type ({type}) can not contain an 'items' field."); } if (!(value is JArray)) { throw new Exception($"Expected an Array, but got {value.GetType().Name} in key 'items'"); } List <object> ary = ((JArray)value).ToObject <List <object> >(); items = new List <ListItem>(); foreach (object o in ary) { EnsureType(typeof(string), o, "items"); items.Add(new ListItem((string)o)); } break; case "index": if (type != "dropdown") { throw new Exception($"Widget type ({type}) can not contain an 'index' field."); } EnsureType(typeof(long), value, "index"); idx = Convert.ToInt32(value); if (idx < 0) { throw new Exception($"Index field must be greater than or equal to 0."); } break; case "enabled": if (type != "label" && type != "dropdown" && type != "switch_picker" && type != "variable_picker" && type != "checkbox" && type != "radiobox" && type != "textbox" && type != "numeric" && type != "button" && type != "multilabel") { throw new Exception($"Widget type ({type}) can not contain an 'enabled' field."); } EnsureType(typeof(bool), value, "enabled"); enabled = Convert.ToBoolean(value); break; case "widgets": if (!(value is JObject)) { throw new Exception($"Expected an Object, but got a {value.GetType().Name} in key 'widgets'"); } ParseWidgets(w, ((JObject)value).ToObject <Dictionary <string, object> >()); break; case "clicked": if (type != "radiobox" && type != "dropdown") { throw new Exception($"Widget type ({type}) can not contain a 'clicked' field."); } if (!(value is JArray)) { throw new Exception($"Expected an Array, but got {value.GetType().Name} in key 'clicked'"); } List <object> clickdata = ((JArray)value).ToObject <List <object> >(); for (int i = 0; i < clickdata.Count; i++) { object action = clickdata[i]; if (!(action is JObject)) { throw new Exception($"Expected an Object, but got {action.GetType().Name} in key 'clicked', element {i}"); } Dictionary <string, object> actionobject = ((JObject)action).ToObject <Dictionary <string, object> >(); if (!actionobject.ContainsKey("action")) { throw new Exception($"Click definition in 'clicked', element {i} must contain an 'action' key."); } string actiontype = null; List <object> actionparams = new List <object>(); string actioncondition = null; foreach (string actionkey in actionobject.Keys) { object actionvalue = actionobject[actionkey]; switch (actionkey) { case "action": EnsureType(typeof(string), actionvalue, "action"); actiontype = (string)actionvalue; if (actiontype != "enable" && actiontype != "disable" && actiontype != "check" && actiontype != "uncheck" && actiontype != "set") { throw new Exception($"Unknown action type '{actiontype}'."); } break; case "parameter": if (!(actionvalue is string) && !(actionvalue is JArray)) { throw new Exception($"Expected a string or Array, but got {actionvalue.GetType().Name} in key 'parameter'."); } if (actionvalue is string) { actionparams.Add((string)actionvalue); } else { List <object> paramlist = ((JArray)actionvalue).ToObject <List <object> >(); foreach (object paramvalue in paramlist) { actionparams.Add(paramvalue); } } break; case "condition": EnsureType(typeof(string), actionvalue, "condition"); actioncondition = (string)actionvalue; break; default: throw new Exception($"Unknown key '{actionkey}' inside 'clicked' definition"); } } clickactions.Add(new ClickAction(actiontype, actionparams, actioncondition)); } break; default: throw new Exception($"Unknown key '{key}' inside widget definition"); } } EnabledLookup.Add(name, enabled); w.SetPosition(X, Y); if (Width != -1 && Height != -1) { w.SetSize(Width, Height); } else if (Width != -1) { w.SetWidth(Width); } else if (Height != -1) { w.SetHeight(Height); } if (type == "label") { if (!string.IsNullOrEmpty(text)) { ((DynamicLabel)w).SetText(text); } if (font != null) { ((DynamicLabel)w).SetFont(font); } //((DynamicLabel) w).SetParser(this); ((DynamicLabel)w).SetColors(ConditionParser.Colors); ((DynamicLabel)w).SetEnabled(enabled); } if (type == "multilabel") { if (!string.IsNullOrEmpty(text)) { ((MultilineDynamicLabel)w).SetText(text); } if (font != null) { ((MultilineDynamicLabel)w).SetFont(font); } //((MultilineDynamicLabel) w).SetParser(this); ((MultilineDynamicLabel)w).SetColors(ConditionParser.Colors); ((MultilineDynamicLabel)w).SetEnabled(enabled); } if (type == "textbox") { if (!string.IsNullOrEmpty(text)) { ((TextBox)w).SetInitialText(text); } if (font != null) { ((TextBox)w).TextArea.SetFont(font); } ((TextBox)w).OnTextChanged += Save; } if (type == "numeric") { ((NumericBox)w).SetValue(wvalue); if (min_value != null) { ((NumericBox)w).MinValue = (int)min_value; } if (max_value != null) { ((NumericBox)w).MaxValue = (int)max_value; } ((NumericBox)w).OnValueChanged += Save; } if (type == "button") { if (!string.IsNullOrEmpty(text)) { ((Button)w).SetText(text); } if (font != null) { ((Button)w).SetFont(font); } } if (type == "dropdown") { if (items == null && idx != -1 || items != null && idx >= items.Count) { throw new Exception($"Index cannot be greater than or equal to the total item size."); } if (items != null) { ((DropdownBox)w).SetItems(items); } if (idx != -1) { ((DropdownBox)w).SetSelectedIndex(idx); } if (font != null) { ((DropdownBox)w).SetFont(font); } ((DropdownBox)w).OnSelectionChanged += Save; ((DropdownBox)w).OnSelectionChanged += delegate(BaseEventArgs e) { EvaluateAction(clickactions); }; } if (type == "checkbox") { if (font != null) { ((CheckBox)w).SetFont(font); } if (!string.IsNullOrEmpty(text)) { ((CheckBox)w).SetText(text); } } if (type == "radiobox") { if (font != null) { ((RadioBox)w).SetFont(font); } if (!string.IsNullOrEmpty(text)) { ((RadioBox)w).SetText(text); } ((RadioBox)w).OnCheckChanged += delegate(BaseEventArgs e) { if (((RadioBox)w).Checked) { EvaluateAction(clickactions); } }; } if (type == "switch_picker") { ((GameSwitchBox)w).OnSwitchChanged += Save; } if (type == "variable_picker") { ((GameVariableBox)w).OnVariableChanged += Save; } if (type == "multitextbox") { if (font != null) { ((MultilineTextBox)w).SetFont(font); } if (!string.IsNullOrEmpty(text)) { ((MultilineTextBox)w).SetText(text); } } SetWidgetEnabled(w, enabled); } }
public Dictionary <string, Widget> ParseWidgets(IContainer ParentWidget, Dictionary <string, object> Data) { Dictionary <string, Widget> Widgets = new Dictionary <string, Widget>(); foreach (string name in Data.Keys) { if (!(Data[name] is JObject)) { throw new Exception($"Expected an Object, but got {Data[name].GetType().Name} in key '{name}' inside 'widgets'"); } Dictionary <string, object> config = ((JObject)Data[name]).ToObject <Dictionary <string, object> >(); if (!config.ContainsKey("type")) { throw new Exception($"Widget definition must contain a 'type' key in widget '{name}'"); } if (!(config["type"] is string)) { throw new Exception($"Widget type must be a string."); } Widget w = null; string type = (string)config["type"]; switch (type) { case "label": w = new Label(ParentWidget); break; case "numeric": w = new NumericBox(ParentWidget); break; case "textbox": w = new TextBox(ParentWidget); break; case "button": w = new Button(ParentWidget); break; case "dropdown": w = new DropdownBox(ParentWidget); break; default: throw new Exception($"Unknown widget type '{type}' in widget '{name}'"); } if (WidgetLookup.ContainsKey(name)) { throw new Exception($"Two or more widgets with the same were found: '{name}'"); } WidgetLookup.Add(name, w); int X = 0; int Y = 0; int Width = -1; int Height = -1; string text = null; int wvalue = 0; int? min_value = null; int? max_value = null; Font font = null; List <ListItem> items = null; int idx = -1; foreach (string key in config.Keys) { if (key == "type") { continue; } object value = config[key]; switch (key) { case "x": EnsureType(typeof(long), value, "x"); X = Convert.ToInt32(value); break; case "y": EnsureType(typeof(long), value, "y"); Y = Convert.ToInt32(value); break; case "width": EnsureType(typeof(long), value, "width"); Width = Convert.ToInt32(value); if (Width < 1) { throw new Exception($"Widget width ({Width}) must be greater than 0."); } break; case "height": EnsureType(typeof(long), value, "height"); Height = Convert.ToInt32(value); if (Height < 1) { throw new Exception($"Widget height ({Height}) must be greater than 0."); } break; case "font": if (type != "label" && type != "textbox" && type != "button" && type != "dropdown") { throw new Exception($"Widget type ({type}) can not contain a 'font' field."); } if (value is string) { if (!Fonts.ContainsKey((string)value)) { throw new Exception($"Undefined font name '{(string) value}"); } font = Fonts[(string)value]; } else if (value is JObject) { font = ParseFont(((JObject)value).ToObject <Dictionary <string, object> >()); } else { throw new Exception($"Expected a String or Object, but got {value.GetType().Name} in 'font'"); } break; case "value": if (type != "numeric") { throw new Exception($"Widget type ({type}) can not contain a 'value' field."); } EnsureType(typeof(long), value, "value"); wvalue = Convert.ToInt32(value); break; case "min_value": if (type != "numeric") { throw new Exception($"Widget type ({type}) can not contain a 'min_value' field."); } EnsureType(typeof(long), value, "min_value"); min_value = Convert.ToInt32(value); break; case "max_value": if (type != "numeric") { throw new Exception($"Widget type ({type}) can not contain a 'max_value' field."); } EnsureType(typeof(long), value, "max_value"); max_value = Convert.ToInt32(value); break; case "text": if (type != "label" && type != "textbox" && type != "button") { throw new Exception($"Widget type ({type}) can not contain a 'text' field."); } EnsureType(typeof(string), value, "text"); text = (string)value; break; case "items": if (type != "dropdown") { throw new Exception($"Widget type ({type}) can not contain an 'items' field."); } if (!(value is JArray)) { throw new Exception($"Expected an Array, but got {value.GetType().Name} in key 'items'"); } List <object> ary = ((JArray)value).ToObject <List <object> >(); items = new List <ListItem>(); foreach (object o in ary) { EnsureType(typeof(string), o, "items"); items.Add(new ListItem((string)o)); } break; case "index": if (type != "dropdown") { throw new Exception($"Widget type ({type}) can not contain an 'index' field."); } EnsureType(typeof(long), value, "index"); idx = Convert.ToInt32(value); if (idx < 0) { throw new Exception($"Index field must be greater than or equal to 0."); } break; case "clicked": if (type != "button") { throw new Exception($"Widget type ({type}) can not contain a 'clicked' field."); } if (!(value is JObject)) { throw new Exception($"Expected an Object, but got {value.GetType().Name} in key 'clicked'"); } Dictionary <string, object> clickdata = ((JObject)value).ToObject <Dictionary <string, object> >(); if (!clickdata.ContainsKey("type")) { throw new Exception("'clicked' definition must contain a 'type' key."); } if (!(clickdata["type"] is string)) { throw new Exception($"Expected a String, but got {clickdata["type"].GetType().Name} in key 'type'"); } string clicktype = (string)clickdata["type"]; bool save = false; bool close = false; foreach (string clickkey in clickdata.Keys) { if (clickkey == "type") { continue; } object clickvalue = clickdata[clickkey]; switch (clickkey) { case "action": EnsureType(typeof(string), clickvalue, "action"); string action = (string)clickvalue; switch (action) { case "save": save = true; break; case "close": close = true; break; case "save_and_close": save = true; close = true; break; default: throw new Exception($"Unknown value '{action}' inside 'action' definition."); } break; default: throw new Exception($"Unknown key '{key}' inside 'clicked' definition"); } } ((Button)w).OnClicked += delegate(BaseEventArgs e) { if (save) { Save(); } if (close) { Window.Close(); } }; break; default: throw new Exception($"Unknown key '{key}' inside widget definition"); } w.SetPosition(X, Y); if (Width != -1) { w.SetWidth(Width); } if (Height != -1) { w.SetHeight(Height); } if (type == "label") { if (!string.IsNullOrEmpty(text)) { ((Label)w).SetText(text); } if (font != null) { ((Label)w).SetFont(font); } } if (type == "textbox") { if (!string.IsNullOrEmpty(text)) { ((TextBox)w).SetInitialText(text); } if (font != null) { ((TextBox)w).TextArea.SetFont(font); } } if (type == "numeric") { ((NumericBox)w).SetValue(wvalue); if (min_value != null) { ((NumericBox)w).MinValue = (int)min_value; } if (max_value != null) { ((NumericBox)w).MaxValue = (int)max_value; } } if (type == "button") { if (!string.IsNullOrEmpty(text)) { ((Button)w).SetText(text); } if (font != null) { ((Button)w).SetFont(font); } } if (type == "dropdown") { if (items == null && idx != -1 || items != null && idx >= items.Count) { throw new Exception($"Index cannot be greater than or equal to the total item size."); } if (items != null) { ((DropdownBox)w).SetItems(items); } if (idx != -1) { ((DropdownBox)w).SetSelectedIndex(idx); } if (font != null) { ((DropdownBox)w).SetFont(font); } } } Widgets.Add(name, w); } return(Widgets); }