Skip to content

Commit

Permalink
Move and refactor AsyncTask API, #174
Browse files Browse the repository at this point in the history
  • Loading branch information
kotcrab committed Oct 20, 2016
1 parent 79ad004 commit 744f217
Show file tree
Hide file tree
Showing 21 changed files with 481 additions and 228 deletions.
14 changes: 6 additions & 8 deletions editor/src/main/java/com/kotcrab/vis/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.kotcrab.vis.editor.plugin.api.ContainerExtension.ExtensionScope;
import com.kotcrab.vis.editor.ui.NoProjectFilesOpenView;
import com.kotcrab.vis.editor.ui.WindowListener;
import com.kotcrab.vis.editor.ui.dialog.AsyncTaskProgressDialog;
import com.kotcrab.vis.editor.ui.dialog.NewProjectDialog;
import com.kotcrab.vis.editor.ui.dialog.SettingsDialog;
import com.kotcrab.vis.editor.ui.dialog.UnsavedResourcesDialog;
Expand All @@ -46,10 +45,11 @@
import com.kotcrab.vis.editor.util.ApplicationUtils;
import com.kotcrab.vis.editor.util.GLFWIconSetter;
import com.kotcrab.vis.editor.util.ThreadUtils;
import com.kotcrab.vis.editor.util.async.AsyncTask;
import com.kotcrab.vis.editor.util.async.Async;
import com.kotcrab.vis.editor.util.scene2d.VisGroup;
import com.kotcrab.vis.editor.util.vis.LaunchConfiguration;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.util.async.AsyncTask;
import com.kotcrab.vis.ui.util.dialog.Dialogs;
import com.kotcrab.vis.ui.util.dialog.Dialogs.OptionDialog;
import com.kotcrab.vis.ui.util.dialog.Dialogs.OptionDialogType;
Expand Down Expand Up @@ -403,13 +403,13 @@ public void yes () {

ProjectLoadingDialogController controller = new ProjectLoadingDialogController();

AsyncTaskProgressDialog dialog = new AsyncTaskProgressDialog("Loading Project", new AsyncTask("ProjectLoaderThread") {
Async.startTask(stage, "Loading Project", new AsyncTask("ProjectLoaderThread") {
@Override
public void execute () {
protected void doInBackground () throws Exception {
setProgressPercent(10);
setMessage("Loading project data...");

executeOnOpenGL(() -> {
executeOnGdx(() -> {
projectLoaded = true;
projectMC.setProject(project);
VisContainers.createProjectModules(projectMC, extensionStorage);
Expand All @@ -419,7 +419,7 @@ public void execute () {
setProgressPercent(50);
ThreadUtils.sleep(10);

executeOnOpenGL(() -> {
executeOnGdx(() -> {
projectMC.init();

settingsDialog.addAll(projectMC.getModules());
Expand All @@ -434,8 +434,6 @@ public void execute () {
}
}
});
dialog.setVisible(true);
stage.addActor(dialog);
}

private void switchProject (final Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
import com.kotcrab.vis.editor.plugin.api.ExporterPlugin;
import com.kotcrab.vis.editor.scene.EditorLayer;
import com.kotcrab.vis.editor.scene.EditorScene;
import com.kotcrab.vis.editor.ui.dialog.AsyncTaskProgressDialog;
import com.kotcrab.vis.editor.ui.dialog.DefaultExporterSettingsDialog;
import com.kotcrab.vis.editor.ui.dialog.UnsavedResourcesDialog;
import com.kotcrab.vis.editor.util.FileUtils;
import com.kotcrab.vis.editor.util.Holder;
import com.kotcrab.vis.editor.util.async.SteppedAsyncTask;
import com.kotcrab.vis.editor.util.async.Async;
import com.kotcrab.vis.editor.util.gdx.VisTexturePacker;
import com.kotcrab.vis.editor.util.vis.ProjectPathUtils;
import com.kotcrab.vis.editor.util.vis.TextureCacheFilter;
Expand All @@ -48,6 +47,7 @@
import com.kotcrab.vis.runtime.data.SceneData;
import com.kotcrab.vis.runtime.properties.StoresAssetDescriptor;
import com.kotcrab.vis.runtime.scene.SceneLoader;
import com.kotcrab.vis.ui.util.async.SteppedAsyncTask;

import java.util.UUID;

Expand Down Expand Up @@ -173,7 +173,7 @@ private void doExport () {

private void exportProject () {
ExportAsyncTask exportTask = new ExportAsyncTask();
stage.addActor(new AsyncTaskProgressDialog("Exporting", exportTask).fadeIn());
Async.startTask(stage, "Exporting", exportTask);
}

private class ExportAsyncTask extends SteppedAsyncTask {
Expand All @@ -186,7 +186,7 @@ public ExportAsyncTask () {
}

@Override
public void execute () {
public void doInBackground () {
setMessage("Preparing for export...");
setTotalSteps(calculateSteps());

Expand Down Expand Up @@ -278,7 +278,7 @@ private void exportScenes (FileHandle sceneDir, FileHandle outDir) {
setMessage("Exporting scene: " + file.name());
outDir.mkdirs();

executeOnOpenGL(() -> scene = sceneCache.get(file));
executeOnGdx(() -> scene = sceneCache.get(file));

SceneData sceneData = new SceneData();

Expand Down Expand Up @@ -319,7 +319,7 @@ private void exportSceneTextures (FileHandle sceneDir, FileHandle outDir) {
sceneTextureDir.deleteDirectory();
sceneTextureDir.mkdirs();

executeOnOpenGL(() -> scene = sceneCache.get(file));
executeOnGdx(() -> scene = sceneCache.get(file));

scene.getSchemes().forEach(scheme -> scheme.getComponents().forEach(component ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
import com.kotcrab.vis.editor.module.project.*;
import com.kotcrab.vis.editor.module.project.converter.DummyConverter;
import com.kotcrab.vis.editor.module.project.converter.ProjectConverter;
import com.kotcrab.vis.editor.ui.dialog.AsyncTaskProgressDialog;
import com.kotcrab.vis.editor.util.CopyFileVisitor;
import com.kotcrab.vis.editor.util.async.AsyncTask;
import com.kotcrab.vis.editor.util.async.AsyncTaskListener;
import com.kotcrab.vis.editor.util.async.SteppedAsyncTask;
import com.kotcrab.vis.editor.util.async.Async;
import com.kotcrab.vis.editor.util.async.AsyncTaskAdapter;
import com.kotcrab.vis.editor.util.vis.EditorException;
import com.kotcrab.vis.editor.util.vis.WikiPages;
import com.kotcrab.vis.ui.util.async.AsyncTask;
import com.kotcrab.vis.ui.util.async.AsyncTaskProgressDialog;
import com.kotcrab.vis.ui.util.async.SteppedAsyncTask;
import com.kotcrab.vis.ui.util.dialog.Dialogs;
import com.kotcrab.vis.ui.util.dialog.Dialogs.OptionDialogType;
import com.kotcrab.vis.ui.util.dialog.OptionDialogAdapter;
Expand Down Expand Up @@ -66,7 +67,6 @@ public class ProjectIOModule extends EditorModule {
private Gson gson;

private Array<ProjectConverter> projectConverters = new Array<>();
private AsyncTaskProgressDialog taskDialog;

@Override
public void init () {
Expand Down Expand Up @@ -148,25 +148,29 @@ public void yes () {
}
});
} else if (descriptor.versionCode < App.VERSION_CODE) {
backupProject(dataFile, descriptor.versionCode, () -> convertAndLoad(dataFile, descriptor.versionCode));
backupProject(dataFile, descriptor.versionCode, new AsyncTaskAdapter() {
@Override
public void finished () {
convertAndLoad(dataFile, descriptor.versionCode);
}
});
} else
doLoadProject(dataFile);
} else //if there is no version file that means that project was just created
doLoadProject(dataFile);

}

private void backupProject (FileHandle dataFile, int oldVersionCode, AsyncTaskListener listener) {
private void backupProject (FileHandle dataFile, int oldVersionCode, AsyncTaskAdapter listener) {
Project project = readProjectDataFile(dataFile);
FileHandle backupRoot = project.getVisDirectory();
FileHandle backupOut = backupRoot.child("modules").child(".conversionBackup");
backupOut.mkdirs();

FileHandle backupArchive = backupOut.child("before-conversion-from-" + oldVersionCode + "-to-" + App.VERSION_CODE + ".zip");

taskDialog = new AsyncTaskProgressDialog("Creating backup", new CreateProjectBackupAsyncTask(backupRoot, backupArchive));
taskDialog.setTaskListener(listener);
stage.addActor(taskDialog.fadeIn());
AsyncTaskProgressDialog taskDialog = Async.startTask(stage, "Creating backup", new CreateProjectBackupAsyncTask(backupRoot, backupArchive));
taskDialog.addListener(listener);
}

private void convertAndLoad (FileHandle dataFile, int versionCode) {
Expand All @@ -184,9 +188,13 @@ private void convertAndLoad (FileHandle dataFile, int versionCode) {
return;
}

taskDialog = new AsyncTaskProgressDialog("Converting project", task);
taskDialog.setTaskListener(() -> convertAndLoad(dataFile, converter.getToVersion())); //damn recursive async tasks
stage.addActor(taskDialog.fadeIn());
AsyncTaskProgressDialog taskDialog = Async.startTask(stage, "Converting project", task);
taskDialog.addListener(new AsyncTaskAdapter() {
@Override
public void finished () {
convertAndLoad(dataFile, converter.getToVersion());
}
});
return;
}
}
Expand Down Expand Up @@ -216,7 +224,7 @@ public void createLibGDXProject (final ProjectLibGDX project) {
AsyncTask task = new AsyncTask("ProjectCreator") {

@Override
public void execute () {
public void doInBackground () {
setMessage("Creating directory structure...");

FileHandle projectRoot = Gdx.files.absolute(project.getRoot());
Expand Down Expand Up @@ -249,18 +257,18 @@ public void execute () {
setProgressPercent(100);
statusBar.setText("Project created!");

executeOnOpenGL(() -> loadNewGeneratedProject(projectFile));
executeOnGdx(() -> loadNewGeneratedProject(projectFile));
}
};

stage.addActor(new AsyncTaskProgressDialog("Creating project", task).fadeIn());
Async.startTask(stage, "Creating project", task);
}

public void createGenericProject (ProjectGeneric project) {
AsyncTask task = new AsyncTask("ProjectCreator") {

@Override
public void execute () {
public void doInBackground () {
setMessage("Creating directory structure...");

FileHandle visDir = project.getVisDirectory();
Expand All @@ -281,11 +289,11 @@ public void execute () {
setProgressPercent(100);
statusBar.setText("Project created!");

executeOnOpenGL(() -> loadNewGeneratedProject(projectFile));
executeOnGdx(() -> loadNewGeneratedProject(projectFile));
}
};

stage.addActor(new AsyncTaskProgressDialog("Creating project", task).fadeIn());
Async.startTask(stage, "Creating project", task);
}

private void saveProjectFile (Project project, FileHandle projectFile) {
Expand Down Expand Up @@ -330,7 +338,7 @@ public CreateProjectBackupAsyncTask (FileHandle backupRoot, FileHandle backupArc
}

@Override
public void execute () {
public void doInBackground () {
try {
setMessage("Creating project backup...");
setTotalSteps(countZipFiles(backupRoot, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.kotcrab.vis.editor.plugin.api.AssetsFileSorter;
import com.kotcrab.vis.editor.plugin.api.AssetsUIContextGeneratorProvider;
import com.kotcrab.vis.editor.ui.SearchField;
import com.kotcrab.vis.editor.ui.dialog.AsyncTaskProgressDialog;
import com.kotcrab.vis.editor.ui.dialog.DeleteDialog;
import com.kotcrab.vis.editor.ui.dialog.EnterPathDialog;
import com.kotcrab.vis.editor.ui.tab.AssetsUsagesTab;
Expand All @@ -52,6 +51,7 @@
import com.kotcrab.vis.editor.util.DirectoryWatcher.WatchListener;
import com.kotcrab.vis.editor.util.FileUtils;
import com.kotcrab.vis.editor.util.Holder;
import com.kotcrab.vis.editor.util.async.Async;
import com.kotcrab.vis.editor.util.async.CopyFileTaskDescriptor;
import com.kotcrab.vis.editor.util.async.CopyFilesAsyncTask;
import com.kotcrab.vis.editor.util.scene2d.*;
Expand Down Expand Up @@ -781,7 +781,7 @@ private void clipboardPasteFiles () {
tasks.add(new CopyFileTaskDescriptor(item.getFile(), currentDirectory, overwrites));
}

stage.addActor(new AsyncTaskProgressDialog("Copying files", new CopyFilesAsyncTask(stage, tasks)).fadeIn());
Async.startTask(stage, "Copying files", new CopyFilesAsyncTask(stage, tasks));
}

private boolean doesFileExists (Array<FileHandle> files, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.kotcrab.vis.editor.module.project.converter;

import com.badlogic.gdx.files.FileHandle;
import com.kotcrab.vis.editor.util.async.AsyncTask;
import com.kotcrab.vis.ui.util.async.AsyncTask;

/** @author Kotcrab */
public class DummyConverter extends ProjectConverter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.badlogic.gdx.files.FileHandle;
import com.kotcrab.vis.editor.module.editor.EditorModuleContainer;
import com.kotcrab.vis.editor.util.async.AsyncTask;
import com.kotcrab.vis.ui.util.async.AsyncTask;

/**
* Base class for project converters.
Expand Down
33 changes: 33 additions & 0 deletions editor/src/main/java/com/kotcrab/vis/editor/util/async/Async.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2014-2016 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.kotcrab.vis.editor.util.async;

import com.badlogic.gdx.scenes.scene2d.Stage;
import com.kotcrab.vis.ui.util.async.AsyncTask;
import com.kotcrab.vis.ui.util.async.AsyncTaskProgressDialog;

/** @author Kotcrab */
public class Async {
private Async () {
}

public static AsyncTaskProgressDialog startTask (Stage stage, String title, AsyncTask task) {
AsyncTaskProgressDialog dialog = new LoggingAsyncTaskProgressDialog(title, task);
stage.addActor(dialog.fadeIn());
return dialog;
}
}
Loading

0 comments on commit 744f217

Please sign in to comment.