From 3d5806f5ce8d72ef2255103e9c9bac97650cc101 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 19 May 2018 18:07:16 +0200 Subject: [PATCH 1/3] Removed glfwFocusWindow --- .../src/main/kotlin/org/openrndr/internal/gl3/ApplicationGL3.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openrndr-gl3/src/main/kotlin/org/openrndr/internal/gl3/ApplicationGL3.kt b/openrndr-gl3/src/main/kotlin/org/openrndr/internal/gl3/ApplicationGL3.kt index 6d881ed74952..23dca8cc3f0b 100644 --- a/openrndr-gl3/src/main/kotlin/org/openrndr/internal/gl3/ApplicationGL3.kt +++ b/openrndr-gl3/src/main/kotlin/org/openrndr/internal/gl3/ApplicationGL3.kt @@ -400,7 +400,7 @@ class ApplicationGL3(private val program: Program, private val configuration: Co glfwSetCursorEnterCallback(window, { window, entered -> logger.debug { "cursor state changed; inside window = $entered" } if (entered) { - glfwFocusWindow(window) + //glfwFocusWindow(window) } }) From a95adcb3a95e313a4097f8c82ad29c896b0757af Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 19 May 2018 18:08:08 +0200 Subject: [PATCH 2/3] Removed unused imports from Composition --- openrndr-shape/src/main/kotlin/org/openrndr/shape/Composition.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/openrndr-shape/src/main/kotlin/org/openrndr/shape/Composition.kt b/openrndr-shape/src/main/kotlin/org/openrndr/shape/Composition.kt index 9d0262b22d6d..02514371af72 100644 --- a/openrndr-shape/src/main/kotlin/org/openrndr/shape/Composition.kt +++ b/openrndr-shape/src/main/kotlin/org/openrndr/shape/Composition.kt @@ -2,7 +2,6 @@ package org.openrndr.shape import org.openrndr.color.ColorRGBa import org.openrndr.math.Matrix44 -import java.security.acl.Group sealed class CompositionNode { var id: String? = null From b7169997e11a5a8d4cb881a0a7c7d20f5f8a97f1 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 19 May 2018 19:57:15 +0200 Subject: [PATCH 3/3] Added Platform, PlatformDriver and implementations Added openrndr-dialogs, native dialogs with proper last directory management --- .../platform/GenericPlatformDriver.kt | 44 +++++++++++ .../openrndr/platform/MacOSPlatformDriver.kt | 46 +++++++++++ .../kotlin/org/openrndr/platform/Platform.kt | 27 +++++++ .../org/openrndr/platform/PlatformDriver.kt | 9 +++ .../platform/WindowsPlatformDriver.kt | 48 +++++++++++ openrndr-dialogs/build.gradle | 9 +++ .../src/main/kotlin/FileDialogs.kt | 79 +++++++++++++++++++ 7 files changed, 262 insertions(+) create mode 100644 openrndr-core/src/main/kotlin/org/openrndr/platform/GenericPlatformDriver.kt create mode 100644 openrndr-core/src/main/kotlin/org/openrndr/platform/MacOSPlatformDriver.kt create mode 100644 openrndr-core/src/main/kotlin/org/openrndr/platform/Platform.kt create mode 100644 openrndr-core/src/main/kotlin/org/openrndr/platform/PlatformDriver.kt create mode 100644 openrndr-core/src/main/kotlin/org/openrndr/platform/WindowsPlatformDriver.kt create mode 100644 openrndr-dialogs/build.gradle create mode 100644 openrndr-dialogs/src/main/kotlin/FileDialogs.kt diff --git a/openrndr-core/src/main/kotlin/org/openrndr/platform/GenericPlatformDriver.kt b/openrndr-core/src/main/kotlin/org/openrndr/platform/GenericPlatformDriver.kt new file mode 100644 index 000000000000..75b209c59c19 --- /dev/null +++ b/openrndr-core/src/main/kotlin/org/openrndr/platform/GenericPlatformDriver.kt @@ -0,0 +1,44 @@ +package org.openrndr.platform + +import java.io.File + +class GenericPlatformDriver : PlatformDriver { + + override fun temporaryDirectory(): File { + val directoryName = System.getProperty("java.io.tmpdir") + "RNDR-" + randomID + val file = File(directoryName) + if (!file.exists()) { + file.mkdirs() + } + return file + } + + override fun cacheDirectory(programName: String): File { + val f = File("./cache") + if (f.exists()) { + val result = f.mkdirs() + if (!result) { + throw RuntimeException("could not create cache directory") + } + } + return f + } + + override fun supportDirectory(programName: String): File { + return File(".") + } + + companion object { + var randomID: String + init { + val alphabet = charArrayOf('a', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') + + var id = "" + for (i in 0..7) { + id += alphabet[i] + } + + randomID = id + } + } +} \ No newline at end of file diff --git a/openrndr-core/src/main/kotlin/org/openrndr/platform/MacOSPlatformDriver.kt b/openrndr-core/src/main/kotlin/org/openrndr/platform/MacOSPlatformDriver.kt new file mode 100644 index 000000000000..b012b99aece0 --- /dev/null +++ b/openrndr-core/src/main/kotlin/org/openrndr/platform/MacOSPlatformDriver.kt @@ -0,0 +1,46 @@ +package org.openrndr.platform + +import java.io.File + +class MacOSPlatformDriver : PlatformDriver { + override fun temporaryDirectory(): File { + val directoryName = System.getProperty("java.io.tmpdir") + "OPENRNDR-" + randomID + val file = File(directoryName) + if (!file.exists()) { + file.mkdirs() + } + return file + } + + override fun cacheDirectory(programName: String): File { + val directoryName = System.getProperty("user.home") + "/Library/Caches/" + programName + val file = File(directoryName) + if (!file.exists()) { + file.mkdirs() + } + return file + } + + override fun supportDirectory(programName: String): File { + val directoryName = System.getProperty("user.home") + "/Library/Application Support/" + programName + val file = File(directoryName) + if (!file.exists()) { + file.mkdirs() + } + return file + } + + companion object { + + var randomID: String + init { + val alphabet = charArrayOf('a', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') + var id = "" + for (i in 0..7) { + id += alphabet[i] + } + randomID = id + } + + } +} \ No newline at end of file diff --git a/openrndr-core/src/main/kotlin/org/openrndr/platform/Platform.kt b/openrndr-core/src/main/kotlin/org/openrndr/platform/Platform.kt new file mode 100644 index 000000000000..4ffa9cb687a5 --- /dev/null +++ b/openrndr-core/src/main/kotlin/org/openrndr/platform/Platform.kt @@ -0,0 +1,27 @@ +package org.openrndr.platform + +import java.io.File + +object Platform { + private val driver = instantiateDriver() + private fun instantiateDriver(): PlatformDriver { + val os = System.getProperty("os.name").toLowerCase() + return when { + os.startsWith("windows") -> WindowsPlatformDriver() + os.startsWith("mac") -> MacOSPlatformDriver() + else -> GenericPlatformDriver() + } + } + + fun tempDirectory(): File { + return driver.temporaryDirectory() + } + + fun cacheDirectory(programName: String): File { + return driver.cacheDirectory(programName) + } + + fun supportDirectory(programName: String): File { + return driver.supportDirectory(programName) + } +} \ No newline at end of file diff --git a/openrndr-core/src/main/kotlin/org/openrndr/platform/PlatformDriver.kt b/openrndr-core/src/main/kotlin/org/openrndr/platform/PlatformDriver.kt new file mode 100644 index 000000000000..d220d0dc09ab --- /dev/null +++ b/openrndr-core/src/main/kotlin/org/openrndr/platform/PlatformDriver.kt @@ -0,0 +1,9 @@ +package org.openrndr.platform + +import java.io.File + +internal interface PlatformDriver { + fun temporaryDirectory(): File + fun cacheDirectory(programName: String): File + fun supportDirectory(programName: String): File +} \ No newline at end of file diff --git a/openrndr-core/src/main/kotlin/org/openrndr/platform/WindowsPlatformDriver.kt b/openrndr-core/src/main/kotlin/org/openrndr/platform/WindowsPlatformDriver.kt new file mode 100644 index 000000000000..70ae50095701 --- /dev/null +++ b/openrndr-core/src/main/kotlin/org/openrndr/platform/WindowsPlatformDriver.kt @@ -0,0 +1,48 @@ +package org.openrndr.platform + + +import java.io.File + +class WindowsPlatformDriver : PlatformDriver { + + override fun temporaryDirectory(): File { + val directoryName = System.getProperty("java.io.tmpdir") + "OPENRNDR-" + randomID + val file = File(directoryName) + if (!file.exists()) { + file.mkdirs() + } + return file + } + + override fun cacheDirectory(programName: String): File { + val f = File(".\\cache") + if (!f.exists()) { + val result = f.mkdirs() + if (!result) { + throw RuntimeException("could not create cache directory") + } + } + return f + } + + override fun supportDirectory(programName: String): File { + return File(".") + } + + companion object { + val randomID: String + + init { + val alphabet = charArrayOf('a', 'b', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') + + var id = "" + for (i in 0..7) { + id += alphabet[i] + } + + randomID = id + } + + } + +} \ No newline at end of file diff --git a/openrndr-dialogs/build.gradle b/openrndr-dialogs/build.gradle new file mode 100644 index 000000000000..b83e073bff5f --- /dev/null +++ b/openrndr-dialogs/build.gradle @@ -0,0 +1,9 @@ +dependencies { + compile project(":openrndr-event") + compile project(":openrndr-core") + compile "org.lwjgl:lwjgl-nfd:$lwjglVersion" + runtime "org.lwjgl:lwjgl-nfd:$lwjglVersion:natives-windows" + runtime "org.lwjgl:lwjgl-nfd:$lwjglVersion:natives-linux" + runtime "org.lwjgl:lwjgl-nfd:$lwjglVersion:natives-macos" + +} diff --git a/openrndr-dialogs/src/main/kotlin/FileDialogs.kt b/openrndr-dialogs/src/main/kotlin/FileDialogs.kt new file mode 100644 index 000000000000..203e81a5c84a --- /dev/null +++ b/openrndr-dialogs/src/main/kotlin/FileDialogs.kt @@ -0,0 +1,79 @@ +import java.io.File + +import org.lwjgl.util.nfd.NativeFileDialog +import org.lwjgl.system.MemoryUtil.* +import org.lwjgl.util.nfd.NativeFileDialog.NFD_OKAY +import org.openrndr.platform.Platform +import java.io.FileInputStream +import java.io.FileOutputStream +import java.util.* + +private fun getDefaultPathForContext(programName:String, contextID:String):String? { + val props = Properties() + + return try { + val f = File(Platform.supportDirectory(programName), "file-dialog.properties") + if (f.exists()) { + val `is` = FileInputStream(f) + props.load(`is`) + `is`.close() + props.getProperty(contextID, null) + } else { + null + } + } catch (e: Exception) { + e.printStackTrace() + null + } +} + +private fun setDefaultPathForContext(programName:String, contextID:String, file:File) { + val props = Properties() + + try { + val f = File(Platform.supportDirectory(programName), "file-dialog.properties") + if (!f.exists()) { + f.createNewFile() + } + val `is` = FileInputStream(f) + props.load(`is`) + props.setProperty(contextID, file.let { if (it.isDirectory) it.absolutePath else it.parentFile.absolutePath }) + `is`.close() + val os = FileOutputStream(f) + props.store(os, "File dialog properties for $programName") + os.close() + } catch (e: Exception) { + e.printStackTrace() + } +} + +fun openFileDialog(programName: String = "OPENRNDR", contextID:String="global", function: (File) -> Unit) { + val filterList: CharSequence? = null + val defaultPath: CharSequence? = getDefaultPathForContext(programName, contextID) + val out = memAllocPointer(1) + + val r = NativeFileDialog.NFD_OpenDialog(filterList, defaultPath, out) + if (r == NFD_OKAY) { + val ptr = out.get(0) + val str = memUTF8(ptr) + val f = File(str) + setDefaultPathForContext(programName, contextID, f) + function(f) + } + memFree(out) +} + +fun saveFileDialog(programName:String = "OPENRNDR", contextID:String="global", function: (File) -> Unit) { + val filterList: CharSequence? = null + val defaultPath: CharSequence? = getDefaultPathForContext(programName, contextID) + val out = memAllocPointer(1) + val r = NativeFileDialog.NFD_SaveDialog(filterList, defaultPath, out) + if (r == NFD_OKAY) { + val ptr = out.get(0) + val str = memUTF8(ptr) + val f = File(str) + setDefaultPathForContext(programName, contextID, f) + function(f) + } + memFree(out) +} \ No newline at end of file