Skip to content

Commit a824dcc

Browse files
authored
Small fixes (#74)
* New text texture filtering detection * Better hover control
1 parent 1419011 commit a824dcc

File tree

5 files changed

+44
-40
lines changed

5 files changed

+44
-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 && enabled)
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 && enabled;
570+
return shouldDraw() && enabled;
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 visible && enabled && isPointInPane(mx, my);
654+
return shouldDraw() && enabled && isPointInPane(mx, my);
646655
}
647656

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

824-
if (onHover == null)
825-
{
826-
return;
827-
}
828-
829-
if (this.wasCursorInPane && !onHover.isVisible() && onHover.isEnabled())
834+
if (onHover != null && this.wasCursorInPane && onHover.hoverSource == null && onHover.isEnabled())
830835
{
831-
onHover.show();
836+
onHover.hoverSource = this;
832837
}
833-
// if onHover was already drawn then we good
834-
// else we have to wait for next frame
835-
else if (!onHover.wasCursorInPane && !this.wasCursorInPane && this.wasCursorInPane == wasCursorInPaneLastTick
836-
&& onHover.isVisible())
838+
// if onHover was already drawn then we good, else we have to wait for next frame
839+
else if (!this.wasCursorInPane && !wasCursorInPaneLastTick)
837840
{
838-
onHover.hide();
841+
if (onHover != null && onHover.hoverSource == this && !onHover.wasCursorInPane)
842+
{
843+
onHover.hoverSource = null;
844+
}
845+
else if (hoverSource != null && !hoverSource.wasCursorInPane)
846+
{
847+
hoverSource = null;
848+
}
839849
}
840850
}
841851

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

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

3840
public static final double DEFAULT_TEXT_SCALE = 1.0d;
@@ -308,30 +310,21 @@ else if (textAlignment.isVerticalCentered())
308310

309311
ms.pushPose();
310312
ms.translate(x + offsetX, y + offsetY, 0.0d);
313+
ms.scale((float) textScale, (float) textScale, 1.0f);
311314

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

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.minecraftforge.eventbus.api.SubscribeEvent;
2626
import org.lwjgl.glfw.GLFW;
2727

28+
import java.nio.file.Path;
2829
import java.util.function.Consumer;
2930

3031
public class ClientEventSubscriber
@@ -62,7 +63,7 @@ public static void onClientTickEvent(final ClientTickEvent event)
6263
final BOWindow window = new BOWindow();
6364
window.addChild(createTestGuiButton(0, "General All-in-one", new ResourceLocation(BlockUI.MOD_ID, "gui/test.xml"), parent -> {
6465
parent.findPaneOfTypeByID("missing_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.ofMinecraftFolder(BlockUI.MOD_ID, "missing_out_of_jar.png"), false);
65-
parent.findPaneOfTypeByID("working_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.ofMinecraftFolder(BlockUI.MOD_ID, "../../src/test/resources/button.png"), false);
66+
parent.findPaneOfTypeByID("working_out_of_jar", Image.class).setImage(OutOfJarResourceLocation.of(BlockUI.MOD_ID, Path.of("../../src/test/resources/button.png")), false);
6667
}));
6768
window.addChild(createTestGuiButton(1, "Tooltip Positioning", new ResourceLocation(BlockUI.MOD_ID, "gui/test2.xml")));
6869
window.addChild(createTestGuiButton(2, "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
@@ -322,6 +322,6 @@ default MutableComponent getLabelNew(final int index)
322322
@Override
323323
public boolean isPointInPane(final double mx, final double my)
324324
{
325-
return super.isPointInPane(mx, my) || (overlay.isVisible() && list.isPointInPane(mx, my));
325+
return super.isPointInPane(mx, my) || (overlay.shouldDraw() && list.isPointInPane(mx, my));
326326
}
327327
}

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)