Skip to content

Commit

Permalink
Update for bevy 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzeh committed Jul 12, 2023
1 parent a53f49b commit c0bb6db
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 78 deletions.
27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ documentation = "https://docs.rs/bevy_iced"
touch = []

[dependencies]
bevy_app = "0.10"
bevy_derive = "0.10"
bevy_ecs = "0.10"
bevy_input = "0.10"
bevy_math = "0.10"
bevy_render = "0.10"
bevy_utils = "0.10"
bevy_window = "0.10"
bevy_app = "0.11"
bevy_derive = "0.11"
bevy_ecs = "0.11"
bevy_input = "0.11"
bevy_math = "0.11"
bevy_render = "0.11"
bevy_utils = "0.11"
bevy_window = "0.11"

iced_renderer = { git = "https://github.com/focustense/iced.git", branch = "wgpu-downgrade-0.15.1", features = ["wgpu"] }
iced_runtime = { git = "https://github.com/focustense/iced.git", branch = "wgpu-downgrade-0.15.1" }
iced_style = { git = "https://github.com/focustense/iced.git", branch = "wgpu-downgrade-0.15.1" }
iced_wgpu = { git = "https://github.com/focustense/iced.git", branch = "wgpu-downgrade-0.15.1" }
iced_renderer = { git = "https://github.com/iced-rs/iced", version = "0.1", features = ["wgpu"] }
iced_runtime = { git = "https://github.com/iced-rs/iced", version = "0.1"}
iced_style = {git = "https://github.com/iced-rs/iced", version = "0.8"}
iced_wgpu = {git = "https://github.com/iced-rs/iced", version = "0.10"}
iced_widget = {git = "https://github.com/iced-rs/iced", version = "0.1.0"}

[dev-dependencies]
bevy = "0.10"
bevy = "0.11"
rand = "0.8"
9 changes: 5 additions & 4 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use bevy::prelude::*;
use bevy_iced::iced::widget::text;
use bevy_iced::iced::core::widget::text;
use bevy_iced::{IcedContext, IcedPlugin};

#[derive(Event)]
pub enum UiMessage {}

pub fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(IcedPlugin)
.add_plugins(IcedPlugin)
.add_event::<UiMessage>()
.add_system(ui_system)
.add_systems(Update, ui_system)
.run();
}

