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

Add multi-texture import, drag and drop support #82

Merged
merged 1 commit into from
Jul 18, 2022
Merged
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
5 changes: 5 additions & 0 deletions editor/src/main/com/mbrlabs/mundus/editor/Editor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.mbrlabs.mundus.commons.utils.ShaderUtils
import com.mbrlabs.mundus.editor.core.project.ProjectContext
import com.mbrlabs.mundus.editor.core.project.ProjectManager
import com.mbrlabs.mundus.editor.core.registry.Registry
import com.mbrlabs.mundus.editor.events.FilesDroppedEvent
import com.mbrlabs.mundus.editor.events.FullScreenEvent
import com.mbrlabs.mundus.editor.events.ProjectChangedEvent
import com.mbrlabs.mundus.editor.events.SceneChangedEvent
Expand Down Expand Up @@ -192,6 +193,10 @@ class Editor : Lwjgl3WindowAdapter(), ApplicationListener,
override fun pause() {}
override fun resume() {}

override fun filesDropped(files: Array<out String>?) {
Mundus.postEvent(FilesDroppedEvent(files))
}

override fun dispose() {
Mundus.dispose()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mbrlabs.mundus.editor.events

/**
* Event which is posted when files are dropped from an operating system into the mundus window.
*
* @author JamesTKhan
* @version July 18, 2022
*/
class FilesDroppedEvent(val files: Array<out String>?) {

interface FilesDroppedListener {
@Subscribe
fun onFilesDropped(event: FilesDroppedEvent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@

package com.mbrlabs.mundus.editor.ui.modules.dialogs

import com.badlogic.gdx.scenes.scene2d.Stage
import com.kotcrab.vis.ui.widget.VisDialog

/**
* Base dialog to extend from. Tracks if it is currently open or not.
*
* @author Marcus Brummer
* @version 25-11-2015
*/
open class BaseDialog(title: String) : VisDialog(title) {
protected var dialogOpen = false

override fun show(stage: Stage?): VisDialog {
dialogOpen = true
return super.show(stage)
}

override fun close() {
super.close()
dialogOpen = false
}

init {
addCloseButton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.badlogic.gdx.utils.Array
import com.kotcrab.vis.ui.util.FloatDigitsOnlyFilter
import com.kotcrab.vis.ui.util.dialog.Dialogs
import com.kotcrab.vis.ui.widget.VisCheckBox
Expand All @@ -44,6 +45,7 @@ import com.mbrlabs.mundus.editor.events.SceneChangedEvent
import com.mbrlabs.mundus.editor.shader.Shaders
import com.mbrlabs.mundus.editor.ui.widgets.ImageChooserField
import com.mbrlabs.mundus.editor.utils.createDefaultSkybox
import java.util.HashMap

/**
* @author Marcus Brummer
Expand All @@ -57,12 +59,12 @@ class SkyboxDialog : BaseDialog("Skybox"), ProjectChangedEvent.ProjectChangedLis
private lateinit var rotateEnabled: VisCheckBox
private var rotateSpeed = VisTextField()

private val positiveX: ImageChooserField = ImageChooserField(100, this)
private var negativeX: ImageChooserField = ImageChooserField(100, this)
private var positiveY: ImageChooserField = ImageChooserField(100, this)
private var negativeY: ImageChooserField = ImageChooserField(100, this)
private var positiveZ: ImageChooserField = ImageChooserField(100, this)
private var negativeZ: ImageChooserField = ImageChooserField(100, this)
private val positiveX: ImageChooserField = ImageChooserField(100, false, this)
private var negativeX: ImageChooserField = ImageChooserField(100, false, this)
private var positiveY: ImageChooserField = ImageChooserField(100, false, this)
private var negativeY: ImageChooserField = ImageChooserField(100, false, this)
private var positiveZ: ImageChooserField = ImageChooserField(100, false, this)
private var negativeZ: ImageChooserField = ImageChooserField(100, false, this)

private var createBtn = VisTextButton("Create skybox")
private var defaultBtn = VisTextButton("Create default skybox")
Expand Down Expand Up @@ -377,4 +379,11 @@ class SkyboxDialog : BaseDialog("Skybox"), ProjectChangedEvent.ProjectChangedLis
validateFields()
}

override fun onImagesChosen(
images: Array<FileHandle>?,
failedFiles: HashMap<FileHandle, String>
) {
// Unused
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,35 @@

package com.mbrlabs.mundus.editor.ui.modules.dialogs.importer

import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener
import com.badlogic.gdx.utils.Align
import com.badlogic.gdx.utils.Array
import com.badlogic.gdx.utils.Disposable
import com.kotcrab.vis.ui.util.dialog.Dialogs
import com.kotcrab.vis.ui.widget.VisTable
import com.kotcrab.vis.ui.widget.VisTextButton
import com.mbrlabs.mundus.editor.Mundus
import com.mbrlabs.mundus.editor.assets.AssetAlreadyExistsException
import com.mbrlabs.mundus.editor.core.project.ProjectManager
import com.mbrlabs.mundus.editor.events.AssetImportEvent
import com.mbrlabs.mundus.editor.events.FilesDroppedEvent
import com.mbrlabs.mundus.editor.ui.UI
import com.mbrlabs.mundus.editor.ui.modules.dialogs.BaseDialog
import com.mbrlabs.mundus.editor.ui.widgets.ImageChooserField
import com.mbrlabs.mundus.editor.ui.widgets.ImageChooserField.validateImageFile
import com.mbrlabs.mundus.editor.utils.Log
import com.mbrlabs.mundus.editor.utils.isImage
import java.io.File
import java.io.IOException

/**
* @author Marcus Brummer
* @version 07-06-2016
*/
class ImportTextureDialog : BaseDialog("Import Texture"), Disposable {
class ImportTextureDialog : BaseDialog("Import Texture"), FilesDroppedEvent.FilesDroppedListener, ImageChooserField.ImageChosenListener, Disposable {

companion object {
private val TAG = ImportTextureDialog::class.java.simpleName
Expand All @@ -49,13 +55,17 @@ class ImportTextureDialog : BaseDialog("Import Texture"), Disposable {
private val projectManager: ProjectManager = Mundus.inject()

init {
Mundus.registerEventListener(this)

isModal = true
isMovable = true

val root = VisTable()
add<Table>(root).expand().fill()
importTextureTable = ImportTextureTable()
importTextureTable = ImportTextureTable(this)

root.add("Drag and drop import is supported.").align(Align.center).row()
root.add("Preview Mode only supported for single texture selection.").align(Align.center).row()
root.add(importTextureTable).minWidth(300f).expand().fill().left().top()
}

Expand All @@ -71,10 +81,10 @@ class ImportTextureDialog : BaseDialog("Import Texture"), Disposable {
/**

*/
private inner class ImportTextureTable : VisTable(), Disposable {
private inner class ImportTextureTable(listener: ImageChooserField.ImageChosenListener) : VisTable(), Disposable {
// UI elements
private val importBtn = VisTextButton("IMPORT")
private val imageChooserField = ImageChooserField(300)
private val imageChooserField = ImageChooserField(300, true, listener)

init {
this.setupUI()
Expand Down Expand Up @@ -115,6 +125,10 @@ class ImportTextureDialog : BaseDialog("Import Texture"), Disposable {
})
}

fun getImageChooserField(): ImageChooserField {
return imageChooserField
}

fun removeTexture() {
imageChooserField.removeImage()
}
Expand All @@ -124,4 +138,80 @@ class ImportTextureDialog : BaseDialog("Import Texture"), Disposable {
}
}

override fun onFilesDropped(event: FilesDroppedEvent) {
if (!dialogOpen) return
if (event.files == null || event.files.isEmpty()) return

if (event.files.size == 1) {
val fileHandle = FileHandle(File(event.files[0]))
val errorMessage = validateImageFile(fileHandle)

if (errorMessage == null) {
importTextureTable.getImageChooserField().setImage(fileHandle)
} else {
Dialogs.showErrorDialog(stage, errorMessage)
}
return
}

// <File Name, Error Message>
val failedFiles = HashMap<FileHandle, String>()
val files = Array<FileHandle>()

for (filePath in event.files) {
val fileHandle = FileHandle(File(filePath))
val errorMessage = validateImageFile(fileHandle)
if (errorMessage == null) {
files.add(fileHandle)
} else {
failedFiles[fileHandle] = errorMessage
}
}

importFiles(files, failedFiles)
}

private fun importFiles(files: Array<FileHandle>, failedFiles: HashMap<FileHandle, String>) {
for (file in files) {
try {
val assetManager = projectManager.current().assetManager
val asset = assetManager.createTextureAsset(file)
Mundus.postEvent(AssetImportEvent(asset))
} catch (ee: AssetAlreadyExistsException) {
Log.exception(TAG, ee)
failedFiles[file] = "There already exists a texture with the same name"
}
}

if (failedFiles.isEmpty()) {
close()
UI.toaster.success("Textures imported")
return
}

val dialogMessage = buildString {
append("The following could not be imported\n\n")
for (file in failedFiles) {
append(file.key.name())
append(" : ")
append(file.value)
append("\n")
}
}

Dialogs.showErrorDialog(stage, dialogMessage)
close()
}

override fun onImageChosen() {
// Unused
}

override fun onImagesChosen(
images: Array<FileHandle>,
failedFiles: HashMap<FileHandle, String>
) {
importFiles(images, failedFiles)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public interface FileSelected {

private FileChooser.SelectionMode mode = FileChooser.SelectionMode.FILES;
private FileSelected fileSelected;
private VisTextField textField;
private VisTextButton fcBtn;
private final VisTextField textField;
private final VisTextButton fcBtn;

private String path;
private FileHandle fileHandle;
Expand Down Expand Up @@ -107,6 +107,7 @@ public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
FileChooser fileChooser = UI.INSTANCE.getFileChooser();
fileChooser.setSelectionMode(mode);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setListener(new SingleFileChooserListener() {
@Override
protected void selected(FileHandle file) {
Expand Down
Loading