-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTiledTexture.java
157 lines (136 loc) · 3.94 KB
/
TiledTexture.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
package com.haxademic.core.draw.image;
import com.haxademic.core.app.P;
import com.haxademic.core.draw.context.PG;
import com.haxademic.core.math.MathUtil;
import processing.core.PGraphics;
import processing.core.PImage;
public class TiledTexture {
protected PImage texture;
protected float uX;
protected float vY;
protected float speedX;
protected float speedY;
protected float sizeX;
protected float sizeY;
protected float rotation;
// stored uv coords
float tlX;
float tlY;
float trX;
float trY;
float brX;
float brY;
float blX;
float blY;
public TiledTexture(PImage image) {
setSource(image);
}
public void setSource(PImage image) {
texture = image;
uX = 0.5f * (float) texture.width;
vY = 0.5f * (float) texture.height;
setSpeed(0, 0);
setZoom(1f, 1f);
}
public void setSpeed(float x, float y) {
speedX = x;
speedY = y;
}
public void setOffset(float x, float y) {
uX = (0.5f + x) * (float) texture.width;
vY = (0.5f + y) * (float) texture.height;
}
public void setZoom(float x, float y) {
sizeX = x;
sizeY = y;
}
public void setRotation(float rads) {
rotation = rads;
}
public float textureWidth() {
return (float) texture.width;
}
public float textureHeight() {
return (float) texture.height;
}
public void update() {
uX += speedX;
if(uX < 0 && speedX < 0f) uX += texture.width;
if(uX > texture.width && speedX > 0f) uX -= texture.width;
vY += speedY;
if(vY < 0 && speedY < 0f) vY += texture.height;
if(vY > texture.height && speedY > 0f) vY -= texture.height;
}
public void draw(PGraphics pg, float drawW, float drawH) {
draw(pg, drawW, drawH, true);
}
public void draw(PGraphics pg, float drawW, float drawH, boolean drawFromCenter) {
PG.setTextureRepeat(pg, true);
float halfDrawW = drawW / 2f;
float halfDrawH = drawH / 2f;
float halfSizeX = halfDrawW * sizeX;
float halfSizeY = halfDrawH * sizeY;
pg.noStroke();
pg.beginShape();
pg.textureMode(P.IMAGE);
pg.texture(texture);
if(rotation == 0) {
tlX = uX - halfSizeX;
tlY = vY - halfSizeY;
trX = uX + halfSizeX;
trY = vY - halfSizeY;
brX = uX + halfSizeX;
brY = vY + halfSizeY;
blX = uX - halfSizeX;
blY = vY + halfSizeY;
} else {
float curRot = rotation + P.PI;
float radius = MathUtil.getDistance(0, 0, halfSizeX, halfSizeY);
float tlRads = -MathUtil.getRadiansToTarget(uX - halfSizeX, vY - halfSizeY, uX, vY) + curRot;
float trRads = -MathUtil.getRadiansToTarget(uX + halfSizeX, vY - halfSizeY, uX, vY) + curRot;
float brRads = -MathUtil.getRadiansToTarget(uX + halfSizeX, vY + halfSizeY, uX, vY) + curRot;
float blRads = -MathUtil.getRadiansToTarget(uX - halfSizeX, vY + halfSizeY, uX, vY) + curRot;
tlX = uX + radius * P.cos(tlRads);
tlY = vY - radius * P.sin(tlRads);
trX = uX + radius * P.cos(trRads);
trY = vY - radius * P.sin(trRads);
brX = uX + radius * P.cos(brRads);
brY = vY - radius * P.sin(brRads);
blX = uX + radius * P.cos(blRads);
blY = vY - radius * P.sin(blRads);
}
if(drawFromCenter) {
pg.vertex(-halfDrawW, -halfDrawH, tlX, tlY);
pg.vertex( halfDrawW, -halfDrawH, trX, trY);
pg.vertex( halfDrawW, halfDrawH, brX, brY);
pg.vertex(-halfDrawW, halfDrawH, blX, blY);
} else {
pg.vertex(0, 0, tlX, tlY);
pg.vertex(drawW, 0, trX, trY);
pg.vertex(drawW, drawH, brX, brY);
pg.vertex(0, drawH, blX, blY);
}
pg.endShape();
}
public void drawDebug(PGraphics pg) {
// fit to small box
// draw background
pg.stroke(0);
pg.fill(0);
pg.rect(0, 0, texture.width, texture.height);
// draw image
pg.image(texture, 0, 0, texture.width, texture.height);
// show texture grab area
// float halfSizeX = (sizeX * (float) texture.width) / 2f;
// float halfSizeY = (sizeY * (float) texture.height) / 2f;
pg.stroke(255, 0, 0, 200);
pg.noFill();
pg.beginShape();
pg.vertex(tlX, tlY);
pg.vertex(trX, trY);
pg.vertex(brX, brY);
pg.vertex(blX, blY);
pg.vertex(tlX, tlY);
pg.endShape();
}
}