-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloater.java
145 lines (130 loc) · 4.35 KB
/
Floater.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
import java.awt.*; //images
import java.awt.image.*; //images
public class Floater extends IntArrayImage implements FloatingObject {
public int x,y,z;
public boolean visible=true;//, alphaTransparent;
public boolean ignoreHeightmap=true;
public int fadeOutAlphaDelta=35;
Floater(){
setSize(0, 0);
}
Floater(int nw, int nh){
setSize(nw, nh);
}
public Floater fastClone(){
Floater f = new Floater();
f.x = x;
f.y = y;
f.z = z;
f.visible = visible;
f.ignoreHeightmap = ignoreHeightmap;
f.fadeOutAlphaDelta = fadeOutAlphaDelta;
f.data=data;
f.w = w;
f.h = h;
f.start = start;
f.stride = stride;
return f;
}
//set ignoreHeighmap to false to draw only the pixel of the floater which are actually
//visible => problem, SIRDS can't be partially visible=>you normally can still see the floater on
//one (only one) eye
public void draw(IntArrayImage output){
if (!visible)
return;
if (data.length!=w*h)
throw new IllegalArgumentException("invalid floater data");
int SIRDW=ZDraw.SIRDW;
//if (w>SIRDW)
// throw new IllegalArgumentException("floater width to large");
//distance between floater copies on height z: SIRDW - z
for (int cy=0;cy<h;cy++){
if (y+cy<0) continue;
if (y+cy>=output.h) break;
int b=output.getLineIndex(y+cy);
int bd=getLineIndex(cy);
int offset=(x+z) % (SIRDW -z);
int cx=-offset; //output x position
final int alpha_decay = fadeOutAlphaDelta;
int decreasingAlpha = alpha_decay * (x+z) / (SIRDW-z);
int decreasingAlphaCapped = Math.max(decreasingAlpha, 0);
while(cx+offset<output.w){
//to[b+cx] = data[bd+cx];
int ncx = (SIRDW+cx) % (SIRDW-z); //x position in floater data
if (ncx>=w) {
cx=cx+(SIRDW-z)-ncx;
if (cx < x) decreasingAlpha -= alpha_decay;
else decreasingAlpha += alpha_decay;
decreasingAlphaCapped = Math.max(decreasingAlpha, 0);
continue;
}
int o=output.data[b+cx+offset];
int n=data[bd+ncx];
int alpha=(n>>>24) - decreasingAlphaCapped;
if (alpha > 0 && (ignoreHeightmap || output.data[b+cx+offset]<=z) ) {
int malpha=0xff - alpha;
output.data[b+cx+offset] = 0xff000000
| (((malpha * ((o>>>16) & 0xff) + alpha*((n>>>16) & 0xff)) / 0xff) << 16)
| (((malpha * ((o>>>8) & 0xff) + alpha*((n>>> 8) & 0xff)) / 0xff) << 8)
| ((malpha * (o & 0xff) + alpha*(n & 0xff)) / 0xff);
}
cx++;
}
}
}
public void drawSimple(IntArrayImage output){
if (!visible) return;
if (data.length!=w*h)
throw new IllegalArgumentException("invalid floater data");
for (int cy=0;cy<h;cy++){
if (y+cy<0) continue;
if (y+cy>=output.h) break;
int b=output.getLineIndex(y+cy);
int bd=getLineIndex(cy);
for (int cx=0;cx<w; cx++){
//to[b+cx] = data[bd+cx];
if (cx+x>=output.w) break;
int o=output.data[b+cx+x];
int n=data[bd+cx];
int alpha=n>>>24;
int malpha=0xff - alpha;
output.data[b+cx+x] = 0xff000000
| (((malpha * ((o>>>16) & 0xff) + alpha*((n>>>16) & 0xff)) / 0xff) << 16)
| (((malpha * ((o>>>8) & 0xff) + alpha*((n>>> 8) & 0xff)) / 0xff) << 8)
| ((malpha * (o & 0xff) + alpha*(n & 0xff)) / 0xff);
}
}
}
//set to methods only change the data, not the position
//override to force max width
public void setToImageARGB(BufferedImage img){
int nw=img.getWidth();
if (nw>ZDraw.SIRDW) nw = ZDraw.SIRDW; //prevent exception
setSize(nw, img.getHeight());
data=img.getRGB(0,0,nw,h, data, 0, nw);
}
public void setToString(String text, FontMetrics fontMetric, Color c){
setToStringARGB(text,fontMetric,c);
}
public void mergeColor(int o){
for (int i=0;i<data.length;i++){
int n=data[i];
int alpha=n >>> 24;
int malpha=0xff - alpha;
data[i] = 0xff000000
| (((malpha * ((o>>>16) & 0xff) + alpha*((n>>>16) & 0xff)) / 0xff) << 16)
| (((malpha * ((o>>>8) & 0xff) + alpha*((n>>> 8) & 0xff)) / 0xff) << 8)
| ((malpha * (o & 0xff) + alpha*(n & 0xff)) / 0xff);
}
}
public void blur(int size){
//blur color components independently
blur(size, 0x000000ff);
blur(size, 0x0000ff00);
blur(size, 0x00ff0000);
blur((size+1)/2,0xff000000);
}
public void setToImage(BufferedImage img){
setToImageARGB(img);
}
}