-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathTextureRegion.java
186 lines (159 loc) · 4.02 KB
/
TextureRegion.java
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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package mdesl.graphics;
/** A trimmed down version of TextureRegion from LibGDX.
* Defines a rectangular area of a texture. The coordinate system used has its origin in the upper left corner with the x-axis
* pointing to the right and the y axis pointing downwards.
* @author mzechner
* @author Nathan Sweet */
public class TextureRegion implements ITexture {
protected Texture texture;
protected float u;
protected float v;
protected float u2;
protected float v2;
protected int regionWidth;
protected int regionHeight;
public TextureRegion(Texture texture) {
this(texture, 0, 0);
}
public TextureRegion(Texture texture, int x, int y) {
this(texture, x, y, texture.getWidth(), texture.getHeight());
}
public TextureRegion(Texture texture, int x, int y, int width, int height) {
set(texture, x, y, width, height);
}
public TextureRegion(Texture texture, float u, float v, float u2, float v2) {
set(texture, u, v, u2, v2);
}
public TextureRegion(TextureRegion region, int x, int y, int width, int height) {
set(region, x, y, width, height);
}
public void set(Texture texture, int x, int y, int width, int height) {
set(texture, x / (float)texture.getWidth(),
y / (float)texture.getHeight(),
(x + width) / (float)texture.getWidth(),
(y + height) / (float)texture.getHeight());
regionWidth = Math.round(width);
regionHeight = Math.round(height);
}
public void set(Texture texture, float u, float v, float u2, float v2) {
this.texture = texture;
this.u = u;
this.v = v;
this.u2 = u2;
this.v2 = v2;
regionWidth = Math.round(Math.abs(u2 - u) * texture.getWidth());
regionHeight = Math.round(Math.abs(v2 - v) * texture.getHeight());
}
public void set(TextureRegion region, int x, int y, int width, int height) {
set(region.texture, x + region.getRegionX(), y + region.getRegionY(), width, height);
}
public int getRegionX () {
return Math.round(u * texture.getWidth());
}
public int getRegionY () {
return Math.round(v * texture.getHeight());
}
public void flip (boolean x, boolean y) {
if (x) {
float temp = u;
u = u2;
u2 = temp;
}
if (y) {
float temp = v;
v = v2;
v2 = temp;
}
}
/**
* @return the texture
*/
public Texture getTexture() {
return texture;
}
/**
* @param texture the texture to set
*/
public void setTexture(Texture texture) {
this.texture = texture;
}
/**
* @return the u
*/
public float getU() {
return u;
}
/**
* @param u the u to set
*/
public void setU(float u) {
this.u = u;
}
/**
* @return the v
*/
public float getV() {
return v;
}
/**
* @param v the v to set
*/
public void setV(float v) {
this.v = v;
}
/**
* @return the u2
*/
public float getU2() {
return u2;
}
/**
* @param u2 the u2 to set
*/
public void setU2(float u2) {
this.u2 = u2;
}
/**
* @return the v2
*/
public float getV2() {
return v2;
}
/**
* @param v2 the v2 to set
*/
public void setV2(float v2) {
this.v2 = v2;
}
/**
* Returns the width (in pixels) of this region.
* @return the width of this region
*/
@Override
public int getWidth() {
return regionWidth;
}
/**
* Returns the height (in pixels) of this region.
* @return the height of this region
*/
@Override
public int getHeight() {
return regionHeight;
}
}