Skip to content

Instantly share code, notes, and snippets.

@eXpl0it3r
Last active June 28, 2024 12:40
Show Gist options
  • Save eXpl0it3r/4cea3aa2e046fc335f99a8b7f0e5ed98 to your computer and use it in GitHub Desktop.
Save eXpl0it3r/4cea3aa2e046fc335f99a8b7f0e5ed98 to your computer and use it in GitHub Desktop.
SFML.Net Mouse Behavior Test
using SFML.Graphics;
using SFML.System;
using SFML.Window;
var window = new RenderWindow(new VideoMode(800, 600), "SFML Test", Styles.None);
window.SetFramerateLimit(30);
var font = new Font("C:/Windows/Fonts/Arial.ttf");
var text = new Text();
text.Font = font;
var textAction = new Text();
textAction.Font = font;
textAction.Position = new Vector2f(0, 50);
var region = new RectangleShape(new Vector2f(5, 5));
region.FillColor = Color.Green;
region.Position = new Vector2f(200, 0);
var pressed = false;
var relativePosition = new Vector2i();
window.Closed += (sender, _) => ((RenderWindow)sender).Close();
window.MouseMoved += (_, e) =>
{
text.DisplayedString = $"({e.X}, {e.Y})";
if (pressed && relativePosition != new Vector2i(0, 0))
{
window.Position += new Vector2i(e.X, e.Y) - relativePosition;
}
};
window.MouseButtonPressed += (_, e) =>
{
var position = new Vector2i(e.X, e.Y);
pressed = true;
if (region.GetGlobalBounds().Contains(position.X, position.Y))
{
relativePosition = position;
}
};
window.MouseButtonReleased += (_, _) =>
{
pressed = false;
relativePosition = new Vector2i(0, 0);
};
while (window.IsOpen)
{
window.DispatchEvents();
textAction.DisplayedString = pressed ? "Pressed" : "Released";
window.Clear();
window.Draw(region);
window.Draw(text);
window.Draw(textAction);
window.Display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment