/SketchModeller/SketchModeller.Modelling/Views/NewSphereViewModel.cs

https://bitbucket.org/alexshtf/sketchmodeller · C# · 140 lines · 117 code · 23 blank · 0 comment · 9 complexity · 123cb50ad90f88fae0d0907fb8cf68d5 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SketchModeller.Infrastructure.Data;
  6. using SketchModeller.Infrastructure.Shared;
  7. using SketchModeller.Infrastructure.Services;
  8. using Microsoft.Practices.Prism.Events;
  9. using System.Windows.Media.Media3D;
  10. using System.Diagnostics.Contracts;
  11. using System.Windows;
  12. using System.Windows.Input;
  13. using SketchModeller.Modelling.Editing;
  14. using Petzold.Media3D;
  15. namespace SketchModeller.Modelling.Views
  16. {
  17. class NewSphereViewModel : NewPrimitiveViewModel
  18. {
  19. private const ModifierKeys RADIUS_MODIFIER = ModifierKeys.Shift;
  20. public const double MIN_RADIUS = 0.005;
  21. private NewSphere model;
  22. private bool isUpdating;
  23. public NewSphereViewModel(
  24. UiState uiState = null,
  25. ICurveAssigner curveAssigner = null,
  26. IEventAggregator eventAggregator = null,
  27. IConstrainedOptimizer optimizer = null)
  28. : base(uiState, curveAssigner, eventAggregator, optimizer)
  29. {
  30. radius = 0.1;
  31. model = new NewSphere();
  32. model.Radius.Value = radius;
  33. }
  34. public void Init(NewSphere newSphere)
  35. {
  36. Contract.Requires(newSphere != null);
  37. Contract.Requires(newSphere.Radius > 0);
  38. Model = model = newSphere;
  39. UpdateFromModel();
  40. }
  41. public override void UpdateFromModel()
  42. {
  43. isUpdating = true;
  44. try
  45. {
  46. Center = model.Center;
  47. Radius = model.Radius;
  48. }
  49. finally
  50. {
  51. isUpdating = false;
  52. }
  53. }
  54. #region Radius property
  55. private double radius;
  56. public double Radius
  57. {
  58. get { return radius; }
  59. set
  60. {
  61. radius = value;
  62. RaisePropertyChanged(() => Radius);
  63. if (!isUpdating)
  64. model.Radius.Value = value;
  65. }
  66. }
  67. #endregion
  68. #region Center property
  69. private Point3D center;
  70. public Point3D Center
  71. {
  72. get { return center; }
  73. set
  74. {
  75. center = value;
  76. RaisePropertyChanged(() => Center);
  77. if (!isUpdating)
  78. model.Center.Value = value;
  79. }
  80. }
  81. #endregion
  82. public override IEditor StartEdit(Point startPos, LineRange startRay)
  83. {
  84. return new Editor(startPos, startRay, this);
  85. }
  86. public override Vector3D ApproximateAxis
  87. {
  88. get { return new Vector3D(0, 0, 0); }
  89. }
  90. #region Editor class
  91. class Editor : BaseEditor
  92. {
  93. private NewSphereViewModel viewModel;
  94. public Editor(Point startPoint, LineRange startRay, NewSphereViewModel viewModel)
  95. : base(startPoint, startRay, viewModel)
  96. {
  97. this.viewModel = viewModel;
  98. }
  99. protected override void PerformDrag(Vector dragVector2d, Vector3D vector3D, Vector3D axisDragVector, Point3D? currDragPosition)
  100. {
  101. if (Keyboard.Modifiers == ModifierKeys.None)
  102. viewModel.Center = viewModel.Center + vector3D;
  103. if (Keyboard.Modifiers == RADIUS_MODIFIER)
  104. {
  105. if (currDragPosition != null)
  106. {
  107. var fromCenter = currDragPosition.Value - viewModel.Center;
  108. fromCenter.Normalize();
  109. var radiusDelta = Vector3D.DotProduct(fromCenter, vector3D);
  110. viewModel.Radius = Math.Max(viewModel.Radius + radiusDelta, NewSphereViewModel.MIN_RADIUS);
  111. }
  112. }
  113. }
  114. }
  115. #endregion
  116. }
  117. }