Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


Mouse and rectangle problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Kurenis   Members   -  Reputation: 451

Like
0Likes
Like

Posted 21 May 2013 - 08:34 PM

So, basically I've been trying to make a draggable menu, with buttons contained inside of it. The problem I've run into is trying to make the menu so that it will not move when the mouse is over any of it's buttons. The classes involved are Resources, Mouse, Object, ClickableObject, ClickableMenu, and Button (ClickableMenu and Button are children of ClickableObject, which is a child of Object). I apologize in advance for the amount of code.

 

Resources.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RPG
{
    class Resources
    {
        public Texture2D texBG;

        //Menu textures
        public Texture2D texMenuTL;
        public Texture2D texMenuTR;
        public Texture2D texMenuBL;
        public Texture2D texMenuBR;
        public Texture2D texMenuVert;
        public Texture2D texMenuHor;
        public Texture2D texMenuC;

        public SpriteFont spCustom;

        public Resources(ContentManager Content)
        {
            texBG = Content.Load<Texture2D>("bg");
            
            texMenuTL = Content.Load<Texture2D>("menu_tl");
            texMenuTR = Content.Load<Texture2D>("menu_tr");
            texMenuBL = Content.Load<Texture2D>("menu_bl");
            texMenuBR = Content.Load<Texture2D>("menu_br");
            texMenuVert = Content.Load<Texture2D>("menu_vert");
            texMenuHor = Content.Load<Texture2D>("menu_hor");
            texMenuC = Content.Load<Texture2D>("menu_c");

            spCustom = Content.Load<SpriteFont>("Custom");
        }
    }
}

 

 

Mouse.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RPG
{
    class Mouse
    {
        MouseState previousState, currentState;
        Texture2D texture;

        public Vector2 Position
        {
            get;
            protected set;
        }

        public Rectangle Rectangle
        {
            get;
            protected set;
        }

        public bool LeftClick
        {
            get
            {
                return currentState.LeftButton == ButtonState.Pressed
                    && previousState.LeftButton == ButtonState.Released;
            }
        }

        public bool LeftPress
        {
            get
            {
                return currentState.LeftButton == ButtonState.Pressed;
            }
        }

        public bool LeftRelease
        {
            get
            {
                return !LeftPress && previousState.LeftButton == ButtonState.Pressed;
            }
        }

        public bool RightClick
        {
            get
            {
                return currentState.RightButton == ButtonState.Pressed
                    && previousState.RightButton == ButtonState.Released;
            }
        }

        public bool RightPress
        {
            get
            {
                return currentState.RightButton == ButtonState.Pressed;
            }
        }

        public bool RightRelease
        {
            get
            {
                return !RightPress && previousState.RightButton == ButtonState.Pressed;
            }
        }

        public bool NormalState
        {
            get
            {
                return !LeftPress && !LeftRelease && !RightPress && !RightRelease;
            }
        }

        public ClickableObject ClickedObject
        {
            get;
            set;
        }

        public Mouse(Texture2D texture)
        {
            this.texture = texture;
        }

        public void Update()
        {
            previousState = currentState;
            currentState = Microsoft.Xna.Framework.Input.Mouse.GetState();

            Position = new Vector2(currentState.X, currentState.Y);
            Rectangle = new Rectangle((int)Position.X, (int)Position.Y, texture.Width, texture.Height);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (texture != null)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(texture, Position, Color.White);
                spriteBatch.End();
            }
        }
    }
}

 

 

Object.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace RPG
{
    class Object
    {
        public Texture2D Texture
        {
            get;
            set;
        }

        private Rectangle rec;
        public Rectangle Rectangle
        {
            get
            {
                return rec;
            }
            set
            {
                rec = value;
                Position = new Vector2(Rectangle.X, Rectangle.Y);
            }
        }

        public Vector2 Position
        {
            get;
            set;
        }

        public bool Visible
        {
            get;
            set;
        }

        public Object()
        {
            Visible = true;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (spriteBatch != null)
                if (Texture != null)
                    spriteBatch.Draw(Texture, Position, Color.White);
        }
    }
}

 

 

