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

Upgrade to bevy 0.10 #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ documentation = "https://docs.rs/bevy_iced"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

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

iced_wgpu = "0.7"
iced_native = "0.7"
iced_wgpu = { git = "https://github.com/kulkalkul/iced.git", branch = "self-usage", package = "iced_wgpu" }
iced_native = { git = "https://github.com/kulkalkul/iced.git", branch = "self-usage", package = "iced_native" }

[dev-dependencies]
bevy = "0.9"
bevy = "0.10"
rand = "0.8"
32 changes: 19 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ use std::sync::Mutex;
use crate::render::IcedNode;
use crate::render::ViewportResource;

use bevy_app::{App, Plugin};
use bevy_app::{App, IntoSystemAppConfig, Plugin};
use bevy_ecs::event::Event;
use bevy_ecs::prelude::EventWriter;
use bevy_ecs::prelude::{EventWriter, Query};
use bevy_ecs::system::{NonSendMut, Res, ResMut, Resource, SystemParam};
use bevy_render::render_graph::RenderGraph;
use bevy_render::renderer::RenderDevice;
use bevy_render::{RenderApp, RenderStage};
use bevy_render::{ExtractSchedule, RenderApp};
use bevy_utils::HashMap;
use bevy_window::Windows;
use bevy_window::Window;
use iced::{user_interface, Element, UserInterface};
pub use iced_native as iced;
use iced_native::{Debug, Size};
Expand Down Expand Up @@ -79,16 +79,23 @@ impl Plugin for IcedPlugin {
render_app
.insert_resource(default_viewport)
.insert_resource(iced_resource)
.add_system_to_stage(RenderStage::Extract, render::extract_iced_data);
.add_system(render::extract_iced_data.in_schedule(ExtractSchedule));
setup_pipeline(&mut render_app.world.get_resource_mut().unwrap());
}
}

#[derive(PartialEq)]
enum DidDraw {
No,
LastFrame,
Yes,
}

struct IcedProps {
renderer: iced_wgpu::Renderer,
debug: iced_native::Debug,
clipboard: iced_native::clipboard::Null,
did_draw: bool,
did_draw: DidDraw,
}

impl IcedProps {
Expand All @@ -109,7 +116,7 @@ impl IcedProps {
)),
debug: Debug::new(),
clipboard: iced_native::clipboard::Null,
did_draw: false,
did_draw: DidDraw::No,
}
}
}
Expand Down Expand Up @@ -139,8 +146,7 @@ fn setup_pipeline(graph: &mut RenderGraph) {
.add_node_edge(
bevy_render::main_graph::node::CAMERA_DRIVER,
render::ICED_PASS,
)
.unwrap();
);
}

#[doc(hidden)]
Expand Down Expand Up @@ -205,10 +211,10 @@ pub struct IcedContext<'w, 's, Message: Event> {
viewport: Res<'w, ViewportResource>,
props: Res<'w, IcedResource>,
settings: Res<'w, IcedSettings>,
windows: Res<'w, Windows>,
windows: Query<'w, 's, &'static Window>,
events: ResMut<'w, IcedEventQueue>,
cache_map: NonSendMut<'w, IcedCache>,
messages: EventWriter<'w, 's, Message>,
messages: EventWriter<'w, Message>,
}

impl<'w, 's, M: Event> IcedContext<'w, 's, M> {
Expand All @@ -225,7 +231,7 @@ impl<'w, 's, M: Event> IcedContext<'w, 's, M> {
let element = element.into();

let cursor_position = {
let window = self.windows.get_primary().unwrap();
let window = self.windows.single();
let cursor_position =
window
.cursor_position()
Expand Down Expand Up @@ -261,6 +267,6 @@ impl<'w, 's, M: Event> IcedContext<'w, 's, M> {

self.events.clear();
*cache_entry = Some(ui.into_cache());
*did_draw = true;
*did_draw = DidDraw::Yes;
}
}
30 changes: 18 additions & 12 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
system::{Commands, Res, Resource},
world::World,
};
use bevy_ecs::{system::{Commands, Res, Resource}, world::World};
use bevy_render::{
render_graph::{Node, NodeRunError, RenderGraphContext},
renderer::RenderContext,
view::ExtractedWindows,
Extract,
};
use bevy_window::Windows;
use iced_native::Size;
use iced_wgpu::{wgpu::util::StagingBelt, Viewport};
use std::sync::Mutex;
use bevy_ecs::prelude::Query;
use bevy_render::renderer::RenderDevice;
use bevy_window::Window;

use crate::{IcedProps, IcedResource, IcedSettings};
use crate::{DidDraw, IcedProps, IcedResource, IcedSettings};

pub const ICED_PASS: &'static str = "bevy_iced_pass";

#[derive(Resource, Deref, DerefMut, Clone)]
pub struct ViewportResource(pub Viewport);

pub(crate) fn update_viewport(
windows: Res<Windows>,
windows: Query<&Window>,
iced_settings: Res<IcedSettings>,
mut commands: Commands,
) {
let window = windows.get_primary().unwrap();
let window = windows.single();
let scale_factor = iced_settings.scale_factor.unwrap_or(window.scale_factor());
let viewport = Viewport::with_physical_size(
Size::new(window.physical_width(), window.physical_height()),
Expand Down Expand Up @@ -75,21 +74,29 @@ impl Node for IcedNode {
did_draw,
..
} = &mut *world.resource::<IcedResource>().lock().unwrap();
let render_device = world.resource::<RenderDevice>();

if !*did_draw {
if *did_draw == DidDraw::No {
return Ok(());
}

match *did_draw {
DidDraw::No => return Ok(()),
DidDraw::LastFrame => *did_draw = DidDraw::No,
DidDraw::Yes => *did_draw = DidDraw::LastFrame,
}

let view = extracted_window.swap_chain_texture.as_ref().unwrap();
let staging_belt = &mut *self.staging_belt.lock().unwrap();

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

renderer.with_primitives(|backend, primitives| {
backend.present(
device,
staging_belt,
&mut render_context.command_encoder,
render_context.command_encoder(),
view,
primitives,
viewport,
Expand All @@ -98,7 +105,6 @@ impl Node for IcedNode {
});

staging_belt.finish();
*did_draw = false;

Ok(())
}
Expand Down