/
Frame.cs
180 lines (144 loc) · 3.8 KB
/
Frame.cs
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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Monofoxe.Engine.Utils;
namespace Monofoxe.Engine.Drawing
{
/// <summary>
/// Drawable frame.
/// </summary>
public class Frame : IDrawable, ICloneable
{
/// <summary>
/// Texture atlas where frame is stored.
/// </summary>
public readonly Texture2D Texture;
/// <summary>
/// Position of the frame on the atlas.
/// </summary>
public readonly Rectangle TexturePosition;
/// <summary>
/// Width of the frame.
/// </summary>
public int Width => TexturePosition.Width;
/// <summary>
/// Height of the frame.
/// </summary>
public int Height => TexturePosition.Height;
public Vector2 Position {get; set;}
public Vector2 Scale = Vector2.One;
public Vector2 Origin;
public Angle Rotation;
public Color Color = Color.White;
/// <summary>
/// Frame's parent sprite.
/// </summary>
public Sprite ParentSprite
{
get => _parentSprite;
internal set
{
if (_parentSprite != null)
{
throw new Exception("This frame already belongs to a sprite!");
}
_parentSprite = value;
}
}
private Sprite _parentSprite = null;
public Frame(Texture2D texture, Rectangle texturePosition, Vector2 origin)
{
Texture = texture;
TexturePosition = texturePosition;
Origin = origin;
}
public void Draw() =>
Draw(Position, Origin, Scale, Rotation, Color);
public void Draw(
Vector2 position,
Vector2 origin,
Vector2 scale,
Angle rotation,
Color color,
SpriteEffects effect
)
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.Sprites);
GraphicsMgr._batch.Draw(
Texture,
position,
TexturePosition,
color,
rotation.RadiansF,
origin,
scale,
effect,
0
);
}
public void Draw(Vector2 position) =>
Draw(position, Origin, Scale, Rotation, Color);
public void Draw(Vector2 position, Vector2 origin) =>
Draw(position, origin, Scale, Rotation, Color);
public void Draw(Vector2 position, Vector2 origin, Vector2 scale, Angle rotation, Color color)
{
var mirroring = SpriteEffects.None;
// Proper negative scaling.
if (scale.X < 0)
{
mirroring = mirroring | SpriteEffects.FlipHorizontally;
scale.X *= -1;
origin.X = Width - origin.X;
}
if (scale.Y < 0)
{
mirroring = mirroring | SpriteEffects.FlipVertically;
scale.Y *= -1;
origin.Y = Height - origin.Y;
}
// Proper negative scaling.
Draw(position, origin, scale, rotation, color, mirroring);
}
public void Draw(Rectangle destRect, Angle rotation, Color color)
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.Sprites);
GraphicsMgr._batch.Draw(
Texture,
destRect,
TexturePosition,
color,
rotation.RadiansF,
// NOTE: Offsets are bugged in 3.6 and mess everything up. Disabled them for now.
Vector2.Zero, // offset,
SpriteEffects.None,
0
);
}
public void Draw(Rectangle destRect, Rectangle srcRect, Angle rotation, Color color)
{
GraphicsMgr.SwitchGraphicsMode(GraphicsMode.Sprites);
srcRect.X += TexturePosition.X;
srcRect.Y += TexturePosition.Y;
GraphicsMgr._batch.Draw(
Texture,
destRect,
srcRect,
color,
rotation.RadiansF,
// NOTE: Offsets are bugged in 3.6 and mess everything up. Disabled them for now.
Vector2.Zero, // offset,
SpriteEffects.None,
0
);
}
public object Clone()
{
var frame = new Frame(Texture, TexturePosition, Origin);
frame.Position = Position;
frame.Scale = Scale;
frame.Rotation = Rotation;
frame.Color = Color;
return frame;
}
}
}