Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

#ActualKhaiy

Posted 18 December 2012 - 03:05 AM

SFML already has a mouse class, so you probably won't need to make one.

The way that mine is set up is in my abstract Screen class I have a method called OnMouseClick. In the main program, after creating the window but before the game loop I register the OnMouseClick event with the Window.MouseButtonPressed event:


RenderWindow myWindow = new RenderWindow(new VideoMode(800, 600), "My Window");

myWindow.Closed += new EventHandler(OnClose);
myWindow.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(currentGameScreen.OnMouseClick);


So for every screen that needs to accept mouse input, I have code parsing any input events that are passed to them. The event handler above accepts MouseButtonEventArgs which is descended from EventArgs:


public override void OnMouseClick(object sender, EventArgs e)
{
	 // Since it comes from a mouse click event, EventArgs argument e carries an enum indicating which mouse input was dispatched with that
	 // event. So we check what it is:
	 if (e.Code == SFML.Window.Mouse.Button.Left)
	 {
		  // Check if cursor is within the button's clickable region
		  if (SFML.Mouse.GetPosition(myWindowsName) == // Within myButton's borders)
		  {
				   // Execute myButton's left mouse click function
		  }
	 }
}


The thing to remember is that the SFML Window class is set up to receive keyboard and mouse input, so the window is where the listening happens. In my game screen's base class I have a public virtual method OnMouseClick, which is empty. That way any descendant can ignore mouse input by default but still pass it along to child elements. If an object gets the mouse button event argument and uses that particular input, it does whatever it's supposed to do.

#2Khaiy

Posted 18 December 2012 - 03:02 AM

SFML already has a mouse class, so you probably won't need to make one.

The way that mine is set up is in my abstract Screen class I have a method called OnMouseClick. In the main program, after creating the window but before the game loop I register the OnMouseClick event with the Window.MouseButtonPressed event:


RenderWindow myWindow = new RenderWindow(new VideoMode(800, 600), "My Window");

myWindow.Closed += new EventHandler(OnClose);
myWindow.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(currentGameScreen.OnMouseClick);


So for every screen that needs to accept mouse input, I have code parsing any input events that are passed to them. The event handler above accepts MouseButtonEventArgs which is descended from EventArgs:


public override void OnMouseClick(object sender, EventArgs e)
{
	 // Since it comes from a mouse click event, EventArgs argument e carries an enum indicating which mouse input was dispatched with that
	 // event. So we check what it is:
	 if (e.Code == SFML.Window.Mouse.Button.Left)
	 {
		  // Check if cursor is within the button's clickable region
		  if (SFML.Mouse.GetPosition(myWindowsName) == // Within myButton's borders)
		  {
				   // Execute myButton's left mouse click function
		  }
	 }
}


The thing to remember is that the SFML Window class is set up to receive keyboard and mouse input, so the window is where the listening happens. In my game screen's base class I have a public virtual method OnMouseClick, which is empty. That way any descendant can ignore mouse input by default but still pass it along to child elements. If an object gets the mouse button event argument and uses that particular input, does whatever it's supposed to do.

#1Khaiy

Posted 18 December 2012 - 02:52 AM

SFML already has a mouse class, so you probably won't need to make one.

The way that mine is set up is in my abstract Screen class I have a method called OnMouseClick. In the main program, after creating the window but before the game loop I register the OnMouseClick event with the Window.MouseButtonPressed event:


RenderWindow myWindow = new RenderWindow(new VideoMode(800, 600), "My Window");

myWindow.Closed += new EventHandler(OnClose);
myWindow.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(currentGameScreen.OnMouseClick);


So for every screen that needs to accept mouse input, I have code parsing any input events that are passed to them. The event handler above accepts MouseButtonEventArgs which is descended from EventArgs:


public override void OnMouseClick(object sender, EventArgs e)
{
	 // Since it comes from a mouse flick event, EventArgs argument e carries an enum indicating which mouse input was dispatched with that
	 // event. So we check what it is
	 if (e.Code == SFML.Window.Mouse.Button.Left)
	 {
		  // Check if cursor is within the button's clickable region
		  if (SFML.Mouse.GetPosition(myWindowsName) == // Within myButton's borders)
		  {
				   // Execute myButton's left mouse click function
		  }
	 }
}


The thing to remember is that the SFML Window class is set up to receive keyboard and mouse input, so the window is where the listening happens. In my game screen's base class I have a public virtual method OnMouseClick, which is empty. That way any descendant can ignore mouse input by default but still pass it along to child elements. If an object gets the mouse button event argument and uses that particular input, does whatever it's supposed to do.

PARTNERS