Skip to content

Commit 21b8063

Browse files
committed
реализация превью при выборе ресурса
1 parent 5abcd5f commit 21b8063

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

resources/ui/css/custom_ids.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@
135135
-fx-max-width: -fx-min-width;
136136
}
137137

138+
#AssetEditorDialogPreviewContainer {
139+
-fx-border-color: -fx-color-0;
140+
-fx-border-width: 1;
141+
-fx-min-height: 256;
142+
-fx-pref-height: -fx-min-height;
143+
-fx-max-height: -fx-min-height;
144+
-fx-min-width: 256;
145+
-fx-pref-width: -fx-min-width;
146+
-fx-max-width: -fx-min-width;
147+
}
148+
138149
#MaterialEditorToolbarLabel {
139150
-fx-min-height: 28;
140151
-fx-pref-height: -fx-min-height;

src/com/ss/editor/manager/JavaFXImageManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,28 @@ public class JavaFXImageManager {
3030
private static final Logger LOGGER = LoggerManager.getLogger(JavaFXImageManager.class);
3131

3232
private static final Array<String> FX_FORMATS = ArrayFactory.newArray(String.class);
33+
private static final Array<String> IMAGE_FORMATS = ArrayFactory.newArray(String.class);
3334

3435
static {
3536
FX_FORMATS.add("png");
3637
FX_FORMATS.add("jpg");
3738
FX_FORMATS.add("gif");
39+
40+
IMAGE_FORMATS.addAll(FX_FORMATS);
41+
IMAGE_FORMATS.add("tga");
42+
IMAGE_FORMATS.add("bmp");
43+
}
44+
45+
/**
46+
* Определение, является ли файл картинкой.
47+
*/
48+
public static boolean isImage(final Path file) {
49+
50+
final Path fileName = file.getFileName();
51+
52+
final String extension = FileUtils.getExtension(fileName.toString());
53+
54+
return IMAGE_FORMATS.contains(extension);
3855
}
3956

4057
private static JavaFXImageManager instance;

src/com/ss/editor/ui/css/CSSIds.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public interface CSSIds {
3333
public static final String ASSET_EDITOR_DIALOG_BUTTON_CONTAINER = "AssetEditorDialogButtonContainer";
3434
public static final String ASSET_EDITOR_DIALOG_BUTTON_OK = "AssetEditorDialogButtonOk";
3535
public static final String ASSET_EDITOR_DIALOG_BUTTON_CANCEL = "AssetEditorDialogButtonCancel";
36+
public static final String ASSET_EDITOR_DIALOG_PREVIEW_CONTAINER = "AssetEditorDialogPreviewContainer";
3637

3738
public static final String MATERIAL_EDITOR_TOOLBAR_LABEL = "MaterialEditorToolbarLabel";
3839
public static final String MATERIAL_EDITOR_TOOLBAR_BOX = "MaterialEditorToolbarBox";

src/com/ss/editor/ui/dialog/asset/AssetEditorDialog.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import com.ss.editor.Messages;
44
import com.ss.editor.config.EditorConfig;
5+
import com.ss.editor.manager.JavaFXImageManager;
56
import com.ss.editor.ui.component.asset.tree.ResourceTree;
67
import com.ss.editor.ui.component.asset.tree.resource.ResourceElement;
78
import com.ss.editor.ui.dialog.EditorDialog;
89

910
import java.awt.*;
11+
import java.nio.file.Files;
1012
import java.nio.file.Path;
1113
import java.util.function.Consumer;
1214

1315
import javafx.geometry.Insets;
16+
import javafx.geometry.Pos;
1417
import javafx.scene.control.Button;
1518
import javafx.scene.control.MultipleSelectionModel;
1619
import javafx.scene.control.TreeItem;
20+
import javafx.scene.image.Image;
21+
import javafx.scene.image.ImageView;
1722
import javafx.scene.layout.HBox;
1823
import javafx.scene.layout.VBox;
1924
import rlib.ui.util.FXUtils;
@@ -22,6 +27,7 @@
2227
import static com.ss.editor.ui.css.CSSIds.ASSET_EDITOR_DIALOG_BUTTON_CANCEL;
2328
import static com.ss.editor.ui.css.CSSIds.ASSET_EDITOR_DIALOG_BUTTON_CONTAINER;
2429
import static com.ss.editor.ui.css.CSSIds.ASSET_EDITOR_DIALOG_BUTTON_OK;
30+
import static com.ss.editor.ui.css.CSSIds.ASSET_EDITOR_DIALOG_PREVIEW_CONTAINER;
2531

2632
/**
2733
* Реализация диалога для выбора объекта из Asset.
@@ -33,6 +39,9 @@ public class AssetEditorDialog extends EditorDialog {
3339
private static final Insets OK_BUTTON_OFFSET = new Insets(0, 4, 0, 0);
3440
private static final Insets CANCEL_BUTTON_OFFSET = new Insets(0, 15, 0, 0);
3541

42+
private static final Insets PREVIEW_OFFSET = new Insets(0, 15, 0, 0);
43+
public static final JavaFXImageManager JAVA_FX_IMAGE_MANAGER = JavaFXImageManager.getInstance();
44+
3645
/**
3746
* Функция для использования выбранного ресурса.
3847
*/
@@ -43,13 +52,21 @@ public class AssetEditorDialog extends EditorDialog {
4352
*/
4453
private ResourceTree resourceTree;
4554

55+
/**
56+
* Превью картинок.
57+
*/
58+
private ImageView imageView;
59+
4660
public AssetEditorDialog(final Consumer<Path> consumer) {
4761
this.consumer = consumer;
4862
}
4963

5064
@Override
5165
protected void createContent(final VBox root) {
5266

67+
final HBox container = new HBox();
68+
container.setAlignment(Pos.CENTER_LEFT);
69+
5370
final EditorConfig editorConfig = EditorConfig.getInstance();
5471
final Consumer<ResourceElement> openFunction = element -> {
5572

@@ -62,8 +79,55 @@ protected void createContent(final VBox root) {
6279
resourceTree = new ResourceTree(openFunction, true);
6380
resourceTree.fill(editorConfig.getCurrentAsset());
6481
resourceTree.prefHeightProperty().bind(root.heightProperty());
82+
resourceTree.prefWidthProperty().bind(root.widthProperty());
83+
resourceTree.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> processSelected(newValue));
84+
85+
86+
final VBox previewContainer = new VBox();
87+
previewContainer.setId(ASSET_EDITOR_DIALOG_PREVIEW_CONTAINER);
88+
89+
imageView = new ImageView();
90+
imageView.fitHeightProperty().bind(previewContainer.heightProperty().subtract(2));
91+
imageView.fitWidthProperty().bind(previewContainer.widthProperty().subtract(2));
92+
93+
FXUtils.addToPane(resourceTree, container);
94+
FXUtils.addToPane(imageView, previewContainer);
95+
FXUtils.addToPane(previewContainer, container);
96+
FXUtils.addToPane(container, root);
97+
98+
HBox.setMargin(previewContainer, PREVIEW_OFFSET);
99+
}
100+
101+
/**
102+
* @return превью картинок.
103+
*/
104+
private ImageView getImageView() {
105+
return imageView;
106+
}
107+
108+
/**
109+
* Обработка выбора в дереве элемента.
110+
*/
111+
private void processSelected(final TreeItem<ResourceElement> newValue) {
112+
113+
final ImageView imageView = getImageView();
114+
115+
if(newValue == null) {
116+
imageView.setImage(null);
117+
return;
118+
}
119+
120+
final ResourceElement element = newValue.getValue();
121+
final Path file = element.getFile();
122+
123+
if(Files.isDirectory(file) || !JavaFXImageManager.isImage(file)) {
124+
imageView.setImage(null);
125+
return;
126+
}
127+
128+
final Image preview = JAVA_FX_IMAGE_MANAGER.getTexturePreview(file, (int) imageView.getFitWidth(), (int) imageView.getFitHeight());
65129

66-
FXUtils.addToPane(resourceTree, root);
130+
imageView.setImage(preview);
67131
}
68132

69133
@Override

0 commit comments

Comments
 (0)