#bevy #ipc #gamedev #game

macro bevy_flurx_ipc_macro

provide ipc-command macro for bevy_flurx_ipc

1 unstable release

0.1.0-alpha1 Jun 2, 2024

#2454 in Game dev

21 downloads per month
Used in 4 crates (via bevy_flurx_ipc)

MIT/Apache

11KB

bevy_flurx_wry

[!CAUTION] This crate is in the early stages of development and is subject to disruptive changes.

Purpose

The purpose of this crate is integrate bevy and wry using bevy_flurx.

In addition to that, I would like to take advantage of bevy's extensibility and discover bevy's potential to transcend the framework of existing game engines.

Platform Support

The operation has been confirmed on Windows and MacOS.

Ubuntu is currently not supported.

Usage

There are two ways to create a webview:

Converts an existing window into a webview window.

simple

examples/simple.rs

use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_flurx_wry::prelude::*;

fn spawn_webview(
    mut commands: Commands,
    window: Query<Entity, With<PrimaryWindow>>,
) {
    // Converts the `Window` attached the entity into a webview window. 
    commands.entity(window.single()).insert(
        WryWebViewBundle {
            uri: WebviewUri::new("https://bevyengine.org/"),
            ..default()
        }
    );
}

Create a webview as child inside a window.

child_view examples/child_view.rs

use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_flurx_wry::prelude::*;

fn spawn_webview(
    mut commands: Commands,
    window: Query<Entity, With<PrimaryWindow>>,
) {
    commands.spawn((
        WryWebViewBundle {
            ..default()
        },
        AsChildBundle {
            // Here, create a webview as child inside a given window.
            parent: ParentWindow(window.single()),
            bounds: Bounds {
                position: Vec2::new(100., 100.),
                size: Vec2::new(500., 500.),
                min_size: Vec2::new(100., 100.),
            },
            ..default()
        },
    ));
}

Ipc

IpcEvent

You can listen events from the webview and, conversely, emit events to the webview.

Webview(javascript) -> bevy

examples/event_listen.rs

javascript

// you can use any type.
const event = {
    message: "message"
};
window.__FLURX__.emit("event_id", event);

rust

use bevy::prelude::*;
use bevy_flurx_wry::prelude::*;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct MessageFromWebview {
    message: String,
}

fn read_webview_message(
    mut er: EventReader<IpcEvent<MessageFromWebview>>
) {
    for e in er.read() {
        println!("webview message: {}", e.payload.message);
    }
}

bevy -> Webview(javascript)

examples/event_emit.rs

javascript

window.__FLURX__.listen("event_id", ({message}) => {
    console.log(message);
});

rust

use bevy::prelude::*;
use bevy_flurx_wry::prelude::*;
use serde_json::json;

fn emit_event(
    mut views: Query<&mut EventEmitter>
) {
    for mut emitter in views.iter_mut() {
        emitter.emit("event_id", &serde_json::json!({
            "message" : "hello world!"
        }));
    }
}

IpcCommand

IpcEvent can't receive the output value from the other side. In this case, IpcCommand can be used.

IpcComamnd can be divided into two command patterns: action-command, task-command

Please check examples/ipc_command.rs for details.

Todo

  • Enhance security
  • Bug fix
  • Add apis
  • Support Linux(X11)
  • Support Linux(Wayland)

ChangeLog

Please see here.

Compatible Bevy versions

bevy_flurx_wry bevy_flurx bevy
0.1.0-alpha1 0.5.2 0.13.2

License

This crate is licensed under the MIT License or the Apache License 2.0.

Dependencies

~3MB
~63K SLoC