Skip to content

Commit 1df31d6

Browse files
committed
Small fixes (#74)
* New text texture filtering detection * Better hover control
1 parent 028a392 commit 1df31d6

File tree

5 files changed

+43
-40
lines changed

5 files changed

+43
-40
lines changed

src/main/java/com/ldtteam/blockui/Pane.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class Pane extends UiRenderMacros
4545
// Runtime
4646
protected BOWindow window;
4747
protected View parent;
48+
protected Pane hoverSource = null;
4849
/**
4950
* Should be only used during drawing methods. Outside drawing scope value may be outdated.
5051
*/
@@ -204,6 +205,14 @@ public boolean isVisible()
204205
return visible;
205206
}
206207

208+
/**
209+
* @return visible because of anything
210+
*/
211+
public boolean shouldDraw()
212+
{
213+
return visible || hoverSource != null;
214+
}
215+
207216
public void setVisible(final boolean v)
208217
{
209218
visible = v;
@@ -337,7 +346,7 @@ public void draw(final BOGuiGraphics target, final double mx, final double my)
337346
wasCursorInPane = isPointInPane(mx, my);
338347
handleHover(oldCursorInPane);
339348

340-
if (visible)
349+
if (shouldDraw())
341350
{
342351
if (wasCursorInPane && isEnabled())
343352
{
@@ -370,7 +379,7 @@ public void draw(final BOGuiGraphics target, final double mx, final double my)
370379
*/
371380
public void drawLast(final BOGuiGraphics target, final double mx, final double my)
372381
{
373-
if (visible)
382+
if (shouldDraw())
374383
{
375384
drawSelfLast(target, mx, my);
376385
}
@@ -411,7 +420,7 @@ public void drawSelfLast(final BOGuiGraphics ms, final double mx, final double m
411420
*/
412421
public boolean isPointInPane(final double mx, final double my)
413422
{
414-
return isVisible() && mx >= x && mx < (x + width) && my >= y && my < (y + height);
423+
return shouldDraw() && mx >= x && mx < (x + width) && my >= y && my < (y + height);
415424
}
416425

417426
/**
@@ -558,7 +567,7 @@ public void putInside(final View newParent)
558567

559568
public boolean isClickable()
560569
{
561-
return visible && isEnabled();
570+
return shouldDraw() && isEnabled();
562571
}
563572

564573
// ----------Mouse-------------//
@@ -642,7 +651,7 @@ public boolean handleRightClick(final double mx, final double my)
642651
*/
643652
public boolean canHandleClick(final double mx, final double my)
644653
{
645-
return isVisible() && isEnabled() && isPointInPane(mx, my);
654+
return shouldDraw() && isEnabled() && isPointInPane(mx, my);
646655
}
647656

648657
/**
@@ -820,23 +829,24 @@ protected void handleHover(final boolean wasCursorInPaneLastTick)
820829
{
821830
onHover = window.findPaneByID(onHoverId); // do not use setHoverPane, here onHover is defined in xml
822831
Objects.requireNonNull(onHover, String.format("Hover pane \"%s\" for \"%s\" was not found.", onHoverId, id));
832+
onHover.hide(); // automatically hide it (in case someone forgot to do so in xml)
823833
}
824834

825-
if (onHover == null)
826-
{
827-
return;
828-
}
829-
830-
if (this.wasCursorInPane && !onHover.isVisible() && onHover.isEnabled())
835+
if (onHover != null && this.wasCursorInPane && onHover.hoverSource == null && onHover.isEnabled())
831836
{
832-
onHover.show();
837+
onHover.hoverSource = this;
833838
}
834-
// if onHover was already drawn then we good
835-
// else we have to wait for next frame
836-
else if (!onHover.wasCursorInPane && !this.wasCursorInPane && this.wasCursorInPane == wasCursorInPaneLastTick
837-
&& onHover.isVisible())
839+
// if onHover was already drawn then we good, else we have to wait for next frame
840+
else if (!this.wasCursorInPane && !wasCursorInPaneLastTick)
838841
{
839-
onHover.hide();
842+
if (onHover != null && onHover.hoverSource == this && !onHover.wasCursorInPane)
843+
{
844+
onHover.hoverSource = null;
845+
}
846+
else if (hoverSource != null && !hoverSource.wasCursorInPane)
847+
{
848+
hoverSource = null;
849+
}
840850
}
841851
}
842852

src/main/java/com/ldtteam/blockui/controls/AbstractTextElement.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
*/
3232
public abstract class AbstractTextElement extends Pane
3333
{
34+
@Deprecated(forRemoval = true, since = "1.20.1")
3435
public static final int FILTERING_ROUNDING = 50;
36+
public static final float FILTERING_MAX_SCALE = 2; // enable texture filtering when text is below this scale (in monitor pixels)
3537
public static final float FILTERING_THRESHOLD = 0.02f; // should be 1/FILTERING_ROUNDING
3638

3739
public static final double DEFAULT_TEXT_SCALE = 1.0d;
@@ -307,30 +309,21 @@ else if (textAlignment.isVerticalCentered())
307309

308310
ms.pushPose();
309311
ms.translate(x + offsetX, y + offsetY, 0.0d);
312+
ms.scale((float) textScale, (float) textScale, 1.0f);
310313

311314
final Matrix4f matrix4f = ms.last().pose();
312315

316+
// we want to see how big is one scaled pixel on monitor (using one texel)
317+
final int fbW = window.getScreen().getFramebufferWidth(), fbH = window.getScreen().getFramebufferHeight();
313318
final Vector4f temp = new Vector4f(1, 1, 0, 0);
314-
matrix4f.transform(temp);
315-
final float oldScaleX = temp.x();
316-
final float oldScaleY = temp.y();
317-
final float newScaleX = (float) Math.round(oldScaleX * textScale * FILTERING_ROUNDING) / FILTERING_ROUNDING;
318-
final float newScaleY = (float) Math.round(oldScaleY * textScale * FILTERING_ROUNDING) / FILTERING_ROUNDING;
319-
320-
if (Math.abs((float) Math.round(newScaleX) - newScaleX) > FILTERING_THRESHOLD
321-
|| Math.abs((float) Math.round(newScaleY) - newScaleY) > FILTERING_THRESHOLD)
322-
{
323-
// smooth the texture
324-
// if (newScaleX < window.getScreen().getVanillaGuiScale() || newScaleY < window.getScreen().getVanillaGuiScale())
325-
// TODO: figure out how to not linear filter when mag filter is used, might just want to use direct ogl call
326-
NeoForgeRenderTypes.enableTextTextureLinearFiltering = true;
327-
ms.scale((float) textScale, (float) textScale, 1.0f);
328-
}
329-
else
330-
{
331-
// round scale if not smoothing
332-
ms.scale(newScaleX / oldScaleX, newScaleY / oldScaleY, 1.0f);
333-
}
319+
matrix4f.transform(temp); // PVM
320+
temp.w = 1; // vector -> point
321+
temp.mulProject(RenderSystem.getProjectionMatrix()); // projection, perspective
322+
temp.add(1, 1, 0, 0); // viewport, discard non (x,y)
323+
temp.mul(fbW / 2.0f, fbH / 2.0f, 0, 0);
324+
325+
final float scale = temp.distanceSquared(FILTERING_THRESHOLD, fbH - FILTERING_THRESHOLD, 0, 0);
326+
NeoForgeRenderTypes.enableTextTextureLinearFiltering = Math.abs(temp.x - fbH + temp.y) > FILTERING_THRESHOLD || scale < FILTERING_MAX_SCALE * FILTERING_MAX_SCALE;
334327

335328
final MultiBufferSource.BufferSource drawBuffer = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder());
336329
int lineShift = 0;

src/main/java/com/ldtteam/blockui/mod/ClientEventSubscriber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void onClientTickEvent(final ClientTickEvent event)
7373
window.addChild(dumpAtlases);
7474
window.addChild(createTestGuiButton(id++, "General All-in-one", new ResourceLocation(BlockUI.MOD_ID, "gui/test.xml"), parent -> {
7575
parent.findPaneOfTypeByID("missing_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.ofMinecraftFolder(BlockUI.MOD_ID, "missing_out_of_jar.png"), false);
76-
parent.findPaneOfTypeByID("working_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.ofMinecraftFolder(BlockUI.MOD_ID, "../../../src/test/resources/button.png"), false);
76+
parent.findPaneOfTypeByID("working_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.of(BlockUI.MOD_ID, Path.of("../../src/test/resources/button.png")), false);
7777
}));
7878
window.addChild(createTestGuiButton(id++, "Tooltip Positioning", new ResourceLocation(BlockUI.MOD_ID, "gui/test2.xml")));
7979
window.addChild(createTestGuiButton(id++, "ItemIcon To BlockState", new ResourceLocation(BlockUI.MOD_ID, "gui/test3.xml"), BlockStateTestGui::setup));

src/main/java/com/ldtteam/blockui/views/DropDownList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,6 @@ default MutableComponent getLabel(final int index)
320320
@Override
321321
public boolean isPointInPane(final double mx, final double my)
322322
{
323-
return super.isPointInPane(mx, my) || (overlay.isVisible() && list.isPointInPane(mx, my));
323+
return super.isPointInPane(mx, my) || (overlay.shouldDraw() && list.isPointInPane(mx, my));
324324
}
325325
}

src/main/java/com/ldtteam/blockui/views/View.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void onUpdate()
246246
// copy to prevent CME during scrolling list updates, ctor uses fast array copy so it's cheap
247247
for (final Pane child : new ArrayList<>(children))
248248
{
249-
if (child.isVisible())
249+
if (child.shouldDraw())
250250
{
251251
child.onUpdate();
252252
}

0 commit comments

Comments
 (0)