Skip to content

Commit

Permalink
add keybind config option, secondary keybinds, CancellAbleEventType, …
Browse files Browse the repository at this point in the history
…KeybindSet

Signed-off-by: J10a1n15 <45315647+j10a1n15@users.noreply.github.com>
  • Loading branch information
j10a1n15 committed Sep 24, 2024
1 parent 444d375 commit dd8b7d5
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 18 deletions.
106 changes: 105 additions & 1 deletion src/main/kotlin/gay/j10a1n15/sillygames/config/Config.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package gay.j10a1n15.sillygames.config

import gay.j10a1n15.sillygames.utils.essentials.InfoProperty
import gay.j10a1n15.sillygames.utils.essentials.info.InfoProperty
import gay.j10a1n15.sillygames.utils.essentials.keybind.KeybindProperty
import gg.essential.vigilance.Vigilant
import gg.essential.vigilance.data.Property
import gg.essential.vigilance.data.PropertyType
import java.io.File

object Config : Vigilant(File("./config/sillygames.toml")) {
/**
* --------------------------------------------------
* | About |
* --------------------------------------------------
*/
@Property(
type = PropertyType.CUSTOM,
name = "Information",
Expand All @@ -17,6 +23,104 @@ object Config : Vigilant(File("./config/sillygames.toml")) {
@Suppress("unused")
var informationAbout = ""


/**
* --------------------------------------------------
* | Keybinds |
* --------------------------------------------------
*/

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Information",
description = "Primary and Secondary Keybinds work if you have a silly game as fullscreen.\n" +
"Secondary Keybinds are for when you have a game in Picture in Picture mode.",
category = "Keybinds",
customPropertyInfo = InfoProperty::class,
)
@Suppress("unused")
var keybindInformation = ""

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Up",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Primary",
customPropertyInfo = KeybindProperty::class,
)
var keybindUp = 17

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Down",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Primary",
customPropertyInfo = KeybindProperty::class,
)
var keybindDown: Int = 31

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Left",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Primary",
customPropertyInfo = KeybindProperty::class,
)
var keybindLeft = 30

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Right",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Primary",
customPropertyInfo = KeybindProperty::class,
)
var keybindRight = 32

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Up",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Secondary",
customPropertyInfo = KeybindProperty::class,
)
var keybindUpSecondary: Int = 200

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Down",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Secondary",
customPropertyInfo = KeybindProperty::class,
)
var keybindDownSecondary = 208

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Left",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Secondary",
customPropertyInfo = KeybindProperty::class,
)
var keybindLeftSecondary = 203

@Property(
type = PropertyType.CUSTOM,
name = "Keybind Right",
description = "Keybinds for Silly Games.",
category = "Keybinds",
subcategory = "Secondary",
customPropertyInfo = KeybindProperty::class,
)
var keybindRightSecondary = 205

