Skip to content

Commit

Permalink
Selected highlight & click, keybind, better toast, remember shard
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Dec 2, 2024
1 parent 37b2f70 commit 644c0da
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 143 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ authors=Falkreon, acikek
contributors=Trudle, Tomate0613, afamiliarquiet, FoundationGames, TheEpicBlock, hama
license=MIT
# Mod Version
baseVersion=1.7.2
baseVersion=1.8.0
# Branch Metadata
branch=1.21
tagBranch=1.21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ protected ActionResult onUse(BlockState state, World world, BlockPos pos, Player
if (tryCollect(world, player, be)) {
return ActionResult.SUCCESS;
}
player.sendMessage(Text.translatable("block.scattered_shards.shard_block.pickup_fail"), true);
return ActionResult.FAIL;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.modfest.scatteredshards.client;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.client.toast.Toast;
Expand All @@ -19,17 +22,26 @@
import net.modfest.scatteredshards.networking.ScatteredShardsNetworking;

public class ScatteredShardsClient implements ClientModInitializer {
public static final String SHARD_MODIFY_TOAST_KEY = "toast.scattered_shards.shard_mod";
public static final KeyBinding VIEW_COLLECTION = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.scattered_shards.collection",
InputUtil.GLFW_KEY_J,
"key.categories.scattered_shards"
));

@Override
public void onInitializeClient() {
ClientShardCommand.register();
ScatteredShardsNetworking.registerClient();
ScatteredShardsContent.registerClient();
ScatteredShardsAPI.initClient();
ClientTickEvents.END_CLIENT_TICK.register(c -> {
if (VIEW_COLLECTION.wasPressed()) {
openShardTablet();
}
});
}

