private void LoadNode(object parent, object component) { // retrieve name var identiable = component as IIdentifiable; var name = identiable != null ? identiable.Id : component.GetType().Name; // retrieve parent node var container = parent == null ? _ui : _objectUI[parent]; // create node content var label = new Label(name, DataPack.Fonts.StudioFont); var node = container.Add(new TreeNode(label)); // create node view var view = _view.Add(new LabelView(label)); _labelViews.Add(label, view); /*var node = component as Node; if (node != null) { foreach (var child in node) { } }*/ }
public void Inspect(object item) { DisposeOfComponents(); if (item == null) return; var ui = Add(new StackPanel(0) { Orientation = Orientation.Vertical, Anchor = new Vector2(1, 0), Padding = new Margin(5, 5), HorizontalAlignment = HorizontalAlignment.Right, }); var view = Add(new Node()); var state = Add(new Node()); // header var header = ui.Add(new StackPanel { Orientation = Orientation.Horizontal, Padding = new Margin(0, 5)}); view.Add(new BackgroundView(ui)); // icon var image = header.Add(new Image(DataPack.Textures.Studio.Info) { Size = new Size(32), Margin = new Margin(2, 0) }); view.Add(new ImageView(image)); // item name var node = item as Node; var name = (node != null ? (string.IsNullOrEmpty(node.Name)? node.GetType().Name : node.Name) : item.ToString()); var itemNameLabel = header.Add(new Label(name, DataPack.Fonts.StudioTitle) { Size = new Size(200, 32), VerticalAlignment = VerticalAlignment.Center, Margin = new Margin(0, 3) }); view.Add(new LabelView(itemNameLabel)); // grid var grid = ui.Add(new GridPanel()); int i = 0; var properties = item.GetType().GetProperties(); foreach (var property in properties) { // skip hidden properties var attributes = property.GetCustomAttributes(true); var isHidden = attributes.OfType<HiddenAttribute>().Any(); if (isHidden) continue; // skip read-only and write-only properties if(!property.CanWrite || !property.CanRead) return; UIControl control = null; var propertyValue = property.GetValue(item); if (propertyValue is Single) { float min = -100; float max = 100; var range = attributes.FirstOrDefault(a => a is RangeAttribute) as RangeAttribute; if (range != null) { min = range.Min; max = range.Max; } PercentChanged set = v => property.SetValue(item, v); Func<float> get = () => (float)property.GetValue(item); var slider = new Slider(set, get(), min, max) { Name = property.Name }; view.Add(new GaugeView(slider)); state.Add(new RecordableProperty<float>(property.Name, () => slider.Current, slider.SetCurrent)); control = slider; } else if (propertyValue is Int32) { float min = -100; float max = 100; var range = attributes.FirstOrDefault(a => a is RangeAttribute) as RangeAttribute; if (range != null) { min = range.Min; max = range.Max; } PercentChanged set = v => property.SetValue(item, (int)v); Func<int> get = () => (int)property.GetValue(item); var slider = new Slider(set, get(), min, max) { Name = property.Name }; view.Add(new GaugeView(slider)); state.Add(new RecordableProperty<float>(property.Name, () => slider.Current, slider.SetCurrent)); control = slider; } else if (propertyValue is Boolean) { ToggleValueChanged set = v => property.SetValue(item, v); Func<bool> get = () => (bool)property.GetValue(item); var cb = new ToggleButton(get(), set) { Name = property.Name }; view.Add(new CheckBoxView(cb)); state.Add(new RecordableProperty<bool>(property.Name, () => cb.Value, v => cb.Value = v)); control = cb; } else { var s = propertyValue as string; if (!string.IsNullOrEmpty(s)) { var label = new Label(s, DataPack.Fonts.StudioFont) { VerticalAlignment = VerticalAlignment.Bottom }; view.Add(new LabelView(label)); control = label; } } if (control != null) { var nameLabel = new Label(property.Name, DataPack.Fonts.StudioFont) { Margin = new Margin { Left = 5, Right = 20, Top = 2, Bottom = 5 } }; view.Add(new LabelView(nameLabel)); control.Margin = new Margin { Right = 5, Top = 5, Bottom = 5}; grid.Add(nameLabel, 0, i); grid.Add(control, 1, i); i++; } } }