Skip to content

Commit

Permalink
Merge branch 'prs-base' into prs
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Aug 31, 2024
2 parents 9bd1ccf + 72772c5 commit 8659353
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions HMCL/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ tasks.getByName<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("sha
"javafx.graphics/javafx.css",
"javafx.graphics/com.sun.javafx.stage",
"javafx.graphics/com.sun.prism",
"javafx.graphics/com.sun.javafx.scene",
"javafx.graphics/com.sun.javafx.sg.prism",
"javafx.controls/com.sun.javafx.scene.control",
"javafx.controls/com.sun.javafx.scene.control.behavior",
"javafx.controls/javafx.scene.control.skin",
Expand Down
11 changes: 10 additions & 1 deletion HMCL/src/main/java/net/burningtnt/hmclprs/PRCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import net.burningtnt.hmclprs.hooks.EntryPoint;
import net.burningtnt.hmclprs.hooks.Final;
import net.burningtnt.hmclprs.hooks.HookContainer;
import net.burningtnt.hmclprs.impl.JavaFXSmoothImageRendering;
import org.jackhuang.hmcl.ui.FXUtils;

import javax.swing.*;
Expand Down Expand Up @@ -66,6 +68,11 @@ public static String onInitApplicationDefaultUpdateLink() {
return PRCollectionConstants.UPDATE_LINK;
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.INJECT)
public static void onApplicationLaunched() {
JavaFXSmoothImageRendering.initialize();
}

@EntryPoint(when = EntryPoint.LifeCycle.RUNTIME, type = EntryPoint.Type.REDIRECT)
public static String onGetApplicationRawVersion() {
return defaultVersion;
Expand All @@ -84,8 +91,9 @@ public static VBox onBuildAnnouncementPane(ObservableList<Node> nodes) {
content.setStyle("-fx-border-color: -fx-base-darker-color; -fx-border-radius: 5px; -fx-border-width: 4px;");
{
ImageView view = new ImageView(FXUtils.newBuiltinImage("/assets/img/teacon-banner.png"));
view.setSmooth(true);
view.setPreserveRatio(true);
view.fitWidthProperty().bind(content.widthProperty().subtract(10));
view.fitWidthProperty().bind(content.widthProperty().subtract(8));
Rectangle clip = new Rectangle();
FXUtils.onChange(view.layoutBoundsProperty(), layout -> {
clip.setX(layout.getMinX());
Expand All @@ -102,6 +110,7 @@ public static VBox onBuildAnnouncementPane(ObservableList<Node> nodes) {
content.getChildren().add(view);

content.setOnMouseClicked(e -> FXUtils.openLink("https://www.teacon.cn/"));
content.setCursor(Cursor.HAND);

Platform.runLater(content::requestLayout); // Java 8 is Java 8.
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package net.burningtnt.hmclprs.impl;

import com.sun.javafx.geom.BaseBounds;
import com.sun.javafx.geom.transform.BaseTransform;
import com.sun.javafx.scene.ImageViewHelper;
import com.sun.javafx.sg.prism.NGImageView;
import com.sun.javafx.sg.prism.NGNode;
import com.sun.prism.Graphics;
import com.sun.prism.Image;
import com.sun.prism.Texture;
import javafx.scene.Node;
import org.jackhuang.hmcl.util.logging.Logger;

import java.lang.reflect.Field;

public final class JavaFXSmoothImageRendering {
private JavaFXSmoothImageRendering() {
}

public static void initialize() {
try {
Class.forName(ImageViewHelper.class.getName(), true, ImageViewHelper.class.getClassLoader());

Field accessorField = ImageViewHelper.class.getDeclaredField("imageViewAccessor");
accessorField.setAccessible(true);
ImageViewHelper.ImageViewAccessor delegate = (ImageViewHelper.ImageViewAccessor) accessorField.get(null);

Field imageField = NGImageView.class.getDeclaredField("image");
imageField.setAccessible(true);

accessorField.set(null, new ImageViewHelper.ImageViewAccessor() {
@Override
public NGNode doCreatePeer(Node node) {
return new NGImageView() {
private boolean isSmooth = false;

@Override
public void setSmooth(boolean s) {
isSmooth = true;
}

@Override
protected void renderContent(Graphics g) {
if (!isSmooth) {
return;
}

Texture tex = g.getResourceFactory().getCachedTexture(getImage(), com.sun.prism.Texture.WrapMode.CLAMP_TO_EDGE);
tex.setLinearFiltering(true);
tex.unlock();
super.renderContent(g);
}

private Image getImage() {
Image image;
try {
image = (Image) imageField.get(this);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
}
return image;
}
};
}

@Override
public void doUpdatePeer(Node node) {
delegate.doUpdatePeer(node);
}

@Override
public BaseBounds doComputeGeomBounds(Node node, BaseBounds bounds, BaseTransform tx) {
return delegate.doComputeGeomBounds(node, bounds, tx);
}

@Override
public boolean doComputeContains(Node node, double localX, double localY) {
return delegate.doComputeContains(node, localX, localY);
}
});
} catch (Throwable t) {
Logger.LOG.warning("Cannot initialize JavaFXSmoothImageRendering.", t);
}
}
}
2 changes: 2 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public void start(Stage primaryStage) {
UpdateChecker.init();

primaryStage.show();

net.burningtnt.hmclprs.PRCollection.onApplicationLaunched();
});
} catch (Throwable e) {
CRASH_REPORTER.uncaughtException(Thread.currentThread(), e);
Expand Down
Binary file modified HMCL/src/main/resources/assets/img/teacon-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8659353

Please sign in to comment.