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;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.StringProperty;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
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<Rectangle2D> viewportProperty() {
return null;
public ObjectProperty<Image> imageProperty() {
return image;
}
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() {
return onAction;

View File

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

View File

@ -58,8 +58,9 @@ public final class LeftPaneController {
public LeftPaneController(AdvancedListBox leftPane) {
this.leftPane = leftPane;
AdvancedListItem2 accountListItem = new AccountAdvancedListItem();
AccountAdvancedListItem accountListItem = new AccountAdvancedListItem();
accountListItem.setOnAction(e -> Controllers.navigate(Controllers.getAccountListPage()));
accountListItem.accountProperty().bind(Accounts.selectedAccountProperty());
AdvancedListItem2 gameListItem = new GameAdvancedListItem();
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.FXUtils;
import static org.jackhuang.hmcl.ui.FXUtils.onInvalidating;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public class AccountAdvancedListItem extends AdvancedListItem2 {
private final ObjectProperty<Image> image = new SimpleObjectProperty<>();
private final ObjectProperty<Rectangle2D> viewport = new SimpleObjectProperty<>(AccountHelper.getViewport(4));
private final StringProperty title = new SimpleStringProperty();
private final StringProperty subtitle = new SimpleStringProperty();
private ObjectProperty<Account> account = new SimpleObjectProperty<Account>() {
public AccountAdvancedListItem() {
FXUtils.onChangeAndOperate(Accounts.selectedAccountProperty(), account -> {
@Override
protected void invalidated() {
Account account = get();
if (account == null) {
title.set(i18n("account.missing"));
subtitle.set(i18n("account.missing.add"));
image.set(new Image("/assets/img/craft_table.png"));
titleProperty().set(i18n("account.missing"));
subtitleProperty().set(i18n("account.missing.add"));
imageProperty().set(new Image("/assets/img/craft_table.png"));
} else {
title.set(account.getCharacter());
subtitle.set(accountSubtitle(account));
titleProperty().set(account.getCharacter());
subtitleProperty().set(accountSubtitle(account));
this.image.set(AccountHelper.getDefaultSkin(account.getUUID(), 4));
imageProperty().set(AccountHelper.getDefaultSkin(account.getUUID(), 4));
if (account instanceof YggdrasilAccount) {
AccountHelper.loadSkinAsync((YggdrasilAccount) account).subscribe(Schedulers.javafx(), () -> {
Image image = AccountHelper.getSkin((YggdrasilAccount) account, 4);
this.image.set(image);
imageProperty().set(image);
});
}
}
});
}
};
public AccountAdvancedListItem() {
viewportProperty().set(AccountHelper.getViewport(4));
}
@Override
protected void layoutChildren() {
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;
public ObjectProperty<Account> accountProperty() {
return account;
}
private static String accountSubtitle(Account account) {

View File

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