1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| public class SpriteModifier
{
// ----------------------------------- enumerations
public enum HotSpot
{
LeftCenter,
CenterCenter,
RightCenter,
LeftTop,
CenterTop,
RightTop,
LeftBottom,
CenterBottom,
RightBottom
};
// ----------------------------------- member variables
private float rotationAngle = 0;
private HotSpot rotationCenter = HotSpot.CenterCenter;
private Color tint = Color.White;
private HotSpot spot = HotSpot.CenterCenter;
private Vector2 scale = new Vector2(1,1);
private float depthLayer = 1;
private float opacity = 1;
private SpriteEffects effect = SpriteEffects.None;
// ----------------------------------- properties
public float RotationAngleRadian
{
get { return rotationAngle; }
set { rotationAngle = value; }
}
public float RotationAngleDegree
{
get { return (float)(rotationAngle * 180 / Math.PI); }
set { rotationAngle = (float)(value * Math.PI / 180); }
}
public HotSpot RotationCenter
{
get { return rotationCenter; }
set { rotationCenter = value; }
}
public Color Tint
{
get
{
return new Color(tint.R, tint.G, tint.B, (byte)(tint.A * opacity));
}
set { tint = value; }
}
public float Opacity
{
get { return opacity; }
set { opacity = value; }
}
public HotSpot Spot
{
get { return spot; }
set { spot = value; }
}
public Vector2 Scale
{
get { return scale; }
set { scale = value; }
}
public float DepthLayer
{
get { return depthLayer; }
set { depthLayer = value; }
}
public SpriteEffects Effect
{
get { return effect; }
set { effect = value; }
}
// ----------------------------------- functions
public SpriteModifier()
{
}
public SpriteModifier(Color color)
{
tint = color;
}
public SpriteModifier(float angle, HotSpot center)
{
rotationAngle = angle;
rotationCenter = center;
}
public SpriteModifier(Vector2 s)
{
scale = s;
}
public SpriteModifier(float s)
{
scale = new Vector2(s, s);
}
public SpriteModifier(float angle, HotSpot center, Vector2 s)
{
rotationAngle = angle;
rotationCenter = center;
scale = s;
}
public SpriteModifier(HotSpot hs)
{
spot = hs;
}
public SpriteModifier(HotSpot hs, float angle, HotSpot center, Vector2 s)
{
rotationAngle = angle;
rotationCenter = center;
spot = hs;
scale = s;
}
static public Vector2 ComputeHotSpotOffset(HotSpot hotSpot, int baseX, int baseY)
{
Vector2 hotSpotOffset = new Vector2(0, 0);
switch (hotSpot)
{
case HotSpot.CenterBottom:
case HotSpot.CenterCenter:
case HotSpot.CenterTop:
hotSpotOffset.X = -baseX / 2;
break;
case HotSpot.RightBottom:
case HotSpot.RightCenter:
case HotSpot.RightTop:
hotSpotOffset.X = -baseX;
break;
}
switch (hotSpot)
{
case HotSpot.CenterCenter:
case HotSpot.LeftCenter:
case HotSpot.RightCenter:
hotSpotOffset.Y = -baseY / 2;
break;
case HotSpot.CenterBottom:
case HotSpot.LeftBottom:
case HotSpot.RightBottom:
hotSpotOffset.Y = -baseY;
break;
}
return hotSpotOffset;
}
public Vector2 ComputeRotationCenter(Rectangle input)
{
if (rotationAngle == 0)
{
return new Vector2(0, 0);
}
else
{
Vector2 offset = ComputeHotSpotOffset(rotationCenter, -input.Width, -input.Height);
return offset;
}
}
public Rectangle Transform(Rectangle input)
{
input.Width = (int)(input.Width * scale.X);
input.Height = (int)(input.Height * scale.Y);
Vector2 offset = ComputeHotSpotOffset(spot, input.Width, input.Height);
input.X = (int)(input.X + offset.X);
input.Y = (int)(input.Y + offset.Y);
return input;
}
} |
Partager