From 1fc863576c2ce659769f906ede84d0e91e57624f Mon Sep 17 00:00:00 2001 From: Smyler Date: Mon, 24 Jun 2024 22:40:16 +0200 Subject: [PATCH 01/10] Dynamic textures --- .../gui/widgets/map/layer/RasterMapLayer.java | 24 ++++------ .../terramap/maps/SavedMapState.java | 2 +- .../terramap/maps/raster/RasterTile.java | 4 +- .../terramap/maps/raster/RasterTiledMap.java | 4 +- .../terramap/maps/raster/imp/ColorTile.java | 8 ++-- .../maps/raster/imp/ColorTiledMap.java | 18 +++----- .../maps/raster/imp/TerrainPreviewMap.java | 3 +- .../maps/raster/imp/TerrainPreviewTile.java | 19 +++----- .../maps/raster/imp/UrlRasterTile.java | 20 ++++----- .../terramap/maps/raster/imp/UrlTiledMap.java | 22 ++++------ .../thesmyler/terramap/util/TerramapUtil.java | 1 - .../http/TerraplusplusHttpClient.java | 3 +- .../net/smyler/smylib/gui/DrawContext.java | 10 +++++ .../smyler/smylib/gui/Lwjgl2DrawContext.java | 44 +++++++++++++++++-- 14 files changed, 102 insertions(+), 80 deletions(-) diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java index 460ae88d..30119a44 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java @@ -3,9 +3,9 @@ import java.util.HashSet; import java.util.Set; +import net.smyler.smylib.Identifier; import net.smyler.smylib.gui.GlState; import net.smyler.smylib.gui.containers.WidgetContainer; -import fr.thesmyler.smylibgui.util.*; import fr.thesmyler.terramap.gui.widgets.map.MapLayer; import fr.thesmyler.terramap.gui.widgets.map.MapWidget; import fr.thesmyler.terramap.maps.raster.RasterTile; @@ -21,10 +21,7 @@ import net.smyler.smylib.math.Vec2dImmutable; import net.smyler.smylib.math.Vec2dMutable; import net.smyler.smylib.math.Vec2dReadOnly; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.profiler.Profiler; -import net.minecraft.util.ResourceLocation; import net.smyler.terramap.util.geo.WebMercatorUtil; import static net.smyler.smylib.Color.WHITE; @@ -59,11 +56,9 @@ protected void initialize() { public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { final RasterTiledMap tiledMap = this.getTiledMap(); - final ResourceLocation defaultTexture = tiledMap.getDefaultTileTexture(); + final Identifier defaultTexture = tiledMap.getDefaultTileTexture(); Font smallFont = getGameClient().smallestFont(); - Minecraft mc = Minecraft.getMinecraft(); - TextureManager textureManager = mc.getTextureManager(); GlState glState = context.glState(); float rotation = this.getRotation(); @@ -217,7 +212,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous } glState.setColor(WHITE); - ResourceLocation texture = defaultTexture; + Identifier texture = defaultTexture; try { if(tile.isTextureAvailable()) texture = tile.getTexture(); else perfectDraw = false; @@ -226,15 +221,12 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous parentMap.reportError(this, e.toString()); } if (texture != null) { - textureManager.bindTexture(texture); - RenderUtil.drawModalRectWithCustomSizedTexture( - dispX, - dispY, + context.drawTexture( + texture, + dispX, dispY, dX, dY, - displayWidth, - displayHeight, - renderSizedSize, - renderSizedSize + displayWidth, displayHeight, + renderSizedSize, renderSizedSize ); } if(debug) { diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/SavedMapState.java b/forge/src/main/java/fr/thesmyler/terramap/maps/SavedMapState.java index 0b0b124d..5d3a7b8b 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/SavedMapState.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/SavedMapState.java @@ -11,7 +11,7 @@ * A map saved state. * Having this in a different class makes it side independent, which might come in handy in the future. * - * @author Smylermap + * @author Smyler */ public class SavedMapState { diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java index 7d3fbcaf..27766e78 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java @@ -1,13 +1,13 @@ package fr.thesmyler.terramap.maps.raster; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePosImmutable; -import net.minecraft.util.ResourceLocation; public interface RasterTile { boolean isTextureAvailable(); - ResourceLocation getTexture() throws Throwable; + Identifier getTexture() throws Throwable; void cancelTextureLoading(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java index a3cd5a79..ef59b905 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java @@ -1,9 +1,9 @@ package fr.thesmyler.terramap.maps.raster; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePos; import net.smyler.terramap.util.geo.TilePosImmutable; import net.smyler.terramap.util.geo.WebMercatorBounds; -import net.minecraft.util.ResourceLocation; /** * A raster map made of individual tiles. @@ -99,6 +99,6 @@ default WebMercatorBounds getBounds(int zoom) { * (e.g. because the right tile is still loading). * Return null to not render anything. */ - ResourceLocation getDefaultTileTexture(); + Identifier getDefaultTileTexture(); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java index 2abf2e4c..8534e720 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java @@ -1,15 +1,15 @@ package fr.thesmyler.terramap.maps.raster.imp; import fr.thesmyler.terramap.maps.raster.RasterTile; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePosImmutable; -import net.minecraft.util.ResourceLocation; public class ColorTile implements RasterTile { private final TilePosImmutable position; - private final ResourceLocation texture; + private final Identifier texture; - public ColorTile(TilePosImmutable position, ResourceLocation texture) { + public ColorTile(TilePosImmutable position, Identifier texture) { this.position = position; this.texture = texture; } @@ -20,7 +20,7 @@ public boolean isTextureAvailable() { } @Override - public ResourceLocation getTexture() { + public Identifier getTexture() { return this.texture; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java index 3e222272..ec583803 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java @@ -3,13 +3,11 @@ import net.smyler.smylib.Color; import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; import fr.thesmyler.terramap.maps.raster.TiledMapProvider; -import net.smyler.terramap.Terramap; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.ImageUtil; import net.smyler.terramap.util.geo.TilePosImmutable; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.DynamicTexture; -import net.minecraft.client.renderer.texture.TextureManager; -import net.minecraft.util.ResourceLocation; + +import java.awt.image.BufferedImage; import static net.smyler.smylib.SmyLib.getGameClient; @@ -17,16 +15,14 @@ public class ColorTiledMap extends CachingRasterTiledMap { private final Color color; private final String name; - private final ResourceLocation textureLocation; + private final Identifier textureLocation; public ColorTiledMap(Color color, String name) { this.color = color; this.name = name; if (getGameClient().isGlAvailabale()) { - TextureManager textureManager = Minecraft.getMinecraft().getTextureManager(); - DynamicTexture texture = new DynamicTexture(ImageUtil.imageFromColor(256, 256, this.color.asRGBInt())); - this.textureLocation = textureManager.getDynamicTextureLocation( - Terramap.MOD_ID + ":color_tile_" + this.color.asHexString(), texture); + BufferedImage image = ImageUtil.imageFromColor(256, 256, this.color.asRGBInt()); + this.textureLocation = getGameClient().guiDrawContext().loadDynamicTexture(image); } else { this.textureLocation = null; } @@ -88,7 +84,7 @@ public boolean isDebug() { } @Override - public ResourceLocation getDefaultTileTexture() { + public Identifier getDefaultTileTexture() { return this.textureLocation; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java index 6c7617fd..ac70b496 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java @@ -2,6 +2,7 @@ import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; import fr.thesmyler.terramap.maps.raster.TiledMapProvider; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePosImmutable; import net.minecraft.util.ResourceLocation; @@ -61,7 +62,7 @@ public boolean isDebug() { } @Override - public ResourceLocation getDefaultTileTexture() { + public Identifier getDefaultTileTexture() { return null; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java index 8371a233..85f3f0d9 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java @@ -6,17 +6,16 @@ import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.maps.raster.RasterTile; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePosImmutable; import net.buildtheearth.terraplusplus.generator.TerrainPreview; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.DynamicTexture; -import net.minecraft.client.renderer.texture.TextureManager; -import net.minecraft.util.ResourceLocation; + +import static net.smyler.smylib.SmyLib.getGameClient; public class TerrainPreviewTile implements RasterTile { private final TilePosImmutable position; - private ResourceLocation texture; + private Identifier texture; private CompletableFuture textureTask; public TerrainPreviewTile(TilePosImmutable position) { @@ -34,7 +33,7 @@ public boolean isTextureAvailable() { } @Override - public ResourceLocation getTexture() throws Throwable { + public Identifier getTexture() throws Throwable { if(this.getPosition().getZoom() < TerrainPreviewMap.BASE_ZOOM_LEVEL) throw new IllegalArgumentException("Trying to request a terrain preview with a zoom that's too low (" + this.position.getZoom() + ")"); @@ -60,9 +59,7 @@ public void cancelTextureLoading() { public void unloadTexture() { this.cancelTextureLoading(); if(this.texture != null) { - Minecraft mc = Minecraft.getMinecraft(); - TextureManager textureManager = mc.getTextureManager(); - textureManager.deleteTexture(this.texture); + getGameClient().guiDrawContext().unloadDynamicTexture(this.texture); this.texture = null; } } @@ -86,10 +83,8 @@ private void tryLoadingTexture() throws Throwable { this.textureTask = null; return; } - Minecraft mc = Minecraft.getMinecraft(); - TextureManager textureManager = mc.getTextureManager(); BufferedImage image = this.textureTask.get(); - this.texture = textureManager.getDynamicTextureLocation("textures/gui/maps/debugterrainpreviewmap/" + this.position.getZoom() + "/" + this.position.getX() + "/" + this.position.getY(), new DynamicTexture(image)); + this.texture = getGameClient().guiDrawContext().loadDynamicTexture(image); this.textureTask = null; } } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java index 8b922f5e..9c553b77 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java @@ -9,12 +9,12 @@ import javax.imageio.ImageIO; import fr.thesmyler.terramap.maps.raster.RasterTile; +import net.smyler.smylib.Identifier; import net.smyler.terramap.Terramap; import net.smyler.terramap.util.geo.TilePosImmutable; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.DynamicTexture; -import net.minecraft.client.renderer.texture.TextureManager; -import net.minecraft.util.ResourceLocation; + + +import static net.smyler.smylib.SmyLib.getGameClient; /** * @author SmylerMC @@ -24,7 +24,7 @@ public class UrlRasterTile implements RasterTile { private final TilePosImmutable pos; private final String url; - private ResourceLocation texture = null; + private Identifier texture = null; private CompletableFuture textureTask; @@ -56,7 +56,7 @@ public boolean isTextureAvailable() { } @Override - public ResourceLocation getTexture() throws Throwable { + public Identifier getTexture() throws Throwable { if(this.texture == null) { if(this.textureTask == null) { this.textureTask = Terramap.instance().http().get(this.getURL()); @@ -80,14 +80,12 @@ private void tryLoadingTexture() throws Throwable { } return; } - Minecraft mc = Minecraft.getMinecraft(); - TextureManager textureManager = mc.getTextureManager(); byte[] buf = this.textureTask.get(); if(buf == null) throw new IOException("404 response"); try (ByteArrayInputStream is = new ByteArrayInputStream(buf)) { BufferedImage image = ImageIO.read(is); if(image == null) throw new IOException("Failed to read image! url: " + this.getURL()); - this.texture = textureManager.getDynamicTextureLocation("textures/gui/maps/" + this.getURL(), new DynamicTexture(image)); + this.texture = getGameClient().guiDrawContext().loadDynamicTexture(image); } } } @@ -104,9 +102,7 @@ public void cancelTextureLoading() { public void unloadTexture() { this.cancelTextureLoading(); if(this.texture != null) { - Minecraft mc = Minecraft.getMinecraft(); - TextureManager textureManager = mc.getTextureManager(); - textureManager.deleteTexture(this.texture); + getGameClient().guiDrawContext().unloadDynamicTexture(this.texture); this.texture = null; } } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java index 8d44a545..e761ead2 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java @@ -1,5 +1,6 @@ package fr.thesmyler.terramap.maps.raster.imp; +import java.awt.image.BufferedImage; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; @@ -11,20 +12,16 @@ import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import fr.thesmyler.terramap.maps.raster.TiledMapProvider; import fr.thesmyler.terramap.network.SP2CMapStylePacket; +import net.smyler.smylib.Identifier; import net.smyler.terramap.util.CopyrightHolder; import net.smyler.smylib.text.Text; import net.smyler.terramap.Terramap; import net.smyler.terramap.util.ImageUtil; import net.smyler.terramap.util.geo.TilePosImmutable; import net.smyler.terramap.util.geo.WebMercatorBounds; -import net.buildtheearth.terraplusplus.util.http.Http; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.texture.DynamicTexture; -import net.minecraft.client.renderer.texture.TextureManager; -import net.minecraft.util.ResourceLocation; -import net.minecraft.util.text.ITextComponent; import static net.smyler.smylib.Preconditions.checkArgument; +import static net.smyler.smylib.SmyLib.getGameClient; /** * Instances are usually created in {@link MapStylesLibrary} and {@link SP2CMapStylePacket}. @@ -50,7 +47,7 @@ public class UrlTiledMap extends CachingRasterTiledMap implements private final boolean debug; private final Map bounds; - private ResourceLocation errorTileTexture = null; + private Identifier errorTileTexture = null; public UrlTiledMap( String[] urlPatterns, @@ -113,7 +110,7 @@ public void setup() { try { URL parsed = new URL(url); if(parsed.getProtocol().startsWith("http")) { - Http.setMaximumConcurrentRequestsTo(url, this.getMaxConcurrentRequests()); + Terramap.instance().http().setMaxConcurrentRequests(url, this.getMaxConcurrentRequests()); } } catch(IllegalArgumentException | MalformedURLException e) { Terramap.instance().logger().error("Failed to set max concurrent requests for host. Url :{}", url); @@ -166,7 +163,7 @@ public String getId() { * * * @param localeKey - the language key to get the copyright for - * @return a copyright as a {@link ITextComponent}, translated to the appropriate language. + * @return a copyright as a {@link Text}, translated to the appropriate language. */ @Override public Text getCopyright(String localeKey) { @@ -269,15 +266,14 @@ public WebMercatorBounds getBounds(int zoom) { } @Override - public ResourceLocation getDefaultTileTexture() { + public Identifier getDefaultTileTexture() { return this.errorTileTexture; } public void registerErrorTexture() { - TextureManager textureManager = Minecraft.getMinecraft().getTextureManager(); int[] color = {170, 211, 223}; - DynamicTexture texture = new DynamicTexture(ImageUtil.imageFromColor(256, 256, color)); - this.errorTileTexture = textureManager.getDynamicTextureLocation(Terramap.MOD_ID + ":error_tile_texture", texture); + BufferedImage image = ImageUtil.imageFromColor(256, 256, color); + this.errorTileTexture = getGameClient().guiDrawContext().loadDynamicTexture(image); } } \ No newline at end of file diff --git a/forge/src/main/java/fr/thesmyler/terramap/util/TerramapUtil.java b/forge/src/main/java/fr/thesmyler/terramap/util/TerramapUtil.java index ad96378f..4aae06af 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/util/TerramapUtil.java +++ b/forge/src/main/java/fr/thesmyler/terramap/util/TerramapUtil.java @@ -18,7 +18,6 @@ public final class TerramapUtil { private TerramapUtil() {} public static final EarthGeneratorSettings BTE_GENERATOR_SETTINGS = EarthGeneratorSettings.parse(EarthGeneratorSettings.BTE_DEFAULT_SETTINGS); - public static final long EARTH_CIRCUMFERENCE = 40075017; public static boolean isServerEarthWorld(World world) { if(!(world.getWorldType() instanceof EarthWorldType)) return false; // Is this a terra save? diff --git a/forge/src/main/java/net/smyler/terramap/http/TerraplusplusHttpClient.java b/forge/src/main/java/net/smyler/terramap/http/TerraplusplusHttpClient.java index d06324e8..cc407cec 100644 --- a/forge/src/main/java/net/smyler/terramap/http/TerraplusplusHttpClient.java +++ b/forge/src/main/java/net/smyler/terramap/http/TerraplusplusHttpClient.java @@ -16,9 +16,8 @@ public CompletableFuture get(String url) { CompletableFuture result = future.thenApply(ByteBufUtil::getBytes); result.exceptionally(t -> { if (t instanceof CancellationException) { - System.out.println("Was cancelled"); + future.cancel(true); } - future.cancel(true); return null; }); return result; diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java b/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java index 4d330d88..4d8b2003 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java @@ -1,8 +1,12 @@ package net.smyler.smylib.gui; import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; import net.smyler.smylib.gui.sprites.Sprite; +import java.awt.*; +import java.awt.image.BufferedImage; + public interface DrawContext { Scissor scissor(); @@ -72,4 +76,10 @@ default void drawSpriteCropped(double x, double y, Sprite sprite, double leftCro //TODO use Text in drawTooltip void drawTooltip(String text, double x, double y); + void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight); + + Identifier loadDynamicTexture(BufferedImage image); + + void unloadDynamicTexture(Identifier texture); + } diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java index 94611172..cc78aa60 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java @@ -1,17 +1,22 @@ package net.smyler.smylib.gui; -import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.util.ResourceLocation; import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; import net.smyler.smylib.gui.sprites.Sprite; import org.lwjgl.opengl.GL11; +import java.awt.image.BufferedImage; +import java.util.concurrent.atomic.AtomicInteger; + import static java.lang.Math.floor; +import static net.minecraft.client.Minecraft.getMinecraft; import static net.smyler.smylib.Preconditions.checkArgument; import static net.smyler.smylib.SmyLib.getGameClient; @@ -19,6 +24,7 @@ public class Lwjgl2DrawContext implements DrawContext { private final Scissor scissor = new Gl11Scissor(); private final GlState glState = new LwjglState(); + private final AtomicInteger dynamicTextureCounter = new AtomicInteger(0); @Override public Scissor scissor() { @@ -69,7 +75,7 @@ public void drawClosedStrokeLine(double z, Color color, float size, double... po @Override public void drawSpriteCropped(double x, double y, double z, Sprite sprite, double leftCrop, double topCrop, double rightCrop, double bottomCrop) { final ResourceLocation location = new ResourceLocation(sprite.texture.namespace, sprite.texture.path); - Minecraft.getMinecraft().getTextureManager().bindTexture(location); + getMinecraft().getTextureManager().bindTexture(location); double uLeft = (sprite.xLeft + leftCrop) / sprite.textureWidth; double uRight = (sprite.xRight - rightCrop) / sprite.textureWidth; double vTop = (sprite.yTop + topCrop) / sprite.textureHeight; @@ -98,7 +104,7 @@ public void drawSpriteCropped(double x, double y, double z, Sprite sprite, doubl @Override public void drawTooltip(String text, double x, double y) { - GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen; + GuiScreen currentScreen = getMinecraft().currentScreen; if (currentScreen == null) { //TODO make it draw by the HUD instead (once we have a working HUD system) return; // We are in game, not in a GUI @@ -116,6 +122,38 @@ public void drawTooltip(String text, double x, double y) { if(!lighting) GlStateManager.disableLighting(); } + @Override + public void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) { + getMinecraft().getTextureManager().bindTexture(new ResourceLocation(texture.namespace, texture.path)); + double f = 1.0f / textureWidth; + double f1 = 1.0f / textureHeight; + GlStateManager.enableAlpha(); + GlStateManager.enableBlend(); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder builder = tessellator.getBuffer(); + builder.begin(7, DefaultVertexFormats.POSITION_TEX); + builder.pos(x, y + height, 0d).tex(u * f, (v + height) * f1).endVertex(); + builder.pos(x + width, y + height, 0d).tex((u + width) * f, (v + height) * f1).endVertex(); + builder.pos(x + width, y, 0d).tex((u + width) * f, v * f1).endVertex(); + builder.pos(x, y, 0d).tex(u * f, v * f1).endVertex(); + tessellator.draw(); + GlStateManager.disableAlpha(); + GlStateManager.disableBlend(); + } + + @Override + public Identifier loadDynamicTexture(BufferedImage image) { + DynamicTexture dynamicTexture = new DynamicTexture(image); + String name = "smylib/dynamic/" + this.dynamicTextureCounter.getAndIncrement() + ".png"; + ResourceLocation location = getMinecraft().getTextureManager().getDynamicTextureLocation(name, dynamicTexture); + return new Identifier(location.getNamespace(), location.getPath()); + } + + @Override + public void unloadDynamicTexture(Identifier texture) { + getMinecraft().getTextureManager().deleteTexture(new ResourceLocation(texture.namespace, texture.path)); + } + private void drawMultiPointsGeometry(int glType, double z, Color color, double... points) { checkArgument(points.length % 2 == 0, "An even number of coordinates is required"); GlStateManager.enableAlpha(); From 3d2708b0af5e703bc05dfd364884b070f23dfcfa Mon Sep 17 00:00:00 2001 From: Smyler Date: Thu, 27 Jun 2024 22:09:29 +0200 Subject: [PATCH 02/10] Remove dependency on TerramapConfig in map styles library --- .../fr/thesmyler/terramap/TerramapConfig.java | 10 --- .../terramap/gui/screens/TerramapScreen.java | 2 +- .../screens/config/TerramapConfigScreen.java | 19 +++-- .../maps/raster/CachingRasterTiledMap.java | 66 ++++++---------- .../maps/raster/MapStylesLibrary.java | 32 +++++--- .../maps/raster/imp/TerrainPreviewMap.java | 4 +- .../terramap/maps/raster/imp/UrlTiledMap.java | 79 ++++++++++++++----- .../terramap/network/SP2CMapStylePacket.java | 13 +-- 8 files changed, 124 insertions(+), 101 deletions(-) diff --git a/forge/src/main/java/fr/thesmyler/terramap/TerramapConfig.java b/forge/src/main/java/fr/thesmyler/terramap/TerramapConfig.java index 5d83465b..5c2e964e 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/TerramapConfig.java +++ b/forge/src/main/java/fr/thesmyler/terramap/TerramapConfig.java @@ -152,16 +152,6 @@ public static final class Client { @Config.RangeDouble(min=0.0, max=8.0) public double tileScaling = 0; - @Config.Ignore public final int TILE_LOAD_MIN = 128; - @Config.Ignore public final int TILE_LOAD_DEFAULT = 512; - @Config.Ignore public final int TILE_LOAD_MAX = 4096; - @Config.Name("max_tile_load") - @Config.LangKey("terramap.config.max_tile_load") - @Config.Comment("This is the maximum number of tiles to keep loaded. A lower number implies lower memory usage, however, if this is lower than the number of tiles displayed on your screen at once you will experience a huge performance drop. Change for a higher value if you experience lag when displaying a map on a large display") - @Config.RangeInt(min=TILE_LOAD_MIN, max=TILE_LOAD_MAX) - @Config.SlidingOption - public int maxTileLoad = TILE_LOAD_DEFAULT; - @Config.Ignore public final int LOW_ZOOM_LEVEL_MIN = 0; @Config.Ignore public final int LOW_ZOOM_LEVEL_DEFAULT = 2; @Config.Ignore public final int LOW_ZOOM_LEVEL_MAX = 3; diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java index ace1778c..7143c744 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java @@ -426,7 +426,7 @@ public void onUpdate(float mouseX, float mouseY, WidgetContainer parent) { debugBuilder.append(String.format(locale, "\nMap provider: %sv%s", backgroundStyle.getProvider(), backgroundStyle.getProviderVersion())); if (backgroundStyle instanceof CachingRasterTiledMap) { CachingRasterTiledMap cachingMap = (CachingRasterTiledMap) backgroundStyle; - debugBuilder.append(String.format(locale, "\nLoaded tiles: %d/%d/%d", cachingMap.getBaseLoad(), cachingMap.getLoadedCount(), cachingMap.getMaxLoad())); + debugBuilder.append(String.format(locale, "\nLoaded tiles: %d/%d/%d", cachingMap.getBaseLoad(), cachingMap.getLoadedCount(), CachingRasterTiledMap.CACHE_SIZE)); if (cachingMap instanceof UrlTiledMap) { UrlTiledMap urlMap = (UrlTiledMap) cachingMap; String[] urls = urlMap.getUrlPatterns(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java index 7c8a30f9..07e5968e 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.Set; +import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; import net.smyler.smylib.game.GameClient; import net.smyler.smylib.game.Key; import net.smyler.smylib.game.Translator; @@ -45,10 +46,9 @@ public class TerramapConfigScreen extends Screen { private final ToggleButtonWidget showChatOnMapToggle = new ToggleButtonWidget(10, false); private final OptionSliderWidget tileScalingSlider = new OptionSliderWidget<>(10, TileScalingOption.values()); private final IntegerSliderWidget doubleClickDelaySlider = new IntegerSliderWidget(10, TerramapConfig.CLIENT.DOUBLE_CLICK_DELAY_MIN, TerramapConfig.CLIENT.DOUBLE_CLICK_DELAY_MAX, TerramapConfig.CLIENT.DOUBLE_CLICK_DELAY_DEFAULT); - private final IntegerSliderWidget maxLoadedTilesSlider = new IntegerSliderWidget(10, TerramapConfig.CLIENT.TILE_LOAD_MIN, TerramapConfig.CLIENT.TILE_LOAD_MAX, TerramapConfig.CLIENT.TILE_LOAD_DEFAULT); + private final IntegerSliderWidget maxLoadedTilesSlider = new IntegerSliderWidget(10, 0, 1024, 512); private final IntegerSliderWidget lowZoomLevelSlider = new IntegerSliderWidget(10, TerramapConfig.CLIENT.LOW_ZOOM_LEVEL_MIN, TerramapConfig.CLIENT.LOW_ZOOM_LEVEL_MAX, TerramapConfig.CLIENT.LOW_ZOOM_LEVEL_DEFAULT); private final ToggleButtonWidget debugMapStylesToggle = new ToggleButtonWidget(10, false); - private TextButtonWidget reloadMapStylesButton; private final TextFieldWidget tpCommandField; private final TextWidget pageText; @@ -173,9 +173,12 @@ public void init() { mapStylesConfigScreen.addWidget(serverText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(proxyText.getY() + proxyText.getHeight() + inter)); mapStylesConfigScreen.addWidget(userText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(serverText.getY() + serverText.getHeight() + inter)); mapStylesConfigScreen.addWidget(effectiveText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(userText.getY() + userText.getHeight() + inter)); - this.reloadMapStylesButton = new TextButtonWidget(mapStylesConfigScreen.getWidth() / 2 - 153, (effectiveText.getY() + effectiveText.getHeight() + mapStylesConfigScreen.getHeight()) / 2 - 10, 10, 150, translator.format("terramap.configmenu.mapstyles.reload"), () -> {MapStylesLibrary.reload(); TerramapConfigScreen.this.init();}); - mapStylesConfigScreen.addWidget(this.reloadMapStylesButton); - mapStylesConfigScreen.addWidget(new TextButtonWidget(this.reloadMapStylesButton.getX() + this.reloadMapStylesButton.getWidth() + 3, this.reloadMapStylesButton.getY(), 10, 150, translator.format("terramap.configmenu.mapstyles.open"), () -> { + TextButtonWidget reloadMapStylesButton = new TextButtonWidget(mapStylesConfigScreen.getWidth() / 2 - 153, (effectiveText.getY() + effectiveText.getHeight() + mapStylesConfigScreen.getHeight()) / 2 - 10, 10, 150, translator.format("terramap.configmenu.mapstyles.reload"), () -> { + MapStylesLibrary.reload(); + TerramapConfigScreen.this.init(); + }); + mapStylesConfigScreen.addWidget(reloadMapStylesButton); + mapStylesConfigScreen.addWidget(new TextButtonWidget(reloadMapStylesButton.getX() + reloadMapStylesButton.getWidth() + 3, reloadMapStylesButton.getY(), 10, 150, translator.format("terramap.configmenu.mapstyles.open"), () -> { try { Desktop.getDesktop().open(MapStylesLibrary.getFile()); } catch (IOException e) { @@ -226,7 +229,6 @@ private void saveAndClose() { TerramapConfig.CLIENT.saveUiState = this.saveUIStateToggle.getState(); TerramapConfig.CLIENT.chatOnMap = this.showChatOnMapToggle.getState(); TerramapConfig.CLIENT.doubleClickDelay = (int) this.doubleClickDelaySlider.getValue(); - TerramapConfig.CLIENT.maxTileLoad = (int) this.maxLoadedTilesSlider.getValue(); TerramapConfig.CLIENT.lowZoomLevel = (int) this.lowZoomLevelSlider.getValue(); TerramapConfig.tpllcmd = this.tpCommandField.getText(); TerramapConfig.enableDebugMaps = this.debugMapStylesToggle.getState(); @@ -244,8 +246,9 @@ private void reset() { this.saveUIStateToggle.setState(TerramapConfig.CLIENT.saveUiState); this.showChatOnMapToggle.setState(TerramapConfig.CLIENT.chatOnMap); this.doubleClickDelaySlider.setValue(TerramapConfig.CLIENT.doubleClickDelay); - this.maxLoadedTilesSlider.setValue(TerramapConfig.CLIENT.maxTileLoad); - this.lowZoomLevelSlider.setValue(TerramapConfig.CLIENT.lowZoomLevel); + this.maxLoadedTilesSlider.setValue(CachingRasterTiledMap.CACHE_SIZE); + this.maxLoadedTilesSlider.setEnabled(false); + this.lowZoomLevelSlider.setValue(CachingRasterTiledMap.LOW_ZOOM); this.tpCommandField.setText(TerramapConfig.tpllcmd); this.debugMapStylesToggle.setState(TerramapConfig.enableDebugMaps); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java index 5488cecb..dc7e5f51 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java @@ -4,7 +4,6 @@ import java.util.LinkedList; import java.util.Map; -import fr.thesmyler.terramap.TerramapConfig; import net.smyler.terramap.Terramap; import net.smyler.terramap.util.geo.TilePos; import net.smyler.terramap.util.geo.TilePosImmutable; @@ -18,23 +17,23 @@ * Tiles with zoom levels lower than a certain value will also never be unloaded, so that backup textures are always kept. * It also holds the metadata defined in the map config. * - * @author SmylerMC + * @author Smyler * * @param The type of tile handled by this map */ public abstract class CachingRasterTiledMap implements RasterTiledMap { + public static final int CACHE_SIZE = 512; + public static final int LOW_ZOOM = 2; + private final LinkedList tileList; // Uses for ordered access when unloading private final Map tileMap; // Used for unordered access - private int lowZoom = 0; private boolean useLowZoom = true; - private int maxLoaded; private int baseLoad = 0; public CachingRasterTiledMap() { this.tileList = new LinkedList<>(); this.tileMap = new HashMap<>(); - this.maxLoaded = TerramapConfig.CLIENT.maxTileLoad; } @Override @@ -67,7 +66,7 @@ protected void loadTile(T tile) { /** * Unloads the given tile: - * unload it's texture from the GPU and stop any pending http request to get that texture, and forgets about it. + * unload its texture from the GPU and stop any pending http request to get that texture, and forgets about it. * * @param tile - the tile to unload */ @@ -88,7 +87,9 @@ public int getLoadedCount() { } private void needTile(T tile) { - if(tile.getPosition().getZoom() <= this.lowZoom) return; // Those should stay where they are + if(tile.getPosition().getZoom() <= LOW_ZOOM) { + return; // Those should stay where they are + } this.tileList.remove(tile); if(this.tileList.size() >= this.baseLoad) { this.tileList.add(this.baseLoad, tile); @@ -100,15 +101,17 @@ private void needTile(T tile) { private void prepareLowTiles() { this.unloadAll(); - this.lowZoom = Math.min(3, TerramapConfig.CLIENT.lowZoomLevel); // We hard-code that here because we really don't want that to go above 3, 4 would already be 341 tiles if(this.useLowZoom) { - for(int zoom=this.getMinZoom(); zoom<=Math.min(this.getMaxZoom(), this.lowZoom); zoom++) { + for(int zoom=this.getMinZoom(); zoom<=Math.min(this.getMaxZoom(), LOW_ZOOM); zoom++) { int size = WebMercatorUtil.getDimensionsInTile(zoom); for(int x=0; x this.maxLoaded) { + while(this.tileList.size() > CACHE_SIZE) { T toUnload = this.tileList.removeLast(); this.tileMap.remove(toUnload.getPosition()); this.unloadTile(toUnload); } } - public boolean getUsesLowZoom() { - return this.useLowZoom; - } - - public void setUseLowZoom(boolean yesNo) { + public void setUsesLowZoom(boolean yesNo) { this.useLowZoom = yesNo; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java index e1857e23..1d3c1c3c 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java @@ -19,6 +19,9 @@ import net.smyler.terramap.Terramap; import net.smyler.terramap.util.geo.WebMercatorBounds; +import static java.lang.Integer.parseInt; +import static net.smyler.smylib.Preconditions.checkState; + public class MapStylesLibrary { @@ -57,6 +60,7 @@ public static void loadBuiltIns() { try { // https://github.com/MinecraftForge/MinecraftForge/issues/5713 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); + checkState(in != null, "Resource not found: " + path); try(BufferedReader txtReader = new BufferedReader(new InputStreamReader(in))) { StringBuilder json = new StringBuilder(); String line = txtReader.readLine(); @@ -193,27 +197,32 @@ private static UrlTiledMap readFromSaved(String id, SavedMapStyle saved, TiledMa patterns = new String[] {saved.url}; } else throw new IllegalArgumentException("Could not find any valid url for map style " + id + "-" + provider + "v" + version); } - Map bounds = new HashMap<>(); - if(saved.bounds != null) for(String key: saved.bounds.keySet()) { - int zoom = Integer.parseInt(key); - bounds.put(zoom, saved.bounds.get(key)); - } - return new UrlTiledMap( + UrlTiledMap map = new UrlTiledMap( patterns, saved.min_zoom, saved.max_zoom, id, - saved.name, - saved.copyright, saved.display_priority, saved.allow_on_minimap, provider, version, comment, saved.max_concurrent_requests, - saved.debug, - bounds - ); + saved.debug + ); + saved.name.forEach(map::setNameTranslation); + saved.copyright.forEach(map::setTranslatedCopyright); + if (saved.bounds != null) { + saved.bounds.forEach((i, b) -> { + try { + int zoomLevel = parseInt(i); + map.setBounds(zoomLevel, b); + } catch (NumberFormatException e) { + Terramap.instance().logger().warn("Ignoring invalid zoom level: {}: {}", i, e.getMessage()); + } + }); + } + return map; } private static Map loadFromFile(File file, TiledMapProvider provider) throws IOException { @@ -256,6 +265,7 @@ public static File getFile() { return configMapsFile; } + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") private static class SavedMapStyle { String url; // Used by legacy versions diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java index ac70b496..6c66d582 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java @@ -4,7 +4,6 @@ import fr.thesmyler.terramap.maps.raster.TiledMapProvider; import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePosImmutable; -import net.minecraft.util.ResourceLocation; import static net.smyler.smylib.SmyLib.getGameClient; @@ -13,8 +12,7 @@ public class TerrainPreviewMap extends CachingRasterTiledMap public static final int BASE_ZOOM_LEVEL = 16; public TerrainPreviewMap() { - super(); - this.setUseLowZoom(false); // Loading tiles at low zoom levels takes forever here + this.setUsesLowZoom(false); // Loading tiles at low zoom levels takes forever here } @Override protected TerrainPreviewTile createNewTile(TilePosImmutable position) { diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java index e761ead2..61e16ff6 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java @@ -3,6 +3,7 @@ import java.awt.image.BufferedImage; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; import java.util.Map; import com.google.common.base.Strings; @@ -11,7 +12,6 @@ import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import fr.thesmyler.terramap.maps.raster.TiledMapProvider; -import fr.thesmyler.terramap.network.SP2CMapStylePacket; import net.smyler.smylib.Identifier; import net.smyler.terramap.util.CopyrightHolder; import net.smyler.smylib.text.Text; @@ -24,7 +24,7 @@ import static net.smyler.smylib.SmyLib.getGameClient; /** - * Instances are usually created in {@link MapStylesLibrary} and {@link SP2CMapStylePacket}. + * Instances are usually created in {@link MapStylesLibrary} or packets. * * @author Smyler * @@ -39,41 +39,36 @@ public class UrlTiledMap extends CachingRasterTiledMap implements private final String id; private final TiledMapProvider provider; - private final Map names; // A map of language key => name - private final Map copyrightJsons; + private final Map names = new HashMap<>(); // A map of language key => name + private final Map copyrights = new HashMap<>(); private final long version; private final String comment; private final int maxConcurrentRequests; // How many concurrent http connections are allowed by this map provider. This should be two by default, as that's what OSM requires private final boolean debug; - private final Map bounds; + private final Map bounds = new HashMap<>(); private Identifier errorTileTexture = null; public UrlTiledMap( String[] urlPatterns, - int minZoom, - int maxZoom, + int minZoom, int maxZoom, String id, - Map names, - Map copyright, int displayPriority, boolean allowOnMinimap, TiledMapProvider provider, long version, String comment, int maxConcurrentDownloads, - boolean debug, - Map bounds) { + boolean debug + ) { checkArgument(urlPatterns.length > 0, "At least one url pattern needed"); checkArgument(minZoom >= 0, "Zoom level must be at least 0"); checkArgument(maxZoom >= 0 && maxZoom <= 25, "Zoom level must be at most 25"); checkArgument(!Strings.isNullOrEmpty(id), "A valid map id needs to be provided"); - checkArgument(names != null, "Valid map names needs to be provided"); - checkArgument(copyright != null, "Valid map coprights needs to be provided"); - checkArgument(provider != null, "Av alid map provider needs to be provided"); + checkArgument(provider != null, "A valid map provider needs to be provided"); checkArgument(version >= 0, "Map version number must be positive"); checkArgument(comment != null, "A valid map comment needs to be provided"); - checkArgument(maxConcurrentDownloads > 0 ,"Max concurent downloads must be at least 1"); + checkArgument(maxConcurrentDownloads > 0 ,"Max concurrent downloads must be at least 1"); for(String pattern: urlPatterns) { String url = pattern.replace("{z}", "0").replace("{x}", "0").replace("{y}", "0"); try { @@ -86,8 +81,6 @@ public UrlTiledMap( this.maxZoom = maxZoom; this.minZoom = minZoom; this.id = id; - this.copyrightJsons = copyright; - this.names = names; this.provider = provider; this.version = version; this.comment = comment; @@ -95,7 +88,6 @@ public UrlTiledMap( this.displayPriority = displayPriority; this.maxConcurrentRequests = maxConcurrentDownloads; this.debug = debug; - this.bounds = bounds; } /** @@ -167,14 +159,14 @@ public String getId() { */ @Override public Text getCopyright(String localeKey) { - return this.copyrightJsons.getOrDefault(localeKey, this.copyrightJsons.get("en_us")); + return this.copyrights.getOrDefault(localeKey, this.copyrights.get("en_us")); } /** * @return the language key => copyright json value map for this map */ public Map getUnlocalizedCopyrights() { - return this.copyrightJsons; + return this.copyrights; } /** @@ -270,6 +262,53 @@ public Identifier getDefaultTileTexture() { return this.errorTileTexture; } + /** + * Set the localized name of this map in a given language. + * If the provided value is null, unset the translation for the given language. + * + * @param languageKey a language key (e.g. eu_US) + * @param value the translated map name + */ + public void setNameTranslation(String languageKey, String value) { + checkArgument(languageKey != null, "Language key cannot be null"); + if (value == null) { + this.names.remove(languageKey); + } else { + this.names.put(languageKey, value); + } + } + + /** + * Set the localized copyright of this map in a given language. + * If the provided value is null, unset the translation for the given language. + * + * @param languageKey a language key (e.g. en_US) + * @param value the translated copyright {@link Text} + */ + public void setTranslatedCopyright(String languageKey, Text value) { + checkArgument(languageKey != null, "Language key cannot be null"); + if (value == null) { + this.copyrights.remove(languageKey); + } else { + this.copyrights.put(languageKey, value); + } + } + + /** + * Set the bounds of this map at a given zoom level. + * If the provided bounds are null, existing bounds are removed from the given zoom level. + * + * @param zoomLevel the zoom level for which to apply the bounds + * @param bounds the bounds + */ + public void setBounds(int zoomLevel, WebMercatorBounds bounds) { + if (bounds == null) { + this.bounds.remove(zoomLevel); + } else { + this.bounds.put(zoomLevel, bounds); + } + } + public void registerErrorTexture() { int[] color = {170, 211, 223}; BufferedImage image = ImageUtil.imageFromColor(256, 256, color); diff --git a/forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java b/forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java index 766c79c3..6d423c55 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java +++ b/forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java @@ -158,22 +158,23 @@ public boolean getBackwardCompat() { } public UrlTiledMap getTiledMap(TiledMapProvider provider) { - return new UrlTiledMap( + UrlTiledMap tiledMap = new UrlTiledMap( this.urlPatterns, this.minZoom, this.maxZoom, this.id, - this.names, - this.copyrights, this.displayPriority, this.isAllowedOnMinimap, provider, this.providerVersion, this.comment, this.maxConcurrentConnections, - this.debug, - this.bounds - ); + this.debug + ); + this.names.forEach(tiledMap::setNameTranslation); + this.copyrights.forEach(tiledMap::setTranslatedCopyright); + this.bounds.forEach(tiledMap::setBounds); + return tiledMap; } public static class SP2CMapStylePacketTerramapHandler implements IMessageHandler { From 3f262baadb5e37c7756e1bd008aa7031e8e2c1c8 Mon Sep 17 00:00:00 2001 From: Smyler Date: Thu, 27 Jun 2024 22:19:53 +0200 Subject: [PATCH 03/10] Expose Terramap version from new Terramap interface --- .../java/net/smyler/terramap/Terramap.java | 2 ++ .../fr/thesmyler/terramap/TerramapMod.java | 5 ++++ .../thesmyler/terramap/TerramapVersion.java | 9 ++++--- .../terramap/gui/screens/TerramapScreen.java | 2 +- .../maps/raster/MapStylesLibrary.java | 2 +- .../fr/thesmyler/terramap/TerramapTest.java | 24 +++++++++++++++++++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/net/smyler/terramap/Terramap.java b/core/src/main/java/net/smyler/terramap/Terramap.java index 3028d6d1..4b0004ed 100644 --- a/core/src/main/java/net/smyler/terramap/Terramap.java +++ b/core/src/main/java/net/smyler/terramap/Terramap.java @@ -12,6 +12,8 @@ static Terramap instance() { String MOD_ID = "terramap"; + String version(); + Logger logger(); HttpClient http(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java b/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java index c65fec1e..0bcda47f 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java +++ b/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java @@ -110,6 +110,11 @@ public void onServerStarts(FMLServerStartingEvent event) { } + @Override + public String version() { + return getVersion().toString(); + } + @Override public Logger logger() { return this.logger; diff --git a/forge/src/main/java/fr/thesmyler/terramap/TerramapVersion.java b/forge/src/main/java/fr/thesmyler/terramap/TerramapVersion.java index ce594719..d29e6b10 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/TerramapVersion.java +++ b/forge/src/main/java/fr/thesmyler/terramap/TerramapVersion.java @@ -13,10 +13,13 @@ /** * Represents a version of Terramap + * + * @deprecated Do not depend on the main version string, use dedicated version numbers instead. * * @author SmylerMC * */ +@Deprecated public class TerramapVersion implements Comparable { public final int majorTarget; @@ -58,10 +61,6 @@ public TerramapVersion(int majorTarget, int minorTarget, int buildTarget, Releas this(majorTarget, minorTarget, buildTarget, type, build, revision, false, ""); } - public TerramapVersion(int majorTarget, int minorTarget, int buildTarget, ReleaseType type, int build) { - this(majorTarget, minorTarget, buildTarget, type, build, 0, false, ""); - } - public TerramapVersion(int majorTarget, int minorTarget, int buildTarget) { this(majorTarget, minorTarget, buildTarget, ReleaseType.RELEASE, 0, 0, false, ""); } @@ -214,7 +213,7 @@ public boolean equals(Object other) { } @Override - public int compareTo(TerramapVersion other) { + public int compareTo(@NotNull TerramapVersion other) { if(other == null) { // Null means not installed, so we are always ahead diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java index 7143c744..521972ac 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java @@ -416,7 +416,7 @@ public void onUpdate(float mouseX, float mouseY, WidgetContainer parent) { TerramapClientContext srv = TerramapClientContext.getContext(); EarthGeneratorSettings generationSettings = srv.getGeneratorSettings(); debugBuilder.append(String.format(locale, "FPS: %s", getGameClient().currentFPS())); - debugBuilder.append(String.format(locale, "\nClient: %s", TerramapMod.getVersion())); + debugBuilder.append(String.format(locale, "\nClient: %s", Terramap.instance().version())); debugBuilder.append(String.format(locale, "\nServer: %s", srv.getServerVersion())); debugBuilder.append(String.format(locale, "\nSledgehammer: %s", srv.getSledgehammerVersion())); debugBuilder.append(String.format(locale, "\nProjection: %s", generationSettings != null ? generationSettings.projection() : null)); diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java index 1d3c1c3c..33ce2794 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java @@ -255,7 +255,7 @@ private static String resolveUpdateURL(String hostname) throws UnknownHostExcept throw new UnknownHostException(String.format("No txt record was found at %s ?? Something is wrong, either with the name server or with your dns provider!", hostname)); } try { - return attribute.split("\\|")[1].replace("${version}", TerramapMod.getVersion().toString()); + return attribute.split("\\|")[1].replace("${version}", Terramap.instance().version()); } catch(IndexOutOfBoundsException e) { throw new UnknownHostException("TXT record was malformatted"); } diff --git a/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java b/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java index 86cc1f04..1008d07e 100644 --- a/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java +++ b/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java @@ -1,8 +1,10 @@ package fr.thesmyler.terramap; +import com.google.gson.Gson; import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import net.smyler.smylib.SmyLibTest; import net.smyler.terramap.Terramap; +import net.smyler.terramap.http.HttpClient; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.BeforeEach; @@ -17,9 +19,31 @@ public void initTerramap() { TerramapClientContext.resetContext(); } + @Override + public String version() { + return "0.0.0"; + } + @Override public Logger logger() { return this.logger; } + @Override + public HttpClient http() { + this.logger.warn("HTTP client not implemented in tests"); + return null; + } + + @Override + public Gson gson() { + return this.gsonPretty(); + } + + @Override + public Gson gsonPretty() { + this.logger.warn("GSON not implemented in tests"); + return null; + } + } From cfa8ca6932c7417fc818de3bf8023e707f4ee9a2 Mon Sep 17 00:00:00 2001 From: Smyler Date: Thu, 27 Jun 2024 22:23:24 +0200 Subject: [PATCH 04/10] Move map style hostname to new Terramap interface --- core/src/main/java/net/smyler/terramap/Terramap.java | 1 + forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java | 1 - .../fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java | 3 +-- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/net/smyler/terramap/Terramap.java b/core/src/main/java/net/smyler/terramap/Terramap.java index 4b0004ed..c0c7a2e7 100644 --- a/core/src/main/java/net/smyler/terramap/Terramap.java +++ b/core/src/main/java/net/smyler/terramap/Terramap.java @@ -11,6 +11,7 @@ static Terramap instance() { } String MOD_ID = "terramap"; + String STYLE_UPDATE_HOSTNAME = "styles.terramap.thesmyler.fr"; //TODO use smyler.net String version(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java b/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java index 0bcda47f..5ce4f639 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java +++ b/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java @@ -34,7 +34,6 @@ @Mod(modid=Terramap.MOD_ID, useMetadata=true, dependencies="required-after:terraplusplus@[1.0.569,)") public class TerramapMod implements Terramap { - public static final String STYLE_UPDATE_HOSTNAME = "styles.terramap.thesmyler.fr"; private static TerramapVersion version; // Read from the metadata private final HttpClient http = new TerraplusplusHttpClient(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java index 33ce2794..f6ab5194 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java +++ b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java @@ -12,7 +12,6 @@ import javax.naming.directory.Attributes; import javax.naming.directory.InitialDirContext; -import fr.thesmyler.terramap.TerramapMod; import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap; import net.smyler.smylib.text.Text; @@ -185,7 +184,7 @@ public static void reload() { userMaps.clear(); loadBuiltIns(); loadInternals(); - loadFromOnline(TerramapMod.STYLE_UPDATE_HOSTNAME); + loadFromOnline(Terramap.STYLE_UPDATE_HOSTNAME); loadFromConfigFile(); } From 8e7cd4b594ff6ef94faa6a4596a577b1a53438df Mon Sep 17 00:00:00 2001 From: Smyler Date: Thu, 4 Jul 2024 00:45:24 +0200 Subject: [PATCH 05/10] Rework map styles into tile sets --- .../java/net/smyler/terramap/Terramap.java | 3 + .../tilesets/raster/CachingRasterTileSet.java | 176 ++++++++-------- .../tilesets/raster/ColorTileSet.java | 49 ++++- .../terramap/tilesets}/raster/RasterTile.java | 2 +- .../tilesets/raster/RasterTileSet.java | 8 +- .../tilesets/raster/RasterTileSetManager.java | 168 +++++++--------- .../raster/RasterTileSetProvider.java | 4 +- .../tilesets/raster/UrlRasterTileSet.java | 188 ++++++++++++++---- .../terramap/TerramapClientContext.java | 37 ++-- .../fr/thesmyler/terramap/TerramapMod.java | 15 +- .../command/TilesetReloadCommand.java | 11 +- .../ServerTerramapEventHandler.java | 2 +- .../terramap/gui/screens/TerramapScreen.java | 35 ++-- .../gui/screens/config/HudConfigScreen.java | 28 +-- .../screens/config/TerramapConfigScreen.java | 69 +++---- .../gui/widgets/map/MinimapWidget.java | 4 +- .../map/layer/GenerationPreviewLayer.java | 8 +- .../map/layer/OnlineRasterMapLayer.java | 20 +- .../gui/widgets/map/layer/RasterMapLayer.java | 8 +- .../terramap/maps/raster/imp/ColorTile.java | 41 ---- .../maps/raster/imp/TerrainPreviewMap.java | 77 ------- .../maps/raster/imp/TerrainPreviewTile.java | 92 --------- .../maps/raster/imp/UrlRasterTile.java | 129 ------------ .../terramap/network/RemoteSynchronizer.java | 9 +- ...cket.java => SP2CRasterTileSetPacket.java} | 38 ++-- .../network/TerramapNetworkManager.java | 12 +- .../terramap/proxy/TerramapClientProxy.java | 4 +- .../raster/TerrainPreviewTileSet.java | 161 +++++++++++++++ .../fr/thesmyler/terramap/TerramapTest.java | 11 +- .../gui/widgets/map/MapWidgetTest.java | 11 +- 30 files changed, 688 insertions(+), 732 deletions(-) rename forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java => core/src/main/java/net/smyler/terramap/tilesets/raster/CachingRasterTileSet.java (64%) rename forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java => core/src/main/java/net/smyler/terramap/tilesets/raster/ColorTileSet.java (59%) rename {forge/src/main/java/fr/thesmyler/terramap/maps => core/src/main/java/net/smyler/terramap/tilesets}/raster/RasterTile.java (87%) rename forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java => core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSet.java (92%) rename forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java => core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetManager.java (58%) rename forge/src/main/java/fr/thesmyler/terramap/maps/raster/TiledMapProvider.java => core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetProvider.java (87%) rename forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java => core/src/main/java/net/smyler/terramap/tilesets/raster/UrlRasterTileSet.java (64%) delete mode 100644 forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java delete mode 100644 forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java delete mode 100644 forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java delete mode 100644 forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java rename forge/src/main/java/fr/thesmyler/terramap/network/{SP2CMapStylePacket.java => SP2CRasterTileSetPacket.java} (85%) create mode 100644 forge/src/main/java/net/smyler/terramap/tilesets/raster/TerrainPreviewTileSet.java diff --git a/core/src/main/java/net/smyler/terramap/Terramap.java b/core/src/main/java/net/smyler/terramap/Terramap.java index c0c7a2e7..67366c92 100644 --- a/core/src/main/java/net/smyler/terramap/Terramap.java +++ b/core/src/main/java/net/smyler/terramap/Terramap.java @@ -2,6 +2,7 @@ import com.google.gson.Gson; import net.smyler.terramap.http.HttpClient; +import net.smyler.terramap.tilesets.raster.RasterTileSetManager; import org.apache.logging.log4j.Logger; public interface Terramap { @@ -23,6 +24,8 @@ static Terramap instance() { Gson gsonPretty(); + RasterTileSetManager rasterTileSetManager(); + class InstanceHolder { private static Terramap instance; public static void setInstance(Terramap instance) { diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/CachingRasterTileSet.java similarity index 64% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/CachingRasterTileSet.java index dc7e5f51..8fda8530 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/CachingRasterTiledMap.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/CachingRasterTileSet.java @@ -1,4 +1,4 @@ -package fr.thesmyler.terramap.maps.raster; +package net.smyler.terramap.tilesets.raster; import java.util.HashMap; import java.util.LinkedList; @@ -15,33 +15,46 @@ * This class is in charge of keeping track of and loading the tiles used for rendering a specific map. * When tiles need to be unloaded, priority is given to keep those that were used recently loaded. * Tiles with zoom levels lower than a certain value will also never be unloaded, so that backup textures are always kept. - * It also holds the metadata defined in the map config. - * - * @author Smyler * - * @param The type of tile handled by this map + * @author Smyler */ -public abstract class CachingRasterTiledMap implements RasterTiledMap { +public abstract class CachingRasterTileSet implements RasterTileSet { public static final int CACHE_SIZE = 512; public static final int LOW_ZOOM = 2; - private final LinkedList tileList; // Uses for ordered access when unloading - private final Map tileMap; // Used for unordered access + private final LinkedList tileList; // Uses for ordered access when unloading + private final Map tileMap; // Used for unordered access private boolean useLowZoom = true; private int baseLoad = 0; - public CachingRasterTiledMap() { + public CachingRasterTileSet() { this.tileList = new LinkedList<>(); this.tileMap = new HashMap<>(); } @Override - public T getTile(TilePos position) { + public void setup() { + this.unloadToMaxLoad(); + if(this.baseLoad <= 0){ + this.prepareLowTiles(); + } + } + + /** + * Create a new tile from the given coordinates. + * + * @param pos the position to create the tile for + * @return a new tile created by the implementation + */ + protected abstract RasterTile createNewTile(TilePosImmutable pos); + + @Override + public RasterTile getTile(TilePos position) { TilePosImmutable pos = position.getImmutable(); WebMercatorBounds b = this.getBounds(pos.getZoom()); if(b != null && !b.contains(pos)) throw new InvalidTilePositionException(); - T tile = this.tileMap.get(pos); + RasterTile tile = this.tileMap.get(pos); if(tile != null) { this.needTile(tile); return tile; @@ -51,32 +64,27 @@ public T getTile(TilePos position) { return tile; } - protected abstract T createNewTile(TilePosImmutable pos); - /** - * Loads a tile and registers it as last used. Doesn't load its texture. - * - * @param tile - the tile to load + * Unloads tiles until we are at the max number of loaded tiles */ - protected void loadTile(T tile) { - this.tileList.add(Math.min(this.baseLoad, this.tileList.size()), tile); - this.tileMap.put(tile.getPosition(), tile); - this.unloadToMaxLoad(); + public void unloadToMaxLoad() { + while(this.tileList.size() > CACHE_SIZE) { + RasterTile toUnload = this.tileList.removeLast(); + this.tileMap.remove(toUnload.getPosition()); + this.unloadTile(toUnload); + } } /** - * Unloads the given tile: - * unload its texture from the GPU and stop any pending http request to get that texture, and forgets about it. - * - * @param tile - the tile to unload + * Unloads all tiles, after this operation, this map will be as if it was just instantiated. */ - public void unloadTile(T tile) { - tile.unloadTexture(); - tile = this.tileMap.remove(tile.getPosition()); - if(tile != null) { - tile.unloadTexture(); - this.tileList.remove(tile); + public void unloadAll() { + while(!this.tileList.isEmpty()) { + RasterTile toUnload = this.tileList.removeLast(); + this.tileMap.remove(toUnload.getPosition()); + this.unloadTile(toUnload); } + this.baseLoad = 0; } /** @@ -86,7 +94,38 @@ public int getLoadedCount() { return this.tileList.size(); } - private void needTile(T tile) { + /** + * @return the number of tiles at low zoom levels this map is keeping loaded + */ + public int getBaseLoad() { + return this.baseLoad; + } + + public void setUsesLowZoom(boolean yesNo) { + this.useLowZoom = yesNo; + } + + @Override + public int compareTo(RasterTileSet other) { + return Integer.compare(this.getDisplayPriority(), other.getDisplayPriority()); + } + + private void loadTile(RasterTile tile) { + this.tileList.add(Math.min(this.baseLoad, this.tileList.size()), tile); + this.tileMap.put(tile.getPosition(), tile); + this.unloadToMaxLoad(); + } + + private void unloadTile(RasterTile tile) { + tile.unloadTexture(); + tile = this.tileMap.remove(tile.getPosition()); + if(tile != null) { + tile.unloadTexture(); + this.tileList.remove(tile); + } + } + + private void needTile(RasterTile tile) { if(tile.getPosition().getZoom() <= LOW_ZOOM) { return; // Those should stay where they are } @@ -101,70 +140,25 @@ private void needTile(T tile) { private void prepareLowTiles() { this.unloadAll(); - if(this.useLowZoom) { - for(int zoom=this.getMinZoom(); zoom<=Math.min(this.getMaxZoom(), LOW_ZOOM); zoom++) { - int size = WebMercatorUtil.getDimensionsInTile(zoom); - for(int x=0; x CACHE_SIZE) { - T toUnload = this.tileList.removeLast(); - this.tileMap.remove(toUnload.getPosition()); - this.unloadTile(toUnload); - } - } - - public void setUsesLowZoom(boolean yesNo) { - this.useLowZoom = yesNo; - } - - @Override - public int compareTo(RasterTiledMap other) { - return Integer.compare(this.getDisplayPriority(), other.getDisplayPriority()); - } - } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/ColorTileSet.java similarity index 59% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/ColorTileSet.java index ec583803..0a380c1f 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTiledMap.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/ColorTileSet.java @@ -1,8 +1,6 @@ -package fr.thesmyler.terramap.maps.raster.imp; +package net.smyler.terramap.tilesets.raster; import net.smyler.smylib.Color; -import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; import net.smyler.smylib.Identifier; import net.smyler.terramap.util.ImageUtil; import net.smyler.terramap.util.geo.TilePosImmutable; @@ -11,13 +9,13 @@ import static net.smyler.smylib.SmyLib.getGameClient; -public class ColorTiledMap extends CachingRasterTiledMap { +public class ColorTileSet extends CachingRasterTileSet { private final Color color; private final String name; private final Identifier textureLocation; - public ColorTiledMap(Color color, String name) { + public ColorTileSet(Color color, String name) { this.color = color; this.name = name; if (getGameClient().isGlAvailabale()) { @@ -29,7 +27,7 @@ public ColorTiledMap(Color color, String name) { } @Override - protected ColorTile createNewTile(TilePosImmutable pos) { + protected RasterTile createNewTile(TilePosImmutable pos) { return new ColorTile(pos, this.textureLocation); } @@ -59,8 +57,8 @@ public String getComment() { } @Override - public TiledMapProvider getProvider() { - return TiledMapProvider.INTERNAL; + public RasterTileSetProvider getProvider() { + return RasterTileSetProvider.INTERNAL; } @Override @@ -92,4 +90,39 @@ public Color getColor() { return this.color; } + private static class ColorTile implements RasterTile { + + private final TilePosImmutable position; + private final Identifier texture; + + public ColorTile(TilePosImmutable position, Identifier texture) { + this.position = position; + this.texture = texture; + } + + @Override + public boolean isTextureAvailable() { + return this.texture != null; + } + + @Override + public Identifier getTexture() { + return this.texture; + } + + @Override + public void cancelTextureLoading() { + } + + @Override + public void unloadTexture() { + // Nop, we don't want to do that ! + } + + @Override + public TilePosImmutable getPosition() { + return this.position; + } + + } } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTile.java similarity index 87% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTile.java index 27766e78..15794f50 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTile.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTile.java @@ -1,4 +1,4 @@ -package fr.thesmyler.terramap.maps.raster; +package net.smyler.terramap.tilesets.raster; import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePosImmutable; diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSet.java similarity index 92% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSet.java index ef59b905..cf6a666a 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/RasterTiledMap.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSet.java @@ -1,4 +1,4 @@ -package fr.thesmyler.terramap.maps.raster; +package net.smyler.terramap.tilesets.raster; import net.smyler.smylib.Identifier; import net.smyler.terramap.util.geo.TilePos; @@ -6,11 +6,11 @@ import net.smyler.terramap.util.geo.WebMercatorBounds; /** - * A raster map made of individual tiles. + * A set of raster tiles that make up a map. * * @author Smyler */ -public interface RasterTiledMap extends Comparable { +public interface RasterTileSet extends Comparable { /** * Initializes the map. @@ -63,7 +63,7 @@ default RasterTile getTile(int zoom, int x, int y) { /** * @return this map's provider */ - TiledMapProvider getProvider(); + RasterTileSetProvider getProvider(); /** * @return the version of this map's provider diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetManager.java similarity index 58% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetManager.java index f6ab5194..84921e5f 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/MapStylesLibrary.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetManager.java @@ -1,4 +1,4 @@ -package fr.thesmyler.terramap.maps.raster; +package net.smyler.terramap.tilesets.raster; import java.io.*; import java.net.InetAddress; @@ -12,49 +12,57 @@ import javax.naming.directory.Attributes; import javax.naming.directory.InitialDirContext; -import fr.thesmyler.terramap.TerramapConfig; -import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap; import net.smyler.smylib.text.Text; import net.smyler.terramap.Terramap; import net.smyler.terramap.util.geo.WebMercatorBounds; import static java.lang.Integer.parseInt; +import static java.util.Collections.unmodifiableMap; import static net.smyler.smylib.Preconditions.checkState; -public class MapStylesLibrary { +public class RasterTileSetManager { private static final String BUILT_IN_MAPS = "assets/terramap/mapstyles.json"; - private static File configMapsFile; - private static final Map baseMaps = new HashMap<>(); - private static final Map userMaps = new HashMap<>(); + + private final File configMapsFile; + + private final Map baseMaps = new HashMap<>(); + private final Map baseMapsReadOnly = unmodifiableMap(this.baseMaps); + private final Map userMaps = new HashMap<>(); + private final Map userMapsReadOnly = unmodifiableMap(this.userMaps); + + public RasterTileSetManager(File file) { + this.configMapsFile = file; + } /** - * Get the default Terramap maps, loaded from the jar and from the online source - * The returned map is a new one, and can be mutated safely. - * Does not actually load the maps, this needs to be done beforehand with {@link MapStylesLibrary#loadBuiltIns()} and {@link MapStylesLibrary#loadFromOnline(String)} - * - * @return a new map that contains id => TiledMap couples + * Provides access to the default Terramap raster map styles, + * which were loaded from the JAR and from the online configuration file. + *
+ * Those maps have to be loaded beforehand with {@link RasterTileSetManager#loadBuiltIns()} + * and {@link RasterTileSetManager#loadFromOnline(String)}. + * + * @return a read-only vue of a map that contains id => {@link RasterTileSet} couples */ - public static Map getBaseMaps() { - return new HashMap<>(baseMaps); + public Map getBaseMaps() { + return this.baseMapsReadOnly; } /** - * Get the default Terramap maps, loaded from config/terramap_user_styles.json. - * The returned map is a new one, and can be mutated safely. - * - * @return a new map that contains id => TiledMap couples + * Provides access to the map styles configured by the user in config/terramap_user_styles.json. + * + * @return a read-only vue of a map that contains id => {@link RasterTileSet} couples */ - public static Map getUserMaps() { - return new HashMap<>(userMaps); + public Map getUserMaps() { + return this.userMapsReadOnly; } /** * Loads map styles from the mod's jar. */ - public static void loadBuiltIns() { - TiledMapProvider.BUILT_IN.setLastError(null); + public void loadBuiltIns() { + RasterTileSetProvider.BUILT_IN.setLastError(null); String path = BUILT_IN_MAPS; try { // https://github.com/MinecraftForge/MinecraftForge/issues/5713 @@ -67,28 +75,17 @@ public static void loadBuiltIns() { json.append(line); line = txtReader.readLine(); } - baseMaps.putAll(loadFromJson(json.toString(), TiledMapProvider.BUILT_IN)); + this.baseMaps.putAll(loadFromJson(json.toString(), RasterTileSetProvider.BUILT_IN)); } } catch(Exception e) { Terramap.instance().logger().fatal("Failed to read built-in map styles, Terramap is likely to not work properly!"); Terramap.instance().logger().fatal("Path: {}", path); Terramap.instance().logger().catching(e); - TiledMapProvider.BUILT_IN.setLastError(e); + RasterTileSetProvider.BUILT_IN.setLastError(e); } } - public static void loadInternals() { - TiledMapProvider.INTERNAL.setLastError(null); - try { - // We currently have no internal styles - } catch(Exception e) { - Terramap.instance().logger().error("Failed to load internal map styles"); - Terramap.instance().logger().catching(e); - TiledMapProvider.INTERNAL.setLastError(e); - } - } - /** * Loads map styles from the online file. * Resolve the TXT field of the given hostname, @@ -100,11 +97,11 @@ public static void loadInternals() { * * @param hostname - the hostname to lookup */ - public static void loadFromOnline(String hostname) { - TiledMapProvider.ONLINE.setLastError(null); + public void loadFromOnline(String hostname) { + RasterTileSetProvider.ONLINE.setLastError(null); String url; try { - url = resolveUpdateURL(hostname); + url = this.resolveUpdateURL(hostname); } catch (UnknownHostException | NamingException e1) { Terramap.instance().logger().error("Failed to resolve map styles urls!"); Terramap.instance().logger().catching(e1); @@ -115,7 +112,7 @@ public static void loadFromOnline(String hostname) { if(e != null) { Terramap.instance().logger().error("Failed to download updated map style file!"); Terramap.instance().logger().catching(e); - TiledMapProvider.ONLINE.setLastError(e); + RasterTileSetProvider.ONLINE.setLastError(e); } try(BufferedReader txtReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)))) { StringBuilder json = new StringBuilder(); @@ -124,71 +121,62 @@ public static void loadFromOnline(String hostname) { json.append(line); line = txtReader.readLine(); } - baseMaps.putAll(loadFromJson(json.toString(), TiledMapProvider.ONLINE)); + baseMaps.putAll(loadFromJson(json.toString(), RasterTileSetProvider.ONLINE)); } catch(Exception f) { Terramap.instance().logger().error("Failed to parse updated map style file!"); Terramap.instance().logger().catching(e); - TiledMapProvider.ONLINE.setLastError(e); + RasterTileSetProvider.ONLINE.setLastError(e); } }); } /** * Load map styles defined in config/terramap_user_styles.json. - * The file to load from needs to be set first with {@link #setConfigMapFile(File)} */ - public static void loadFromConfigFile() { - TiledMapProvider.CUSTOM.setLastError(null); - if(configMapsFile == null) { - Terramap.instance().logger().error("Map config file was null!"); - TiledMapProvider.CUSTOM.setLastError(new NullPointerException("Map style config files was null")); + public void loadFromConfigFile() { + if (this.configMapsFile == null) { + Terramap.instance().logger().info("No tile sets config file provided!"); return; } - if(!configMapsFile.exists()) { + RasterTileSetProvider.CUSTOM.setLastError(null); + if(!this.configMapsFile.exists()) { try { Terramap.instance().logger().debug("Map config file did not exist, creating a blank one."); - MapStyleFile mapFile = new MapStyleFile(new MapFileMetadata(0, "Add custom map styles here. See an example at styles.terramap.thesmyler.fr (open in your browser, do not add http or https prefix)")); - Files.write(configMapsFile.toPath(), Terramap.instance().gsonPretty().toJson(mapFile).getBytes(Charset.defaultCharset())); + TileSetFile mapFile = new TileSetFile(new TileSetFileMetadata(0, "Add custom map styles here. See an example at styles.terramap.thesmyler.fr (open in your browser, do not add http or https prefix)")); + Files.write(this.configMapsFile.toPath(), Terramap.instance().gsonPretty().toJson(mapFile).getBytes(Charset.defaultCharset())); } catch (IOException e) { Terramap.instance().logger().error("Failed to create map style config file!"); Terramap.instance().logger().catching(e); - TiledMapProvider.CUSTOM.setLastError(e); + RasterTileSetProvider.CUSTOM.setLastError(e); } } else { try { - userMaps.putAll(loadFromFile(configMapsFile, TiledMapProvider.CUSTOM)); + this.userMaps.putAll(this.loadFromCustomFile(this.configMapsFile)); } catch (Exception e) { Terramap.instance().logger().error("Failed to read map style config file!"); Terramap.instance().logger().catching(e); - TiledMapProvider.CUSTOM.setLastError(e); + RasterTileSetProvider.CUSTOM.setLastError(e); } } } - /** - * Set the config file that should contain the user's custom map syles - * It will be created if it does not exist. - * - * @param file - the json config file - */ - public static void setConfigMapFile(File file) { - configMapsFile = file; - } - /** * Reload map styles from the jar, online, and config/terramap_user_styles.json */ - public static void reload() { - baseMaps.clear(); - userMaps.clear(); - loadBuiltIns(); - loadInternals(); - loadFromOnline(Terramap.STYLE_UPDATE_HOSTNAME); - loadFromConfigFile(); + public void reload(boolean enableDebugTileSet) { + this.baseMaps.clear(); + this.userMaps.clear(); + this.loadBuiltIns(); + this.loadFromOnline(Terramap.STYLE_UPDATE_HOSTNAME); + this.loadFromConfigFile(); + if (!enableDebugTileSet) { + this.baseMaps.values().removeIf(RasterTileSet::isDebug); + this.userMaps.values().removeIf(RasterTileSet::isDebug); + } } - private static UrlTiledMap readFromSaved(String id, SavedMapStyle saved, TiledMapProvider provider, long version, String comment) { + private UrlRasterTileSet readFromSaved(String id, TileSetDefinition saved, RasterTileSetProvider provider, long version, String comment) { String[] patterns = saved.urls; if(patterns == null || patterns.length == 0) { if(saved.url != null) { @@ -196,7 +184,7 @@ private static UrlTiledMap readFromSaved(String id, SavedMapStyle saved, TiledMa patterns = new String[] {saved.url}; } else throw new IllegalArgumentException("Could not find any valid url for map style " + id + "-" + provider + "v" + version); } - UrlTiledMap map = new UrlTiledMap( + UrlRasterTileSet map = new UrlRasterTileSet( patterns, saved.min_zoom, saved.max_zoom, @@ -224,26 +212,22 @@ private static UrlTiledMap readFromSaved(String id, SavedMapStyle saved, TiledMa return map; } - private static Map loadFromFile(File file, TiledMapProvider provider) throws IOException { + private Map loadFromCustomFile(File file) throws IOException { String json = String.join("", Files.readAllLines(file.toPath())); - return loadFromJson(json, provider); + return loadFromJson(json, RasterTileSetProvider.CUSTOM); } - private static Map loadFromJson(String json, TiledMapProvider provider) { - MapStyleFile savedStyles = Terramap.instance().gson().fromJson(json, MapStyleFile.class); - Map styles = new HashMap<>(); + private Map loadFromJson(String json, RasterTileSetProvider provider) { + TileSetFile savedStyles = Terramap.instance().gson().fromJson(json, TileSetFile.class); + Map styles = new HashMap<>(); for(String id: savedStyles.maps.keySet()) { - UrlTiledMap style = readFromSaved(id, savedStyles.maps.get(id), provider, savedStyles.metadata.version, savedStyles.metadata.comment); - if(!TerramapConfig.enableDebugMaps && style.isDebug()) { - Terramap.instance().logger().info("Not loading debug map style {}", style.getId()); - continue; - } + UrlRasterTileSet style = readFromSaved(id, savedStyles.maps.get(id), provider, savedStyles.metadata.version, savedStyles.metadata.comment); styles.put(id, style); } return styles; } - private static String resolveUpdateURL(String hostname) throws UnknownHostException, NamingException { + private String resolveUpdateURL(String hostname) throws UnknownHostException, NamingException { InetAddress inetAddress = InetAddress.getByName(hostname); InitialDirContext iDirC = new InitialDirContext(); Attributes attributes = iDirC.getAttributes("dns:/" + inetAddress.getHostName(), new String[] {"TXT"}); @@ -260,12 +244,12 @@ private static String resolveUpdateURL(String hostname) throws UnknownHostExcept } } - public static File getFile() { - return configMapsFile; + public File getFile() { + return this.configMapsFile; } @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") - private static class SavedMapStyle { + private static class TileSetDefinition { String url; // Used by legacy versions String[] urls; @@ -282,24 +266,24 @@ private static class SavedMapStyle { } @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") - private static class MapStyleFile { + private static class TileSetFile { - Map maps; - MapFileMetadata metadata; + Map maps; + TileSetFileMetadata metadata; - MapStyleFile(MapFileMetadata metadata) { + TileSetFile(TileSetFileMetadata metadata) { this.metadata = metadata; this.maps = new HashMap<>(); } } - private static class MapFileMetadata { + private static class TileSetFileMetadata { long version; String comment; - MapFileMetadata(long version, String comment) { + TileSetFileMetadata(long version, String comment) { this.comment = comment; this.version = version; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/TiledMapProvider.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetProvider.java similarity index 87% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/TiledMapProvider.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetProvider.java index c7556fe4..8fbe252b 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/TiledMapProvider.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/RasterTileSetProvider.java @@ -1,6 +1,6 @@ -package fr.thesmyler.terramap.maps.raster; +package net.smyler.terramap.tilesets.raster; -public enum TiledMapProvider { +public enum RasterTileSetProvider { BUILT_IN, // From the jar, should never be used INTERNAL, // Loaded from code, usually debug maps diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java b/core/src/main/java/net/smyler/terramap/tilesets/raster/UrlRasterTileSet.java similarity index 64% rename from forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java rename to core/src/main/java/net/smyler/terramap/tilesets/raster/UrlRasterTileSet.java index 61e16ff6..241e6df7 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlTiledMap.java +++ b/core/src/main/java/net/smyler/terramap/tilesets/raster/UrlRasterTileSet.java @@ -1,17 +1,15 @@ -package fr.thesmyler.terramap.maps.raster.imp; +package net.smyler.terramap.tilesets.raster; import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; -import com.google.common.base.Strings; - -import fr.thesmyler.terramap.TerramapConfig; -import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; import net.smyler.smylib.Identifier; import net.smyler.terramap.util.CopyrightHolder; import net.smyler.smylib.text.Text; @@ -20,16 +18,19 @@ import net.smyler.terramap.util.geo.TilePosImmutable; import net.smyler.terramap.util.geo.WebMercatorBounds; +import javax.imageio.ImageIO; + import static net.smyler.smylib.Preconditions.checkArgument; import static net.smyler.smylib.SmyLib.getGameClient; +import static net.smyler.smylib.Strings.isNullOrEmpty; /** - * Instances are usually created in {@link MapStylesLibrary} or packets. + * Instances are usually created in {@link RasterTileSetManager} or packets. * * @author Smyler * */ -public class UrlTiledMap extends CachingRasterTiledMap implements CopyrightHolder { +public class UrlRasterTileSet extends CachingRasterTileSet implements CopyrightHolder { private final String[] urlPatterns; private final int maxZoom; @@ -38,7 +39,7 @@ public class UrlTiledMap extends CachingRasterTiledMap implements private final boolean allowOnMinimap; private final String id; - private final TiledMapProvider provider; + private final RasterTileSetProvider provider; private final Map names = new HashMap<>(); // A map of language key => name private final Map copyrights = new HashMap<>(); private final long version; @@ -49,13 +50,13 @@ public class UrlTiledMap extends CachingRasterTiledMap implements private Identifier errorTileTexture = null; - public UrlTiledMap( + public UrlRasterTileSet( String[] urlPatterns, int minZoom, int maxZoom, String id, int displayPriority, boolean allowOnMinimap, - TiledMapProvider provider, + RasterTileSetProvider provider, long version, String comment, int maxConcurrentDownloads, @@ -64,18 +65,13 @@ public UrlTiledMap( checkArgument(urlPatterns.length > 0, "At least one url pattern needed"); checkArgument(minZoom >= 0, "Zoom level must be at least 0"); checkArgument(maxZoom >= 0 && maxZoom <= 25, "Zoom level must be at most 25"); - checkArgument(!Strings.isNullOrEmpty(id), "A valid map id needs to be provided"); + checkArgument(!isNullOrEmpty(id), "A valid map id needs to be provided"); checkArgument(provider != null, "A valid map provider needs to be provided"); checkArgument(version >= 0, "Map version number must be positive"); checkArgument(comment != null, "A valid map comment needs to be provided"); checkArgument(maxConcurrentDownloads > 0 ,"Max concurrent downloads must be at least 1"); for(String pattern: urlPatterns) { - String url = pattern.replace("{z}", "0").replace("{x}", "0").replace("{y}", "0"); - try { - new URL(url); // Checking if the URL is valid - } catch (MalformedURLException e) { - throw new IllegalArgumentException(url + " is not a valid url pattern"); - } + checkUrlPattern(pattern); } this.urlPatterns = urlPatterns; this.maxZoom = maxZoom; @@ -90,30 +86,15 @@ public UrlTiledMap( this.debug = debug; } - /** - * Initializes this map by loading all tiles bellow a certain zoom level specified in {@link TerramapConfig}, and starts loading their textures, if it hasn't been done yet. - * Also makes sure the cache follows the mod's config - */ @Override public void setup() { this.registerErrorTexture(); - for(String urlPattern: this.getUrlPatterns()) { - String url = urlPattern.replace("{z}", "0").replace("{x}", "0").replace("{y}", "0"); - try { - URL parsed = new URL(url); - if(parsed.getProtocol().startsWith("http")) { - Terramap.instance().http().setMaxConcurrentRequests(url, this.getMaxConcurrentRequests()); - } - } catch(IllegalArgumentException | MalformedURLException e) { - Terramap.instance().logger().error("Failed to set max concurrent requests for host. Url :{}", url); - Terramap.instance().logger().catching(e); - } - } + this.enforceMaxConcurrentRequests(); super.setup(); } @Override - protected UrlRasterTile createNewTile(TilePosImmutable pos) { + protected RasterTile createNewTile(TilePosImmutable pos) { String pat = this.urlPatterns[(pos.getZoom() + pos.getX() + pos.getY()) % this.urlPatterns.length]; return new UrlRasterTile(pat, pos); } @@ -124,8 +105,7 @@ public boolean isDebug() { } /** - * - * @return the minimum zoom level that map supports, that's usually 0 + * @return the minimum zoom level supported by this tile set, which is usually 0 */ @Override public int getMinZoom() { @@ -133,7 +113,7 @@ public int getMinZoom() { } /** - * @return the maximum zoom level this map supports + * @return the maximum zoom level supported by this tile set */ @Override public int getMaxZoom() { @@ -215,7 +195,7 @@ public String getComment() { * @return this map's provider */ @Override - public TiledMapProvider getProvider() { + public RasterTileSetProvider getProvider() { return this.provider; } @@ -309,10 +289,136 @@ public void setBounds(int zoomLevel, WebMercatorBounds bounds) { } } - public void registerErrorTexture() { + private void registerErrorTexture() { int[] color = {170, 211, 223}; BufferedImage image = ImageUtil.imageFromColor(256, 256, color); this.errorTileTexture = getGameClient().guiDrawContext().loadDynamicTexture(image); } + private void enforceMaxConcurrentRequests() { + for(String urlPattern: this.getUrlPatterns()) { + String url = urlPattern.replace("{z}", "0").replace("{x}", "0").replace("{y}", "0"); + try { + URL parsed = new URL(url); + if(parsed.getProtocol().startsWith("http")) { + Terramap.instance().http().setMaxConcurrentRequests(url, this.getMaxConcurrentRequests()); + } + } catch(IllegalArgumentException | MalformedURLException e) { + Terramap.instance().logger().error("Failed to set max concurrent requests for host. Url :{}", url); + Terramap.instance().logger().catching(e); + } + } + } + + private static void checkUrlPattern(String pattern) { + String url = pattern.replace("{z}", "0").replace("{x}", "0").replace("{y}", "0"); + try { + new URL(url); // Checking if the URL is valid + } catch (MalformedURLException e) { + throw new IllegalArgumentException(url + " is not a valid url pattern"); + } + } + + private static class UrlRasterTile implements RasterTile { + + private final TilePosImmutable pos; + private final String url; + private Identifier texture = null; + private CompletableFuture textureTask; + + public UrlRasterTile(String urlPattern, TilePosImmutable pos) { + this.pos = pos; + this.url = urlPattern + .replace("{x}", String.valueOf(this.getPosition().getX())) + .replace("{y}", String.valueOf(this.getPosition().getY())) + .replace("{z}", String.valueOf(this.getPosition().getZoom())); + } + + public String getURL() { + return this.url; + } + + @Override + public boolean isTextureAvailable() { + if(texture != null) return true; // Don't try loading the texture if it has already been loaded + try { + this.tryLoadingTexture(); + } catch (Throwable e) { + return false; + } + return this.texture != null; + } + + @Override + public Identifier getTexture() throws Throwable { + if(this.texture == null) { + if(this.textureTask == null) { + this.textureTask = Terramap.instance().http().get(this.getURL()); + } else this.tryLoadingTexture(); + } + return this.texture; + } + + private void tryLoadingTexture() throws Throwable { + //TODO Do that fully async, DynamicTexture::new is expensive + if(this.textureTask != null && this.textureTask.isDone()){ + if(this.textureTask.isCompletedExceptionally()) { + if(this.textureTask.isCancelled()) { + this.textureTask = null; + } else { + try { + this.textureTask.get(); // That will throw an exception + } catch(ExecutionException e) { + throw e.getCause(); + } + } + return; + } + byte[] buf = this.textureTask.get(); + if(buf == null) throw new IOException("404 response"); + try (ByteArrayInputStream is = new ByteArrayInputStream(buf)) { + BufferedImage image = ImageIO.read(is); + if(image == null) throw new IOException("Failed to read image! url: " + this.getURL()); + this.texture = getGameClient().guiDrawContext().loadDynamicTexture(image); + } + } + } + + @Override + public void cancelTextureLoading() { + if(this.textureTask != null) { + this.textureTask.cancel(true); + this.textureTask = null; + } + } + + @Override + public void unloadTexture() { + this.cancelTextureLoading(); + if(this.texture != null) { + getGameClient().guiDrawContext().unloadDynamicTexture(this.texture); + this.texture = null; + } + } + + @Override + public boolean equals(Object obj) { + if(obj == this) return true; + if(obj == null) return false; + if(!(obj instanceof UrlRasterTile)) return false; + UrlRasterTile other = (UrlRasterTile) obj; + return other.url.equals(this.url); + } + + @Override + public TilePosImmutable getPosition() { + return this.pos; + } + + @Override + public int hashCode() { + return this.url.hashCode(); + } + + } } \ No newline at end of file diff --git a/forge/src/main/java/fr/thesmyler/terramap/TerramapClientContext.java b/forge/src/main/java/fr/thesmyler/terramap/TerramapClientContext.java index f2edf566..c83059d0 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/TerramapClientContext.java +++ b/forge/src/main/java/fr/thesmyler/terramap/TerramapClientContext.java @@ -18,11 +18,7 @@ import fr.thesmyler.terramap.gui.screens.SavedMainScreenState; import fr.thesmyler.terramap.gui.screens.TerramapScreen; import fr.thesmyler.terramap.input.KeyBindings; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; -import fr.thesmyler.terramap.maps.raster.imp.TerrainPreviewMap; -import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap; +import net.smyler.terramap.tilesets.raster.*; import fr.thesmyler.terramap.network.TerramapNetworkManager; import fr.thesmyler.terramap.network.playersync.C2SPRegisterForUpdatesPacket; import fr.thesmyler.terramap.network.playersync.PlayerSyncStatus; @@ -58,7 +54,7 @@ public class TerramapClientContext { private static TerramapClientContext instance; - private static final GeographicProjection TERRAIN_PREVIEW_PROJECTION = new WebMercatorProjection(TerrainPreviewMap.BASE_ZOOM_LEVEL); + private static final GeographicProjection TERRAIN_PREVIEW_PROJECTION = new WebMercatorProjection(TerrainPreviewTileSet.BASE_ZOOM_LEVEL); private final Map remotePlayers = new HashMap<>(); private PlayerSyncStatus serverSyncPlayers = PlayerSyncStatus.DISABLED; @@ -71,8 +67,8 @@ public class TerramapClientContext { private TerrainPreview terrainPreview = null; private boolean isRegisteredForUpdates = false; private String tpCommand = null; - private final Map serverMaps = new HashMap<>(); - private final Map proxyMaps = new HashMap<>(); + private final Map serverMaps = new HashMap<>(); + private final Map proxyMaps = new HashMap<>(); private boolean proxyHasWarpSupport = false; private boolean serverHasWarpSupport = false; private boolean allowPlayerRadar = true; @@ -193,12 +189,12 @@ public List getEntities() { return Minecraft.getMinecraft().world.loadedEntityList; } - public void addServerMapStyle(UrlTiledMap map) { + public void addServerRasterTileSet(UrlRasterTileSet map) { this.serverMaps.put(map.getId(), map); HudScreenHandler.updateMinimap(); } - public void addProxyMapStyle(UrlTiledMap map) { + public void addProxyRasterTileSet(UrlRasterTileSet map) { this.proxyMaps.put(map.getId(), map); HudScreenHandler.updateMinimap(); } @@ -210,23 +206,24 @@ public void resetWorld() { HudScreenHandler.updateMinimap(); } - public Map getServerMapStyles() { + public Map getServerRasterTileSets() { return this.serverMaps; } - public Map getProxyMapStyles() { + public Map getProxyRasterTileSets() { return this.proxyMaps; } /** - * @return a new Map containing all available mapstyles + * @return a new Map containing all available raster tile sets */ - public Map getMapStyles() { - Map maps = new HashMap<>(); - maps.putAll(MapStylesLibrary.getBaseMaps()); + public Map getRasterTileSets() { + Map maps = new HashMap<>(); + RasterTileSetManager manager = Terramap.instance().rasterTileSetManager(); + maps.putAll(manager.getBaseMaps()); maps.putAll(this.proxyMaps); maps.putAll(this.serverMaps); - maps.putAll(MapStylesLibrary.getUserMaps()); + maps.putAll(manager.getUserMaps()); return maps; } @@ -471,7 +468,7 @@ public boolean isOnEarthWorld() { } public void setupMaps() { - for(RasterTiledMap map: this.getMapStyles().values()) { + for(RasterTileSet map: this.getRasterTileSets().values()) { map.setup(); } } @@ -525,8 +522,8 @@ public static TerramapClientContext getContext() { public static void resetContext() { Terramap.instance().logger().info("Reseting client context"); - TiledMapProvider.SERVER.setLastError(null); - TiledMapProvider.PROXY.setLastError(null); + RasterTileSetProvider.SERVER.setLastError(null); + RasterTileSetProvider.PROXY.setLastError(null); TerramapClientContext.instance = new TerramapClientContext(); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java b/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java index 5ce4f639..012f877b 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java +++ b/forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java @@ -17,7 +17,7 @@ import fr.thesmyler.terramap.TerramapVersion.InvalidVersionString; import fr.thesmyler.terramap.TerramapVersion.ReleaseType; import fr.thesmyler.terramap.eventhandlers.CommonTerramapEventHandler; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; +import net.smyler.terramap.tilesets.raster.RasterTileSetManager; import fr.thesmyler.terramap.permissions.PermissionManager; import fr.thesmyler.terramap.proxy.TerramapProxy; import net.minecraftforge.common.MinecraftForge; @@ -38,6 +38,8 @@ public class TerramapMod implements Terramap { private final HttpClient http = new TerraplusplusHttpClient(); + private RasterTileSetManager rasterTileSetManager; + // These are notable versions public static final TerramapVersion OLDEST_COMPATIBLE_CLIENT = new TerramapVersion(1, 0, 0, ReleaseType.BETA, 6, 0); public static final TerramapVersion OLDEST_TERRA121_TERRAMAP_VERSION = new TerramapVersion(1, 0, 0, ReleaseType.BETA, 6, 7); @@ -78,8 +80,8 @@ public void preInit(FMLPreInitializationEvent event) { } this.logger.info("Terramap version: {}", getVersion()); TerramapMod.proxy.preInit(event); - File mapStyleFile = new File(event.getModConfigurationDirectory().getAbsolutePath() + "/terramap_user_styles.json"); - MapStylesLibrary.setConfigMapFile(mapStyleFile); + File tileSetsConfigFile = new File(event.getModConfigurationDirectory().getAbsolutePath() + "/terramap_user_styles.json"); + this.rasterTileSetManager = new RasterTileSetManager(tileSetsConfigFile); } @EventHandler @@ -87,7 +89,7 @@ public void init(FMLInitializationEvent event) { MinecraftForge.EVENT_BUS.register(new CommonTerramapEventHandler()); TerramapMod.proxy.init(event); PermissionManager.registerNodes(); - MapStylesLibrary.loadFromConfigFile(); + this.rasterTileSetManager().loadFromConfigFile(); } public static TerramapVersion getVersion() { @@ -134,4 +136,9 @@ public Gson gsonPretty() { return this.gsonPretty; } + @Override + public RasterTileSetManager rasterTileSetManager() { + return this.rasterTileSetManager; + } + } diff --git a/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java b/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java index 20cd9348..147255f5 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java +++ b/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java @@ -3,13 +3,14 @@ import java.util.ArrayList; import java.util.List; +import fr.thesmyler.terramap.TerramapMod; +import net.smyler.terramap.Terramap; import org.jetbrains.annotations.Nullable; import fr.thesmyler.terramap.TerramapVersion; import fr.thesmyler.terramap.TerramapVersion.ReleaseType; import fr.thesmyler.terramap.command.TranslationContextBuilder.TranslationContext; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; +import net.smyler.terramap.tilesets.raster.RasterTileSetProvider; import fr.thesmyler.terramap.permissions.Permission; import fr.thesmyler.terramap.permissions.PermissionManager; import net.minecraft.command.CommandBase; @@ -40,11 +41,11 @@ public String getUsage(ICommandSender sender) { public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { TranslationContext ctx = this.translationContextBuilder.createNewContext(sender); if(sender instanceof EntityPlayer && !this.checkPermission(server, sender)) { - ctx.commandException("terramap.commands.reloadmapstyles.forbidden"); + throw ctx.commandException("terramap.commands.reloadmapstyles.forbidden"); } ITextComponent msg = ctx.getComponent("terramap.commands.reloadmapstyles.done"); - MapStylesLibrary.loadFromConfigFile(); - if(TiledMapProvider.CUSTOM.getLastError() == null) { + Terramap.instance().rasterTileSetManager().loadFromConfigFile(); + if(RasterTileSetProvider.CUSTOM.getLastError() == null) { msg.setStyle(new Style().setColor(TextFormatting.GREEN).setBold(false)); } else { msg = ctx.getComponent("terramap.commands.reloadmapstyles.error"); diff --git a/forge/src/main/java/fr/thesmyler/terramap/eventhandlers/ServerTerramapEventHandler.java b/forge/src/main/java/fr/thesmyler/terramap/eventhandlers/ServerTerramapEventHandler.java index 8f8f0af5..a66c7914 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/eventhandlers/ServerTerramapEventHandler.java +++ b/forge/src/main/java/fr/thesmyler/terramap/eventhandlers/ServerTerramapEventHandler.java @@ -18,7 +18,7 @@ public class ServerTerramapEventHandler { @SubscribeEvent public void onPlayerLoggedIn(PlayerLoggedInEvent event){ EntityPlayerMP player = (EntityPlayerMP) event.player; - RemoteSynchronizer.sendMapStylesToClient(player); + RemoteSynchronizer.sendRasterTileSetsToClient(player); TerramapVersion remoteVersion = TerramapVersion.getClientVersion(player); if(remoteVersion == null) { // Not installed on client diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java index 521972ac..7a8981c1 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java @@ -10,13 +10,13 @@ import net.smyler.smylib.text.ImmutableText; import net.smyler.smylib.text.TextStyle; import net.smyler.terramap.Terramap; +import net.smyler.terramap.tilesets.raster.UrlRasterTileSet; import net.smyler.terramap.util.geo.GeoServices; import org.jetbrains.annotations.Nullable; import fr.thesmyler.terramap.gui.widgets.map.*; import fr.thesmyler.terramap.gui.widgets.map.layer.OnlineRasterMapLayer; -import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; -import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap; +import net.smyler.terramap.tilesets.raster.CachingRasterTileSet; import net.buildtheearth.terraplusplus.generator.EarthGeneratorSettings; import net.smyler.smylib.Color; import net.smyler.smylib.game.GameClient; @@ -49,7 +49,6 @@ import net.smyler.smylib.gui.widgets.text.TextWidget; import fr.thesmyler.terramap.MapContext; import fr.thesmyler.terramap.TerramapClientContext; -import fr.thesmyler.terramap.TerramapMod; import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.gui.screens.config.TerramapConfigScreen; import fr.thesmyler.terramap.gui.widgets.CircularCompassWidget; @@ -57,8 +56,8 @@ import fr.thesmyler.terramap.gui.widgets.markers.markers.Marker; import fr.thesmyler.terramap.gui.widgets.markers.markers.entities.MainPlayerMarker; import fr.thesmyler.terramap.input.KeyBindings; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; +import net.smyler.terramap.tilesets.raster.RasterTileSet; +import net.smyler.terramap.tilesets.raster.RasterTileSetProvider; import net.buildtheearth.terraplusplus.projection.GeographicProjection; import net.buildtheearth.terraplusplus.projection.OutOfProjectionBoundsException; import net.minecraft.client.Minecraft; @@ -421,14 +420,14 @@ public void onUpdate(float mouseX, float mouseY, WidgetContainer parent) { debugBuilder.append(String.format(locale, "\nSledgehammer: %s", srv.getSledgehammerVersion())); debugBuilder.append(String.format(locale, "\nProjection: %s", generationSettings != null ? generationSettings.projection() : null)); this.map.getRasterBackgroundLayer().ifPresent(layer -> { - RasterTiledMap backgroundStyle = layer.getTiledMap(); + RasterTileSet backgroundStyle = layer.getTiledMap(); debugBuilder.append(String.format(locale, "\nMap id: %s", backgroundStyle.getId())); debugBuilder.append(String.format(locale, "\nMap provider: %sv%s", backgroundStyle.getProvider(), backgroundStyle.getProviderVersion())); - if (backgroundStyle instanceof CachingRasterTiledMap) { - CachingRasterTiledMap cachingMap = (CachingRasterTiledMap) backgroundStyle; - debugBuilder.append(String.format(locale, "\nLoaded tiles: %d/%d/%d", cachingMap.getBaseLoad(), cachingMap.getLoadedCount(), CachingRasterTiledMap.CACHE_SIZE)); - if (cachingMap instanceof UrlTiledMap) { - UrlTiledMap urlMap = (UrlTiledMap) cachingMap; + if (backgroundStyle instanceof CachingRasterTileSet) { + CachingRasterTileSet cachingMap = (CachingRasterTileSet) backgroundStyle; + debugBuilder.append(String.format(locale, "\nLoaded tiles: %d/%d/%d", cachingMap.getBaseLoad(), cachingMap.getLoadedCount(), CachingRasterTileSet.CACHE_SIZE)); + if (cachingMap instanceof UrlRasterTileSet) { + UrlRasterTileSet urlMap = (UrlRasterTileSet) cachingMap; String[] urls = urlMap.getUrlPatterns(); int showingIndex = (int) ((System.currentTimeMillis() / 3000) % urls.length); debugBuilder.append(String.format(locale, "\nMap urls (%d) %s", urls.length, urls[showingIndex])); @@ -457,7 +456,7 @@ private void setZoomRestrictions() { double maxZoom = 25; double zoom = controller.getZoom(); if (backgroundLayer.isPresent()) { - RasterTiledMap style = backgroundLayer.get().getTiledMap(); + RasterTileSet style = backgroundLayer.get().getTiledMap(); minZoom = style.getMinZoom(); if (!TerramapConfig.CLIENT.unlockZoom) { maxZoom = style.getMaxZoom(); @@ -577,7 +576,7 @@ private class BackgroundStylePanelListContainer extends FlexibleWidgetContainer BackgroundStylePanelListContainer() { super(0, 0, 0, 0, 0); Widget lw = null; - for(TiledMapProvider provider: TiledMapProvider.values()) { + for(RasterTileSetProvider provider: RasterTileSetProvider.values()) { Throwable e = provider.getLastError(); if(e == null) continue; float x = 0; @@ -589,9 +588,9 @@ private class BackgroundStylePanelListContainer extends FlexibleWidgetContainer this.addWidget(w); lw = w; } - ArrayList maps = new ArrayList<>(TerramapClientContext.getContext().getMapStyles().values()); + ArrayList maps = new ArrayList<>(TerramapClientContext.getContext().getRasterTileSets().values()); maps.sort((m1, m2) -> Integer.compare(m2.getDisplayPriority(), m1.getDisplayPriority())); - for(RasterTiledMap map: maps) { + for(RasterTileSet map: maps) { MapPreview w = new MapPreview(50, map, m -> { TerramapScreen.this.map.getRasterBackgroundLayer().ifPresent(l -> { l.setTiledMap(m.previewLayer.getTiledMap()); @@ -655,10 +654,10 @@ public float getY() { private static class FailedMapLoadingNotice extends AbstractSolidWidget { - private final TiledMapProvider provider; + private final RasterTileSetProvider provider; private final Throwable exception; - public FailedMapLoadingNotice(float x, float y, int z, float width, float height, TiledMapProvider provider, Throwable e) { + public FailedMapLoadingNotice(float x, float y, int z, float width, float height, RasterTileSetProvider provider, Throwable e) { super(x, y, z, width, height); this.provider = provider; this.exception = e; @@ -727,7 +726,7 @@ private class MapPreview extends MapWidget { final Consumer onClick; final OnlineRasterMapLayer previewLayer; - public MapPreview(int z, RasterTiledMap map, Consumer onClick) { + public MapPreview(int z, RasterTileSet map, Consumer onClick) { super(z, MapContext.PREVIEW, TerramapScreen.this.map.getTileScaling()); this.setInteractive(false); this.setRightClickMenuEnabled(false); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java index e6e565c6..1bf770da 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java @@ -32,7 +32,7 @@ import fr.thesmyler.terramap.gui.widgets.markers.controllers.PlayerDirectionsVisibilityController; import fr.thesmyler.terramap.gui.widgets.markers.controllers.PlayerNameVisibilityController; import fr.thesmyler.terramap.gui.widgets.markers.markers.Marker; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; +import net.smyler.terramap.tilesets.raster.RasterTileSet; import net.buildtheearth.terraplusplus.projection.GeographicProjection; import net.buildtheearth.terraplusplus.projection.OutOfProjectionBoundsException; import net.minecraft.client.Minecraft; @@ -51,8 +51,8 @@ public class HudConfigScreen extends Screen { private final WindowedContainer compassWindow = new WindowedContainer(16, this.compassScreen, ""); private final OptionSliderWidget tileScalingSlider = new OptionSliderWidget<>(10, TileScalingOption.values()); private final IntegerSliderWidget zoomSlider = new IntegerSliderWidget(11, 0, 20, 10); - private final OptionSliderWidget styleSlider; - private MapStyleSliderEntry[] mapStyles = new MapStyleSliderEntry[0]; + private final OptionSliderWidget styleSlider; + private RasterTileSetSliderEntry[] tileSets = new RasterTileSetSliderEntry[0]; private final ToggleButtonWidget otherPlayersButton = new ToggleButtonWidget(10, false); private final ToggleButtonWidget entitiesButton = new ToggleButtonWidget(10, false); private final ToggleButtonWidget minimapButton = new ToggleButtonWidget(10, false); @@ -67,13 +67,13 @@ public class HudConfigScreen extends Screen { public HudConfigScreen() { super(BackgroundOption.NONE); final MapController controller = this.minimap.getController(); - List maps = new ArrayList<>(); - TerramapClientContext.getContext().getMapStyles().values().stream() - .sorted(((Comparator) RasterTiledMap::compareTo).reversed()) - .filter(RasterTiledMap::isAllowedOnMinimap) - .forEachOrdered(m -> maps.add(new MapStyleSliderEntry(m))); - this.mapStyles = maps.toArray(this.mapStyles); - this.styleSlider = new OptionSliderWidget<>(0, 0, 15, 10, this.mapStyles); + List maps = new ArrayList<>(); + TerramapClientContext.getContext().getRasterTileSets().values().stream() + .sorted(((Comparator) RasterTileSet::compareTo).reversed()) + .filter(RasterTileSet::isAllowedOnMinimap) + .forEachOrdered(m -> maps.add(new RasterTileSetSliderEntry(m))); + this.tileSets = maps.toArray(this.tileSets); + this.styleSlider = new OptionSliderWidget<>(0, 0, 15, 10, this.tileSets); this.minimap.setInteractive(false); this.minimap.setCopyrightVisibility(false); this.minimap.setRightClickMenuEnabled(false); @@ -317,7 +317,7 @@ private void reset() { MapController minimapController = this.minimap.getController(); minimapController.setTracksRotation(TerramapConfig.CLIENT.minimap.playerRotation); if(!TerramapConfig.CLIENT.minimap.playerRotation) minimapController.setRotation(0f, false); - for(MapStyleSliderEntry map: this.mapStyles) if(map.map.getId().equals(TerramapConfig.CLIENT.minimap.style)) { + for(RasterTileSetSliderEntry map: this.tileSets) if(map.map.getId().equals(TerramapConfig.CLIENT.minimap.style)) { this.styleSlider.setCurrentOption(map); break; } @@ -380,9 +380,9 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous } - private static class MapStyleSliderEntry { - private final RasterTiledMap map; - private MapStyleSliderEntry(RasterTiledMap map) { + private static class RasterTileSetSliderEntry { + private final RasterTileSet map; + private RasterTileSetSliderEntry(RasterTileSet map) { this.map = map; } @Override diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java index 07e5968e..6b67dcfe 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/TerramapConfigScreen.java @@ -4,11 +4,12 @@ import java.io.IOException; import java.util.Set; -import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; +import net.smyler.terramap.tilesets.raster.CachingRasterTileSet; import net.smyler.smylib.game.GameClient; import net.smyler.smylib.game.Key; import net.smyler.smylib.game.Translator; import net.smyler.terramap.Terramap; +import net.smyler.terramap.tilesets.raster.RasterTileSetManager; import org.jetbrains.annotations.Nullable; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; @@ -26,7 +27,6 @@ import net.smyler.smylib.gui.widgets.text.TextWidget; import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.TerramapConfig; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import static net.smyler.smylib.SmyLib.getGameClient; import static net.smyler.smylib.text.ImmutableText.ofPlainText; @@ -48,7 +48,7 @@ public class TerramapConfigScreen extends Screen { private final IntegerSliderWidget doubleClickDelaySlider = new IntegerSliderWidget(10, TerramapConfig.CLIENT.DOUBLE_CLICK_DELAY_MIN, TerramapConfig.CLIENT.DOUBLE_CLICK_DELAY_MAX, TerramapConfig.CLIENT.DOUBLE_CLICK_DELAY_DEFAULT); private final IntegerSliderWidget maxLoadedTilesSlider = new IntegerSliderWidget(10, 0, 1024, 512); private final IntegerSliderWidget lowZoomLevelSlider = new IntegerSliderWidget(10, TerramapConfig.CLIENT.LOW_ZOOM_LEVEL_MIN, TerramapConfig.CLIENT.LOW_ZOOM_LEVEL_MAX, TerramapConfig.CLIENT.LOW_ZOOM_LEVEL_DEFAULT); - private final ToggleButtonWidget debugMapStylesToggle = new ToggleButtonWidget(10, false); + private final ToggleButtonWidget debugRasterTileSetsToggle = new ToggleButtonWidget(10, false); private final TextFieldWidget tpCommandField; private final TextWidget pageText; @@ -94,11 +94,11 @@ public void init() { this.addWidget(this.next.setX(save.getX() + save.getWidth() + 5).setY(save.getY() + 2)); this.addWidget(this.previous.setX(cancel.getX() - 20).setY(this.next.getY())); FlexibleWidgetContainer mapConfigScreen = new FlexibleWidgetContainer(20, 20, 1, width - 40, height - 75); - FlexibleWidgetContainer mapStylesConfigScreen = new FlexibleWidgetContainer(20, 20, 1, width - 40, height - 75); + FlexibleWidgetContainer tileSetsConfigScreen = new FlexibleWidgetContainer(20, 20, 1, width - 40, height - 75); FlexibleWidgetContainer otherConfigScreen = new FlexibleWidgetContainer(20, 20, 1, width - 40, height - 75); this.pages = new FlexibleWidgetContainer[] { mapConfigScreen, - mapStylesConfigScreen, + tileSetsConfigScreen, otherConfigScreen }; this.titles = new String[] { @@ -153,34 +153,35 @@ public void init() { hudButton.setTooltip(translator.format("terramap.configmenu.configureminimap.tooltip")); mapConfigScreen.addWidget(hudButton); - // Map styles - TextWidget debugMapStylesText = new TextWidget(10, ofTranslation("terramap.configmenu.debugmapstyles"), game.defaultFont()); - mapStylesConfigScreen.addWidget(debugMapStylesText.setAnchorX((mapStylesConfigScreen.getWidth() - debugMapStylesToggle.getWidth() - debugMapStylesText.getWidth() - 3) / 2).setAnchorY(mapStylesConfigScreen.getHeight() / 4 - 30)); - debugMapStylesToggle.setTooltip(translator.format("terramap.configmenu.debugmapstyles.tooltip")); - mapStylesConfigScreen.addWidget(debugMapStylesToggle.setX(debugMapStylesText.getX() + debugMapStylesText.getWidth() + 3).setY(debugMapStylesText.getY() - 4)); - Set baseIDs = MapStylesLibrary.getBaseMaps().keySet(); - Set userIDs = MapStylesLibrary.getUserMaps().keySet(); - Set serverIDs = TerramapClientContext.getContext().getServerMapStyles().keySet(); - Set proxyIDs = TerramapClientContext.getContext().getProxyMapStyles().keySet(); - Set resolved = TerramapClientContext.getContext().getMapStyles().keySet(); - TextWidget baseText = new TextWidget(mapStylesConfigScreen.getWidth() / 2, 40, 10, ofTranslation("terramap.configmenu.mapstyles.base", baseIDs.size(), String.join(", ", baseIDs)), TextAlignment.CENTER, getGameClient().defaultFont()); - TextWidget proxyText = new TextWidget(mapStylesConfigScreen.getWidth() / 2, 57, 10, ofTranslation("terramap.configmenu.mapstyles.proxy", proxyIDs.size(), String.join(", ", proxyIDs)), TextAlignment.CENTER, getGameClient().defaultFont()); - TextWidget serverText = new TextWidget(mapStylesConfigScreen.getWidth() / 2, 74, 10, ofTranslation("terramap.configmenu.mapstyles.server", serverIDs.size(), String.join(", ", serverIDs)), TextAlignment.CENTER, getGameClient().defaultFont()); - TextWidget userText = new TextWidget( mapStylesConfigScreen.getWidth() / 2, 91, 10, ofTranslation("terramap.configmenu.mapstyles.custom", userIDs.size(), String.join(", ", userIDs)),TextAlignment.CENTER, getGameClient().defaultFont()); - TextWidget effectiveText = new TextWidget(mapStylesConfigScreen.getWidth() / 2, 108, 10, ofTranslation("terramap.configmenu.mapstyles.effective", resolved.size(), String.join(", ", resolved)), TextAlignment.CENTER, getGameClient().defaultFont()); - mapStylesConfigScreen.addWidget(baseText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(debugMapStylesToggle.getY() + debugMapStylesToggle.getHeight() + 10)); - mapStylesConfigScreen.addWidget(proxyText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(baseText.getY() + baseText.getHeight() + inter)); - mapStylesConfigScreen.addWidget(serverText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(proxyText.getY() + proxyText.getHeight() + inter)); - mapStylesConfigScreen.addWidget(userText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(serverText.getY() + serverText.getHeight() + inter)); - mapStylesConfigScreen.addWidget(effectiveText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(userText.getY() + userText.getHeight() + inter)); - TextButtonWidget reloadMapStylesButton = new TextButtonWidget(mapStylesConfigScreen.getWidth() / 2 - 153, (effectiveText.getY() + effectiveText.getHeight() + mapStylesConfigScreen.getHeight()) / 2 - 10, 10, 150, translator.format("terramap.configmenu.mapstyles.reload"), () -> { - MapStylesLibrary.reload(); + // Tile sets + RasterTileSetManager tileSetManager = Terramap.instance().rasterTileSetManager(); + TextWidget debugRasterTileSetsText = new TextWidget(10, ofTranslation("terramap.configmenu.debugmapstyles"), game.defaultFont()); + tileSetsConfigScreen.addWidget(debugRasterTileSetsText.setAnchorX((tileSetsConfigScreen.getWidth() - debugRasterTileSetsToggle.getWidth() - debugRasterTileSetsText.getWidth() - 3) / 2).setAnchorY(tileSetsConfigScreen.getHeight() / 4 - 30)); + debugRasterTileSetsToggle.setTooltip(translator.format("terramap.configmenu.debugmapstyles.tooltip")); + tileSetsConfigScreen.addWidget(debugRasterTileSetsToggle.setX(debugRasterTileSetsText.getX() + debugRasterTileSetsText.getWidth() + 3).setY(debugRasterTileSetsText.getY() - 4)); + Set baseIDs = tileSetManager.getBaseMaps().keySet(); + Set userIDs = tileSetManager.getUserMaps().keySet(); + Set serverIDs = TerramapClientContext.getContext().getServerRasterTileSets().keySet(); + Set proxyIDs = TerramapClientContext.getContext().getProxyRasterTileSets().keySet(); + Set resolved = TerramapClientContext.getContext().getRasterTileSets().keySet(); + TextWidget baseText = new TextWidget(tileSetsConfigScreen.getWidth() / 2, 40, 10, ofTranslation("terramap.configmenu.mapstyles.base", baseIDs.size(), String.join(", ", baseIDs)), TextAlignment.CENTER, getGameClient().defaultFont()); + TextWidget proxyText = new TextWidget(tileSetsConfigScreen.getWidth() / 2, 57, 10, ofTranslation("terramap.configmenu.mapstyles.proxy", proxyIDs.size(), String.join(", ", proxyIDs)), TextAlignment.CENTER, getGameClient().defaultFont()); + TextWidget serverText = new TextWidget(tileSetsConfigScreen.getWidth() / 2, 74, 10, ofTranslation("terramap.configmenu.mapstyles.server", serverIDs.size(), String.join(", ", serverIDs)), TextAlignment.CENTER, getGameClient().defaultFont()); + TextWidget userText = new TextWidget( tileSetsConfigScreen.getWidth() / 2, 91, 10, ofTranslation("terramap.configmenu.mapstyles.custom", userIDs.size(), String.join(", ", userIDs)),TextAlignment.CENTER, getGameClient().defaultFont()); + TextWidget effectiveText = new TextWidget(tileSetsConfigScreen.getWidth() / 2, 108, 10, ofTranslation("terramap.configmenu.mapstyles.effective", resolved.size(), String.join(", ", resolved)), TextAlignment.CENTER, getGameClient().defaultFont()); + tileSetsConfigScreen.addWidget(baseText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(debugRasterTileSetsToggle.getY() + debugRasterTileSetsToggle.getHeight() + 10)); + tileSetsConfigScreen.addWidget(proxyText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(baseText.getY() + baseText.getHeight() + inter)); + tileSetsConfigScreen.addWidget(serverText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(proxyText.getY() + proxyText.getHeight() + inter)); + tileSetsConfigScreen.addWidget(userText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(serverText.getY() + serverText.getHeight() + inter)); + tileSetsConfigScreen.addWidget(effectiveText.setMaxWidth(mapConfigScreen.getWidth()).setAnchorY(userText.getY() + userText.getHeight() + inter)); + TextButtonWidget reloadRasterTileSetsButton = new TextButtonWidget(tileSetsConfigScreen.getWidth() / 2 - 153, (effectiveText.getY() + effectiveText.getHeight() + tileSetsConfigScreen.getHeight()) / 2 - 10, 10, 150, translator.format("terramap.configmenu.mapstyles.reload"), () -> { + tileSetManager.reload(TerramapConfig.enableDebugMaps); TerramapConfigScreen.this.init(); }); - mapStylesConfigScreen.addWidget(reloadMapStylesButton); - mapStylesConfigScreen.addWidget(new TextButtonWidget(reloadMapStylesButton.getX() + reloadMapStylesButton.getWidth() + 3, reloadMapStylesButton.getY(), 10, 150, translator.format("terramap.configmenu.mapstyles.open"), () -> { + tileSetsConfigScreen.addWidget(reloadRasterTileSetsButton); + tileSetsConfigScreen.addWidget(new TextButtonWidget(reloadRasterTileSetsButton.getX() + reloadRasterTileSetsButton.getWidth() + 3, reloadRasterTileSetsButton.getY(), 10, 150, translator.format("terramap.configmenu.mapstyles.open"), () -> { try { - Desktop.getDesktop().open(MapStylesLibrary.getFile()); + Desktop.getDesktop().open(tileSetManager.getFile()); } catch (IOException e) { Terramap.instance().logger().error("Failed to open map style config file: "); Terramap.instance().logger().catching(e); @@ -231,7 +232,7 @@ private void saveAndClose() { TerramapConfig.CLIENT.doubleClickDelay = (int) this.doubleClickDelaySlider.getValue(); TerramapConfig.CLIENT.lowZoomLevel = (int) this.lowZoomLevelSlider.getValue(); TerramapConfig.tpllcmd = this.tpCommandField.getText(); - TerramapConfig.enableDebugMaps = this.debugMapStylesToggle.getState(); + TerramapConfig.enableDebugMaps = this.debugRasterTileSetsToggle.getState(); TerramapConfig.sync(); this.close(); } @@ -246,11 +247,11 @@ private void reset() { this.saveUIStateToggle.setState(TerramapConfig.CLIENT.saveUiState); this.showChatOnMapToggle.setState(TerramapConfig.CLIENT.chatOnMap); this.doubleClickDelaySlider.setValue(TerramapConfig.CLIENT.doubleClickDelay); - this.maxLoadedTilesSlider.setValue(CachingRasterTiledMap.CACHE_SIZE); + this.maxLoadedTilesSlider.setValue(CachingRasterTileSet.CACHE_SIZE); this.maxLoadedTilesSlider.setEnabled(false); - this.lowZoomLevelSlider.setValue(CachingRasterTiledMap.LOW_ZOOM); + this.lowZoomLevelSlider.setValue(CachingRasterTileSet.LOW_ZOOM); this.tpCommandField.setText(TerramapConfig.tpllcmd); - this.debugMapStylesToggle.setState(TerramapConfig.enableDebugMaps); + this.debugRasterTileSetsToggle.setState(TerramapConfig.enableDebugMaps); } @Override diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MinimapWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MinimapWidget.java index 153bf780..d001d429 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MinimapWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MinimapWidget.java @@ -13,7 +13,7 @@ import fr.thesmyler.terramap.gui.widgets.markers.controllers.*; import fr.thesmyler.terramap.gui.widgets.markers.markers.Marker; import fr.thesmyler.terramap.maps.SavedMapState; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; +import net.smyler.terramap.tilesets.raster.RasterTileSet; import net.buildtheearth.terraplusplus.projection.GeographicProjection; import net.minecraft.client.Minecraft; @@ -91,7 +91,7 @@ private void loadState() { double minZoom = 0d; double maxZoom = 25d; if (background.isPresent()) { - RasterTiledMap style = TerramapClientContext.getContext().getMapStyles().get(TerramapConfig.CLIENT.minimap.style); + RasterTileSet style = TerramapClientContext.getContext().getRasterTileSets().get(TerramapConfig.CLIENT.minimap.style); OnlineRasterMapLayer layer = background.get(); if (style != null) { layer.setTiledMap(style); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/GenerationPreviewLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/GenerationPreviewLayer.java index 08c78b88..1ec39269 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/GenerationPreviewLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/GenerationPreviewLayer.java @@ -1,14 +1,14 @@ package fr.thesmyler.terramap.gui.widgets.map.layer; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; -import fr.thesmyler.terramap.maps.raster.imp.TerrainPreviewMap; +import net.smyler.terramap.tilesets.raster.RasterTileSet; +import net.smyler.terramap.tilesets.raster.TerrainPreviewTileSet; import static net.smyler.smylib.SmyLib.getGameClient; public class GenerationPreviewLayer extends RasterMapLayer { - private final TerrainPreviewMap map = new TerrainPreviewMap(); + private final TerrainPreviewTileSet map = new TerrainPreviewTileSet(); @Override public String name() { @@ -31,7 +31,7 @@ public FlexibleWidgetContainer createConfigurationContainer() { } @Override - public RasterTiledMap getTiledMap() { + public RasterTileSet getTiledMap() { return this.map; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/OnlineRasterMapLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/OnlineRasterMapLayer.java index 56b4daf9..634e2580 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/OnlineRasterMapLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/OnlineRasterMapLayer.java @@ -11,8 +11,8 @@ import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.gui.widgets.map.MapController; import fr.thesmyler.terramap.gui.widgets.map.MapWidget; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; -import fr.thesmyler.terramap.maps.raster.imp.ColorTiledMap; +import net.smyler.terramap.tilesets.raster.RasterTileSet; +import net.smyler.terramap.tilesets.raster.ColorTileSet; import net.smyler.terramap.util.CopyrightHolder; import net.smyler.smylib.game.GameClient; import net.smyler.smylib.gui.DrawContext; @@ -35,13 +35,13 @@ public class OnlineRasterMapLayer extends RasterMapLayer implements CopyrightHolder { - protected RasterTiledMap tiledMap = new ColorTiledMap(Color.WHITE, "Empty map"); + protected RasterTileSet tiledMap = new ColorTileSet(Color.WHITE, "Empty map"); - public RasterTiledMap getTiledMap() { + public RasterTileSet getTiledMap() { return this.tiledMap; } - public void setTiledMap(RasterTiledMap map) { + public void setTiledMap(RasterTileSet map) { this.tiledMap = map; this.getMap().updateCopyright(); } @@ -66,7 +66,7 @@ public void loadSettings(JsonObject json) { try { JsonPrimitive primitiveValue = json.getAsJsonPrimitive("style"); String styleId = primitiveValue.getAsString(); - RasterTiledMap tiledMap = TerramapClientContext.getContext().getMapStyles().get(styleId); + RasterTileSet tiledMap = TerramapClientContext.getContext().getRasterTileSets().get(styleId); if (tiledMap != null) { this.setTiledMap(tiledMap); } @@ -105,7 +105,7 @@ public FlexibleWidgetContainer createConfigurationContainer() { class StyleEntry extends WidgetContainer { - final RasterTiledMap style; + final RasterTileSet style; final TextWidget nameText; final TextWidget infoText; @@ -118,7 +118,7 @@ class StyleEntry extends WidgetContainer { final Animation backgroundColorAnimation = new Animation(200); - public StyleEntry(RasterTiledMap style) { + public StyleEntry(RasterTileSet style) { super(0); this.style = style; float y = margin; @@ -232,9 +232,9 @@ public float getHeight() { } FlexibleWidgetContainer container = new FlexibleWidgetContainer(0f, 0f, 0, width, 200f); - List styles = TerramapClientContext.getContext().getMapStyles().values() + List styles = TerramapClientContext.getContext().getRasterTileSets().values() .stream() - .sorted(comparing(RasterTiledMap::getDisplayPriority).reversed()) + .sorted(comparing(RasterTileSet::getDisplayPriority).reversed()) .map(s -> new StyleEntry(s)) .collect(toList()); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java index 30119a44..b2ec3528 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java @@ -8,8 +8,8 @@ import net.smyler.smylib.gui.containers.WidgetContainer; import fr.thesmyler.terramap.gui.widgets.map.MapLayer; import fr.thesmyler.terramap.gui.widgets.map.MapWidget; -import fr.thesmyler.terramap.maps.raster.RasterTile; -import fr.thesmyler.terramap.maps.raster.RasterTiledMap; +import net.smyler.terramap.tilesets.raster.RasterTile; +import net.smyler.terramap.tilesets.raster.RasterTileSet; import net.smyler.smylib.Color; import net.smyler.smylib.gui.DrawContext; import net.smyler.smylib.gui.Font; @@ -42,7 +42,7 @@ abstract public class RasterMapLayer extends MapLayer { private Vec2dReadOnly renderingSpaceDimensions; private Vec2dReadOnly halfRenderingSpaceDimensions; - public abstract RasterTiledMap getTiledMap(); + public abstract RasterTileSet getTiledMap(); @Override protected void initialize() { @@ -55,7 +55,7 @@ protected void initialize() { @Override public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { - final RasterTiledMap tiledMap = this.getTiledMap(); + final RasterTileSet tiledMap = this.getTiledMap(); final Identifier defaultTexture = tiledMap.getDefaultTileTexture(); Font smallFont = getGameClient().smallestFont(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java deleted file mode 100644 index 8534e720..00000000 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/ColorTile.java +++ /dev/null @@ -1,41 +0,0 @@ -package fr.thesmyler.terramap.maps.raster.imp; - -import fr.thesmyler.terramap.maps.raster.RasterTile; -import net.smyler.smylib.Identifier; -import net.smyler.terramap.util.geo.TilePosImmutable; - -public class ColorTile implements RasterTile { - - private final TilePosImmutable position; - private final Identifier texture; - - public ColorTile(TilePosImmutable position, Identifier texture) { - this.position = position; - this.texture = texture; - } - - @Override - public boolean isTextureAvailable() { - return this.texture != null; - } - - @Override - public Identifier getTexture() { - return this.texture; - } - - @Override - public void cancelTextureLoading() { - } - - @Override - public void unloadTexture() { - // Nop, we don't want to do that ! - } - - @Override - public TilePosImmutable getPosition() { - return this.position; - } - -} diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java deleted file mode 100644 index 6c66d582..00000000 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewMap.java +++ /dev/null @@ -1,77 +0,0 @@ -package fr.thesmyler.terramap.maps.raster.imp; - -import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; -import net.smyler.smylib.Identifier; -import net.smyler.terramap.util.geo.TilePosImmutable; - -import static net.smyler.smylib.SmyLib.getGameClient; - -public class TerrainPreviewMap extends CachingRasterTiledMap { - - public static final int BASE_ZOOM_LEVEL = 16; - - public TerrainPreviewMap() { - this.setUsesLowZoom(false); // Loading tiles at low zoom levels takes forever here - } - @Override - protected TerrainPreviewTile createNewTile(TilePosImmutable position) { - return new TerrainPreviewTile(position); - } - - @Override - public String getId() { - return "terrain_preview_debug"; - } - - @Override - public String getLocalizedName(String localeKey) { - return getGameClient().translator().format("terramap.maps.debug.terrain"); // This is always local - } - - @Override - public String getComment() { - return "Terra++ terrain preview debug map"; - } - - @Override - public TiledMapProvider getProvider() { - return TiledMapProvider.INTERNAL; - } - - @Override - public long getProviderVersion() { - return 0; - } - - @Override - public int getDisplayPriority() { - return 0; - } - - @Override - public boolean isAllowedOnMinimap() { - return true; - } - - @Override - public boolean isDebug() { - return true; - } - - @Override - public Identifier getDefaultTileTexture() { - return null; - } - - @Override - public int getMinZoom() { - return BASE_ZOOM_LEVEL; - } - - @Override - public int getMaxZoom() { - return 20; - } - -} diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java deleted file mode 100644 index 85f3f0d9..00000000 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/TerrainPreviewTile.java +++ /dev/null @@ -1,92 +0,0 @@ -package fr.thesmyler.terramap.maps.raster.imp; - -import java.awt.image.BufferedImage; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import fr.thesmyler.terramap.TerramapClientContext; -import fr.thesmyler.terramap.maps.raster.RasterTile; -import net.smyler.smylib.Identifier; -import net.smyler.terramap.util.geo.TilePosImmutable; -import net.buildtheearth.terraplusplus.generator.TerrainPreview; - -import static net.smyler.smylib.SmyLib.getGameClient; - -public class TerrainPreviewTile implements RasterTile { - - private final TilePosImmutable position; - private Identifier texture; - private CompletableFuture textureTask; - - public TerrainPreviewTile(TilePosImmutable position) { - this.position = position; - } - - @Override - public boolean isTextureAvailable() { - try { - this.tryLoadingTexture(); - } catch (Throwable e) { - return false; - } - return this.texture != null; - } - - @Override - public Identifier getTexture() throws Throwable { - - if(this.getPosition().getZoom() < TerrainPreviewMap.BASE_ZOOM_LEVEL) - throw new IllegalArgumentException("Trying to request a terrain preview with a zoom that's too low (" + this.position.getZoom() + ")"); - - if(this.getPosition().getZoom() != TerrainPreviewMap.BASE_ZOOM_LEVEL) return null; - - if(this.texture == null) { - if(this.textureTask == null) { - TerrainPreview preview = TerramapClientContext.getContext().getTerrainPreview(); - if(preview != null) { - this.textureTask = preview.tile(this.position.getX(), this.position.getY(), TerrainPreviewMap.BASE_ZOOM_LEVEL - this.position.getZoom()); - } - } else this.tryLoadingTexture(); - } - return this.texture; - } - - @Override - public void cancelTextureLoading() { - } - - @Override - public void unloadTexture() { - this.cancelTextureLoading(); - if(this.texture != null) { - getGameClient().guiDrawContext().unloadDynamicTexture(this.texture); - this.texture = null; - } - } - - @Override - public TilePosImmutable getPosition() { - return this.position; - } - - private void tryLoadingTexture() throws Throwable { - if(this.textureTask != null && this.textureTask.isDone()){ - if(this.textureTask.isCompletedExceptionally()) { - if(!this.textureTask.isCancelled()) { - try { - this.textureTask.get(); // That will throw an exception - } catch(ExecutionException e) { - this.textureTask = null; - throw e.getCause(); - } - } - this.textureTask = null; - return; - } - BufferedImage image = this.textureTask.get(); - this.texture = getGameClient().guiDrawContext().loadDynamicTexture(image); - this.textureTask = null; - } - } - -} diff --git a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java b/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java deleted file mode 100644 index 9c553b77..00000000 --- a/forge/src/main/java/fr/thesmyler/terramap/maps/raster/imp/UrlRasterTile.java +++ /dev/null @@ -1,129 +0,0 @@ -package fr.thesmyler.terramap.maps.raster.imp; - -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import javax.imageio.ImageIO; - -import fr.thesmyler.terramap.maps.raster.RasterTile; -import net.smyler.smylib.Identifier; -import net.smyler.terramap.Terramap; -import net.smyler.terramap.util.geo.TilePosImmutable; - - -import static net.smyler.smylib.SmyLib.getGameClient; - -/** - * @author SmylerMC - * - */ -public class UrlRasterTile implements RasterTile { - - private final TilePosImmutable pos; - private final String url; - private Identifier texture = null; - private CompletableFuture textureTask; - - - public UrlRasterTile(String urlPattern, TilePosImmutable pos) { - this.pos = pos; - this.url = urlPattern - .replace("{x}", String.valueOf(this.getPosition().getX())) - .replace("{y}", String.valueOf(this.getPosition().getY())) - .replace("{z}", String.valueOf(this.getPosition().getZoom())); - } - - public UrlRasterTile(String urlPattern, int zoom, int x, int y) { - this(urlPattern, new TilePosImmutable(zoom, x , y)); - } - - public String getURL() { - return this.url; - } - - @Override - public boolean isTextureAvailable() { - if(texture != null) return true; // Don't try loading the texture if it has already been loaded - try { - this.tryLoadingTexture(); - } catch (Throwable e) { - return false; - } - return this.texture != null; - } - - @Override - public Identifier getTexture() throws Throwable { - if(this.texture == null) { - if(this.textureTask == null) { - this.textureTask = Terramap.instance().http().get(this.getURL()); - } else this.tryLoadingTexture(); - } - return this.texture; - } - - private void tryLoadingTexture() throws Throwable { - //TODO Do that fully async, DynamicTexture::new is expensive - if(this.textureTask != null && this.textureTask.isDone()){ - if(this.textureTask.isCompletedExceptionally()) { - if(this.textureTask.isCancelled()) { - this.textureTask = null; - } else { - try { - this.textureTask.get(); // That will throw an exception - } catch(ExecutionException e) { - throw e.getCause(); - } - } - return; - } - byte[] buf = this.textureTask.get(); - if(buf == null) throw new IOException("404 response"); - try (ByteArrayInputStream is = new ByteArrayInputStream(buf)) { - BufferedImage image = ImageIO.read(is); - if(image == null) throw new IOException("Failed to read image! url: " + this.getURL()); - this.texture = getGameClient().guiDrawContext().loadDynamicTexture(image); - } - } - } - - @Override - public void cancelTextureLoading() { - if(this.textureTask != null) { - this.textureTask.cancel(true); - this.textureTask = null; - } - } - - @Override - public void unloadTexture() { - this.cancelTextureLoading(); - if(this.texture != null) { - getGameClient().guiDrawContext().unloadDynamicTexture(this.texture); - this.texture = null; - } - } - - @Override - public boolean equals(Object obj) { - if(obj == this) return true; - if(obj == null) return false; - if(!(obj instanceof UrlRasterTile)) return false; - UrlRasterTile other = (UrlRasterTile) obj; - return other.url.equals(this.url); - } - - @Override - public TilePosImmutable getPosition() { - return this.pos; - } - - @Override - public int hashCode() { - return this.url.hashCode(); - } - -} diff --git a/forge/src/main/java/fr/thesmyler/terramap/network/RemoteSynchronizer.java b/forge/src/main/java/fr/thesmyler/terramap/network/RemoteSynchronizer.java index 7fd6187f..a65487d2 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/network/RemoteSynchronizer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/network/RemoteSynchronizer.java @@ -9,8 +9,7 @@ import fr.thesmyler.terramap.TerramapVersion.TerraDependency; import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.saving.server.TerramapServerPreferences; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; -import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap; +import net.smyler.terramap.tilesets.raster.UrlRasterTileSet; import fr.thesmyler.terramap.network.playersync.PlayerSyncStatus; import fr.thesmyler.terramap.network.playersync.SP2CPlayerSyncPacket; import fr.thesmyler.terramap.network.playersync.SP2CRegistrationExpiresPacket; @@ -109,14 +108,14 @@ public static void sendTpCommandToClient(EntityPlayerMP player) { TerramapNetworkManager.CHANNEL_TERRAMAP.sendTo(new S2CTpCommandPacket(TerramapConfig.tpllcmd), player); } - public static void sendMapStylesToClient(EntityPlayerMP player) { + public static void sendRasterTileSetsToClient(EntityPlayerMP player) { TerramapVersion clientVersion = TerramapVersion.getClientVersion(player); if(clientVersion == null) return; boolean compat = clientVersion.getTerraDependency() != TerraDependency.TERRAPLUSPLUS; if(TerramapConfig.SERVER.sendCusomMapsToClient) { - for(UrlTiledMap map: MapStylesLibrary.getUserMaps().values()) { + for(UrlRasterTileSet map: Terramap.instance().rasterTileSetManager().getUserMaps().values()) { if(!TerramapConfig.enableDebugMaps && map.isDebug()) continue; - SP2CMapStylePacket pkt = new SP2CMapStylePacket(map); + SP2CRasterTileSetPacket pkt = new SP2CRasterTileSetPacket(map); if(compat) pkt.setBackwardCompat(); TerramapNetworkManager.CHANNEL_TERRAMAP.sendTo(pkt, player); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java b/forge/src/main/java/fr/thesmyler/terramap/network/SP2CRasterTileSetPacket.java similarity index 85% rename from forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java rename to forge/src/main/java/fr/thesmyler/terramap/network/SP2CRasterTileSetPacket.java index 6d423c55..2054abb9 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/network/SP2CMapStylePacket.java +++ b/forge/src/main/java/fr/thesmyler/terramap/network/SP2CRasterTileSetPacket.java @@ -6,12 +6,12 @@ import com.google.gson.JsonParseException; import net.smyler.smylib.text.Text; import net.smyler.terramap.Terramap; +import net.smyler.terramap.tilesets.raster.UrlRasterTileSet; import org.apache.logging.log4j.util.Strings; import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.TerramapConfig; -import fr.thesmyler.terramap.maps.raster.TiledMapProvider; -import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap; +import net.smyler.terramap.tilesets.raster.RasterTileSetProvider; import net.smyler.terramap.util.geo.WebMercatorBounds; import io.netty.buffer.ByteBuf; import net.minecraft.client.Minecraft; @@ -20,7 +20,7 @@ import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; -public class SP2CMapStylePacket implements IMessage { +public class SP2CRasterTileSetPacket implements IMessage { private String id; private long providerVersion; @@ -37,7 +37,7 @@ public class SP2CMapStylePacket implements IMessage { private boolean backwardCompat = false; private Map bounds; - public SP2CMapStylePacket(UrlTiledMap map) { + public SP2CRasterTileSetPacket(UrlRasterTileSet map) { this.id = map.getId(); this.providerVersion = map.getProviderVersion(); this.urlPatterns = map.getUrlPatterns(); @@ -57,7 +57,7 @@ public SP2CMapStylePacket(UrlTiledMap map) { } } - public SP2CMapStylePacket() {} + public SP2CRasterTileSetPacket() {} @Override public void fromBytes(ByteBuf buf) { @@ -157,8 +157,8 @@ public boolean getBackwardCompat() { return this.backwardCompat; } - public UrlTiledMap getTiledMap(TiledMapProvider provider) { - UrlTiledMap tiledMap = new UrlTiledMap( + public UrlRasterTileSet getTiledMap(RasterTileSetProvider provider) { + UrlRasterTileSet tiledMap = new UrlRasterTileSet( this.urlPatterns, this.minZoom, this.maxZoom, @@ -177,49 +177,49 @@ public UrlTiledMap getTiledMap(TiledMapProvider provider) { return tiledMap; } - public static class SP2CMapStylePacketTerramapHandler implements IMessageHandler { + public static class SP2CRasterTileSetPacketTerramapHandler implements IMessageHandler { - public SP2CMapStylePacketTerramapHandler() {} + public SP2CRasterTileSetPacketTerramapHandler() {} @Override - public IMessage onMessage(SP2CMapStylePacket message, MessageContext ctx) { + public IMessage onMessage(SP2CRasterTileSetPacket message, MessageContext ctx) { try { - UrlTiledMap map = message.getTiledMap(TiledMapProvider.SERVER); + UrlRasterTileSet map = message.getTiledMap(RasterTileSetProvider.SERVER); Terramap.instance().logger().debug("Got custom map style from server: {} / {}", map.getId(), String.join(";", map.getUrlPatterns())); if(!TerramapConfig.enableDebugMaps && map.isDebug()) { Terramap.instance().logger().debug("Ignoring debug map from server: {}", map.getId()); return null; } - Minecraft.getMinecraft().addScheduledTask(() -> TerramapClientContext.getContext().addServerMapStyle(map)); + Minecraft.getMinecraft().addScheduledTask(() -> TerramapClientContext.getContext().addServerRasterTileSet(map)); } catch(Exception e) { Terramap.instance().logger().error("Failed to unpack a map style sent by the server"); Terramap.instance().logger().catching(e); - TiledMapProvider.SERVER.setLastError(e); + RasterTileSetProvider.SERVER.setLastError(e); } return null; } } - public static class SP2CMapStylePacketSledgehammerHandler implements IMessageHandler { + public static class SP2CRasterTileSetPacketSledgehammerHandler implements IMessageHandler { - public SP2CMapStylePacketSledgehammerHandler() {} + public SP2CRasterTileSetPacketSledgehammerHandler() {} @Override - public IMessage onMessage(SP2CMapStylePacket message, MessageContext ctx) { + public IMessage onMessage(SP2CRasterTileSetPacket message, MessageContext ctx) { try { - UrlTiledMap map = message.getTiledMap(TiledMapProvider.PROXY); + UrlRasterTileSet map = message.getTiledMap(RasterTileSetProvider.PROXY); Terramap.instance().logger().debug("Got custom map style from proxy: {} / {}", map.getId(), String.join(";", map.getUrlPatterns())); if(!TerramapConfig.enableDebugMaps && map.isDebug()) { Terramap.instance().logger().debug("Ignoring debug map from proxy: {}", map.getId()); return null; } - Minecraft.getMinecraft().addScheduledTask(() -> TerramapClientContext.getContext().addProxyMapStyle(map)); + Minecraft.getMinecraft().addScheduledTask(() -> TerramapClientContext.getContext().addProxyRasterTileSet(map)); } catch(Exception e) { Terramap.instance().logger().error("Failed to unpack a map style sent by the proxy"); Terramap.instance().logger().catching(e); - TiledMapProvider.PROXY.setLastError(e); + RasterTileSetProvider.PROXY.setLastError(e); } return null; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/network/TerramapNetworkManager.java b/forge/src/main/java/fr/thesmyler/terramap/network/TerramapNetworkManager.java index 56b86282..3aaeb7e6 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/network/TerramapNetworkManager.java +++ b/forge/src/main/java/fr/thesmyler/terramap/network/TerramapNetworkManager.java @@ -3,8 +3,8 @@ import fr.thesmyler.terramap.network.P2CSledgehammerHelloPacket.P2CSledgehammerHelloPacketHandler; import fr.thesmyler.terramap.network.S2CTerramapHelloPacket.S2CTerramapHelloPacketHandler; import fr.thesmyler.terramap.network.S2CTpCommandPacket.S2CTpCommandPacketHandler; -import fr.thesmyler.terramap.network.SP2CMapStylePacket.SP2CMapStylePacketSledgehammerHandler; -import fr.thesmyler.terramap.network.SP2CMapStylePacket.SP2CMapStylePacketTerramapHandler; +import fr.thesmyler.terramap.network.SP2CRasterTileSetPacket.SP2CRasterTileSetPacketSledgehammerHandler; +import fr.thesmyler.terramap.network.SP2CRasterTileSetPacket.SP2CRasterTileSetPacketTerramapHandler; import fr.thesmyler.terramap.network.playersync.C2SPRegisterForUpdatesPacket; import fr.thesmyler.terramap.network.playersync.C2SPRegisterForUpdatesPacket.C2SRegisterForUpdatesPacketHandler; import fr.thesmyler.terramap.network.playersync.SP2CPlayerSyncPacket; @@ -33,21 +33,21 @@ public abstract class TerramapNetworkManager { public static void registerHandlers(Side side){ registerTerramapS2C(S2C_TERRAMAP_HELLO_DISCRIMINATOR, S2CTerramapHelloPacketHandler.class, S2CTerramapHelloPacket.class); registerTerramapS2C(S2C_TERRAMAP_TPCMD_DISCRIMINATOR, S2CTpCommandPacketHandler.class, S2CTpCommandPacket.class); - registerTerramapS2C(S2C_TERRAMAP_MAPSTYLE_DISCRIMINATOR, SP2CMapStylePacketTerramapHandler.class, SP2CMapStylePacket.class); + registerTerramapS2C(S2C_TERRAMAP_RASTER_TILE_SET_DISCRIMINATOR, SP2CRasterTileSetPacketTerramapHandler.class, SP2CRasterTileSetPacket.class); registerMapsyncCP2S(C2SP_MAPSYNC_REGISTER_DISCRIMINATOR, C2SRegisterForUpdatesPacketHandler.class, C2SPRegisterForUpdatesPacket.class); registerMapsyncSP2C(SP2C_MAPSYNC_PLAYERSYNC_DISCRIMINATOR, S2CPlayerSyncPacketHandler.class, SP2CPlayerSyncPacket.class); registerMapsyncSP2C(SP2C_MAPSYNC_REGISTRATION_EXPIRES_DISCRIMINATOR, S2CRegistrationExpiresPacketHandler.class, SP2CRegistrationExpiresPacket.class); registerSledgehammerP2C(P2C_SH_HELLO_DISCRIMINATOR, P2CSledgehammerHelloPacketHandler.class, P2CSledgehammerHelloPacket.class); - registerSledgehammerP2C(P2C_SH_MAPSTYLE_DISCRIMINATOR, SP2CMapStylePacketSledgehammerHandler.class, SP2CMapStylePacket.class); + registerSledgehammerP2C(P2C_SH_RASTER_TILESET_DISCRIMINATOR, SP2CRasterTileSetPacketSledgehammerHandler.class, SP2CRasterTileSetPacket.class); } // terramap:terramap private static final int S2C_TERRAMAP_HELLO_DISCRIMINATOR = 0; private static final int S2C_TERRAMAP_TPCMD_DISCRIMINATOR = 1; - private static final int S2C_TERRAMAP_MAPSTYLE_DISCRIMINATOR = 2; + private static final int S2C_TERRAMAP_RASTER_TILE_SET_DISCRIMINATOR = 2; // terramap:mapsync private static final int C2SP_MAPSYNC_REGISTER_DISCRIMINATOR = 0; @@ -56,7 +56,7 @@ public static void registerHandlers(Side side){ //terramap:sh private static final int P2C_SH_HELLO_DISCRIMINATOR = 0; - private static final int P2C_SH_MAPSTYLE_DISCRIMINATOR = 2; + private static final int P2C_SH_RASTER_TILESET_DISCRIMINATOR = 2; private static void registerTerramapS2C(int discriminator, Class> handlerclass, Class msgclass) { CHANNEL_TERRAMAP.registerMessage(handlerclass, msgclass, discriminator, Side.CLIENT); diff --git a/forge/src/main/java/fr/thesmyler/terramap/proxy/TerramapClientProxy.java b/forge/src/main/java/fr/thesmyler/terramap/proxy/TerramapClientProxy.java index 18c0e28f..12863a18 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/proxy/TerramapClientProxy.java +++ b/forge/src/main/java/fr/thesmyler/terramap/proxy/TerramapClientProxy.java @@ -1,6 +1,7 @@ package fr.thesmyler.terramap.proxy; import fr.thesmyler.smylibgui.screen.HudScreen; +import fr.thesmyler.terramap.TerramapConfig; import net.smyler.smylib.gui.screen.TestScreen; import fr.thesmyler.terramap.TerramapMod; import fr.thesmyler.terramap.command.OpenMapCommand; @@ -8,7 +9,6 @@ import fr.thesmyler.terramap.gui.HudScreenHandler; import fr.thesmyler.terramap.gui.widgets.markers.MarkerControllerManager; import fr.thesmyler.terramap.input.KeyBindings; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import fr.thesmyler.terramap.network.TerramapNetworkManager; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.AbstractClientPlayer; @@ -66,7 +66,7 @@ public void init(FMLInitializationEvent event) { MinecraftForge.EVENT_BUS.register(new ClientTerramapEventHandler()); KeyBindings.registerBindings(); MarkerControllerManager.registerBuiltInControllers(); - MapStylesLibrary.reload(); + Terramap.instance().rasterTileSetManager().reload(TerramapConfig.enableDebugMaps); ClientCommandHandler.instance.registerCommand(new OpenMapCommand()); MinecraftForge.EVENT_BUS.register(TestScreen.class); } diff --git a/forge/src/main/java/net/smyler/terramap/tilesets/raster/TerrainPreviewTileSet.java b/forge/src/main/java/net/smyler/terramap/tilesets/raster/TerrainPreviewTileSet.java new file mode 100644 index 00000000..8c749eab --- /dev/null +++ b/forge/src/main/java/net/smyler/terramap/tilesets/raster/TerrainPreviewTileSet.java @@ -0,0 +1,161 @@ +package net.smyler.terramap.tilesets.raster; + +import fr.thesmyler.terramap.TerramapClientContext; +import net.buildtheearth.terraplusplus.generator.TerrainPreview; +import net.smyler.smylib.Identifier; +import net.smyler.terramap.util.geo.TilePosImmutable; + +import java.awt.image.BufferedImage; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static net.smyler.smylib.SmyLib.getGameClient; + +public class TerrainPreviewTileSet extends CachingRasterTileSet { + + public static final int BASE_ZOOM_LEVEL = 16; + + public TerrainPreviewTileSet() { + this.setUsesLowZoom(false); // Loading tiles at low zoom levels takes forever here + } + + @Override + protected RasterTile createNewTile(TilePosImmutable position) { + return new TerrainPreviewTile(position); + } + + @Override + public String getId() { + return "terrain_preview_debug"; + } + + @Override + public String getLocalizedName(String localeKey) { + return getGameClient().translator().format("terramap.maps.debug.terrain"); // This is always local + } + + @Override + public String getComment() { + return "Terra++ terrain preview debug map"; + } + + @Override + public RasterTileSetProvider getProvider() { + return RasterTileSetProvider.INTERNAL; + } + + @Override + public long getProviderVersion() { + return 0; + } + + @Override + public int getDisplayPriority() { + return 0; + } + + @Override + public boolean isAllowedOnMinimap() { + return true; + } + + @Override + public boolean isDebug() { + return true; + } + + @Override + public Identifier getDefaultTileTexture() { + return null; + } + + @Override + public int getMinZoom() { + return BASE_ZOOM_LEVEL; + } + + @Override + public int getMaxZoom() { + return 20; + } + + private static class TerrainPreviewTile implements RasterTile { + + private final TilePosImmutable position; + private Identifier texture; + private CompletableFuture textureTask; + + public TerrainPreviewTile(TilePosImmutable position) { + this.position = position; + } + + @Override + public boolean isTextureAvailable() { + try { + this.tryLoadingTexture(); + } catch (Throwable e) { + return false; + } + return this.texture != null; + } + + @Override + public Identifier getTexture() throws Throwable { + + if(this.getPosition().getZoom() < TerrainPreviewTileSet.BASE_ZOOM_LEVEL) + throw new IllegalArgumentException("Trying to request a terrain preview with a zoom that's too low (" + this.position.getZoom() + ")"); + + if(this.getPosition().getZoom() != TerrainPreviewTileSet.BASE_ZOOM_LEVEL) return null; + + if(this.texture == null) { + if(this.textureTask == null) { + TerrainPreview preview = TerramapClientContext.getContext().getTerrainPreview(); + if(preview != null) { + this.textureTask = preview.tile(this.position.getX(), this.position.getY(), TerrainPreviewTileSet.BASE_ZOOM_LEVEL - this.position.getZoom()); + } + } else this.tryLoadingTexture(); + } + return this.texture; + } + + @Override + public void cancelTextureLoading() { + } + + @Override + public void unloadTexture() { + this.cancelTextureLoading(); + if(this.texture != null) { + getGameClient().guiDrawContext().unloadDynamicTexture(this.texture); + this.texture = null; + } + } + + @Override + public TilePosImmutable getPosition() { + return this.position; + } + + private void tryLoadingTexture() throws Throwable { + if(this.textureTask != null && this.textureTask.isDone()){ + if(this.textureTask.isCompletedExceptionally()) { + if(!this.textureTask.isCancelled()) { + try { + this.textureTask.get(); // That will throw an exception + } catch(ExecutionException e) { + this.textureTask = null; + throw e.getCause(); + } + } + this.textureTask = null; + return; + } + BufferedImage image = this.textureTask.get(); + this.texture = getGameClient().guiDrawContext().loadDynamicTexture(image); + this.textureTask = null; + } + } + + } + +} diff --git a/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java b/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java index 1008d07e..29077063 100644 --- a/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java +++ b/forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java @@ -1,10 +1,10 @@ package fr.thesmyler.terramap; import com.google.gson.Gson; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import net.smyler.smylib.SmyLibTest; import net.smyler.terramap.Terramap; import net.smyler.terramap.http.HttpClient; +import net.smyler.terramap.tilesets.raster.RasterTileSetManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.BeforeEach; @@ -12,10 +12,12 @@ public class TerramapTest extends SmyLibTest implements Terramap { private final Logger logger = LogManager.getLogger("Terramap unit test"); + private final RasterTileSetManager rasterTileSetManager = new RasterTileSetManager(null); + @BeforeEach public void initTerramap() { Terramap.InstanceHolder.setInstance(this); - MapStylesLibrary.reload(); + this.rasterTileSetManager.reload(true); TerramapClientContext.resetContext(); } @@ -46,4 +48,9 @@ public Gson gsonPretty() { return null; } + @Override + public RasterTileSetManager rasterTileSetManager() { + return null; + } + } diff --git a/forge/src/test/java/fr/thesmyler/terramap/gui/widgets/map/MapWidgetTest.java b/forge/src/test/java/fr/thesmyler/terramap/gui/widgets/map/MapWidgetTest.java index a253eaf2..dfa3109e 100644 --- a/forge/src/test/java/fr/thesmyler/terramap/gui/widgets/map/MapWidgetTest.java +++ b/forge/src/test/java/fr/thesmyler/terramap/gui/widgets/map/MapWidgetTest.java @@ -7,9 +7,10 @@ import fr.thesmyler.terramap.gui.widgets.map.layer.RasterMapLayer; import fr.thesmyler.terramap.maps.SavedLayerState; import fr.thesmyler.terramap.maps.SavedMapState; -import fr.thesmyler.terramap.maps.raster.MapStylesLibrary; import net.smyler.smylib.gui.screen.Screen; import net.smyler.smylib.math.Vec2dImmutable; +import net.smyler.terramap.Terramap; +import net.smyler.terramap.tilesets.raster.RasterTileSetManager; import net.smyler.terramap.util.geo.GeoPointImmutable; import org.junit.jupiter.api.Test; @@ -26,15 +27,16 @@ class MapWidgetTest extends TerramapTest { @Test void canSaveMapWidgetToSavedMapState() throws InterruptedException { TestGameClient client = this.getTestGameClient(); + RasterTileSetManager tileSetManager = Terramap.instance().rasterTileSetManager(); client.setWindowDimensions(500f, 500f); client.setTargetFps(60); MapWidget map = new MapWidget(0f, 0f, 0, 500f, 500f, FULLSCREEN, 1f); client.getCurrentScreen().addWidget(map); OnlineRasterMapLayer raster_osm = (OnlineRasterMapLayer) map.createLayer(RASTER_LAYER_ID); OnlineRasterMapLayer osm_fr_hot = (OnlineRasterMapLayer) map.createLayer(RASTER_LAYER_ID); - raster_osm.setTiledMap(MapStylesLibrary.getBaseMaps().get("osm")); + raster_osm.setTiledMap(tileSetManager.getBaseMaps().get("osm")); map.setLayerZ(raster_osm, -2); - osm_fr_hot.setTiledMap(MapStylesLibrary.getBaseMaps().get("osm_fr_hot")); + osm_fr_hot.setTiledMap(tileSetManager.getBaseMaps().get("osm_fr_hot")); map.setLayerZ(osm_fr_hot, -1); osm_fr_hot.setAlpha(0.5f); osm_fr_hot.setVisibility(false); @@ -76,6 +78,7 @@ void canSaveMapWidgetToSavedMapState() throws InterruptedException { public void canRestoreMapState() throws InterruptedException { TestGameClient client = this.getTestGameClient(); + RasterTileSetManager tileSetManager = Terramap.instance().rasterTileSetManager(); client.setWindowDimensions(500f, 500f); client.setTargetFps(60); Screen screen = client.getCurrentScreen(); @@ -86,7 +89,7 @@ public void canRestoreMapState() throws InterruptedException { // Let's start in some random state OnlineRasterMapLayer rasterLayer = (OnlineRasterMapLayer) map.createLayer(RASTER_LAYER_ID); - rasterLayer.setTiledMap(MapStylesLibrary.getBaseMaps().get("osm")); + rasterLayer.setTiledMap(tileSetManager.getBaseMaps().get("osm")); rasterLayer.setVisibility(false); map.setLayerZ(rasterLayer, -1); map.getController().setZoomStaticLocation(PARIS); From 00b52566962ae980530dfb3bad54bbcbcf12a09e Mon Sep 17 00:00:00 2001 From: Smyler Date: Sat, 6 Jul 2024 21:11:05 +0200 Subject: [PATCH 06/10] Add more advanced rendering --- .../thesmyler/smylibgui/util/RenderUtil.java | 91 ----------- .../command/TilesetReloadCommand.java | 1 - .../gui/widgets/CircularCompassWidget.java | 89 ++++++----- .../gui/widgets/RibbonCompassWidget.java | 98 +++++------- .../entities/AbstractPlayerMarker.java | 14 +- .../net/smyler/smylib/gui/DrawContext.java | 7 +- .../smylib/gui/advanced/AdvancedDrawing.java | 14 ++ .../smyler/smylib/gui/advanced/DrawMode.java | 10 ++ .../smylib/gui/advanced/VertexBuilder.java | 16 ++ .../smylib/gui/advanced/VertexFormat.java | 17 ++ .../smyler/smylib/gui/Lwjgl2DrawContext.java | 9 ++ .../advanced/TesselatorAdvancedDrawing.java | 151 ++++++++++++++++++ 12 files changed, 324 insertions(+), 193 deletions(-) create mode 100644 smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java create mode 100644 smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java create mode 100644 smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java create mode 100644 smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java create mode 100644 smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java diff --git a/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java b/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java index 45afcdab..ffd2bebf 100644 --- a/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java +++ b/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java @@ -1,51 +1,14 @@ package fr.thesmyler.smylibgui.util; -import net.smyler.smylib.Color; -import net.smyler.smylib.gui.DrawContext; -import org.lwjgl.opengl.GL11; - import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import static net.smyler.smylib.SmyLib.getGameClient; @Deprecated public final class RenderUtil { - @Deprecated - public static void drawRect(int z, double xLeft, double yTop, double xRight, double yBottom, Color color) { - drawGradientRect(z, xLeft, yTop, xRight, yBottom, color, color, color, color); - } - - @Deprecated - public static void drawRect(double xLeft, double yTop, double xRight, double yBottom, Color color) { - drawGradientRect(0, xLeft, yTop, xRight, yBottom, color, color, color, color); - } - - @Deprecated - public static void drawRectWithContour(int z, double xLeft, double yTop, double xRight, double yBottom, Color color, float contourSize, Color contourColor) { - DrawContext context = getGameClient().guiDrawContext(); - context.drawRectangleWithContours(z, xLeft, yTop, xRight, yBottom, color, contourSize, contourColor); - } - - @Deprecated - public static void drawRectWithContour(double xLeft, double yTop, double xRight, double yBottom, Color color, float contourSize, Color contourColor) { - drawRectWithContour(0, xLeft, yTop, xRight, yBottom, color, contourSize, contourColor); - } - - @Deprecated - public static void drawGradientRect(int z, double xLeft, double yTop, double xRight, double yBottom, Color upperLeftColor, Color lowerLeftColor, Color lowerRightColor, Color upperRightColor) { - DrawContext context = getGameClient().guiDrawContext(); - context.drawGradientRectangle(z, xLeft, yTop, xRight, yBottom, upperLeftColor, lowerLeftColor, lowerRightColor, upperRightColor); - } - - @Deprecated - public static void drawGradientRect(double xLeft, double yTop, double xRight, double yBottom, Color upperLeftColor, Color lowerLeftColor, Color lowerRightColor, Color upperRightColor) { - drawGradientRect(0, xLeft, yTop, xRight, yBottom, upperLeftColor, lowerLeftColor, lowerRightColor, upperRightColor); - } - @Deprecated public static void drawModalRectWithCustomSizedTexture(double x, double y, double z, double u, double v, double width, double height, double textureWidth, double textureHeight) { double f = 1.0f / textureWidth; @@ -69,58 +32,4 @@ public static void drawModalRectWithCustomSizedTexture(double x, double y, doubl drawModalRectWithCustomSizedTexture(x, y, 0d, u, v, width, height, textureWidth, textureHeight); } - @Deprecated - public static void drawTexturedModalRect(double x, double y, double z, double minU, double minV, double maxU, double maxV) { - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder builder = tessellator.getBuffer(); - GlStateManager.enableAlpha(); - GlStateManager.enableBlend(); - builder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX); - builder.pos(x, y + maxV, z).tex(minU * 0.00390625, (minV + maxV) * 0.00390625).endVertex(); - builder.pos(x + maxU, y + maxV, z).tex((minU + maxU) * 0.00390625, (minV + maxV) * 0.00390625).endVertex(); - builder.pos(x + maxU, y, z).tex((minU + maxU) * 0.00390625, minV * 0.00390625).endVertex(); - builder.pos(x, y, z).tex(minU * 0.00390625, minV * 0.00390625).endVertex(); - tessellator.draw(); - GlStateManager.disableAlpha(); - GlStateManager.disableBlend(); - } - - @Deprecated - public static void drawTexturedModalRect(double x, double y, double minU, double minV, double maxU, double maxV) { - drawTexturedModalRect(x, y, 0, minU, minV, maxU, maxV); - } - - @Deprecated - public static void drawPolygon(double z, Color color, double... points) { - DrawContext context = getGameClient().guiDrawContext(); - context.drawPolygon(z, color, points); - } - - @Deprecated - public static void drawPolygon(Color color, double... points) { - drawPolygon(0d, color, points); - } - - @Deprecated - public static void drawStrokeLine(double z, Color color, float size, double... points) { - DrawContext context = getGameClient().guiDrawContext(); - context.drawStrokeLine(z, color, size, points); - } - - @Deprecated - public static void drawStrokeLine(Color color, float size, double... points) { - drawStrokeLine(0, color, size, points); - } - - @Deprecated - public static void drawClosedStrokeLine(double z, Color color, float size, double... points) { - DrawContext context = getGameClient().guiDrawContext(); - context.drawClosedStrokeLine(z, color, size, points); - } - - @Deprecated - public static void drawClosedStrokeLine(Color color, float size, double... points) { - drawClosedStrokeLine(0d, color, size, points); - } - } diff --git a/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java b/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java index 147255f5..0661b4d3 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java +++ b/forge/src/main/java/fr/thesmyler/terramap/command/TilesetReloadCommand.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.List; -import fr.thesmyler.terramap.TerramapMod; import net.smyler.terramap.Terramap; import org.jetbrains.annotations.Nullable; diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java index aaefa434..92aae6ec 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java @@ -2,8 +2,8 @@ import net.smyler.smylib.gui.DrawContext; import net.smyler.smylib.gui.GlState; +import net.smyler.smylib.gui.advanced.AdvancedDrawing; import net.smyler.smylib.math.Vec2dMutable; -import org.lwjgl.opengl.GL11; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Animation; @@ -11,12 +11,10 @@ import net.smyler.smylib.Color; import net.smyler.smylib.gui.widgets.Widget; import net.smyler.smylib.math.Mat2d; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import static net.smyler.smylib.Color.WHITE; +import static net.smyler.smylib.Color.*; +import static net.smyler.smylib.gui.advanced.DrawMode.*; +import static net.smyler.smylib.gui.advanced.VertexFormat.*; public class CircularCompassWidget implements Widget { @@ -24,8 +22,8 @@ public class CircularCompassWidget implements Widget { private float x, y, size; private final int z; private Color backgroundColor = Color.DARKER_OVERLAY; - private Color northColor = Color.RED; - private Color northDarkColor = Color.RED.withRed(0.4f); + private Color northColor = RED; + private Color northDarkColor = RED.withRed(0.4f); private Color southColor = WHITE; private Color southColorDark = new Color(0.4f, 0.4f, 0.4f); private Runnable onClick; @@ -35,7 +33,7 @@ public class CircularCompassWidget implements Widget { private final Animation fader = new Animation(1000); - private double[] vertices; + private double[][] vertices; private final Vec2dMutable vertexCalculationHelper = new Vec2dMutable(); public CircularCompassWidget(float x, float y, int z, float size) { @@ -64,37 +62,42 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous float radius = this.size / 2; + AdvancedDrawing drawing = context.advancedDrawing(); + GlState glState = context.glState(); - GlStateManager.pushMatrix(); - GlStateManager.translate(x + radius, y + radius, 0); - context.drawPolygon(background, this.vertices); - //RenderUtil.drawClosedStrokeLine(Color.BLACK, 1f, vertices); - GlStateManager.rotate(this.azimuth, 0, 0, 1); - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder builder = tessellator.getBuffer(); - glState.setColor(WHITE); - GlStateManager.shadeModel(7425); - context.glState().enableAlpha(); - GlStateManager.enableBlend(); - GlStateManager.disableTexture2D(); - GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); - builder.begin(GL11.GL_POLYGON, DefaultVertexFormats.POSITION_COLOR); - builder.pos(0d, -radius, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).endVertex(); - builder.pos(-radius/3, radius * 0.1, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).endVertex(); - builder.pos(0d, 0d, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).endVertex(); - builder.pos(radius/3, radius * 0.1, 0d).color(northDark.redf(), northDark.greenf(), northDark.bluef(), northDark.alphaf()).endVertex(); - tessellator.draw(); - builder.begin(GL11.GL_POLYGON, DefaultVertexFormats.POSITION_COLOR); - builder.pos(0, 0, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).endVertex(); - builder.pos(-radius/3, radius * 0.1, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).endVertex(); - builder.pos(0, radius, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).endVertex(); - builder.pos(radius/3, radius * 0.1, 0).color(southColor.redf(), southColor.greenf(), southColor.bluef(), southColor.alphaf()).endVertex(); - tessellator.draw(); - GlStateManager.shadeModel(7424); - context.glState().disableAlpha(); - GlStateManager.disableBlend(); - GlStateManager.enableTexture2D(); - GlStateManager.popMatrix(); + glState.pushViewMatrix(); + glState.translate(x + radius, y + radius); + + // Background dark circle + drawing.begin(TRIANGLE_FAN, POSITION); + drawing.color(background); + drawing.vertex().position(0d, 0d, 0d).end(); + for (double[] vertex : this.vertices) { + drawing.vertex().position(vertex[0], vertex[1], 0d).end(); + } + drawing.vertex().position(this.vertices[0][0], this.vertices[0][1], 0d).end(); + drawing.draw(); + + glState.rotate(this.azimuth); + + // North arrow + drawing.begin(TRIANGLE_FAN, POSITION_COLOR); + drawing.color(WHITE); + drawing.vertex().position(0d, -radius, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); + drawing.vertex().position(-radius/3, radius * 0.1, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); + drawing.vertex().position(0d, 0d, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); + drawing.vertex().position(radius/3, radius * 0.1, 0d).color(northDark.redf(), northDark.greenf(), northDark.bluef(), northDark.alphaf()).end(); + drawing.draw(); + + // South arrow + drawing.begin(TRIANGLE_FAN, POSITION_COLOR); + drawing.vertex().position(0, 0, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); + drawing.vertex().position(-radius/3, radius * 0.1, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); + drawing.vertex().position(0, radius, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); + drawing.vertex().position(radius/3, radius * 0.1, 0).color(southColor.redf(), southColor.greenf(), southColor.bluef(), southColor.alphaf()).end(); + drawing.draw(); + + glState.popViewMatrix(); } @Override @@ -167,12 +170,14 @@ public void setSize(float size) { this.size = size; int vertexCount = (int) (2*Math.PI*this.size); float radius = this.size / 2; - this.vertices = new double[vertexCount*2]; + this.vertices = new double[vertexCount][2]; this.vertexCalculationHelper.set(0, -radius); Mat2d rot = Mat2d.forRotation(-Math.PI*2 / vertexCount); for(int i = 0; i < vertexCount; i++) { - this.vertices[2*i] = this.vertexCalculationHelper.x(); - this.vertices[2*i + 1] = this.vertexCalculationHelper.y(); + this.vertices[i] = new double[] { + this.vertexCalculationHelper.x(), + this.vertexCalculationHelper.y() + }; this.vertexCalculationHelper.apply(rot); } } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java index 5700bd8a..10955e2f 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java @@ -1,32 +1,33 @@ package fr.thesmyler.terramap.gui.widgets; +import net.smyler.smylib.Identifier; import net.smyler.smylib.gui.DrawContext; import net.smyler.smylib.gui.GlState; +import net.smyler.smylib.gui.advanced.AdvancedDrawing; import net.smyler.terramap.Terramap; import org.lwjgl.opengl.GL11; import net.smyler.smylib.gui.containers.WidgetContainer; -import fr.thesmyler.smylibgui.util.TextureUtil; -import fr.thesmyler.smylibgui.util.TextureUtil.TextureProperties; -import fr.thesmyler.smylibgui.util.TextureUtil.UnknownTextureException; import net.smyler.smylib.gui.widgets.Widget; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.util.ResourceLocation; import static net.smyler.smylib.Color.WHITE; +import static net.smyler.smylib.gui.advanced.DrawMode.QUADS; +import static net.smyler.smylib.gui.advanced.VertexFormat.POSITION_TEXTURE; +import static net.smyler.smylib.gui.advanced.VertexFormat.POSITION_TEXTURE_COLOR; public class RibbonCompassWidget implements Widget { - private static final ResourceLocation COMPASS_BACKGROUND_TEXTURE = new ResourceLocation(Terramap.MOD_ID, "textures/gui/compass_ribbon_background.png"); - private static final ResourceLocation COMPASS_INDICATOR_TEXTURE = new ResourceLocation(Terramap.MOD_ID, "textures/gui/compass_ribbon_indicator.png"); + private static final Identifier COMPASS_BACKGROUND_TEXTURE = new Identifier(Terramap.MOD_ID, "textures/gui/compass_ribbon_background.png"); + private static final Identifier COMPASS_INDICATOR_TEXTURE = new Identifier(Terramap.MOD_ID, "textures/gui/compass_ribbon_indicator.png"); private float x, y; private final int z; - private float width, height, textureWidth, indicatorWidth, indicatorHeight; + private float width; + private final float height; + private final float textureWidth; + private final float indicatorWidth; + private final float indicatorHeight; private float azimuth = 0; private boolean visibility = true; @@ -35,71 +36,60 @@ public RibbonCompassWidget(float x, float y, int z, float width) { this.y = y; this.z = z; this.width = width; - try { - TextureProperties p1 = TextureUtil.getTextureProperties(COMPASS_BACKGROUND_TEXTURE); - this.height = p1.getHeight(); - this.textureWidth = p1.getWidth(); - TextureProperties p2 = TextureUtil.getTextureProperties(COMPASS_INDICATOR_TEXTURE); - this.indicatorHeight = p2.getHeight(); - this.indicatorWidth = p2.getWidth(); - } catch (UnknownTextureException e) { - Terramap.instance().logger().error("Failed to get texture heiht for ribbon compass"); - Terramap.instance().logger().catching(e); - this.height = 16; - } - } - - public RibbonCompassWidget(int z) { - this(0, 0, z, 0); + this.height = 16f; + this.textureWidth = 360f; + this.indicatorHeight = 16f; + this.indicatorWidth = 1f; } @Override public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { - double blendBorder = 10; // How many pixels to fade to alpha=0 on the sides + double blendBorder = 10d; // How many pixels to fade to alpha=0 on the sides double leftU = (double)(this.azimuth - 180) / 360 + (double)(this.textureWidth - this.width) / this.textureWidth / 2; double leftCU = leftU + blendBorder/this.textureWidth; double rightU = leftU + (double) this.width / this.textureWidth; double rightCU = rightU - blendBorder/this.textureWidth; GlState glState = context.glState(); - Tessellator tess = Tessellator.getInstance(); - BufferBuilder buff = tess.getBuffer(); - GlStateManager.enableTexture2D(); glState.enableAlpha(); GlStateManager.enableBlend(); - Minecraft.getMinecraft().getTextureManager().bindTexture(COMPASS_BACKGROUND_TEXTURE); GlStateManager.shadeModel(GL11.GL_SMOOTH); - GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); - buff.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_COLOR); - buff.pos(x, y, 0).tex(leftU, 0d).color(1f, 1f, 1f, 0f).endVertex(); - buff.pos(x, y + this.height, 0).tex(leftU, 1d).color(1f, 1f, 1f, 0f).endVertex(); - buff.pos(x + blendBorder, y + this.height, 0).tex(leftCU, 1d).color(1f, 1f, 1f, 1f).endVertex(); - buff.pos(x + blendBorder, y, 0).tex(leftCU, 0d).color(1f, 1f, 1f, 1f).endVertex(); + AdvancedDrawing drawing = context.advancedDrawing(); + + drawing.begin(QUADS, POSITION_TEXTURE_COLOR); + drawing.texture(COMPASS_BACKGROUND_TEXTURE); - buff.pos(x + blendBorder, y, 0).tex(leftCU, 0d).color(1f, 1f, 1f, 1f).endVertex(); - buff.pos(x + blendBorder, y + this.height, 0).tex(leftCU, 1d).color(1f, 1f, 1f, 1f).endVertex(); - buff.pos(x + this.width - blendBorder, y + this.height, 0).tex(rightCU, 1d).color(1f, 1f, 1f, 1f).endVertex(); - buff.pos(x + this.width - blendBorder, y, 0).tex(rightCU, 0d).color(1f, 1f, 1f, 1f).endVertex(); + drawing.vertex().position(x, y, 0d).texture(leftU, 0d).color(1f, 1f, 1f, 0f).end(); + drawing.vertex().position(x, y + this.height, 0d).texture(leftU, 1d).color(1f, 1f, 1f, 0f).end(); + drawing.vertex().position(x + blendBorder, y + this.height, 0d).texture(leftCU, 1f).color(1f, 1f, 1f, 1f).end(); + drawing.vertex().position(x + blendBorder, y, 0d).texture(leftCU, 0f).color(1f, 1f, 1f, 1f).end(); - buff.pos(x + this.width - blendBorder, y, 0).tex(rightCU, 0d).color(1f, 1f, 1f, 1f).endVertex(); - buff.pos(x + this.width - blendBorder, y + this.height, 0).tex(rightCU, 1d).color(1f, 1f, 1f, 1f).endVertex(); - buff.pos(x + this.width, y + this.height, 0).tex(rightU, 1d).color(1f, 1f, 1f, 0f).endVertex(); - buff.pos(x + this.width, y, 0).tex(rightU, 0d).color(1f, 1f, 1f, 0f).endVertex(); + drawing.vertex().position(x + blendBorder, y, 0d).texture(leftCU, 0d).color(1f, 1f, 1f, 1f).end(); + drawing.vertex().position(x + blendBorder, y + this.height, 0d).texture(leftCU, 1d).color(1f, 1f, 1f, 1f).end(); + drawing.vertex().position(x + this.width - blendBorder, y + this.height, 0d).texture(rightCU, 1d).color(1f, 1f, 1f, 1f).end(); + drawing.vertex().position(x + this.width - blendBorder, y, 0d).texture(rightCU, 0d).color(1f, 1f, 1f, 1f).end(); - tess.draw(); + drawing.vertex().position(x + this.width - blendBorder, y, 0d).texture(rightCU, 0d).color(1f, 1f, 1f, 1f).end(); + drawing.vertex().position(x + this.width - blendBorder, y + this.height, 0d).texture(rightCU, 1d).color(1f, 1f, 1f, 1f).end(); + drawing.vertex().position(x + this.width, y + this.height, 0d).texture(rightU, 1d).color(1f, 1f, 1f, 0f).end(); + drawing.vertex().position(x + this.width, y, 0d).texture(rightU, 0d).color(1f, 1f, 1f, 0f).end(); + + drawing.draw(); - Minecraft.getMinecraft().getTextureManager().bindTexture(COMPASS_INDICATOR_TEXTURE); glState.setColor(WHITE); - buff.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX); + double indX = x + (double)(this.width - this.indicatorWidth) / 2; double indY = y + (double)(this.height - this.indicatorHeight) / 2; - buff.pos(indX, indY, 0).tex(0d, 0d).endVertex(); - buff.pos(indX, indY + this.indicatorHeight, 0).tex(0d, 1d).endVertex(); - buff.pos(indX + this.indicatorWidth, indY + this.indicatorHeight, 0).tex(1d, 1d).endVertex(); - buff.pos(indX + this.indicatorWidth, indY, 0).tex(1d, 0d).endVertex(); - tess.draw(); + + drawing.begin(QUADS, POSITION_TEXTURE); + drawing.texture(COMPASS_INDICATOR_TEXTURE); + drawing.vertex().position(indX, indY, 0d).texture(0d, 0d).end(); + drawing.vertex().position(indX, indY + this.indicatorHeight, 0d).texture(0d, 1d).end(); + drawing.vertex().position(indX + this.indicatorWidth, indY + this.indicatorHeight, 0d).texture(1d, 1d).end(); + drawing.vertex().position(indX + this.indicatorWidth, indY, 0d).texture(1d, 0d).end(); + drawing.draw(); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java index 8759bc86..1b25e86f 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java @@ -41,10 +41,16 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous // Draw the direction arrow if(this.showDirection(hovered) && Float.isFinite(this.azimuth)) { float azimuth = this.azimuth; - if(parent instanceof MapWidget) azimuth += ((MapWidget)parent).getController().getRotation(); - GlStateManager.pushMatrix(); - GlStateManager.translate(x + this.width / 2, y + this.height / 2, 0); - GlStateManager.rotate(azimuth, 0, 0, 1); + if(parent instanceof MapWidget) { + azimuth += ((MapWidget)parent).getController().getRotation(); + } + + GlState gl = context.glState(); + gl.pushViewMatrix(); + + gl.translate(x + this.width / 2, y + this.height / 2); + gl.rotate(azimuth); + GlStateManager.disableTexture2D(); GlStateManager.enableBlend(); context.glState().disableAlpha(); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java b/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java index 4d8b2003..04f9ba6d 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java @@ -2,9 +2,9 @@ import net.smyler.smylib.Color; import net.smyler.smylib.Identifier; +import net.smyler.smylib.gui.advanced.AdvancedDrawing; import net.smyler.smylib.gui.sprites.Sprite; -import java.awt.*; import java.awt.image.BufferedImage; public interface DrawContext { @@ -41,8 +41,10 @@ default void drawGradientRectangle(double xLeft, double yTop, double xRight, dou this.drawGradientRectangle(0d, xLeft, yTop, xRight, yBottom, upperLeftColor, lowerLeftColor, lowerRightColor, upperRightColor); } + @Deprecated void drawPolygon(double z, Color color, double... points); + @Deprecated default void drawPolygon(Color color, double... points) { this.drawPolygon(0d, color, points); } @@ -76,10 +78,13 @@ default void drawSpriteCropped(double x, double y, Sprite sprite, double leftCro //TODO use Text in drawTooltip void drawTooltip(String text, double x, double y); + @Deprecated void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight); Identifier loadDynamicTexture(BufferedImage image); void unloadDynamicTexture(Identifier texture); + AdvancedDrawing advancedDrawing(); + } diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java new file mode 100644 index 00000000..5f14d770 --- /dev/null +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java @@ -0,0 +1,14 @@ +package net.smyler.smylib.gui.advanced; + +import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; + +public interface AdvancedDrawing { + + void begin(DrawMode mode, VertexFormat components); + void texture(Identifier identifier); + void color(Color color); + VertexBuilder vertex(); + void draw(); + +} diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java new file mode 100644 index 00000000..10890334 --- /dev/null +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java @@ -0,0 +1,10 @@ +package net.smyler.smylib.gui.advanced; + +public enum DrawMode { + LINES, + LINE_STRIP, + TRIANGLES, + TRIANGLE_STRIP, + TRIANGLE_FAN, + QUADS, +} diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java new file mode 100644 index 00000000..3ec75e08 --- /dev/null +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java @@ -0,0 +1,16 @@ +package net.smyler.smylib.gui.advanced; + +import net.smyler.smylib.Color; + +public interface VertexBuilder { + + VertexBuilder position(double x, double y, double z); + + VertexBuilder color(float r, float g, float b, float a); + VertexBuilder color(Color color); + + VertexBuilder texture(double u, double v); + + void end(); + +} diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java new file mode 100644 index 00000000..49b1fb23 --- /dev/null +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java @@ -0,0 +1,17 @@ +package net.smyler.smylib.gui.advanced; + +public enum VertexFormat { + POSITION(true, false, false), + POSITION_TEXTURE(true, true, false), + POSITION_COLOR(true, false, true), + POSITION_TEXTURE_COLOR(true, true, true); + + final boolean position; + final boolean texture; + final boolean color; + VertexFormat(boolean position, boolean texture, boolean color) { + this.position = position; + this.color = color; + this.texture = texture; + } +} diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java index cc78aa60..8da025cf 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java @@ -9,6 +9,8 @@ import net.minecraft.util.ResourceLocation; import net.smyler.smylib.Color; import net.smyler.smylib.Identifier; +import net.smyler.smylib.gui.advanced.AdvancedDrawing; +import net.smyler.smylib.gui.advanced.TesselatorAdvancedDrawing; import net.smyler.smylib.gui.sprites.Sprite; import org.lwjgl.opengl.GL11; @@ -24,6 +26,7 @@ public class Lwjgl2DrawContext implements DrawContext { private final Scissor scissor = new Gl11Scissor(); private final GlState glState = new LwjglState(); + private final AdvancedDrawing advancedDrawing = new TesselatorAdvancedDrawing(); private final AtomicInteger dynamicTextureCounter = new AtomicInteger(0); @Override @@ -86,6 +89,7 @@ public void drawSpriteCropped(double x, double y, double z, Sprite sprite, doubl GlStateManager.color(1f, 1f, 1f, 1f); GlStateManager.enableAlpha(); GlStateManager.enableBlend(); + GlStateManager.enableTexture2D(); GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); @@ -154,6 +158,11 @@ public void unloadDynamicTexture(Identifier texture) { getMinecraft().getTextureManager().deleteTexture(new ResourceLocation(texture.namespace, texture.path)); } + @Override + public AdvancedDrawing advancedDrawing() { + return this.advancedDrawing; + } + private void drawMultiPointsGeometry(int glType, double z, Color color, double... points) { checkArgument(points.length % 2 == 0, "An even number of coordinates is required"); GlStateManager.enableAlpha(); diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java new file mode 100644 index 00000000..868d2a2a --- /dev/null +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java @@ -0,0 +1,151 @@ +package net.smyler.smylib.gui.advanced; + +import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import net.minecraft.util.ResourceLocation; +import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; +import org.lwjgl.opengl.GL11; + +import static net.minecraft.client.Minecraft.getMinecraft; +import static net.smyler.smylib.Preconditions.checkState; + +public class TesselatorAdvancedDrawing implements AdvancedDrawing { + + private final Tessellator tessellator = Tessellator.getInstance(); + private final BufferBuilder bufferBuilder = this.tessellator.getBuffer(); + private final TextureManager textureManager = getMinecraft().getTextureManager(); + + private VertexFormat currentFormat = null; + + @Override + public void begin(DrawMode mode, VertexFormat format) { + int glMode; + net.minecraft.client.renderer.vertex.VertexFormat glVertexFormat; + switch (mode) { + case LINES: + glMode = GL11.GL_LINES; + break; + case LINE_STRIP: + glMode = GL11.GL_LINE_STRIP; + break; + case TRIANGLES: + glMode = GL11.GL_TRIANGLES; + break; + case TRIANGLE_STRIP: + glMode = GL11.GL_TRIANGLE_STRIP; + break; + case TRIANGLE_FAN: + glMode = GL11.GL_TRIANGLE_FAN; + break; + case QUADS: + glMode = GL11.GL_QUADS; + break; + default: + throw new IllegalStateException("Unsupported mode: " + mode); + } + switch (format) { + case POSITION: + glVertexFormat = DefaultVertexFormats.POSITION; + break; + case POSITION_COLOR: + glVertexFormat = DefaultVertexFormats.POSITION_COLOR; + break; + case POSITION_TEXTURE: + glVertexFormat = DefaultVertexFormats.POSITION_TEX; + break; + case POSITION_TEXTURE_COLOR: + glVertexFormat = DefaultVertexFormats.POSITION_TEX_COLOR; + break; + default: + throw new IllegalStateException("Unsupported format: " + format); + } + this.bufferBuilder.begin(glMode, glVertexFormat); + this.currentFormat = format; + } + + @Override + public void texture(Identifier identifier) { + this.textureManager.bindTexture(new ResourceLocation(identifier.namespace, identifier.path)); + } + + @Override + public void color(Color color) { + GlStateManager.color(color.redf(), color.greenf(), color.bluef(), color.alphaf()); + } + + @Override + public VertexBuilder vertex() { + checkState(this.currentFormat != null, "Not building!"); + return new VertexBuilderImplementation(); + } + + @Override + public void draw() { + if (this.currentFormat.texture) { + GlStateManager.enableTexture2D(); + GlStateManager.shadeModel(GL11.GL_FLAT); + } else { + GlStateManager.disableTexture2D(); + GlStateManager.shadeModel(GL11.GL_SMOOTH); + } + GlStateManager.enableAlpha(); + GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); + GlStateManager.enableBlend(); + this.tessellator.draw(); + this.currentFormat = null; + } + + private class VertexBuilderImplementation implements VertexBuilder { + + private double x, y, z; + private float r, g, b, a; + private double u, v; + + @Override + public VertexBuilder position(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + return this; + } + + @Override + public VertexBuilder color(float r, float g, float b, float a) { + this.r = r; + this.g = g; + this.b = b; + this.a = a; + return this; + } + + @Override + public VertexBuilder color(Color color) { + return this.color(color.redf(), color.greenf(), color.bluef(), color.alphaf()); + } + + @Override + public VertexBuilder texture(double u, double v) { + this.u = u; + this.v = v; + return this; + } + + @Override + public void end() { + VertexFormat format = TesselatorAdvancedDrawing.this.currentFormat; + TesselatorAdvancedDrawing.this.bufferBuilder.pos(this.x, this.y, this.z); + if (format.texture) { + TesselatorAdvancedDrawing.this.bufferBuilder.tex(this.u, this.v); + } + if (format.color) { + TesselatorAdvancedDrawing.this.bufferBuilder.color(this.r, this.g, this.b, this.a); + } + TesselatorAdvancedDrawing.this.bufferBuilder.endVertex(); + } + } + +} From 022b22c6fa91a3e14b1201f740463ef29926cbc7 Mon Sep 17 00:00:00 2001 From: Smyler Date: Sat, 6 Jul 2024 21:50:41 +0200 Subject: [PATCH 07/10] Merge AdvancedDrawing and GlState into GlContext --- .../container/WindowedContainer.java | 8 +- .../thesmyler/smylibgui/screen/HudScreen.java | 16 +-- .../smylibgui/widgets/ChatWidget.java | 4 +- .../gui/screens/LayerListContainer.java | 4 +- .../terramap/gui/screens/TerramapScreen.java | 8 +- .../gui/screens/config/HudConfigScreen.java | 4 +- .../gui/widgets/CircularCompassWidget.java | 63 ++++----- .../gui/widgets/RibbonCompassWidget.java | 65 +++++---- .../terramap/gui/widgets/map/InputLayer.java | 10 +- .../terramap/gui/widgets/map/MapLayer.java | 8 +- .../terramap/gui/widgets/map/MapWidget.java | 4 +- .../gui/widgets/map/ScaleIndicatorWidget.java | 4 +- .../widgets/map/layer/DistortionLayer.java | 8 +- .../gui/widgets/map/layer/McChunksLayer.java | 12 +- .../map/layer/OnlineRasterMapLayer.java | 4 +- .../gui/widgets/map/layer/RasterMapLayer.java | 16 +-- .../map/layer/RenderingDeltaPreviewLayer.java | 8 +- .../markers/markers/RightClickMarker.java | 4 +- .../entities/AbstractLivingMarker.java | 12 +- .../entities/AbstractPlayerMarker.java | 14 +- .../net/smyler/smylib/game/GameClient.java | 4 +- .../{DrawContext.java => UiDrawContext.java} | 9 +- .../smylib/gui/advanced/AdvancedDrawing.java | 14 -- .../smylib/gui/containers/RootContainer.java | 4 +- .../containers/ScrollableWidgetContainer.java | 4 +- .../gui/containers/SlidingPanelWidget.java | 4 +- .../gui/containers/WidgetContainer.java | 6 +- .../smylib/gui/{ => gl}/ColorLogic.java | 2 +- .../smylib/gui/{advanced => gl}/DrawMode.java | 2 +- .../gui/{GlState.java => gl/GlContext.java} | 12 +- .../smyler/smylib/gui/{ => gl}/Scissor.java | 2 +- .../gui/{advanced => gl}/VertexBuilder.java | 2 +- .../gui/{advanced => gl}/VertexFormat.java | 2 +- .../net/smyler/smylib/gui/popups/Popup.java | 4 +- .../smylib/gui/widgets/ColorPickerWidget.java | 4 +- .../smyler/smylib/gui/widgets/MenuWidget.java | 8 +- .../smylib/gui/widgets/ScrollbarWidget.java | 6 +- .../smylib/gui/widgets/SpriteWidget.java | 4 +- .../net/smyler/smylib/gui/widgets/Widget.java | 4 +- .../widgets/buttons/AbstractButtonWidget.java | 4 +- .../widgets/buttons/SpriteButtonWidget.java | 4 +- .../gui/widgets/buttons/TextButtonWidget.java | 4 +- .../widgets/buttons/ToggleButtonWidget.java | 4 +- .../widgets/sliders/AbstractSliderWidget.java | 4 +- .../gui/widgets/text/TextFieldWidget.java | 10 +- .../smylib/gui/widgets/text/TextWidget.java | 6 +- .../smyler/smylib/game/WrappedMinecraft.java | 6 +- ...wContext.java => Lwjgl2UiDrawContext.java} | 17 +-- .../net/smyler/smylib/gui/LwjglState.java | 106 -------------- .../smylib/gui/{ => gl}/Gl11Scissor.java | 2 +- .../Lwjgl2GlContext.java} | 131 +++++++++++++++--- .../smylib/gui/screen/GuiScreenProxy.java | 6 +- .../gui/screen/PopupScreenImplementation.java | 5 +- .../smyler/smylib/game/TestGameClient.java | 13 +- .../smyler/smylib/gui/DummyDrawContext.java | 51 ------- .../net/smyler/smylib/gui/DummyGlContext.java | 115 +++++++++++++++ .../net/smyler/smylib/gui/DummyGlState.java | 64 --------- .../net/smyler/smylib/gui/DummyScissor.java | 2 + .../smyler/smylib/gui/DummyUiDrawContext.java | 82 +++++++++++ 59 files changed, 531 insertions(+), 478 deletions(-) rename smylib/core/src/main/java/net/smyler/smylib/gui/{DrawContext.java => UiDrawContext.java} (96%) delete mode 100644 smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java rename smylib/core/src/main/java/net/smyler/smylib/gui/{ => gl}/ColorLogic.java (93%) rename smylib/core/src/main/java/net/smyler/smylib/gui/{advanced => gl}/DrawMode.java (74%) rename smylib/core/src/main/java/net/smyler/smylib/gui/{GlState.java => gl/GlContext.java} (61%) rename smylib/core/src/main/java/net/smyler/smylib/gui/{ => gl}/Scissor.java (98%) rename smylib/core/src/main/java/net/smyler/smylib/gui/{advanced => gl}/VertexBuilder.java (88%) rename smylib/core/src/main/java/net/smyler/smylib/gui/{advanced => gl}/VertexFormat.java (91%) rename smylib/forge/src/main/java/net/smyler/smylib/gui/{Lwjgl2DrawContext.java => Lwjgl2UiDrawContext.java} (94%) delete mode 100644 smylib/forge/src/main/java/net/smyler/smylib/gui/LwjglState.java rename smylib/forge/src/main/java/net/smyler/smylib/gui/{ => gl}/Gl11Scissor.java (99%) rename smylib/forge/src/main/java/net/smyler/smylib/gui/{advanced/TesselatorAdvancedDrawing.java => gl/Lwjgl2GlContext.java} (57%) delete mode 100644 smylib/testing/src/main/java/net/smyler/smylib/gui/DummyDrawContext.java create mode 100644 smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java delete mode 100644 smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlState.java create mode 100644 smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java diff --git a/forge/src/main/java/fr/thesmyler/smylibgui/container/WindowedContainer.java b/forge/src/main/java/fr/thesmyler/smylibgui/container/WindowedContainer.java index 2a7b3941..ea512618 100644 --- a/forge/src/main/java/fr/thesmyler/smylibgui/container/WindowedContainer.java +++ b/forge/src/main/java/fr/thesmyler/smylibgui/container/WindowedContainer.java @@ -1,6 +1,6 @@ package fr.thesmyler.smylibgui.container; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; import net.smyler.smylib.gui.containers.WidgetContainer; import org.lwjgl.input.Cursor; @@ -73,7 +73,7 @@ public WindowedContainer(int z, String title) { @Override public void draw( - DrawContext context, float x, + UiDrawContext context, float x, float y, float mouseX, float mouseY, @@ -120,7 +120,7 @@ public int getZ() { protected abstract Color getBackgroundColor(); @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { context.drawRectangle(x, y, x + this.getWidth(), y + this.getHeight(), this.getBackgroundColor()); if(this.lastHovered != hovered && !getGameClient().mouse().isButtonPressed(0)) { if(hovered && this.isCursorEnabled() && WindowedContainer.this.enableCustomCursors) Cursors.trySetCursor(this.cursor); @@ -517,7 +517,7 @@ public float getHeight() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { super.draw(context, x, y, mouseX, mouseY, hovered, focused, parent); float width = this.getWidth(); String toDraw = parent.getFont().trimRight(WindowedContainer.this.windowTitle, this.getWidth()); diff --git a/forge/src/main/java/fr/thesmyler/smylibgui/screen/HudScreen.java b/forge/src/main/java/fr/thesmyler/smylibgui/screen/HudScreen.java index c4c93f71..f0534ad1 100644 --- a/forge/src/main/java/fr/thesmyler/smylibgui/screen/HudScreen.java +++ b/forge/src/main/java/fr/thesmyler/smylibgui/screen/HudScreen.java @@ -4,7 +4,7 @@ import java.util.List; import net.smyler.smylib.gui.containers.RootContainer; -import net.smyler.smylib.gui.GlState; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.smylib.gui.containers.WidgetContainer; import fr.thesmyler.smylibgui.event.HudScreenInitEvent; import net.smyler.smylib.Color; @@ -25,8 +25,8 @@ import net.smyler.smylib.game.GameClient; import net.smyler.smylib.game.Mouse; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.Scissor; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.Scissor; import net.smyler.smylib.gui.screen.InputProcessor; import org.jetbrains.annotations.Nullable; @@ -58,8 +58,8 @@ public static void onRenderHUD(RenderGameOverlayEvent.Pre e) { if(!e.getType().equals(ElementType.HOTBAR)) return; GameClient game = getGameClient(); Mouse mouse = game.mouse(); - DrawContext drawContext = game.guiDrawContext(); - GlState glState = drawContext.glState(); + UiDrawContext drawContext = game.guiDrawContext(); + GlContext gl = drawContext.gl(); Scissor scissor = drawContext.scissor(); boolean chatOpen = Minecraft.getMinecraft().currentScreen instanceof GuiChat; float width = game.windowWidth(); @@ -72,12 +72,12 @@ public static void onRenderHUD(RenderGameOverlayEvent.Pre e) { float mouseX = mouse.x(); float mouseY = mouse.y(); CONTAINER.onUpdate(mouseX, mouseY, null); - Color color = glState.getColor(); + Color color = gl.getColor(); scissor.push(); scissor.cropScreen(-1f, -1f, renderWidth + 1f, renderHeight + 1f); CONTAINER.draw(drawContext, 0, 0, mouseX, mouseY, chatOpen && !isOverChat(mouseX, mouseY), false, null); - drawContext.glState().enableAlpha(); - glState.setColor(color); // Reset color to what it was + drawContext.gl().enableAlpha(); + gl.setColor(color); // Reset color to what it was } @SubscribeEvent diff --git a/forge/src/main/java/fr/thesmyler/smylibgui/widgets/ChatWidget.java b/forge/src/main/java/fr/thesmyler/smylibgui/widgets/ChatWidget.java index 364bede5..2888d3dc 100644 --- a/forge/src/main/java/fr/thesmyler/smylibgui/widgets/ChatWidget.java +++ b/forge/src/main/java/fr/thesmyler/smylibgui/widgets/ChatWidget.java @@ -17,7 +17,7 @@ import net.minecraft.util.ITabCompleter; import net.minecraftforge.fml.common.ObfuscationReflectionHelper; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import org.jetbrains.annotations.Nullable; import static net.smyler.smylib.SmyLib.getLogger; @@ -93,7 +93,7 @@ public float getHeight() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { if (!this.visible) { return; } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/LayerListContainer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/LayerListContainer.java index e20b8bda..821799a0 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/LayerListContainer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/LayerListContainer.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.List; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Animation; @@ -18,7 +19,6 @@ import fr.thesmyler.terramap.gui.widgets.map.MapWidget; import fr.thesmyler.terramap.gui.widgets.map.layer.RasterMapLayer; -import net.smyler.smylib.gui.DrawContext; import org.jetbrains.annotations.Nullable; import static net.smyler.smylib.gui.sprites.SmyLibSprites.*; @@ -103,7 +103,7 @@ public LayerEntry(MapLayer layer, float y, float height) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { context.drawRectangleWithContours(x, y, x + this.getWidth(), y + this.getHeight(), Color.LIGHT_OVERLAY , 1f, Color.DARK_GRAY); super.draw(context, x, y, mouseX, mouseY, screenHovered, screenFocused, parent); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java index 7a8981c1..8c955ea3 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java @@ -4,8 +4,8 @@ import java.util.function.Consumer; import net.smyler.smylib.game.Key; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.Scissor; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.Scissor; import net.smyler.smylib.gui.sprites.WarningWidget; import net.smyler.smylib.text.ImmutableText; import net.smyler.smylib.text.TextStyle; @@ -664,7 +664,7 @@ public FailedMapLoadingNotice(float x, float y, int z, float width, float height } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { float width = this.getWidth(); float height = this.getHeight(); Translator translator = getGameClient().translator(); @@ -738,7 +738,7 @@ public MapPreview(int z, RasterTileSet map, Consumer onClick) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { super.draw(context, x, y, mouseX, mouseY, hovered, focused, parent); Color textColor = hovered? Color.SELECTION: Color.WHITE; String text = this.previewLayer.getTiledMap().getLocalizedName(getGameClient().translator().language()); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java index 1bf770da..e54c4dae 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java @@ -38,7 +38,7 @@ import net.minecraft.client.Minecraft; import net.smyler.smylib.game.GameClient; import net.smyler.smylib.game.Translator; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import static net.smyler.smylib.SmyLib.getGameClient; import static net.smyler.smylib.text.ImmutableText.ofTranslation; @@ -372,7 +372,7 @@ private static class CompassScreen extends FlexibleWidgetContainer { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { this.compass.setWidth(this.getWidth()); super.draw(context, x, y, mouseX, mouseY, screenHovered, screenFocused, parent); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java index 92aae6ec..d47a039d 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java @@ -1,8 +1,7 @@ package fr.thesmyler.terramap.gui.widgets; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.GlState; -import net.smyler.smylib.gui.advanced.AdvancedDrawing; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.smylib.math.Vec2dMutable; import net.smyler.smylib.gui.containers.WidgetContainer; @@ -13,8 +12,8 @@ import net.smyler.smylib.math.Mat2d; import static net.smyler.smylib.Color.*; -import static net.smyler.smylib.gui.advanced.DrawMode.*; -import static net.smyler.smylib.gui.advanced.VertexFormat.*; +import static net.smyler.smylib.gui.gl.DrawMode.*; +import static net.smyler.smylib.gui.gl.VertexFormat.*; public class CircularCompassWidget implements Widget { @@ -44,7 +43,7 @@ public CircularCompassWidget(float x, float y, int z, float size) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { Color background = this.backgroundColor; Color north = this.northColor; @@ -62,42 +61,40 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous float radius = this.size / 2; - AdvancedDrawing drawing = context.advancedDrawing(); - - GlState glState = context.glState(); - glState.pushViewMatrix(); - glState.translate(x + radius, y + radius); + GlContext gl = context.gl(); + gl.pushViewMatrix(); + gl.translate(x + radius, y + radius); // Background dark circle - drawing.begin(TRIANGLE_FAN, POSITION); - drawing.color(background); - drawing.vertex().position(0d, 0d, 0d).end(); + gl.startDrawing(TRIANGLE_FAN, POSITION); + gl.setColor(background); + gl.vertex().position(0d, 0d, 0d).end(); for (double[] vertex : this.vertices) { - drawing.vertex().position(vertex[0], vertex[1], 0d).end(); + gl.vertex().position(vertex[0], vertex[1], 0d).end(); } - drawing.vertex().position(this.vertices[0][0], this.vertices[0][1], 0d).end(); - drawing.draw(); + gl.vertex().position(this.vertices[0][0], this.vertices[0][1], 0d).end(); + gl.draw(); - glState.rotate(this.azimuth); + gl.rotate(this.azimuth); // North arrow - drawing.begin(TRIANGLE_FAN, POSITION_COLOR); - drawing.color(WHITE); - drawing.vertex().position(0d, -radius, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); - drawing.vertex().position(-radius/3, radius * 0.1, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); - drawing.vertex().position(0d, 0d, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); - drawing.vertex().position(radius/3, radius * 0.1, 0d).color(northDark.redf(), northDark.greenf(), northDark.bluef(), northDark.alphaf()).end(); - drawing.draw(); + gl.startDrawing(TRIANGLE_FAN, POSITION_COLOR); + gl.setColor(WHITE); + gl.vertex().position(0d, -radius, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); + gl.vertex().position(-radius/3, radius * 0.1, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); + gl.vertex().position(0d, 0d, 0d).color(north.redf(), north.greenf(), north.bluef(), north.alphaf()).end(); + gl.vertex().position(radius/3, radius * 0.1, 0d).color(northDark.redf(), northDark.greenf(), northDark.bluef(), northDark.alphaf()).end(); + gl.draw(); // South arrow - drawing.begin(TRIANGLE_FAN, POSITION_COLOR); - drawing.vertex().position(0, 0, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); - drawing.vertex().position(-radius/3, radius * 0.1, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); - drawing.vertex().position(0, radius, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); - drawing.vertex().position(radius/3, radius * 0.1, 0).color(southColor.redf(), southColor.greenf(), southColor.bluef(), southColor.alphaf()).end(); - drawing.draw(); - - glState.popViewMatrix(); + gl.startDrawing(TRIANGLE_FAN, POSITION_COLOR); + gl.vertex().position(0, 0, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); + gl.vertex().position(-radius/3, radius * 0.1, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); + gl.vertex().position(0, radius, 0).color(south.redf(), south.greenf(), south.bluef(), south.alphaf()).end(); + gl.vertex().position(radius/3, radius * 0.1, 0).color(southColor.redf(), southColor.greenf(), southColor.bluef(), southColor.alphaf()).end(); + gl.draw(); + + gl.popViewMatrix(); } @Override diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java index 10955e2f..0f4b575a 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java @@ -1,9 +1,8 @@ package fr.thesmyler.terramap.gui.widgets; import net.smyler.smylib.Identifier; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.GlState; -import net.smyler.smylib.gui.advanced.AdvancedDrawing; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.terramap.Terramap; import org.lwjgl.opengl.GL11; @@ -12,9 +11,9 @@ import net.minecraft.client.renderer.GlStateManager; import static net.smyler.smylib.Color.WHITE; -import static net.smyler.smylib.gui.advanced.DrawMode.QUADS; -import static net.smyler.smylib.gui.advanced.VertexFormat.POSITION_TEXTURE; -import static net.smyler.smylib.gui.advanced.VertexFormat.POSITION_TEXTURE_COLOR; +import static net.smyler.smylib.gui.gl.DrawMode.QUADS; +import static net.smyler.smylib.gui.gl.VertexFormat.POSITION_TEXTURE; +import static net.smyler.smylib.gui.gl.VertexFormat.POSITION_TEXTURE_COLOR; public class RibbonCompassWidget implements Widget { @@ -43,53 +42,51 @@ public RibbonCompassWidget(float x, float y, int z, float width) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { double blendBorder = 10d; // How many pixels to fade to alpha=0 on the sides double leftU = (double)(this.azimuth - 180) / 360 + (double)(this.textureWidth - this.width) / this.textureWidth / 2; double leftCU = leftU + blendBorder/this.textureWidth; double rightU = leftU + (double) this.width / this.textureWidth; double rightCU = rightU - blendBorder/this.textureWidth; - GlState glState = context.glState(); + GlContext gl = context.gl(); - glState.enableAlpha(); + gl.enableAlpha(); GlStateManager.enableBlend(); GlStateManager.shadeModel(GL11.GL_SMOOTH); - AdvancedDrawing drawing = context.advancedDrawing(); + gl.startDrawing(QUADS, POSITION_TEXTURE_COLOR); + gl.setTexture(COMPASS_BACKGROUND_TEXTURE); - drawing.begin(QUADS, POSITION_TEXTURE_COLOR); - drawing.texture(COMPASS_BACKGROUND_TEXTURE); + gl.vertex().position(x, y, 0d).texture(leftU, 0d).color(1f, 1f, 1f, 0f).end(); + gl.vertex().position(x, y + this.height, 0d).texture(leftU, 1d).color(1f, 1f, 1f, 0f).end(); + gl.vertex().position(x + blendBorder, y + this.height, 0d).texture(leftCU, 1f).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + blendBorder, y, 0d).texture(leftCU, 0f).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x, y, 0d).texture(leftU, 0d).color(1f, 1f, 1f, 0f).end(); - drawing.vertex().position(x, y + this.height, 0d).texture(leftU, 1d).color(1f, 1f, 1f, 0f).end(); - drawing.vertex().position(x + blendBorder, y + this.height, 0d).texture(leftCU, 1f).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + blendBorder, y, 0d).texture(leftCU, 0f).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + blendBorder, y, 0d).texture(leftCU, 0d).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + blendBorder, y + this.height, 0d).texture(leftCU, 1d).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + this.width - blendBorder, y + this.height, 0d).texture(rightCU, 1d).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + this.width - blendBorder, y, 0d).texture(rightCU, 0d).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + blendBorder, y, 0d).texture(leftCU, 0d).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + blendBorder, y + this.height, 0d).texture(leftCU, 1d).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + this.width - blendBorder, y + this.height, 0d).texture(rightCU, 1d).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + this.width - blendBorder, y, 0d).texture(rightCU, 0d).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + this.width - blendBorder, y, 0d).texture(rightCU, 0d).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + this.width - blendBorder, y + this.height, 0d).texture(rightCU, 1d).color(1f, 1f, 1f, 1f).end(); + gl.vertex().position(x + this.width, y + this.height, 0d).texture(rightU, 1d).color(1f, 1f, 1f, 0f).end(); + gl.vertex().position(x + this.width, y, 0d).texture(rightU, 0d).color(1f, 1f, 1f, 0f).end(); - drawing.vertex().position(x + this.width - blendBorder, y, 0d).texture(rightCU, 0d).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + this.width - blendBorder, y + this.height, 0d).texture(rightCU, 1d).color(1f, 1f, 1f, 1f).end(); - drawing.vertex().position(x + this.width, y + this.height, 0d).texture(rightU, 1d).color(1f, 1f, 1f, 0f).end(); - drawing.vertex().position(x + this.width, y, 0d).texture(rightU, 0d).color(1f, 1f, 1f, 0f).end(); + gl.draw(); - drawing.draw(); - - glState.setColor(WHITE); + gl.setColor(WHITE); double indX = x + (double)(this.width - this.indicatorWidth) / 2; double indY = y + (double)(this.height - this.indicatorHeight) / 2; - drawing.begin(QUADS, POSITION_TEXTURE); - drawing.texture(COMPASS_INDICATOR_TEXTURE); - drawing.vertex().position(indX, indY, 0d).texture(0d, 0d).end(); - drawing.vertex().position(indX, indY + this.indicatorHeight, 0d).texture(0d, 1d).end(); - drawing.vertex().position(indX + this.indicatorWidth, indY + this.indicatorHeight, 0d).texture(1d, 1d).end(); - drawing.vertex().position(indX + this.indicatorWidth, indY, 0d).texture(1d, 0d).end(); - drawing.draw(); + gl.startDrawing(QUADS, POSITION_TEXTURE); + gl.setTexture(COMPASS_INDICATOR_TEXTURE); + gl.vertex().position(indX, indY, 0d).texture(0d, 0d).end(); + gl.vertex().position(indX, indY + this.indicatorHeight, 0d).texture(0d, 1d).end(); + gl.vertex().position(indX + this.indicatorWidth, indY + this.indicatorHeight, 0d).texture(1d, 1d).end(); + gl.vertex().position(indX + this.indicatorWidth, indY, 0d).texture(1d, 0d).end(); + gl.draw(); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java index 36f5a3b8..097b405a 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java @@ -1,11 +1,11 @@ package fr.thesmyler.terramap.gui.widgets.map; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; import fr.thesmyler.terramap.MapContext; import fr.thesmyler.terramap.input.KeyBindings; -import net.smyler.smylib.gui.DrawContext; import net.smyler.terramap.util.geo.GeoPointReadOnly; import net.smyler.terramap.util.geo.WebMercatorUtil; import net.smyler.smylib.math.Mat2d; @@ -64,15 +64,15 @@ public InputLayer(MapWidget map) { protected void initialize() {} @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { if(this.isRotating) { // If we are processing rotation input, draw pentagons at the corresponding spot - context.glState().pushViewMatrix(); - context.glState().translate(x + this.rotatePosition.x, y + this.rotatePosition.y); + context.gl().pushViewMatrix(); + context.gl().translate(x + this.rotatePosition.x, y + this.rotatePosition.y); context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_OUTER); context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_INNER); - context.glState().popViewMatrix(); + context.gl().popViewMatrix(); } if (this.getMap().isDebugMode()) { diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapLayer.java index 04405a6b..6b3fdf04 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapLayer.java @@ -1,8 +1,8 @@ package fr.thesmyler.terramap.gui.widgets.map; import com.google.gson.JsonObject; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.GlState; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.gui.popups.Popup; @@ -202,8 +202,8 @@ void updateViewPorts() { * @param drawX the X coordinate the widget is supposed to be drawn at * @param drawY the Y coordinate the widget is supposed to be drawn at */ - protected void applyRotationGl(DrawContext context, float drawX, float drawY) { - GlState gl = context.glState(); + protected void applyRotationGl(UiDrawContext context, float drawX, float drawY) { + GlContext gl = context.gl(); gl.translate(drawX + this.map.getWidth() / 2, drawY + this.map.getHeight() / 2); gl.rotate(this.rotation); gl.translate(-this.renderSpaceDimensionsHalf.x, -this.renderSpaceDimensionsHalf.y); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapWidget.java index 08827eb7..06ad10f2 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/MapWidget.java @@ -7,7 +7,7 @@ import fr.thesmyler.terramap.gui.widgets.map.layer.RasterMapLayer; import fr.thesmyler.terramap.maps.SavedLayerState; import fr.thesmyler.terramap.maps.SavedMapState; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.Font; import net.smyler.smylib.math.DoubleRange; import net.smyler.smylib.math.Vec2dMutable; @@ -283,7 +283,7 @@ public void removeMarker(Marker marker) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { this.profiler.endSection(); // End rest of the screen section this.profiler.startSection("draw"); super.draw(context, x, y, mouseX, mouseY, hovered, focused, parent); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/ScaleIndicatorWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/ScaleIndicatorWidget.java index 2bfa9cc5..7aed5b8b 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/ScaleIndicatorWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/ScaleIndicatorWidget.java @@ -3,7 +3,7 @@ import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; import net.smyler.smylib.gui.widgets.Widget; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.terramap.util.geo.GeoPointMutable; import net.smyler.terramap.util.geo.WebMercatorUtil; @@ -79,7 +79,7 @@ public ScaleIndicatorWidget setVisibility(boolean yesNo) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { float barY = y + 5; String lengthstr = "-"; float barwidth = this.getWidth(); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/DistortionLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/DistortionLayer.java index 592fd752..d19f7475 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/DistortionLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/DistortionLayer.java @@ -1,12 +1,12 @@ package fr.thesmyler.terramap.gui.widgets.map.layer; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.gui.widgets.map.MapLayer; import fr.thesmyler.terramap.gui.widgets.map.MapWidget; -import net.smyler.smylib.gui.DrawContext; import net.smyler.terramap.util.geo.GeoPointMutable; import net.smyler.terramap.util.geo.WebMercatorUtil; import net.smyler.smylib.math.Vec2dMutable; @@ -35,12 +35,12 @@ protected void initialize() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { MapWidget map = (MapWidget) parent; GeographicProjection projection = TerramapClientContext.getContext().getProjection(); if(projection == null) return; map.getProfiler().startSection("layer-distortion"); - context.glState().pushViewMatrix(); + context.gl().pushViewMatrix(); this.applyRotationGl(context, x, y); double maxX = this.renderSpaceDimensions.x(); @@ -64,7 +64,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous } } - context.glState().popViewMatrix(); + context.gl().popViewMatrix(); map.getProfiler().endSection(); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/McChunksLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/McChunksLayer.java index 8decd780..5e0daa41 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/McChunksLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/McChunksLayer.java @@ -8,6 +8,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.FlexibleWidgetContainer; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; @@ -17,7 +18,6 @@ import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.gui.widgets.map.MapLayer; import fr.thesmyler.terramap.gui.widgets.map.MapWidget; -import net.smyler.smylib.gui.DrawContext; import net.smyler.smylib.gui.Font; import net.smyler.terramap.Terramap; import net.smyler.terramap.util.geo.GeoPointImmutable; @@ -132,7 +132,7 @@ private void loadColor(JsonObject object, Consumer setter) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { MapWidget map = (MapWidget)parent; GeographicProjection projection = TerramapClientContext.getContext().getProjection(); if(projection == null) return; @@ -162,7 +162,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous return; } - context.glState().pushViewMatrix(); + context.gl().pushViewMatrix(); this.applyRotationGl(context, x, y); @@ -184,11 +184,11 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous } this.cache.cycle(); - context.glState().popViewMatrix(); + context.gl().popViewMatrix(); map.getProfiler().endSection(); } - private void renderGrid(DrawContext context, float x, float y, int discriminator, long tileSize, Color color, float lineWidth) { + private void renderGrid(UiDrawContext context, float x, float y, int discriminator, long tileSize, Color color, float lineWidth) { final int maxTiles = 100; // Maximum drawing iterations, for safety @@ -241,7 +241,7 @@ private void renderGrid(DrawContext context, float x, float y, int discriminator } } - private void renderTile(DrawContext context, float x, float y, int discriminator, Color color, float lineWidth, boolean[] loopingConditions) { + private void renderTile(UiDrawContext context, float x, float y, int discriminator, Color color, float lineWidth, boolean[] loopingConditions) { try { for(int i=0; i controller) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { context.drawSprite(x, y, hovered ? SPRITE_HOVERED : SPRITE); } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractLivingMarker.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractLivingMarker.java index f75f84be..aded28df 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractLivingMarker.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractLivingMarker.java @@ -1,6 +1,7 @@ package fr.thesmyler.terramap.gui.widgets.markers.markers.entities; -import net.smyler.smylib.gui.GlState; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; import fr.thesmyler.smylibgui.util.RenderUtil; @@ -9,7 +10,6 @@ import fr.thesmyler.terramap.gui.widgets.map.MapWidget; import fr.thesmyler.terramap.gui.widgets.markers.controllers.MarkerController; import fr.thesmyler.terramap.gui.widgets.markers.markers.AbstractMovingMarker; -import net.smyler.smylib.gui.DrawContext; import net.smyler.terramap.Terramap; import net.smyler.terramap.util.geo.GeoPointImmutable; import net.buildtheearth.terraplusplus.projection.GeographicProjection; @@ -43,17 +43,17 @@ public AbstractLivingMarker(MarkerController controller, float width, float h } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { - GlState glState = context.glState(); + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + GlContext gl = context.gl(); boolean drawName = hovered; if(parent instanceof MapWidget) { MapWidget map = (MapWidget) parent; drawName = drawName && !map.getContext().equals(MapContext.MINIMAP); } - glState.enableAlpha(); + gl.enableAlpha(); if(hovered) context.drawRectangle(x +1, y +1, x + 1 + this.width, y + 1 + this.height, Color.LIGHT_OVERLAY); Minecraft.getMinecraft().getTextureManager().bindTexture(this.texture); - glState.setColor(WHITE); + gl.setColor(WHITE); GlStateManager.enableBlend(); RenderUtil.drawModalRectWithCustomSizedTexture(x, y, this.u, this.v, this.width, this.height, this.textureWidth, this.textureHeight); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java index 1b25e86f..af039949 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java @@ -1,7 +1,7 @@ package fr.thesmyler.terramap.gui.widgets.markers.markers.entities; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.GlState; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.GlContext; import org.lwjgl.opengl.GL11; import net.smyler.smylib.gui.containers.WidgetContainer; @@ -30,8 +30,8 @@ public AbstractPlayerMarker(MarkerController controller, int downscaleFactor) } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { - GlState glState = context.glState(); + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + GlContext glState = context.gl(); boolean drawName = this.showName(hovered); float textureSize = 128f / this.downScaleFactor; @@ -45,7 +45,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous azimuth += ((MapWidget)parent).getController().getRotation(); } - GlState gl = context.glState(); + GlContext gl = context.gl(); gl.pushViewMatrix(); gl.translate(x + this.width / 2, y + this.height / 2); @@ -53,7 +53,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous GlStateManager.disableTexture2D(); GlStateManager.enableBlend(); - context.glState().disableAlpha(); + context.gl().disableAlpha(); GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); GlStateManager.shadeModel(7425); Tessellator tess = Tessellator.getInstance(); @@ -66,7 +66,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous tess.draw(); GlStateManager.shadeModel(7424); GlStateManager.disableBlend(); - context.glState().enableAlpha(); + context.gl().enableAlpha(); GlStateManager.enableTexture2D(); GlStateManager.popMatrix(); } diff --git a/smylib/core/src/main/java/net/smyler/smylib/game/GameClient.java b/smylib/core/src/main/java/net/smyler/smylib/game/GameClient.java index ed3de553..58d9c9ec 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/game/GameClient.java +++ b/smylib/core/src/main/java/net/smyler/smylib/game/GameClient.java @@ -1,6 +1,6 @@ package net.smyler.smylib.game; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.Font; import net.smyler.smylib.gui.popups.Popup; import net.smyler.smylib.gui.screen.Screen; @@ -80,7 +80,7 @@ default Font smallestFont() { return this.defaultFont().withScale(1f / this.scaleFactor()); } - DrawContext guiDrawContext(); + UiDrawContext guiDrawContext(); /** * Get the sprites for this client. diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java b/smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java similarity index 96% rename from smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java index 04f9ba6d..c997f381 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/DrawContext.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java @@ -2,16 +2,17 @@ import net.smyler.smylib.Color; import net.smyler.smylib.Identifier; -import net.smyler.smylib.gui.advanced.AdvancedDrawing; +import net.smyler.smylib.gui.gl.GlContext; +import net.smyler.smylib.gui.gl.Scissor; import net.smyler.smylib.gui.sprites.Sprite; import java.awt.image.BufferedImage; -public interface DrawContext { +public interface UiDrawContext { Scissor scissor(); - GlState glState(); + GlContext gl(); default void drawRectangle(double z, double xLeft, double yTop, double xRight, double yBottom, Color color) { this.drawGradientRectangle(z, xLeft, yTop, xRight, yBottom, color, color, color, color); @@ -85,6 +86,4 @@ default void drawSpriteCropped(double x, double y, Sprite sprite, double leftCro void unloadDynamicTexture(Identifier texture); - AdvancedDrawing advancedDrawing(); - } diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java b/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java deleted file mode 100644 index 5f14d770..00000000 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/AdvancedDrawing.java +++ /dev/null @@ -1,14 +0,0 @@ -package net.smyler.smylib.gui.advanced; - -import net.smyler.smylib.Color; -import net.smyler.smylib.Identifier; - -public interface AdvancedDrawing { - - void begin(DrawMode mode, VertexFormat components); - void texture(Identifier identifier); - void color(Color color); - VertexBuilder vertex(); - void draw(); - -} diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/RootContainer.java b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/RootContainer.java index 36ce6f54..c0d8af27 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/RootContainer.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/RootContainer.java @@ -1,7 +1,7 @@ package net.smyler.smylib.gui.containers; import net.smyler.smylib.gui.widgets.Widget; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import org.jetbrains.annotations.Nullable; @@ -22,7 +22,7 @@ public RootContainer() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, @Nullable WidgetContainer alwaysNull) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, @Nullable WidgetContainer alwaysNull) { super.draw(context, x, y, mouseX, mouseY, screenHovered, screenFocused, null); Widget hoveredWidget = this.getHoveredWidget(); boolean mouseMoved = mouseX != this.lastRenderMouseX && mouseY != this.lastRenderMouseY; diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/ScrollableWidgetContainer.java b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/ScrollableWidgetContainer.java index cd5d15c1..2c4a51b8 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/ScrollableWidgetContainer.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/ScrollableWidgetContainer.java @@ -6,7 +6,7 @@ import net.smyler.smylib.gui.widgets.ScrollbarWidget.ScrollbarOrientation; import net.smyler.smylib.gui.widgets.buttons.SpriteButtonWidget; import net.smyler.smylib.gui.widgets.buttons.SpriteButtonWidget.ButtonSprites; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import static net.smyler.smylib.Preconditions.checkArgument; @@ -92,7 +92,7 @@ public void updateScrollbars() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { context.drawRectangle(x, y, x + this.getWidth(), y + this.getHeight(), this.backgroundColor); super.draw(context, x, y, mouseX, mouseY, screenHovered, screenFocused, parent); context.drawRectangleWithContours(x, y, x + this.getWidth(), y + this.getHeight(), Color.TRANSPARENT, this.contourSize, this.contourColor); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/SlidingPanelWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/SlidingPanelWidget.java index d9069842..568edcbd 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/SlidingPanelWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/SlidingPanelWidget.java @@ -3,7 +3,7 @@ import net.smyler.smylib.Animation; import net.smyler.smylib.Animation.AnimationState; import net.smyler.smylib.Color; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import org.jetbrains.annotations.Nullable; public class SlidingPanelWidget extends FlexibleWidgetContainer { @@ -30,7 +30,7 @@ public SlidingPanelWidget(int z, long delay) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, @Nullable WidgetContainer parent){ + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, @Nullable WidgetContainer parent){ context.drawRectangleWithContours(x, y, x + this.getWidth(), y + this.getHeight(), this.backgroundColor, this.contourSize, this.contourColor); super.draw(context, x, y, mouseX, mouseY, hovered, focused, parent); this.mainAnimation.update(); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/WidgetContainer.java b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/WidgetContainer.java index 3371a20e..5ea411a1 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/containers/WidgetContainer.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/containers/WidgetContainer.java @@ -5,8 +5,8 @@ import java.util.List; import java.util.TreeSet; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.Scissor; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.Scissor; import net.smyler.smylib.gui.screen.Screen; import net.smyler.smylib.gui.widgets.Widget; import net.smyler.smylib.gui.widgets.MenuWidget; @@ -347,7 +347,7 @@ public int getZ() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, @Nullable WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, @Nullable WidgetContainer parent) { Scissor scissor = context.scissor(); if(this.doScissor) { scissor.push(); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/ColorLogic.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/ColorLogic.java similarity index 93% rename from smylib/core/src/main/java/net/smyler/smylib/gui/ColorLogic.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/gl/ColorLogic.java index 920add86..942213b7 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/ColorLogic.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/ColorLogic.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui; +package net.smyler.smylib.gui.gl; public enum ColorLogic { CLEAR, // 0 diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/DrawMode.java similarity index 74% rename from smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/gl/DrawMode.java index 10890334..45b18bf1 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/DrawMode.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/DrawMode.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui.advanced; +package net.smyler.smylib.gui.gl; public enum DrawMode { LINES, diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/GlState.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java similarity index 61% rename from smylib/core/src/main/java/net/smyler/smylib/gui/GlState.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java index 28129bcc..5f32e10e 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/GlState.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java @@ -1,8 +1,9 @@ -package net.smyler.smylib.gui; +package net.smyler.smylib.gui.gl; import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; -public interface GlState { +public interface GlContext { void enableAlpha(); @@ -12,6 +13,8 @@ public interface GlState { Color getColor(); + void setTexture(Identifier texture); + void enableColorLogic(ColorLogic colorLogic); void disableColorLogic(); @@ -26,4 +29,9 @@ public interface GlState { void popViewMatrix(); + void startDrawing(DrawMode mode, VertexFormat format); + + VertexBuilder vertex(); + + void draw(); } diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/Scissor.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/Scissor.java similarity index 98% rename from smylib/core/src/main/java/net/smyler/smylib/gui/Scissor.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/gl/Scissor.java index b01e5f4d..4e66fab3 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/Scissor.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/Scissor.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui; +package net.smyler.smylib.gui.gl; /** * Limits rendering to a specific part of the screen. diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/VertexBuilder.java similarity index 88% rename from smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/gl/VertexBuilder.java index 3ec75e08..b6ec5b7d 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexBuilder.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/VertexBuilder.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui.advanced; +package net.smyler.smylib.gui.gl; import net.smyler.smylib.Color; diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/VertexFormat.java similarity index 91% rename from smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java rename to smylib/core/src/main/java/net/smyler/smylib/gui/gl/VertexFormat.java index 49b1fb23..0139a7d1 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/advanced/VertexFormat.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/VertexFormat.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui.advanced; +package net.smyler.smylib.gui.gl; public enum VertexFormat { POSITION(true, false, false), diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/popups/Popup.java b/smylib/core/src/main/java/net/smyler/smylib/gui/popups/Popup.java index bd376ea5..161e59cd 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/popups/Popup.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/popups/Popup.java @@ -2,7 +2,7 @@ import net.smyler.smylib.Color; import net.smyler.smylib.game.Key; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.WidgetContainer; import org.jetbrains.annotations.Nullable; @@ -33,7 +33,7 @@ public Popup(float width, float height) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { float right = x + this.getWidth(); float bottom = y + this.getHeight(); context.drawRectangle(x, y, right, bottom, this.backgroundColor); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ColorPickerWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ColorPickerWidget.java index 49f4420c..c8caf54b 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ColorPickerWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ColorPickerWidget.java @@ -3,7 +3,7 @@ import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; import net.smyler.smylib.gui.widgets.text.TextFieldWidget; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.Font; import java.util.Optional; @@ -76,7 +76,7 @@ public void setColor(Color color) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { super.draw(context, x, y, mouseX, mouseY, hovered, focused, parent); float width = this.getWidth(); float height = this.getHeight(); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/MenuWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/MenuWidget.java index e56a45ae..d11ef098 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/MenuWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/MenuWidget.java @@ -3,7 +3,7 @@ import java.util.ArrayList; import java.util.List; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.WidgetContainer; import org.jetbrains.annotations.Nullable; @@ -51,7 +51,7 @@ public MenuWidget(int z, Font font) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean mouseHoverMenu, boolean hasFocus, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean mouseHoverMenu, boolean hasFocus, WidgetContainer parent) { this.mainAnimation.update(); this.hoverAnimation.update(); float width = this.getWidth(); @@ -60,7 +60,7 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous float lh = fh + padding * 2; float sh = 3; float dw = this.font.computeWidth(" >"); - context.glState().enableAlpha(); + context.gl().enableAlpha(); Color separatorColor = this.mainAnimation.fadeColor(this.separatorColor); Color borderColor = this.mainAnimation.fadeColor(this.borderColor); Color backgroundColor = this.mainAnimation.fadeColor(this.backgroundColor); @@ -116,7 +116,7 @@ else if(hovered || (entry.getSubMenu() != null && entry.getSubMenu().equals(this ty += sh; } } - context.glState().disableAlpha(); + context.gl().disableAlpha(); } @Override diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ScrollbarWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ScrollbarWidget.java index 6da2111f..2804363f 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ScrollbarWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/ScrollbarWidget.java @@ -4,7 +4,7 @@ import net.smyler.smylib.Color; import net.smyler.smylib.gui.widgets.buttons.SpriteButtonWidget; import net.smyler.smylib.gui.widgets.buttons.SpriteButtonWidget.ButtonSprites; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import static net.smyler.smylib.Preconditions.checkArgument; import static net.smyler.smylib.SmyLib.getGameClient; @@ -91,7 +91,7 @@ public boolean onDoubleClick(float mouseX, float mouseY, int mouseButton, Widget } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean screenHovered, boolean screenFocused, WidgetContainer parent) { float width = this.getWidth(); float height = this.getHeight(); context.drawRectangle(x, y, x + width, y + height, BAR_BG_COLOR); @@ -218,7 +218,7 @@ public void onMouseDragged(float mouseX, float mouseY, float dX, float dY, int b } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { Color bgcolor = hovered || focused ? DRAG_BG_COLOR_HOVER: DRAG_BG_COLOR; float height = this.getHeight(); context.drawRectangle(x, y, x + this.getWidth(), y + height, bgcolor); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/SpriteWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/SpriteWidget.java index 0355ca12..eb4b1878 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/SpriteWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/SpriteWidget.java @@ -2,7 +2,7 @@ import net.smyler.smylib.gui.sprites.Sprite; import net.smyler.smylib.gui.containers.WidgetContainer; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; public class SpriteWidget extends AbstractSolidWidget { @@ -15,7 +15,7 @@ public SpriteWidget(float x, float y, int z, Sprite sprite) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { context.drawSprite(x, y, this.sprite); } diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/Widget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/Widget.java index c096ad9d..77a0b640 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/Widget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/Widget.java @@ -1,7 +1,7 @@ package net.smyler.smylib.gui.widgets; import net.smyler.smylib.game.Key; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.WidgetContainer; import org.jetbrains.annotations.Nullable; @@ -54,7 +54,7 @@ public interface Widget { * @param focused indicates whether this widget has its parent's focus (it will get keystrokes and so on) * @param parent the parent Screen */ - void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, @Nullable WidgetContainer parent); + void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, @Nullable WidgetContainer parent); /** * If this returns false, this widget will not be rendered and or notified of user actions diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/AbstractButtonWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/AbstractButtonWidget.java index d2445896..6b27f05d 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/AbstractButtonWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/AbstractButtonWidget.java @@ -1,6 +1,6 @@ package net.smyler.smylib.gui.widgets.buttons; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.widgets.AbstractSolidWidget; import org.jetbrains.annotations.Nullable; @@ -30,7 +30,7 @@ public AbstractButtonWidget(float x, float y, int z, float width, float height) } @Override - public abstract void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent); + public abstract void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent); @Override public boolean onClick(float mouseX, float mouseY, int mouseButton, WidgetContainer parent) { diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/SpriteButtonWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/SpriteButtonWidget.java index 781b140c..77a41614 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/SpriteButtonWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/SpriteButtonWidget.java @@ -1,6 +1,6 @@ package net.smyler.smylib.gui.widgets.buttons; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.sprites.Sprite; import org.jetbrains.annotations.Nullable; @@ -71,7 +71,7 @@ public SpriteButtonWidget(int z, ButtonSprites sprites) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { Sprite sprite = this.sprite; if(!this.isEnabled()) { sprite = this.spriteDisabled; diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/TextButtonWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/TextButtonWidget.java index 678e3eb1..8096e3c7 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/TextButtonWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/TextButtonWidget.java @@ -5,7 +5,7 @@ import net.smyler.smylib.gui.sprites.SpriteLibrary; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import static net.smyler.smylib.SmyLib.getGameClient; @@ -52,7 +52,7 @@ public TextButtonWidget(int z, String str) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { Sprite sprite = this.buttonSprite; Color textColor = this.enabledTextColor; diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/ToggleButtonWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/ToggleButtonWidget.java index 35cf9692..d1e9a3ac 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/ToggleButtonWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/buttons/ToggleButtonWidget.java @@ -3,7 +3,7 @@ import java.util.function.Consumer; import net.smyler.smylib.gui.containers.WidgetContainer; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.sprites.Sprite; import static net.smyler.smylib.gui.sprites.SmyLibSprites.*; @@ -100,7 +100,7 @@ public ToggleButtonWidget(int z, boolean startValue) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { Sprite sprite; if(!this.isEnabled()) { if(this.getState()) { diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/sliders/AbstractSliderWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/sliders/AbstractSliderWidget.java index ae5f3aa8..524295fb 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/sliders/AbstractSliderWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/sliders/AbstractSliderWidget.java @@ -1,6 +1,6 @@ package net.smyler.smylib.gui.widgets.sliders; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.sprites.Sprite; import org.jetbrains.annotations.Nullable; @@ -115,7 +115,7 @@ public void onKeyTyped(char typedChar, @Nullable Key key, @Nullable WidgetContai @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean hasFocus, WidgetContainer parent) { double leftWidth = this.width / 2; double splitHeight = min(10, this.height / 2); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextFieldWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextFieldWidget.java index c8eb4b20..82ea08fb 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextFieldWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextFieldWidget.java @@ -5,6 +5,8 @@ import net.smyler.smylib.Identifier; import net.smyler.smylib.gui.*; +import net.smyler.smylib.gui.gl.ColorLogic; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.smylib.gui.sprites.Sprite; import org.jetbrains.annotations.Nullable; @@ -120,9 +122,9 @@ public TextFieldWidget(int z) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { - GlState glState = context.glState(); + GlContext gl = context.gl(); this.cursorAnimation.update(); @@ -191,14 +193,14 @@ public void draw(DrawContext context, float x, float y, float mouseX, float mous } - private void drawSelectionHighlight(DrawContext context, float x, float y, float x1, float y1, float x2, float y2) { + private void drawSelectionHighlight(UiDrawContext context, float x, float y, float x1, float y1, float x2, float y2) { float xRight = Math.max(x1, x2); float yBottom = Math.max(y1, y2); float xLeft = Math.min(x1, x2); float yTop = Math.min(y1, y2); xLeft = Math.min(xLeft, x + this.getEffectiveWidth()); xRight = Math.min(xRight, x + this.getEffectiveWidth()); - GlState state = context.glState(); + GlContext state = context.gl(); state.enableColorLogic(ColorLogic.OR_REVERSE); context.drawRectangle(xLeft, yTop, xRight, yBottom, BLUE); state.disableColorLogic(); diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextWidget.java b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextWidget.java index 33487149..1ae163ef 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextWidget.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/widgets/text/TextWidget.java @@ -1,6 +1,6 @@ package net.smyler.smylib.gui.widgets.text; -import net.smyler.smylib.gui.DrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.text.ImmutableText; import net.smyler.smylib.text.Text; import org.jetbrains.annotations.Nullable; @@ -70,8 +70,8 @@ public TextWidget(int z, Font font) { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { - context.glState().enableAlpha(); + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + context.gl().enableAlpha(); float w = this.getWidth(); float h = this.getHeight(); context.drawRectangle(x, y, x + w, y + h, this.backgroundColor); diff --git a/smylib/forge/src/main/java/net/smyler/smylib/game/WrappedMinecraft.java b/smylib/forge/src/main/java/net/smyler/smylib/game/WrappedMinecraft.java index 106d2ce6..6cfdd038 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/game/WrappedMinecraft.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/game/WrappedMinecraft.java @@ -34,7 +34,7 @@ public class WrappedMinecraft implements GameClient { private final SoundSystem soundSystem = new MinecraftSoundSystem(); private final Translator translator = new I18nTranslator(); private final Font font = new ReflectedFontRenderer(1f, 0.5f); - private final DrawContext drawContext = new Lwjgl2DrawContext(); + private final UiDrawContext uiDrawContext = new Lwjgl2UiDrawContext(); private final SpriteLibrary sprites = new LegacyVanillaSprites(); private WrappedVanillaScreen lastAccessedVanillaScreen = null; @@ -120,8 +120,8 @@ public Font defaultFont() { } @Override - public DrawContext guiDrawContext() { - return this.drawContext; + public UiDrawContext guiDrawContext() { + return this.uiDrawContext; } @Override diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java similarity index 94% rename from smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java rename to smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java index 8da025cf..7ed01965 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2DrawContext.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java @@ -9,8 +9,7 @@ import net.minecraft.util.ResourceLocation; import net.smyler.smylib.Color; import net.smyler.smylib.Identifier; -import net.smyler.smylib.gui.advanced.AdvancedDrawing; -import net.smyler.smylib.gui.advanced.TesselatorAdvancedDrawing; +import net.smyler.smylib.gui.gl.*; import net.smyler.smylib.gui.sprites.Sprite; import org.lwjgl.opengl.GL11; @@ -22,11 +21,10 @@ import static net.smyler.smylib.Preconditions.checkArgument; import static net.smyler.smylib.SmyLib.getGameClient; -public class Lwjgl2DrawContext implements DrawContext { +public class Lwjgl2UiDrawContext implements UiDrawContext { private final Scissor scissor = new Gl11Scissor(); - private final GlState glState = new LwjglState(); - private final AdvancedDrawing advancedDrawing = new TesselatorAdvancedDrawing(); + private final GlContext gl = new Lwjgl2GlContext(); private final AtomicInteger dynamicTextureCounter = new AtomicInteger(0); @Override @@ -35,8 +33,8 @@ public Scissor scissor() { } @Override - public GlState glState() { - return this.glState; + public GlContext gl() { + return this.gl; } @Override @@ -158,11 +156,6 @@ public void unloadDynamicTexture(Identifier texture) { getMinecraft().getTextureManager().deleteTexture(new ResourceLocation(texture.namespace, texture.path)); } - @Override - public AdvancedDrawing advancedDrawing() { - return this.advancedDrawing; - } - private void drawMultiPointsGeometry(int glType, double z, Color color, double... points) { checkArgument(points.length % 2 == 0, "An even number of coordinates is required"); GlStateManager.enableAlpha(); diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/LwjglState.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/LwjglState.java deleted file mode 100644 index 5649d9b7..00000000 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/LwjglState.java +++ /dev/null @@ -1,106 +0,0 @@ -package net.smyler.smylib.gui; - -import net.minecraft.client.renderer.GlStateManager; -import net.smyler.smylib.Color; -import org.lwjgl.opengl.GL11; - -public class LwjglState implements GlState { - @Override - public void enableAlpha() { - GlStateManager.enableAlpha(); - } - - @Override - public void disableAlpha() { - GlStateManager.disableAlpha(); - } - - @Override - public void setColor(Color color) { - GlStateManager.color( - color.redf(), - color.greenf(), - color.bluef(), - color.alphaf() - ); - } - - @Override - public Color getColor() { - return new Color(GL11.glGetInteger(GL11.GL_CURRENT_COLOR)); - } - - @Override - public void enableColorLogic(ColorLogic colorLogic) { - GlStateManager.LogicOp op = this.getGlColorLogic(colorLogic); - GlStateManager.colorLogicOp(op); - GlStateManager.enableColorLogic(); - } - - private GlStateManager.LogicOp getGlColorLogic(ColorLogic logic) { - switch (logic) { - case CLEAR: - return GlStateManager.LogicOp.CLEAR; - case SET: - return GlStateManager.LogicOp.SET; - case COPY: - return GlStateManager.LogicOp.COPY; - case COPY_INVERTED: - return GlStateManager.LogicOp.COPY_INVERTED; - case NOOP: - return GlStateManager.LogicOp.NOOP; - case INVERT: - return GlStateManager.LogicOp.INVERT; - case AND: - return GlStateManager.LogicOp.AND; - case NAND: - return GlStateManager.LogicOp.NAND; - case OR: - return GlStateManager.LogicOp.OR; - case NOR: - return GlStateManager.LogicOp.NOR; - case XOR: - return GlStateManager.LogicOp.XOR; - case EQUIV: - return GlStateManager.LogicOp.EQUIV; - case AND_REVERSE: - return GlStateManager.LogicOp.AND_REVERSE; - case OR_REVERSE: - return GlStateManager.LogicOp.OR_REVERSE; - case OR_INVERTED: - return GlStateManager.LogicOp.OR_INVERTED; - } - throw new IllegalStateException("Illegal enum value"); - } - - @Override - public void disableColorLogic() { - GlStateManager.disableColorLogic(); - } - - @Override - public void pushViewMatrix() { - GlStateManager.pushMatrix(); - } - - @Override - public void rotate(double angle) { - GlStateManager.rotate((float)angle, 0, 0, 1f); - } - - @Override - public void translate(double x, double y) { - GlStateManager.translate(x, y, 0); - } - - @Override - public void scale(double x, double y) { - GlStateManager.scale(x, y, 1d); - } - - @Override - public void popViewMatrix() { - GlStateManager.popMatrix(); - } - -} diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/Gl11Scissor.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Gl11Scissor.java similarity index 99% rename from smylib/forge/src/main/java/net/smyler/smylib/gui/Gl11Scissor.java rename to smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Gl11Scissor.java index 49138360..a9285c93 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/Gl11Scissor.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Gl11Scissor.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui; +package net.smyler.smylib.gui.gl; import net.smyler.smylib.game.GameClient; import org.lwjgl.opengl.GL11; diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java similarity index 57% rename from smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java rename to smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java index 868d2a2a..e8ff3c38 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/advanced/TesselatorAdvancedDrawing.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui.advanced; +package net.smyler.smylib.gui.gl; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; @@ -13,7 +13,7 @@ import static net.minecraft.client.Minecraft.getMinecraft; import static net.smyler.smylib.Preconditions.checkState; -public class TesselatorAdvancedDrawing implements AdvancedDrawing { +public class Lwjgl2GlContext implements GlContext { private final Tessellator tessellator = Tessellator.getInstance(); private final BufferBuilder bufferBuilder = this.tessellator.getBuffer(); @@ -22,7 +22,110 @@ public class TesselatorAdvancedDrawing implements AdvancedDrawing { private VertexFormat currentFormat = null; @Override - public void begin(DrawMode mode, VertexFormat format) { + public void enableAlpha() { + GlStateManager.enableAlpha(); + } + + @Override + public void disableAlpha() { + GlStateManager.disableAlpha(); + } + + @Override + public void setColor(Color color) { + GlStateManager.color( + color.redf(), + color.greenf(), + color.bluef(), + color.alphaf() + ); + } + + @Override + public Color getColor() { + return new Color(GL11.glGetInteger(GL11.GL_CURRENT_COLOR)); + } + + @Override + public void setTexture(Identifier identifier) { + this.textureManager.bindTexture(new ResourceLocation(identifier.namespace, identifier.path)); + } + + @Override + public void enableColorLogic(ColorLogic colorLogic) { + GlStateManager.LogicOp op = this.getGlColorLogic(colorLogic); + GlStateManager.colorLogicOp(op); + GlStateManager.enableColorLogic(); + } + + private GlStateManager.LogicOp getGlColorLogic(ColorLogic logic) { + switch (logic) { + case CLEAR: + return GlStateManager.LogicOp.CLEAR; + case SET: + return GlStateManager.LogicOp.SET; + case COPY: + return GlStateManager.LogicOp.COPY; + case COPY_INVERTED: + return GlStateManager.LogicOp.COPY_INVERTED; + case NOOP: + return GlStateManager.LogicOp.NOOP; + case INVERT: + return GlStateManager.LogicOp.INVERT; + case AND: + return GlStateManager.LogicOp.AND; + case NAND: + return GlStateManager.LogicOp.NAND; + case OR: + return GlStateManager.LogicOp.OR; + case NOR: + return GlStateManager.LogicOp.NOR; + case XOR: + return GlStateManager.LogicOp.XOR; + case EQUIV: + return GlStateManager.LogicOp.EQUIV; + case AND_REVERSE: + return GlStateManager.LogicOp.AND_REVERSE; + case OR_REVERSE: + return GlStateManager.LogicOp.OR_REVERSE; + case OR_INVERTED: + return GlStateManager.LogicOp.OR_INVERTED; + } + throw new IllegalStateException("Illegal enum value"); + } + + @Override + public void disableColorLogic() { + GlStateManager.disableColorLogic(); + } + + @Override + public void pushViewMatrix() { + GlStateManager.pushMatrix(); + } + + @Override + public void rotate(double angle) { + GlStateManager.rotate((float)angle, 0, 0, 1f); + } + + @Override + public void translate(double x, double y) { + GlStateManager.translate(x, y, 0); + } + + @Override + public void scale(double x, double y) { + GlStateManager.scale(x, y, 1d); + } + + @Override + public void popViewMatrix() { + GlStateManager.popMatrix(); + } + + @Override + public void startDrawing(DrawMode mode, VertexFormat format) { int glMode; net.minecraft.client.renderer.vertex.VertexFormat glVertexFormat; switch (mode) { @@ -67,16 +170,6 @@ public void begin(DrawMode mode, VertexFormat format) { this.currentFormat = format; } - @Override - public void texture(Identifier identifier) { - this.textureManager.bindTexture(new ResourceLocation(identifier.namespace, identifier.path)); - } - - @Override - public void color(Color color) { - GlStateManager.color(color.redf(), color.greenf(), color.bluef(), color.alphaf()); - } - @Override public VertexBuilder vertex() { checkState(this.currentFormat != null, "Not building!"); @@ -85,6 +178,7 @@ public VertexBuilder vertex() { @Override public void draw() { + checkState(this.currentFormat != null, "Not building!"); if (this.currentFormat.texture) { GlStateManager.enableTexture2D(); GlStateManager.shadeModel(GL11.GL_FLAT); @@ -136,15 +230,16 @@ public VertexBuilder texture(double u, double v) { @Override public void end() { - VertexFormat format = TesselatorAdvancedDrawing.this.currentFormat; - TesselatorAdvancedDrawing.this.bufferBuilder.pos(this.x, this.y, this.z); + VertexFormat format = Lwjgl2GlContext.this.currentFormat; + BufferBuilder builder = Lwjgl2GlContext.this.bufferBuilder; + builder.pos(this.x, this.y, this.z); if (format.texture) { - TesselatorAdvancedDrawing.this.bufferBuilder.tex(this.u, this.v); + builder.tex(this.u, this.v); } if (format.color) { - TesselatorAdvancedDrawing.this.bufferBuilder.color(this.r, this.g, this.b, this.a); + builder.color(this.r, this.g, this.b, this.a); } - TesselatorAdvancedDrawing.this.bufferBuilder.endVertex(); + builder.endVertex(); } } diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/GuiScreenProxy.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/GuiScreenProxy.java index e2adcb0b..58cf4c65 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/GuiScreenProxy.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/GuiScreenProxy.java @@ -5,8 +5,8 @@ import net.smyler.smylib.game.GameClient; import net.smyler.smylib.game.Key; import net.smyler.smylib.game.Mouse; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.Scissor; +import net.smyler.smylib.gui.UiDrawContext; +import net.smyler.smylib.gui.gl.Scissor; import org.jetbrains.annotations.NotNull; import static net.smyler.smylib.SmyLib.getGameClient; @@ -41,7 +41,7 @@ protected void keyTyped(char typedChar, int keyCode) { public void drawScreen(int nopX, int nopY, float partialTicks) { GameClient client = getGameClient(); Mouse mouse = client.mouse(); - DrawContext context = client.guiDrawContext(); + UiDrawContext context = client.guiDrawContext(); Scissor scissor = context.scissor(); scissor.push(); // We need to make sure everything is visible diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/PopupScreenImplementation.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/PopupScreenImplementation.java index 437d583d..db35d3f9 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/PopupScreenImplementation.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/screen/PopupScreenImplementation.java @@ -1,12 +1,11 @@ package net.smyler.smylib.gui.screen; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.gui.popups.Popup; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; -import net.smyler.smylib.gui.DrawContext; - import static net.smyler.smylib.gui.popups.PopupImplementationProxy.setPopupPosition; @@ -26,7 +25,7 @@ public void init() { } @Override - public void draw(DrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { + public void draw(UiDrawContext context, float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) { if(this.other != null) this.other.drawScreen((int) mouseX, (int) mouseY, 0); context.drawRectangle(0, 0, this.getWidth(), this.getHeight(), this.getPopup().getShadingColor()); super.draw(context, x, y, mouseX, mouseY, hovered, focused, parent); diff --git a/smylib/testing/src/main/java/net/smyler/smylib/game/TestGameClient.java b/smylib/testing/src/main/java/net/smyler/smylib/game/TestGameClient.java index 1835dccc..82b521c2 100644 --- a/smylib/testing/src/main/java/net/smyler/smylib/game/TestGameClient.java +++ b/smylib/testing/src/main/java/net/smyler/smylib/game/TestGameClient.java @@ -1,9 +1,8 @@ package net.smyler.smylib.game; -import net.smyler.smylib.gui.DrawContext; -import net.smyler.smylib.gui.DummyDrawContext; -import net.smyler.smylib.gui.DummyFont; -import net.smyler.smylib.gui.Font; +import net.smyler.smylib.gui.*; +import net.smyler.smylib.gui.DummyUiDrawContext; +import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.popups.Popup; import net.smyler.smylib.gui.screen.PopupScreen; import net.smyler.smylib.gui.screen.Screen; @@ -37,7 +36,7 @@ public class TestGameClient implements GameClient { private final SoundSystem soundSystem = new DummySoundSystem(); private final Translator translator = new DummyTranslator(); private final Font font = new DummyFont(1f, 1f); - private final DrawContext drawContext = new DummyDrawContext(); + private final UiDrawContext uiDrawContext = new DummyUiDrawContext(); private final SpriteLibrary spriteLibrary = new SpriteLibrary(); private final Path gameDirectory; @@ -153,8 +152,8 @@ public Font defaultFont() { } @Override - public DrawContext guiDrawContext() { - return this.drawContext; + public UiDrawContext guiDrawContext() { + return this.uiDrawContext; } @Override diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyDrawContext.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyDrawContext.java deleted file mode 100644 index 9e2d288c..00000000 --- a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyDrawContext.java +++ /dev/null @@ -1,51 +0,0 @@ -package net.smyler.smylib.gui; - -import net.smyler.smylib.Color; -import net.smyler.smylib.gui.sprites.Sprite; - -public class DummyDrawContext implements DrawContext { - - private final Scissor scissor = new DummyScissor(); - private final GlState state = new DummyGlState(); - - @Override - public Scissor scissor() { - return this.scissor; - } - - @Override - public GlState glState() { - return this.state; - } - - @Override - public void drawGradientRectangle(double z, double xLeft, double yTop, double xRight, double yBottom, Color upperLeftColor, Color lowerLeftColor, Color lowerRightColor, Color upperRightColor) { - - } - - @Override - public void drawPolygon(double z, Color color, double... points) { - - } - - @Override - public void drawStrokeLine(double z, Color color, float size, double... points) { - - } - - @Override - public void drawClosedStrokeLine(double z, Color color, float size, double... points) { - - } - - @Override - public void drawSpriteCropped(double x, double y, double z, Sprite sprite, double leftCrop, double topCrop, double rightCrop, double bottomCrop) { - - } - - @Override - public void drawTooltip(String text, double x, double y) { - - } - -} diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java new file mode 100644 index 00000000..646d0600 --- /dev/null +++ b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java @@ -0,0 +1,115 @@ +package net.smyler.smylib.gui; + +import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; +import net.smyler.smylib.gui.gl.*; + +import static net.smyler.smylib.Color.WHITE; + +public class DummyGlContext implements GlContext { + + @Override + public void enableAlpha() { + + } + + @Override + public void disableAlpha() { + + } + + @Override + public void setColor(Color color) { + + } + + @Override + public Color getColor() { + return WHITE; + } + + @Override + public void setTexture(Identifier texture) { + + } + + @Override + public void enableColorLogic(ColorLogic colorLogic) { + + } + + @Override + public void disableColorLogic() { + + } + + @Override + public void pushViewMatrix() { + + } + + @Override + public void rotate(double angle) { + + } + + @Override + public void translate(double x, double y) { + + } + + @Override + public void scale(double x, double y) { + + } + + @Override + public void popViewMatrix() { + + } + + @Override + public void startDrawing(DrawMode mode, VertexFormat format) { + + } + + @Override + public VertexBuilder vertex() { + return new DummyVertexBuilder(); + } + + @Override + public void draw() { + + } + + private static class DummyVertexBuilder implements VertexBuilder { + + @Override + public VertexBuilder position(double x, double y, double z) { + return this; + } + + @Override + public VertexBuilder color(float r, float g, float b, float a) { + return this; + } + + @Override + public VertexBuilder color(Color color) { + return this; + } + + @Override + public VertexBuilder texture(double u, double v) { + return this; + } + + @Override + public void end() { + + } + + } + +} diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlState.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlState.java deleted file mode 100644 index 77376e56..00000000 --- a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlState.java +++ /dev/null @@ -1,64 +0,0 @@ -package net.smyler.smylib.gui; - -import net.smyler.smylib.Color; - -import static net.smyler.smylib.Color.WHITE; - -public class DummyGlState implements GlState { - - @Override - public void enableAlpha() { - - } - - @Override - public void disableAlpha() { - - } - - @Override - public void setColor(Color color) { - - } - - @Override - public Color getColor() { - return WHITE; - } - - @Override - public void enableColorLogic(ColorLogic colorLogic) { - - } - - @Override - public void disableColorLogic() { - - } - - @Override - public void pushViewMatrix() { - - } - - @Override - public void rotate(double angle) { - - } - - @Override - public void translate(double x, double y) { - - } - - @Override - public void scale(double x, double y) { - - } - - @Override - public void popViewMatrix() { - - } - -} diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyScissor.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyScissor.java index ba6c7f69..6ea01847 100644 --- a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyScissor.java +++ b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyScissor.java @@ -1,5 +1,7 @@ package net.smyler.smylib.gui; +import net.smyler.smylib.gui.gl.Scissor; + public class DummyScissor implements Scissor { @Override public void setEnabled(boolean yesNo) { diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java new file mode 100644 index 00000000..6b66146b --- /dev/null +++ b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java @@ -0,0 +1,82 @@ +package net.smyler.smylib.gui; + +import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; +import net.smyler.smylib.gui.gl.GlContext; +import net.smyler.smylib.gui.gl.Scissor; +import net.smyler.smylib.gui.sprites.Sprite; + +import java.awt.image.BufferedImage; +import java.util.HashSet; +import java.util.Set; + +public class DummyUiDrawContext implements UiDrawContext { + + private final Scissor scissor = new DummyScissor(); + private final GlContext state = new DummyGlContext(); + private int dynamicTextureIndex = 0; + private final Set dynamicTextures = new HashSet<>(); + + @Override + public Scissor scissor() { + return this.scissor; + } + + @Override + public GlContext gl() { + return this.state; + } + + @Override + public void drawGradientRectangle(double z, double xLeft, double yTop, double xRight, double yBottom, Color upperLeftColor, Color lowerLeftColor, Color lowerRightColor, Color upperRightColor) { + + } + + @Override + public void drawPolygon(double z, Color color, double... points) { + + } + + @Override + public void drawStrokeLine(double z, Color color, float size, double... points) { + + } + + @Override + public void drawClosedStrokeLine(double z, Color color, float size, double... points) { + + } + + @Override + public void drawSpriteCropped(double x, double y, double z, Sprite sprite, double leftCrop, double topCrop, double rightCrop, double bottomCrop) { + + } + + @Override + public void drawTooltip(String text, double x, double y) { + + } + + @Override + public void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) { + + } + + @Override + public Identifier loadDynamicTexture(BufferedImage image) { + Identifier id = new Identifier( + "smylib", + "smylib_test_dynamic_" + this.dynamicTextureIndex++ + ); + this.dynamicTextures.add(id); + return id; + } + + @Override + public void unloadDynamicTexture(Identifier texture) { + if (!this.dynamicTextures.remove(texture)) { + throw new IllegalStateException("Tried to unload a dynamic texture that is not loaded."); + } + } + +} From 6f1e2a8c0b673e288beba87b2af459f796cf9e96 Mon Sep 17 00:00:00 2001 From: Smyler Date: Sat, 6 Jul 2024 22:51:26 +0200 Subject: [PATCH 08/10] Add shading mode to GL context --- .../thesmyler/smylibgui/util/RenderUtil.java | 1 + .../gui/widgets/CircularCompassWidget.java | 1 + .../gui/widgets/RibbonCompassWidget.java | 5 +-- .../entities/AbstractPlayerMarker.java | 34 +++++++------------ .../net/smyler/smylib/gui/gl/GlContext.java | 10 ++++++ .../smyler/smylib/gui/gl/Lwjgl2GlContext.java | 12 +++++-- .../net/smyler/smylib/gui/DummyGlContext.java | 10 ++++++ 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java b/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java index ffd2bebf..f6ef8baa 100644 --- a/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java +++ b/forge/src/main/java/fr/thesmyler/smylibgui/util/RenderUtil.java @@ -15,6 +15,7 @@ public static void drawModalRectWithCustomSizedTexture(double x, double y, doubl double f1 = 1.0f / textureHeight; GlStateManager.enableAlpha(); GlStateManager.enableBlend(); + GlStateManager.enableTexture2D(); Tessellator tessellator = Tessellator.getInstance(); BufferBuilder builder = tessellator.getBuffer(); builder.begin(7, DefaultVertexFormats.POSITION_TEX); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java index d47a039d..9b0b8b7c 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java @@ -62,6 +62,7 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo float radius = this.size / 2; GlContext gl = context.gl(); + gl.enableSmoothShading(); gl.pushViewMatrix(); gl.translate(x + radius, y + radius); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java index 0f4b575a..6a81d9e0 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java @@ -4,11 +4,9 @@ import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.gl.GlContext; import net.smyler.terramap.Terramap; -import org.lwjgl.opengl.GL11; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.gui.widgets.Widget; -import net.minecraft.client.renderer.GlStateManager; import static net.smyler.smylib.Color.WHITE; import static net.smyler.smylib.gui.gl.DrawMode.QUADS; @@ -52,8 +50,7 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo GlContext gl = context.gl(); gl.enableAlpha(); - GlStateManager.enableBlend(); - GlStateManager.shadeModel(GL11.GL_SMOOTH); + gl.enableSmoothShading(); gl.startDrawing(QUADS, POSITION_TEXTURE_COLOR); gl.setTexture(COMPASS_BACKGROUND_TEXTURE); diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java index af039949..469f7fb4 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/markers/markers/entities/AbstractPlayerMarker.java @@ -2,7 +2,6 @@ import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.gl.GlContext; -import org.lwjgl.opengl.GL11; import net.smyler.smylib.gui.containers.WidgetContainer; import net.smyler.smylib.Color; @@ -12,13 +11,12 @@ import fr.thesmyler.terramap.gui.widgets.markers.controllers.MarkerController; import fr.thesmyler.terramap.gui.widgets.markers.markers.AbstractMovingMarker; import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.util.ResourceLocation; import static net.smyler.smylib.Color.WHITE; +import static net.smyler.smylib.gui.gl.DrawMode.TRIANGLE_FAN; +import static net.smyler.smylib.gui.gl.VertexFormat.POSITION_COLOR; public abstract class AbstractPlayerMarker extends AbstractMovingMarker { @@ -46,28 +44,20 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo } GlContext gl = context.gl(); - gl.pushViewMatrix(); + gl.pushViewMatrix(); gl.translate(x + this.width / 2, y + this.height / 2); gl.rotate(azimuth); - GlStateManager.disableTexture2D(); - GlStateManager.enableBlend(); - context.gl().disableAlpha(); - GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); - GlStateManager.shadeModel(7425); - Tessellator tess = Tessellator.getInstance(); - BufferBuilder buff = tess.getBuffer(); - buff.begin(GL11.GL_POLYGON, DefaultVertexFormats.POSITION_COLOR); - buff.pos(0, -this.height*1.2, 0).color(1f, 0, 0, 0.7f).endVertex(); - buff.pos(-this.width/2, -this.height * 0.7, 0).color(0.8f, 0, 0, 0.9f).endVertex(); - buff.pos(0, -this.height * 0.8, 0).color(0.5f, 0, 0, 1f).endVertex(); - buff.pos(this.width/2, -this.height * 0.7, 0).color(0.8f, 0, 0, 0.9f).endVertex(); - tess.draw(); - GlStateManager.shadeModel(7424); - GlStateManager.disableBlend(); - context.gl().enableAlpha(); - GlStateManager.enableTexture2D(); + gl.disableAlpha(); + gl.enableSmoothShading(); + gl.startDrawing(TRIANGLE_FAN, POSITION_COLOR); + gl.vertex().position(0, -this.height*1.2, 0).color(1f, 0, 0, 0.7f).end(); + gl.vertex().position(-this.width/2, -this.height * 0.7, 0).color(0.8f, 0, 0, 0.9f).end(); + gl.vertex().position(0, -this.height * 0.8, 0).color(0.5f, 0, 0, 1f).end(); + gl.vertex().position(this.width/2, -this.height * 0.7, 0).color(0.8f, 0, 0, 0.9f).end(); + gl.draw(); + GlStateManager.popMatrix(); } diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java index 5f32e10e..c96bac6e 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/gl/GlContext.java @@ -3,6 +3,12 @@ import net.smyler.smylib.Color; import net.smyler.smylib.Identifier; + +/** + * Provides a more direct access to the underlying OpenGL context. + * + * @author Smyler + */ public interface GlContext { void enableAlpha(); @@ -19,6 +25,10 @@ public interface GlContext { void disableColorLogic(); + void enableSmoothShading(); + + void enableFlatShading(); + void pushViewMatrix(); void rotate(double angle); diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java index e8ff3c38..d65b5ec4 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/gl/Lwjgl2GlContext.java @@ -99,6 +99,16 @@ public void disableColorLogic() { GlStateManager.disableColorLogic(); } + @Override + public void enableSmoothShading() { + GlStateManager.shadeModel(GL11.GL_SMOOTH); + } + + @Override + public void enableFlatShading() { + GlStateManager.shadeModel(GL11.GL_FLAT); + } + @Override public void pushViewMatrix() { GlStateManager.pushMatrix(); @@ -181,10 +191,8 @@ public void draw() { checkState(this.currentFormat != null, "Not building!"); if (this.currentFormat.texture) { GlStateManager.enableTexture2D(); - GlStateManager.shadeModel(GL11.GL_FLAT); } else { GlStateManager.disableTexture2D(); - GlStateManager.shadeModel(GL11.GL_SMOOTH); } GlStateManager.enableAlpha(); GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO); diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java index 646d0600..76da53c8 100644 --- a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java +++ b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyGlContext.java @@ -43,6 +43,16 @@ public void disableColorLogic() { } + @Override + public void enableSmoothShading() { + + } + + @Override + public void enableFlatShading() { + + } + @Override public void pushViewMatrix() { From 7ba31b471645ee317b2a95d31cde838ef6a997b4 Mon Sep 17 00:00:00 2001 From: Smyler Date: Sat, 6 Jul 2024 23:56:49 +0200 Subject: [PATCH 09/10] Remove UiDrawContext#drawPolygon() --- .../terramap/gui/widgets/map/InputLayer.java | 45 ++++++++++++++----- .../gui/widgets/map/layer/RasterMapLayer.java | 24 ++++++---- .../net/smyler/smylib/gui/UiDrawContext.java | 11 ----- .../smylib/gui/Lwjgl2UiDrawContext.java | 24 ---------- .../smyler/smylib/gui/DummyUiDrawContext.java | 10 ----- 5 files changed, 49 insertions(+), 65 deletions(-) diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java index 097b405a..d4130fca 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/InputLayer.java @@ -6,6 +6,7 @@ import net.smyler.smylib.Color; import fr.thesmyler.terramap.MapContext; import fr.thesmyler.terramap.input.KeyBindings; +import net.smyler.smylib.gui.gl.GlContext; import net.smyler.terramap.util.geo.GeoPointReadOnly; import net.smyler.terramap.util.geo.WebMercatorUtil; import net.smyler.smylib.math.Mat2d; @@ -16,6 +17,8 @@ import static java.lang.Math.*; import static net.smyler.smylib.SmyLib.getGameClient; +import static net.smyler.smylib.gui.gl.DrawMode.TRIANGLE_FAN; +import static net.smyler.smylib.gui.gl.VertexFormat.POSITION; /** * Processes inputs for a map and propagates them to the map controller. @@ -34,8 +37,8 @@ public class InputLayer extends MapLayer { // Stuff that can be pre-computed and that's used later when drawing a polygon at the place the map rotates around. private static final int ROTATION_POLYGON_VERTEX_COUNT = 5; - private static final double[] ROTATION_POLYGON_VERTICES_OUTER = new double[ROTATION_POLYGON_VERTEX_COUNT * 2]; - private static final double[] ROTATION_POLYGON_VERTICES_INNER = new double[ROTATION_POLYGON_VERTEX_COUNT * 2]; + private static final double[][] ROTATION_POLYGON_VERTICES_OUTER = new double[ROTATION_POLYGON_VERTEX_COUNT][2]; + private static final double[][] ROTATION_POLYGON_VERTICES_INNER = new double[ROTATION_POLYGON_VERTEX_COUNT][2]; private static final float ROTATION_POLYGON_RADIUS_OUTER = 5; private static final float ROTATION_POLYGON_RADIUS_INNER = 2; static { @@ -43,10 +46,14 @@ public class InputLayer extends MapLayer { Vec2dMutable inner = new Vec2dMutable(0, -ROTATION_POLYGON_RADIUS_INNER); Mat2d rot = Mat2d.forRotation(-PI*2 / ROTATION_POLYGON_VERTEX_COUNT); for(int i = 0; i < ROTATION_POLYGON_VERTEX_COUNT; i++) { - ROTATION_POLYGON_VERTICES_OUTER[2*i] = outer.x; - ROTATION_POLYGON_VERTICES_OUTER[2*i + 1] = outer.y; - ROTATION_POLYGON_VERTICES_INNER[2*i] = inner.x; - ROTATION_POLYGON_VERTICES_INNER[2*i + 1] = inner.y; + ROTATION_POLYGON_VERTICES_OUTER[i] = new double[] { + outer.x, + outer.y + }; + ROTATION_POLYGON_VERTICES_INNER[i] = new double[] { + inner.x, + inner.y + }; outer.apply(rot); inner.apply(rot); } @@ -68,11 +75,7 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo if(this.isRotating) { // If we are processing rotation input, draw pentagons at the corresponding spot - context.gl().pushViewMatrix(); - context.gl().translate(x + this.rotatePosition.x, y + this.rotatePosition.y); - context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_OUTER); - context.drawPolygon(Color.DARK_OVERLAY, ROTATION_POLYGON_VERTICES_INNER); - context.gl().popViewMatrix(); + this.drawRotationSpot(context, x + this.rotatePosition.x, y + this.rotatePosition.y); } if (this.getMap().isDebugMode()) { @@ -232,4 +235,24 @@ private boolean isShortcutEnabled() { return this.map.isInteractive() && Keyboard.isKeyDown(KeyBindings.MAP_SHORTCUT.getKeyCode()) && this.map.allowsQuickTp(); } + private void drawRotationSpot(UiDrawContext context, double x, double y) { + GlContext gl = context.gl(); + gl.pushViewMatrix(); + gl.translate(x, y); + gl.setColor(Color.DARK_OVERLAY); + this.drawConvexPolygon(gl, ROTATION_POLYGON_VERTICES_OUTER); + this.drawConvexPolygon(gl, ROTATION_POLYGON_VERTICES_INNER); + gl.popViewMatrix(); + } + + private void drawConvexPolygon(GlContext gl, double[][] vertices) { + gl.startDrawing(TRIANGLE_FAN, POSITION); + gl.vertex().position(0d, 0d, 0d); + for (double[] vertex : vertices) { + gl.vertex().position(vertex[0], vertex[1], 0d).end(); + } + gl.vertex().position(0d, 0d, 0d); + gl.draw(); + } + } diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java index 51e7475a..e4c961c9 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/map/layer/RasterMapLayer.java @@ -26,6 +26,8 @@ import static net.smyler.smylib.Color.WHITE; import static net.smyler.smylib.SmyLib.getGameClient; +import static net.smyler.smylib.gui.gl.DrawMode.QUADS; +import static net.smyler.smylib.gui.gl.VertexFormat.POSITION_TEXTURE; abstract public class RasterMapLayer extends MapLayer { @@ -211,7 +213,6 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo dY += factorY * renderSizedSize; } - gl.setColor(WHITE); Identifier texture = defaultTexture; try { if(tile.isTextureAvailable()) texture = tile.getTexture(); @@ -221,13 +222,19 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo parentMap.reportError(this, e.toString()); } if (texture != null) { - context.drawTexture( - texture, - dispX, dispY, - dX, dY, - displayWidth, displayHeight, - renderSizedSize, renderSizedSize - ); + double f = 1.0f / renderSizedSize; + double uLeft = dX * f; + double uRight = (dX + displayWidth) * f; + double uTop = dY * f; + double uBottom = (dY + displayHeight) * f; + gl.setTexture(texture); + gl.setColor(whiteWithAlpha); + gl.startDrawing(QUADS, POSITION_TEXTURE); + gl.vertex().position(dispX, dispY + displayHeight, 0d).texture(uLeft, uBottom).end(); + gl.vertex().position(dispX + displayWidth, dispY + displayHeight, 0d).texture(uRight, uBottom).end(); + gl.vertex().position(dispX + displayWidth, dispY, 0d).texture(uRight, uTop).end(); + gl.vertex().position(dispX, dispY, 0d).texture(uLeft, uTop).end(); + gl.draw(); } if(debug) { Color lineColor = texture == null? Color.GREEN: lowerResRender? unlockedZoomRender? Color.BLUE: Color.RED : WHITE; @@ -241,7 +248,6 @@ public void draw(UiDrawContext context, float x, float y, float mouseX, float mo smallFont.draw((float)dispX + 2, (float)(dispY + displayHeight/2), GeoServices.formatGeoCoordForDisplay(dispX), lineColor, false); smallFont.drawCentered((float)(dispX + displayWidth/2), (float)dispY + 2, GeoServices.formatGeoCoordForDisplay(dispY), lineColor, false); } - gl.setColor(WHITE); } } diff --git a/smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java b/smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java index c997f381..f078a47f 100644 --- a/smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java +++ b/smylib/core/src/main/java/net/smyler/smylib/gui/UiDrawContext.java @@ -42,14 +42,6 @@ default void drawGradientRectangle(double xLeft, double yTop, double xRight, dou this.drawGradientRectangle(0d, xLeft, yTop, xRight, yBottom, upperLeftColor, lowerLeftColor, lowerRightColor, upperRightColor); } - @Deprecated - void drawPolygon(double z, Color color, double... points); - - @Deprecated - default void drawPolygon(Color color, double... points) { - this.drawPolygon(0d, color, points); - } - void drawStrokeLine(double z, Color color, float size, double... points); default void drawStrokeLine(Color color, float size, double... points) { @@ -79,9 +71,6 @@ default void drawSpriteCropped(double x, double y, Sprite sprite, double leftCro //TODO use Text in drawTooltip void drawTooltip(String text, double x, double y); - @Deprecated - void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight); - Identifier loadDynamicTexture(BufferedImage image); void unloadDynamicTexture(Identifier texture); diff --git a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java index 7ed01965..4b6baaff 100644 --- a/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java +++ b/smylib/forge/src/main/java/net/smyler/smylib/gui/Lwjgl2UiDrawContext.java @@ -56,11 +56,6 @@ public void drawGradientRectangle(double z, double xLeft, double yTop, double xR GlStateManager.enableTexture2D(); } - @Override - public void drawPolygon(double z, Color color, double... points) { - this.drawMultiPointsGeometry(GL11.GL_POLYGON, z, color, points); - } - @Override public void drawStrokeLine(double z, Color color, float size, double... points) { GL11.glLineWidth(size * getGameClient().scaleFactor()); @@ -124,25 +119,6 @@ public void drawTooltip(String text, double x, double y) { if(!lighting) GlStateManager.disableLighting(); } - @Override - public void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) { - getMinecraft().getTextureManager().bindTexture(new ResourceLocation(texture.namespace, texture.path)); - double f = 1.0f / textureWidth; - double f1 = 1.0f / textureHeight; - GlStateManager.enableAlpha(); - GlStateManager.enableBlend(); - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder builder = tessellator.getBuffer(); - builder.begin(7, DefaultVertexFormats.POSITION_TEX); - builder.pos(x, y + height, 0d).tex(u * f, (v + height) * f1).endVertex(); - builder.pos(x + width, y + height, 0d).tex((u + width) * f, (v + height) * f1).endVertex(); - builder.pos(x + width, y, 0d).tex((u + width) * f, v * f1).endVertex(); - builder.pos(x, y, 0d).tex(u * f, v * f1).endVertex(); - tessellator.draw(); - GlStateManager.disableAlpha(); - GlStateManager.disableBlend(); - } - @Override public Identifier loadDynamicTexture(BufferedImage image) { DynamicTexture dynamicTexture = new DynamicTexture(image); diff --git a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java index 6b66146b..841d67d6 100644 --- a/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java +++ b/smylib/testing/src/main/java/net/smyler/smylib/gui/DummyUiDrawContext.java @@ -32,11 +32,6 @@ public void drawGradientRectangle(double z, double xLeft, double yTop, double xR } - @Override - public void drawPolygon(double z, Color color, double... points) { - - } - @Override public void drawStrokeLine(double z, Color color, float size, double... points) { @@ -57,11 +52,6 @@ public void drawTooltip(String text, double x, double y) { } - @Override - public void drawTexture(Identifier texture, double x, double y, double u, double v, double width, double height, double textureWidth, double textureHeight) { - - } - @Override public Identifier loadDynamicTexture(BufferedImage image) { Identifier id = new Identifier( From 94aee4033aebc6dffe1cbbc1379d804067268544 Mon Sep 17 00:00:00 2001 From: Smyler Date: Sun, 7 Jul 2024 00:01:46 +0200 Subject: [PATCH 10/10] Move compass widgets to core Terramap module --- .../net/smyler}/terramap/gui/widgets/CircularCompassWidget.java | 2 +- .../net/smyler}/terramap/gui/widgets/RibbonCompassWidget.java | 2 +- .../main/java/fr/thesmyler/terramap/gui/HudScreenHandler.java | 2 +- .../java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java | 2 +- .../thesmyler/terramap/gui/screens/config/HudConfigScreen.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename {forge/src/main/java/fr/thesmyler => core/src/main/java/net/smyler}/terramap/gui/widgets/CircularCompassWidget.java (99%) rename {forge/src/main/java/fr/thesmyler => core/src/main/java/net/smyler}/terramap/gui/widgets/RibbonCompassWidget.java (99%) diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java b/core/src/main/java/net/smyler/terramap/gui/widgets/CircularCompassWidget.java similarity index 99% rename from forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java rename to core/src/main/java/net/smyler/terramap/gui/widgets/CircularCompassWidget.java index 9b0b8b7c..7125e897 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/CircularCompassWidget.java +++ b/core/src/main/java/net/smyler/terramap/gui/widgets/CircularCompassWidget.java @@ -1,4 +1,4 @@ -package fr.thesmyler.terramap.gui.widgets; +package net.smyler.terramap.gui.widgets; import net.smyler.smylib.gui.UiDrawContext; import net.smyler.smylib.gui.gl.GlContext; diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java b/core/src/main/java/net/smyler/terramap/gui/widgets/RibbonCompassWidget.java similarity index 99% rename from forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java rename to core/src/main/java/net/smyler/terramap/gui/widgets/RibbonCompassWidget.java index 6a81d9e0..78ae3500 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/widgets/RibbonCompassWidget.java +++ b/core/src/main/java/net/smyler/terramap/gui/widgets/RibbonCompassWidget.java @@ -1,4 +1,4 @@ -package fr.thesmyler.terramap.gui.widgets; +package net.smyler.terramap.gui.widgets; import net.smyler.smylib.Identifier; import net.smyler.smylib.gui.UiDrawContext; diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/HudScreenHandler.java b/forge/src/main/java/fr/thesmyler/terramap/gui/HudScreenHandler.java index 85e6f76c..eb6f2c30 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/HudScreenHandler.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/HudScreenHandler.java @@ -9,7 +9,7 @@ import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.gui.screens.LayerRenderingOffsetPopup; import fr.thesmyler.terramap.gui.screens.config.HudConfigScreen; -import fr.thesmyler.terramap.gui.widgets.RibbonCompassWidget; +import net.smyler.terramap.gui.widgets.RibbonCompassWidget; import fr.thesmyler.terramap.gui.widgets.map.MinimapWidget; import fr.thesmyler.terramap.gui.widgets.map.layer.OnlineRasterMapLayer; import net.buildtheearth.terraplusplus.projection.GeographicProjection; diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java index 8c955ea3..5f6ae230 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/TerramapScreen.java @@ -51,7 +51,7 @@ import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.gui.screens.config.TerramapConfigScreen; -import fr.thesmyler.terramap.gui.widgets.CircularCompassWidget; +import net.smyler.terramap.gui.widgets.CircularCompassWidget; import fr.thesmyler.terramap.gui.widgets.markers.controllers.FeatureVisibilityController; import fr.thesmyler.terramap.gui.widgets.markers.markers.Marker; import fr.thesmyler.terramap.gui.widgets.markers.markers.entities.MainPlayerMarker; diff --git a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java index e54c4dae..a0035977 100644 --- a/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java +++ b/forge/src/main/java/fr/thesmyler/terramap/gui/screens/config/HudConfigScreen.java @@ -22,7 +22,7 @@ import fr.thesmyler.terramap.TerramapClientContext; import fr.thesmyler.terramap.TerramapConfig; import fr.thesmyler.terramap.gui.screens.config.TerramapConfigScreen.TileScalingOption; -import fr.thesmyler.terramap.gui.widgets.RibbonCompassWidget; +import net.smyler.terramap.gui.widgets.RibbonCompassWidget; import fr.thesmyler.terramap.gui.widgets.map.MapController; import fr.thesmyler.terramap.gui.widgets.map.MapWidget; import fr.thesmyler.terramap.gui.widgets.map.layer.McChunksLayer;