private Grid CreateFileEditor <T>(Record record, bool hasSetter, string filter) { if (Settings.AssetManager == null) { return(null); } var propertyType = record.Type; var value = record.GetValue(_object); var subGrid = new Grid { ColumnSpacing = 8, HorizontalAlignment = HorizontalAlignment.Stretch }; subGrid.ColumnsProportions.Add(new Proportion(ProportionType.Fill)); subGrid.ColumnsProportions.Add(new Proportion()); var baseObject = _object as BaseObject; var path = string.Empty; if (baseObject != null) { baseObject.Resources.TryGetValue(record.Name, out path); } var textBox = new TextBox { Text = path }; subGrid.Widgets.Add(textBox); var button = new ImageTextButton { Text = "Change...", ContentHorizontalAlignment = HorizontalAlignment.Center, Tag = value, HorizontalAlignment = HorizontalAlignment.Stretch, GridColumn = 1 }; subGrid.Widgets.Add(button); if (hasSetter) { button.Click += (sender, args) => { var dlg = new FileDialog(FileDialogMode.OpenFile) { Filter = filter }; if (!string.IsNullOrEmpty(textBox.Text)) { var filePath = textBox.Text; if (!Path.IsPathRooted(filePath) && !string.IsNullOrEmpty(Settings.BasePath)) { filePath = Path.Combine(Settings.BasePath, filePath); } dlg.FilePath = filePath; } else if (!string.IsNullOrEmpty(Settings.BasePath)) { dlg.Folder = Settings.BasePath; } dlg.Closed += (s, a) => { if (!dlg.Result) { return; } try { var newValue = Settings.AssetManager.Load <T>(dlg.FilePath); var filePath = dlg.FilePath; if (!string.IsNullOrEmpty(Settings.BasePath)) { filePath = PathUtils.TryToMakePathRelativeTo(filePath, Settings.BasePath); } textBox.Text = filePath; SetValue(record, _object, newValue); if (baseObject != null) { baseObject.Resources[record.Name] = filePath; } FireChanged(propertyType.Name); } catch (Exception) { } }; dlg.ShowModal(); }; } else { button.Enabled = false; } return(subGrid); }
private TextBox CreateStringEditor(Record record, bool hasSetter) { var propertyType = record.Type; var value = record.GetValue(_object); var tf = new TextBox { Text = value != null?value.ToString() : string.Empty }; if (hasSetter) { tf.TextChanged += (sender, args) => { try { object result; if (propertyType.IsNullablePrimitive()) { if (string.IsNullOrEmpty(tf.Text)) { result = null; } else { result = Convert.ChangeType(tf.Text, record.Type.GetNullableType()); } } else { result = Convert.ChangeType(tf.Text, record.Type); } SetValue(record, _object, result); if (record.Type.IsValueType) { var tg = this; var pg = tg._parentGrid; while (pg != null && tg._parentProperty != null) { tg._parentProperty.SetValue(pg._object, tg._object); if (!tg._parentProperty.Type.IsValueType) { break; } tg = pg; pg = tg._parentGrid; } } FireChanged(record.Name); } catch (Exception) { // TODO: Rework this ugly type conversion solution } }; } else { tf.Enabled = false; } return(tf); }
private SpinButton CreateNumericEditor(Record record, bool hasSetter) { var propertyType = record.Type; var value = record.GetValue(_object); var numericType = propertyType; if (propertyType.IsNullablePrimitive()) { numericType = propertyType.GetNullableType(); } var spinButton = new SpinButton { Integer = numericType.IsNumericInteger(), Nullable = propertyType.IsNullablePrimitive(), Value = value != null ? (float)Convert.ChangeType(value, typeof(float)) : default(float?) }; if (hasSetter) { spinButton.ValueChanged += (sender, args) => { try { object result; if (spinButton.Value != null) { result = Convert.ChangeType(spinButton.Value.Value, numericType); } else { result = null; } SetValue(record, _object, result); if (record.Type.IsValueType) { // Handle structs var tg = this; var pg = tg._parentGrid; while (pg != null && tg._parentProperty != null && tg._parentProperty.Type.IsValueType) { tg._parentProperty.SetValue(pg._object, tg._object); if (!tg._parentProperty.Type.IsValueType) { break; } tg = pg; pg = tg._parentGrid; } } FireChanged(record.Name); } catch (InvalidCastException) { // TODO: Rework this ugly type conversion solution } catch (Exception ex) { spinButton.Value = args.OldValue; var dialog = Dialog.CreateMessageBox("Error", ex.ToString()); dialog.ShowModal(); } }; } else { spinButton.Enabled = false; } return(spinButton); }
public SubGrid(PropertyGrid parent, object value, string header, string category, Record parentProperty) { InternalChild = new Grid { ColumnSpacing = 4, RowSpacing = 4 }; InternalChild.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); InternalChild.ColumnsProportions.Add(new Proportion(ProportionType.Fill)); InternalChild.RowsProportions.Add(new Proportion(ProportionType.Auto)); InternalChild.RowsProportions.Add(new Proportion(ProportionType.Auto)); _propertyGrid = new PropertyGrid(parent.PropertyGridStyle, category, parentProperty, parent) { Object = value, Visible = false, HorizontalAlignment = HorizontalAlignment.Stretch, GridColumn = 1, GridRow = 1 }; // Mark _mark = new ImageButton(null) { Toggleable = true, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center }; _mark.ApplyImageButtonStyle(parent.PropertyGridStyle.MarkStyle); InternalChild.Widgets.Add(_mark); _mark.PressedChanged += (sender, args) => { if (_mark.IsPressed) { _propertyGrid.Visible = true; parent._expandedCategories.Add(category); } else { _propertyGrid.Visible = false; parent._expandedCategories.Remove(category); } }; var expanded = true; if (parentProperty != null && parentProperty.FindAttribute <DesignerFoldedAttribute>() != null) { expanded = false; } if (expanded) { _mark.IsPressed = true; } var label = new Label(null) { Text = header, GridColumn = 1 }; label.ApplyLabelStyle(parent.PropertyGridStyle.LabelStyle); InternalChild.Widgets.Add(label); InternalChild.Widgets.Add(_propertyGrid); HorizontalAlignment = HorizontalAlignment.Stretch; VerticalAlignment = VerticalAlignment.Stretch; }
private Grid CreateBrushEditor(Record record, bool hasSetter) { var propertyType = record.Type; var value = record.GetValue(_object) as SolidBrush; var subGrid = new Grid { ColumnSpacing = 8, HorizontalAlignment = HorizontalAlignment.Stretch }; subGrid.ColumnsProportions.Add(new Proportion()); subGrid.ColumnsProportions.Add(new Proportion(ProportionType.Fill)); var color = Color.Transparent; if (value != null) { color = value.Color; } var image = new Image { Renderable = DefaultAssets.WhiteRegion, VerticalAlignment = VerticalAlignment.Center, Width = 32, Height = 16, Color = color }; subGrid.Widgets.Add(image); var button = new ImageTextButton { Text = "Change...", ContentHorizontalAlignment = HorizontalAlignment.Center, Tag = value, HorizontalAlignment = HorizontalAlignment.Stretch, GridColumn = 1 }; subGrid.Widgets.Add(button); if (hasSetter) { button.Click += (sender, args) => { var dlg = new ColorPickerDialog() { Color = image.Color }; dlg.Closed += (s, a) => { if (!dlg.Result) { return; } image.Color = dlg.Color; SetValue(record, _object, new SolidBrush(dlg.Color)); var baseObject = _object as BaseObject; if (baseObject != null) { baseObject.Resources[record.Name] = dlg.Color.ToHexString(); } FireChanged(propertyType.Name); }; dlg.ShowModal(); }; } else { button.Enabled = false; } return(subGrid); }