/// <summary> /// Check if the button is in this boundary when the user clicks. /// </summary> /// <param name="shiftCenteredX"></param> /// <param name="shiftCenteredY"></param> /// <param name="containerParentX"></param> /// <param name="containerParentY"></param> /// <param name="mouseState"></param> public void CheckMouseEvent(int shiftCenteredX, int shiftCenteredY, int containerParentX, int containerParentY, MouseState mouseState) { if (this.currentState == UIObjectState.Disabled) { return; // disabled buttons dont react } // The position of the button relative to the minimap int minimapButtonRelativeX = -(containerParentX) - X; // Left to right int minimapButtonRelativeY = -(containerParentY) - Y; // Top to bottom int buttonPositionXToMap = shiftCenteredX - minimapButtonRelativeX; int buttonPositionYToMap = shiftCenteredY - minimapButtonRelativeY; // The position of the mouse relative to the game Rectangle rect = new Rectangle( buttonPositionXToMap - (shiftCenteredX), buttonPositionYToMap - (shiftCenteredY), CanvasSnapshotWidth, CanvasSnapshotHeight); //System.Diagnostics.Debug.WriteLine("Button rect: " + rect.ToString()); //System.Diagnostics.Debug.WriteLine("Mouse X: " + mouseState.X + ", Y: " + mouseState.Y); if (rect.Contains(mouseState.X, mouseState.Y)) { if (mouseState.LeftButton == ButtonState.Pressed) { SetButtonState(UIObjectState.Pressed); if (seBtMouseClick != null) // play mouse click sound { seBtMouseClick.Play(); } } else if (mouseState.LeftButton == ButtonState.Released) { UIObjectState priorState = this.currentState; SetButtonState(UIObjectState.MouseOver); if (priorState == UIObjectState.Pressed) // this after setting the MouseOver state, so user-code does not get override { // Invoke clicked event ButtonClickReleased?.Invoke(this); } } else { SetButtonState(UIObjectState.MouseOver); if (seBtMouseOver != null) // play mouse over sound { seBtMouseOver.Play(); } } } else { SetButtonState(UIObjectState.Normal); } }
/// <summary> /// Gets the current BaseDXDrawableItem based upon the current button state. /// </summary> /// <param name="state"></param> /// <returns></returns> public BaseDXDrawableItem GetBaseDXDrawableItemByState(UIObjectState state = UIObjectState.Null) { if (state == UIObjectState.Null) { state = currentState; } switch (state) { case UIObjectState.Pressed: return(pressedState); case UIObjectState.Disabled: return(disabledState); case UIObjectState.MouseOver: return(mouseOverState); default: case UIObjectState.Null: case UIObjectState.Normal: return(normalState); } }
/// <summary> /// Sets the current state of the button /// </summary> /// <param name="state"></param> public void SetButtonState(UIObjectState state) { this.currentState = state; }