[editor only] Tileset auto splitter

This commit is contained in:
fgnm
2021-05-15 19:07:36 +02:00
parent 6bb47ce2bf
commit 39f53d5587
7 changed files with 180 additions and 14 deletions
@@ -34,12 +34,14 @@ import games.rednblack.editor.plugin.tiled.save.DataToSave;
import games.rednblack.editor.plugin.tiled.save.SaveDataManager;
import games.rednblack.editor.plugin.tiled.tools.DeleteTileTool;
import games.rednblack.editor.plugin.tiled.tools.DrawTileTool;
import games.rednblack.editor.plugin.tiled.view.dialog.ImportTileSetDialogMediator;
import games.rednblack.editor.renderer.components.MainItemComponent;
import games.rednblack.editor.renderer.components.TextureRegionComponent;
import games.rednblack.editor.renderer.components.TransformComponent;
import games.rednblack.editor.renderer.components.ZIndexComponent;
import games.rednblack.editor.renderer.utils.ComponentRetriever;
import games.rednblack.editor.renderer.utils.CustomVariables;
import games.rednblack.h2d.common.MenuAPI;
import games.rednblack.h2d.common.plugins.H2DPluginAdapter;
import net.mountainblade.modular.annotations.Implementation;
@@ -57,6 +59,7 @@ public class TiledPlugin extends H2DPluginAdapter {
public static final String TILE_SELECTED = CLASS_NAME + ".TILE_SELECTED";
public static final String OPEN_DROP_DOWN = CLASS_NAME + ".OPEN_DROP_DOWN";
public static final String GRID_CHANGED = CLASS_NAME + ".GRID_CHANGED";
public static final String IMPORT_TILESET_PANEL_OPEN = CLASS_NAME + ".IMPORT_TILESET_PANEL_OPEN";
public static final String ACTION_DELETE_TILE = CLASS_NAME + ".ACTION_DELETE_TILE";
public static final String ACTION_SET_OFFSET = CLASS_NAME + ".ACTION_SET_OFFSET";
public static final String ACTION_OPEN_OFFSET_PANEL = CLASS_NAME + ".ACTION_OPEN_OFFSET_PANEL";
@@ -91,6 +94,7 @@ public class TiledPlugin extends H2DPluginAdapter {
@Override
public void initPlugin() {
facade.registerMediator(new TiledPanelMediator(this));
facade.registerMediator(new ImportTileSetDialogMediator(pluginAPI, facade));
pluginRM = new ResourcesManager(this);
offsetPanel = new OffsetPanel(this);
@@ -117,6 +121,8 @@ public class TiledPlugin extends H2DPluginAdapter {
pluginAPI.addTool(DeleteTileTool.NAME, tileDeleteButtonStyle, false, deleteTileTool);
pluginAPI.setDropDownItemName(ACTION_SET_GRID_SIZE_FROM_ITEM, "Set tile grid size");
pluginAPI.addMenuItem(MenuAPI.RESOURCE_MENU, "Import Tile Set...", IMPORT_TILESET_PANEL_OPEN);
}
@Override
@@ -0,0 +1,83 @@
package games.rednblack.editor.plugin.tiled.view.dialog;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.kotcrab.vis.ui.util.Validators;
import com.kotcrab.vis.ui.widget.VisLabel;
import com.kotcrab.vis.ui.widget.VisTable;
import com.kotcrab.vis.ui.widget.VisTextButton;
import com.kotcrab.vis.ui.widget.VisValidatableTextField;
import com.kotcrab.vis.ui.widget.file.FileChooser;
import games.rednblack.h2d.common.H2DDialog;
import games.rednblack.h2d.common.view.ui.StandardWidgetsFactory;
import games.rednblack.h2d.common.view.ui.widget.InputFileWidget;
import org.puremvc.java.interfaces.IFacade;
public class ImportTileSetDialog extends H2DDialog {
private static final String prefix = "games.rednblack.editor.plugin.tiled.view.dialog.ImportTileSetDialog";
public static final String IMPORT_TILESET = prefix + ".IMPORT_TILESET";
private final VisValidatableTextField width, height;
private final InputFileWidget imagePathField;
private final VisTextButton importButton;
private final IFacade facade;
public ImportTileSetDialog(IFacade facade) {
super("Import TileSet");
this.facade = facade;
setModal(true);
addCloseButton();
VisTable fileTable = new VisTable();
fileTable.pad(6);
//
fileTable.add(new VisLabel("Tile Set:")).right().padRight(5);
imagePathField = new InputFileWidget(FileChooser.Mode.OPEN, FileChooser.SelectionMode.FILES, false);
imagePathField.setTextFieldWidth(156);
fileTable.add(imagePathField);
getContentTable().add(fileTable);
//
getContentTable().row().padTop(10);
//
Validators.IntegerValidator validator = new Validators.IntegerValidator();
width = StandardWidgetsFactory.createValidableTextField(validator);
height = StandardWidgetsFactory.createValidableTextField(validator);
VisTable sizeTable = new VisTable();
sizeTable.add("Width:").padRight(3);
sizeTable.add(width).width(60);
sizeTable.add("px");
sizeTable.row().padTop(5);
sizeTable.add("Height:").padRight(3);
sizeTable.add(height).width(60);
sizeTable.add("px");
getContentTable().add(sizeTable);
importButton = StandardWidgetsFactory.createTextButton("Import");
getButtonsTable().add(importButton);
pack();
setListeners();
}
public int getTileWidth() {
return Integer.parseInt(width.getText());
}
public int getTileHeight() {
return Integer.parseInt(height.getText());
}
private void setListeners() {
importButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
if (width.isInputValid() && height.isInputValid() && imagePathField.getValue() != null) {
facade.sendNotification(IMPORT_TILESET, imagePathField.getValue());
}
}
});
}
}
@@ -0,0 +1,81 @@
package games.rednblack.editor.plugin.tiled.view.dialog;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import games.rednblack.editor.plugin.tiled.TiledPlugin;
import games.rednblack.h2d.common.MsgAPI;
import games.rednblack.h2d.common.plugins.PluginAPI;
import org.puremvc.java.interfaces.IFacade;
import org.puremvc.java.interfaces.INotification;
import org.puremvc.java.patterns.mediator.Mediator;
import java.io.File;
public class ImportTileSetDialogMediator extends Mediator<ImportTileSetDialog> {
private static final String TAG = ImportTileSetDialogMediator.class.getCanonicalName();
public static final String NAME = TAG;
private final PluginAPI pluginAPI;
public ImportTileSetDialogMediator(PluginAPI pluginAPI, IFacade facade) {
super(NAME, new ImportTileSetDialog(facade));
this.pluginAPI = pluginAPI;
}
@Override
public String[] listNotificationInterests() {
return new String[] {
TiledPlugin.IMPORT_TILESET_PANEL_OPEN,
ImportTileSetDialog.IMPORT_TILESET
};
}
@Override
public void handleNotification(INotification notification) {
switch (notification.getName()) {
case TiledPlugin.IMPORT_TILESET_PANEL_OPEN:
viewComponent.show(pluginAPI.getUIStage());
break;
case ImportTileSetDialog.IMPORT_TILESET:
FileHandle tileset = notification.getBody();
importTileset(tileset);
break;
}
}
private void importTileset(FileHandle tileset) {
byte[] image = tileset.readBytes();
Pixmap pixmap = new Pixmap(image, 0, image.length);
String name = tileset.nameWithoutExtension();
int tileW = viewComponent.getTileWidth();
int tileH = viewComponent.getTileHeight();
int i = 0;
for (int x = 0; x < pixmap.getWidth(); x += tileW) {
for (int y = 0; y < pixmap.getHeight(); y += tileH) {
int w = x + tileW <= pixmap.getWidth() ? tileW : pixmap.getWidth() - x;
int h = y + tileH <= pixmap.getHeight() ? tileH : pixmap.getHeight() - y;
Pixmap tilePixmap = new Pixmap(w, h, Pixmap.Format.RGBA8888);
tilePixmap.drawPixmap(pixmap, 0, 0, x, y, w, h);
String imagesPath = getCurrentRawImagesPath() + File.separator + name + i + ".png";
FileHandle path = new FileHandle(imagesPath);
PixmapIO.writePNG(path, tilePixmap);
tilePixmap.dispose();
i++;
}
}
pixmap.dispose();
viewComponent.close();
facade.sendNotification(MsgAPI.ACTION_REPACK);
}
public String getCurrentRawImagesPath() {
return pluginAPI.getProjectPath() + File.separator + "assets" + File.separator + "orig" + File.separator + "images";
}
}