private static CodeExpression CreateDefaultValue(Type type, object defaultValue) { if (defaultValue == null) { return(new CodePrimitiveExpression(null)); } var codeTypeReference = CreateCodeTypeReference(type); if (defaultValue.GetType().IsPrimitive) { return(new CodePrimitiveExpression(Convert.ChangeType(defaultValue, type))); } if (defaultValue is Point || defaultValue is Vector || defaultValue is Size) { var point = (Point)RegisterValueConverter.ConvertToUsualType(defaultValue); return(new CodeObjectCreateExpression( codeTypeReference, new CodePrimitiveExpression(point.X), new CodePrimitiveExpression(point.Y))); } if (defaultValue is Point3D || defaultValue is Vector3D) { var point3D = (Point3D)RegisterValueConverter.ConvertToUsualType(defaultValue); return(new CodeObjectCreateExpression( codeTypeReference, new CodePrimitiveExpression(point3D.X), new CodePrimitiveExpression(point3D.Y), new CodePrimitiveExpression(point3D.Z))); } if (defaultValue is Point4D point4D) { return(new CodeObjectCreateExpression( codeTypeReference, new CodePrimitiveExpression(point4D.X), new CodePrimitiveExpression(point4D.Y), new CodePrimitiveExpression(point4D.Z), new CodePrimitiveExpression(point4D.W))); } if (defaultValue is Color color) { return(new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(codeTypeReference), "FromArgb", new CodePrimitiveExpression(color.A), new CodePrimitiveExpression(color.R), new CodePrimitiveExpression(color.G), new CodePrimitiveExpression(color.B))); } return(new CodeDefaultValueExpression(codeTypeReference)); }
private static CodeExpression CreateDefaultValue(object defaultValue) { if (defaultValue == null) { return(new CodePrimitiveExpression(null)); } else { CodeTypeReference codeTypeReference = CreateCodeTypeReference(defaultValue.GetType()); if (defaultValue.GetType().IsPrimitive) { return(new CodeCastExpression(codeTypeReference, new CodePrimitiveExpression(defaultValue))); } else if (defaultValue is Point || defaultValue is Vector || defaultValue is Size) { Point point = (Point)RegisterValueConverter.ConvertToUsualType(defaultValue); return(new CodeObjectCreateExpression(codeTypeReference, new CodePrimitiveExpression(point.X), new CodePrimitiveExpression(point.Y))); } else if (defaultValue is Point3D || defaultValue is Vector3D) { Point3D point3D = (Point3D)RegisterValueConverter.ConvertToUsualType(defaultValue); return(new CodeObjectCreateExpression(codeTypeReference, new CodePrimitiveExpression(point3D.X), new CodePrimitiveExpression(point3D.Y), new CodePrimitiveExpression(point3D.Z))); } else if (defaultValue is Point4D) { Point4D point4D = (Point4D)defaultValue; return(new CodeObjectCreateExpression(codeTypeReference, new CodePrimitiveExpression(point4D.X), new CodePrimitiveExpression(point4D.Y), new CodePrimitiveExpression(point4D.Z), new CodePrimitiveExpression(point4D.W))); } else if (defaultValue is Color) { Color color = (Color)defaultValue; return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(codeTypeReference), "FromArgb", new CodePrimitiveExpression(color.A), new CodePrimitiveExpression(color.R), new CodePrimitiveExpression(color.G), new CodePrimitiveExpression(color.B))); } else { return(new CodeDefaultValueExpression(codeTypeReference)); } } }
private void GenerateShaderInputControl(ShaderModelConstantRegister register) { string toolTipText = String.IsNullOrEmpty(register.Description) ? null : register.Description; TextBlock textBlock = new TextBlock { Foreground = Brushes.White, Margin = new Thickness(5), Inlines = { new Run { Foreground = (Brush)Application.Current.FindResource("HighlightBrush"), Text = register.RegisterName }, new Run { Text = String.Format(" : {0}", register.RegisterType.Name) }, }, ToolTip = toolTipText }; inputControlPanel.Children.Add(textBlock); Control control = null; if (register.RegisterType == typeof(Brush)) { control = new TexturePicker(register); } else if (register.RegisterType == typeof(double) || register.RegisterType == typeof(float)) { double minValue = Convert.ToDouble(register.MinValue); double maxValue = Convert.ToDouble(register.MaxValue); //double defaultValue = Double.Parse(register.DefaultValue.ToString(), NumberStyles.Any, null); double defaultValue = Convert.ToDouble(register.DefaultValue); control = new AdjustableSlider { Minimum = Math.Min(minValue, defaultValue), Maximum = Math.Max(maxValue, defaultValue), Value = defaultValue }; } else if (register.RegisterType == typeof(Point) || register.RegisterType == typeof(Vector) || register.RegisterType == typeof(Size)) { Point minValue = (Point)RegisterValueConverter.ConvertToUsualType(register.MinValue); Point maxValue = (Point)RegisterValueConverter.ConvertToUsualType(register.MaxValue); Point defaultValue = (Point)RegisterValueConverter.ConvertToUsualType(register.DefaultValue); control = new AdjustableSliderPair { Minimum = new Point(Math.Min(minValue.X, defaultValue.X), Math.Min(minValue.Y, defaultValue.Y)), Maximum = new Point(Math.Max(maxValue.X, defaultValue.X), Math.Max(maxValue.Y, defaultValue.Y)), Value = defaultValue }; } else if (register.RegisterType == typeof(Point3D) || register.RegisterType == typeof(Vector3D)) { Point3D minValue = (Point3D)RegisterValueConverter.ConvertToUsualType(register.MinValue); Point3D maxValue = (Point3D)RegisterValueConverter.ConvertToUsualType(register.MaxValue); Point3D defaultValue = (Point3D)RegisterValueConverter.ConvertToUsualType(register.DefaultValue); control = new AdjustableSliderTriplet { Minimum = new Point3D(Math.Min(minValue.X, defaultValue.X), Math.Min(minValue.Y, defaultValue.Y), Math.Min(minValue.Z, defaultValue.Z)), Maximum = new Point3D(Math.Max(maxValue.X, defaultValue.X), Math.Max(maxValue.Y, defaultValue.Y), Math.Max(maxValue.Z, defaultValue.Z)), Value = defaultValue }; } else if (register.RegisterType == typeof(Color)) { Color defaultValue = (Color)register.DefaultValue; //control = new Telerik.Windows.Controls.RadColorEditor //{ // HorizontalAlignment = HorizontalAlignment.Left, // SelectedColor = defaultValue //}; control = new AdjustableColor { HorizontalAlignment = HorizontalAlignment.Left, Value = defaultValue }; // ((control) as AdjustableColor).; //control = new TextBox //{ // Background = Brushes.LightYellow, // Width = 150, // HorizontalAlignment = HorizontalAlignment.Left, // Text = defaultValue.ToString() //}; } else if (register.RegisterType == typeof(Point4D)) { Point4D minValue = (Point4D)register.MinValue; Point4D maxValue = (Point4D)register.MaxValue; Point4D defaultValue = (Point4D)register.DefaultValue; control = new AdjustableSliderQuadruplet { Minimum = new Point4D(Math.Min(minValue.X, defaultValue.X), Math.Min(minValue.Y, defaultValue.Y), Math.Min(minValue.Z, defaultValue.Z), Math.Min(minValue.W, defaultValue.W)), Maximum = new Point4D(Math.Max(maxValue.X, defaultValue.X), Math.Max(maxValue.Y, defaultValue.Y), Math.Max(maxValue.Z, defaultValue.Z), Math.Max(maxValue.W, defaultValue.W)), Value = defaultValue }; } if (control != null) { control.Margin = new Thickness(15, 2, 25, 5); control.ToolTip = toolTipText; this.inputControlPanel.Children.Add(control); register.AffiliatedControl = control; } }