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

1. adds capability to export/import library items ... #421

Open
wants to merge 1 commit into
base: master
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allprojects {
version = '0.1.3'
ext {
appName = 'Overlap2D'
gdxVersion = '1.7.1'
gdxVersion = '1.9.3'
box2DLightsVersion = '1.4'
visuiVersion = '0.9.2'

Expand Down
3 changes: 3 additions & 0 deletions overlap2d-common-api/src/com/commons/MsgAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public class MsgAPI {
public static final String ACTION_ADD_TO_LIBRARY = SANDBOX_PREFIX + "ACTION_ADD_TO_LIBRARY";
public static final String ACTION_EDIT_PHYSICS = SANDBOX_PREFIX + "ACTION_EDIT_PHYSICS";
public static final String ACTION_SET_GRID_SIZE_FROM_ITEM = SANDBOX_PREFIX + "ACTION_SET_GRID_SIZE_FROM_ITEM";
public static final String ACTION_EXPORT_AS_LIBRARY_ITEM = SANDBOX_PREFIX + "ACTION_EXPORT_AS_LIBRARY_ITEM";
public static final String ACTION_EXPORT_AS_LIBRARY_ITEM_FROM_RES_PANEL = SANDBOX_PREFIX + "ACTION_EXPORT_AS_LIBRARY_ITEM_FROM_RES_PANEL";

public static final String ACTION_ITEMS_MOVE_TO = SANDBOX_PREFIX + "ACTION_ITEMS_MOVE_TO";
public static final String ACTION_ITEM_AND_CHILDREN_TO = GLOBAL_PREFIX + "ACTION_ITEM_AND_CHILDREN_TO";
public static final String ACTION_ITEM_TRANSFORM_TO = SANDBOX_PREFIX + "ACTION_ITEM_TRANSFORM_TO";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void execute(Notification notification) {
facade.registerCommand(MsgAPI.ACTION_CREATE_ITEM, CreateItemCommand.class);
facade.registerCommand(MsgAPI.ACTION_CAMERA_CHANGE_COMPOSITE, CompositeCameraChangeCommand.class);
facade.registerCommand(MsgAPI.ACTION_CREATE_PRIMITIVE, CreatePrimitiveCommand.class);
facade.registerCommand(MsgAPI.ACTION_EXPORT_AS_LIBRARY_ITEM, ExportLibraryItemCommand.class);

facade.registerCommand(MsgAPI.ACTION_DELETE_LAYER, DeleteLayerCommand.class);
facade.registerCommand(MsgAPI.ACTION_NEW_LAYER, NewLayerCommand.class);
Expand Down Expand Up @@ -84,6 +85,8 @@ public void execute(Notification notification) {
facade.registerCommand(MsgAPI.ACTION_PLUGIN_PROXY_COMMAND, PluginItemCommand.class);

// Resources
facade.registerCommand(MsgAPI.ACTION_EXPORT_AS_LIBRARY_ITEM_FROM_RES_PANEL, ExportLibraryItemFromResPanelCommand.class);

facade.registerCommand(MsgAPI.ACTION_DELETE_IMAGE_RESOURCE, DeleteImageResource.class);
facade.registerCommand(MsgAPI.ACTION_DELETE_LIBRARY_ITEM, DeleteLibraryItem.class);
facade.registerCommand(MsgAPI.ACTION_DELETE_PARTICLE_EFFECT, DeleteParticleEffect.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.uwsoft.editor.controller.commands;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import com.puremvc.patterns.observer.Notification;
import com.uwsoft.editor.renderer.components.*;
import com.uwsoft.editor.renderer.data.*;
import com.uwsoft.editor.renderer.factory.EntityFactory;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
* Created by Sasun Poghosyan on 11/28/2016.
*/
public class ExportLibraryItemCommand extends NonRevertibleCommand {
private final Json json = new Json(JsonWriter.OutputType.json);

public ExportLibraryItemCommand() {
super();
}

@Override
public void execute(Notification notification) {
super.execute(notification);
}

@Override
public void doAction() {
getJsonStringFromEntities(sandbox.getSelector().getSelectedItems());
}

private void getJsonStringFromEntities(Set<Entity> entities) {
CompositeVO holderComposite = new CompositeVO();
for (Entity entity : entities) {
int entityType = ComponentRetriever.get(entity, MainItemComponent.class).entityType;
if (entityType == EntityFactory.COMPOSITE_TYPE) {
CompositeItemVO vo = new CompositeItemVO();
vo.loadFromEntity(entity);
if (vo.itemName.length() == 0) {
System.out.println("item is not a library item");
return;
}
holderComposite.sComposites.add(vo);
vo.cleanIds();
} else {
System.out.println("item is not a composite");
return;
}
}

for (int i = 0; i < holderComposite.sComposites.size(); i++) {
CompositeItemVO compositeItemVO = holderComposite.sComposites.get(i);
String jsonString = json.toJson(compositeItemVO);
String currentProjectPath = projectManager.getCurrentProjectPath();

try {
FileUtils.writeStringToFile(new File(currentProjectPath + "\\export\\libraryItems\\" + compositeItemVO.itemName + ".json"), jsonString, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void callDoAction() {
super.callDoAction();
}

@Override
public void cancel() {
super.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.uwsoft.editor.controller.commands;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import com.puremvc.patterns.observer.Notification;
import com.uwsoft.editor.Overlap2DFacade;
import com.uwsoft.editor.proxy.ProjectManager;
import com.uwsoft.editor.renderer.components.MainItemComponent;
import com.uwsoft.editor.renderer.data.CompositeItemVO;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
import com.uwsoft.editor.utils.runtime.EntityUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
* Created by Sasun Poghosyan on 11/28/2016.
*/
public class ExportLibraryItemFromResPanelCommand extends NonRevertibleCommand {
private static final String CLASS_NAME = "com.uwsoft.editor.controller.commands.resource.ExportLibraryItemFromResPanelCommand";
public static final String DONE = CLASS_NAME + "DONE";
private final Json json = new Json(JsonWriter.OutputType.json);

public ExportLibraryItemFromResPanelCommand() {
super();
}

@Override
public void execute(Notification notification) {
super.execute(notification);
String libraryItemName = notification.getBody();
String currentProjectPath = projectManager.getCurrentProjectPath();

ProjectManager projectManager = Overlap2DFacade.getInstance().retrieveProxy(ProjectManager.NAME);
HashMap<String, CompositeItemVO> libraryItems = projectManager.currentProjectInfoVO.libraryItems;
CompositeItemVO compositeItemVO = libraryItems.get(libraryItemName);

String jsonString = json.toJson(compositeItemVO);
json.prettyPrint(jsonString);

try {
FileUtils.writeStringToFile(new File(currentProjectPath + "\\export\\libraryItems\\" + libraryItemName + ".json"), jsonString, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void doAction() {

}

@Override
public void callDoAction() {
super.callDoAction();
}

@Override
public void cancel() {
super.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.badlogic.ashley.core.Entity;
import com.uwsoft.editor.controller.commands.NonRevertibleCommand;
import com.uwsoft.editor.proxy.ResolutionManager;
import com.uwsoft.editor.renderer.components.TextureRegionComponent;
import com.uwsoft.editor.renderer.data.CompositeItemVO;
import com.uwsoft.editor.renderer.data.SceneVO;
Expand Down Expand Up @@ -31,8 +30,9 @@ public void doAction() {
if (projectManager.deleteSingleImage(imageName)) {
deleteEntitiesWithImages(sandbox.getRootEntity(), imageName);
deleteAllItemsImages(imageName);
ResolutionManager resolutionManager = facade.retrieveProxy(ResolutionManager.NAME);
resolutionManager.rePackProjectImagesForAllResolutions();
//TODO open 34 and 35 lines for repacking project images after image deletion
// ResolutionManager resolutionManager = facade.retrieveProxy(ResolutionManager.NAME);
// resolutionManager.rePackProjectImagesForAllResolutions();
projectManager.loadProjectData(projectManager.getCurrentProjectPath());
sendNotification(DONE, imageName);
SceneVO vo = sandbox.sceneVoFromItems();
Expand Down
81 changes: 75 additions & 6 deletions overlap2d/src/com/uwsoft/editor/proxy/ProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import com.google.common.collect.Lists;
import com.kotcrab.vis.ui.util.dialog.DialogUtils;
import com.kotcrab.vis.ui.widget.file.FileChooser;
import com.kotcrab.vis.ui.widget.file.FileChooserAdapter;
import com.puremvc.patterns.proxy.BaseProxy;
import com.uwsoft.editor.Overlap2DFacade;
import com.uwsoft.editor.data.manager.PreferencesManager;
Expand All @@ -50,7 +53,10 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -140,6 +146,7 @@ public void createEmptyProject(String projectPath, int width, int height, int pi

FileUtils.forceMkdir(new File(projPath));
FileUtils.forceMkdir(new File(projPath + File.separator + "export"));
FileUtils.forceMkdir(new File(projPath + File.separator + "export/libraryItems"));
FileUtils.forceMkdir(new File(projPath + File.separator + "assets"));
FileUtils.forceMkdir(new File(projPath + File.separator + "scenes"));
FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig"));
Expand Down Expand Up @@ -213,7 +220,7 @@ public void openProjectAndLoadAllData(String projectPath, String resolution) {
String prjInfoFilePath = projectPath + "/project.dt";
FileHandle projectInfoFile = Gdx.files.internal(prjInfoFilePath);
String projectInfoContents = FileUtils.readFileToString(projectInfoFile.file());
ProjectInfoVO voInfo = json.fromJson(ProjectInfoVO.class, projectInfoContents);
ProjectInfoVO voInfo = json.fromJson(ProjectInfoVO.class, projectInfoContents);// here project reads all data from json
currentProjectInfoVO = voInfo;

} catch (IOException e) {
Expand Down Expand Up @@ -274,13 +281,64 @@ public void loadProjectData(String projectPath) {

public void saveCurrentProject() {
try {
//here project saves all necessary files
FileUtils.writeStringToFile(new File(currentProjectPath + "/project.pit"), currentProjectVO.constructJsonString(), "utf-8");
FileUtils.writeStringToFile(new File(currentProjectPath + "/project.dt"), currentProjectInfoVO.constructJsonString(), "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
}

public void exportAllLibraryItems() {
Json json = new Json(JsonWriter.OutputType.json);
String jsonString;
for (String libraryItemName : currentProjectInfoVO.libraryItems.keySet()) {
CompositeItemVO compositeItemVO = currentProjectInfoVO.libraryItems.get(libraryItemName);
jsonString = json.toJson(compositeItemVO);
json.prettyPrint(jsonString);
try {
FileUtils.writeStringToFile(new File(currentProjectPath + "\\export\\libraryItems\\" + libraryItemName + ".json"), jsonString, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void importLibraryItems() {
Sandbox sandbox = Sandbox.getInstance();
//chooser creation
FileChooser fileChooser = new FileChooser(FileChooser.Mode.OPEN);
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setListener(new FileChooserAdapter() {
@Override
public void selected(Array<FileHandle> files) {
System.out.println("file chosen");
for (FileHandle fileHandle : files) {
String path = fileHandle.file().getAbsolutePath();
if (fileHandle.extension().equals("json")) {
openFromLibraryItem(path);
}
}
}
});
sandbox.getUIStage().addActor(fileChooser.fadeIn());
}

private void openFromLibraryItem(String projectPath) {
Json json = new Json();
FileHandle projectInfoFile = Gdx.files.internal(projectPath);
String projectInfoContents = null;
try {
projectInfoContents = FileUtils.readFileToString(projectInfoFile.file());
} catch (IOException e) {
e.printStackTrace();
}
CompositeItemVO voInfo = json.fromJson(CompositeItemVO.class, projectInfoContents);
String fileNameAndExtension = projectInfoFile.name();
String fileName = FilenameUtils.removeExtension(fileNameAndExtension);
this.currentProjectInfoVO.libraryItems.put(fileName, voInfo);
}

public void saveCurrentProject(SceneVO vo) {
saveCurrentProject();
SceneDataManager sceneDataManager = facade.retrieveProxy(SceneDataManager.NAME);
Expand Down Expand Up @@ -1166,18 +1224,29 @@ public boolean deleteImage(String imageName) {
path.resolveSibling(path.getFileName() + ".png"),
path.resolveSibling(path.getFileName() + ".9.png"));

for(Path p : possibleFiles) {
for (Path p : possibleFiles) {
if (p.toFile().exists())
return p.toFile().delete();
}

throw new IllegalStateException(String.format("The file %s is not found",path.toString()));
throw new IllegalStateException(String.format("The file %s is not found", path.toString()));

}

public boolean deleteSingleImage(String imageName) {
String imagesPath = currentProjectPath + File.separator + IMAGE_DIR_PATH + File.separator;
String filePath = imagesPath + imageName + ".png";
String imagesPath;
String filePath;

for (ResolutionEntryVO resolutionEntryVO : currentProjectInfoVO.resolutions) {
imagesPath = currentProjectPath + File.separator + "assets/" + resolutionEntryVO.name + "/images" + File.separator;
filePath = imagesPath + imageName + ".png";
File file = new File(filePath);
if (file.delete()) {
System.out.println("deleted " + resolutionEntryVO.name + "'s resolution " + imageName + ".png" + " image");
}
}
imagesPath = currentProjectPath + File.separator + IMAGE_DIR_PATH + File.separator;
filePath = imagesPath + imageName + ".png";
return (new File(filePath)).delete();
}

Expand Down
Loading