public override void Update(GameTime gameTime) { if (currentDragableControl != null) { currentDragableControl.Location = new Vector2(this.CurrentFrameMouseState.Position.X - mouseOrigin.X, this.CurrentFrameMouseState.Position.Y - mouseOrigin.Y); if (this.CurrentFrameMouseState.LeftButton == ButtonState.Released) { // if hovering over no slot, return the control back to original location (only if the control is inside the root matrix window) if (hoveringDragableContaner == null && this.RootParent.GlobalRectangle.Contains(this.CurrentFrameMouseState.Position)) // && inside the window. { currentDragableControl.Location = Vector2.Zero; currentDragableControl = null; currentDragableContainer = null; } else { // TO-DO: prompt to drop the item from inventory. currentDragableControl.Location = Vector2.Zero; currentDragableControl = null; currentDragableContainer = null; } } } // updated every frame so this is fine. hoveringDragableContaner = null; contextMenu.Update(gameTime); base.Update(gameTime); }
void tmpContainer_Pressed(object sender, EventArgs e) { // Press a container // If the container has a control in it, grab it // DONT Clear that control from the container DragableContainer clickedOn = sender as DragableContainer; contextMenu.IsDrawn = false; if (currentDragableControl == null && clickedOn.ControlContained != null) { if (currentDragableContainer == null) { currentDragableContainer = clickedOn; } currentDragableControl = clickedOn.ControlContained; mouseOrigin = new Vector2(this.CurrentFrameMouseState.Position.X - currentDragableControl.Location.X, this.CurrentFrameMouseState.Position.Y - currentDragableControl.Location.Y); } }
void tmpContainer_HoverRelease(object sender, EventArgs e) { // Check to see if the container released over has a control in it // if so, swap // else add. if (currentDragableControl != null) { DragableContainer hoveringOver = sender as DragableContainer; hoveringDragableContaner = hoveringOver; DragableControl lastControl = hoveringOver.SetDragableControl(currentDragableControl); if (lastControl != null && currentDragableContainer != null) { currentDragableContainer.SetDragableControl(lastControl); } currentDragableContainer = null; currentDragableControl = null; } }
void tmpContainer_Hover(object sender, EventArgs e) { // if this container does not have an item in it, return DragableContainer container = sender as DragableContainer; if (container == null || container.ControlContained == null || container.ControlContained.Item == null) { //Console.WriteLine("nothing"); //contextMenu.IsDrawn = false; return; } // display hovering container showing stats of item. Item.Item item = container.ControlContained.Item; // menu location if (currentDragableContainer == null) { contextMenu.Location = new Vector2(this.CurrentFrameMouseState.Position.X, this.CurrentFrameMouseState.Position.Y); } else { contextMenu.Location = new Vector2(currentDragableControl.GlobalLocation().X + currentDragableControl.Size.X, currentDragableControl.GlobalLocation().Y); } // load item info if (contextMenu.Item != item) { contextMenu.Load(item); } // make sure it draws contextMenu.IsDrawn = true; }
/// <summary> /// Sets up containers relative to this matrix position. Will create square containers with sidelength appropriate for maxSlots. /// </summary> private void SetupContainers() { // TO-DO: center the controls or provide divider, they will probably be off a little due to floats. //Math.Max(Size.X % tileSize, Size.Y % tileSize); float dividerX = 0; float dividerY = 0; if (maxSlots == 1) { sideLength = Math.Min(Size.X, Size.Y); } else { // find optimal container size when max number of slots are in this matrix // http://stackoverflow.com/questions/868997/max-square-size-for-unknown-number-inside-rectangle // come up with an initial guess double aspect = (double)Size.Y / Size.X; double xf = Math.Sqrt(maxSlots / aspect); double yf = xf * aspect; int tx = (int)Math.Max(1.0, Math.Floor(xf)); int ty = (int)Math.Max(1.0, Math.Floor(yf)); int x_size = (int)Math.Floor((double)Size.X / tx); int y_size = (int)Math.Floor((double)Size.Y / ty); int tileSize = Math.Min(x_size, y_size); // test our guess: tx = (int)Math.Floor((double)Size.X / tileSize); ty = (int)Math.Floor((double)Size.Y / tileSize); if (tx * ty < maxSlots) // we guessed too high { if (((tx + 1) * ty < maxSlots) && (tx * (ty + 1) < maxSlots)) { // case 2: the upper bound is correct // compute the tileSize that will // result in (x+1)*(y+1) tiles x_size = (int)Math.Floor((double)Size.X / (tx + 1)); y_size = (int)Math.Floor((double)Size.Y / (ty + 1)); tileSize = Math.Min(x_size, y_size); } else { // case 3: solve an equation to determine // the final x and y dimensions // and then compute the tileSize // that results in those dimensions int test_x = (int)Math.Ceiling((double)maxSlots / ty); int test_y = (int)Math.Ceiling((double)maxSlots / tx); x_size = (int)Math.Min(Math.Floor((double)Size.X / test_x), Math.Floor((double)Size.Y / ty)); y_size = (int)Math.Min(Math.Floor((double)Size.X / tx), Math.Floor((double)Size.Y / test_y)); tileSize = Math.Max(x_size, y_size); } } sideLength = tileSize; dividerX = (Size.X % tileSize) / ((float)Math.Floor(Size.X / tileSize) - 1); dividerY = (Size.Y % tileSize) / ((float)Math.Floor(Size.Y / tileSize) - 1); } // add the containers via for int count = 0; for (float y = 0; y + sideLength <= Size.Y && count < maxSlots; y += (sideLength + dividerY)) { for (float x = 0; x + sideLength <= Size.X && count < maxSlots; x += (sideLength + dividerX)) { DragableContainer tmpContainer = new DragableContainer(new Vector2(sideLength, sideLength)); tmpContainer.Location = new Vector2(x, y); tmpContainer.HoverRelease += tmpContainer_HoverRelease; tmpContainer.Hover += tmpContainer_Hover; tmpContainer.Pressed += tmpContainer_Pressed; tmpContainer.parent = this; Add(tmpContainer); containers.Add(tmpContainer); count++; } } }