• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jadendecar

Mouse and rectangle problems

0 posts in this topic

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);
        }
    }
}
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0