简体   繁体   English

C#Monogame-检测鼠标按住

[英]C# Monogame - detect mouse hold

I'm writing a complex GUI for my game's main menu. 我正在为游戏的主菜单编写一个复杂的GUI。 I have already created a ButtonGUI class with it's own constructor with the parameters of button text, text color etc. I have a background texture drawn behind the text for every button, to make it look pretty. 我已经创建了一个ButtonGUI类,它具有自己的构造函数,并带有按钮文本,文本颜色等参数。我在每个按钮的文本后均绘制了背景纹理,以使其看起来更漂亮。 So I implemented a simple mouse input system: 因此,我实现了一个简单的鼠标输入系统:

void DetectBtnInput(ButtonGUI btn)
{
    if (mouseRect.Intersects(btn.SourceRect))
    {
        btn.IsHovered = true;
        if (mouseState.LeftButton == ButtonState.Pressed) settingsWindow.isOpen = true;
    }
}

This method is called in Update(GameTime gameTime) for each instance of the ButtonGUI class. ButtonGUI类的每个实例的Update(GameTime gameTime)调用此方法。 So mouseRect = new Rectangle(mouseState.X, mouseState.Y, 1, 1); 所以mouseRect = new Rectangle(mouseState.X, mouseState.Y, 1, 1); each instance of the ButtonGUI class has a Rectangle SourceRect property, which is equal to the position of the button passed to the constructor (obviously the button's size is the same every time). ButtonGUI类的每个实例都具有Rectangle SourceRect属性,该属性等于传递给构造函数的按钮的位置(显然,每次按钮的大小都是相同的)。 The logic behind the above code is simple, if the mouse is hovering the button instance's SourceRect , btn.IsHovered is set to true, it changes text color. 上面代码的逻辑很简单,如果鼠标悬停在按钮实例的SourceRectbtn.IsHovered设置为true,它将更改文本颜色。 When clicked, my WindowGUI class instance opens with additional settings. 单击后,我的WindowGUI类实例将打开并带有其他设置。

So my aim is at making these buttons look nice and have the Windows styled button mechanics. 因此,我的目标是使这些按钮看起来不错,并具有Windows样式的按钮机制。 So I am looking for a code to check for mouse hold-like event and have for example a bool that changes the button texture or whatever I can do myself. 因此,我正在寻找一种代码来检查类似鼠标按住的事件,例如具有更改按钮纹理的bool或我自己可以做的任何事情。 But the problem is that I tried incorporating the solution people give about the previousMouseState and the newMouseState , but I am not certain as if it works in Monogame as it worked in XNA... Or was my implementation wrong? 但是问题是我尝试结合人们给出的关于previousMouseStatenewMouseState的解决方案,但是我不确定它是否像在XNA中那样在Monogame中起作用...还是我的实现错了? Can I have a clearer example on how you handle mouse-hold in your games? 关于您如何处理游戏中的鼠标按住键,我是否可以有一个更清晰的示例? Thanks in advance! 提前致谢!

If you mean the player can click and hold (as per question title), and nothing happens until they then release. 如果您是说玩家可以单击并按住(按问题标题),则在他们释放之前什么也不会发生。 previous and current state should work fine as an implementation such as. 以前和当前状态应可以很好地实现,例如。

Forgive any incorrections in syntax, hopefully you can take this as pseudo code enough to make your own. 宽恕语法上的任何错误,希望您可以将其视为足以构成自己的伪代码。

MouseState prev, curr;
curr = GetMouseState();

update()
{
  prev = curr;
  curr = GetMouseState();

  // Simple check for a mouse click (from being held down)
  if (curr == MouseState.Up && prev == MouseState.Down)
  {
    // Do mouse stuff here
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM