public override void OnDragging(ViewControl vc, Point scrPt) { if (m_hitRegion == HitRegion.None || m_activeOp == null || m_activeOp.NodeList.Count == 0) { return; } Matrix4F view = vc.Camera.ViewMatrix; // compute world * view Matrix4F wv = new Matrix4F(); wv.Mul(HitMatrix, view); // create ray in view space. Ray3F rayV = vc.GetRay(scrPt, vc.Camera.ProjectionMatrix); Vec3F xAxis = wv.XAxis; Vec3F yAxis = wv.YAxis; Vec3F zAxis = wv.ZAxis; Vec3F origin = wv.Translation; m_scale = new Vec3F(1, 1, 1); float scale = 1; float a1, a2; switch (m_hitRegion) { case HitRegion.XAxis: case HitRegion.NegXAxis: { a1 = Math.Abs(Vec3F.Dot(HitRayV.Direction, yAxis)); a2 = Math.Abs(Vec3F.Dot(HitRayV.Direction, zAxis)); Vec3F axis = (a1 > a2 ? yAxis : zAxis); Vec3F p0 = HitRayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); Vec3F p1 = rayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); float dragAmount = Vec3F.Dot((p1 - p0), xAxis); if (m_hitRegion == HitRegion.NegXAxis) { dragAmount *= -1; } m_scale.X = 1.0f + dragAmount / m_hitScale; scale = m_scale.X; } break; case HitRegion.YAxis: case HitRegion.NegYAxis: { a1 = Math.Abs(Vec3F.Dot(HitRayV.Direction, zAxis)); a2 = Math.Abs(Vec3F.Dot(HitRayV.Direction, xAxis)); Vec3F axis = (a1 > a2 ? zAxis : xAxis); Vec3F p0 = HitRayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); Vec3F p1 = rayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); float dragAmount = Vec3F.Dot((p1 - p0), yAxis); if (m_hitRegion == HitRegion.NegYAxis) { dragAmount *= -1; } m_scale.Y = 1.0f + dragAmount / m_hitScale; scale = m_scale.Y; } break; case HitRegion.ZAxis: case HitRegion.NegZAxis: { a1 = Math.Abs(Vec3F.Dot(HitRayV.Direction, xAxis)); a2 = Math.Abs(Vec3F.Dot(HitRayV.Direction, yAxis)); Vec3F axis = (a1 > a2 ? xAxis : yAxis); Vec3F p0 = HitRayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); Vec3F p1 = rayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); float dragAmount = Vec3F.Dot((p1 - p0), zAxis); if (m_hitRegion == HitRegion.NegZAxis) { dragAmount *= -1; } m_scale.Z = 1.0f + dragAmount / m_hitScale; scale = m_scale.Z; } break; default: throw new ArgumentOutOfRangeException(); } if (m_isUniformScaling) { m_scale = new Vec3F(scale, scale, scale); } // scale for (int i = 0; i < m_activeOp.NodeList.Count; i++) { ITransformable node = m_activeOp.NodeList[i]; node.Scale = Vec3F.Mul(m_originalScales[i], m_scale); Matrix4F mtrx = TransformUtils.CalcTransform( Vec3F.ZeroVector, node.Rotation, node.Scale, m_pivotOffset[i]); node.Translation = m_originalTranslations[i] + mtrx.Translation; } }
public override void OnBeginDrag() { if (m_hitRegion == HitRegion.None) { return; } var selection = DesignView.Context.As <ISelectionContext>().Selection; var transactionContext = DesignView.Context.As <ITransactionContext>(); NodeList.Clear(); m_isUniformScaling = false; IEnumerable <DomNode> rootDomNodes = DomNode.GetRoots(selection.AsIEnumerable <DomNode>()); foreach (DomNode node in rootDomNodes) { ITransformable transNode = node.As <ITransformable>(); if (transNode == null || (transNode.TransformationType & TransformationTypes.Scale) == 0) { continue; } IVisible vn = node.As <IVisible>(); if (!vn.Visible) { continue; } ILockable lockable = node.As <ILockable>(); if (lockable.IsLocked) { continue; } // force uniform scaling if any node requires it if ((transNode.TransformationType & TransformationTypes.UniformScale) == TransformationTypes.UniformScale) { m_isUniformScaling = true; } NodeList.Add(transNode); IManipulatorNotify notifier = transNode.As <IManipulatorNotify>(); if (notifier != null) { notifier.OnBeginDrag(); } } // to compute offset use bounding box in local space. Vec3F offset = Vec3F.ZeroVector;// 0.5f; // use bounding box in local space switch (m_hitRegion) { case HitRegion.XAxis: case HitRegion.YAxis: case HitRegion.ZAxis: offset = new Vec3F(-1, -1, -1); break; case HitRegion.NegXAxis: case HitRegion.NegYAxis: case HitRegion.NegZAxis: offset = new Vec3F(1, 1, 1); break; default: break; } m_originalScales = new Vec3F[NodeList.Count]; m_originalTranslations = new Vec3F[NodeList.Count]; m_pivotOffset = new Vec3F[NodeList.Count]; int k = 0; foreach (ITransformable node in NodeList) { IBoundable boundable = node.As <IBoundable>(); Vec3F pivot = Vec3F.Mul(boundable.LocalBoundingBox.Radius, offset); m_pivotOffset[k] = pivot; m_originalScales[k] = node.Scale; Matrix4F mtrx = TransformUtils.CalcTransform( Vec3F.ZeroVector, node.Rotation, node.Scale, pivot ); m_originalTranslations[k] = node.Translation - mtrx.Translation; k++; } if (NodeList.Count > 0) { transactionContext.Begin("Extend".Localize()); } }
public override void OnBeginDrag(ViewControl vc, Point scrPt) { if (m_hitRegion == HitRegion.None) { return; } m_activeOp = null; var op = new ManipulatorActiveOperation( "Extend", DesignView.Context.As <ISelectionContext>(), (ITransformable node) => (node.TransformationType & TransformationTypes.Scale) != 0, false); // to compute offset use bounding box in local space. Vec3F offset = Vec3F.ZeroVector;// 0.5f; // use bounding box in local space switch (m_hitRegion) { case HitRegion.XAxis: case HitRegion.YAxis: case HitRegion.ZAxis: offset = new Vec3F(-1, -1, -1); break; case HitRegion.NegXAxis: case HitRegion.NegYAxis: case HitRegion.NegZAxis: offset = new Vec3F(1, 1, 1); break; default: break; } m_isUniformScaling = false; m_originalScales = new Vec3F[op.NodeList.Count]; m_originalTranslations = new Vec3F[op.NodeList.Count]; m_pivotOffset = new Vec3F[op.NodeList.Count]; int k = 0; foreach (ITransformable node in op.NodeList) { // if any of the selected nodes are marked as "UniformScale", then the // entire operation must be a uniform scale operaion if ((node.TransformationType & TransformationTypes.UniformScale) == TransformationTypes.UniformScale) { m_isUniformScaling = true; } IBoundable boundable = node.As <IBoundable>(); Vec3F pivot = Vec3F.Mul(boundable.LocalBoundingBox.Radius, offset); m_pivotOffset[k] = pivot; m_originalScales[k] = node.Scale; Matrix4F mtrx = TransformUtils.CalcTransform( Vec3F.ZeroVector, node.Rotation, node.Scale, pivot ); m_originalTranslations[k] = node.Translation - mtrx.Translation; k++; } m_activeOp = op; }
public override void OnDragging(ViewControl vc, Point scrPt) { if (m_hitRegion == HitRegion.None || NodeList.Count == 0) { return; } Matrix4F view = vc.Camera.ViewMatrix; // compute world * view Matrix4F wv = new Matrix4F(); wv.Mul(HitMatrix, view); // create ray in view space. Ray3F rayV = vc.GetRay(scrPt, vc.Camera.ProjectionMatrix); Vec3F xAxis = wv.XAxis; Vec3F yAxis = wv.YAxis; Vec3F zAxis = wv.ZAxis; Vec3F origin = wv.Translation; //Vec3F pos; m_scale = new Vec3F(1, 1, 1); float scale = 1; float a1, a2; switch (m_hitRegion) { case HitRegion.XAxis: { a1 = Math.Abs(Vec3F.Dot(HitRayV.Direction, yAxis)); a2 = Math.Abs(Vec3F.Dot(HitRayV.Direction, zAxis)); Vec3F axis = (a1 > a2 ? yAxis : zAxis); Vec3F p0 = HitRayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); Vec3F p1 = rayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); float dragAmount = Vec3F.Dot((p1 - p0), xAxis); m_scale.X = 1.0f + dragAmount / m_hitScale; scale = m_scale.X; } break; case HitRegion.YAxis: { a1 = Math.Abs(Vec3F.Dot(HitRayV.Direction, zAxis)); a2 = Math.Abs(Vec3F.Dot(HitRayV.Direction, xAxis)); Vec3F axis = (a1 > a2 ? zAxis : xAxis); Vec3F p0 = HitRayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); Vec3F p1 = rayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); float dragAmount = Vec3F.Dot((p1 - p0), yAxis); m_scale.Y = 1.0f + dragAmount / m_hitScale; scale = m_scale.Y; } break; case HitRegion.ZAxis: { a1 = Math.Abs(Vec3F.Dot(HitRayV.Direction, xAxis)); a2 = Math.Abs(Vec3F.Dot(HitRayV.Direction, yAxis)); Vec3F axis = (a1 > a2 ? xAxis : yAxis); Vec3F p0 = HitRayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); Vec3F p1 = rayV.IntersectPlane(axis, -Vec3F.Dot(axis, origin)); float dragAmount = Vec3F.Dot((p1 - p0), zAxis); m_scale.Z = 1.0f + dragAmount / m_hitScale; scale = m_scale.Z; } break; case HitRegion.FreeRect: { Vec3F axis = new Vec3F(0, 0, 1); Vec3F p0 = HitRayV.IntersectPlane(axis, -origin.Z); Vec3F p1 = rayV.IntersectPlane(axis, -origin.Z); Vec3F dragVec = p1 - p0; float dragAmount = 1.0f + dragVec.X / m_hitScale; m_scale.X = dragAmount; m_scale.Y = dragAmount; m_scale.Z = dragAmount; scale = m_scale.X; } break; default: throw new ArgumentOutOfRangeException(); } if (m_isUniformScaling) { m_scale = new Vec3F(scale, scale, scale); } // scale for (int i = 0; i < NodeList.Count; i++) { ITransformable transformable = NodeList[i]; transformable.Scale = Vec3F.Mul(m_originalValues[i], m_scale); } }