aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/keybindings/SavedKeyBinding.kt
blob: f73eb49e87bf277ad9f80892cb9f8cf10b3a5d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package moe.nea.firmament.keybindings

import kotlinx.serialization.Serializable
import org.lwjgl.glfw.GLFW

@Serializable
data class SavedKeyBinding(
    val keyCode: Int,
    val shift: Boolean = false,
    val ctrl: Boolean = false,
    val alt: Boolean = false,
) : IKeyBinding {
    constructor(keyCode: Int, mods: Triple<Boolean, Boolean, Boolean>) : this(
        keyCode,
        mods.first && keyCode != GLFW.GLFW_KEY_LEFT_SHIFT && keyCode != GLFW.GLFW_KEY_RIGHT_SHIFT,
        mods.second && keyCode != GLFW.GLFW_KEY_LEFT_CONTROL && keyCode != GLFW.GLFW_KEY_RIGHT_CONTROL,
        mods.third && keyCode != GLFW.GLFW_KEY_LEFT_ALT && keyCode != GLFW.GLFW_KEY_RIGHT_ALT,
    )

    constructor(keyCode: Int, mods: Int) : this(keyCode, getMods(mods))

    companion object {
        fun getMods(modifiers: Int): Triple<Boolean, Boolean, Boolean> {
            return Triple(
                modifiers and GLFW.GLFW_MOD_SHIFT != 0,
                modifiers and GLFW.GLFW_MOD_CONTROL != 0,
                modifiers and GLFW.GLFW_MOD_ALT != 0
            )
        }
    }

    override fun matches(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
        return keyCode == this.keyCode && getMods(modifiers) == Triple(shift, ctrl, alt)
    }

}