using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Security;
namespace SFML
{
namespace Window
{
////////////////////////////////////////////////////////////
///
/// Enumeration of window creation styles
///
////////////////////////////////////////////////////////////
[Flags]
public enum Styles
{
/// No border / title bar (this flag and all others are mutually exclusive)
None = 0,
/// Title bar + fixed border
Titlebar = 1 << 0,
/// Titlebar + resizable border + maximize button
Resize = 1 << 1,
/// Titlebar + close button
Close = 1 << 2,
/// Fullscreen mode (this flag and all others are mutually exclusive))
Fullscreen = 1 << 3
}
////////////////////////////////////////////////////////////
///
/// Window is a rendering window ; it can create a new window
/// or connect to an existing one
///
////////////////////////////////////////////////////////////
public class Window : ObjectBase
{
////////////////////////////////////////////////////////////
///
/// Create the window with default style and creation settings
///
/// Video mode to use
/// Title of the window
////////////////////////////////////////////////////////////
public Window(VideoMode mode, string title) :
this(mode, title, Styles.Resize | Styles.Close, new ContextSettings(24, 8))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window with default creation settings
///
/// Video mode to use
/// Title of the window
/// Window style (Resize | Close by default)
////////////////////////////////////////////////////////////
public Window(VideoMode mode, string title, Styles style) :
this(mode, title, style, new ContextSettings(24, 8))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window
///
/// Video mode to use
/// Title of the window
/// Window style (Resize | Close by default)
/// Creation parameters
////////////////////////////////////////////////////////////
public Window(VideoMode mode, string title, Styles style, ContextSettings settings) :
base(sfWindow_Create(mode, title, style, ref settings))
{
myInput = new Input(sfWindow_GetInput(This));
}
////////////////////////////////////////////////////////////
///
/// Create the window from an existing control with default creation settings
///
/// Platform-specific handle of the control
////////////////////////////////////////////////////////////
public Window(IntPtr handle) :
this(handle, new ContextSettings(24, 8))
{
}
////////////////////////////////////////////////////////////
///
/// Create the window from an existing control
///
/// Platform-specific handle of the control
/// Creation parameters
////////////////////////////////////////////////////////////
public Window(IntPtr Handle, ContextSettings settings) :
base(sfWindow_CreateFromHandle(Handle, ref settings))
{
myInput = new Input(sfWindow_GetInput(This));
}
////////////////////////////////////////////////////////////
///
/// Input manager of the window
///
////////////////////////////////////////////////////////////
public Input Input
{
get {return myInput;}
}
////////////////////////////////////////////////////////////
///
/// Tell whether or not the window is opened (ie. has been created).
/// Note that a hidden window (Show(false))
/// will still return true
///
/// True if the window is opened
////////////////////////////////////////////////////////////
public virtual bool IsOpened()
{
return sfWindow_IsOpened(This);
}
////////////////////////////////////////////////////////////
///
/// Close (destroy) the window.
/// The Window instance remains valid and you can call
/// Create to recreate the window
///
////////////////////////////////////////////////////////////
public virtual void Close()
{
sfWindow_Close(This);
}
////////////////////////////////////////////////////////////
///
/// Display the window on screen
///
////////////////////////////////////////////////////////////
public virtual void Display()
{
sfWindow_Display(This);
}
////////////////////////////////////////////////////////////
///
/// Width of the rendering region of the window
///
////////////////////////////////////////////////////////////
public virtual uint Width
{
get {return sfWindow_GetWidth(This);}
}
////////////////////////////////////////////////////////////
///
/// Height of the rendering region of the window
///
////////////////////////////////////////////////////////////
public virtual uint Height
{
get {return sfWindow_GetHeight(This);}
}
////////////////////////////////////////////////////////////
///
/// Creation settings of the window
///
////////////////////////////////////////////////////////////
public virtual ContextSettings Settings
{
get {return sfWindow_GetSettings(This);}
}
////////////////////////////////////////////////////////////
///
/// Enable / disable vertical synchronization
///
/// True to enable v-sync, false to deactivate
////////////////////////////////////////////////////////////
public virtual void UseVerticalSync(bool enable)
{
sfWindow_UseVerticalSync(This, enable);
}
////////////////////////////////////////////////////////////
///
/// Show or hide the mouse cursor
///
/// True to show, false to hide
////////////////////////////////////////////////////////////
public virtual void ShowMouseCursor(bool show)
{
sfWindow_ShowMouseCursor(This, show);
}
////////////////////////////////////////////////////////////
///
/// Change the position of the mouse cursor
///
/// Left coordinate of the cursor, relative to the window
/// Top coordinate of the cursor, relative to the window
////////////////////////////////////////////////////////////
public virtual void SetCursorPosition(uint x, uint y)
{
sfWindow_SetCursorPosition(This, x, y);
}
////////////////////////////////////////////////////////////
///
/// Change the position of the window on screen.
/// Only works for top-level windows
///
/// Left position
/// Top position
////////////////////////////////////////////////////////////
public virtual void SetPosition(int x, int y)
{
sfWindow_SetPosition(This, x, y);
}
////////////////////////////////////////////////////////////
///
/// Change the size of the rendering region of the window
///
/// New width
/// New height
////////////////////////////////////////////////////////////
public virtual void SetSize(uint width, uint height)
{
sfWindow_SetSize(This, width, height);
}
////////////////////////////////////////////////////////////
///
/// Show or hide the window
///
/// True to show, false to hide
////////////////////////////////////////////////////////////
public virtual void Show(bool show)
{
sfWindow_Show(This, show);
}
////////////////////////////////////////////////////////////
///
/// Enable or disable automatic key-repeat.
/// Automatic key-repeat is enabled by default
///
/// True to enable, false to disable
////////////////////////////////////////////////////////////
public virtual void EnableKeyRepeat(bool enable)
{
sfWindow_EnableKeyRepeat(This, enable);
}
////////////////////////////////////////////////////////////
///
/// Change the window's icon
///
/// Icon's width, in pixels
/// Icon's height, in pixels
/// Array of pixels, format must be RGBA 32 bits
////////////////////////////////////////////////////////////
public virtual void SetIcon(uint width, uint height, byte[] pixels)
{
unsafe
{
fixed (byte* PixelsPtr = pixels)
{
sfWindow_SetIcon(This, width, height, PixelsPtr);
}
}
}
////////////////////////////////////////////////////////////
///
/// Activate the window as the current target
/// for rendering
///
/// True if operation was successful, false otherwise
////////////////////////////////////////////////////////////
public virtual bool SetActive()
{
return SetActive(true);
}
////////////////////////////////////////////////////////////
///
/// Activate of deactivate the window as the current target
/// for rendering
///
/// True to activate, false to deactivate (true by default)
/// True if operation was successful, false otherwise
////////////////////////////////////////////////////////////
public virtual bool SetActive(bool active)
{
return sfWindow_SetActive(This, active);
}
////////////////////////////////////////////////////////////
///
/// Limit the framerate to a maximum fixed frequency
///
/// Framerate limit, in frames per seconds (use 0 to disable limit)
////////////////////////////////////////////////////////////
public virtual void SetFramerateLimit(uint limit)
{
sfWindow_SetFramerateLimit(This, limit);
}
////////////////////////////////////////////////////////////
///
/// Get time elapsed since last frame
///
/// Time elapsed, in seconds
////////////////////////////////////////////////////////////
public virtual float GetFrameTime()
{
return sfWindow_GetFrameTime(This);
}
////////////////////////////////////////////////////////////
///
/// Change the joystick threshold, ie. the value below which
/// no move event will be generated
///
/// New threshold, in range [0, 100]
////////////////////////////////////////////////////////////
public virtual void SetJoystickThreshold(float threshold)
{
sfWindow_SetJoystickThreshold(This, threshold);
}
////////////////////////////////////////////////////////////
///
/// Wait for a new event and dispatch it to the corresponding
/// event handler
///
////////////////////////////////////////////////////////////
public void WaitAndDispatchEvents()
{
Event e;
if (WaitEvent(out e))
CallEventHandler(e);
}
////////////////////////////////////////////////////////////
///
/// Call the event handlers for each pending event
///
////////////////////////////////////////////////////////////
public void DispatchEvents()
{
Event e;
while (GetEvent(out e))
CallEventHandler(e);
}
////////////////////////////////////////////////////////////
///
/// Constructor for derived classes
///
/// Pointer to the internal object
/// Internal hack :)
////////////////////////////////////////////////////////////
protected Window(IntPtr thisPtr, int dummy) :
base(thisPtr)
{
// TODO : find a cleaner way of separating this constructor from Window(IntPtr handle)
}
////////////////////////////////////////////////////////////
///
/// Internal function to get the next event (non-blocking)
///
/// Variable to fill with the raw pointer to the event structure
/// True if there was an event, false otherwise
////////////////////////////////////////////////////////////
protected virtual bool GetEvent(out Event eventToFill)
{
return sfWindow_GetEvent(This, out eventToFill);
}
////////////////////////////////////////////////////////////
///
/// Internal function to get the next event (blocking)
///
/// Variable to fill with the raw pointer to the event structure
/// False if any error occured
////////////////////////////////////////////////////////////
protected virtual bool WaitEvent(out Event eventToFill)
{
return sfWindow_WaitEvent(This, out eventToFill);
}
////////////////////////////////////////////////////////////
///
/// Handle the destruction of the object
///
/// Is the GC disposing the object, or is it an explicit call ?
////////////////////////////////////////////////////////////
protected override void Destroy(bool disposing)
{
sfWindow_Destroy(This);
}
////////////////////////////////////////////////////////////
///
/// Call the event handler for the given event
///
/// Event to dispatch
////////////////////////////////////////////////////////////
private void CallEventHandler(Event e)
{
switch (e.Type)
{
case EventType.Closed :
if (Closed != null)
Closed(this, EventArgs.Empty);
break;
case EventType.GainedFocus :
if (GainedFocus != null)
GainedFocus(this, EventArgs.Empty);
break;
case EventType.JoyButtonPressed :
if (JoyButtonPressed != null)
JoyButtonPressed(this, new JoyButtonEventArgs(e.JoyButton));
break;
case EventType.JoyButtonReleased :
if (JoyButtonReleased != null)
JoyButtonReleased(this, new JoyButtonEventArgs(e.JoyButton));
break;
case EventType.JoyMoved :
if (JoyMoved != null)
JoyMoved(this, new JoyMoveEventArgs(e.JoyMove));
break;
case EventType.KeyPressed :
if (KeyPressed != null)
KeyPressed(this, new KeyEventArgs(e.Key));
break;
case EventType.KeyReleased :
if (KeyReleased != null)
KeyReleased(this, new KeyEventArgs(e.Key));
break;
case EventType.LostFocus :
if (LostFocus != null)
LostFocus(this, EventArgs.Empty);
break;
case EventType.MouseButtonPressed :
if (MouseButtonPressed != null)
MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton));
break;
case EventType.MouseButtonReleased :
if (MouseButtonReleased != null)
MouseButtonReleased(this, new MouseButtonEventArgs(e.MouseButton));
break;
case EventType.MouseEntered :
if (MouseEntered != null)
MouseEntered(this, EventArgs.Empty);
break;
case EventType.MouseLeft :
if (MouseLeft != null)
MouseLeft(this, EventArgs.Empty);
break;
case EventType.MouseMoved :
if (MouseMoved != null)
MouseMoved(this, new MouseMoveEventArgs(e.MouseMove));
break;
case EventType.MouseWheelMoved :
if (MouseWheelMoved != null)
MouseWheelMoved(this, new MouseWheelEventArgs(e.MouseWheel));
break;
case EventType.Resized :
if (Resized != null)
Resized(this, new SizeEventArgs(e.Size));
break;
case EventType.TextEntered :
if (TextEntered != null)
TextEntered(this, new TextEventArgs(e.Text));
break;
}
}
/// Event handler for the Closed event
public event EventHandler Closed = null;
/// Event handler for the Resized event
public event EventHandler Resized = null;
/// Event handler for the LostFocus event
public event EventHandler LostFocus = null;
/// Event handler for the GainedFocus event
public event EventHandler GainedFocus = null;
/// Event handler for the TextEntered event
public event EventHandler TextEntered = null;
/// Event handler for the KeyPressed event
public event EventHandler KeyPressed = null;
/// Event handler for the KeyReleased event
public event EventHandler KeyReleased = null;
/// Event handler for the MouseWheelMoved event
public event EventHandler MouseWheelMoved = null;
/// Event handler for the MouseButtonPressed event
public event EventHandler MouseButtonPressed = null;
/// Event handler for the MouseButtonReleased event
public event EventHandler MouseButtonReleased = null;
/// Event handler for the MouseMoved event
public event EventHandler MouseMoved = null;
/// Event handler for the MouseEntered event
public event EventHandler MouseEntered = null;
/// Event handler for the MouseLeft event
public event EventHandler MouseLeft = null;
/// Event handler for the JoyButtonPressed event
public event EventHandler JoyButtonPressed = null;
/// Event handler for the JoyButtonReleased event
public event EventHandler JoyButtonReleased = null;
/// Event handler for the JoyMoved event
public event EventHandler JoyMoved = null;
protected Input myInput = null;
#region Imports
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfWindow_Create(VideoMode Mode, string Title, Styles Style, ref ContextSettings Params);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfWindow_CreateFromHandle(IntPtr Handle, ref ContextSettings Params);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_Destroy(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern IntPtr sfWindow_GetInput(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern bool sfWindow_IsOpened(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_Close(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern bool sfWindow_GetEvent(IntPtr This, out Event Evt);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern bool sfWindow_WaitEvent(IntPtr This, out Event Evt);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_Display(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern uint sfWindow_GetWidth(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern uint sfWindow_GetHeight(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern ContextSettings sfWindow_GetSettings(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_UseVerticalSync(IntPtr This, bool Enable);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_ShowMouseCursor(IntPtr This, bool Show);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_SetCursorPosition(IntPtr This, uint X, uint Y);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_SetPosition(IntPtr This, int X, int Y);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_SetSize(IntPtr This, uint Width, uint Height);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_Show(IntPtr This, bool Show);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_EnableKeyRepeat(IntPtr This, bool Enable);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
unsafe static extern void sfWindow_SetIcon(IntPtr This, uint Width, uint Height, byte* Pixels);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern bool sfWindow_SetActive(IntPtr This, bool Active);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_SetFramerateLimit(IntPtr This, uint Limit);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern float sfWindow_GetFrameTime(IntPtr This);
[DllImport("csfml-window"), SuppressUnmanagedCodeSecurity]
static extern void sfWindow_SetJoystickThreshold(IntPtr This, float Threshold);
#endregion
}
}
}