Skip to content

Commit

Permalink
use vanilla tooltip drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed May 24, 2024
1 parent f71c881 commit 468b1a5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 125 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ org.gradle.configureondemand=true
# Enable advanced multi-module optimizations (share tiny-remaper instance between projects)
fabric.loom.multiProjectOptimisation=true
# Mod Properties
baseVersion = 2.7.2
baseVersion = 2.7.3
defaultBranch = 1.20
branch = 1.20
2 changes: 1 addition & 1 deletion libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fl = "0.15.0"
yarn = "1.20.1+build.10"
fapi = "0.83.0+1.20.1"
kaleidoConfig = "0.1.1+1.1.0-beta.3"
surveyor = "0.4.3+1.20"
surveyor = "0.5.0+1.20"

[plugins]
loom = { id = "fabric-loom", version.ref = "loom" }
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/folk/sisby/antique_atlas/gui/AtlasScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -727,7 +726,7 @@ private void renderPlayer(DrawContext context, PlayerSummary player, float iconS
RenderSystem.setShaderColor(1, 1, 1, 1);

if (hovering && !player.username().equals(this.player.getGameProfile().getName())) {
drawTooltip(Collections.singletonList(Text.literal(player.username()).formatted(player.online() ? Formatting.LIGHT_PURPLE : Formatting.GRAY)), textRenderer);
context.drawTooltip(textRenderer, Text.literal(player.username()).formatted(player.online() ? Formatting.LIGHT_PURPLE : Formatting.GRAY), (int) getMouseX() - getGuiX(), (int) getMouseY() - getGuiY());
}
}

Expand All @@ -750,7 +749,7 @@ private void renderMarker(DrawContext context, Landmark<?> landmark, MarkerTextu
RenderSystem.setShaderColor(1, 1, 1, 1);

if (hovering && landmark.name() != null && !landmark.name().getString().isEmpty()) {
drawTooltip(Collections.singletonList(landmark.name()), textRenderer);
context.drawTooltip(textRenderer, landmark.name(), (int) getMouseX() - getGuiX(), (int) getMouseY() - getGuiY());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import com.mojang.blaze3d.systems.RenderSystem;
import folk.sisby.antique_atlas.AntiqueAtlas;
import folk.sisby.antique_atlas.gui.core.ToggleButtonComponent;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;

public class BookmarkButton extends ToggleButtonComponent {
public static final Identifier TEXTURE_LEFT = AntiqueAtlas.id("textures/gui/bookmark_left.png");
public static final Identifier TEXTURE_RIGHT = AntiqueAtlas.id("textures/gui/bookmark_right.png");
Expand Down Expand Up @@ -84,7 +81,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic

public void renderTooltip(DrawContext context, int mouseX, int mouseY, float partialTick, boolean mouseOver) {
if (mouseOver && !title.getString().isEmpty()) {
drawTooltip(Collections.singletonList(title), MinecraftClient.getInstance().textRenderer);
context.drawTooltip(textRenderer, title, mouseX, mouseY);
}
}
}
122 changes: 10 additions & 112 deletions src/main/java/folk/sisby/antique_atlas/gui/core/Component.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package folk.sisby.antique_atlas.gui.core;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import org.lwjgl.opengl.GL11;

import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;

/**
* Core visual component class, which facilitates hierarchy. You can add child
Expand All @@ -18,11 +17,6 @@
* parent component.
*/
public class Component extends Screen {
@FunctionalInterface
interface UiCall {
boolean call(Component c);
}

private Component parent = null;
private final List<Component> children = new CopyOnWriteArrayList<>();

Expand All @@ -36,10 +30,6 @@ interface UiCall {
*/
int contentWidth;
int contentHeight;
/**
* If true, content size will be validated on the next update.
*/
private boolean sizeIsInvalid = false;
/**
* If true, this GUI will not be rendered.
*/
Expand All @@ -50,7 +40,6 @@ interface UiCall {
*/
private int guiX = 0, guiY = 0;

// TODO
public Component() {
super(Text.literal("component"));
}
Expand All @@ -68,7 +57,7 @@ public void setGuiCoords(int x, int y) {
child.offsetGuiCoords(dx, dy);
}
if (parent != null && (dx != 0 || dy != 0)) {
parent.invalidateSize();
parent.updateSize();
}
}

Expand Down Expand Up @@ -151,7 +140,7 @@ public void setSize(int width, int height) {
this.properHeight = height;
this.contentWidth = width;
this.contentHeight = height;
invalidateSize();
updateSize();
}

/**
Expand Down Expand Up @@ -198,7 +187,7 @@ private void doAddChild(Component inFrontOf, Component child, Component behind)
if (MinecraftClient.getInstance() != null) {
child.init(MinecraftClient.getInstance(), width, height);
}
invalidateSize();
updateSize();
}

/**
Expand All @@ -208,15 +197,15 @@ protected Component removeChild(Component child) {
if (child != null && children.contains(child)) {
child.parent = null;
children.remove(child);
invalidateSize();
updateSize();
onChildClosed(child);
}
return child;
}

void removeAllChildren() {
children.clear();
invalidateSize();
updateSize();
}

/**
Expand All @@ -230,21 +219,21 @@ List<Component> getChildren() {
return children;
}

boolean iterateInput(UiCall callMethod) {
boolean iterateInput(Predicate<Component> callMethod) {
// Traverse children backwards, because the topmost child should be the
// first to process input:
ListIterator<Component> iter = children.listIterator(children.size());
while (iter.hasPrevious()) {
Component child = iter.previous();
if (callMethod.call(child)) {
if (callMethod.test(child)) {
return true;
}
}

return false;
}

boolean iterateMouseInput(UiCall callMethod) {
boolean iterateMouseInput(Predicate<Component> callMethod) {
return iterateInput(callMethod);
}

Expand Down Expand Up @@ -338,11 +327,6 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic
child.render(context, mouseX, mouseY, partialTick);
}
}
// Draw any hovering text requested by child components:
if (hoveringTextInfo.shouldDraw) {
drawHoveringText2(context, hoveringTextInfo.lines, hoveringTextInfo.x, hoveringTextInfo.y, hoveringTextInfo.font);
hoveringTextInfo.shouldDraw = false;
}
}

/**
Expand All @@ -365,11 +349,7 @@ public void tick() {
for (Component child : children) {
child.tick();
}

super.tick();
if (sizeIsInvalid) {
validateSize();
}
}

@Override
Expand Down Expand Up @@ -403,21 +383,7 @@ void setClipped(boolean value) {
this.isClipped = value;
}

/**
* Cause the size of the component to be recalculate on the next update
* tick. If this GUI has a parent, the parent's size will be invalidated too.
*/
private void invalidateSize() {
sizeIsInvalid = true;
if (parent != null) {
parent.invalidateSize();
}
}

/**
* Recalculate the dimensions of the contents (children) of this GUI.
*/
void validateSize() {
void updateSize() {
int leftmost = Integer.MAX_VALUE;
int rightmost = Integer.MIN_VALUE;
int topmost = Integer.MAX_VALUE;
Expand All @@ -442,81 +408,13 @@ void validateSize() {
}
contentWidth = Math.max(properWidth, rightmost - leftmost);
contentHeight = Math.max(properHeight, bottommost - topmost);
sizeIsInvalid = false;
}

@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return mouseX >= getGuiX() && mouseX < getGuiX() + getWidth() && mouseY >= getGuiY() && mouseY < getGuiY() + getHeight();
}

/**
* Draws a standard Minecraft hovering text window, constrained by this
* component's dimensions (i.e. if it won't fit in when drawn to the left
* of the cursor, it will be drawn to the right instead).
*/
private void drawHoveringText2(DrawContext context, List<Text> lines, double x, double y, TextRenderer font) {
boolean stencilEnabled = GL11.glIsEnabled(GL11.GL_STENCIL_TEST);
if (stencilEnabled) GL11.glDisable(GL11.GL_STENCIL_TEST);
context.getMatrices().push();
context.getMatrices().translate(x, y, 0);
context.drawTooltip(font, lines, 0, 0);
context.getMatrices().pop();
if (stencilEnabled) GL11.glEnable(GL11.GL_STENCIL_TEST);
}

/**
* Returns the top level parent of this component, or itself if it has no
* parent. Useful for correctly drawing hovering text.
*/
private Component getTopLevelParent() {
Component component = this;
while (component.parent != null) {
component = component.parent;
}
return component;
}

/**
* Draws a text tooltip at mouse coordinates.
* <p>
* Same as {@link #drawHoveringText2(DrawContext, List, double, double, TextRenderer)}, but
* the text is drawn on the top level parent component, after all its child
* components have finished drawing. This allows the hovering text to be
* unobscured by other components.
* </p>
* <p>
* Only one instance of hovering text can be drawn via this method, i.e.
* from several components which occupy the same position on the screen.
* </p>
*/
protected void drawTooltip(List<Text> lines, TextRenderer font) {
Component topLevel = getTopLevelParent();
topLevel.hoveringTextInfo.lines = lines;
topLevel.hoveringTextInfo.x = getMouseX();
topLevel.hoveringTextInfo.y = getMouseY();
topLevel.hoveringTextInfo.font = font;
topLevel.hoveringTextInfo.shouldDraw = true;
}

/**
* Wrapper for data used to draw hovering text at the end of rendering
* current frame. It is used by child components that wish to draw hovering
* text unobscured by their neighboring components.
*/
private final HoveringTextInfo hoveringTextInfo = new HoveringTextInfo();

private static class HoveringTextInfo {
List<Text> lines;
double x, y;
TextRenderer font;
/**
* Whether to draw this hovering text during rendering current frame.
* This flag is reset to false after rendering finishes.
*/
boolean shouldDraw = false;
}

/**
* Remove itself from its parent component (if any), notifying it.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public boolean mouseScrolled(double mx, double my, double wheelMove) {
* during initGui().
*/
public void setScrollPos(int scrollPos) {
viewport.content.validateSize();
viewport.validateSize();
viewport.content.updateSize();
viewport.updateSize();
doSetScrollPos(scrollPos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public int getHeight() {
}

@Override
protected void validateSize() {
super.validateSize();
protected void updateSize() {
super.updateSize();
// Update the clipping flag on content's child components:
for (Component child : this.getChildren()) {
child.setClipped(child.getGuiY() > getGuiY() + properHeight ||
Expand Down

0 comments on commit 468b1a5

Please sign in to comment.