ClickableObject.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPG
{
    public enum ObjectClickedState
    {
        Normal,
        Clicked,
        Released
    }

    class ClickableObject : Object
    {
        public ObjectClickedState LeftState
        {
            get;
            protected set;
        }

        public ObjectClickedState RightState
        {
            get;
            protected set;
        }

        public Mouse ActiveMouse
        {
            get;
            protected set;
        }

        public ClickableObject() : base()
        {
            LeftState = RightState = ObjectClickedState.Normal;
        }

        public virtual void OnLeftClick() { }
        public virtual void OnLeftPress() { }
        public virtual void OnLeftRelease() { }
        public virtual void OnLeftNormal() { }
        public virtual void OnRightClick() { }
        public virtual void OnRightPress() { }
        public virtual void OnRightRelease() { }
        public virtual void OnRightNormal() { }

        public virtual void OnHover() { }
        public virtual void OnLeave() { }

        public void Update(Mouse activeMouse)
        {
            bool intersect = activeMouse.Rectangle.Intersects(Rectangle);

            if (intersect || (ActiveMouse != null && (LeftState != ObjectClickedState.Normal || RightState != ObjectClickedState.Normal)))
            {
                ActiveMouse = activeMouse;
                ActiveMouse.ClickedObject = this;
                if (intersect)
                    OnHover();
                if (ActiveMouse.LeftPress && (LeftState == ObjectClickedState.Normal || LeftState == ObjectClickedState.Released))
                {
                    LeftState = ObjectClickedState.Clicked;
                    OnLeftClick();
                }
                else if (ActiveMouse.LeftPress && LeftState == ObjectClickedState.Clicked)
                    OnLeftPress();
                else if (ActiveMouse.LeftRelease && LeftState == ObjectClickedState.Clicked)
                {
                    LeftState = ObjectClickedState.Released;
                    OnLeftRelease();
                }
                else if (!ActiveMouse.LeftPress && LeftState == ObjectClickedState.Released)
                {
                    LeftState = ObjectClickedState.Normal;
                    OnLeftNormal();
                }

                if (ActiveMouse.RightPress && (RightState == ObjectClickedState.Normal || RightState == ObjectClickedState.Released))
                {
                    RightState = ObjectClickedState.Clicked;
                    OnRightClick();
                }
                else if (ActiveMouse.RightPress && RightState == ObjectClickedState.Clicked)
                    OnRightPress();
                else if (ActiveMouse.RightRelease && RightState == ObjectClickedState.Clicked)
                {
                    RightState = ObjectClickedState.Released;
                    OnRightRelease();
                }
                else if (!ActiveMouse.RightPress && RightState == ObjectClickedState.Released)
                {
                    RightState = ObjectClickedState.Normal;
                    OnRightNormal();
                }
            }
            else
            {
                if (ActiveMouse != null)
                {
                    ActiveMouse.ClickedObject = null;
                    ActiveMouse = null;
                    OnLeave();
                }
            }
        }
    }
}

 

 

ClickableMenu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RPG
{

    class ClickableMenu : ClickableObject
    {

        private SpriteFont SpriteText
        {
            get;
            set;
        }
        private Texture2D TextureTopLeft
        {
            get;
            set;
        }
        private Texture2D TextureTopRight
        {
            get;
            set;
        }
        private Texture2D TextureBottomLeft
        {
            get;
            set;
        }
        private Texture2D TextureBottomRight
        {
            get;
            set;
        }
        private Texture2D TextureVertical
        {
            get;
            set;
        }
        private Texture2D TextureHorizontal
        {
            get;
            set;
        }
        private Texture2D TextureCenter
        {
            get;
            set;
        }

        private string Title
        {
            get;
            set;
        }

        public Vector2 DragPoint
        {
            get;
            set;
        }

        List<Button> Buttons = new List<Button>();

        public ClickableMenu(Rectangle rectangle) : base()
        {
            Rectangle = rectangle;            
            Title = "Menu";
            Visible = true;
            DragPoint = new Vector2(-1, -1);
        }

        public ClickableMenu(Rectangle rectangle, string title) : base()
        {
            Rectangle = rectangle;
            Title = title;
            Visible = true;
            DragPoint = new Vector2(-1, -1);
        }

        public void Init(Resources r)
        {
            SpriteText = r.spCustom;
            TextureTopLeft = r.texMenuTL;
            TextureTopRight = r.texMenuTR;
            TextureBottomLeft = r.texMenuBL;
            TextureBottomRight = r.texMenuBR;
            TextureVertical = r.texMenuVert;
            TextureHorizontal = r.texMenuHor;
            TextureCenter = r.texMenuC;
        }

        public void AddButton(Rectangle relativeRectangle, Resources r)
        {
            Button button = new Button(this, relativeRectangle);
            button.Init(r);
            Buttons.Add(button);
        }

        public override void OnLeftPress()
        {
            if (DragPoint == new Vector2(-1, -1))
                DragPoint = new Vector2(ActiveMouse.Rectangle.X - Rectangle.X, ActiveMouse.Rectangle.Y - Rectangle.Y);
            Rectangle = new Rectangle(ActiveMouse.Rectangle.X - (int)DragPoint.X, ActiveMouse.Rectangle.Y - (int)DragPoint.Y, Rectangle.Width, Rectangle.Height);
            foreach (Button button in Buttons)
                button.Update(this);
        }

        public override void OnLeftRelease()
        {
            DragPoint = new Vector2(-1, -1);
        }

        new public void Draw(SpriteBatch spriteBatch)
        {
            if (spriteBatch != null && TextureTopLeft != null && TextureTopRight != null && TextureBottomLeft != null && TextureBottomRight != null && TextureVertical != null && TextureHorizontal != null && TextureCenter != null && Visible)
            {
                spriteBatch.Draw(TextureTopLeft, new Rectangle(Rectangle.X, Rectangle.Y, 9, 9), Color.White);
                spriteBatch.Draw(TextureVertical, new Rectangle(Rectangle.X + 9, Rectangle.Y, Rectangle.Width - 18, 9), Color.White);
                spriteBatch.Draw(TextureTopRight, new Rectangle(Rectangle.X + (Rectangle.Width - 9), Rectangle.Y, 9, 9), Color.White);
                spriteBatch.Draw(TextureHorizontal, new Rectangle(Rectangle.X, Rectangle.Y + 9, 9, Rectangle.Height - 18), Color.White);
                spriteBatch.Draw(TextureCenter, new Rectangle(Rectangle.X + 9, Rectangle.Y + 9, Rectangle.Width - 18, Rectangle.Height - 18), Color.White);
                spriteBatch.Draw(TextureHorizontal, new Rectangle(Rectangle.X + (Rectangle.Width - 9), Rectangle.Y + 9, 9, Rectangle.Height - 18), Color.White);
                spriteBatch.Draw(TextureBottomLeft, new Rectangle(Rectangle.X, Rectangle.Y + (Rectangle.Height - 9), 9, 9), Color.White);
                spriteBatch.Draw(TextureVertical, new Rectangle(Rectangle.X + 9, Rectangle.Y + (Rectangle.Height - 9), Rectangle.Width - 18, 9), Color.White);
                spriteBatch.Draw(TextureBottomRight, new Rectangle(Rectangle.X + (Rectangle.Width - 9), Rectangle.Y + (Rectangle.Height - 9), 9, 9), Color.White);
                spriteBatch.DrawString(SpriteText, Title, new Vector2(Rectangle.X + 15, Rectangle.Y + 15), Color.White);
                foreach (Button button in Buttons)
                    button.Draw(spriteBatch);
            }
        }
    }
}

 