public static void triggerShardCollectAnimation(Identifier shardId) {
public static void onShardCollected(Identifier shardId) {
var library = ScatteredShardsAPI.getClientLibrary();
var collection = ScatteredShardsAPI.getClientCollection();

Expand All @@ -39,6 +51,9 @@ public static void triggerShardCollectAnimation(Identifier shardId) {
return;
}

ShardTabletGuiDescription.INITIAL_SHARD = shardId;
ShardTabletGuiDescription.INITIAL_SCROLL_POSITION = -1;

collection.add(shardId);
ScatteredShards.LOGGER.info("Collected shard '{}'!", shardId.toString());

Expand All @@ -47,15 +62,15 @@ public static void triggerShardCollectAnimation(Identifier shardId) {
.flatMap(ShardType::collectSound)
.ifPresent((sound) -> MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(sound, 1.0F, 0.8F)));

Toast toast = new ShardToast(shard);
Toast toast = new ShardCollectedToast(shard);
MinecraftClient.getInstance().getToastManager().add(toast);
}

public static void triggerShardModificationToast(Identifier shardId, boolean success) {
var toast = new SystemToast(
SystemToast.Type.PERIODIC_NOTIFICATION,
Text.translatable(SHARD_MODIFY_TOAST_KEY + ".title"),
Text.stringifiedTranslatable(SHARD_MODIFY_TOAST_KEY + "." + (success ? "success" : "fail"), shardId)
Text.translatable("toast.scattered_shards.shard_mod.title"),
Text.stringifiedTranslatable(success ? "toast.scattered_shards.shard_mod.success" : "toast.scattered_shards.shard_mod.success.fail", shardId)
);
MinecraftClient.getInstance().getToastManager().add(toast);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package net.modfest.scatteredshards.client;

import com.mojang.datafixers.util.Either;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.toast.Toast;
import net.minecraft.client.toast.ToastManager;
import net.minecraft.item.ItemStack;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
import net.modfest.scatteredshards.api.shard.Shard;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.util.ModMetaUtil;

import java.util.ArrayList;
import java.util.List;

public class ShardCollectedToast implements Toast {
public static final int TITLE_COLOR = 0xFF_FFFF00;
public static final Text TITLE = Text.translatable("toast.scattered_shards.collected");
public static final Text HINT = Text.translatable("toast.scattered_shards.collected.prompt", Text.keybind(ScatteredShardsClient.VIEW_COLLECTION.getTranslationKey()).formatted(Formatting.GOLD).formatted(Formatting.BOLD));
private static final Identifier TEXTURE = Identifier.ofVanilla("toast/advancement");
public static final int DURATION = 5000;

Either<ItemStack, Identifier> icon;
List<OrderedText> descLines;
List<OrderedText> hintLines;
private final int height;

public ShardCollectedToast(Shard shard) {
this.icon = shard.icon();
this.descLines = wrap(List.of(shard.name().copy().withColor(ScatteredShardsAPI.getClientLibrary().shardTypes().get(shard.shardTypeId()).orElse(ShardType.MISSING).textColor())));
this.hintLines = wrap(List.of(HINT));
this.height = 32 + Math.max(0, Math.max(this.descLines.size(), this.hintLines.size()) - 1) * 11;
icon.ifRight(ModMetaUtil::touchIconTexture);
}

@Override
public Visibility draw(DrawContext graphics, ToastManager manager, long startTime) {
graphics.drawGuiTexture(TEXTURE, 0, 0, this.getWidth(), this.getHeight());
TextRenderer textRenderer = manager.getClient().textRenderer;

graphics.drawText(
textRenderer,
TITLE, 32, 7, TITLE_COLOR,
false
);

double time = DURATION * manager.getNotificationDisplayTimeMultiplier();

List<OrderedText> body = startTime >= (time / 2) && !hintLines.isEmpty() ? hintLines : descLines;

for (int i = 0; i < body.size(); i++) {
graphics.drawText(textRenderer, body.get(i), 32, 18 + i * 11, 0xFF_FFFFFF, false);
}

icon.ifLeft(it -> graphics.drawItemWithoutEntity(it, 8, 8));
icon.ifRight(it -> ScreenDrawing.texturedRect(graphics, 8, 8, 16, 16, it, 0xFF_FFFFFF));
return startTime >= time ? Toast.Visibility.HIDE : Toast.Visibility.SHOW;
}

private List<OrderedText> wrap(List<Text> messages) {
List<OrderedText> list = new ArrayList<>();
messages.forEach(text -> list.addAll(MinecraftClient.getInstance().textRenderer.wrapLines(text, getWidth() - 40)));
return list;
}

@Override
public int getHeight() {
return height;
}
}
57 changes: 0 additions & 57 deletions src/main/java/net/modfest/scatteredshards/client/ShardToast.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@

public class ClientShardCommand {

private static DynamicCommandExceptionType createInvalidException(String item) {
return new DynamicCommandExceptionType(
obj -> Text.stringifiedTranslatable("error.scattered_shards.invalid_" + item, obj)
);
}

private static final DynamicCommandExceptionType INVALID_SET_ID = createInvalidException("set_id");
private static final DynamicCommandExceptionType INVALID_SHARD_ID = createInvalidException("shard_id");
private static final DynamicCommandExceptionType INVALID_SET_ID = new DynamicCommandExceptionType(
obj -> Text.stringifiedTranslatable("error.scattered_shards.invalid_set_id", obj)
);
private static final DynamicCommandExceptionType INVALID_SHARD_ID = new DynamicCommandExceptionType(
obj -> Text.stringifiedTranslatable("error.scattered_shards.invalid_shard_id", obj)
);

public static int view(CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
Identifier id = context.getArgument("set_id", Identifier.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@
import java.util.Objects;

public class ShardCreatorGuiDescription extends LightweightGuiDescription {
public static final String BASE_KEY = "gui.scattered_shards.creator.";
public static final Text TITLE_TEXT = Text.translatable(BASE_KEY + "title");
public static final Text NAME_TEXT = Text.translatable(BASE_KEY + "field.name");
public static final Text LORE_TEXT = Text.translatable(BASE_KEY + "field.lore");
public static final Text HINT_TEXT = Text.translatable(BASE_KEY + "field.hint");
public static final Text TEXTURE_TEXT = Text.translatable(BASE_KEY + "field.texture");
public static final Text ICON_TEXTURE_TEXT = Text.translatable(BASE_KEY + "icon.texture");
public static final Text ICON_ITEM_TEXT = Text.translatable(BASE_KEY + "icon.item");
public static final Text ITEM_TEXT = Text.translatable(BASE_KEY + "field.item.id");
public static final Text NBT_TEXT = Text.translatable(BASE_KEY + "field.item.nbt");
public static final Text USE_MOD_ICON_TEXT = Text.translatable(BASE_KEY + "toggle.mod_icon");
public static final Text SAVE_TEXT = Text.translatable(BASE_KEY + "button.save");
public static final Text TITLE_TEXT = Text.translatable("gui.scattered_shards.creator.title");
public static final Text NAME_TEXT = Text.translatable("gui.scattered_shards.creator.field.name");
public static final Text LORE_TEXT = Text.translatable("gui.scattered_shards.creator.field.lore");
public static final Text HINT_TEXT = Text.translatable("gui.scattered_shards.creator.field.hint");
public static final Text TEXTURE_TEXT = Text.translatable("gui.scattered_shards.creator.field.texture");
public static final Text ICON_TEXTURE_TEXT = Text.translatable("gui.scattered_shards.creator.icon.texture");
public static final Text ICON_ITEM_TEXT = Text.translatable("gui.scattered_shards.creator.icon.item");
public static final Text ITEM_TEXT = Text.translatable("gui.scattered_shards.creator.field.item.id");
public static final Text NBT_TEXT = Text.translatable("gui.scattered_shards.creator.field.item.nbt");
public static final Text USE_MOD_ICON_TEXT = Text.translatable("gui.scattered_shards.creator.toggle.mod_icon");
public static final Text SAVE_TEXT = Text.translatable("gui.scattered_shards.creator.button.save");

private static final Gson GSON = new Gson();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.modfest.scatteredshards.api.ShardCollection;
import net.modfest.scatteredshards.api.ShardLibrary;
import net.modfest.scatteredshards.api.shard.Shard;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.client.ScatteredShardsClient;
import net.modfest.scatteredshards.client.screen.widget.WLeftRightPanel;
import net.modfest.scatteredshards.client.screen.widget.WShardPanel;
Expand All @@ -28,6 +29,9 @@
import java.util.List;

public class ShardTabletGuiDescription extends LightweightGuiDescription {
public static int INITIAL_SCROLL_POSITION = 0;
public static Identifier INITIAL_SHARD = ShardType.MISSING_ID;

protected final ShardCollection collection;
protected final ShardLibrary library;

Expand All @@ -39,8 +43,7 @@ public ShardTabletGuiDescription(ShardCollection collection, ShardLibrary librar
this.collection = collection;
this.library = library;

shardPanel.setShard(Shard.MISSING_SHARD);
shardPanel.setHidden(true);
shardPanel.setShard(library.shards().get(INITIAL_SHARD).orElse(Shard.MISSING_SHARD));

List<Identifier> ids = new ArrayList<>(this.library.shardSets().keySet());
ids.sort(Comparator.comparing(Identifier::getNamespace));
Expand Down Expand Up @@ -84,6 +87,17 @@ public ShardTabletGuiDescription(ShardCollection collection, ShardLibrary librar
this.setRootPanel(root);

root.validate(this);

if (shardPanel.getShard() == Shard.MISSING_SHARD && INITIAL_SCROLL_POSITION >= 0) { // Only reload scrolling without a selected shard
shardSelector.getScrollBar().setValue(INITIAL_SCROLL_POSITION);
} else if (shardPanel.getShard() != Shard.MISSING_SHARD) { // Try scroll to relevant shard set
for (int i = 0; i < ids.size(); i++) {
if (library.shardSets().get(ids.get(i)).contains(INITIAL_SHARD)) {
shardSelector.getScrollBar().setValue(i - 3); // 7 rows on screen, so center by -3
break;
}
}
}
}

private int getLayoutWidth(WPanelWithInsets panel) {
Expand Down Expand Up @@ -111,5 +125,14 @@ public static class Screen extends CottonClientScreen {
public Screen(ShardCollection collection, ShardLibrary library) {
super(new ShardTabletGuiDescription(collection, library));
}

@Override
public void close() {
if (description instanceof ShardTabletGuiDescription desc) { // Silly, but description has no onClose.
INITIAL_SCROLL_POSITION = desc.shardSelector.getScrollBar().getValue();
INITIAL_SHARD = desc.library.shards().get(desc.shardPanel.getShard()).orElse(ShardType.MISSING_ID);
}
super.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import io.github.cottonmc.cotton.gui.widget.data.InputResult;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
Expand All @@ -17,12 +20,14 @@
import net.modfest.scatteredshards.api.shard.ShardIconOffsets;
import net.modfest.scatteredshards.api.shard.ShardType;
import net.modfest.scatteredshards.client.ScatteredShardsClient;
import net.modfest.scatteredshards.client.screen.ShardTabletGuiDescription;
import net.modfest.scatteredshards.util.ModMetaUtil;

import java.util.function.Consumer;

public class WMiniShard extends WWidget {
private static final Identifier MINI_OUTLINE = ScatteredShards.id("textures/gui/shards/mini_outline.png");
private static final Identifier MINI_OUTLINE_SLIGHT = ScatteredShards.id("textures/gui/shards/mini_outline_slight.png");

protected Shard shard = null;
protected ShardType shardType = null;
Expand Down Expand Up @@ -77,8 +82,14 @@ public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
ScreenDrawing.texturedRect(context, x - 2, y - 2, 16, 20, MINI_OUTLINE, 0, 0, 1, 1, 0xFF_FFFFFF);

renderTooltip(context, x, y, mouseX, mouseY);
} else if ( // Awful bullshit write real code later
MinecraftClient.getInstance().currentScreen instanceof ShardTabletGuiDescription.Screen stgds
&& stgds.getDescription().getRootPanel() instanceof WLeftRightPanel wlrp
&& wlrp.rightPanel instanceof WShardPanel wsp
&& wsp.getShard() == shard
) {
ScreenDrawing.texturedRect(context, x - 2, y - 2, 16, 20, MINI_OUTLINE_SLIGHT, 0, 0, 1, 1, 0xFF_FFFFFF);
}

}

@Override
Expand All @@ -101,6 +112,7 @@ public void addTooltip(TooltipBuilder tooltip) {
@Override
public InputResult onClick(int x, int y, int button) {
if (button == 0) {
MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK.value(), 1.0f, 0.25f));
shardConsumer.accept(shard);
return InputResult.PROCESSED;
} else {
Expand All @@ -117,9 +129,4 @@ public int getWidth() {
public int getHeight() {
return 16;
}

@Override
public boolean canResize() {
return false;
}
}
Loading

0 comments on commit 644c0da

Please sign in to comment.