Allow scaling images

This commit is contained in:
Guntram Blohm 2019-04-05 16:03:26 +02:00
parent 3830dc1924
commit 20517bfa16
5 changed files with 137 additions and 43 deletions

View File

@ -6,13 +6,15 @@ public class ImageMap
private int x;
private int y;
private boolean fastsend;
private double scale;
public ImageMap(String image, int x, int y, boolean fastsend)
public ImageMap(String image, int x, int y, boolean fastsend, double scale)
{
this.image = image;
this.x = x;
this.y = y;
this.fastsend = fastsend;
this.scale = scale;
}
public String getImage()
@ -35,7 +37,12 @@ public class ImageMap
return fastsend;
}
public boolean isSimilar(String file, int x2, int y2)
public double getScale()
{
return scale;
}
public boolean isSimilar(String file, int x2, int y2, double d)
{
if (!getImage().equalsIgnoreCase(file))
return false;
@ -44,6 +51,7 @@ public class ImageMap
if (getY() != y2)
return false;
return true;
double diff=d - getScale();
return (diff > -0.0001 && diff < 0.0001);
}
}

View File

@ -1,5 +1,6 @@
package de.craftlancer.imagemaps;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
@ -28,16 +29,24 @@ public class ImageMapCommand implements TabExecutor
case 1:
return getMatches(args[0], new File(plugin.getDataFolder(), "images").list());
case 2:
return Arrays.asList("true", "false", "reload");
default:
return Collections.emptyList();
return Arrays.asList("true", "false", "reload", "download", "scale", "info");
case 3:
if (args[2].equals("true") || args[2].equals("false"))
return Arrays.asList("scale");
break;
case 5:
if (args[2].equals("scale")) {
return Arrays.asList("true", "false");
}
break;
}
return Collections.emptyList();
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if (!sender.hasPermission("imagemaps.use") || !(sender instanceof Player))
if (!sender.hasPermission("imagemaps.use"))
return true;
if (args.length < 1)
@ -50,11 +59,74 @@ public class ImageMapCommand implements TabExecutor
return true;
}
boolean fastsend = args.length >= 2 && Boolean.parseBoolean(args[1]);
if (args.length>=2 && args[1].equals("info")) {
BufferedImage image=plugin.loadImage(args[0]);
if (image == null) {
sender.sendMessage("Error getting this image, please consult server logs");
return true;
}
int tileWidth=image.getWidth()+127/128;
int tileHeight=image.getHeight()+127/128;
sender.sendMessage("This image is "+tileWidth+" tiles ("+image.getWidth()+" pixels) wide and "+tileHeight+" tiles ("+image.getHeight()+" pixels) high");
return true;
}
if (!(sender instanceof Player)) {
sender.sendMessage("You need to be a player to do that");
return true;
}
plugin.startPlacing((Player) sender, args[0], fastsend);
BufferedImage image=plugin.loadImage(args[0]);
if (image == null) {
sender.sendMessage("Error getting this image, please consult server logs");
return true;
}
boolean fastsend = false;
int tilesx = 0, tilesy = 0;
for (int i=1; i<args.length; i++) {
if (args[i].equalsIgnoreCase("true")) {
fastsend=true;
} else if (args[i].equalsIgnoreCase("false")) {
fastsend=false;
} else if (args[i].equalsIgnoreCase("scale") && i+2<args.length) {
try {
tilesx=Integer.parseInt(args[i+1]);
tilesy=Integer.parseInt(args[i+2]);
} catch (NumberFormatException ex) {
tilesx = tilesy = 0;
}
if (tilesx < 0 || tilesy < 0) {
sender.sendMessage("Need to pass two integers to scale");
return true;
}
i+=2;
} else {
sender.sendMessage("ignoring unknown parameter "+args[i]+" (continuing)");
}
}
sender.sendMessage("Started placing of " + args[0] + ". Rightclick on a block, that shall be the upper left corner.");
double scalex=tilesx * 128.0 / image.getWidth();
double scaley=tilesy * 128.0 / image.getHeight();
if (scalex == 0 && scaley == 0) {
scalex = scaley = 1.0;
} else if (scalex == 0) {
scalex = scaley;
} else if (scaley == 0) {
scaley = scalex;
} else {
if (scalex > scaley) {
scalex = scaley;
} else {
scaley = scalex;
}
}
plugin.startPlacing((Player) sender, args[0], fastsend, scalex);
sender.sendMessage("Started placing of " + args[0] + " which needs "+image.getWidth()*scalex+" width and "+image.getHeight()*scaley+" height. Rightclick on a block, that shall be the upper left corner.");
return true;
}

