This repository was archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSubtexture.cs
More file actions
167 lines (138 loc) · 4.13 KB
/
Copy pathSubtexture.cs
File metadata and controls
167 lines (138 loc) · 4.13 KB
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
using System;
using System.Numerics;
namespace Foster.Framework;
/// <summary>
/// A Subtexture, representing a rectangular segment of a Texture
/// </summary>
public class Subtexture
{
/// <summary>
/// The Texture coordinates. These are set automatically based on the Source rectangle
/// </summary>
public readonly Vector2[] TexCoords = new Vector2[4];
/// <summary>
/// The draw coordinates. These are set automatically based on the Source and Frame rectangle
/// </summary>
public readonly Vector2[] DrawCoords = new Vector2[4];
/// <summary>
/// The Texture this Subtexture is... a subtexture of
/// </summary>
public Texture? Texture
{
get => texture;
set
{
if (texture != value)
{
texture = value;
UpdateCoords();
}
}
}
/// <summary>
/// The source rectangle to sample from the Texture
/// </summary>
public Rect Source
{
get => source;
set
{
source = value;
UpdateCoords();
}
}
/// <summary>
/// The frame of the Subtexture. This is useful if you trim transparency and want to store the original size of the image
/// For example, if the original image was (64, 64), but the trimmed version is (32, 48), the Frame may be (-16, -8, 64, 64)
/// </summary>
public Rect Frame
{
get => frame;
set
{
frame = value;
UpdateCoords();
}
}
/// <summary>
/// The Draw Width of the Subtexture
/// </summary>
public float Width => frame.Width;
/// <summary>
/// The Draw Height of the Subtexture
/// </summary>
public float Height => frame.Height;
private Texture? texture;
private Rect frame;
private Rect source;
public Subtexture()
{
}
public Subtexture(Texture texture)
: this(texture, new Rect(0, 0, texture.Width, texture.Height))
{
}
public Subtexture(Texture texture, Rect source)
: this(texture, source, new Rect(0, 0, source.Width, source.Height))
{
}
public Subtexture(Texture texture, Rect source, Rect frame)
{
this.texture = texture;
this.source = source;
this.frame = frame;
UpdateCoords();
}
public void Reset(Texture texture, Rect source, Rect frame)
{
this.texture = texture;
this.source = source;
this.frame = frame;
UpdateCoords();
}
public (Rect Source, Rect Frame) GetClip(in Rect clip)
{
(Rect Source, Rect Frame) result;
result.Source = (clip + Source.Position + Frame.Position).OverlapRect(Source);
result.Frame.X = MathF.Min(0, Frame.X + clip.X);
result.Frame.Y = MathF.Min(0, Frame.Y + clip.Y);
result.Frame.Width = clip.Width;
result.Frame.Height = clip.Height;
return result;
}
public (Rect Source, Rect Frame) GetClip(float x, float y, float w, float h)
{
return GetClip(new Rect(x, y, w, h));
}
public Subtexture GetClipSubtexture(Rect clip)
{
var (source, frame) = GetClip(clip);
return new Subtexture(Texture!, source, frame);
}
private void UpdateCoords()
{
DrawCoords[0].X = -frame.X;
DrawCoords[0].Y = -frame.Y;
DrawCoords[1].X = -frame.X + source.Width;
DrawCoords[1].Y = -frame.Y;
DrawCoords[2].X = -frame.X + source.Width;
DrawCoords[2].Y = -frame.Y + source.Height;
DrawCoords[3].X = -frame.X;
DrawCoords[3].Y = -frame.Y + source.Height;
if (texture != null)
{
var tx0 = source.X / texture.Width;
var ty0 = source.Y / texture.Height;
var tx1 = source.Right / texture.Width;
var ty1 = source.Bottom / texture.Height;
TexCoords[0].X = tx0;
TexCoords[0].Y = ty0;
TexCoords[1].X = tx1;
TexCoords[1].Y = ty0;
TexCoords[2].X = tx1;
TexCoords[2].Y = ty1;
TexCoords[3].X = tx0;
TexCoords[3].Y = ty1;
}
}
}