This commit is contained in:
huangyuhui 2018-08-31 22:21:15 +08:00
parent 5efc8d6fa9
commit b3d84c4f99
5 changed files with 55 additions and 79 deletions

View File

@ -17,27 +17,36 @@
*/ */
package org.jackhuang.hmcl.ui; package org.jackhuang.hmcl.ui;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.*;
import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D; import javafx.geometry.Rectangle2D;
import javafx.scene.control.Control; import javafx.scene.control.Control;
import javafx.scene.control.Skin; import javafx.scene.control.Skin;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import org.jackhuang.hmcl.game.AccountHelper;
public abstract class AdvancedListItem2 extends Control { public class AdvancedListItem2 extends Control {
private final ObjectProperty<Image> image = new SimpleObjectProperty<>();
private final ObjectProperty<Rectangle2D> viewport = new SimpleObjectProperty<>();
private final StringProperty title = new SimpleStringProperty();
private final StringProperty subtitle = new SimpleStringProperty();
public abstract ObjectProperty<Image> imageProperty(); public ObjectProperty<Image> imageProperty() {
return image;
public ObjectProperty<Rectangle2D> viewportProperty() {
return null;
} }
public abstract StringProperty titleProperty(); public ObjectProperty<Rectangle2D> viewportProperty() {
return viewport;
}
public abstract StringProperty subtitleProperty(); public StringProperty titleProperty() {
return title;
}
public StringProperty subtitleProperty() {
return subtitle;
}
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() {
return onAction; return onAction;

View File

@ -54,8 +54,7 @@ public class AdvancedListItemSkin extends SkinBase<AdvancedListItem2> {
FXUtils.limitSize(imageView, 32, 32); FXUtils.limitSize(imageView, 32, 32);
imageView.setPreserveRatio(true); imageView.setPreserveRatio(true);
imageView.imageProperty().bind(skinnable.imageProperty()); imageView.imageProperty().bind(skinnable.imageProperty());
Optional.ofNullable(skinnable.viewportProperty()) imageView.viewportProperty().bind(skinnable.viewportProperty());
.ifPresent(imageView.viewportProperty()::bind);
imageViewContainer.getChildren().setAll(imageView); imageViewContainer.getChildren().setAll(imageView);
VBox vbox = new VBox(); VBox vbox = new VBox();
@ -69,14 +68,17 @@ public class AdvancedListItemSkin extends SkinBase<AdvancedListItem2> {
title.setTextAlignment(TextAlignment.JUSTIFY); title.setTextAlignment(TextAlignment.JUSTIFY);
vbox.getChildren().add(title); vbox.getChildren().add(title);
if (skinnable.subtitleProperty() != null) { Label subtitle = new Label();
Label subtitle = new Label(); subtitle.textProperty().bind(skinnable.subtitleProperty());
subtitle.textProperty().bind(skinnable.subtitleProperty()); subtitle.setMaxWidth(90);
subtitle.setMaxWidth(90); subtitle.setStyle("-fx-font-size: 10;");
subtitle.setStyle("-fx-font-size: 10;"); subtitle.setTextAlignment(TextAlignment.JUSTIFY);
subtitle.setTextAlignment(TextAlignment.JUSTIFY); vbox.getChildren().add(subtitle);
vbox.getChildren().add(subtitle);
} FXUtils.onChangeAndOperate(skinnable.subtitleProperty(), subtitleString -> {
if (subtitleString == null) vbox.getChildren().setAll(title);
else vbox.getChildren().setAll(title, subtitle);
});
left.getChildren().setAll(imageViewContainer, vbox); left.getChildren().setAll(imageViewContainer, vbox);
root.setLeft(left); root.setLeft(left);

View File

@ -58,8 +58,9 @@ public final class LeftPaneController {
public LeftPaneController(AdvancedListBox leftPane) { public LeftPaneController(AdvancedListBox leftPane) {
this.leftPane = leftPane; this.leftPane = leftPane;
AdvancedListItem2 accountListItem = new AccountAdvancedListItem(); AccountAdvancedListItem accountListItem = new AccountAdvancedListItem();
accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage())); accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage()));
accountListItem.accountProperty().bind(Accounts.selectedAccountProperty());
AdvancedListItem2 gameListItem = new GameAdvancedListItem(); AdvancedListItem2 gameListItem = new GameAdvancedListItem();
gameListItem.setOnAction(e -> Controllers.navigate(Controllers.getGameListPage())); gameListItem.setOnAction(e -> Controllers.navigate(Controllers.getGameListPage()));

View File

@ -32,60 +32,41 @@ import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.ui.AdvancedListItem2; import org.jackhuang.hmcl.ui.AdvancedListItem2;
import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.FXUtils;
import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public class AccountAdvancedListItem extends AdvancedListItem2 { public class AccountAdvancedListItem extends AdvancedListItem2 {
private final ObjectProperty<Image> image = new SimpleObjectProperty<>(); private ObjectProperty<Account> account = new SimpleObjectProperty<Account>() {
private final ObjectProperty<Rectangle2D> viewport = new SimpleObjectProperty<>(AccountHelper.getViewport(4));
private final StringProperty title = new SimpleStringProperty();
private final StringProperty subtitle = new SimpleStringProperty();
public AccountAdvancedListItem() { @Override
protected void invalidated() {
FXUtils.onChangeAndOperate(Accounts.selectedAccountProperty(), account -> { Account account = get();
if (account == null) { if (account == null) {
title.set(i18n("account.missing")); titleProperty().set(i18n("account.missing"));
subtitle.set(i18n("account.missing.add")); subtitleProperty().set(i18n("account.missing.add"));
image.set(new Image("/assets/img/craft_table.png")); imageProperty().set(new Image("/assets/img/craft_table.png"));
} else { } else {
title.set(account.getCharacter()); titleProperty().set(account.getCharacter());
subtitle.set(accountSubtitle(account)); subtitleProperty().set(accountSubtitle(account));
this.image.set(AccountHelper.getDefaultSkin(account.getUUID(), 4)); imageProperty().set(AccountHelper.getDefaultSkin(account.getUUID(), 4));
if (account instanceof YggdrasilAccount) { if (account instanceof YggdrasilAccount) {
AccountHelper.loadSkinAsync((YggdrasilAccount) account).subscribe(Schedulers.javafx(), () -> { AccountHelper.loadSkinAsync((YggdrasilAccount) account).subscribe(Schedulers.javafx(), () -> {
Image image = AccountHelper.getSkin((YggdrasilAccount) account, 4); Image image = AccountHelper.getSkin((YggdrasilAccount) account, 4);
this.image.set(image); imageProperty().set(image);
}); });
} }
} }
}); }
};
public AccountAdvancedListItem() {
viewportProperty().set(AccountHelper.getViewport(4));
} }
@Override public ObjectProperty<Account> accountProperty() {
protected void layoutChildren() { return account;
super.layoutChildren();
}
@Override
public ObjectProperty<Image> imageProperty() {
return image;
}
@Override
public ObjectProperty<Rectangle2D> viewportProperty() {
return viewport;
}
@Override
public StringProperty titleProperty() {
return title;
}
@Override
public StringProperty subtitleProperty() {
return subtitle;
} }
private static String accountSubtitle(Account account) { private static String accountSubtitle(Account account) {

View File

@ -35,8 +35,6 @@ import org.jackhuang.hmcl.ui.WeakListenerHelper;
import java.io.File; import java.io.File;
public class GameAdvancedListItem extends AdvancedListItem2 { public class GameAdvancedListItem extends AdvancedListItem2 {
private final ObjectProperty<Image> image = new SimpleObjectProperty<>();
private final StringProperty title = new SimpleStringProperty();
private final WeakListenerHelper helper = new WeakListenerHelper(); private final WeakListenerHelper helper = new WeakListenerHelper();
private Profile profile; private Profile profile;
@ -69,25 +67,10 @@ public class GameAdvancedListItem extends AdvancedListItem2 {
String version = profile.getSelectedVersion(); String version = profile.getSelectedVersion();
File iconFile = profile.getRepository().getVersionIcon(version); File iconFile = profile.getRepository().getVersionIcon(version);
if (iconFile.exists()) if (iconFile.exists())
image.set(new Image("file:" + iconFile.getAbsolutePath())); imageProperty().set(new Image("file:" + iconFile.getAbsolutePath()));
else else
image.set(new Image("/assets/img/grass.png")); imageProperty().set(new Image("/assets/img/grass.png"));
title.set(version); titleProperty().set(version);
}
@Override
public ObjectProperty<Image> imageProperty() {
return image;
}
@Override
public StringProperty titleProperty() {
return title;
}
@Override
public StringProperty subtitleProperty() {
return null;
} }
} }