Last active
December 6, 2015 04:30
-
-
Save zwcloud/0c0e9f52d8010e1d5164 to your computer and use it in GitHub Desktop.
a SFML dragable window demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using SFML.System; | |
using SFML.Window; | |
namespace DragableWindow | |
{ | |
class DragableWindow : Window | |
{ | |
private Vector2i grabbedOffset; | |
private bool grabbedWindow; | |
private void WindowOnClosed(object sender, EventArgs eventArgs) | |
{ | |
Window window = (Window)sender; | |
window.Close(); | |
} | |
private void OnMouseButtonPressed(object sender, MouseButtonEventArgs e) | |
{ | |
Window window = (Window)sender; | |
if (e.Button == Mouse.Button.Left) | |
{ | |
grabbedOffset = window.Position - Mouse.GetPosition(); | |
grabbedWindow = true; | |
} | |
} | |
private void OnMouseMoved(object sender, MouseMoveEventArgs e) | |
{ | |
Window window = (Window)sender; | |
if (grabbedWindow) | |
{ | |
var position = Mouse.GetPosition(); | |
var newPosition = position + grabbedOffset; | |
if (window.Position != newPosition) | |
{ | |
window.Position = newPosition; | |
System.Diagnostics.Debug.WriteLine("offset: {0}, position: {1}", grabbedOffset, window.Position); | |
} | |
} | |
} | |
private void OnMouseButtonReleased(object sender, MouseButtonEventArgs e) | |
{ | |
if (e.Button == Mouse.Button.Left) | |
{ | |
grabbedWindow = false; | |
} | |
} | |
public DragableWindow(VideoMode mode, string title, Styles style) : base(mode, title, style) | |
{ | |
this.Closed += WindowOnClosed; | |
this.MouseButtonPressed += OnMouseButtonPressed; | |
this.MouseMoved += OnMouseMoved; | |
this.MouseButtonReleased += OnMouseButtonReleased; | |
} | |
} | |
static class Program | |
{ | |
[STAThread] | |
public static void Main() | |
{ | |
Window window = new DragableWindow(new VideoMode(512, 512), "SFML Dragable Window", Styles.None); | |
// Start game loop | |
while (window.IsOpen) | |
{ | |
// Process events | |
window.DispatchEvents(); | |
// Display the rendered frame on screen | |
window.Display(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment