【问题标题】:Monogame, how to draw a button<Monogame,如何画一个按钮<
【发布时间】:2021-07-14 13:00:04
【问题描述】:

谁能帮我在 Monogame 中画一些按钮? 我正在使用这个库,它应该提供基本的控件,如按钮、标签、文本框 adc..

但是我没有找到任何关于该主题的文档,所以我开始尝试绘制一个按钮,但是我的皮肤有问题?

我不明白它的含义:D

请有任何人使用此 UI 的经验 (Link on in Controls (MonoGame) GitHub) 可以给我写一些在窗口上绘制按钮的基本命令吗?

绘制按钮的我的命令是:

Manager manager = new Manager(this, _graphics);
Button myButton = new Button(manager);
Skin skin = new Skin(manager, "text");
mujButton.Width = 100;
mujButton.Height = 100;
mujButton.BackColor = Color.Red;

非常感谢你:) 顺便说一句,请原谅我的英语 - 我知道不是很好:(但我希望我抓住了主要思想:)

【问题讨论】:

  • 它说:无法创建控件。未加载皮肤

标签: c# monogame


【解决方案1】:

我从未使用过您之前提到的库。但是,您可以尝试使用这种方法:

    /// <summary>
    /// Describes the state of the button.
    /// </summary>
    public enum ButtonStatus
    {
        //The reason the enum is called ButtonStatus is to avoid confusion
        //with the XNA enum called ButtonState.
        Normal,
        MouseOver,
        Pressed,
    }

    /// <summary>
    /// Stores the appearance and functionality of a button.
    /// </summary>
    class Button : Sprite3
    {
        // Store the MouseState of the last frame.        
        private MouseState currentMouseState;
        private MouseState previousMouseState;
        private float cursor_x, cursor_y;

        // The the different state textures.
        private Texture2D texHover;
        private Texture2D texPressed;        

        // Store the current state of the button.
        private ButtonStatus buttonState = ButtonStatus.Normal;

        // Gets fired when the button is pressed.
        public event EventHandler Clicked;

        // Gets fired when the button is held down.
        public event EventHandler OnPress;

        //Constructor
        /// <summary>
        /// Constructs a new button.
        /// </summary>
        /// <param name="texture">The normal texture for the button.</param>
        /// <param name="hoverTexture">The texture drawn when the mouse is over the button.</param>
        /// <param name="pressedTexture">The texture drawn when the button has been pressed.</param>
        /// <param name="position">The position where the button will be drawn.</param>
        public Button(bool visibleZ, Texture2D texZ, Texture2D texHover,
            Texture2D texPressed, float x, float y)
         : base(visibleZ, texZ, x, y)
        {
            this.texHover = texHover;
            this.texPressed = texPressed;            
        }//constructor

        /// <summary>
        /// Updates the buttons state.
        /// </summary>
        /// <param name="gameTime">The current game time.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
            cursor_x = currentMouseState.X;
            cursor_y = currentMouseState.Y;

            bool isMouseOver = this.getBoundingBoxAA().Contains(cursor_x, cursor_y);

            if (isMouseOver && buttonState != ButtonStatus.Pressed)
            {
                buttonState = ButtonStatus.MouseOver;
            }
            else if (isMouseOver == false && buttonState != ButtonStatus.Pressed)
            {
                buttonState = ButtonStatus.Normal;
            }

            // Check if the player holds down the button.
            if (currentMouseState.LeftButton == ButtonState.Pressed &&
             previousMouseState.LeftButton == ButtonState.Released)
            {
                if (isMouseOver == true)
                {
                    // Update the button state.
                    buttonState = ButtonStatus.Pressed;

                    if (OnPress != null)
                    {
                        // Fire the OnPress event.
                        OnPress(this, EventArgs.Empty);
                    }
                }
            }

            // Check if the player releases the button.
            if (currentMouseState.LeftButton == ButtonState.Released &&
             previousMouseState.LeftButton == ButtonState.Pressed)
            {
                if (isMouseOver == true)
                {
                    // update the button state.
                    buttonState = ButtonStatus.MouseOver;
                    if (Clicked != null)
                    {
                        // Fire the clicked event.
                        Clicked(this, EventArgs.Empty);
                    }
                }

                else if (buttonState == ButtonStatus.Pressed)
                {
                    buttonState = ButtonStatus.Normal;
                }
            }
        }//end Update

        /// <summary>
        /// Draws the button.
        /// </summary>
        /// <param name="spriteBatch">A SpriteBatch that has been started</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            switch (buttonState)
            {
                case ButtonStatus.Normal:
                    spriteBatch.Draw(texBase, this.getBoundingBoxAA(), Color.White);
                    //base.Draw(spriteBatch);
                    break;
                case ButtonStatus.MouseOver:
                    spriteBatch.Draw(texHover, this.getBoundingBoxAA(), Color.White);
                    break;
                case ButtonStatus.Pressed:
                    spriteBatch.Draw(texPressed, this.getBoundingBoxAA(), Color.White);
                    break;
                default:
                    spriteBatch.Draw(texBase, this.getBoundingBoxAA(), Color.White);
                    break;
            }            
        }//end Draw
    }

如果您有任何不明白的地方,请提出问题。我会尽力为你澄清:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多