Skip to content

Commit 566eaad

Browse files
author
Slikey
committed
Added TextEffect to write Text with particles
1 parent 40f29ea commit 566eaad

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package de.slikey.effectlib.effect;
2+
3+
import java.awt.Color;
4+
import java.awt.Font;
5+
import java.awt.image.BufferedImage;
6+
7+
import org.bukkit.Location;
8+
import org.bukkit.util.Vector;
9+
10+
import de.slikey.effectlib.EffectManager;
11+
import de.slikey.effectlib.EffectType;
12+
import de.slikey.effectlib.util.MathUtils;
13+
import de.slikey.effectlib.util.ParticleEffect;
14+
import de.slikey.effectlib.util.StringParser;
15+
import de.slikey.effectlib.util.VectorUtils;
16+
17+
public class TextLocationEffect extends LocationEffect {
18+
19+
public ParticleEffect particle = ParticleEffect.FLAME;
20+
public String text = "Text";
21+
public boolean invert = false;
22+
public int stepX = 2;
23+
public int stepY = 2;
24+
public float size = (float) 1 / 15;
25+
26+
protected final StringParser parser;
27+
28+
public TextLocationEffect(EffectManager effectManager, Location location) {
29+
this(effectManager, location, new Font("Tahoma", Font.PLAIN, 48));
30+
}
31+
32+
public TextLocationEffect(EffectManager effectManager, Location location, Font font) {
33+
super(effectManager, location);
34+
parser = new StringParser(font);
35+
type = EffectType.REPEATING;
36+
period = 10;
37+
iterations = -1;
38+
}
39+
40+
@Override
41+
public void onRun() {
42+
int clr = 0;
43+
BufferedImage image = parser.stringToBufferedImage(text);
44+
for (int y = 0; y < image.getHeight(); y += stepY) {
45+
for (int x = 0; x < image.getWidth(); x += stepX) {
46+
clr = image.getRGB(x, y);
47+
if (!invert && Color.black.getRGB() != clr)
48+
continue;
49+
else if (invert && Color.black.getRGB() == clr)
50+
continue;
51+
Vector v = new Vector(x, image.getHeight() - y, 0).multiply(size);
52+
VectorUtils.rotateAroundAxisY(v, -location.getYaw() * MathUtils.degreesToRadians);
53+
particle.display(location.add(v), visibleRange);
54+
location.subtract(v);
55+
}
56+
}
57+
}
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package de.slikey.effectlib.util;
2+
3+
import java.awt.Color;
4+
import java.awt.Font;
5+
import java.awt.FontMetrics;
6+
import java.awt.Graphics;
7+
import java.awt.font.FontRenderContext;
8+
import java.awt.geom.Rectangle2D;
9+
import java.awt.image.BufferedImage;
10+
11+
/**
12+
* Based on answer at StackOverflow
13+
* @see http://stackoverflow.com/questions/17282495/java-parsing-truetype-font-to-extract-each-characters-as-image-its-code
14+
* @author Kevin
15+
*
16+
*/
17+
public class StringParser {
18+
protected final Font font;
19+
20+
public StringParser(Font font) {
21+
this.font = font;
22+
}
23+
24+
public BufferedImage stringToBufferedImage(String s) {
25+
//First, we have to calculate the string's width and height
26+
27+
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
28+
Graphics g = img.getGraphics();
29+
30+
//Set the font to be used when drawing the string
31+
g.setFont(font);
32+
33+
//Get the string visual bounds
34+
FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
35+
Rectangle2D rect = font.getStringBounds(s, frc);
36+
//Release resources
37+
g.dispose();
38+
39+
//Then, we have to draw the string on the final image
40+
41+
//Create a new image where to print the character
42+
img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR);
43+
g = img.getGraphics();
44+
g.setColor(Color.black); //Otherwise the text would be white
45+
g.setFont(font);
46+
47+
//Calculate x and y for that string
48+
FontMetrics fm = g.getFontMetrics();
49+
int x = 0;
50+
int y = fm.getAscent(); //getAscent() = baseline
51+
g.drawString(s, x, y);
52+
53+
//Release resources
54+
g.dispose();
55+
56+
//Return the image
57+
return img;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)