forked from openrndr/openrndr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
9 changed files
with
263 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
openrndr-core/src/main/kotlin/org/openrndr/platform/GenericPlatformDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
openrndr-core/src/main/kotlin/org/openrndr/platform/MacOSPlatformDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
openrndr-core/src/main/kotlin/org/openrndr/platform/Platform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
openrndr-core/src/main/kotlin/org/openrndr/platform/PlatformDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
48 changes: 48 additions & 0 deletions
48
openrndr-core/src/main/kotlin/org/openrndr/platform/WindowsPlatformDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters