Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circle in tooltip #15

Merged
merged 7 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified gradlew
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions src/client/java/dev/enjarai/trickster/TricksterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;

public class TricksterClient implements ClientModInitializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Mixin(PlayerEntityRenderer.class)
public abstract class PlayerRendererMixin {
@Unique
public SpellCircleRenderer trickster$renderer = new SpellCircleRenderer();
public SpellCircleRenderer trickster$renderer = new SpellCircleRenderer(false);

@Inject(method = "render(Lnet/minecraft/client/network/AbstractClientPlayerEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V", at = @At("HEAD"))
public void trickster$onRender(AbstractClientPlayerEntity player, float $$1, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int $$5, CallbackInfo ci) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.enjarai.trickster.mixin.client;

import dev.enjarai.trickster.SpellTooltipData;
import dev.enjarai.trickster.render.SpellTooltipComponent;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.item.tooltip.TooltipData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(TooltipComponent.class)
public interface SpellToolTipMixin {

@Inject(method = "of(Lnet/minecraft/item/tooltip/TooltipData;)Lnet/minecraft/client/gui/tooltip/TooltipComponent;", at = @At("HEAD"), cancellable = true)
private static void trickster$getTooltipData(TooltipData data, CallbackInfoReturnable<TooltipComponent> cir) {
if (data instanceof SpellTooltipData spellData)
cir.setReturnValue(new SpellTooltipComponent(spellData.contents()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SpellCircleBlockEntityRenderer implements BlockEntityRenderer<Spell
private final SpellCircleRenderer renderer;

public SpellCircleBlockEntityRenderer(BlockEntityRendererFactory.Context ctx) {
this.renderer = new SpellCircleRenderer();
this.renderer = new SpellCircleRenderer(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix4f;
Expand All @@ -32,19 +33,23 @@ public class SpellCircleRenderer {
public static final int CLICK_HITBOX_SIZE = 6;

private final boolean inUI;
private final boolean inEditor;

private Supplier<SpellPart> drawingPartGetter;
private Supplier<List<Byte>> drawingPatternGetter;
private double mouseX;
private double mouseY;

public SpellCircleRenderer() {
this.inUI = false;
public SpellCircleRenderer(Boolean inUI) {
this.inUI = inUI;
this.inEditor = false;
}

public SpellCircleRenderer(Supplier<SpellPart> drawingPartGetter, Supplier<List<Byte>> drawingPatternGetter) {
this.drawingPartGetter = drawingPartGetter;
this.drawingPatternGetter = drawingPatternGetter;
this.inUI = true;
this.inEditor = true;
}

public void setMousePosition(double mouseX, double mouseY) {
Expand Down Expand Up @@ -135,24 +140,26 @@ protected void drawGlyph(MatrixStack matrices, VertexConsumerProvider vertexCons
renderPart(matrices, vertexConsumers, Optional.of(part), x, y, size / 3, startingAngle, delta, alphaGetter, normal);
} else {
matrices.push();
drawSide(matrices, vertexConsumers, parent, x, y, size, glyph);
drawSide(matrices, vertexConsumers, parent, x, y, size, alphaGetter, glyph);
matrices.pop();

if (!inUI) {
matrices.push();
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(180));
drawSide(matrices, vertexConsumers, parent, -x, y, size, glyph);
drawSide(matrices, vertexConsumers, parent, -x, y, size, alphaGetter, glyph);
matrices.pop();
}
}
}

private void drawSide(MatrixStack matrices, VertexConsumerProvider vertexConsumers, SpellPart parent, float x, float y, float size, Fragment glyph) {
private void drawSide(MatrixStack matrices, VertexConsumerProvider vertexConsumers, SpellPart parent, float x, float y, float size, Function<Float, Float> alphaGetter, Fragment glyph) {
var alpha = alphaGetter.apply(size);

if (glyph instanceof PatternGlyph pattern) {
var patternSize = size / PATTERN_TO_PART_RATIO;
var pixelSize = patternSize / PART_PIXEL_RADIUS;

var isDrawing = inUI && drawingPartGetter.get() == parent;
var isDrawing = inEditor && drawingPartGetter.get() == parent;
var patternList = isDrawing ? drawingPatternGetter.get() : pattern.orderedPattern();

for (int i = 0; i < 9; i++) {
Expand All @@ -161,10 +168,10 @@ private void drawSide(MatrixStack matrices, VertexConsumerProvider vertexConsume
var isLinked = patternList.contains(Integer.valueOf(i).byteValue());
float dotScale = 1;

if (inUI && isInsideHitbox(pos, pixelSize, mouseX, mouseY) && isCircleClickable(size)) {
if (inEditor && isInsideHitbox(pos, pixelSize, mouseX, mouseY) && isCircleClickable(size)) {
dotScale = 1.6f;
} else if (!isLinked) {
if (inUI && isCircleClickable(size)) {
if (inEditor && isCircleClickable(size)) {
var mouseDistance = new Vector2f((float) (mouseX - pos.x), (float) (mouseY - pos.y)).length();
dotScale = Math.clamp(patternSize / mouseDistance - 0.2f, 0, 1);
} else {
Expand All @@ -180,21 +187,21 @@ private void drawSide(MatrixStack matrices, VertexConsumerProvider vertexConsume
c.accept(pos.x - dotSize, pos.y + dotSize);
c.accept(pos.x + dotSize, pos.y + dotSize);
c.accept(pos.x + dotSize, pos.y - dotSize);
}, 0, isDrawing && isLinked ? 0.5f : 1, isDrawing && isLinked ? 0.5f : 1, 1, 0.5f);
}, 0, isDrawing && isLinked ? 0.5f : 1, isDrawing && isLinked ? 0.5f : 1, 1, 0.5f * alpha);
}

Vector2f last = null;
for (var b : patternList) {
var now = getPatternDotPosition(x, y, b, patternSize);
if (last != null) {
drawGlyphLine(matrices, vertexConsumers, last, now, pixelSize, isDrawing, 1, 0.5f);
drawGlyphLine(matrices, vertexConsumers, last, now, pixelSize, isDrawing, 1, 0.5f * alpha);
}
last = now;
}

if (inUI && isDrawing && last != null) {
if (inEditor && isDrawing && last != null) {
var now = new Vector2f((float) mouseX, (float) mouseY);
drawGlyphLine(matrices, vertexConsumers, last, now, pixelSize, true, 1, 0.5f);
drawGlyphLine(matrices, vertexConsumers, last, now, pixelSize, true, 1, 0.5f * alpha);
}
} else {
var textRenderer = MinecraftClient.getInstance().textRenderer;
Expand All @@ -208,9 +215,11 @@ private void drawSide(MatrixStack matrices, VertexConsumerProvider vertexConsume
matrices.translate(x, y, 0);
matrices.scale(size / 1.3f / width, size / 1.3f / width, 1);

var color = ColorHelper.Argb.withAlpha((int) (alpha * 0xff), 0xffffff);

textRenderer.draw(
text,
-width / 2f, -height / 2f, 0x88ffffff, false,
-width / 2f, -height / 2f, color, false,
matrices.peek().getPositionMatrix(),
vertexConsumers, TextRenderer.TextLayerType.NORMAL,
0, 0xf000f0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.enjarai.trickster.render;

import dev.enjarai.trickster.spell.SpellPart;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix4f;

import java.util.Optional;

public class SpellTooltipComponent implements TooltipComponent {

private final SpellCircleRenderer renderer;

private final SpellPart spell;

public SpellTooltipComponent(SpellPart spell) {
this.spell = spell;
this.renderer = new SpellCircleRenderer(true);
}

@Override
public int getHeight() {
return 110;
}

@Override
public int getWidth(TextRenderer textRenderer) {
return 110;
}

@Override
public void drawText(TextRenderer textRenderer, int x, int y, Matrix4f matrix, VertexConsumerProvider.Immediate vertexConsumers) {

}

@Override
public void drawItems(TextRenderer textRenderer, int x, int y, DrawContext context) {
var matrices = context.getMatrices();
var vertexConsumers = context.getVertexConsumers();
var delta = 0;
var size = 30f;

matrices.push();
renderer.renderPart(matrices, vertexConsumers, Optional.of(spell), x + (float) getWidth(textRenderer) / 2, y + (float) getHeight() / 2, size,
0.0, delta, a -> a/size, new Vec3d(0, 0, -1) );
matrices.pop();
}
}
1 change: 1 addition & 0 deletions src/client/resources/trickster.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"plugin": "dev.enjarai.trickster.ConditionalMixins",
"client": [
"AbstractClientPlayerEntityMixin",
"SpellToolTipMixin",
"HeldItemRendererMixin",
"InGameHudMixin",
"MouseMixin",
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/dev/enjarai/trickster/SpellTooltipData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.enjarai.trickster;

import dev.enjarai.trickster.spell.SpellPart;

import net.minecraft.item.tooltip.TooltipData;

public record SpellTooltipData(SpellPart spell) implements TooltipData {
public SpellTooltipData(SpellPart spell) {
this.spell = spell;
}

public SpellPart contents() {
return this.spell;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.enjarai.trickster.item;


import dev.enjarai.trickster.screen.ScrollAndQuillScreenHandler;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/dev/enjarai/trickster/mixin/SpellTooltipMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.enjarai.trickster.mixin;


import dev.enjarai.trickster.SpellTooltipData;
import dev.enjarai.trickster.item.component.ModComponents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;


@Mixin(Item.class)
public abstract class SpellTooltipMixin {

@Inject(method = "getTooltipData", at = @At("HEAD"), cancellable = true)
private void trickster$getSpellTooltipData(ItemStack stack, CallbackInfoReturnable<Optional<TooltipData>> cir) {
var spellComponent = stack.get(ModComponents.SPELL);
if (spellComponent != null) {
cir.setReturnValue(Optional.of(new SpellTooltipData(spellComponent.spell())));
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/trickster.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"AbstractBlockStateMixin",
"LivingEntityMixin",
"SpellTooltipMixin",
"WorldChunkMixin",
"event.LivingEntityMixin",
"event.ServerPlayerInteractionManagerMixin"
Expand Down