Skip to content

Commit

Permalink
Fixed a bug where Onkeypressed in scene was not working (#368)
Browse files Browse the repository at this point in the history
* changed the documentation of the Key Event onKeyTyped

* reworked the onKeyEvent EventHandler that they now work on the primary Scene that is shown

* Added a consume event to the key events so that they don't get triggered multiple times with elements in the scene

* Updated CHANGELOG.md

* Applied spotless.

---------

Co-authored-by: Vadym.Kosin <Vadym.Kosin@tu-dortmund.de>
Co-authored-by: Amin Bouzerda <amin.bouzerda@tu-dortmund.de>
Co-authored-by: Amin Bouzerda <45982681+heaterscar@users.noreply.github.com>
  • Loading branch information
4 people committed May 17, 2023
1 parent ec87e64 commit 4ad460a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
# Changelog
All notable changes to this project will be documented in this file.

## [0.8] - 22.05.2022
## [0.8] - 22.05.2023

### Added
- `CameraPane` component.
- `HexagonView` component.
- `HexagonGrid` with two coordinate systems. axial and offset coordinates.

### Fixed
- `onKeyPressed` listener not working on scenes.

## [0.7.3] - 31.08.2022

### Added
Expand Down
8 changes: 8 additions & 0 deletions bgw-gui/src/main/kotlin/tools/aqua/bgw/builder/Frontend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,16 @@ internal class Frontend : Application() {
children.clear()
children.add(backgroundPane)
children.addAll(activePanes)
this.setOnKeyTyped { activePanes.lastOrNull()?.onKeyTyped?.handle(it) }
this.setOnKeyReleased { activePanes.lastOrNull()?.onKeyReleased?.handle(it) }
this.setOnKeyPressed { activePanes.lastOrNull()?.onKeyPressed?.handle(it) }
}

primaryStage?.scene.apply {
this?.setOnKeyTyped { scenePane.onKeyTyped?.handle(it) }
this?.setOnKeyReleased { scenePane.onKeyReleased?.handle(it) }
this?.setOnKeyPressed { scenePane.onKeyPressed?.handle(it) }
}
primaryStage?.scene?.root = scenePane

primaryStage?.forceRefresh()
Expand Down
10 changes: 6 additions & 4 deletions bgw-gui/src/main/kotlin/tools/aqua/bgw/builder/SceneBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package tools.aqua.bgw.builder

import javafx.scene.input.KeyEvent
import javafx.scene.input.MouseEvent
import javafx.scene.layout.Pane
import javafx.scene.layout.StackPane
Expand Down Expand Up @@ -94,17 +93,20 @@ object SceneBuilder {
prefHeight = scene.height
prefWidth = scene.width

addEventFilter(KeyEvent.KEY_TYPED) {
setOnKeyTyped {
if (scene !is BoardGameScene || !scene.internalLockedProperty.value)
scene.onKeyTyped?.invoke(it.toKeyEvent())
it.consume()
}
addEventFilter(KeyEvent.KEY_PRESSED) {
setOnKeyPressed {
if (scene !is BoardGameScene || !scene.internalLockedProperty.value)
scene.onKeyPressed?.invoke(it.toKeyEvent())
it.consume()
}
addEventFilter(KeyEvent.KEY_RELEASED) {
setOnKeyReleased {
if (scene !is BoardGameScene || !scene.internalLockedProperty.value)
scene.onKeyReleased?.invoke(it.toKeyEvent())
it.consume()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ internal constructor(posX: Number, posY: Number, width: Number, height: Number,
var onKeyReleased: ((KeyEvent) -> Unit)? = null

/**
* Gets invoked with a [KeyEvent] whenever a key is typed while this [ComponentView] has focus.
* Gets invoked after [onKeyPressed].
* Gets invoked with a [KeyEvent] whenever a Character is typed while this [ComponentView] has
* focus. Gets invoked after [onKeyPressed].
*
* @see KeyEvent
* @see onKeyPressed
Expand Down
3 changes: 2 additions & 1 deletion bgw-gui/src/main/kotlin/tools/aqua/bgw/core/Scene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ sealed class Scene<T : ComponentView>(width: Number, height: Number, background:
var onKeyReleased: ((KeyEvent) -> Unit)? = null

/**
* Gets invoked with a [KeyEvent] whenever a key is typed. Gets invoked after [onKeyPressed].
* Gets invoked with a [KeyEvent] whenever a Character is typed. Gets invoked after [onKeyPressed]
* .
*
* @see KeyEvent
* @see onKeyPressed
Expand Down

0 comments on commit 4ad460a

Please sign in to comment.