using System; using System.Runtime.InteropServices; using System.Security; namespace SFML { namespace Graphics { //////////////////////////////////////////////////////////// /// /// Shape defines a drawable convex shape ; it also defines /// helper functions to draw simple shapes like /// lines, rectangles, circles, etc. /// //////////////////////////////////////////////////////////// public class Shape : Drawable { //////////////////////////////////////////////////////////// /// /// Default constructor /// //////////////////////////////////////////////////////////// public Shape() : base(sfShape_Create()) { } //////////////////////////////////////////////////////////// /// /// Position of the object on screen /// //////////////////////////////////////////////////////////// public override Vector2 Position { get { return new Vector2(sfShape_GetX(This), sfShape_GetY(This)); } set { sfShape_SetPosition(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Rotation of the object, defined in degrees /// //////////////////////////////////////////////////////////// public override float Rotation { get { return sfShape_GetRotation(This); } set { sfShape_SetRotation(This, value); } } //////////////////////////////////////////////////////////// /// /// Vertical and horizontal scale of the object /// //////////////////////////////////////////////////////////// public override Vector2 Scale { get { return new Vector2(sfShape_GetScaleX(This), sfShape_GetScaleY(This)); } set { sfShape_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(sfShape_GetOriginX(This), sfShape_GetOriginY(This)); } set { sfShape_SetOrigin(This, value.X, value.Y); } } //////////////////////////////////////////////////////////// /// /// Global color of the object /// //////////////////////////////////////////////////////////// public override Color Color { get { return sfShape_GetColor(This); } set { sfShape_SetColor(This, value); } } //////////////////////////////////////////////////////////// /// /// Blending mode of the object /// //////////////////////////////////////////////////////////// public override BlendMode BlendMode { get { return sfShape_GetBlendMode(This); } set { sfShape_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; sfShape_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; sfShape_TransformToGlobal(This, point.X, point.Y, out Transformed.X, out Transformed.Y); return Transformed; } //////////////////////////////////////////////////////////// /// /// Add a point to the shape /// /// Position of the point /// Color of the point //////////////////////////////////////////////////////////// public void AddPoint(Vector2 position, Color color) { AddPoint(position, color, Color.Black); } //////////////////////////////////////////////////////////// /// /// Add a point to the shape /// /// Position of the point /// Color of the point /// Outline color of the point //////////////////////////////////////////////////////////// public void AddPoint(Vector2 position, Color color, Color outlineColor) { sfShape_AddPoint(This, position.X, position.Y, color, outlineColor); } //////////////////////////////////////////////////////////// /// /// Enable or disable filling the shape. /// Fill is enabled by default /// /// True to enable, false to disable //////////////////////////////////////////////////////////// public void EnableFill(bool enable) { sfShape_EnableFill(This, enable); } //////////////////////////////////////////////////////////// /// /// Enable or disable drawing the shape outline. /// Outline is enabled by default /// /// True to enable, false to disable //////////////////////////////////////////////////////////// public void EnableOutline(bool enable) { sfShape_EnableOutline(This, enable); } //////////////////////////////////////////////////////////// /// /// Width of the shape outline /// //////////////////////////////////////////////////////////// public float OutlineWidth { get {return sfShape_GetOutlineWidth(This);} set {sfShape_SetOutlineWidth(This, value);} } //////////////////////////////////////////////////////////// /// /// Totla number of points of the shape /// //////////////////////////////////////////////////////////// public uint NbPoints { get {return sfShape_GetNbPoints(This);} } //////////////////////////////////////////////////////////// /// /// Set the position of a point /// /// Index of the point, in range [0, NbPoints - 1] /// New position of the index-th point //////////////////////////////////////////////////////////// public void SetPointPosition(uint index, Vector2 position) { sfShape_SetPointPosition(This, index, position.X, position.Y); } //////////////////////////////////////////////////////////// /// /// Get the position of a point /// /// Index of the point, in range [0, NbPoints - 1] /// Position of the index-th point //////////////////////////////////////////////////////////// public Vector2 GetPointPosition(uint index) { Vector2 Pos; sfShape_GetPointPosition(This, index, out Pos.X, out Pos.Y); return Pos; } //////////////////////////////////////////////////////////// /// /// Set the color of a point /// /// Index of the point, in range [0, NbPoints - 1] /// New color of the index-th point //////////////////////////////////////////////////////////// public void SetPointColor(uint index, Color color) { sfShape_SetPointColor(This, index, color); } //////////////////////////////////////////////////////////// /// /// Get the color of a point /// /// Index of the point, in range [0, NbPoints - 1] /// Color of the index-th point //////////////////////////////////////////////////////////// public Color GetPointColor(uint index) { return sfShape_GetPointColor(This, index); } //////////////////////////////////////////////////////////// /// /// Set the outline color of a point /// /// Index of the point, in range [0, NbPoints - 1] /// New outline color of the index-th point //////////////////////////////////////////////////////////// public void SetPointOutlineColor(uint index, Color color) { sfShape_SetPointOutlineColor(This, index, color); } //////////////////////////////////////////////////////////// /// /// Get the outline color of a point /// /// Index of the point, in range [0, NbPoints - 1] /// Outline color of the index-th point //////////////////////////////////////////////////////////// public Color GetPointOutlineColor(uint index) { return sfShape_GetPointOutlineColor(This, index); } //////////////////////////////////////////////////////////// /// /// Create a shape made of a single line /// /// Position of the first point /// Position of the second point /// Line thickness /// Color used to draw the line /// New line shape built with the given parameters //////////////////////////////////////////////////////////// public static Shape Line(Vector2 p1, Vector2 p2, float thickness, Color color) { return Line(p1, p2, thickness, color, 0, Color.White); } //////////////////////////////////////////////////////////// /// /// Create a shape made of a single line /// /// Position of the first point /// Position of the second point /// Line thickness /// Color used to draw the line /// Outline width /// Color used to draw the outline /// New line shape built with the given parameters //////////////////////////////////////////////////////////// public static Shape Line(Vector2 p1, Vector2 p2, float thickness, Color color, float outline, Color outlineColor) { return new Shape(sfShape_CreateLine(p1.X, p1.Y, p2.X, p2.Y, thickness, color, outline, outlineColor)); } //////////////////////////////////////////////////////////// /// /// Create a shape made of a single rectangle /// /// Position of the top-left corner /// Position of the bottom-right corner /// Color used to fill the rectangle /// New rectangle shape built with the given parameters //////////////////////////////////////////////////////////// public static Shape Rectangle(Vector2 p1, Vector2 p2, Color color) { return Rectangle(p1, p2, color, 0, Color.White); } //////////////////////////////////////////////////////////// /// /// Create a shape made of a single rectangle /// /// Position of the top-left corner /// Position of the bottom-right corner /// Color used to fill the rectangle /// Outline width /// Color used to draw the outline /// New rectangle shape built with the given parameters //////////////////////////////////////////////////////////// public static Shape Rectangle(Vector2 p1, Vector2 p2, Color color, float outline, Color outlineColor) { return new Shape(sfShape_CreateRectangle(p1.X, p1.Y, p2.X, p2.Y, color, outline, outlineColor)); } //////////////////////////////////////////////////////////// /// /// Create a shape made of a single circle /// /// Position of the center /// Radius of the circle /// Color used to fill the circle /// New circle shape built with the given parameters //////////////////////////////////////////////////////////// public static Shape Circle(Vector2 center, float radius, Color color) { return Circle(center, radius, color, 0, Color.White); } //////////////////////////////////////////////////////////// /// /// Create a shape made of a single circle /// /// Position of the center /// Radius of the circle /// Color used to fill the circle /// Outline width /// Color used to draw the outline /// New circle shape built with the given parameters //////////////////////////////////////////////////////////// public static Shape Circle(Vector2 center, float radius, Color color, float outline, Color outlineColor) { return new Shape(sfShape_CreateCircle(center.X, center.Y, radius, color, outline, outlineColor)); } //////////////////////////////////////////////////////////// /// /// Render the object into the given render window /// /// Target render window //////////////////////////////////////////////////////////// internal override void Render(RenderWindow target) { sfRenderWindow_DrawShape(target.This, This); } //////////////////////////////////////////////////////////// /// /// Render the object into the given render image /// /// Target render image //////////////////////////////////////////////////////////// internal override void Render(RenderImage target) { sfRenderImage_DrawShape(target.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) { sfShape_Destroy(This); } //////////////////////////////////////////////////////////// /// /// Internal constructor /// /// Pointer to the internal object in C library //////////////////////////////////////////////////////////// private Shape(IntPtr thisPtr) : base(thisPtr) { } #region Imports [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfShape_Create(); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_Destroy(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetPosition(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetRotation(IntPtr This, float Rotation); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetRotation(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetScale(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetScaleX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetScaleY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetOrigin(IntPtr This, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetOriginX(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetOriginY(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetColor(IntPtr This, Color Color); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Color sfShape_GetColor(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetBlendMode(IntPtr This, BlendMode Mode); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern BlendMode sfShape_GetBlendMode(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Vector2 sfShape_TransformToLocal(IntPtr This, float PointX, float PointY, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Vector2 sfShape_TransformToGlobal(IntPtr This, float PointX, float PointY, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderWindow_DrawShape(IntPtr This, IntPtr Shape); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfRenderImage_DrawShape(IntPtr This, IntPtr Shape); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfShape_CreateLine(float P1X, float P1Y, float P2X, float P2Y, float Thickness, Color Col, float Outline, Color OutlineCol); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfShape_CreateRectangle(float P1X, float P1Y, float P2X, float P2Y, Color Col, float Outline, Color OutlineCol); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfShape_CreateCircle(float X, float Y, float Radius, Color Col, float Outline, Color OutlineCol); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_AddPoint(IntPtr This, float X, float Y, Color Col, Color OutlineCol); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_EnableFill(IntPtr This, bool Enable); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_EnableOutline(IntPtr This, bool Enable); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetOutlineWidth(IntPtr This, float Width); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern float sfShape_GetOutlineWidth(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern uint sfShape_GetNbPoints(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetPointPosition(IntPtr This, uint Index, float X, float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_GetPointPosition(IntPtr This, uint Index, out float X, out float Y); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetPointColor(IntPtr This, uint Index, Color Col); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Color sfShape_GetPointColor(IntPtr This, uint Index); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfShape_SetPointOutlineColor(IntPtr This, uint Index, Color Col); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern Color sfShape_GetPointOutlineColor(IntPtr This, uint Index); #endregion } } }