-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add keybind config option, secondary keybinds, CancellAbleEventType, …
…KeybindSet Signed-off-by: J10a1n15 <45315647+j10a1n15@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
255 additions
and
18 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
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/gay/j10a1n15/sillygames/data/KeybindSet.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,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) | ||
} | ||
} |
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
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
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
2 changes: 1 addition & 1 deletion
2
...llygames/utils/essentials/InfoProperty.kt → ...mes/utils/essentials/info/InfoProperty.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
2 changes: 1 addition & 1 deletion
2
...utils/essentials/InfoPropertyComponent.kt → .../essentials/info/InfoPropertyComponent.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
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/gay/j10a1n15/sillygames/utils/essentials/keybind/KeybindProperty.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,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) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/kotlin/gay/j10a1n15/sillygames/utils/essentials/keybind/KeybindPropertyComponent.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,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" | ||
} | ||
} |