SadConsole also supports mouse input, it works very similar to keyboard input.
Where it also works both on a console and on a global scale.
ProcessMouse method on console
The ProcessMouse method provides a MouseScreenObjectState which contains several
methods that can help you determine where the mouse is situated.
For example, you can see if the mouse is on the console by using state.IsOnScreenObject
This checks if the mouse is within the given console.
An alternative is state.Mouse.IsOnScreen this checks if the mouse is anywhere within the rendering frame, so not just the console.
You also can see the position on the state:
- SurfacePixelPosition (The position of the pixel on the console where the mouse is)
- SurfaceCellPosition (The position of the cell on the console where the mouse is)
- WorldCellPosition (A cell-based location of the mouse based on the screen, not the console)
- CellPosition (The position of the state.Cell (the cell where the mouse is on))
Example setting background color on mouse
private Point? _prevCell;
protected override void OnMouseExit(MouseScreenObjectState state)
{
if (_prevCell != null)
{
Surface.SetBackground(_prevCell.Value.X, _prevCell.Value.Y, Surface.DefaultBackground);
_prevCell = null;
}
base.OnMouseExit(state);
}
public override bool ProcessMouse(MouseScreenObjectState state)
{
if (state.IsOnScreenObject)
{
// Reset color
if (_prevCell != null)
Surface.SetBackground(_prevCell.Value.X, _prevCell.Value.Y, Surface.DefaultBackground);
// Change current cell color
_prevCell = state.SurfaceCellPosition;
Surface.SetBackground(_prevCell.Value.X, _prevCell.Value.Y, Color.Blue);
}
else
{
if (_prevCell != null)
{
Surface.SetBackground(_prevCell.Value.X, _prevCell.Value.Y, Surface.DefaultBackground);
_prevCell = null;
}
}
return base.ProcessMouse(state);
}
Global mouse state
The mouse can be accessed on:
Game.Instance.Mouse
It is usually done in the FrameUpdate event which runs every frame.
static void OnFrameUpdate(object sender, GameHost host)
{
if (Game.Instance.Mouse.LeftClicked)
{
System.Console.WriteLine("Mouse was left clicked!");
}
}
Add it to the game configuration:
new Game.Configuration().UseFrameUpdateEvent(OnFrameUpdate)
Thats it for mouse input!
