Creating Controls

Oct 22, 2011 at 11:13 AM
Edited Oct 22, 2011 at 1:35 PM

Hi,

Editted the original post as it wasn't necessary ;-)

What's the best way to create controls?

When it comes to creating PreviewButton:Control I can't seem to set Control.Texture to a Texture2D, as it is readonly. So, should I override the Render() method and use the spritebatch to draw my own texture2D?

One last thing.. it needs to implement Clone(), which returns a Control. I've just put return this; which compiles but I don't think that's correct?

This is what I have so far.

 

public class PreviewTurnControl : Control, IContentHost
    {
        private Texture2D _controlTexture;

        public PreviewTurnControl()
        {
            this.X = 0;
            this.Y = 0;
            this.Width = 53;
            this.Height = 85;
        }

        public override void OnClicked()
        {
            base.OnClicked();
            var _gameState = Application.GameState as BattleGameState;

            _gameState.PreviewTurn = true;
        }

        public override void LoadContent(IContentCatalogue catalogue, ContentManager manager)
        {
            _controlTexture = manager.Load<Texture2D>("Textures/PreviewButton");
        }

        public override void UnloadContent(IContentCatalogue catalogue)
        {
        }

        public override Control Clone()
        {
            return this;
        }

        public override void Render(SpriteBatch spriteRenderer)
        {
            //base.Render(spriteRenderer);

            spriteRenderer.Draw(_controlTexture, new Vector2(0, 0), Color.White);

        }
    }

Thanks in advance! It doesn't seem to respond to the OnClicked() event yet, I'm passing in the PlayerInput into the Screen base constructor which has the control added to it, so not sure why it doesn't respond, but I'll figure it out

Coordinator
Oct 22, 2011 at 2:30 PM

You need to override the CanFocus getter property and return true so that the Control is added to the Screen Focusable Controls list and you can receive Input events.

Oct 22, 2011 at 2:37 PM
Thanks, figured it was something I was missing

Sent from WP7

From: indiefreaks
Sent: 22 October 2011 15:30
To: james_watton@hotmail.co.uk
Subject: Re: Creating Controls [igf:276809]

From: indiefreaks

You need to override the CanFocus getter property and return true so that the Control is added to the Screen Focusable Controls list and you can receive Input events.

Coordinator
Oct 22, 2011 at 2:51 PM

Note that you may also override the HandleInput virtual method to perform any specific input handling when the control has the focus.

Oct 22, 2011 at 5:11 PM

Is there anything else I'm missing? Still doesn't seem to like responding to my mouse input?

 

/// <summary>
    /// This is the Base Layer that displays the hud screens
    /// </summary>
    public class HudLayer : Layer, IContentHost
    {
        private TimeControls _timeControls;

        public HudLayer(GameState gameState)
            : base(gameState)
        {
            // we create a new time controls screen
            _timeControls = new TimeControls();

            // we get the GUIManager so we can add the time controls screen to it
            gameState.SunBurn.GetManager<GuiManager>(true).AddScreen(_timeControls);

            // make sure this is visible
            this.IsVisible = true;

            // set its alpha to 1f
            _timeControls.Alpha = 1f;

            // we ask the gamestate to preload this
            gameState.PreLoad(_timeControls);
        }

        public void LoadContent(IContentCatalogue catalogue, ContentManager manager)
        {
            catalogue.Add(_timeControls);
        }

        public void UnloadContent(IContentCatalogue catalogue)
        {
        }
    }


public class TimeControls : Screen, IContentHost
    {
        private PreviewTurnControl _previewTurnControl;

        public TimeControls()
            : base(true, false, Application.SunBurn.GetManager<SessionManager>(true).GetIdentifiedPlayer(LogicalPlayerIndex.One).Input)
        {
            this.IsVisible = true;
            this.Width = 500;
            this.Height = 200;
            this.X = Application.Graphics.GraphicsDevice.Viewport.Width / 2 - Width / 2;
            this.Y = 500;
            //add a new PreviewTurnControl control
_previewTurnControl = new PreviewTurnControl(); Add(_previewTurnControl); } public void LoadContent(IContentCatalogue catalogue, ContentManager manager) { catalogue.Add(_previewTurnControl); } public void UnloadContent(IContentCatalogue catalogue) { } }

public class PreviewTurnControl : Control, IContentHost
    {
        private Texture2D _controlTexture;

        public PreviewTurnControl()
        {
            this.X = 0;
            this.Y = 0;
            this.Width = 53;
            this.Height = 85;

            this.SetFocus();
            this.Clicked += OnClicked;
        }

        public override void OnClicked()
        {
            OnClicked(null, null);
            base.OnClicked();
        }

        public void OnClicked(object sender, EventArgs e)
        {
            base.OnClicked();
            var _gameState = Application.GameState as BattleGameState;

            _gameState.PreviewTurn = true;
        }

        public override bool CanFocus
        {
            get
            {
                return true;
            }
        }

        public override void LoadContent(IContentCatalogue catalogue, ContentManager manager)
        {
            _controlTexture = manager.Load<Texture2D>("Textures/PreviewButton");
        }

        public override void UnloadContent(IContentCatalogue catalogue)
        {
        }

        public override Control Clone()
        {
            return this;
        }

        public override void Render(SpriteBatch spriteRenderer)
        {
            //base.Render(spriteRenderer);

            spriteRenderer.Draw(_controlTexture, new Vector2(0, 0), Color.White);

        }
    }

Sorry for posting all the code, but I can't see what is missing. Hopefully someone else will able to spot the (probably obvious) mistake.. In the GameState I'm just using this in initialise..

            // create our new SunBurn layer to render SunBurn object instances
               _battleLayer = new BattleLayer(this);

            // create our new HudLayer
               _hudLayer = new HudLayer(this);
            
            // set PreviewTurn to false
               PreviewTurn = false;

            // we add our layer to the system so it renders
            AddLayer(_battleLayer);
            AddLayer(_hudLayer);