This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
201 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
@file:JvmName("NativeIO") | ||
|
||
package io.github.homchom.recode.io | ||
|
||
import io.github.homchom.recode.mc | ||
import org.lwjgl.PointerBuffer | ||
import org.lwjgl.system.MemoryStack | ||
import org.lwjgl.util.tinyfd.TinyFileDialogs | ||
import java.io.IOException | ||
import java.nio.file.Path | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.pathString | ||
|
||
/** | ||
* Opens a native file picker, blocks the current thread until the dialog closes, and returns the [Path] to | ||
* the picked file if one was picked. | ||
* | ||
* @param defaultPath The path to start at. If `null`, the operating system will choose. | ||
* @param filter The [FileFilter] to apply. If `null`, all files will be permitted. | ||
* | ||
* @throws IOException If the native file dialog errors for any reason. | ||
*/ | ||
@JvmOverloads | ||
fun pickFile(title: String? = null, defaultPath: Path? = null, filter: FileFilter? = null) = | ||
openFileDialog(filter?.patterns) { filterBuffer -> | ||
TinyFileDialogs.tinyfd_openFileDialog( | ||
title, | ||
defaultPath?.pathString, | ||
filterBuffer, | ||
filter?.description, | ||
false | ||
)?.let(::Path) | ||
} | ||
|
||
/** | ||
* Opens a native file picker, blocks the current thread until the dialog closes, and returns the [Path]s to | ||
* the picked files if any were picked. The opened file picker can pick multiple files. | ||
* | ||
* @see pickFile | ||
*/ | ||
@JvmOverloads | ||
fun pickMultipleFiles(title: String? = null, defaultPath: Path? = null, filter: FileFilter? = null) = | ||
openFileDialog(filter?.patterns) { filterBuffer -> | ||
val joinedPaths = TinyFileDialogs.tinyfd_openFileDialog( | ||
title, | ||
defaultPath?.pathString, | ||
filterBuffer, | ||
filter?.description, | ||
true | ||
) | ||
joinedPaths?.split('|')?.map(::Path) | ||
} | ||
|
||
/** | ||
* A file filter, to be passed to [pickFile] or [pickMultipleFiles]. | ||
*/ | ||
data class FileFilter(val patterns: List<String>, val description: String? = null) | ||
|
||
private inline fun <R : Any> openFileDialog( | ||
filterPatterns: List<String>?, | ||
nativeDialogFunction: (PointerBuffer?) -> R? | ||
): R? { | ||
MemoryStack.stackPush().use { stack -> | ||
val filterPatternBuffer = filterPatterns?.let { patterns -> | ||
val buffer = stack.mallocPointer(patterns.size) | ||
for (pattern in patterns) buffer.put(stack.UTF8(pattern)) | ||
buffer.flip() | ||
} | ||
if (mc.isRunning) mc.mouseHandler.releaseMouse() | ||
return nativeDialogFunction(filterPatternBuffer) | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/main/java/io/github/homchom/recode/ui/screen/DummyScreen.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,19 @@ | ||
package io.github.homchom.recode.ui.screen | ||
|
||
import io.github.homchom.recode.ui.text.toVanilla | ||
import net.kyori.adventure.text.Component | ||
import net.minecraft.client.gui.GuiGraphics | ||
import net.minecraft.client.gui.screens.Screen | ||
|
||
/** | ||
* A dummy [Screen] whose sole purpose is to release the mouse. This is, for example, useful in conjunction | ||
* with native IO functions like [io.github.homchom.recode.io.pickFile]. | ||
*/ | ||
class DummyScreen( | ||
title: Component, | ||
private val renderBackground: Boolean = true | ||
) : Screen(title.toVanilla()) { | ||
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, tickDelta: Float) { | ||
if (renderBackground) renderBackground(guiGraphics, mouseX, mouseY, tickDelta) | ||
} | ||
} |