using System; using System.Security; using System.Runtime.InteropServices; namespace SFML { namespace Graphics { //////////////////////////////////////////////////////////// /// /// This class defines a graphical 2D text, that can be drawn on screen /// //////////////////////////////////////////////////////////// public class String2D : Drawable { //////////////////////////////////////////////////////////// /// /// Enumerate the string drawing styles /// //////////////////////////////////////////////////////////// [Flags] public enum Styles { /// Regular characters, no style Regular = 0, /// Characters are bold Bold = 1 << 0, /// Characters are in italic Italic = 1 << 1, /// Characters are underlined Underlined = 1 << 2 } //////////////////////////////////////////////////////////// /// /// Default constructor /// //////////////////////////////////////////////////////////// public String2D() : this("") { } //////////////////////////////////////////////////////////// /// /// Construct the string from a text /// /// Text to display //////////////////////////////////////////////////////////// public String2D(string text) : this(text, Font.DefaultFont) { } //////////////////////////////////////////////////////////// /// /// Construct the string from a text and a font /// /// Text to display /// Font to use //////////////////////////////////////////////////////////// public String2D(string text, Font font) : this(text, font, 30) { } //////////////////////////////////////////////////////////// /// /// Construct the string from a text, font and size /// /// Text to display /// Font to use /// Base characters size //////////////////////////////////////////////////////////// public String2D(string text, Font font, uint size) : base(sfString_Create()) { Text = text; Font = font; Size = size; } //////////////////////////////////////////////////////////// /// /// Position of the object on screen /// //////////////////////////////////////////////////////////// public override Vector2 Position { get { return new Vector2(sfString_GetX(This), sfString_GetY(This)); } set { sfString_SetPosition(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Rotation of the object, defined in degrees /// //////////////////////////////////////////////////////////// public override float Rotation { get { return sfString_GetRotation(This); } set { sfString_SetRotation(This, value); } } //////////////////////////////////////////////////////////// /// /// Vertical and horizontal scale of the object /// //////////////////////////////////////////////////////////// public override Vector2 Scale { get { return new Vector2(sfString_GetScaleX(This), sfString_GetScaleY(This)); } set { sfString_SetScale(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Origin of the transformation of the object /// (center of translation, rotation and scale) /// //////////////////////////////////////////////////////////// public override Vector2 Origin { get { return new Vector2(sfString_GetOriginX(This), sfString_GetOriginY(This)); } set { sfString_SetOrigin(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Global color of the object /// //////////////////////////////////////////////////////////// public override Color Color { get { return sfString_GetColor(This); } set { sfString_SetColor(This, value); } } //////////////////////////////////////////////////////////// /// /// Blending mode of the object /// //////////////////////////////////////////////////////////// public override BlendMode BlendMode { get { return sfString_GetBlendMode(This); } set { sfString_SetBlendMode(This, value); } } //////////////////////////////////////////////////////////// /// /// Transform a point from global coordinates into local coordinates /// (ie it applies the inverse of object's origin, translation, rotation and scale to the point) /// /// Point to transform /// Transformed point //////////////////////////////////////////////////////////// public override Vector2 TransformToLocal(Vector2 point) { Vector2 Transformed; sfString_TransformToLocal(This, point.X, point.Y, out Transformed.X, out Transformed.Y); return Transformed; } //////////////////////////////////////////////////////////// /// /// Transform a point from local coordinates into global coordinates /// (ie it applies the object's origin, translation, rotation and scale to the point) /// /// Point to transform /// Transformed point //////////////////////////////////////////////////////////// public override Vector2 TransformToGlobal(Vector2 point) { Vector2 Transformed; sfString_TransformToGlobal(This, point.X, point.Y, out Transformed.X, out Transformed.Y); return Transformed; } //////////////////////////////////////////////////////////// /// /// Text displayed /// //////////////////////////////////////////////////////////// public string Text { // TODO : use unicode functions // (convert from UTF-16 to UTF-32, and find how to marshal System.String as sfUint32*...) get {return sfString_GetText(This);} set {sfString_SetText(This, value);} } //////////////////////////////////////////////////////////// /// /// Font used to display the text /// //////////////////////////////////////////////////////////// public Font Font { get {return myFont;} set {myFont = value; sfString_SetFont(This, value != null ? value.This : IntPtr.Zero);} } //////////////////////////////////////////////////////////// /// /// Base size of characters /// //////////////////////////////////////////////////////////// public float Size { get {return sfString_GetSize(This);} set {sfString_SetSize(This, value);} } //////////////////////////////////////////////////////////// /// /// Style of the text (see Styles enum) /// //////////////////////////////////////////////////////////// public Styles Style { get {return sfString_GetStyle(This);} set {sfString_SetStyle(This, value);} } //////////////////////////////////////////////////////////// /// /// Get the string rectangle on screen /// /// String rectangle in global coordinates (doesn't include rotation) //////////////////////////////////////////////////////////// public FloatRect GetRect() { return sfString_GetRect(This); } //////////////////////////////////////////////////////////// /// /// Return the visual position of the Index-th character of the string, /// in coordinates relative to the string /// (note : translation, origin, rotation and scale are not applied) /// /// Index of the character /// Position of the Index-th character (end of string if Index is out of range) //////////////////////////////////////////////////////////// public Vector2 GetCharacterPos(uint index) { Vector2 Pos; sfString_GetCharacterPos(This, index, out Pos.X, out Pos.Y); return Pos; } //////////////////////////////////////////////////////////// /// /// Render the object into the given render window /// /// Target window //////////////////////////////////////////////////////////// internal override void Render(RenderWindow window) { sfRenderWindow_DrawString(window.This, This); } //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object /// /// Is the GC disposing the object, or is it an explicit call ? //////////////////////////////////////////////////////////// protected override void Destroy(bool disposing) { sfString_Destroy(This); } private Font myFont = Font.DefaultFont; #region Imports [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfString_Create(); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_Destroy(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetPosition(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetRotation(IntPtr This, float Rotation); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetRotation(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetScale(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetScaleX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetScaleY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetOrigin(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetOriginX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetOriginY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetColor(IntPtr This, Color Color); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Color sfString_GetColor(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetBlendMode(IntPtr This, BlendMode Mode); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern BlendMode sfString_GetBlendMode(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Vector2 sfString_TransformToLocal(IntPtr This, float PointX, float PointY, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Vector2 sfString_TransformToGlobal(IntPtr This, float PointX, float PointY, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderWindow_DrawString(IntPtr This, IntPtr String); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetWidth(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetHeight(IntPtr This); [DllImport("csfml-graphics", CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity] static extern void sfString_SetText(IntPtr This, string Text); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetFont(IntPtr This, IntPtr Font); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetSize(IntPtr This, float Size); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_SetStyle(IntPtr This, Styles Style); [DllImport("csfml-graphics", CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity] static extern string sfString_GetText(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfString_GetSize(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Styles sfString_GetStyle(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern FloatRect sfString_GetRect(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfString_GetCharacterPos(IntPtr This, uint Index, out float X, out float Y); #endregion } } }