Skip to content

Commit

Permalink
Allow not rendering a default tile
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jan 14, 2024
1 parent 0635f73 commit 14af06a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ public void init() {
* @throws IllegalArgumentException if there is no such layer type id
*/
public MapLayer createLayer(String layerTypeId) throws IllegalArgumentException {
Supplier<? extends MapLayer> constructor = MapLayerRegistry.INSTANCE.getRegistrations(layerTypeId).getConstructor();
if (constructor == null) throw new IllegalArgumentException("No such layer type registered: " + layerTypeId);
MapLayerRegistry.LayerRegistration<?> registration = MapLayerRegistry.INSTANCE.getRegistrations(layerTypeId);
if (registration == null) throw new IllegalArgumentException("No such layer type registered: " + layerTypeId);
Supplier<? extends MapLayer> constructor = registration.getConstructor();
MapLayer layer = constructor.get();
layer.setMap(this);
layer.setType(layerTypeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import fr.thesmyler.terramap.gui.widgets.map.MapWidget;
import fr.thesmyler.terramap.maps.raster.RasterTile;
import fr.thesmyler.terramap.maps.raster.RasterTiledMap;
import fr.thesmyler.terramap.maps.raster.imp.UrlRasterTile;
import fr.thesmyler.terramap.util.geo.*;
import fr.thesmyler.terramap.util.geo.TilePos.InvalidTilePositionException;
import fr.thesmyler.terramap.util.math.Mat2d;
Expand Down Expand Up @@ -51,6 +50,7 @@ protected void initialize() {
public void draw(float x, float y, float mouseX, float mouseY, boolean hovered, boolean focused, WidgetContainer parent) {

final RasterTiledMap tiledMap = this.getTiledMap();
final ResourceLocation defaultTexture = tiledMap.getDefaultTileTexture();

Font smallFont = Util.getSmallestFont();
Minecraft mc = Minecraft.getMinecraft();
Expand Down Expand Up @@ -207,26 +207,28 @@ public void draw(float x, float y, float mouseX, float mouseY, boolean hovered,
}

whiteWithAlpha.applyGL();
ResourceLocation texture = UrlRasterTile.errorTileTexture;
ResourceLocation texture = defaultTexture;
try {
if(tile.isTextureAvailable()) texture = tile.getTexture();
else perfectDraw = false;
} catch (Throwable e) {
perfectDraw = false;
parentMap.reportError(this, e.toString());
}
textureManager.bindTexture(texture);
RenderUtil.drawModalRectWithCustomSizedTexture(
dispX,
dispY,
dX, dY,
displayWidth,
displayHeight,
renderSizedSize,
renderSizedSize
);
if (texture != null) {
textureManager.bindTexture(texture);
RenderUtil.drawModalRectWithCustomSizedTexture(
dispX,
dispY,
dX, dY,
displayWidth,
displayHeight,
renderSizedSize,
renderSizedSize
);
}
if(debug) {
Color lineColor = lowerResRender? unlockedZoomRender? Color.BLUE: Color.RED : Color.WHITE;
Color lineColor = texture == null? Color.GREEN: lowerResRender? unlockedZoomRender? Color.BLUE: Color.RED : Color.WHITE;
RenderUtil.drawClosedStrokeLine(lineColor, 1f,
dispX, dispY,
dispX, dispY + displayHeight - 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import fr.thesmyler.terramap.util.geo.TilePos;
import fr.thesmyler.terramap.util.geo.TilePosImmutable;
import fr.thesmyler.terramap.util.geo.WebMercatorBounds;
import net.minecraft.util.ResourceLocation;

/**
* A raster map made of individual tiles.
*
* @author Smyler
*/
public interface RasterTiledMap extends Comparable<RasterTiledMap> {

/**
Expand Down Expand Up @@ -43,7 +49,7 @@ default RasterTile getTile(int zoom, int x, int y) {
/**
* Gets a name for this map, translated in the appropriate language,
* or English if it isn't available.
*
*
* @param localeKey the language key to get the copyright for
* @return the name of this map, translated to the appropriate language.
*/
Expand Down Expand Up @@ -78,7 +84,7 @@ default RasterTile getTile(int zoom, int x, int y) {
* @return true if this map should be considered a debug map
*/
boolean isDebug();

/**
* @param zoom the zoom level to consider
*
Expand All @@ -88,5 +94,11 @@ default WebMercatorBounds getBounds(int zoom) {
return null;
}

/**
* @return the default tile texture to show when no other is available
* (e.g. because the right tile is still loading).
* Return null to not render anything.
*/
ResourceLocation getDefaultTileTexture();

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public boolean isDebug() {
return true;
}

@Override
public ResourceLocation getDefaultTileTexture() {
return this.textureLocation;
}

public Color getColor() {
return this.color;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.thesmyler.terramap.maps.raster.CachingRasterTiledMap;
import fr.thesmyler.terramap.maps.raster.TiledMapProvider;
import fr.thesmyler.terramap.util.geo.TilePosImmutable;
import net.minecraft.util.ResourceLocation;

public class TerrainPreviewMap extends CachingRasterTiledMap<TerrainPreviewTile> {

Expand All @@ -30,7 +31,7 @@ public String getLocalizedName(String localeKey) {

@Override
public String getComment() {
return "Terra++ terrain perview debug map";
return "Terra++ terrain preview debug map";
}

@Override
Expand Down Expand Up @@ -58,6 +59,11 @@ public boolean isDebug() {
return true;
}

@Override
public ResourceLocation getDefaultTileTexture() {
return null;
}

@Override
public int getMinZoom() {
return BASE_ZOOM_LEVEL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean isTextureAvailable() {
public ResourceLocation 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 to low (" + this.position.getZoom() + ")");
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class UrlRasterTile implements RasterTile {
private ResourceLocation texture = null;
private CompletableFuture<ByteBuf> textureTask;

public static ResourceLocation errorTileTexture = null;

public UrlRasterTile(String urlPattern, TilePosImmutable pos) {
this.pos = pos;
Expand Down Expand Up @@ -136,11 +135,4 @@ public int hashCode() {
return this.url.hashCode();
}

public static void registerErrorTexture() {
TextureManager textureManager = Minecraft.getMinecraft().getTextureManager();
int[] color = {170, 211, 223};
DynamicTexture texture = new DynamicTexture(ImageUtil.imageFromColor(256, 256, color));
UrlRasterTile.errorTileTexture = textureManager.getDynamicTextureLocation(TerramapMod.MODID + ":error_tile_texture", texture);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
import fr.thesmyler.terramap.maps.raster.TiledMapProvider;
import fr.thesmyler.terramap.network.SP2CMapStylePacket;
import fr.thesmyler.terramap.util.CopyrightHolder;
import fr.thesmyler.terramap.util.ImageUtil;
import fr.thesmyler.terramap.util.geo.TilePosImmutable;
import fr.thesmyler.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;

/**
* Instances are usually created in {@link MapStylesLibrary} and {@link SP2CMapStylePacket}.
*
* @author SmylerMC
* @author Smyler
*
*/
public class UrlTiledMap extends CachingRasterTiledMap<UrlRasterTile> implements CopyrightHolder {
Expand All @@ -44,6 +49,7 @@ public class UrlTiledMap extends CachingRasterTiledMap<UrlRasterTile> implements
private final Map<Integer, WebMercatorBounds> bounds;

private static final ITextComponent FALLBACK_COPYRIGHT = ITextComponent.Serializer.jsonToComponent("{\"text\":\"The text component for this copyright notice was malformatted!\",\"color\":\"dark_red\"}");
private ResourceLocation errorTileTexture = null;

public UrlTiledMap(
String[] urlPatterns,
Expand Down Expand Up @@ -100,6 +106,7 @@ public UrlTiledMap(
*/
@Override
public void setup() {
this.registerErrorTexture();
for(String urlPattern: this.getUrlPatterns()) {
String url = urlPattern.replace("{z}", "0").replace("{x}", "0").replace("{y}", "0");
try {
Expand Down Expand Up @@ -259,7 +266,7 @@ public int getMaxConcurrentRequests() {
}

/**
* @return Whether or not this map can be used on the minimap
* @return Whether this map can be used on the minimap
*/
@Override
public boolean isAllowedOnMinimap() {
Expand All @@ -271,4 +278,16 @@ public WebMercatorBounds getBounds(int zoom) {
return this.bounds.get(zoom);
}

@Override
public ResourceLocation 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(TerramapMod.MODID + ":error_tile_texture", texture);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void init(FMLInitializationEvent event) {

MinecraftForge.EVENT_BUS.register(new ClientTerramapEventHandler());
KeyBindings.registerBindings();
UrlRasterTile.registerErrorTexture();
MarkerControllerManager.registerBuiltInControllers();
MapStylesLibrary.reload();
ClientCommandHandler.instance.registerCommand(new OpenMapCommand());
Expand Down

0 comments on commit 14af06a

Please sign in to comment.