init {
initialize()

Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/gay/j10a1n15/sillygames/data/KeybindSet.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gay.j10a1n15.sillygames.data

import gay.j10a1n15.sillygames.SillyGames
import gay.j10a1n15.sillygames.utils.Vector2d

class KeybindSet(
val keybindUp: Int,
val keybindDown: Int,
val keybindLeft: Int,
val keybindRight: Int,
) {

fun getDirection(key: Int): Vector2d? {
return when (key) {
keybindUp -> Vector2d(0, -1)
keybindDown -> Vector2d(0, 1)
keybindLeft -> Vector2d(-1, 0)
keybindRight -> Vector2d(1, 0)
else -> null
}
}

companion object {
private val config get() = SillyGames.config

fun configPrimary() = KeybindSet(config.keybindUp, config.keybindDown, config.keybindLeft, config.keybindRight)

fun configSecondary() =
KeybindSet(config.keybindUpSecondary, config.keybindDownSecondary, config.keybindLeftSecondary, config.keybindRightSecondary)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ object EventHandler {
}

@SubscribeEvent
fun onKeyPressed(event: GuiScreenEvent.KeyboardInputEvent.Post) {
fun onKeyPressed(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
if (Keyboard.getEventKeyState() && Keyboard.getEventKey() != 0) {
val key = Keyboard.getEventKey()
Events.KEYBOARD_DOWN.post(key)
if (Events.KEYBOARD_DOWN.post(key)) {
event.isCanceled = true
}
}
}
}
20 changes: 19 additions & 1 deletion src/main/kotlin/gay/j10a1n15/sillygames/events/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Events {
val RENDER = EventType<UMatrixStack>()
val TICK = EventType<Unit>()
val KEYBOARD = EventType<Int>()
val KEYBOARD_DOWN = EventType<Int>()
val KEYBOARD_DOWN = CancellableEventType<Int>()
}

class EventType<T> {
Expand All @@ -20,5 +20,23 @@ class EventType<T> {
fun post(event: T) {
listeners.forEach { it(event) }
}
}

class CancellableEventType<T> {

private val listeners = mutableListOf<(T) -> Boolean>()

fun register(listener: (T) -> Boolean) {
listeners.add(listener)
}

fun post(event: T): Boolean {
var cancelled = false
listeners.forEach {
if (it(event)) {
cancelled = true
}
}
return cancelled
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/gay/j10a1n15/sillygames/games/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ abstract class Game {

open fun onKeyHeld(key: Int) {}

open fun onKeyPressed(key: Int) {}
open fun onKeyPressed(key: Int): Boolean {
return false
}

open val supportsPictureInPicture = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ object GameManager {
init {
Events.TICK.register { game?.onTick() }
Events.KEYBOARD.register { game?.onKeyHeld(it) }
Events.KEYBOARD_DOWN.register { game?.onKeyPressed(it) }
Events.KEYBOARD_DOWN.register { game?.onKeyPressed(it) ?: false }
}
}
11 changes: 3 additions & 8 deletions src/main/kotlin/gay/j10a1n15/sillygames/games/Snake.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gay.j10a1n15.sillygames.games

import gay.j10a1n15.sillygames.data.KeybindSet
import gay.j10a1n15.sillygames.rpc.RpcInfo
import gay.j10a1n15.sillygames.rpc.RpcProvider
import gay.j10a1n15.sillygames.utils.Vector2d
Expand All @@ -17,7 +18,6 @@ import gg.essential.elementa.dsl.constrain
import gg.essential.elementa.dsl.constraint
import gg.essential.elementa.dsl.percent
import gg.essential.elementa.dsl.plus
import gg.essential.universal.UKeyboard
import java.awt.Color

class Snake : Game(), RpcProvider {
Expand Down Expand Up @@ -68,13 +68,8 @@ class Snake : Game(), RpcProvider {
override fun onKeyHeld(key: Int) {
if (gameOver) return

val newDirection = when (key) {
UKeyboard.KEY_W -> Vector2d(0, -1)
UKeyboard.KEY_S -> Vector2d(0, 1)
UKeyboard.KEY_A -> Vector2d(-1, 0)
UKeyboard.KEY_D -> Vector2d(1, 0)
else -> return
}
// TODO: If in PIP, only use secondary keybinds
val newDirection = KeybindSet.configPrimary().getDirection(key) ?: KeybindSet.configSecondary().getDirection(key) ?: return

if (newDirection + direction == Vector2d(0, 0)) return
direction = newDirection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,13 @@ class Wordle : Game(), RpcProvider {
this.time = System.currentTimeMillis()
}

override fun onKeyPressed(key: Int) {
if (wordIndexInput.hasFocus()) return
override fun onKeyPressed(key: Int): Boolean {
if (wordIndexInput.hasFocus()) return false
when (key) {
UKeyboard.KEY_ENTER -> guess()
else -> state.keyPress(key)
}
return false
}

override fun onTick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gay.j10a1n15.sillygames.utils.essentials
package gay.j10a1n15.sillygames.utils.essentials.info

import gg.essential.vigilance.data.PropertyInfo
import gg.essential.vigilance.gui.settings.SettingComponent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gay.j10a1n15.sillygames.utils.essentials
package gay.j10a1n15.sillygames.utils.essentials.info

import gg.essential.vigilance.gui.settings.SettingComponent

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gay.j10a1n15.sillygames.utils.essentials.keybind

import gg.essential.vigilance.data.PropertyInfo
import gg.essential.vigilance.gui.settings.SettingComponent

class KeybindProperty : PropertyInfo() {
override fun createSettingComponent(initialValue: Any?): SettingComponent {
return KeybindPropertyComponent(initialValue as Int)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package gay.j10a1n15.sillygames.utils.essentials.keybind

import gay.j10a1n15.sillygames.events.Events
import gg.essential.elementa.components.UIBlock
import gg.essential.elementa.components.UIText
import gg.essential.elementa.constraints.CenterConstraint
import gg.essential.elementa.dsl.childOf
import gg.essential.elementa.dsl.constrain
import gg.essential.elementa.dsl.pixels
import gg.essential.elementa.dsl.toConstraint
import gg.essential.elementa.utils.withAlpha
import gg.essential.vigilance.gui.DataBackedSetting
import gg.essential.vigilance.gui.settings.SettingComponent
import org.lwjgl.input.Keyboard
import java.awt.Color

class KeybindPropertyComponent(initialValue: Int) : SettingComponent() {

private var listeningForKey = false
private var currentKey: Int = initialValue
private val keyDisplay: UIText
private val container: UIBlock

init {
container = UIBlock().constrain {
x = (DataBackedSetting.INNER_PADDING + 10f).pixels(alignOpposite = true)
y = CenterConstraint()
width = 100.pixels()
height = 20.pixels()
color = Color.BLACK.withAlpha(0.25f).toConstraint()
}.onMouseEnter {
startListeningForKey()
}.onMouseLeave {
stopListeningForKey()
} as UIBlock childOf this

keyDisplay = UIText(getKeyName(currentKey)).constrain {
x = CenterConstraint()
y = CenterConstraint()
} childOf container


Events.KEYBOARD_DOWN.register { onKeyTyped(it) }
}

private fun startListeningForKey() {
listeningForKey = true
keyDisplay.setText("Press a key...")
}

private fun onKeyTyped(keyCode: Int): Boolean {
if (listeningForKey && keyCode != Keyboard.KEY_NONE) {
setKeybind(keyCode)
stopListeningForKey()
return true
}
return false
}

private fun setKeybind(keyCode: Int) {
currentKey = keyCode
keyDisplay.setText(getKeyName(keyCode))
changeValue(currentKey)
}

private fun stopListeningForKey() {
listeningForKey = false
keyDisplay.setText(getKeyName(currentKey))
}

private fun getKeyName(keyCode: Int): String {
return Keyboard.getKeyName(keyCode) ?: "None"
}
}

0 comments on commit dd8b7d5

Please sign in to comment.