Ejemplo n.º 1
0
        public override async void OnMouseMove(Mouse3DEventArgs mouseEvent3D, bool mouseIsOver)
        {
            var selectedItem = RootSelection;

            ActiveSelectedItem = selectedItem;

            if (MouseIsOver || MouseDownOnControl)
            {
                diameterValueDisplayInfo.Visible = true;
            }
            else if (!hadClickOnControl || scaleController.HasChange)
            {
                diameterValueDisplayInfo.Visible = false;
            }

            if (MouseDownOnControl && hitPlane != null)
            {
                var info = hitPlane.GetClosestIntersection(mouseEvent3D.MouseRay);

                if (info != null &&
                    selectedItem != null)
                {
                    var delta = info.HitPosition - initialHitPosition;

                    var lockedBottomCenter = ObjectSpace.GetCenterPosition(selectedItem, placement);

                    var(hit, otherSide) = GetHitIndices(selectedItem);
                    var grabPositionEdge  = ObjectSpace.GetEdgePosition(selectedItem, otherSide);
                    var stretchDirection  = (ObjectSpace.GetEdgePosition(selectedItem, hit) - grabPositionEdge).GetNormal();
                    var deltaAlongStretch = stretchDirection.Dot(delta);

                    // scale it
                    var newSize = scaleController.InitialState.Diameters[diameterIndex];
                    newSize += deltaAlongStretch * 2;
                    newSize  = Math.Max(Math.Max(newSize, .001), Object3DControlContext.SnapGridDistance);

                    if (Object3DControlContext.SnapGridDistance > 0)
                    {
                        // snap this position to the grid
                        double snapGridDistance = Object3DControlContext.SnapGridDistance;

                        // snap this position to the grid
                        newSize = ((int)((newSize / snapGridDistance) + .5)) * snapGridDistance;
                    }

                    scaleController.ScaleDiameter(newSize, diameterIndex);

                    await selectedItem.Rebuild();

                    // and keep the locked edge in place
                    Vector3 newBottomCenter = ObjectSpace.GetCenterPosition(selectedItem, placement);

                    selectedItem.Matrix *= Matrix4X4.CreateTranslation(lockedBottomCenter - newBottomCenter);

                    Invalidate();
                }
            }

            base.OnMouseMove(mouseEvent3D, mouseIsOver);
        }
Ejemplo n.º 2
0
        public ScaleDiameterControl(IObject3DControlContext context,
                                    Func <double> getHeight,
                                    Action <double> setHeight,
                                    List <Func <double> > getDiameters,
                                    List <Action <double> > setDiameters,
                                    int diameterIndex,
                                    ObjectSpace.Placement placement = ObjectSpace.Placement.Bottom,
                                    Func <bool> controlVisible      = null,
                                    double angleOffset = 0)
            : base(context)
        {
            this.getHeight      = getHeight;
            this.setHeight      = setHeight;
            this.getDiameters   = getDiameters;
            this.setDiameters   = setDiameters;
            this.controlVisible = controlVisible;
            this.placement      = placement;
            this.diameterIndex  = diameterIndex;
            this.angleOffset    = angleOffset;
            theme = MatterControl.AppContext.Theme;

            scaleController = new ScaleController(Object3DControlContext, null, null, null, null, getHeight, setHeight, getDiameters, setDiameters);

            diameterValueDisplayInfo = new InlineEditControl()
            {
                ForceHide        = ForceHideScale,
                GetDisplayString = (value) => "{0:0.0}".FormatWith(value),
            };

            diameterValueDisplayInfo.EditComplete += async(s, e) =>
            {
                var newDiameter = diameterValueDisplayInfo.Value != 0 ? diameterValueDisplayInfo.Value : getDiameters[diameterIndex]();

                if (newDiameter == scaleController.FinalState.Diameters[diameterIndex])
                {
                    return;
                }

                Vector3 lockedEdge = ObjectSpace.GetCenterPosition(ActiveSelectedItem, placement);
                scaleController.ScaleDiameter(newDiameter, diameterIndex);
                await ActiveSelectedItem.Rebuild();

                // and keep the locked edge in place
                Vector3 newLockedEdge = ObjectSpace.GetCenterPosition(ActiveSelectedItem, placement);
                ActiveSelectedItem.Translate(lockedEdge - newLockedEdge);

                scaleController.EditComplete();
                scaleController = new ScaleController(Object3DControlContext, null, null, null, null, getHeight, setHeight, getDiameters, setDiameters);
            };

            diameterValueDisplayInfo.VisibleChanged += (s, e) =>
            {
                if (!diameterValueDisplayInfo.Visible)
                {
                    hadClickOnControl = false;
                }
            };

            Object3DControlContext.GuiSurface.AddChild(diameterValueDisplayInfo);

            DrawOnTop = true;

            grabControlMesh = SphereObject3D.CreateSphere(grabControlSize, 15, 10);

            CollisionVolume = grabControlMesh.CreateBVHData();

            Object3DControlContext.GuiSurface.BeforeDraw += Object3DControl_BeforeDraw;
        }