diff --git a/examples/test_no_startup.rs b/examples/test_no_startup.rs new file mode 100644 index 0000000000000000000000000000000000000000..8567aa2669daad96676fb8ca5d250b786e778fb3 --- /dev/null +++ b/examples/test_no_startup.rs @@ -0,0 +1,49 @@ +use bevy::prelude::*; +use kayak_ui::prelude::{widgets::*, *}; + +#[derive(Default, Clone, Copy, PartialEq, Hash, Eq, Debug)] +pub enum GameState { + #[default] + First, + Second, +} + +fn first_sys(mut state: ResMut<State<GameState>>) { + state.overwrite_replace(GameState::Second).unwrap(); +} + +fn second_sys( + mut commands: Commands, + asset_server: Res<AssetServer>, + mut font_mapping: ResMut<FontMapping>, +) { + font_mapping.set_default(asset_server.load("roboto.kayak_font")); + + let mut widget_context = KayakRootContext::new(); + widget_context.add_plugin(KayakWidgetsContextPlugin); + let parent_id = None; + rsx! { + <KayakAppBundle> + <TextWidgetBundle + text={TextProps { + content: "Hello World".into(), + size: 20.0, + ..Default::default() + }} + /> + </KayakAppBundle> + } + + commands.spawn(UICameraBundle::new(widget_context)); +} + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugin(KayakContextPlugin) + .add_plugin(KayakWidgets) + .add_state(GameState::First) + .add_system_set(SystemSet::on_enter(GameState::First).with_system(first_sys)) + .add_system_set(SystemSet::on_enter(GameState::Second).with_system(second_sys)) + .run() +} diff --git a/src/widgets/app.rs b/src/widgets/app.rs index c234a92142ae98331e46de8ec5eb3b67e720b352..db871b596648cc32792d3d0623f98a9bd104240d 100644 --- a/src/widgets/app.rs +++ b/src/widgets/app.rs @@ -41,18 +41,33 @@ impl Default for KayakAppBundle { pub fn app_update( In((widget_context, entity, previous_props_entity)): In<(KayakWidgetContext, Entity, Entity)>, - windows: Res<Windows>, widget_param: WidgetParam<KayakApp, EmptyState>, + camera: Query<&Camera, With<CameraUIKayak>>, + windows: Res<Windows>, ) -> bool { - let primary_window = windows.get_primary().unwrap(); - let mut window_change = false; + if let Ok(app_style) = widget_param.style_query.get(entity) { - if app_style.width != StyleProp::Value(Units::Pixels(primary_window.width())) { - window_change = true; - } - if app_style.height != StyleProp::Value(Units::Pixels(primary_window.height())) { - window_change = true; + if let Some(camera_entity) = widget_context.camera_entity { + if let Ok(camera) = camera.get(camera_entity) { + if let Some(size) = camera.logical_viewport_size() { + if app_style.width != StyleProp::Value(Units::Pixels(size.x)) { + window_change = true; + } + if app_style.height != StyleProp::Value(Units::Pixels(size.y)) { + window_change = true; + } + } else { + let primary_window = windows.get_primary().unwrap(); + if app_style.width != StyleProp::Value(Units::Pixels(primary_window.width())) { + window_change = true; + } + if app_style.height != StyleProp::Value(Units::Pixels(primary_window.height())) + { + window_change = true; + } + } + } } } @@ -65,6 +80,8 @@ pub fn app_render( mut commands: Commands, mut query: Query<(&mut KStyle, &KChildren)>, camera: Query<&Camera, With<CameraUIKayak>>, + windows: Res<Windows>, + images: Res<Assets<Image>>, ) -> bool { let (mut width, mut height) = (0.0, 0.0); @@ -73,6 +90,17 @@ pub fn app_render( if let Some(size) = camera.logical_viewport_size() { width = size.x; height = size.y; + } else if let Some(viewport) = camera + .target + .get_render_target_info(&windows, &images) + .as_ref() + .map(|target_info| { + let scale = target_info.scale_factor; + (target_info.physical_size.as_dvec2() / scale).as_vec2() + }) + { + width = viewport.x; + height = viewport.y; } } }