public DragableMatrixV2(Vector2 size, int maxSlots) { // make sure valid w/h for number of max slots if (size.X * size.Y < maxSlots) { throw new Exception("More max slots then allowed by size of control!"); } this.Size = size; containers = new List <DragableContainer>(); this.maxSlots = maxSlots; // setup containers relative to loc. // if this controls list doesnt have the context menu, add it once! contextMenu = new ItemStatsUI(new Vector2(150, 150)) { IsDrawn = false }; SetupContainers(); mouseOrigin = new Vector2(-1, -1); currentDragableControl = null; }
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); }
/// <summary> /// Adds a control to the matrix. Will be added to the first empty container. /// </summary> /// <param name="control">The control to add.</param> public void AddToMatrix(Item.Item item) { // find first open container and set. foreach (DragableContainer container in containers) { if (container.ControlContained == null) { DragableControl tmpDragable = new DragableControl(item, sideLength); container.SetDragableControl(tmpDragable); break; } } // TO-DO: prompt to drop something. }
/// <summary> /// Sets the dragable control to be contained. /// </summary> /// <param name="controlToSet">The new control to be contained in this container.</param> /// <returns>The old control contained.</returns> public DragableControl SetDragableControl(DragableControl controlToSet) { //Console.WriteLine("OLD: {1} NEW: {0}", controlToSet.Item.name, this.controlInContainer.Item.name); // if given nothing, dont set anything. if (controlToSet == null) { return(null); } // should this even EVER happen? if (controlToSet == controlInContainer) { controlToSet.Location = Vector2.Zero; return(null); //return controlToSet; } DragableControl controlToReplace = controlInContainer; // Clear this container. // Clear the continar this came from. // this assumes the parent is the dragable container! if (controlToSet.parent != null) { controlToSet.parent.Clear(); } Clear(); // All controls should be centered in their container... controlInContainer = controlToSet; controlInContainer.Location = Vector2.Zero; controlInContainer.Alignment = ControlAlignment.Center; //controlInContainer.parent = this; Add(controlInContainer); if (controlToReplace != null) { controlToReplace.parent = null; } return(controlToReplace); // parent for controlToReplace is still this. // controlToReplace is no longer in this container. }
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; } }
public DragableContainer(Vector2 size, DragableControl controlInContainer = null, string name = null) { this.Size = size; if (namesInUse.Contains(name)) { throw new Exception("WARNING: CONFLICTING CONTAINER NAMES!"); } if (!string.IsNullOrWhiteSpace(name)) { namesInUse.Add(name); } this.name = name; this.controlInContainer = controlInContainer; SetDragableControl(controlInContainer); myID = id++; }
public override void Clear() { controlInContainer = null; base.Clear(); }