Performing component click

Hello guys, i’ve been working on simple desktop rts game and i’ve little hard work to performs a click on the right element.

Well, i’ve made this way to perform a correctly click:

namespace MGUI.Components
{
    public abstract class MGUIDrawable : DrawableGameComponent
    {
        public virtual Vector2 Location { get; set; }

        public virtual Vector2 Size { get; set; }

        public virtual bool IsVisible { get; set; }

        public virtual bool IsClickAble { get; set; }

        public Rectangle Rectangle
        {
            get
            {
                return new Rectangle(this.Location.ToPoint(), this.Size.ToPoint());
            }
        }

        public MGUIDrawable(MGUIGame Game)
            : base(Game)
        {
        }

        public virtual void click()
        {
            return;
        }
    }
}

And a cursor for perform the click:

namespace MGUI.Components
{
    public class MGUICursor : MGUIDrawable
    {
        protected MouseState MouseCursor
        {
            get
            {
                return Mouse.GetState();
            }
        }

        public override Vector2 Location
        {
            get
            {
                return this.MouseCursor.Position.ToVector2();
            }
        }

        private bool IsLeftHold { get; set; }

        public MGUICursor(MGUIGame Game)
            : base(Game)
        {
            this.DrawOrder = int.MaxValue;
        }

        public override void click()
        {
            try
            {
                this.Game.Components.OfType<MGUIDrawable>().Where((n) =>
                {
                    return !n.Location.Equals(Vector2.Zero) && n.IsVisible
                        && n.IsClickAble && n.Rectangle.Contains(this.Location);
                }).OrderByDescending(x => x.DrawOrder).First().click();
            }
            catch
            {
            }

            return;
        }

        public override void Update(GameTime gameTime)
        {
            if (this.Game.IsActive && !(this.Location.X < 0 || this.Location.Y < 0 ||
                this.Location.X >= this.Game.Window.ClientBounds.Width ||
                this.Location.Y >= this.Game.Window.ClientBounds.Height))
            {
                if (!this.IsLeftHold && this.MouseCursor.LeftButton.Equals(ButtonState.Pressed))
                {
                    this.IsLeftHold = true;
                }
                else if (this.IsLeftHold && this.MouseCursor.LeftButton.Equals(ButtonState.Released))
                {
                    this.IsLeftHold = false;
                    this.click();
                }
            }
            base.Update(gameTime);
        }
    }
}

And the game class:

namespace MGUI
{
    public abstract class MGUIGame : Game
    {
        public SpriteBatch SpriteBatch { get; private set; }

        public GraphicsDeviceManager GraphicsDeviceManager { get; private set; }

        public MGUIGame()
        {
            this.GraphicsDeviceManager = new GraphicsDeviceManager(this);
        }

        protected override void LoadContent()
        {
            this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
            this.Components.Add(new MGUICursor(this));
            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            this.GraphicsDevice.Clear(Color.CornflowerBlue);
            base.Draw(gameTime);
        }
    }
}

This is working pretty fine to me… i’ve searched for GUI Tools for monogame but no one works and needs to be refactored to work with monogame…

I think this worth it and solves the most problems about clicking on the right component (controlled by DrawOrder).

I’m thinking right? If yes, i hope this help some one on the future. Thanks :slight_smile:

Neoforce Controls works with MonoGame.

I have noticed a recent issue with the OpenGL on windows using windows 10 for the click event. But everything else works.

Cursor support needs to improve still. (Managed method to load the cursor files)

But hopefully it can also save you some time.

If you extends my main class MGUICursor and override the Draw() method, you can easily add the cursor file for mouse position.

I promisse you, i’ll check this Neoforce Controls!