Skip to content

Commit 8a45008

Browse files
committed
Improve the render for different types of pixels
1 parent 5f9a41e commit 8a45008

File tree

6 files changed

+117
-99
lines changed

6 files changed

+117
-99
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/MapRendererData.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -363,41 +363,8 @@ public String toString() {
363363

364364
private void setPixels(@Nonnull final MapCanvas canvas) {
365365
getPixels().forEach(mapPixel -> {
366-
this.setPixel(canvas, mapPixel);
367-
this.setText(canvas, mapPixel);
368-
this.setImage(canvas, mapPixel);
366+
mapPixel.render(this,canvas);
369367
});
370368
}
371369

372-
private void setPixel(final MapCanvas canvas, final MapPixel mapPixel) {
373-
if (mapPixel instanceof MapColoredPixel) {
374-
final Color color = ((MapColoredPixel) mapPixel).getColor();
375-
if (ItemCreator.getServerVersion() < 20.0F)
376-
canvas.setPixel(mapPixel.getX(), mapPixel.getY(), MapPalette.matchColor(color));
377-
else
378-
canvas.setPixelColor(mapPixel.getX(), mapPixel.getY(), color);
379-
}
380-
}
381-
382-
private void setText(@Nonnull final MapCanvas canvas,@Nonnull final MapPixel mapPixel) {
383-
if (mapPixel instanceof TextOverlay) {
384-
TextOverlay textOverlay = (TextOverlay) mapPixel;
385-
final MapFont mapFont = textOverlay.getMapFont();
386-
if (mapFont instanceof MinecraftFont)
387-
canvas.drawText(mapPixel.getX(), mapPixel.getY(), mapFont, textOverlay.getText());
388-
else {
389-
final MapTextRenderer mapTextRenderer = new MapTextRenderer(canvas, this, textOverlay);
390-
mapTextRenderer.drawCustomFontText(mapPixel.getX(), mapPixel.getY());
391-
}
392-
}
393-
}
394-
395-
private void setImage(@Nonnull final MapCanvas canvas,@Nonnull final MapPixel mapPixel) {
396-
if (mapPixel instanceof ImageOverlay) {
397-
ImageOverlay textOverlay = (ImageOverlay) mapPixel;
398-
final Image image = textOverlay.getImage();
399-
if (image != null)
400-
canvas.drawImage(mapPixel.getX(), mapPixel.getY(), image);
401-
}
402-
}
403370
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/font/customdraw/MapTextRenderer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void drawPixelsOffset(CharacterSprite sprite, int startX, int startY, Co
122122
for (int py = 0; py < h; py++) {
123123
for (int px = 0; px < w; px++) {
124124
if (pixels[py * w + px]) {
125-
setMapPixel(canvas, startX + px, startY + py, color);
125+
setMapPixel(startX + px, startY + py, color);
126126
}
127127
}
128128
}
@@ -134,16 +134,15 @@ private void drawPixelsOffset(CharacterSprite sprite, int startX, int startY, Co
134134
* Spigot 1.20+ uses {@link MapCanvas#setPixelColor(int, int, java.awt.Color)},
135135
* older versions use {@link MapPalette#matchColor(Color)}.
136136
*
137-
* @param canvas the target map canvas to draw on
138137
* @param x location where draw in digonal direction.
139138
* @param y location where draw in vertical direction.
140139
* @param color the color to set for the pixel
141140
*/
142-
private void setMapPixel(MapCanvas canvas, int x, int y, Color color) {
141+
private void setMapPixel( int x, int y, Color color) {
143142
if (ItemCreator.getServerVersion() < 20.0F) {
144-
canvas.setPixel(x, y, MapPalette.matchColor(color));
143+
this.canvas.setPixel(x, y, MapPalette.matchColor(color));
145144
} else {
146-
canvas.setPixelColor(x, y, color);
145+
this.canvas.setPixelColor(x, y, color);
147146
}
148147
}
149148

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/pixel/ImageOverlay.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.broken.arrow.library.itemcreator.meta.map.pixel;
22

3+
import org.broken.arrow.library.itemcreator.meta.map.MapRendererData;
34
import org.broken.arrow.library.logging.Logging;
5+
import org.bukkit.map.MapCanvas;
46

57
import javax.annotation.Nonnull;
68
import javax.annotation.Nullable;
@@ -55,6 +57,14 @@ public Image getImage() {
5557
return imageId;
5658
}
5759

60+
@Override
61+
public void render(final @Nonnull MapRendererData mapRendererData, @Nonnull final MapCanvas canvas) {
62+
final Image image = this.getImage();
63+
if (image != null) {
64+
canvas.drawImage(this.getX(), this.getY(), image);
65+
}
66+
}
67+
5868
/**
5969
* Serializes this ImageOverlay into a map representation.
6070
* The image is converted into a PNG byte array for serialization.
Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package org.broken.arrow.library.itemcreator.meta.map.pixel;
22

3+
import org.broken.arrow.library.itemcreator.ItemCreator;
4+
import org.broken.arrow.library.itemcreator.meta.map.MapRendererData;
5+
import org.bukkit.map.MapCanvas;
6+
import org.bukkit.map.MapPalette;
7+
38
import javax.annotation.Nonnull;
49
import javax.annotation.Nullable;
510
import java.awt.*;
@@ -11,68 +16,78 @@
1116
* The color may be null, indicating a transparent or unspecified pixel.
1217
*/
1318
public class MapColoredPixel extends MapPixel {
14-
private final Color color;
19+
private final Color color;
1520

16-
/**
17-
* Constructs a colored pixel at the given coordinates with no color (null).
18-
*
19-
* @param x the x-coordinate of the pixel.
20-
* @param y the y-coordinate of the pixel.
21-
*/
22-
public MapColoredPixel(final int x, final int y) {
23-
this(x, y, null);
24-
}
21+
/**
22+
* Constructs a colored pixel at the given coordinates with no color (null).
23+
*
24+
* @param x the x-coordinate of the pixel.
25+
* @param y the y-coordinate of the pixel.
26+
*/
27+
public MapColoredPixel(final int x, final int y) {
28+
this(x, y, null);
29+
}
2530

26-
/**
27-
* Constructs a colored pixel at the given coordinates with the specified color.
28-
*
29-
* @param x the x-coordinate of the pixel.
30-
* @param y the y-coordinate of the pixel.
31-
* @param color the color of the pixel, or null if unspecified.
32-
*/
33-
public MapColoredPixel(final int x, final int y, @Nullable final Color color) {
34-
super(x, y);
35-
this.color = color;
36-
}
31+
@Override
32+
public void render(final @Nonnull MapRendererData mapRendererData, @Nonnull final MapCanvas canvas) {
33+
final Color pixelColor = this.getColor();
34+
if (ItemCreator.getServerVersion() < 20.0F) {
35+
canvas.setPixel(this.getX(), this.getY(), MapPalette.matchColor(pixelColor));
36+
} else {
37+
canvas.setPixelColor(this.getX(), this.getY(), pixelColor);
38+
}
39+
}
3740

38-
/**
39-
* Gets the color of this pixel.
40-
*
41-
* @return the color, or null if no color is set.
42-
*/
43-
public Color getColor() {
44-
return color;
45-
}
41+
/**
42+
* Constructs a colored pixel at the given coordinates with the specified color.
43+
*
44+
* @param x the x-coordinate of the pixel.
45+
* @param y the y-coordinate of the pixel.
46+
* @param color the color of the pixel, or null if unspecified.
47+
*/
48+
public MapColoredPixel(final int x, final int y, @Nullable final Color color) {
49+
super(x, y);
50+
this.color = color;
51+
}
4652

47-
/**
48-
* Serializes this colored pixel to a map containing its type, coordinates, and color (if set).
49-
*
50-
* @return a map representing this colored pixel for storage or transmission.
51-
*/
52-
@Nonnull
53-
@Override
54-
public Map<String, Object> serialize() {
55-
Map<String, Object> map = new HashMap<>();
56-
map.put("type", type());
57-
map.put("x", getX());
58-
map.put("y", getY());
59-
if (color != null) {
60-
map.put("color", color.getRGB());
53+
/**
54+
* Gets the color of this pixel.
55+
*
56+
* @return the color, or null if no color is set.
57+
*/
58+
public Color getColor() {
59+
return color;
6160
}
62-
return map;
63-
}
6461

65-
/**
66-
* Deserializes a colored pixel from the given map representation.
67-
*
68-
* @param map the map containing serialized pixel data.
69-
* @return a new {@code MapColoredPixel} instance.
70-
*/
71-
public static MapColoredPixel deserialize(Map<String, Object> map) {
72-
int x = (int) map.get("x");
73-
int y = (int) map.get("y");
74-
Object colorObject = map.get("color");
75-
Color color = colorObject != null ? new Color((int) colorObject) : null;
76-
return new MapColoredPixel(x, y, color);
77-
}
62+
/**
63+
* Serializes this colored pixel to a map containing its type, coordinates, and color (if set).
64+
*
65+
* @return a map representing this colored pixel for storage or transmission.
66+
*/
67+
@Nonnull
68+
@Override
69+
public Map<String, Object> serialize() {
70+
Map<String, Object> map = new HashMap<>();
71+
map.put("type", type());
72+
map.put("x", getX());
73+
map.put("y", getY());
74+
if (color != null) {
75+
map.put("color", color.getRGB());
76+
}
77+
return map;
78+
}
79+
80+
/**
81+
* Deserializes a colored pixel from the given map representation.
82+
*
83+
* @param map the map containing serialized pixel data.
84+
* @return a new {@code MapColoredPixel} instance.
85+
*/
86+
public static MapColoredPixel deserialize(Map<String, Object> map) {
87+
int x = (int) map.get("x");
88+
int y = (int) map.get("y");
89+
Object colorObject = map.get("color");
90+
Color color = colorObject != null ? new Color((int) colorObject) : null;
91+
return new MapColoredPixel(x, y, color);
92+
}
7893
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/pixel/MapPixel.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.broken.arrow.library.itemcreator.meta.map.pixel;
22

3+
import org.broken.arrow.library.itemcreator.meta.map.MapRendererData;
4+
import org.bukkit.map.MapCanvas;
5+
36
import javax.annotation.Nonnull;
47
import java.util.Map;
58

@@ -22,6 +25,15 @@ protected MapPixel(int x, int y) {
2225
this.y = y;
2326
}
2427

28+
/**
29+
* Renders this pixel to the given canvas.
30+
*
31+
* @param mapRendererData the global data set for the map
32+
* @param canvas the map canvas where this pixel's color, text, or image will be drawn
33+
*/
34+
abstract public void render(@Nonnull final MapRendererData mapRendererData, @Nonnull final MapCanvas canvas);
35+
36+
2537
/**
2638
* Gets the x-coordinate of this pixel.
2739
*
@@ -40,6 +52,7 @@ public int getY() {
4052
return y;
4153
}
4254

55+
4356
/**
4457
* Serialize the data in your class to a format that
4558
* can be saved to a database.

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/meta/map/pixel/TextOverlay.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.broken.arrow.library.itemcreator.meta.map.pixel;
22

3+
import org.broken.arrow.library.itemcreator.meta.map.MapRendererData;
34
import org.broken.arrow.library.itemcreator.meta.map.color.parser.AmpersandHexColorParser;
45
import org.broken.arrow.library.itemcreator.meta.map.color.parser.ColorParser;
56
import org.broken.arrow.library.itemcreator.meta.map.font.CharacterSprite;
67
import org.broken.arrow.library.itemcreator.meta.map.font.MapFontWrapper;
8+
import org.broken.arrow.library.itemcreator.meta.map.font.customdraw.MapTextRenderer;
9+
import org.bukkit.map.MapCanvas;
10+
import org.bukkit.map.MapFont;
11+
import org.bukkit.map.MinecraftFont;
712

813
import javax.annotation.Nonnull;
914
import java.awt.*;
10-
import java.awt.image.BufferedImage;
1115
import java.util.HashMap;
1216
import java.util.Map;
1317

@@ -31,6 +35,17 @@ public TextOverlay(final int x, final int y,@Nonnull final String text) {
3135
this.text = text;
3236
}
3337

38+
@Override
39+
public void render(final @Nonnull MapRendererData mapRendererData, @Nonnull final MapCanvas canvas) {
40+
final MapFont mapFont = this.getMapFont();
41+
if (mapFont instanceof MinecraftFont)
42+
canvas.drawText(this.getX(), this.getY(), mapFont, this.getText());
43+
else {
44+
final MapTextRenderer mapTextRenderer = new MapTextRenderer(canvas, mapRendererData, this);
45+
mapTextRenderer.drawCustomFontText(this.getX(), this.getY());
46+
}
47+
}
48+
3449
/**
3550
* Gets the text content of this overlay.
3651
*
@@ -97,7 +112,6 @@ public MapFontWrapper getMapFontWrapper() {
97112
return mapFontWrapper;
98113
}
99114

100-
101115
/**
102116
* Serializes this TextOverlay into a map representation for saving context.
103117
*

0 commit comments

Comments
 (0)