Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bevy egui keyboard #91

Merged
merged 19 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"apps/social/common",
"apps/social/networking",
"apps/social/server",
"crates/bevy_egui_keyboard",
"crates/egui-picking",
"crates/picking-xr",
"crates/replicate/client",
Expand Down Expand Up @@ -64,6 +65,7 @@ tracing-subscriber = "0.3.18"
url = "2.5.0"
uuid = "1.7.0"
wtransport = "0.1.11"
bevy_egui_keyboard.path = "crates/bevy_egui_keyboard"

[workspace.dependencies.derive_more]
version = "0.99"
Expand Down
24 changes: 24 additions & 0 deletions crates/bevy_egui_keyboard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "bevy_egui_keyboard"
version.workspace = true
license.workspace = true
repository.workspace = true
edition.workspace = true
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy.workspace = true
bevy_egui.workspace = true

[dev-dependencies]
egui-picking.workspace = true
bevy_mod_picking = { workspace = true, default-features = false, features = [
"backend_raycast",
"backend_bevy_ui",
"backend_sprite",
"selection",
"highlight",
] }

163 changes: 163 additions & 0 deletions crates/bevy_egui_keyboard/examples/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
use bevy::input::keyboard::KeyboardInput;
use bevy::window::PrimaryWindow;
use bevy::{
prelude::*,
render::render_resource::{Extent3d, TextureUsages},
};
use bevy_egui::{egui, EguiContexts, EguiPlugin, EguiRenderToTexture};
use bevy_egui_keyboard::{
draw_keyboard, EguiKeyboard, KeyValue, ModifierState, OnScreenKeyboard,
};
use bevy_mod_picking::DefaultPickingPlugins;
use egui_picking::{PickabelEguiPlugin, WorldSpaceUI};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPickingPlugins)
.add_plugins(EguiPlugin)
.add_plugins(PickabelEguiPlugin)
.add_plugins(EguiKeyboard)
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.,
})
.add_systems(Startup, setup_worldspace)
// Systems that create Egui widgets should be run during the `CoreSet::Update` set,
// or after the `EguiSet::BeginFrame` system (which belongs to the `CoreSet::PreUpdate` set).
.add_systems(Update, (update_screenspace, update_worldspace))
.run()
}
fn setup_worldspace(
mut images: ResMut<Assets<Image>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut commands: Commands,
) {
let output_texture = images.add({
let size = Extent3d {
width: 512,
height: 512,
depth_or_array_layers: 1,
};
let mut output_texture = Image {
// You should use `0` so that the pixels are transparent.
data: vec![0; (size.width * size.height * 4) as usize],
..default()
};
output_texture.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
output_texture.texture_descriptor.size = size;
output_texture
});
commands.spawn((
PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y).mesh().size(1.0, 1.0)),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
base_color_texture: Some(Handle::clone(&output_texture)),
// Remove this if you want it to use the world's lighting.
unlit: true,

..default()
}),
transform: Transform::looking_at(
Transform::IDENTITY,
Vec3::Y,
Vec3::splat(1.5),
),
..default()
},
WorldSpaceUI::new(output_texture, 1.0, 1.0),
));
let output_texture = images.add({
let size = Extent3d {
width: 512,
height: 512,
depth_or_array_layers: 1,
};
let mut output_texture = Image {
// You should use `0` so that the pixels are transparent.
data: vec![0; (size.width * size.height * 4) as usize],
..default()
};
output_texture.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
output_texture.texture_descriptor.size = size;
output_texture
});
commands.spawn((
PbrBundle {
mesh: meshes.add(Plane3d::new(Vec3::Y).mesh().size(1.0, 1.0)),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
base_color_texture: Some(Handle::clone(&output_texture)),
// Remove this if you want it to use the world's lighting.
unlit: true,

..default()
}),
transform: Transform::looking_at(
Transform::IDENTITY,
Vec3::Y,
Vec3::splat(1.5),
)
.with_translation(Vec3::new(0.0, 0.0, 1.0)),
..default()
},
WorldSpaceUI::new(output_texture, 1.0, 1.0),
KeyboardBoi,
));
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(1.5, 1.5, 1.5)
.looking_at(Vec3::new(0., 0., 0.), Vec3::Y),
..default()
});
}

fn update_screenspace(mut contexts: EguiContexts, mut buf: Local<String>) {
egui::Window::new("Screenspace UI").show(contexts.ctx_mut(), |ui| {
ui.label("I'm rendering to screenspace!");
let buf: &mut String = &mut buf;
ui.text_edit_singleline(buf);
});
}

#[derive(Component)]
pub struct KeyboardBoi;

fn update_worldspace(
mut contexts: Query<
&mut bevy_egui::EguiContext,
(With<EguiRenderToTexture>, Without<KeyboardBoi>),
>,
mut contexts2: Query<&mut bevy_egui::EguiContext, With<KeyboardBoi>>,
window: Query<Entity, With<PrimaryWindow>>,
_just_pressed: Local<Option<KeyCode>>,
mut event_writer: EventWriter<KeyboardInput>,
mut char_writer: EventWriter<ReceivedCharacter>,
mut previously_pressed: Local<Option<KeyValue>>,
mut buf: Local<String>,
mut modifier_state: Local<ModifierState>,
mut on_screen_keyboard: ResMut<OnScreenKeyboard>,
) {
for mut ctx in contexts2.iter_mut() {
egui::Window::new("other_ui").show(ctx.get_mut(), |ui| {
draw_keyboard(
ui,
window.get_single().unwrap(),
&mut previously_pressed,
&mut event_writer,
&mut char_writer,
&mut modifier_state,
&mut on_screen_keyboard,
);
});
}

for mut ctx in contexts.iter_mut() {
egui::Window::new("Worldspace UI").show(ctx.get_mut(), |ui| {
ui.label("I'm rendering to a texture in worldspace!");
let buf: &mut String = &mut buf;
ui.text_edit_singleline(buf);
});
}
}
Loading
Loading