Skip to content
Snippets Groups Projects
Unverified Commit 3ac13d54 authored by John's avatar John Committed by GitHub
Browse files

Merge pull request #168 from banana-studios/main

Fix app not updating if camera width/height changes
parents f109c07c 71d98f5c
No related branches found
No related tags found
No related merge requests found
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()
}
......@@ -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;
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment