Imported existing sources

This commit is contained in:
Daniel Brunner committed 2017-02-04 13:48:07 +01:00
1 parent 5678b2c7c6
commit 31f6f92506
26 files changed
+5818

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
/*
* Audio - Audio allows sound to be played during a animation.
*
* Author: Phillip Piper
* Date: 18/01/2010 5:29 PM
*
* Change log:
* 2010-01-18 JPP - Initial version
*
* To do:
*
* Copyright (C) 2010 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Media;
using System.IO;
using System.Reflection;
using System.Threading;
namespace BrightIdeasSoftware
{
/// <summary>
/// Instances of this class allow sound to be played at specified
/// points within a animation.
/// </summary>
/// <remarks>
/// <para>
/// This class uses the SoundPlayer class internally, and thus can
/// only handle system sounds and WAV sound files.
/// </para>
/// <para>A sound that is already playing cannot be paused.</para>
/// </remarks>
public class Audio : Animateable
{
#region Life and death
/// <summary>
/// Load a sound from a named resource.
/// </summary>
/// <param name="resourceName">The name of the resource including the trailing ".wav"</param>
/// <remarks>To embed a wav file, simple add it to the project, and change "Build Action"
/// to "Embedded Resource".</remarks>
/// <see cref="http://msdn.microsoft.com/en-us/library/ms950960.aspx"/>
public static Audio FromResource(string resourceName) {
Audio sound = new Audio();
sound.ResourceName = resourceName;
return sound;
}
/// <summary>
/// Create an empty Audio object
/// </summary>
public Audio() {
}
/// <summary>
/// Creates an Audio object that will play the given "wav" file
/// </summary>
/// <param name="fileName"></param>
public Audio(string fileName) {
this.FileName = fileName;
}
/// <summary>
/// Creates an Audio object that will play the given system sound
/// </summary>
/// <param name="sound"></param>
public Audio(SystemSound sound) {
this.SystemSound = sound;
}
#endregion
#region Implementation properties
/// <summary>
/// Gets or sets the name of the audio file that will be played.
/// </summary>
protected string FileName ;
protected SoundPlayer Player ;
protected string ResourceName ;
protected SystemSound SystemSound ;
#endregion
#region Animation methods
/// <summary>
/// Start the sound playing
/// </summary>
public override void Start() {
// If we are supposed to play an application resource, try to load it
if (!String.IsNullOrEmpty(this.ResourceName)) {
Assembly executingAssembly = Assembly.GetExecutingAssembly();
string assemblyName = executingAssembly.GetName().Name;
Stream stream = executingAssembly.GetManifestResourceStream(assemblyName + "." + this.ResourceName);
if (stream != null)
this.Player = new SoundPlayer(stream);
}
if (!String.IsNullOrEmpty(this.FileName)) {
this.Player = new SoundPlayer(this.FileName);
}
// We could just use Play() and let the player handle the threading for us, but:
// - there is no builtin way to know when the sound has finished
// - on XP (at least), using Play() on a Stream gives noise -- but PlaySync() works fine.
this.done = false;
Thread newThread = new Thread((ThreadStart)delegate {
if (this.SystemSound != null)
this.SystemSound.Play();
else {
if (this.Player != null)
this.Player.PlaySync();
}
this.done = true;
});
newThread.Start();
}
bool done;
/// <summary>
/// Advance the audio and return if it is done.
/// </summary>
/// <param name="elapsed"></param>
/// <returns></returns>
public override bool Tick(long elapsed) {
return !this.done;
}
/// <summary>
/// Stop the sound
/// </summary>
public override void Stop() {
if (this.SystemSound != null)
return;
if (this.Player != null) {
this.Player.Stop();
this.Player.Dispose();
this.Player = null;
}
}
#endregion
}
}
+143
View File
@@ -0,0 +1,143 @@
/*
* Sprite - A graphic item that can be animated on a animation
*
* Author: Phillip Piper
* Date: 2/3/2010 9:36 AM
*
* Change log:
* 2010-03-02 JPP - Initial version (Separated from Sprite.cs)
*
* To do:
*
* Copyright (C) 2010 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
public interface ISprite : IAnimateable
{
/// <summary>
/// Gets or sets where the sprite is located
/// </summary>
Point Location { get; set; }
/// <summary>
/// Gets or sets how transparent the sprite is.
/// 0.0 is completely transparent, 1.0 is completely opaque.
/// </summary>
float Opacity { get; set; }
/// <summary>
/// Gets or sets the scaling that is applied to the extent of the sprite.
/// The location of the sprite is not scaled.
/// </summary>
float Scale { get; set; }
/// <summary>
/// Gets or sets the size of the sprite
/// </summary>
Size Size { get; set; }
/// <summary>
/// Gets or sets the angle in degrees of the sprite.
/// 0 means no angle, 90 means right edge lifted vertical.
/// </summary>
float Spin { get; set; }
/// <summary>
/// Gets or sets the bounds of the sprite. This is boundary within which
/// the sprite will be drawn.
/// </summary>
Rectangle Bounds { get; set; }
/// <summary>
/// Gets the outer bounds of this sprite, which is normally the
/// bounds of the control that is hosting the story board.
/// Nothing outside of this rectangle will be drawn.
/// </summary>
Rectangle OuterBounds { get; }
/// <summary>
/// Gets or sets the reference rectangle in relation to which
/// the sprite will be drawn. This is normal the ClientArea of
/// the control that is hosting the story board, though it
/// could be a subarea of that control (e.g. a particular
/// cell within a ListView).
/// </summary>
/// <remarks>This value is controlled by ReferenceBoundsLocator property.</remarks>
Rectangle ReferenceBounds { get; set; }
/// <summary>
/// Gets or sets the locator that will calculate the reference rectangle
/// for the sprite.
/// </summary>
IRectangleLocator ReferenceBoundsLocator { get; set; }
/// <summary>
/// Gets or sets the point at which this sprite will always be placed.
/// </summary>
/// <remarks>
/// Most sprites play with their location as part of their animation.
/// But other just want to stay in the same place.
/// Do not set this if you use Move or Goto effects on the sprite.
/// </remarks>
IPointLocator FixedLocation { get; set; }
/// <summary>
/// Gets or sets the bounds at which this sprite will always be placed.
/// </summary>
/// <remarks>See remarks on FixedLocation</remarks>
IRectangleLocator FixedBounds { get; set; }
/// <summary>
/// Draw the sprite in its current state
/// </summary>
/// <param name="g"></param>
void Draw(Graphics g);
/// <summary>
/// Add an Effect to this sprite. This effect will run at the beginning of
/// the sprite and will have 0 duration.
/// </summary>
/// <param name="effect">The effect to be applied to the sprite</param>
void Add(IEffect effect);
/// <summary>
/// Add an Effect to this sprite. This effect will commences startTick's
/// after the sprite begins and will have 0 duration
/// </summary>
/// <param name="startTick">When will the effect begins?</param>
/// <param name="effect">What effect will be applied?</param>
void Add(long startTick, IEffect effect);
/// <summary>
/// The main entry point for adding effects to Sprites.
/// </summary>
/// <param name="startTick">When will the effect begin?</param>
/// <param name="duration">For how long will it last?</param>
/// <param name="effect">What effect will be applied?</param>
void Add(long startTick, long duration, IEffect effect);
}
}
+164
View File
@@ -0,0 +1,164 @@
/*
* ImageSprite - A sprite that draws an Image
*
* Author: Phillip Piper
* Date: 08/02/2010 6:18 PM
*
* Change log:
* 2010-02-08 JPP - Initial version
*
* To do:
*
* Copyright (C) 2010 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// An ImageSprite draws an image onto the animation, to which animations can be applied
/// </summary>
/// <remarks>The image can even be an animated GIF!</remarks>
public class ImageSprite : Sprite
{
#region Life and death
public ImageSprite(Image image) {
this.Image = image;
}
#endregion
#region Implementation properties
protected Image Image ;
#endregion
#region Sprite properties
/// <summary>
/// Gets or sets how big the image is
/// </summary>
/// <remarks>The image size cannot be set, since it is natural size multiplied by
/// the Scale property.</remarks>
public override Size Size {
get {
if (this.Image == null)
return Size.Empty;
// Internal the Image class cannot handle being accessed by multiple threads
// at the same time. So make sure the access is serialized.
lock (this.locker) {
if (this.Scale == 1.0f)
return this.Image.Size;
else
return new Size((int)(this.Image.Size.Width * this.Scale),
(int)(this.Image.Size.Height * this.Scale));
}
}
set {
}
}
#endregion
#region Sprite methods
public override void Start() {
if (this.Image != null && ImageAnimator.CanAnimate(this.Image)) {
isAnimatedImage = true;
ImageAnimator.Animate(this.Image, this.OnFrameChanged);
}
}
bool isAnimatedImage;
public override void Stop() {
if (this.isAnimatedImage)
ImageAnimator.StopAnimate(this.Image, this.OnFrameChanged);
}
public override void Draw(Graphics g) {
this.ApplyState(g);
lock (this.locker) {
if (this.Image != null) {
if (this.isAnimatedImage)
ImageAnimator.UpdateFrames(this.Image);
this.DrawTransparentBitmap(g, this.Bounds, this.Image, this.Opacity);
}
this.UnapplyState(g);
}
}
private Object locker = new object();
#endregion
#region Implementation methods
/// <summary>
/// The frame on an animated GIF has changed. Normally we would redraw, but
/// we leave that to the animation controller.
/// </summary>
/// <param name="o"></param>
/// <param name="e"></param>
private void OnFrameChanged(object o, EventArgs e) {
}
/// <summary>
/// Draw an image in a (possibilty) transluscent fashion
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="image"></param>
/// <param name="transparency"></param>
protected void DrawTransparentBitmap(Graphics g, Rectangle r, Image image, float transparency) {
if (transparency <= 0.0f)
return;
ImageAttributes imageAttributes = null;
if (transparency < 1.0f) {
imageAttributes = new ImageAttributes();
float[][] colorMatrixElements = {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, transparency, 0},
new float[] {0, 0, 0, 0, 1}};
imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrixElements));
}
Rectangle dest = new Rectangle(Point.Empty, this.Size);
g.DrawImage(image,
dest, // destination rectangle
0, 0, image.Size.Width, image.Size.Height, // source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
#endregion
}
}
+385
View File
@@ -0,0 +1,385 @@
/*
* ShapeSprite - A sprite that draws a shape
*
* Author: Phillip Piper
* Date: 08/02/2010 6:18 PM
*
* Change log:
* 2010-02-08 JPP - Initial version
*
* To do:
* 2010-02-08 Given TextSprite more formatting options
* 2010-01-18 Change ShapeSprite so it can use arbitrary Pens and Brushes
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// Instances of ShapeSprite draw a geometric shape within their bounds
/// </summary>
public class ShapeSprite : Sprite
{
protected enum Shapes
{
None,
Rectangle,
Triangle,
RoundedRectangle,
Square,
Circle,
Oval,
IsoTriangle,
EquiTriangle
}
#region Factories
public static ShapeSprite Rectangle(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.Rectangle, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite RoundedRectangle(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.RoundedRectangle, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite Circle(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.Circle, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite Oval(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.Oval, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite Square(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.Square, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite Triangle(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.Triangle, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite IsoTriangle(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.IsoTriangle, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite EquiTriangle(float penWidth, Color foreColor) {
return new ShapeSprite(Shapes.EquiTriangle, penWidth, foreColor, Color.Empty);
}
public static ShapeSprite FilledRectangle(Color backColor) {
return new ShapeSprite(Shapes.Rectangle, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledRoundedRectangle(Color backColor) {
return new ShapeSprite(Shapes.RoundedRectangle, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledCircle(Color backColor) {
return new ShapeSprite(Shapes.Circle, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledOval(Color backColor) {
return new ShapeSprite(Shapes.Oval, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledSquare(Color backColor) {
return new ShapeSprite(Shapes.Square, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledTriangle(Color backColor) {
return new ShapeSprite(Shapes.Triangle, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledIsoTriangle(Color backColor) {
return new ShapeSprite(Shapes.IsoTriangle, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite FilledEquiTriangle(Color backColor) {
return new ShapeSprite(Shapes.EquiTriangle, 0.0f, Color.Empty, backColor);
}
public static ShapeSprite Rectangle(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.Rectangle, penWidth, foreColor, backColor);
}
public static ShapeSprite RoundedRectangle(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.RoundedRectangle, penWidth, foreColor, backColor);
}
public static ShapeSprite Circle(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.Circle, penWidth, foreColor, backColor);
}
public static ShapeSprite Oval(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.Oval, penWidth, foreColor, backColor);
}
public static ShapeSprite Square(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.Square, penWidth, foreColor, backColor);
}
public static ShapeSprite Triangle(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.Triangle, penWidth, foreColor, backColor);
}
public static ShapeSprite IsoTriangle(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.IsoTriangle, penWidth, foreColor, backColor);
}
public static ShapeSprite EquiTriangle(float penWidth, Color foreColor, Color backColor) {
return new ShapeSprite(Shapes.EquiTriangle, penWidth, foreColor, backColor);
}
#endregion
#region Life and death
public ShapeSprite() :
this(Shapes.Rectangle, 2.0f, Color.DarkBlue, Color.CornflowerBlue)
{
}
protected ShapeSprite(Shapes shape, float penWidth, Color foreColor, Color backColor) {
this.Shape = shape;
this.PenWidth = penWidth;
this.ForeColor = foreColor;
this.BackColor = backColor;
this.CornerRounding = 16;
}
#endregion
#region Public properties
protected Shapes Shape ;
public float PenWidth ;
public Color ForeColor ;
public Color BackColor ;
/// <summary>
/// How rounded should the corners of a rounded rectangle be?
/// Has no impact on other shapes
/// </summary>
public float CornerRounding ;
#endregion
#region Implementation properties
/// <summary>
/// Gets whether the shape should be drawn filled in
/// </summary>
protected bool Filled {
get {
return !this.BackColor.IsEmpty;
}
}
/// <summary>
/// Gets whether the shape should be drawn with an outline
/// </summary>
protected bool Lined {
get {
return this.PenWidth > 0.0f && !this.ForeColor.IsEmpty;
}
}
#endregion
#region Sprite methods
public override void Draw(Graphics g) {
this.ApplyState(g);
// We don't draw the shape into Bounds, since the location of the
// shape is handled by a co-ordinate transformation. So we draw the
// shape as if it was at origin 0,0
this.DrawShape(g, new Rectangle(Point.Empty, this.Size), this.Opacity);
this.UnapplyState(g);
}
#endregion
#region Implementation methods
protected virtual void DrawShape(Graphics g, Rectangle r, float opacity) {
using (GraphicsPath path = this.GetGraphicsPath(r)) {
if (this.Filled) {
using (Brush b = this.GetFillBrush(r, opacity)) {
g.FillPath(b, path);
}
}
if (this.Lined) {
using (Pen p = this.GetLinePen(r, opacity)) {
g.DrawPath(p, path);
}
}
}
}
protected virtual Pen GetLinePen(Rectangle r, float opacity) {
return new Pen(this.ApplyOpacityToColor(opacity, this.ForeColor), this.PenWidth);
}
protected virtual Brush GetFillBrush(Rectangle r, float opacity) {
return new SolidBrush(this.ApplyOpacityToColor(opacity, this.BackColor));
}
protected Color ApplyOpacityToColor(float opacity, Color c) {
if (opacity < 1.0f)
return Color.FromArgb((int)(opacity * c.A), this.BackColor);
else
return c;
}
protected virtual GraphicsPath GetGraphicsPath(Rectangle r) {
GraphicsPath path = new GraphicsPath();
int minimumAxis = Math.Min(r.Width, r.Height);
Point midPoint = new Point(r.X + r.Width / 2, r.Y + r.Height / 2);
switch (this.Shape) {
case Shapes.Rectangle:
path.AddRectangle(new Rectangle(Point.Empty, r.Size));
break;
case Shapes.RoundedRectangle:
path = ShapeSprite.GetRoundedRect(r, this.CornerRounding);
break;
case Shapes.Circle:
path.AddEllipse(midPoint.X - minimumAxis / 2, midPoint.Y - minimumAxis / 2, minimumAxis, minimumAxis);
break;
case Shapes.Oval:
path.AddEllipse(r);
break;
case Shapes.Square:
path.AddRectangle(new Rectangle(midPoint.X - minimumAxis / 2, midPoint.Y - minimumAxis / 2,
minimumAxis, minimumAxis));
break;
case Shapes.Triangle:
path.AddLines(new Point[] {
new Point(r.Left, r.Top),
new Point(r.Left, r.Bottom),
new Point(r.Right, r.Top),
new Point(r.Left, r.Top)
});
break;
case Shapes.IsoTriangle:
path.AddLines(new Point[] {
new Point(r.Left, r.Bottom),
new Point(r.Right, r.Bottom),
new Point(midPoint.X, r.Top),
new Point(r.Left, r.Bottom)
});
break;
case Shapes.EquiTriangle:
//TODO
break;
default:
break;
}
return path;
}
#endregion
#region Utilities
/// <summary>
/// Return a GraphicPath that is a round cornered rectangle
/// </summary>
/// <param name="rect">The rectangle</param>
/// <param name="diameter">The diameter of the corners</param>
/// <returns>A round cornered rectagle path</returns>
/// <remarks>If I could rely on people using C# 3.0+, this should be
/// an extension method of GraphicsPath.</remarks>
public static GraphicsPath GetRoundedRect(Rectangle rect, float diameter) {
GraphicsPath path = new GraphicsPath();
if (diameter > 0) {
RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
} else {
path.AddRectangle(rect);
}
return path;
}
#endregion
}
public class ParallelogramSprite : ShapeSprite
{
public ParallelogramSprite(int horizontalSize, bool forwardSlope) {
this.HorizontalSide = horizontalSize;
this.SlopeForward = forwardSlope;
}
/// <summary>
/// Gets or sets the length of the parallel size of the shape
/// </summary>
public int HorizontalSide ;
/// <summary>
/// Gets or sets if the slope of the parallelogram is forward
/// (left edge of bottom is left of the left edge of the top
/// </summary>
public bool SlopeForward ;
protected override GraphicsPath GetGraphicsPath(Rectangle r) {
GraphicsPath path = new GraphicsPath();
if (this.SlopeForward) {
path.AddLines(new Point[] {
new Point(r.Left, r.Bottom),
new Point(Math.Min(r.Left + this.HorizontalSide, r.Right), r.Bottom),
new Point(r.Right, r.Top),
new Point(Math.Max(r.Right - this.HorizontalSide, r.Left), r.Top),
new Point(r.Left, r.Bottom)
});
} else {
path.AddLines(new Point[] {
new Point(r.Left, r.Top),
new Point(Math.Min(r.Left + this.HorizontalSide, r.Right), r.Top),
new Point(r.Right, r.Bottom),
new Point(Math.Max(r.Right - this.HorizontalSide, r.Left), r.Bottom),
new Point(r.Left, r.Top)
});
}
return path;
}
}
}
+400
View File
@@ -0,0 +1,400 @@
/*
* Sprite - A graphic item that can be animated on a animation
*
* Author: Phillip Piper
* Date: 23/10/2009 10:29 PM
*
* Change log:
* 2010-03-01 JPP - Added FixedLocation and FixedBounds properties
* 2010-02-08 JPP - Handle multithreaded access to ImageSprites
* 2009-10-23 JPP - Initial version
*
* To do:
* 2010-02-08 Given TextSprite more formatting options
* 2010-01-18 Change ShapeSprite so it can use arbitrary Pens and Brushes
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A Sprite is an animated graphic.
/// </summary>
/// <remarks>
/// <para>A sprite is animated by adding effects, which change its properties between frames.</para>
/// </remarks>
public class Sprite : Animateable, ISprite
{
#region Life and death
/// <summary>
/// Create new do nothing sprite.
/// </summary>
public Sprite() {
this.Init();
this.ReferenceBoundsLocator = new AnimationBoundsLocator();
this.ReferenceBoundsLocator.Sprite = this;
}
#endregion
#region Configuration properties
/// <summary>
/// Gets or sets where the sprite is located
/// </summary>
public virtual Point Location {
get { return location; }
set { location = value; }
}
private Point location;
/// <summary>
/// Gets or sets how transparent the sprite is.
/// 0.0 is completely transparent, 1.0 is completely opaque.
/// </summary>
public virtual float Opacity {
get { return opacity; }
set { opacity = value; }
}
private float opacity;
/// <summary>
/// Gets or sets the scaling that is applied to the extent of the sprite.
/// The location of the sprite is not scaled.
/// </summary>
public virtual float Scale {
get { return scale; }
set { scale = value; }
}
private float scale;
/// <summary>
/// Gets or sets the size of the sprite
/// </summary>
public virtual Size Size {
get { return size; }
set { size = value; }
}
private Size size;
/// <summary>
/// Gets or sets the angle in degrees of the sprite.
/// 0 means no angle, 90 means right edge lifted vertical.
/// </summary>
public virtual float Spin {
get { return spin; }
set { spin = value; }
}
private float spin;
/// <summary>
/// Gets or set if the spinning should be done around the
/// top left of the sprite. If this is false, the sprite
/// will spin around the center of the sprite.
/// </summary>
public bool SpinAroundOrigin {
get { return spinAroundOrigin; }
set { spinAroundOrigin = value; }
}
private bool spinAroundOrigin;
/// <summary>
/// Gets or sets the point at which this sprite will always be placed.
/// </summary>
/// <remarks>
/// Most sprites play with their location as part of their animation.
/// But other just want to stay in the same place.
/// Do not set this if you use Move or Goto effects on the sprite.
/// </remarks>
public IPointLocator FixedLocation {
get { return fixedLocation; }
set {
fixedLocation = value;
if (fixedLocation != null)
fixedLocation.Sprite = this;
}
}
private IPointLocator fixedLocation;
/// <summary>
/// Gets or sets the bounds at which this sprite will always be placed.
/// </summary>
/// <remarks>See remarks on FixedLocation</remarks>
public IRectangleLocator FixedBounds {
get { return fixedBounds; }
set {
fixedBounds = value;
if (fixedBounds != null)
fixedBounds.Sprite = this;
}
}
private IRectangleLocator fixedBounds;
#endregion
#region Reference properties
/// <summary>
/// Gets or sets the bounds of the sprite. This is boundary within which
/// the sprite will be drawn.
/// </summary>
public virtual Rectangle Bounds {
get {
return new Rectangle(this.Location, this.Size);
}
set {
this.Location = value.Location;
this.Size = value.Size;
}
}
/// <summary>
/// Gets the outer bounds of this sprite, which is normally the
/// bounds of the control that is hosting the story board.
/// Nothing outside of this rectangle will be drawn.
/// </summary>
public virtual Rectangle OuterBounds {
get { return this.Animation.Bounds; }
}
/// <summary>
/// Gets or sets the reference rectangle in relation to which
/// the sprite will be drawn. This is normal the ClientArea of
/// the control that is hosting the story board, though it
/// could be a subarea of that control (e.g. a particular
/// cell within a ListView).
/// </summary>
/// <remarks>This value is controlled by ReferenceBoundsLocator property.</remarks>
public virtual Rectangle ReferenceBounds {
get { return this.ReferenceBoundsLocator.GetRectangle(); }
set { this.ReferenceBoundsLocator = new FixedRectangleLocator(value); }
}
/// <summary>
/// Gets or sets the locator that will calculate the reference rectangle
/// for the sprite.
/// </summary>
public virtual IRectangleLocator ReferenceBoundsLocator {
get { return referenceBoundsLocator; }
set { referenceBoundsLocator = value; }
}
private IRectangleLocator referenceBoundsLocator;
#endregion
#region Animation methods
/// <summary>
/// Draw the sprite in its current state
/// </summary>
/// <param name="g"></param>
public virtual void Draw(Graphics g) {
}
/// <summary>
/// Set the sprite to its initial state
/// </summary>
public virtual void Init() {
this.Location = Point.Empty;
this.Opacity = 1.0f;
this.Scale = 1.0f;
this.Spin = 0.0f;
}
/// <summary>
/// The sprite should advance its state.
/// </summary>
/// <param name="elapsed">Milliseconds since Start() was called</param>
/// <returns>True if Tick() should be called again</returns>
public override bool Tick(long elapsed) {
float elapsedAsFloat = (float)elapsed;
foreach (EffectControlBlock cb in this.ControlBlocks) {
if (!cb.Stopped && cb.ScheduledStartTick <= elapsed) {
if (!cb.Started) {
cb.StartTick = elapsed;
cb.Effect.Start();
cb.Started = true;
}
if (cb.Duration > 0 && elapsed <= cb.ScheduledEndTick) {
float fractionDone = (elapsedAsFloat - cb.StartTick) / cb.Duration;
cb.Effect.Apply(fractionDone);
} else {
cb.Effect.Apply(1.0f);
cb.Effect.Stop();
cb.Stopped = true;
}
}
}
this.ApplyFixedLocations();
return (this.ControlBlocks.Exists(delegate(EffectControlBlock cb) { return !cb.Stopped; }));
}
/// <summary>
/// Apply any FixedLocation or FixedBounds properties that have been set
/// </summary>
protected void ApplyFixedLocations() {
if (this.FixedBounds != null)
this.Bounds = this.FixedBounds.GetRectangle();
else
if (this.FixedLocation != null)
this.Location = this.FixedLocation.GetPoint();
}
/// <summary>
/// Reset the sprite to its neutral state
/// </summary>
public override void Reset() {
this.Init();
// Reset the effects in reverse order so their side-effects are unwound
List<EffectControlBlock> sortedBlocks = new List<EffectControlBlock>(this.ControlBlocks);
sortedBlocks.Sort(delegate(EffectControlBlock b1, EffectControlBlock b2) {
return b2.ScheduledStartTick.CompareTo(b1.ScheduledStartTick);
});
foreach (EffectControlBlock cb in sortedBlocks) {
cb.Effect.Reset();
cb.StartTick = 0;
cb.Started = false;
cb.Stopped = false;
}
}
/// <summary>
/// Stop this sprite
/// </summary>
public override void Stop() {
foreach (EffectControlBlock cb in this.ControlBlocks) {
if (cb.Started && !cb.Stopped) {
cb.Effect.Stop();
cb.Stopped = true;
}
}
}
#endregion
#region Effect methods
/// <summary>
/// Add a run-once effect which starts with the sprite
/// </summary>
/// <param name="effect"></param>
public void Add(IEffect effect) {
this.Add(0, 0, effect);
}
/// <summary>
/// Add a run-once effect
/// </summary>
/// <param name="startTick"></param>
/// <param name="effect"></param>
public void Add(long startTick, IEffect effect) {
this.Add(startTick, 0, effect);
}
/// <summary>
/// Add an effect to this sprite
/// </summary>
/// <param name="startTick">When should the effect begin in ms since Start()</param>
/// <param name="duration">For how many milliseconds should the effect continue.
/// 0 means the effect will be applied only once.</param>
/// <param name="effect">The effect to be applied</param>
public void Add(long startTick, long duration, IEffect effect) {
EffectControlBlock cb = new EffectControlBlock(startTick, duration, effect);
effect.Sprite = this;
this.ControlBlocks.Add(cb);
}
private List<EffectControlBlock> ControlBlocks = new List<EffectControlBlock>();
#endregion
#region Drawing utility methods
/// <summary>
/// Apply any graphic state (translation, rotation, scale) to the given graphic context
/// </summary>
/// <remarks>Once the state is applied, the co-ordinates will be translated so that
/// Location is at (0,0). This is necessary for spinning to work. So when the sprite
/// draws itself, all its coordinattes should be based on 0,0, not on this.Location.
/// This means you cannot use this.Bounds when drawing.
/// g.DrawRectangle(this.Bounds, Pens.Black); will not draw your rectangle where you want.
/// g.DrawRectangle(new Rectangle(Point.Empty, this.Size), Pens.Black); will work.</remarks>
/// <param name="g">The graphic to be configured</param>
protected virtual void ApplyState(Graphics g) {
Matrix m = new Matrix();
if (this.Spin != 0) {
Rectangle r = this.Bounds;
// TODO: Make a SpinCentre property
Point spinCentre = r.Location;
if (!this.SpinAroundOrigin)
spinCentre = new Point(r.X + r.Width / 2, r.Y + r.Height / 2);
m.RotateAt(this.Spin, spinCentre);
}
m.Translate((float)this.Location.X, (float)this.Location.Y);
g.Transform = m;
}
/// <summary>
/// Remove any graphic state applied by ApplyState().
/// </summary>
/// <param name="g">The graphic to be configured</param>
protected virtual void UnapplyState(Graphics g) {
g.ResetTransform();
}
#endregion
/// <summary>
/// Instances of this class hold the state of an effect as it progresses
/// </summary>
protected class EffectControlBlock
{
public EffectControlBlock(long start, long duration, IEffect effect) {
this.ScheduledStartTick = start;
this.Duration = duration;
this.Effect = effect;
}
public long ScheduledStartTick;
public long Duration ;
public bool Started ;
public bool Stopped ;
public long StartTick ;
public long ScheduledEndTick {
get { return this.StartTick + this.Duration; }
}
public IEffect Effect ;
}
}
}
+339
View File
@@ -0,0 +1,339 @@
/*
* TextSprite - A sprite that draws text
*
* Author: Phillip Piper
* Date: 08/02/2010 6:18 PM
*
* Change log:
* 2010-03-31 JPP - Correctly calculate the height of wrapped text
* - Cleaned up
* 2010-02-29 JPP - Add more formatting options (wrap, border, background)
* 2010-02-08 JPP - Initial version
*
* To do:
*
* Copyright (C) 2010 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A TextSprite is animated text. Like all Sprites, animation is achieved
/// by adding animations to it.
/// </summary>
public class TextSprite : Sprite
{
#region Life and death
public TextSprite() {
this.Font = new Font("Tahoma", 12);
this.ForeColor = Color.Blue;
this.BackColor = Color.Empty;
}
public TextSprite(string text) {
this.Text = text;
}
public TextSprite(string text, Font font, Color foreColor) {
this.Text = text;
this.Font = font;
this.ForeColor = foreColor;
}
public TextSprite(string text, Font font, Color foreColor, Color backColor, Color borderColor, float borderWidth) {
this.Text = text;
this.Font = font;
this.ForeColor = foreColor;
this.BackColor = backColor;
this.BorderColor = borderColor;
this.BorderWidth = borderWidth;
}
#endregion
#region Configuration properties
/// <summary>
/// Gets or sets the text that will be rendered by the sprite
/// </summary>
public string Text {
get { return text; }
set { text = value; }
}
private string text;
/// <summary>
/// Gets or sets the font in which the text will be rendered.
/// This will be scaled before being used to draw the text.
/// </summary>
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the color of the text
/// </summary>
public Color ForeColor {
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor = Color.Empty;
/// <summary>
/// Gets or sets the background color of the text
/// Set this to Color.Empty to not draw a background
/// </summary>
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor = Color.Empty;
/// <summary>
/// Gets or sets the color of the border around the billboard.
/// Set this to Color.Empty to remove the border
/// </summary>
public Color BorderColor {
get { return this.borderColor; }
set { this.borderColor = value; }
}
private Color borderColor = Color.Empty;
/// <summary>
/// Gets or sets the width of the border around the text
/// </summary>
public float BorderWidth {
get { return this.borderWidth; }
set { this.borderWidth = value; }
}
private float borderWidth;
/// <summary>
/// How rounded should the corners of the border be? 0 means no rounding.
/// </summary>
/// <remarks>If this value is too large, the edges of the border will appear odd.</remarks>
public float CornerRounding {
get { return this.cornerRounding; }
set { this.cornerRounding = value; }
}
private float cornerRounding = 16.0f;
/// <summary>
/// Gets the font that will be used to draw the text or a reasonable default
/// </summary>
public Font FontOrDefault {
get {
return this.Font ?? new Font("Tahoma", 16);
}
}
/// <summary>
/// Does this text have a background?
/// </summary>
public bool HasBackground {
get {
return this.BackColor != Color.Empty;
}
}
/// <summary>
/// Does this overlay have a border?
/// </summary>
public bool HasBorder {
get {
return this.BorderColor != Color.Empty && this.BorderWidth > 0;
}
}
/// <summary>
/// Gets or sets the maximum width of the text. Text longer than this will wrap.
/// 0 means no maximum.
/// </summary>
public int MaximumTextWidth {
get { return this.maximumTextWidth; }
set { this.maximumTextWidth = value; }
}
private int maximumTextWidth = 0;
/// <summary>
/// Gets or sets the formatting that should be used on the text
/// </summary>
public StringFormat StringFormat {
get {
if (this.stringFormat == null) {
this.stringFormat = new StringFormat();
this.stringFormat.Alignment = StringAlignment.Center;
this.stringFormat.LineAlignment = StringAlignment.Center;
this.stringFormat.Trimming = StringTrimming.EllipsisCharacter;
if (!this.Wrap)
this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
}
return this.stringFormat;
}
set { this.stringFormat = value; }
}
private StringFormat stringFormat;
/// <summary>
/// Gets or sets whether the text will wrap when it exceeds its bounds
/// </summary>
public bool Wrap {
get { return wrap; }
set { wrap = value; }
}
private bool wrap;
#endregion
#region Sprite properties
/// <summary>
/// Gets the size of the drawn text. This is always calculated from the
/// natural size of the text when drawn in the correctly scaled font.
/// It cannot be set directly.
/// </summary>
public override Size Size {
get {
if (String.IsNullOrEmpty(this.Text))
return Size.Empty;
// This is a stupid hack to get a Graphics object. How should this be done?
lock (dummyImageLock) {
using (Graphics g = Graphics.FromImage(this.dummyImage)) {
return g.MeasureString(this.Text, this.ActualFont, this.CalcMaxLineWidth(), this.StringFormat).ToSize();
}
}
}
set { }
}
private Image dummyImage = new Bitmap(1, 1);
private object dummyImageLock = new object();
#endregion
#region Implementation properties
/// <summary>
/// Gets the font that will be used to draw the text. This takes
/// scaling into account.
/// </summary>
protected Font ActualFont {
get {
if (this.Scale == 1.0f)
return this.Font;
else
// TODO: Cache this font and discard it when either Font or Scale changed.
return new Font(this.Font.FontFamily, this.Font.SizeInPoints * this.Scale, this.Font.Style);
}
}
#endregion
#region Sprite methods
public override void Draw(Graphics g) {
if (String.IsNullOrEmpty(this.Text) || this.Opacity <= 0.0f)
return;
this.ApplyState(g);
this.DrawText(g, this.Text, this.Opacity);
this.UnapplyState(g);
}
#endregion
#region Drawing methods
protected void DrawText(Graphics g, string s, float opacity) {
Font f = this.ActualFont;
SizeF textSize = g.MeasureString(s, f, this.CalcMaxLineWidth(), this.StringFormat);
this.DrawBorderedText(g, new Rectangle(0, 0, 1+(int)textSize.Width, 1+(int)textSize.Height), s, f, opacity);
}
protected int CalcMaxLineWidth() {
return this.MaximumTextWidth > 0 ? (int)(this.MaximumTextWidth * this.Scale) : Int32.MaxValue;
}
protected Brush GetTextBrush(float opacity) {
if (opacity < 1.0f)
return new SolidBrush(Color.FromArgb((int)(opacity * 255), this.ForeColor));
else
return new SolidBrush(this.ForeColor);
}
protected Brush GetBackgroundBrush(float opacity) {
if (opacity < 1.0f)
return new SolidBrush(Color.FromArgb((int)(opacity * 255), this.BackColor));
else
return new SolidBrush(this.BackColor);
}
protected Pen GetBorderPen(float opacity) {
if (opacity < 1.0f)
return new Pen(Color.FromArgb((int)(opacity * 255), this.BorderColor), this.BorderWidth);
else
return new Pen(this.BorderColor, this.BorderWidth);
}
/// <summary>
/// Draw the text with a border
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="textRect">The bounds within which the text should be drawn</param>
/// <param name="text">The text to draw</param>
protected void DrawBorderedText(Graphics g, Rectangle textRect, string text, Font font, float opacity) {
Rectangle borderRect = textRect;
if (this.BorderWidth > 0.0f)
borderRect.Inflate((int)this.BorderWidth / 2, (int)this.BorderWidth / 2);
borderRect.Y -= 1; // Looks better a little higher
using (GraphicsPath path = ShapeSprite.GetRoundedRect(borderRect, this.CornerRounding * this.Scale)) {
if (this.HasBackground) {
using (Brush brush = this.GetBackgroundBrush(opacity)) {
g.FillPath(brush, path);
}
}
using (Brush textBrush = this.GetTextBrush(opacity)) {
g.DrawString(text, font, textBrush, textRect, this.StringFormat);
}
if (this.HasBorder) {
using (Pen pen = this.GetBorderPen(opacity)) {
g.DrawPath(pen, path);
}
}
}
}
#endregion
}
}