Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview image selection window, simpler keybind #581

Open
wants to merge 3 commits into
base: pre-rewrite/fabric/1.19.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 39 additions & 34 deletions src/main/java/fi/dy/masa/litematica/gui/GuiSchematicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import fi.dy.masa.malilib.interfaces.IStringConsumerFeedback;
import fi.dy.masa.malilib.util.InfoUtils;
import fi.dy.masa.malilib.util.StringUtils;
import org.lwjgl.util.tinyfd.TinyFileDialogs;

public class GuiSchematicManager extends GuiSchematicBrowserBase implements ISelectionListener<DirectoryEntry>
{
Expand Down Expand Up @@ -261,46 +262,50 @@ else if (this.type == Type.DELETE_SCHEMATIC)
}
else if (this.type == Type.SET_PREVIEW)
{
if (GuiBase.isShiftDown() && GuiBase.isCtrlDown() && GuiBase.isAltDown() && fileType == FileType.LITEMATICA_SCHEMATIC)
if (GuiBase.isCtrlDown() && fileType == FileType.LITEMATICA_SCHEMATIC)
{
File imageFile = new File(entry.getDirectory(), "thumb.png");
if (imageFile.isFile() && imageFile.canRead())
{
LitematicaSchematic schematic = LitematicaSchematic.createFromFile(entry.getDirectory(), entry.getName());
String val = TinyFileDialogs.tinyfd_openFileDialog(
"Select a preview image",
entry.getDirectory().getAbsolutePath(),
null,
null,
false
);

if (val == null) {
InfoUtils.showGuiAndInGameMessage(MessageType.ERROR, "Image not selected");
return;
}
File imageFile = new File(val);
LitematicaSchematic schematic = LitematicaSchematic.createFromFile(entry.getDirectory(), entry.getName());

if (schematic != null)
if (schematic != null)
{
try
{
try
InputStream inputStream = Files.newInputStream(imageFile.toPath());
NativeImage image = NativeImage.read(inputStream);
int x = image.getWidth() >= image.getHeight() ? (image.getWidth() - image.getHeight()) / 2 : 0;
int y = image.getHeight() >= image.getWidth() ? (image.getHeight() - image.getWidth()) / 2 : 0;
int longerSide = Math.min(image.getWidth(), image.getHeight());
//System.out.printf("w: %d, h: %d, x: %d, y: %d\n", screenshot.getWidth(), screenshot.getHeight(), x, y);
int previewDimensions = 140;
NativeImage scaled = new NativeImage(previewDimensions, previewDimensions, false);
image.resizeSubRectTo(x, y, longerSide, longerSide, scaled);
@SuppressWarnings("deprecation")
int[] pixels = scaled.makePixelArray();

schematic.getMetadata().setPreviewImagePixelData(pixels);
schematic.getMetadata().setTimeModifiedToNow();

if (schematic.writeToFile(entry.getDirectory(), entry.getName(), true))
{
InputStream inputStream = Files.newInputStream(imageFile.toPath());
NativeImage image = NativeImage.read(inputStream);
int x = image.getWidth() >= image.getHeight() ? (image.getWidth() - image.getHeight()) / 2 : 0;
int y = image.getHeight() >= image.getWidth() ? (image.getHeight() - image.getWidth()) / 2 : 0;
int longerSide = Math.min(image.getWidth(), image.getHeight());
//System.out.printf("w: %d, h: %d, x: %d, y: %d\n", screenshot.getWidth(), screenshot.getHeight(), x, y);
int previewDimensions = 140;
NativeImage scaled = new NativeImage(previewDimensions, previewDimensions, false);
image.resizeSubRectTo(x, y, longerSide, longerSide, scaled);
@SuppressWarnings("deprecation")
int[] pixels = scaled.makePixelArray();

schematic.getMetadata().setPreviewImagePixelData(pixels);
schematic.getMetadata().setTimeModifiedToNow();

if (schematic.writeToFile(entry.getDirectory(), entry.getName(), true))
{
InfoUtils.showGuiAndInGameMessage(MessageType.SUCCESS, "Custom preview image set");
}

return;
InfoUtils.showGuiAndInGameMessage(MessageType.SUCCESS, "Custom preview image set");
}
catch (Exception ignore) {}

return;
}
}
else
{
InfoUtils.showGuiAndInGameMessage(MessageType.ERROR, "Image 'thumb.png' not found");
return;
catch (Exception ignore) {}
}
InfoUtils.showGuiAndInGameMessage(MessageType.ERROR, "Failed to set custom preview image");
return;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/litematica/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"litematica.hotkeys.category.generic_hotkeys": "Generic hotkeys",

"litematica.info.schematic_load.schematic_loaded": "Schematic '%s' successfully loaded to memory",
"litematica.info.schematic_manager.preview.right_click_to_cancel": "- Right click the button to cancel a pending preview operation\n- Ctrl + Alt + Shift + click to set the thumbnail from an existing PNG image\n called 'thumb.png' from the same directory where the schematic file is.",
"litematica.info.schematic_manager.preview.right_click_to_cancel": "- Right click the button to cancel a pending preview operation\n- Ctrl + click to set the thumbnail from an existing PNG image.",
"litematica.info.schematic_manager.preview.set_preview_by_taking_a_screenshot": "Use the normal screenshot key to set the preview.\nOr right click the Set preview button to cancel setting the preview.",
"litematica.info.schematic_manager.preview.success": "Preview image successfully set",

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/litematica/lang/ru_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"litematica.hotkeys.category.generic_hotkeys": "Основные клавиши",

"litematica.info.schematic_load.schematic_loaded": "Схема \"%s\" успешно загружена в память",
"litematica.info.schematic_manager.preview.right_click_to_cancel": "- Нажмите ПКМ по кнопке, чтобы прервать операцию создания миниатюры\n- Удерживайте Ctrl+Alt+Shift при нажатии, чтобы загрузить картинку из существующего\n PNG-файла с именем \"thumb.png\", лежащего в той же директории, что и файл схемы.",
"litematica.info.schematic_manager.preview.right_click_to_cancel": "- Нажмите ПКМ по кнопке, чтобы прервать операцию создания миниатюры\n- Удерживайте Ctrl при нажатии, чтобы загрузить картинку из существующего\n PNG-файла.",
"litematica.info.schematic_manager.preview.set_preview_by_taking_a_screenshot": "Используйте обычную клавишу создания скриншота, чтобы установить миниатюру. Нажмите ПКМ по этой кнопке, чтобы прервать операцию.",
"litematica.info.schematic_manager.preview.success": "Миниатюра успешно установлена",

Expand Down