View File

@ -1,6 +1,8 @@
package de.craftlancer.imagemaps;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import org.bukkit.entity.Player;
@ -10,30 +12,36 @@ import org.bukkit.map.MapView;
public class ImageMapRenderer extends MapRenderer
{
private Image image = null;
private BufferedImage image = null;
private boolean first = true;
public ImageMapRenderer(BufferedImage image, int x1, int y1)
public ImageMapRenderer(BufferedImage image, int x1, int y1, double scale)
{
recalculateInput(image, x1, y1);
recalculateInput(image, x1, y1, scale);
}
public void recalculateInput(BufferedImage input, int x1, int y1)
public void recalculateInput(BufferedImage input, int x1, int y1, double scale)
{
int x2 = ImageMaps.MAP_WIDTH;
int y2 = ImageMaps.MAP_HEIGHT;
if (x1 > input.getWidth() || y1 > input.getHeight())
if (x1 > input.getWidth()* scale + 0.001 || y1 > input.getHeight() * scale + 0.001)
return;
if (x1 + x2 >= input.getWidth())
x2 = input.getWidth() - x1;
if (y1 + y2 >= input.getHeight())
y2 = input.getHeight() - y1;
this.image = input.getSubimage(x1, y1, x2, y2);
if (x1 + x2 >= input.getWidth() * scale)
x2 = (int)(input.getWidth() * scale) - x1;
if (y1 + y2 >= input.getHeight() * scale)
y2 = (int)(input.getHeight() * scale) - y1;
this.image = input.getSubimage((int)(x1/scale), (int)(y1/scale), (int)(x2/scale), (int)(y2/scale));
if (scale != 1.0) {
BufferedImage resized = new BufferedImage(ImageMaps.MAP_WIDTH, ImageMaps.MAP_HEIGHT, input.getType());
AffineTransform at = new AffineTransform();
at.scale(scale, scale);
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
this.image = scaleOp.filter(this.image, resized);
}
first = true;
}
@ -46,5 +54,4 @@ public class ImageMapRenderer extends MapRenderer
first = false;
}
}
}

View File

