单游戏,如何画一个按钮<



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

但我没有找到任何关于这个主题的文档,所以我开始尝试画一个按钮,但我对皮肤有一些问题?

我不明白这意味着什么:D

请有人使用此UI的经验(Controls(MonoGame(GitHub中的链接(可以给我写一些在窗口上画一些按钮的基本命令吗?

绘制按钮的Mine命令是:

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;

非常感谢:(顺便说一句,对不起我的英语-我知道不是很好:(但我希望我抓住了主要的想法:(

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

/// <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
}

如果你什么都不懂,就提出一个问题。我会尽力为您澄清:(

最新更新