protected override void OnStartDemo(SampleViewport viewport) { var bgbox = new LayoutFarm.CustomWidgets.SimpleBox(800, 600); bgbox.BackColor = Color.White; bgbox.SetLocation(0, 0); SetupBackgroundProperties(bgbox); viewport.AddContent(bgbox); //-------------------------------- var box1 = new LayoutFarm.CustomWidgets.SimpleBox(150, 150); box1.BackColor = Color.Red; box1.SetLocation(10, 10); //box1.dbugTag = 1; SetupActiveBoxProperties(box1); viewport.AddContent(box1); //-------------------------------- var box2 = new LayoutFarm.CustomWidgets.SimpleBox(60, 60); box2.SetLocation(50, 50); //box2.dbugTag = 2; SetupActiveBoxProperties(box2); viewport.AddContent(box2); controllerBox1 = new UIControllerBox(40, 40); Color c = KnownColors.FromKnownColor(KnownColor.Yellow); controllerBox1.BackColor = new Color(100, c.R, c.G, c.B); controllerBox1.SetLocation(200, 200); //controllerBox1.dbugTag = 3; controllerBox1.Visible = false; SetupControllerBoxProperties(controllerBox1); viewport.AddContent(controllerBox1); }
UIControllerBox GetFreeUserControllerBox() { if (userControllerPool.Count > 0) { var controlBox = userControllerPool.Dequeue(); //------------------------------------------- //register to working box list workingControllerBoxes.Add(controlBox); return controlBox; } else { //create new one //controller box 1 (red corners) var controllerBox1 = new UIControllerBox(40, 40); Color c = KnownColors.FromKnownColor(KnownColor.Yellow); controllerBox1.BackColor = new Color(100, c.R, c.G, c.B); controllerBox1.SetLocation(200, 200); //controllerBox1.dbugTag = 3; controllerBox1.Visible = false; SetupControllerBoxProperties(controllerBox1); //------------------------------------------- //register to working box list workingControllerBoxes.Add(controllerBox1); return controllerBox1; } }
static void SetupControllerBoxProperties(UIControllerBox controllerBox) { //for controller box controllerBox.MouseDrag += (s, e) => { Point pos = controllerBox.Position; int newX = pos.X + e.XDiff; int newY = pos.Y + e.YDiff; controllerBox.SetLocation(newX, newY); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetLocation(newX + 5, newY + 5); } e.CancelBubbling = true; }; }
void FindSelectedUserBoxes() { //find users box in selected area int j = this.bgbox.ChildCount; var primSelectionBox = selectionBox.GetPrimaryRenderElement(rootgfx); var primGlobalPoint = primSelectionBox.GetGlobalLocation(); var selectedRectArea = new Rectangle(primGlobalPoint, primSelectionBox.Size); List <UIBox> selectedList = new List <UIBox>(); for (int i = 0; i < j; ++i) { var box = bgbox.GetChild(i) as UIBox; if (box == null) { continue; } var primElement = box.GetPrimaryRenderElement(rootgfx); if (!primElement.Visible) { continue; } //get global area Point globalLocation = primElement.GetGlobalLocation(); var userElementArea = new Rectangle(globalLocation, primElement.Size); if (selectedRectArea.Contains(userElementArea)) { //selected= true; selectedList.Add(box); //------ //create user controller box for the selected box UIControllerBox userControllerBox = GetFreeUserControllerBox(); userControllerBox.TargetBox = box; var globalTargetPos = box.GetGlobalLocation(); userControllerBox.SetLocation(globalTargetPos.X - 5, globalTargetPos.Y - 5); userControllerBox.SetSize(box.Width + 10, box.Height + 10); userControllerBox.Visible = true; viewport.AddContent(userControllerBox); } } }
static void MoveWithSnapToGrid(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; int gridSize = 5; float halfGrid = (float)gridSize / 2f; int nearestX = (int)((newX + halfGrid) / gridSize) * gridSize; int nearestY = (int)((newY + halfGrid) / gridSize) * gridSize; controllerBox.SetLocation(nearestX, nearestY); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetLocation(nearestX + gridSize, nearestY + gridSize); } }
protected override void OnStartDemo(SampleViewport viewport) { //-------------------------------- var bgbox = new LayoutFarm.CustomWidgets.SimpleBox(800, 600); bgbox.BackColor = Color.White; bgbox.SetLocation(0, 0); SetupBackgroundProperties(bgbox); viewport.AddContent(bgbox); //-------------------------------- var box1 = new LayoutFarm.CustomWidgets.SimpleBox(150, 150); box1.BackColor = Color.Red; box1.SetLocation(10, 10); //box1.dbugTag = 1; SetupActiveBoxProperties(box1); viewport.AddContent(box1); //-------------------------------- var box2 = new LayoutFarm.CustomWidgets.SimpleBox(60, 60); box2.SetLocation(50, 50); //box2.dbugTag = 2; SetupActiveBoxProperties(box2); viewport.AddContent(box2); controllerBox1 = new UIControllerBox(40, 40); Color c = KnownColors.FromKnownColor(KnownColor.Yellow); controllerBox1.BackColor = new Color(100, c.R, c.G, c.B); controllerBox1.SetLocation(200, 200); //controllerBox1.dbugTag = 3; controllerBox1.Visible = false; SetupControllerBoxProperties(controllerBox1); viewport.AddContent(controllerBox1); }
static void MoveWithSnapToGrid(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; int gridSize = 5; float halfGrid = (float)gridSize / 2f; int nearestX = (int)((newX + halfGrid) / gridSize) * gridSize; int nearestY = (int)((newY + halfGrid) / gridSize) * gridSize; controllerBox.SetLocation(nearestX, nearestY); UIBox targetBox = controllerBox.TargetBox; if (targetBox != null) { int xdiff = nearestX - pos.X; int ydiff = nearestY - pos.Y; targetBox.SetLocation(targetBox.Left + xdiff, targetBox.Top + ydiff); } }
static void MoveWithSnapToGrid(UIControllerBox controllerBox, UIMouseEventArgs e) { //sample move with snap to grid Point pos = controllerBox.Position; int newX = pos.X + e.XDiff; int newY = pos.Y + e.YDiff; //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; controllerBox.SetLocation(nearestX, nearestY); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetLocation(nearestX + gridSize, nearestY + gridSize); } }
void SetupActiveBoxProperties(LayoutFarm.CustomWidgets.EaseBox box) { //1. mouse down box.MouseDown += (s, e) => { box.BackColor = KnownColors.FromKnownColor(KnownColor.DeepSkyBlue); e.MouseCursorStyle = MouseCursorStyle.Pointer; UIControllerBox userControllerBox = null; if (this.controllerBoxMode == ControllerBoxMode.MultipleBoxes) { //multiple box userControllerBox = GetFreeUserControllerBox(); } else { //single box if (singleControllerBox != null) { ReleaseUserControllerBox(singleControllerBox); } //get new one userControllerBox = singleControllerBox = GetFreeUserControllerBox(); } //request user controller for this box userControllerBox.TargetBox = box; viewport.AddContent(userControllerBox); //location relative to global position of target box var globalTargetPos = box.GetGlobalLocation(); userControllerBox.SetLocation(globalTargetPos.X - 5, globalTargetPos.Y - 5); userControllerBox.SetSize(box.Width + 10, box.Height + 10); userControllerBox.Visible = true; //move mouse capture to new controller box //for next drag e.SetMouseCapture(userControllerBox); }; //2. mouse up box.MouseUp += (s, e) => { e.MouseCursorStyle = MouseCursorStyle.Default; box.BackColor = Color.LightGray; RemoveAllUserControllerBoxes(); }; box.DragOver += (s, e) => { switch (e.UserMsgFlags) { case 1: { //sample only box.BackColor = Color.Green; } break; case 2: { //sample only //leaving box.BackColor = Color.Blue; } break; case 3: { //drop var sender = e.Sender as UIControllerBox; var droppingBox = sender.TargetBox as UIBox; if (droppingBox != null) { //move from original var parentBox = droppingBox.ParentUI as LayoutFarm.CustomWidgets.SimpleBox; if (parentBox != null) { var newParentGlobalLoca = box.GetGlobalLocation(); var droppingGlobalLoca = droppingBox.GetGlobalLocation(); parentBox.RemoveChild(droppingBox); box.AddChild(droppingBox); //TODO: review here , //set location relative to other element droppingBox.SetLocation( droppingGlobalLoca.X - newParentGlobalLoca.X, droppingGlobalLoca.Y - newParentGlobalLoca.Y); } else { } } } break; } }; }
static void ResizeTargetWithSnapToGrid(SpaceName tinyBoxSpaceName, UIControllerBox controllerBox, UIMouseEventArgs e) { //sample move with snap to grid Point pos = controllerBox.Position; int newX = pos.X + e.XDiff; int newY = pos.Y + e.YDiff; //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.SetLocation(controllerBox.Left + xdiff, controllerBox.Top + ydiff); controllerBox.SetSize(controllerBox.Width - xdiff, controllerBox.Height - ydiff); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetLocationAndSize(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } break; case SpaceName.RightTop: { if (xdiff != 0 || ydiff != 0) { controllerBox.SetLocation(controllerBox.Left, controllerBox.Top + ydiff); controllerBox.SetSize(controllerBox.Width + xdiff, controllerBox.Height - ydiff); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetLocationAndSize(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } 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.SetLocationAndSize(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } break; case SpaceName.LeftBottom: { if (xdiff != 0 || ydiff != 0) { controllerBox.SetLocation(controllerBox.Left + xdiff, controllerBox.Top); controllerBox.SetSize(controllerBox.Width - xdiff, controllerBox.Height + ydiff); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetLocationAndSize(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } break; } }
static void ResizeTargetWithSnapToGrid(SpaceName tinyBoxSpaceName, UIControllerBox controllerBox, UIMouseEventArgs e) { //sample move with snap to grid Point pos = controllerBox.Position; int newX = pos.X + e.XDiff; int newY = pos.Y + e.YDiff; //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.SetLocation(controllerBox.Left + xdiff, controllerBox.Top + ydiff); controllerBox.SetSize(controllerBox.Width - xdiff, controllerBox.Height - ydiff); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetBounds(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } break; case SpaceName.RightTop: { if (xdiff != 0 || ydiff != 0) { controllerBox.SetLocation(controllerBox.Left, controllerBox.Top + ydiff); controllerBox.SetSize(controllerBox.Width + xdiff, controllerBox.Height - ydiff); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetBounds(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } 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.SetBounds(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } break; case SpaceName.LeftBottom: { if (xdiff != 0 || ydiff != 0) { controllerBox.SetLocation(controllerBox.Left + xdiff, controllerBox.Top); controllerBox.SetSize(controllerBox.Width - xdiff, controllerBox.Height + ydiff); var targetBox = controllerBox.TargetBox; if (targetBox != null) { //move target box too targetBox.SetBounds(controllerBox.Left + 5, controllerBox.Top + 5, controllerBox.Width - 10, controllerBox.Height - 10); } } } break; } }