Skip to content

Instantly share code, notes, and snippets.

@hellola
Created September 5, 2012 09:58
Show Gist options
  • Save hellola/3634335 to your computer and use it in GitHub Desktop.
Save hellola/3634335 to your computer and use it in GitHub Desktop.
Shapes!
namespace MyGame
{
/// <summary>
/// The drawing class helps to manage shapes on a canvas.
/// There are helper methods like AddShape, SelectShapeAtPoint etc.
/// The drawing class can also draw all of the shapes that it contains.
/// </summary>
public class Drawing
{
/// <summary>
/// Initializes a new instance of the <see cref="MyGame.Drawing"/> class.
/// </summary>
public Drawing()
{
shapes = new List<Shape>();
}
private List<Shape> shapes;
/// <summary>
/// Gets the shapes that belong to the drawing.
/// </summary>
/// <value>
/// The shapes.
/// </value>
public List<Shape> Shapes {
get {
return shapes;
}
}
private Shape selectedShape;
/// <summary>
/// Gets the selected shape.
/// </summary>
/// <value>
/// The selected shape.
/// </value>
public Shape SelectedShape {
get {
return selectedShape;
}
}
/// <summary>
/// Adds the shape.
/// </summary>
/// <param name='s'>
/// S.
/// </param>
public void AddShape(Shape s)
{
shapes.Add(s);
}
/// <summary>
/// Selects the shape at the given point.
/// </summary>
/// <param name='point'>
/// Point.
/// </param>
public void SelectShapeAtPoint (Point2D point)
{
foreach (Shape s in Shapes) {
if (s.IsAt(point))
{
selectedShape = s;
}
return;
}
}
/// <summary>
/// Draw the collection of shapes.
/// </summary>
public void Draw ()
{
foreach (Shape s in Shapes) {
s.Draw();
}
}
}
/// <summary>
/// The shape class is used to represent a generic shape and its properties.
/// It has properties that all shapes have in common.
/// </summary>
public class Shape
{
private Color color;
private Point2D position;
private int width;
private int height;
/// <summary>
/// Initializes a new instance of the <see cref="MyGame.Shape"/> class.
/// </summary>
public Shape ()
{
width = height = 0;
}
/// <summary>
/// Gets or sets the color, used for the colour of the shape.
/// </summary>
/// <value>
/// The color.
/// </value>
public Color Color
{
get { return color; }
set { color = value; }
}
/// <summary>
/// Gets or sets the position.
/// Used for the position on the screen of the shape.
/// </summary>
/// <value>
/// The position.
/// </value>
public Point2D Position
{
get { return position; }
set { position = value; }
}
/// <summary>
/// Gets or sets the width.
/// This sets the width of the shape.
/// </summary>
/// <value>
/// The width.
/// </value>
public int Width
{
get { return width; }
set { width = value; }
}
/// <summary>
/// Gets or sets the height.
/// This controls the height of the shape.
/// </summary>
/// <value>
/// The height.
/// </value>
public int Height
{
get { return height; }
set { height = value; }
}
/// <summary>
/// Determines whether this instance is at the specified point.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is at the specified point; otherwise, <c>false</c>.
/// </returns>
/// <param name='point'>
/// If set to <c>true</c> point.
/// </param>
public bool IsAt(Point2D point)
{
return point.X >= Position.X &&
point.X <= Position.X + Width &&
point.Y >= Position.Y &&
point.Y <= Position.Y + Height;
}
/// <summary>
/// Draw this instance.
/// </summary>
public void Draw()
{
Graphics.FillRectangle(Color, Position.X, Position.Y, Width, Height);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment