Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
edwin committed May 28, 2018
2 parents 7cfbdea + b716999 commit 936cb56
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}

}
}
27 changes: 27 additions & 0 deletions openrndr-core/src/main/kotlin/org/openrndr/platform/Platform.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}

}

}
9 changes: 9 additions & 0 deletions openrndr-dialogs/build.gradle
Original file line number Diff line number Diff line change
@@ -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"

}
79 changes: 79 additions & 0 deletions openrndr-dialogs/src/main/kotlin/FileDialogs.kt
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 936cb56

Please sign in to comment.