mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-12 06:41:51 +09:00
mouse latch class
This commit is contained in:
@@ -21,7 +21,7 @@ object DefaultConfig {
|
||||
"screenheight" to TerrarumScreenSize.defaultH,
|
||||
"fullscreen" to false,
|
||||
"atlastexsize" to 2048,
|
||||
"savegamecomp" to "zstd",
|
||||
"savegamecomp" to "snappy",
|
||||
|
||||
"audio_buffer_size" to 512,
|
||||
"audio_dynamic_source_max" to 128,
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2024-01-10.
|
||||
*/
|
||||
class MouseLatch {
|
||||
}
|
||||
56
src/net/torvald/terrarum/ui/MouseLatch.kt
Normal file
56
src/net/torvald/terrarum/ui/MouseLatch.kt
Normal file
@@ -0,0 +1,56 @@
|
||||
package net.torvald.terrarum.ui
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import net.torvald.terrarum.App
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2024-01-10.
|
||||
*/
|
||||
class MouseLatch(val button: List<Int> = listOf(App.getConfigInt("config_mouseprimary"))) {
|
||||
|
||||
private val status = AtomicBoolean()
|
||||
|
||||
/**
|
||||
* Performs the `action` when the mouse-latch is unlatched and any of the specified buttons are down. After the `action` has run, the unlatch check will be done immediately.
|
||||
*
|
||||
* @param action what to do when the mouse is clicked. The latch will be latched right before the action runs.
|
||||
* Return `false` to unlatch the mouse-latch. Returning null WILL latch it.
|
||||
*/
|
||||
fun latchSelectively(action: () -> Boolean?) {
|
||||
if (isNotLatched() && button.any { Gdx.input.isButtonPressed(it) }) {
|
||||
status.set(true)
|
||||
status.set(action() ?: true)
|
||||
}
|
||||
|
||||
if (isLatched() && button.none { Gdx.input.isButtonPressed(it) }) {
|
||||
status.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the `action` when the mouse-latch is unlatched and any of the specified buttons are down. After the `action` has run, the unlatch check will be done immediately.
|
||||
*
|
||||
* @param action what to do when the mouse is clicked. The mouse-latch will be latched right before the action runs
|
||||
*/
|
||||
fun latch(action: () -> Unit) {
|
||||
if (isNotLatched() && button.any { Gdx.input.isButtonPressed(it) }) {
|
||||
status.set(true)
|
||||
action()
|
||||
}
|
||||
|
||||
if (isLatched() && button.none { Gdx.input.isButtonPressed(it) }) {
|
||||
status.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
fun unlatch() {
|
||||
if (button.none { Gdx.input.isButtonPressed(it) }) {
|
||||
status.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
fun isLatched() = status.get()
|
||||
fun isNotLatched() = !status.get()
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user