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 Text : 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 Text() : this("") { } //////////////////////////////////////////////////////////// /// /// Construct the text from a string /// /// String to display //////////////////////////////////////////////////////////// public Text(string str) : this(str, Font.DefaultFont) { } //////////////////////////////////////////////////////////// /// /// Construct the text from a string and a font /// /// String to display /// Font to use //////////////////////////////////////////////////////////// public Text(string str, Font font) : this(str, font, 30) { } //////////////////////////////////////////////////////////// /// /// Construct the text from a string, font and size /// /// String to display /// Font to use /// Base characters size //////////////////////////////////////////////////////////// public Text(string str, Font font, uint size) : base(sfText_Create()) { DisplayedString = str; Font = font; Size = size; } //////////////////////////////////////////////////////////// /// /// Position of the object on screen /// //////////////////////////////////////////////////////////// public override Vector2 Position { get { return new Vector2(sfText_GetX(This), sfText_GetY(This)); } set { sfText_SetPosition(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Rotation of the object, defined in degrees /// //////////////////////////////////////////////////////////// public override float Rotation { get { return sfText_GetRotation(This); } set { sfText_SetRotation(This, value); } } //////////////////////////////////////////////////////////// /// /// Vertical and horizontal scale of the object /// //////////////////////////////////////////////////////////// public override Vector2 Scale { get { return new Vector2(sfText_GetScaleX(This), sfText_GetScaleY(This)); } set { sfText_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(sfText_GetOriginX(This), sfText_GetOriginY(This)); } set { sfText_SetOrigin(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Global color of the object /// //////////////////////////////////////////////////////////// public override Color Color { get { return sfText_GetColor(This); } set { sfText_SetColor(This, value); } } //////////////////////////////////////////////////////////// /// /// Blending mode of the object /// //////////////////////////////////////////////////////////// public override BlendMode BlendMode { get { return sfText_GetBlendMode(This); } set { sfText_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; sfText_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; sfText_TransformToGlobal(This, point.X, point.Y, out Transformed.X, out Transformed.Y); return Transformed; } //////////////////////////////////////////////////////////// /// /// String which is displayed /// //////////////////////////////////////////////////////////// public string DisplayedString { // TODO : use unicode functions // (convert from UTF-16 to UTF-32, and find how to marshal System.String as sfUint32*...) get {return sfText_GetString(This);} set {sfText_SetString(This, value);} } //////////////////////////////////////////////////////////// /// /// Font used to display the text /// //////////////////////////////////////////////////////////// public Font Font { get {return myFont;} set {myFont = value; sfText_SetFont(This, value != null ? value.This : IntPtr.Zero);} } //////////////////////////////////////////////////////////// /// /// Base size of characters /// //////////////////////////////////////////////////////////// public uint Size { get {return sfText_GetCharacterSize(This);} set {sfText_SetCharacterSize(This, value);} } //////////////////////////////////////////////////////////// /// /// Style of the text (see Styles enum) /// //////////////////////////////////////////////////////////// public Styles Style { get {return sfText_GetStyle(This);} set {sfText_SetStyle(This, value);} } //////////////////////////////////////////////////////////// /// /// Get the text rectangle on screen /// /// Text rectangle in global coordinates (doesn't include rotation) //////////////////////////////////////////////////////////// public FloatRect GetRect() { return sfText_GetRect(This); } //////////////////////////////////////////////////////////// /// /// Return the visual position of the Index-th character of the text, /// in coordinates relative to the text /// (note : translation, origin, rotation and scale are not applied) /// /// Index of the character /// Position of the Index-th character (end of text if Index is out of range) //////////////////////////////////////////////////////////// public Vector2 GetCharacterPos(uint index) { Vector2 Pos; sfText_GetCharacterPos(This, index, out Pos.X, out Pos.Y); return Pos; } //////////////////////////////////////////////////////////// /// /// Render the object into the given render window /// /// Target render window /// Shader to apply //////////////////////////////////////////////////////////// internal override void Render(RenderWindow target, Shader shader) { if (shader == null) sfRenderWindow_DrawText(target.This, This); else sfRenderWindow_DrawTextWithShader(target.This, This, shader.This); } //////////////////////////////////////////////////////////// /// /// Render the object into the given render image /// /// Target render image /// Shader to apply //////////////////////////////////////////////////////////// internal override void Render(RenderImage target, Shader shader) { if (shader == null) sfRenderImage_DrawText(target.This, This); else sfRenderImage_DrawTextWithShader(target.This, This, shader.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) { sfText_Destroy(This); } private Font myFont = Font.DefaultFont; #region Imports [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfText_Create(); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_Destroy(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetPosition(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetRotation(IntPtr This, float Rotation); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetRotation(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetScale(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetScaleX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetScaleY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetOrigin(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetOriginX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfText_GetOriginY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetColor(IntPtr This, Color Color); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Color sfText_GetColor(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetBlendMode(IntPtr This, BlendMode Mode); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern BlendMode sfText_GetBlendMode(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Vector2 sfText_TransformToLocal(IntPtr This, float PointX, float PointY, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Vector2 sfText_TransformToGlobal(IntPtr This, float PointX, float PointY, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderWindow_DrawText(IntPtr This, IntPtr String); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderWindow_DrawTextWithShader(IntPtr This, IntPtr String, IntPtr Shader); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_DrawText(IntPtr This, IntPtr String); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_DrawTextWithShader(IntPtr This, IntPtr String, IntPtr Shader); [DllImport("csfml-graphics", CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity] static extern void sfText_SetString(IntPtr This, string Text); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetFont(IntPtr This, IntPtr Font); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetCharacterSize(IntPtr This, uint Size); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_SetStyle(IntPtr This, Styles Style); [DllImport("csfml-graphics", CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity] static extern string sfText_GetString(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern uint sfText_GetCharacterSize(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Styles sfText_GetStyle(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern FloatRect sfText_GetRect(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfText_GetCharacterPos(IntPtr This, uint Index, out float X, out float Y); #endregion } } }