@ -71,8 +71,8 @@ public class ImageMaps extends JavaPlugin implements Listener {
return sendList;
}
public void startPlacing(Player p, String image, boolean fastsend) {
placing.put(p.getName(), new PlacingCacheEntry(image, fastsend));
public void startPlacing(Player p, String image, boolean fastsend, double scale) {
placing.put(p.getName(), new PlacingCacheEntry(image, fastsend, scale));
}
public boolean placeImage(Block block, BlockFace face, PlacingCacheEntry cache) {
@ -106,8 +106,8 @@ public class ImageMaps extends JavaPlugin implements Listener {
Block b = block.getRelative(face);
int width = (int) Math.ceil((double) image.getWidth() / (double) MAP_WIDTH);
int height = (int) Math.ceil((double) image.getHeight() / (double) MAP_HEIGHT);
int width = (int) Math.ceil((double) image.getWidth() / (double) MAP_WIDTH * cache.getScale() - 0.0001);
int height = (int) Math.ceil((double) image.getHeight() / (double) MAP_HEIGHT * cache.getScale() - 0.0001);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
@ -160,7 +160,7 @@ public class ImageMaps extends JavaPlugin implements Listener {
i.setFacingDirection(face, false);
ItemStack item = getMapItem(cache.getImage(), x, y, image);
ItemStack item = getMapItem(cache.getImage(), x, y, image, cache.getScale());
i.setItem(item);
int id = ((MapMeta) item.getItemMeta()).getMapId();
@ -170,27 +170,27 @@ public class ImageMaps extends JavaPlugin implements Listener {
sendTask.addToQueue(id);
}
maps.put(id, new ImageMap(cache.getImage(), x, y, sendList.contains(id)));
maps.put(id, new ImageMap(cache.getImage(), x, y, sendList.contains(id), cache.getScale()));
}
@SuppressWarnings("deprecation")
private ItemStack getMapItem(String file, int x, int y, BufferedImage image) {
private ItemStack getMapItem(String file, int x, int y, BufferedImage image, double scale) {
ItemStack item = new ItemStack(Material.MAP);
for (Entry<Integer, ImageMap> entry : maps.entrySet()) {
if (entry.getValue().isSimilar(file, x, y)) {
if (entry.getValue().isSimilar(file, x, y, scale)) {
MapMeta meta = (MapMeta) item.getItemMeta();
meta.setMapId(entry.getKey());
item.setItemMeta(meta);
return item;
}
}
MapView map = getServer().createMap(getServer().getWorlds().get(0));
for (MapRenderer r : map.getRenderers())
map.removeRenderer(r);
map.addRenderer(new ImageMapRenderer(image, x, y));
map.addRenderer(new ImageMapRenderer(image, x, y, scale));
MapMeta meta = ((MapMeta) item.getItemMeta());
meta.setMapId(map.getId());
@ -241,6 +241,7 @@ public class ImageMaps extends JavaPlugin implements Listener {
int x = config.getInt(key + ".x");
int y = config.getInt(key + ".y");
boolean fastsend = config.getBoolean(key + ".fastsend", false);
double scale = config.getDouble(key + ".scale", 1.0);
BufferedImage bimage = loadImage(image);
@ -255,8 +256,8 @@ public class ImageMaps extends JavaPlugin implements Listener {
if (fastsend)
sendList.add(id);
map.addRenderer(new ImageMapRenderer(loadImage(image), x, y));
maps.put(id, new ImageMap(image, x, y, fastsend));
map.addRenderer(new ImageMapRenderer(loadImage(image), x, y, scale));
maps.put(id, new ImageMap(image, x, y, fastsend, scale));
}
}
@ -272,6 +273,7 @@ public class ImageMaps extends JavaPlugin implements Listener {
config.set(e.getKey() + ".x", e.getValue().getX());
config.set(e.getKey() + ".y", e.getValue().getY());
config.set(e.getKey() + ".fastsend", e.getValue().isFastSend());
config.set(e.getKey() + ".scale", e.getValue().getScale());
}
try {
@ -295,17 +297,16 @@ public class ImageMaps extends JavaPlugin implements Listener {
int width = (int) Math.ceil((double) image.getWidth() / (double) MAP_WIDTH);
int height = (int) Math.ceil((double) image.getHeight() / (double) MAP_HEIGHT);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
int id = (short) ((MapMeta) getMapItem(file, x * MAP_WIDTH, y * MAP_HEIGHT, image).getItemMeta()).getMapId();
for (Entry<Integer, ImageMap> entry : maps.entrySet()) {
ImageMap imageMap = entry.getValue();
if (imageMap.getImage().equals(file)) {
int id=((MapMeta) getMapItem(file, imageMap.getX(), imageMap.getY(), image, imageMap.getScale()).getItemMeta()).getMapId();
MapView map = getServer().getMap(id);
for (MapRenderer renderer : map.getRenderers())
if (renderer instanceof ImageMapRenderer)
((ImageMapRenderer) renderer).recalculateInput(image, x * MAP_WIDTH, y * MAP_HEIGHT);
((ImageMapRenderer) renderer).recalculateInput(image, imageMap.getX(), imageMap.getY(), imageMap.getScale());
sendTask.addToQueue(id);
}
}
}
}

View File

@ -4,11 +4,13 @@ public class PlacingCacheEntry
{
private String image;
private boolean fastsend;
private double scale;
public PlacingCacheEntry(String image, boolean fastsend)
public PlacingCacheEntry(String image, boolean fastsend, double scale)
{
this.image = image;
this.fastsend = fastsend;
this.scale = scale;
}
public String getImage()
@ -20,4 +22,8 @@ public class PlacingCacheEntry
{
return fastsend;
}
public double getScale() {
return scale;
}
}