fn ui_system(time: Res<Time>, mut ctx: IcedContext<UiMessage>) {
ctx.display(text(format!(
ctx.display(text::Text::new(format!(
"Hello Iced! Running for {:.2} seconds.",
time.elapsed_seconds()
)));
Expand Down
31 changes: 15 additions & 16 deletions examples/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use bevy::{
prelude::*,
};
use bevy_iced::{
iced::widget::{slider, text, text_input, Button, Column, Row},
widget::{slider, text, text_input, Button, Column, Row},
IcedContext, IcedPlugin, IcedSettings,
};
use rand::random as rng;

#[derive(Clone)]
#[derive(Clone, Event)]
enum UiMessage {
BoxRequested,
Scale(f32),
Expand All @@ -34,9 +34,9 @@ pub fn main() {
}),
..Default::default()
}))
.add_plugin(IcedPlugin)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugins(IcedPlugin)
.add_plugins(FrameTimeDiagnosticsPlugin::default())
.add_plugins(LogDiagnosticsPlugin::default())
.add_event::<UiMessage>()
.insert_resource(UiActive(true))
.insert_resource(UiData {
Expand All @@ -45,17 +45,16 @@ pub fn main() {
})
.insert_resource(IcedSettings {
scale_factor: None,
theme: bevy_iced::iced_wgpu::Theme::Light,
style: bevy_iced::iced::renderer::Style {
text_color: bevy_iced::iced::Color::from_rgb(0.0, 1.0, 1.0),
theme: bevy_iced::style::Theme::Light,
style: bevy_iced::iced::core::renderer::Style {
text_color: bevy_iced::iced::core::Color::from_rgb(0.0, 1.0, 1.0),
},
})
.add_startup_system(build_program)
.add_system(tick)
.add_system(box_system)
.add_system(update_scale_factor)
.add_system(toggle_ui)
.add_system(ui_system)
.add_systems(Startup, build_program)
.add_systems(
Update,
(tick, box_system, update_scale_factor, toggle_ui, ui_system),
)
.run();
}

Expand Down Expand Up @@ -137,7 +136,7 @@ fn ui_system(

let row = Row::new()
.spacing(10)
.align_items(iced_native::Alignment::Center)
.align_items(bevy_iced::iced::core::Alignment::Center)
.push(Button::new(text("Request box")).on_press(UiMessage::BoxRequested))
.push(text(format!(
"{} boxes (amplitude: {})",
Expand All @@ -146,7 +145,7 @@ fn ui_system(
)));
let edit = text_input("", &data.text).on_input(UiMessage::Text);
let column = Column::new()
.align_items(iced_native::Alignment::Center)
.align_items(bevy_iced::iced::core::Alignment::Center)
.spacing(10)
.push(edit)
.push(slider(0.0..=100.0, data.scale, UiMessage::Scale))
Expand Down
9 changes: 5 additions & 4 deletions examples/toggle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bevy::prelude::*;
use bevy_iced::iced::widget::text;
use bevy_iced::widget::text;
use bevy_iced::{IcedContext, IcedPlugin};
use bevy_input::keyboard::KeyboardInput;
use bevy_input::ButtonState;

#[derive(Event)]
pub enum UiMessage {}

#[derive(Resource, PartialEq, Eq)]
Expand All @@ -12,11 +13,11 @@ pub struct UiActive(bool);
pub fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(IcedPlugin)
.add_plugins(IcedPlugin)
.add_event::<UiMessage>()
.insert_resource(UiActive(true))
.add_system(toggle_system)
.add_system(ui_system.run_if(resource_equals(UiActive(true))))
.add_systems(Update, toggle_system)
.add_systems(Update, ui_system.run_if(resource_equals(UiActive(true))))
.run();
}

Expand Down
20 changes: 10 additions & 10 deletions src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ pub fn key_code(virtual_keycode: BevyKeyCode) -> IcedKeyCode {
BevyKeyCode::Grave => IcedKeyCode::Grave,
BevyKeyCode::Kana => IcedKeyCode::Kana,
BevyKeyCode::Kanji => IcedKeyCode::Kanji,
BevyKeyCode::LAlt => IcedKeyCode::LAlt,
BevyKeyCode::LBracket => IcedKeyCode::LBracket,
BevyKeyCode::LControl => IcedKeyCode::LControl,
BevyKeyCode::LShift => IcedKeyCode::LShift,
BevyKeyCode::LWin => IcedKeyCode::LWin,
BevyKeyCode::AltLeft => IcedKeyCode::LAlt,
BevyKeyCode::BracketLeft => IcedKeyCode::LBracket,
BevyKeyCode::ControlLeft => IcedKeyCode::LControl,
BevyKeyCode::ShiftLeft => IcedKeyCode::LShift,
BevyKeyCode::SuperLeft => IcedKeyCode::LWin,
BevyKeyCode::Mail => IcedKeyCode::Mail,
BevyKeyCode::MediaSelect => IcedKeyCode::MediaSelect,
BevyKeyCode::MediaStop => IcedKeyCode::MediaStop,
Expand All @@ -148,11 +148,11 @@ pub fn key_code(virtual_keycode: BevyKeyCode) -> IcedKeyCode {
BevyKeyCode::PlayPause => IcedKeyCode::PlayPause,
BevyKeyCode::Power => IcedKeyCode::Power,
BevyKeyCode::PrevTrack => IcedKeyCode::PrevTrack,
BevyKeyCode::RAlt => IcedKeyCode::RAlt,
BevyKeyCode::RBracket => IcedKeyCode::RBracket,
BevyKeyCode::RControl => IcedKeyCode::RControl,
BevyKeyCode::RShift => IcedKeyCode::RShift,
BevyKeyCode::RWin => IcedKeyCode::RWin,
BevyKeyCode::AltRight => IcedKeyCode::RAlt,
BevyKeyCode::BracketRight => IcedKeyCode::RBracket,
BevyKeyCode::ControlRight => IcedKeyCode::RControl,
BevyKeyCode::ShiftRight => IcedKeyCode::RShift,
BevyKeyCode::SuperRight => IcedKeyCode::RWin,
BevyKeyCode::Semicolon => IcedKeyCode::Semicolon,
BevyKeyCode::Slash => IcedKeyCode::Slash,
BevyKeyCode::Sleep => IcedKeyCode::Sleep,
Expand Down
38 changes: 22 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::sync::{
use crate::render::{ICED_PASS, IcedNode};
use crate::render::ViewportResource;

use bevy_app::{App, IntoSystemAppConfig, Plugin};
use bevy_app::{App, Plugin, Update};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
event::Event,
Expand All @@ -59,7 +59,6 @@ use bevy_render::{
use bevy_utils::HashMap;
use bevy_window::{PrimaryWindow, Window};
use iced::{user_interface::Cache as UiCache, UserInterface};
use iced_renderer::Backend;
pub use iced_runtime as iced;
use iced_runtime::{
core::{clipboard, Element, Event as IcedEvent, Point, Size},
Expand All @@ -69,6 +68,8 @@ use iced_runtime::{
use iced_runtime::core::touch::Event as TouchEvent;
use iced_style::Theme;
pub use iced_wgpu;
pub use iced_style as style;
pub use iced_widget as widget;
use iced_wgpu::{
core::{
renderer::Style,
Expand All @@ -91,24 +92,28 @@ pub struct IcedPlugin;

impl Plugin for IcedPlugin {
fn build(&self, app: &mut App) {
let default_viewport = Viewport::with_physical_size(Size::new(1600, 900), 1.0);
let default_viewport = ViewportResource(default_viewport);
let iced_resource: IcedResource = IcedProps::new(app).into();

app.add_system(systems::process_input)
.add_system(render::update_viewport)
app.add_systems(Update, (systems::process_input, render::update_viewport))
.insert_resource(DidDraw::default())
.insert_resource(iced_resource.clone())
.insert_resource(IcedSettings::default())
.insert_non_send_resource(IcedCache::default())
.insert_resource(IcedEventQueue::default())
.insert_resource(IcedEventQueue::default());
}

fn finish(&self, app: &mut App) {
let default_viewport = Viewport::with_physical_size(Size::new(1600, 900), 1.0);
let default_viewport = ViewportResource(default_viewport);
let iced_resource: IcedResource = IcedProps::new(app).into();

app
.insert_resource(iced_resource.clone())
.insert_resource(default_viewport.clone());

let render_app = app.sub_app_mut(RenderApp);
render_app
.insert_resource(default_viewport)
.insert_resource(iced_resource)
.add_system(render::extract_iced_data.in_schedule(ExtractSchedule));
.add_systems(ExtractSchedule, render::extract_iced_data);
setup_pipeline(&mut render_app.world.get_resource_mut().unwrap());
}
}
Expand All @@ -134,7 +139,7 @@ impl IcedProps {
let format = TextureFormat::Bgra8UnormSrgb;

Self {
renderer: Renderer::new(Backend::Wgpu(WgpuBackend::new(
renderer: Renderer::Wgpu(iced_wgpu::Renderer::new(WgpuBackend::new(
device,
queue,
Default::default(),
Expand Down Expand Up @@ -258,12 +263,12 @@ impl<'w, 's, M: Event> IcedContext<'w, 's, M> {

window
.cursor_position()
.map(|Vec2 { x, y }| Point {
.map(|Vec2 { x, y }| iced_wgpu::core::mouse::Cursor::Available(Point {
x: x * bounds.width / window.width(),
y: (window.height() - y) * bounds.height / window.height(),
})
y: y * bounds.height / window.height(),
}))
.or_else(|| process_touch_input(self))
.unwrap_or(Point::ORIGIN)
.unwrap_or(iced_wgpu::core::mouse::Cursor::Unavailable)
};

let mut messages = Vec::<M>::new();
Expand Down Expand Up @@ -327,6 +332,7 @@ fn process_touch_input<M: Event>(context: &IcedContext<M>) -> Option<Point> {
}

#[cfg(not(feature = "touch"))]
fn process_touch_input<M: Event>(_: &IcedContext<M>) -> Option<Point> {
fn process_touch_input<M: Event>(_: &IcedContext<M>) -> Option<iced_wgpu::core::mouse::Cursor> {

None
}
5 changes: 2 additions & 3 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use bevy_render::{
};
use bevy_window::Window;
use iced_runtime::core::Size;
use iced_renderer::Backend;
use iced_wgpu::graphics::Viewport;

use crate::{DidDraw, IcedProps, IcedResource, IcedSettings};
Expand Down Expand Up @@ -81,13 +80,13 @@ impl Node for IcedNode {
return Ok(());
}

let view = extracted_window.swap_chain_texture.as_ref().unwrap();
let view = extracted_window.swap_chain_texture_view.as_ref().unwrap();

let viewport = world.resource::<ViewportResource>();
let device = render_device.wgpu_device();

let iced_renderer::Renderer::Wgpu(renderer) = renderer else { return Ok(()); };
renderer.with_primitives(|backend, primitives| {
let Backend::Wgpu(ref mut backend) = backend else { return; };
backend.present(
device,
queue,
Expand Down
24 changes: 12 additions & 12 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ pub struct InputEvents<'w, 's> {

fn compute_modifiers(input_map: &Input<KeyCode>) -> keyboard::Modifiers {
let mut modifiers = keyboard::Modifiers::default();
if input_map.any_pressed([KeyCode::LControl, KeyCode::RControl]) {
if input_map.any_pressed([KeyCode::ControlLeft, KeyCode::ControlRight]) {
modifiers |= keyboard::Modifiers::CTRL;
}
if input_map.any_pressed([KeyCode::LShift, KeyCode::RShift]) {
if input_map.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]) {
modifiers |= keyboard::Modifiers::SHIFT;
}
if input_map.any_pressed([KeyCode::LAlt, KeyCode::RAlt]) {
if input_map.any_pressed([KeyCode::AltLeft, KeyCode::AltRight]) {
modifiers |= keyboard::Modifiers::ALT;
}
if input_map.any_pressed([KeyCode::LWin, KeyCode::RWin]) {
if input_map.any_pressed([KeyCode::SuperLeft, KeyCode::SuperRight]) {
modifiers |= keyboard::Modifiers::LOGO;
}
modifiers
Expand Down Expand Up @@ -93,14 +93,14 @@ pub fn process_input(
use keyboard::Event::*;
let modifiers = compute_modifiers(&input_map);
let event = match code {
KeyCode::LControl
| KeyCode::RControl
| KeyCode::LShift
| KeyCode::RShift
| KeyCode::LAlt
| KeyCode::RAlt
| KeyCode::LWin
| KeyCode::RWin => ModifiersChanged(modifiers),
KeyCode::ControlLeft
| KeyCode::ControlRight
| KeyCode::ShiftLeft
| KeyCode::ShiftRight
| KeyCode::AltLeft
| KeyCode::AltRight
| KeyCode::SuperLeft
| KeyCode::SuperRight => ModifiersChanged(modifiers),
code => {
let key_code = conversions::key_code(code);
if ev.state.is_pressed() {
Expand Down

0 comments on commit c0bb6db

Please sign in to comment.