mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 12:21:52 +09:00
mouse latch class
This commit is contained in:
@@ -16,6 +16,7 @@ import net.torvald.terrarum.gameworld.fmod
|
||||
import net.torvald.terrarum.modulebasegame.MusicContainer
|
||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||
import net.torvald.terrarum.ui.BasicDebugInfoWindow
|
||||
import net.torvald.terrarum.ui.MouseLatch
|
||||
import net.torvald.terrarum.ui.Toolkit
|
||||
import net.torvald.terrarum.ui.UICanvas
|
||||
import net.torvald.terrarum.utils.JsonFetcher
|
||||
@@ -178,6 +179,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
// printdbg(this, "setMusicName $str; strLen = $nameLengthOld -> $nameLength; overflown=$nameOverflown; transitionTime=$TRANSITION_LENGTH")
|
||||
}
|
||||
|
||||
private val mouseLatch = MouseLatch()
|
||||
|
||||
private var mouseOnButton: Int? = null
|
||||
private var mouseOnList: Int? = null
|
||||
|
||||
@@ -311,16 +314,18 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
|
||||
|
||||
// make button work
|
||||
if (!playControlButtonLatched && mouseOnButton != null && Terrarum.mouseDown) {
|
||||
playControlButtonLatched = true
|
||||
mouseLatch.latch {
|
||||
if (mouseOnButton != null) {
|
||||
when (mouseOnButton) {
|
||||
0 -> { // album
|
||||
albumsListCacheIsStale = true // clicking the button will refresh the album list, even if the current view is the album list
|
||||
albumsListCacheIsStale =
|
||||
true // clicking the button will refresh the album list, even if the current view is the album list
|
||||
|
||||
if (mode < MODE_SHOW_LIST) {
|
||||
if (!transitionOngoing) {
|
||||
transitionRequest = MODE_SHOW_LIST
|
||||
currentListMode = 0 // no list transition anim is needed this time, just set the variable
|
||||
currentListMode =
|
||||
0 // no list transition anim is needed this time, just set the variable
|
||||
resetAlbumlistScroll()
|
||||
}
|
||||
}
|
||||
@@ -333,6 +338,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
transitionRequest = AudioMixer.musicTrack.isPlaying.toInt() * MODE_MOUSE_UP
|
||||
}
|
||||
}
|
||||
|
||||
1 -> { // prev
|
||||
// prev song
|
||||
if (mode < MODE_SHOW_LIST) {
|
||||
@@ -343,7 +349,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
// prev page in the playlist
|
||||
else if (listViewPanelScroll == 1f) {
|
||||
val scrollMax = ((currentlySelectedAlbum?.length ?: 0).toFloat() / PLAYLIST_LINES).ceilToInt() * PLAYLIST_LINES
|
||||
val scrollMax = ((currentlySelectedAlbum?.length
|
||||
?: 0).toFloat() / PLAYLIST_LINES).ceilToInt() * PLAYLIST_LINES
|
||||
playlistScroll = (playlistScroll - PLAYLIST_LINES) fmod scrollMax
|
||||
}
|
||||
// prev page in the albumlist
|
||||
@@ -352,6 +359,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
albumlistScroll = (albumlistScroll - PLAYLIST_LINES) fmod scrollMax
|
||||
}
|
||||
}
|
||||
|
||||
2 -> { // stop
|
||||
if (mode < MODE_SHOW_LIST) { // disable stop button entirely on MODE_SHOW_LIST
|
||||
if (AudioMixer.musicTrack.isPlaying) {
|
||||
@@ -366,6 +374,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3 -> { // next
|
||||
// next song
|
||||
if (mode < MODE_SHOW_LIST) {
|
||||
@@ -375,7 +384,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
// next page in the playlist
|
||||
else if (listViewPanelScroll == 1f) {
|
||||
val scrollMax = ((currentlySelectedAlbum?.length ?: 0).toFloat() / PLAYLIST_LINES).ceilToInt() * PLAYLIST_LINES
|
||||
val scrollMax = ((currentlySelectedAlbum?.length
|
||||
?: 0).toFloat() / PLAYLIST_LINES).ceilToInt() * PLAYLIST_LINES
|
||||
playlistScroll = (playlistScroll + PLAYLIST_LINES) fmod scrollMax
|
||||
}
|
||||
// next page in the albumlist
|
||||
@@ -384,11 +394,13 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
albumlistScroll = (albumlistScroll + PLAYLIST_LINES) fmod scrollMax
|
||||
}
|
||||
}
|
||||
|
||||
4 -> { // playlist
|
||||
if (mode < MODE_SHOW_LIST) {
|
||||
if (!transitionOngoing) {
|
||||
transitionRequest = MODE_SHOW_LIST
|
||||
currentListMode = 1 // no list transition anim is needed this time, just set the variable
|
||||
currentListMode =
|
||||
1 // no list transition anim is needed this time, just set the variable
|
||||
resetPlaylistScroll()
|
||||
}
|
||||
}
|
||||
@@ -404,8 +416,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
}
|
||||
// make playlist clicking work
|
||||
else if (listViewPanelScroll == 1f && !playControlButtonLatched && mouseOnList != null && Terrarum.mouseDown) {
|
||||
playControlButtonLatched = true
|
||||
else if (listViewPanelScroll == 1f && mouseOnList != null) {
|
||||
val index = playlistScroll + mouseOnList!!
|
||||
val list = songsInGovernor
|
||||
if (index < list.size) {
|
||||
@@ -422,8 +433,7 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
}
|
||||
// make album list clicking work
|
||||
else if (listViewPanelScroll == 0f && !playControlButtonLatched && mouseOnList != null && Terrarum.mouseDown) {
|
||||
playControlButtonLatched = true
|
||||
else if (listViewPanelScroll == 0f && mouseOnList != null) {
|
||||
val index = albumlistScroll + mouseOnList!!
|
||||
val list = albumsList//.map { albumPropCache[it] }
|
||||
|
||||
@@ -441,21 +451,15 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() {
|
||||
}
|
||||
}
|
||||
// click on the music title to return to MODE_MOUSE_UP
|
||||
else if (mouseUp && relativeMouseY.toFloat() in _posY + height - capsuleHeight .. _posY + height && Terrarum.mouseDown && !transitionOngoing && mode > MODE_MOUSE_UP) {
|
||||
playControlButtonLatched = true
|
||||
else if (mouseUp && relativeMouseY.toFloat() in _posY + height - capsuleHeight.._posY + height && !transitionOngoing && mode > MODE_MOUSE_UP) {
|
||||
transitionRequest = MODE_MOUSE_UP
|
||||
}
|
||||
// unlatch the click latch
|
||||
else if (!Terrarum.mouseDown) {
|
||||
playControlButtonLatched = false
|
||||
}
|
||||
|
||||
|
||||
// printdbg(this, "mode = $mode; req = $transitionRequest")
|
||||
}
|
||||
|
||||
private var playControlButtonLatched = false
|
||||
|
||||
private fun resetAlbumlistScroll() {
|
||||
val currentlyPlaying = albumsList.indexOfFirst { it.canonicalPath.replace('\\', '/') == ingame.musicGovernor.playlistSource }
|
||||
if (currentlyPlaying >= 0) {
|
||||
|
||||
@@ -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