Button.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RPG
{
    class Button : ClickableObject
    {
        private SpriteFont SpriteText
        {
            get;
            set;
        }

        private string Title
        {
            get;
            set;
        }

        public Rectangle RelativeRectangle
        {
            get;
            set;
        }

        public Button(Object obj, Rectangle relativeRectangle) : base()
        {
            Rectangle = obj.Rectangle;
            RelativeRectangle = relativeRectangle;
            Visible = true;
            Title = "Button";
        }

        public Button(Object obj, Rectangle relativeRectangle, string title)
            : base()
        {
            Rectangle = obj.Rectangle;
            RelativeRectangle = relativeRectangle;
            Visible = true;
            Title = title;
        }

        public void Init(Resources r)
        {
            SpriteText = r.spCustom;
            Texture = r.texMenuC;
        }

        public void Update(Object obj)
        {
            Rectangle = obj.Rectangle;
        }

        new public void Draw(SpriteBatch spriteBatch)
        {
            if (spriteBatch != null && Texture != null && Visible)
            {
                spriteBatch.Draw(Texture, new Rectangle(Rectangle.X + RelativeRectangle.X, Rectangle.Y + RelativeRectangle.Y, RelativeRectangle.Width, RelativeRectangle.Height), Color.Black);
                spriteBatch.DrawString(SpriteText, Title, new Vector2(Rectangle.X + RelativeRectangle.X + 10, Rectangle.Y + RelativeRectangle.Y + 10), Color.White);
            }
        }
    }
}

 

 

and lastly the Game1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace RPG
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Resources res;

        Mouse mouse;

        ClickableMenu objMenu;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 640;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "RPG";
            objMenu = new ClickableMenu(new Rectangle(50, 400, 300, 200));
            base.Initialize();
        }

        /// <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);
            res = new Resources(this.Content);
            mouse = new Mouse(Content.Load<Texture2D>("cursor"));
            objMenu.Init(res);
            objMenu.AddButton(new Rectangle(20, 60, 80, 40), res);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            mouse.Update();
            PerformUpdates();
            base.Update(gameTime);
        }

        private void PerformUpdates()
        {
            if (mouse.ClickedObject == null)
            {
                objMenu.Update(mouse);
            }
            else
            {
                mouse.ClickedObject.Update(mouse);
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            spriteBatch.Draw(res.texBG, new Rectangle(0, 0, 800, 640), Color.White);
            objMenu.Draw(spriteBatch);
            spriteBatch.End();
            mouse.Draw(spriteBatch);
            base.Draw(gameTime);
        }
    }
}


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS