Skip to content

Commit

Permalink
Feature/1.21/map info changes (#308)
Browse files Browse the repository at this point in the history
feat: overhaul minimap info display

Configurable components, which can be enabled/disabled/reordered.
  • Loading branch information
MichaelHillcox authored Jul 23, 2024
1 parent acba792 commit 399c03a
Show file tree
Hide file tree
Showing 28 changed files with 1,068 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ enum StandardProblem implements ClaimResult {
NOT_LOADED("not_loaded"),
;

public static final NameMap<StandardProblem> NAME_MAP = NameMap.of(NOT_OWNER, values()).create();
public static final NameMap<StandardProblem> NAME_MAP = NameMap.of(NOT_OWNER, values()).baseNameKey("ftbchunks.standard_problem").create();

private final String resultName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.ftb.mods.ftbchunks.api.client;

import com.google.common.collect.ImmutableList;
import dev.ftb.mods.ftbchunks.api.client.minimap.MinimapInfoComponent;
import dev.ftb.mods.ftbchunks.api.client.waypoint.WaypointManager;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -27,4 +29,25 @@ public interface FTBChunksClientAPI {
* entities...)
*/
void requestMinimapIconRefresh();

/**
* Register a custom minimap info component {@link MinimapInfoComponent} to be rendered on the minimap.
*
* This should be called during mod initialization as this list will be finalized once Minecraft has "started"
* per the client lifecycle events
*
* @param component the component to register
*/
void registerMinimapComponent(MinimapInfoComponent component);

//Todo
boolean isMinimapComponentEnabled(MinimapInfoComponent component);

//Todo
void setMinimapComponentEnabled(MinimapInfoComponent component, boolean enabled);

/**
* Provides an immutable list of all registered minimap components.
*/
ImmutableList<MinimapInfoComponent> getMinimapComponents();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.ftb.mods.ftbchunks.api.client.minimap;

import dev.ftb.mods.ftbchunks.client.map.MapDimension;
import dev.ftb.mods.ftblibrary.math.XZ;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.phys.Vec3;

import java.util.Map;

/**
* Minimal context for Minimap Info Components
*
* @param minecraft The Minecraft instance (Helper)
* @param player The client player
* @param mapDimension The dimension of the players location
* @param mapChunksPos The chunk for the players location
* @param playerPos the players pos
* @param infoSettings raw settings for this component
*/
public record MinimapContext(
Minecraft minecraft,
LocalPlayer player,
MapDimension mapDimension,
XZ mapChunksPos,
Vec3 playerPos,
Map<String, String> infoSettings
) {

public String getSetting(MinimapInfoComponent infoComponent) {
return infoSettings.getOrDefault(infoComponent.id().toString(), "");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package dev.ftb.mods.ftbchunks.api.client.minimap;

import dev.ftb.mods.ftbchunks.client.FTBChunksClientConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.Collections;
import java.util.Set;

/**
* An entry point for developers to create custom minimap info components.
*/
public interface MinimapInfoComponent {
/**
* The ID of this component.
*/
ResourceLocation id();

/**
* Render your component here, the {@link com.mojang.blaze3d.vertex.PoseStack} will already be scaled and
* translated to the correct position (centered by default). We do not provide an X and Y position as
* 0, 0 is the center of the correct location. Use 0, 0 as the center of the component and {@link #height(MinimapContext)}
* to allocate the correct height for the component.
*
* @param context The minimap context
* @param graphics The graphics object see {@link GuiGraphics}
* @param font The font object
*/
void render(MinimapContext context, GuiGraphics graphics, Font font);

/**
* Set of Info {@link TranslatedOption} that are used to configure options for rendering the waypoint
* this is exposed in the right click action of Minimap Info GUI
* @return the set of {@link TranslatedOption}.
*/
default Set<TranslatedOption> getConfigComponents() {
return Set.of();
}

/**
* The height of the component is used to allocate the correct space for the component. Failure to return the correct
* height will result in the component overlapping with other components.
*
* @param context The minimap context
* @return The height of the component
*/
default int height(MinimapContext context) {
return computeLineHeight(context.minecraft(), 1) + 1;
}

/**
* Checked on each render frame to determine if the height for the component should be allocated
*/
default boolean shouldRender(MinimapContext context) {
return true;
}

/**
* Helper method to compute the height of a text component whilst taking into account the font scale
*/
default int computeLineHeight(Minecraft minecraft, int lines) {
final float fontScale = FTBChunksClientConfig.MINIMAP_FONT_SCALE.get().floatValue();
return (int) ((minecraft.font.lineHeight + 2) * lines * fontScale);
}

/**
* Helper method to draw centered text without the faff of calculating the width of the text
*/
default void drawCenteredText(Font font, GuiGraphics graphics, Component text, int y) {
int textWidth = font.width(text.getVisualOrderText());
graphics.drawString(font, text, -textWidth / 2, y, 0xFFFFFFFF, true);
}


/**
* @return display name render in the Minimap Info Settings GUI
*/
default Component displayName() {
return Component.translatable("minimap.info." + id().getNamespace() + "." + id().getPath() + ".title");
}

/**
* @return hover texted displayed render in the Minimap Info Settings GUI
*/
default Component description() {
return Component.translatable("minimap.info." + id().getNamespace() + "." + id().getPath() + ".description");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.ftb.mods.ftbchunks.api.client.minimap;

public record TranslatedOption(
String optionName,
String translationKey
) {

public static TranslatedOption of(String optionName) {
String translatedKey = optionName.toLowerCase().replaceAll("[^a-z0-9]", "_");
return new TranslatedOption(optionName, "minimap.option." + translatedKey);
}
}
Loading

0 comments on commit 399c03a

Please sign in to comment.