LayoutFarm.Demo_DragSelectionBox.UIControllerBox.ResizeTargetWithSnapToGrid C# (CSharp) Méthode

ResizeTargetWithSnapToGrid() static private méthode

static private ResizeTargetWithSnapToGrid ( SpaceName tinyBoxSpaceName, UIControllerBox controllerBox, int dx, int dy ) : void
tinyBoxSpaceName SpaceName
controllerBox UIControllerBox
dx int
dy int
Résultat void
            static void ResizeTargetWithSnapToGrid(SpaceName tinyBoxSpaceName, UIControllerBox controllerBox, int dx, int dy)
            {
                //sample move with snap to grid
                Point pos = controllerBox.Position;
                int newX = pos.X + dx;
                int newY = pos.Y + dy;
                //snap to gridsize =5;
                //find nearest snap x 
                int gridSize = 5;
                float halfGrid = (float)gridSize / 2f;
                int nearestX = (int)((newX + halfGrid) / gridSize) * gridSize;
                int nearestY = (int)((newY + halfGrid) / gridSize) * gridSize;
                int xdiff = nearestX - pos.X;
                int ydiff = nearestY - pos.Y;
                switch (tinyBoxSpaceName)
                {
                    case SpaceName.LeftTop:
                        {
                            if (xdiff != 0 || ydiff != 0)
                            {
                                controllerBox.SetBounds(controllerBox.Left + xdiff, controllerBox.Top + ydiff,
                                    controllerBox.Width - xdiff, controllerBox.Height - ydiff);
                                var targetBox = controllerBox.TargetBox;
                                if (targetBox != null)
                                {
                                    //move target box too                                     
                                    targetBox.SetBounds(targetBox.Left + xdiff, targetBox.Top + ydiff,
                                        targetBox.Width - xdiff, targetBox.Height - ydiff);
                                }
                            }
                        }
                        break;
                    case SpaceName.RightTop:
                        {
                            if (xdiff != 0 || ydiff != 0)
                            {
                                controllerBox.SetBounds(controllerBox.Left, controllerBox.Top + ydiff,
                                    controllerBox.Width + xdiff, controllerBox.Height - ydiff);
                                var targetBox = controllerBox.TargetBox;
                                if (targetBox != null)
                                {
                                    targetBox.SetBounds(targetBox.Left, targetBox.Top + ydiff,
                                        targetBox.Width + xdiff, targetBox.Height - ydiff);
                                }
                            }
                        }
                        break;
                    case SpaceName.RightBottom:
                        {
                            if (xdiff != 0 || ydiff != 0)
                            {
                                controllerBox.SetSize(controllerBox.Width + xdiff, controllerBox.Height + ydiff);
                                var targetBox = controllerBox.TargetBox;
                                if (targetBox != null)
                                {
                                    //move target box too 
                                    targetBox.SetSize(targetBox.Width + xdiff, targetBox.Height + ydiff);
                                }
                            }
                        }
                        break;
                    case SpaceName.LeftBottom:
                        {
                            if (xdiff != 0 || ydiff != 0)
                            {
                                controllerBox.SetBounds(controllerBox.Left + xdiff, controllerBox.Top,
                                    controllerBox.Width - xdiff, controllerBox.Height + ydiff);
                                var targetBox = controllerBox.TargetBox;
                                if (targetBox != null)
                                {
                                    //move target box too 
                                    targetBox.SetBounds(targetBox.Left + xdiff, targetBox.Top,
                                        targetBox.Width - xdiff, targetBox.Height + ydiff);
                                }
                            }
                        }
                        break;
                }
            }