Guest User

Untitled

a guest
Dec 19th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. public class SelectableCube : UIElement3D
  2. {
  3. public Point3D Center
  4. {
  5. get { return (Point3D)GetValue(CenterProperty); }
  6. set { SetValue(CenterProperty, value);
  7. }
  8. }
  9.  
  10. // Using a DependencyProperty as the backing store for Center. This enables animation, styling, binding, etc...
  11. public static readonly DependencyProperty CenterProperty =
  12. DependencyProperty.Register("Center", typeof(Point3D), typeof(SelectableCube),new PropertyMetadata(CenterChangeCallback));
  13.  
  14. private static void CenterChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  15. {
  16. ((SelectableCube)d).SetCurrentValue(CenterProperty, (Point3D)e.NewValue);
  17. MeshBuilder builder = new MeshBuilder();
  18. builder.AddBox(((SelectableCube)d).Center, 1, 1, 1);
  19. GeometryModel3D model = new GeometryModel3D(builder.ToMesh(), MaterialHelper.CreateMaterial(new SolidColorBrush(Color.FromRgb(255, 0, 0))));
  20. ((SelectableCube)d).Visual3DModel = model;
  21. }
  22.  
  23. public SelectableCube()
  24. {
  25. }
  26.  
  27. protected override void OnMouseEnter(MouseEventArgs e)
  28. {
  29. base.OnMouseEnter(e);
  30. GeometryModel3D g = Visual3DModel as GeometryModel3D;
  31. if (!g.Material.Equals(Materials.Gold))
  32. {
  33. g.Material = Materials.Gray;
  34. }
  35. e.Handled = true;
  36. }
  37.  
  38. protected override void OnMouseLeave(MouseEventArgs e)
  39. {
  40. base.OnMouseLeave(e);
  41. GeometryModel3D g = Visual3DModel as GeometryModel3D;
  42.  
  43. if (!g.Material.Equals(Materials.Gold))
  44. {
  45. g.Material = Materials.LightGray;
  46. }
  47. e.Handled = true;
  48. }
  49.  
  50. protected override void OnMouseDown(MouseButtonEventArgs e)
  51. {
  52. base.OnMouseDown(e);
  53. GeometryModel3D g = Visual3DModel as GeometryModel3D;
  54. if (g.Material.Equals(Materials.Gold))
  55. {
  56. g.Material = Materials.Gray;
  57. }
  58. else
  59. {
  60. g.Material = Materials.Gold;
  61. }
  62. e.Handled = true;
  63. }
  64. }
  65.  
  66. public partial class MainWindow : Window
  67. {
  68.  
  69. public Visual3DCollection Cubes { get; set; }
  70.  
  71. public MainWindow()
  72. {
  73. InitializeComponent();
  74. this.MainViewport.MouseDown += MainViewport_MouseDown;
  75. ContainerUIElement3D col = this.MainViewport.Children.FirstOrDefault(x => x.GetType().Equals(typeof(ContainerUIElement3D))) as ContainerUIElement3D;
  76. ModelVisual3D cubeContainer = col.Children[0] as ModelVisual3D;
  77. Cubes = cubeContainer.Children;
  78. Cubes.Add(new SelectableCube() { Center = new Point3D(1,1,1)});
  79. }
  80.  
  81. private void MainViewport_MouseDown(object sender, MouseButtonEventArgs e)
  82. {
  83. Point pointerLocation = e.GetPosition(this.MainViewport);
  84. HitTestResult res = VisualTreeHelper.HitTest(this.MainViewport, pointerLocation);
  85. }
  86.  
  87. private void ColSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  88. {
  89. Redraw();
  90. }
  91.  
  92. private void RowSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  93. {
  94. Redraw();
  95. }
  96.  
  97. private void Redraw()
  98. {
  99. Cubes.Clear();
  100. for (int row = 1; row <= this.RowSlider.Value; row++)
  101. {
  102. for (int col = 1; col <= this.ColSlider.Value; col++)
  103. {
  104. SelectableCube c = new SelectableCube() { Center = new Point3D(row + (row - 1) * this.DistanceSlider.Value, col + (col - 1) + this.DistanceSlider.Value, 5) };
  105. Cubes.Add(c);
  106. }
  107. }
  108.  
  109. }
  110. }
  111.  
  112. <Window x:Class="HelixTest.MainWindow"
  113. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  114. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  115. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  116. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  117. xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
  118. xmlns:local="clr-namespace:HelixTest"
  119. mc:Ignorable="d"
  120. Title="MainWindow" Height="350" Width="525">
  121. <Grid>
  122.  
  123. <Grid.RowDefinitions>
  124. <RowDefinition></RowDefinition>
  125. </Grid.RowDefinitions>
  126. <Grid.ColumnDefinitions>
  127. <ColumnDefinition></ColumnDefinition>
  128. <ColumnDefinition></ColumnDefinition>
  129. </Grid.ColumnDefinitions>
  130. <HelixToolkit:HelixViewport3D Grid.Column="0" ZoomExtentsWhenLoaded="True" Name="MainViewport">
  131. <HelixToolkit:SunLight/>
  132. <ContainerUIElement3D>
  133. <ModelVisual3D/>
  134. </ContainerUIElement3D>
  135. <HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
  136. </HelixToolkit:HelixViewport3D>
  137. <StackPanel Grid.Column="1" Orientation="Vertical">
  138. <StackPanel Orientation="Horizontal">
  139. <Label Content="# Columns:"/>
  140. <Slider Name="ColSlider" Width="120" Maximum="100"
  141. TickFrequency="1" IsSnapToTickEnabled="True"
  142. ValueChanged="ColSlider_ValueChanged"></Slider>
  143. <Label Content="{Binding ElementName=ColSlider, Path=Value}"></Label>
  144. </StackPanel>
  145. <StackPanel Orientation="Horizontal">
  146. <Label Content="# Rows:"/>
  147. <Slider Name="RowSlider" Width="120" Maximum="100"
  148. TickFrequency="1" IsSnapToTickEnabled="True"
  149. ValueChanged="RowSlider_ValueChanged"></Slider>
  150. <Label Content="{Binding ElementName=RowSlider, Path=Value}"></Label>
  151. </StackPanel>
  152. <StackPanel Orientation="Horizontal">
  153. <Label Content="# Distance:"/>
  154. <Slider Name="DistanceSlider" Width="120" Maximum="2"
  155. TickFrequency="0.1" IsSnapToTickEnabled="True"
  156. ValueChanged="DistanceSlider_ValueChanged"></Slider>
  157. <Label Content="{Binding ElementName=DistanceSlider, Path=Value}"></Label>
  158. </StackPanel>
  159. </StackPanel>
  160. </Grid>
Add Comment
Please, Sign In to add comment