Skip to content

Commit

Permalink
Added scene event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
smilefx committed Nov 26, 2024
1 parent ae0f51e commit 73c55db
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
6 changes: 3 additions & 3 deletions bgw-gui/src/jsMain/kotlin/tools/aqua/bgw/elements/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,12 @@ val App = FC<AppProps> { props ->
}

fun globalKeyDown(e: KeyboardEvent<*>) {
JCEFEventDispatcher.dispatchEvent(e.toKeyEventData("global", KeyEventAction.PRESS))
JCEFEventDispatcher.dispatchEvent(e.toKeyEventData("global", KeyEventAction.TYPE))
JCEFEventDispatcher.dispatchGlobalEvent(e.toKeyEventData("global", KeyEventAction.PRESS))
JCEFEventDispatcher.dispatchGlobalEvent(e.toKeyEventData("global", KeyEventAction.TYPE))
}

fun globalKeyUp(e: KeyboardEvent<*>) {
JCEFEventDispatcher.dispatchEvent(e.toKeyEventData("global", KeyEventAction.RELEASE))
JCEFEventDispatcher.dispatchGlobalEvent(e.toKeyEventData("global", KeyEventAction.RELEASE))
}

useEffectWithCleanup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import jsonMapper
object JCEFEventDispatcher : EventDispatcher {
init { initialize() }

fun dispatchGlobalEvent(event: KeyEventData) {
val json = jsonMapper.encodeToString(event)
try {
window.asDynamic().bgwSceneQuery(Base64.encode(json))
} catch (e: Throwable) {
println("Error while dispatching event: $e")
}
}

override fun dispatchEvent(event: AnimationFinishedEventData) {
val json = jsonMapper.encodeToString(event)
try {
Expand All @@ -31,5 +40,6 @@ object JCEFEventDispatcher : EventDispatcher {
private fun initialize() {
js("window.bgwQuery = function(request) { window.cefQuery({request: request, persistent: false, onSuccess: function (response) {}, onFailure: function (error_code, error_message) {}}) }")
js("window.bgwAnimationQuery = function(request) { window.cefAnimationQuery({request: request, persistent: false, onSuccess: function (response) {}, onFailure: function (error_code, error_message) {}}) }")
js("window.bgwSceneQuery = function(request) { window.cefSceneQuery({request: request, persistent: false, onSuccess: function (response) {}, onFailure: function (error_code, error_message) {}}) }")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ class JCEFApplication : Application {
}
}

@OptIn(DelicateCoroutinesApi::class)
override fun stop() {
frame?.dispose()
CefApp.getInstance().dispose()
println("Stopping JCEF Application")

GlobalScope.launch {
frame?.dispose()
exitProcess(0)
}
}

override fun clearAllEventListeners() {
Expand Down Expand Up @@ -234,6 +239,7 @@ class MainFrame(

var msgRouter : CefMessageRouter? = null
var animationMsgRouter : CefMessageRouter? = null
var globalEventMsgRouter : CefMessageRouter? = null
var client : CefClient

var dialogMap : MutableMap<CefBrowser, DialogData> = mutableMapOf()
Expand Down Expand Up @@ -288,6 +294,44 @@ class MainFrame(
}
msgRouter?.addHandler(myHandler, true)


val globalEventConfig = CefMessageRouterConfig()
globalEventConfig.jsQueryFunction = "cefSceneQuery"
globalEventConfig.jsCancelFunction = "cefSceneQueryCancel"
globalEventMsgRouter = CefMessageRouter.create(globalEventConfig)
client.addMessageRouter(globalEventMsgRouter)
val globalHandler = object : CefMessageRouterHandlerAdapter() {
override fun onQuery(browser: CefBrowser, frame: CefFrame, query_id: Long, request: String, persistent: Boolean, callback: CefQueryCallback): Boolean {
val json = Base64.decode(request)
val eventData = jsonMapper.decodeFromString<KeyEventData>(json)

try {
val keyEvent = KeyEvent(eventData.keyCode, eventData.character, eventData.isControlDown, eventData.isShiftDown, eventData.isAltDown)
val menuScene = Frontend.menuScene
val boardGameScene = Frontend.boardGameScene

when(eventData.action) {
KeyEventAction.PRESS -> {
menuScene?.onKeyPressed?.invoke(keyEvent)
boardGameScene?.onKeyPressed?.invoke(keyEvent)
}
KeyEventAction.RELEASE -> {
menuScene?.onKeyReleased?.invoke(keyEvent)
boardGameScene?.onKeyReleased?.invoke(keyEvent)
}
KeyEventAction.TYPE -> {
menuScene?.onKeyTyped?.invoke(keyEvent)
boardGameScene?.onKeyTyped?.invoke(keyEvent)
}
}
return true
} catch(e : Exception) { }

return false
}
}
globalEventMsgRouter?.addHandler(globalHandler, true)

/* Animation Message Router */
val animationConfig = CefMessageRouterConfig()
animationConfig.jsQueryFunction = "cefAnimationQuery"
Expand Down
2 changes: 1 addition & 1 deletion bgw-gui/src/jvmMain/kotlin/tools/aqua/bgw/core/Frontend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ internal class Frontend {

/** Stops the application. */
internal fun exit() {
TODO("Not yet implemented")
applicationEngine.stop()
}

fun loadFont(path : String, fontName : String, weight : Font.FontWeight): Boolean {
Expand Down

0 comments on commit 73c55db

Please sign in to comment.