/// <summary> /// Initalise the button manager /// </summary> public ButtonManager() { mUnderMouseButton = null; MouseManager.MouseMove += new EventHandler(MouseManager_MouseMove); MouseManager.LeftMousePress += new EventHandler(MouseManager_LeftMousePress); MouseManager.LeftMouseRelease += new EventHandler(MouseManager_LeftMouseRelease); MouseManager.LeftMouseDoubleClick += new EventHandler(MouseManager_LeftMouseDoubleClick); MouseManager.RightMousePress += new EventHandler(MouseManager_RightMousePress); MouseManager.RightMouseRelease += new EventHandler(MouseManager_RightMouseRelease); mButtons = new List<PCButtonGroup>(); mButtons.Add(new PCButtonGroup()); //this is the default button group to check, always mButtons[0] mExclusionRectangle = new Rectangle(); //don't exclude anywhere unless told }
/// <summary> /// Add a button to an existing button group /// </summary> /// <param name="lButton">New button - should be in close proximity of other buttons for greatest efficency</param> public void AddButton(PCButton lButton) { lButton.AddToButtonGroup(ref buttonArea); mButtons.Add(lButton); }
/// <summary> /// Add the PCButton to the standard group /// </summary> /// <param name="button">The new PCButton, make sure you have already registered to any desired events</param> public void AddButton(PCButton button) { mButtons[0].AddButton(button); //add the button to the standard group }
private void MouseManager_MouseMove(object sender, EventArgs e) { Point mousePosition = MouseManager.MousePosition; if (mExclusionRectangle.Contains(mousePosition)) { return; } if (mUnderMouseButton != null && mUnderMouseButton.CheckForCollision(mousePosition)) { return; } else { if (mUnderMouseButton != null) { mUnderMouseButton.MouseUnHovered(); } mUnderMouseButton = null; foreach (PCButtonGroup buttonGroup in mButtons) { mUnderMouseButton = buttonGroup.CheckForCollision(mousePosition); if (mUnderMouseButton != null) { mUnderMouseButton.MouseHovered(); } } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); baseTexture = Content.Load<Texture2D>("BaseTexture"); buttonTexture = Content.Load<Texture2D>("ButtonTexture"); PCButton myBtn = new PCButton(buttonTexture, new Rectangle(10, 10, 100, 100)); //create the button with position and size of the button myBtn.ConfigureButton(PCButtonState.hover, new Point(100, 0)); //Configure specific button states - eg setting the hover image to be at 100,0 in the texture myBtn.ConfigureButton(PCButtonState.pressed, new Point(200,0)); myBtn.ConfigureButton(PCButtonState.released, new Point(300, 0)); //Register to specific button events myBtn.MouseHover += new EventHandler(myBtn_MouseHover); myBtn.RightMousePress += new EventHandler(myBtn_RightMousePress); myBtn.LeftMousePress += new EventHandler(myBtn_LeftMousePress); myBtn.LeftMouseDoubleClick += new EventHandler(myBtn_LeftMouseDoubleClick); myBtn.HotKey = Keys.G; //setting the hotkey means that the button listens for this key and fires mouse click mainWindowButtonManager.AddButton(myBtn); player = new Character(baseTexture); }