2019-04-30 16:49:02 +02:00
package org.hso.texturesyncclient.view.importView
2019-06-12 22:42:28 +02:00
import javafx.scene.image.Image
import javafx.stage.FileChooser
2019-06-03 17:33:23 +02:00
import org.hso.texturesyncclient.controller.RootController
2019-04-30 16:49:02 +02:00
import tornadofx.Controller
2019-06-07 13:49:40 +02:00
import java.io.File
2019-06-12 22:42:28 +02:00
import java.io.FileInputStream
2019-06-07 13:49:40 +02:00
2019-04-30 16:49:02 +02:00
class ImportViewController : Controller ( ) {
2019-05-01 22:38:30 +02:00
2019-06-03 17:33:23 +02:00
private val iv = find ( ImportView :: class )
private val rootc = find ( RootController :: class )
2019-06-07 13:49:40 +02:00
private var lastImportDir : String = System . getProperty ( " user.home " )
2019-05-01 22:38:30 +02:00
fun btnFileChooserAction ( ) {
2019-06-07 13:49:40 +02:00
val fileChooser = FileChooser ( )
val fileExtensions = FileChooser . ExtensionFilter (
" Texturen vom Bildformat: PNG oder JPEG " , " *.png " , " *.PNG " , " *.jpg " , " *.JPG " , " *.jpeg " , " *.JPEG "
)
fileChooser . extensionFilters . addAll ( fileExtensions )
fileChooser . initialDirectory = File ( lastImportDir )
fileChooser . title = " Textur auswählen "
val file = fileChooser . showOpenDialog ( null )
2019-06-03 17:33:23 +02:00
2019-06-07 13:49:40 +02:00
if ( file != null ) {
iv . tfFilePath . text = file . absolutePath
iv . tfName . text = file . nameWithoutExtension
lastImportDir = file . parent . toString ( ) //store last user chosen dir
2019-06-12 22:42:28 +02:00
runAsync {
try {
val fileInput = FileInputStream ( file . absolutePath )
val img = Image ( fileInput )
iv . preview . setTexture ( img )
iv . preview . root . isVisible = true
} catch ( e : Exception ) {
// Got a catch'em all
println ( e . message )
}
}
2019-06-03 17:33:23 +02:00
}
}
2019-05-05 23:45:13 +02:00
2019-06-03 17:33:23 +02:00
fun btnImportAction ( ) {
rootc . importTexture ( iv . tfFilePath . text , iv . tfName . text , iv . cvTags . chips )
2019-06-17 20:19:54 +02:00
RootController . switchImportToMain ( true )
2019-06-14 01:14:01 +02:00
reset ( )
2019-06-03 17:33:23 +02:00
}
2019-05-05 23:45:13 +02:00
2019-06-03 17:33:23 +02:00
fun validateImport ( ) {
2019-06-07 13:25:33 +02:00
iv . btnImport . isVisible =
iv . tfFilePath . text . isNotEmpty ( ) && iv . tfName . text . isNotEmpty ( ) && iv . cvTags . chips . isNotEmpty ( ) && iv . cvTags . chips . stream ( ) . allMatch { x -> x . length < 32 }
2019-05-01 22:38:30 +02:00
}
2019-06-07 13:25:33 +02:00
fun btnBackAction ( ) {
2019-06-17 20:19:54 +02:00
RootController . switchImportToMain ( false )
2019-06-14 01:14:01 +02:00
reset ( )
}
2019-06-17 22:40:52 +02:00
private fun reset ( ) {
2019-06-07 13:25:33 +02:00
iv . tfFilePath . clear ( )
iv . tfName . clear ( )
iv . cvTags . chips . clear ( )
2019-06-14 01:14:01 +02:00
iv . preview . root . isVisible = false
2019-06-07 13:25:33 +02:00
}
}