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

Simplify main render system #106

Merged
merged 13 commits into from
Nov 2, 2023
24 changes: 11 additions & 13 deletions examples/basic_sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,24 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {

let scale_factor = primary_window.scale_factor() as f32;

let cosmic_edit = (
CosmicEditBundle {
metrics: CosmicMetrics {
font_size: 14.,
line_height: 18.,
scale_factor,
},
text_position: CosmicTextPosition::Center,
attrs: CosmicAttrs(AttrsOwned::new(attrs)),
text_setter: CosmicText::OneStyle("😀😀😀 x => y".to_string()),
..default()
let cosmic_edit = (CosmicEditBundle {
metrics: CosmicMetrics {
font_size: 14.,
line_height: 18.,
scale_factor,
},
SpriteBundle {
text_position: CosmicTextPosition::Center,
attrs: CosmicAttrs(AttrsOwned::new(attrs)),
text_setter: CosmicText::OneStyle("😀😀😀 x => y".to_string()),
sprite_bundle: SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(primary_window.width(), primary_window.height())),
..default()
},
..default()
},
);
..default()
},);

let cosmic_edit = commands.spawn(cosmic_edit).id();

Expand Down
39 changes: 23 additions & 16 deletions examples/basic_ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*, window::PrimaryWindow};
use bevy_cosmic_edit::{
AttrsOwned, CosmicAttrs, CosmicEditBundle, CosmicEditPlugin, CosmicEditor, CosmicFontConfig,
CosmicMetrics, CosmicText, CosmicTextPosition, Focus,
CosmicMetrics, CosmicSource, CosmicText, CosmicTextPosition, Focus,
};

fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
Expand All @@ -20,8 +20,8 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {

let scale_factor = primary_window.scale_factor() as f32;

let cosmic_edit = (
CosmicEditBundle {
let cosmic_edit = commands
.spawn(CosmicEditBundle {
metrics: CosmicMetrics {
font_size: 14.,
line_height: 18.,
Expand All @@ -31,21 +31,28 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
attrs: CosmicAttrs(AttrsOwned::new(attrs)),
text_setter: CosmicText::OneStyle("😀😀😀 x => y".to_string()),
..default()
},
// Use buttonbundle for layout
ButtonBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
})
.id();

commands
.spawn(
// Use buttonbundle for layout
// Includes Interaction and UiImage which are used by the plugin.
ButtonBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
..default()
},
// Needs to be set to prevent a bug where nothing is displayed
background_color: Color::WHITE.into(),
..default()
},
// Needs to be set to prevent a bug where nothing is displayed
background_color: Color::WHITE.into(),
..default()
},
);

let cosmic_edit = commands.spawn(cosmic_edit).id();
)
// point editor at this entity.
// Plugin looks for UiImage and sets it's
// texture to the editor's rendered image
.insert(CosmicSource(cosmic_edit));

commands.insert_resource(Focus(Some(cosmic_edit)));
}
Expand Down
52 changes: 28 additions & 24 deletions examples/bevy_api_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_cosmic_edit::*;
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());

// spawn a new CosmicEditBundle
// UI editor
let ui_editor = commands
.spawn(CosmicEditBundle {
attrs: CosmicAttrs(AttrsOwned::new(
Expand All @@ -13,30 +13,33 @@ fn setup(mut commands: Commands) {
max_lines: CosmicMaxLines(1),
..default()
})
.insert(ButtonBundle {
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Placeholder".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.id();

commands
.spawn(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
left: Val::Px(100.),
top: Val::Px(100.),
// needs to be set to prevent a bug where nothing is displayed
..default()
},
// needs to be set to prevent a bug where nothing is displayed
background_color: BackgroundColor(Color::WHITE),
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Placeholder".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.id();
.insert(CosmicSource(ui_editor));

commands.spawn((
CosmicEditBundle { ..default() },
SpriteBundle {
// Sprite editor
commands.spawn((CosmicEditBundle {
sprite_bundle: SpriteBundle {
// Sets size of text box
sprite: Sprite {
custom_size: Some(Vec2::new(300., 100.)),
Expand All @@ -46,7 +49,8 @@ fn setup(mut commands: Commands) {
transform: Transform::from_xyz(0., 100., 0.),
..default()
},
));
..default()
},));

commands.insert_resource(Focus(Some(ui_editor)));
}
Expand All @@ -63,16 +67,13 @@ fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
fn change_active_editor_ui(
mut commands: Commands,
mut interaction_query: Query<
(&Interaction, Entity),
(
Changed<Interaction>,
(With<CosmicEditor>, Without<ReadOnly>),
),
(&Interaction, &CosmicSource),
(Changed<Interaction>, Without<ReadOnly>),
>,
) {
for (interaction, entity) in interaction_query.iter_mut() {
for (interaction, source) in interaction_query.iter_mut() {
if let Interaction::Pressed = interaction {
commands.insert_resource(Focus(Some(entity)));
commands.insert_resource(Focus(Some(source.0)));
}
}
}
Expand All @@ -82,16 +83,19 @@ fn change_active_editor_sprite(
windows: Query<&Window, With<PrimaryWindow>>,
buttons: Res<Input<MouseButton>>,
mut cosmic_edit_query: Query<
(&mut Sprite, &GlobalTransform, Entity),
(&mut Sprite, &GlobalTransform, &Visibility, Entity),
(With<CosmicEditor>, Without<ReadOnly>),
>,
camera_q: Query<(&Camera, &GlobalTransform)>,
) {
let window = windows.single();
let (camera, camera_transform) = camera_q.single();
if buttons.just_pressed(MouseButton::Left) {
for (sprite, node_transform, entity) in &mut cosmic_edit_query.iter_mut() {
let size = sprite.custom_size.unwrap_or(Vec2::new(1., 1.));
for (sprite, node_transform, visibility, entity) in &mut cosmic_edit_query.iter_mut() {
if visibility == Visibility::Hidden {
continue;
}
let size = sprite.custom_size.unwrap_or(Vec2::ONE);
let x_min = node_transform.affine().translation.x - size.x / 2.;
let y_min = node_transform.affine().translation.y - size.y / 2.;
let x_max = node_transform.affine().translation.x + size.x / 2.;
Expand Down
36 changes: 27 additions & 9 deletions examples/every_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,33 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
max_lines: CosmicMaxLines(1),
text_setter: CosmicText::OneStyle("BANANA IS THE CODEWORD!".into()),
mode: CosmicMode::Wrap,
canvas: Default::default(),
// CosmicEdit draws to this spritebundle
sprite_bundle: SpriteBundle {
sprite: Sprite {
// when using another target like a UI element, this is overridden
custom_size: Some(Vec2::ONE * 128.0),
..default()
},
// this is the default behaviour for targeting UI elements.
// If wanting a sprite, define your own SpriteBundle and
// leave the visibility on. See examples/basic_sprite.rs
visibility: Visibility::Hidden,
..default()
},
// Computed fields
padding: Default::default(),
widget_size: Default::default(),
})
.insert(ButtonBundle {
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Placeholder".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(CosmicColor::rgb(88, 88, 88)),
)),
})
.id();

commands
.spawn(ButtonBundle {
border_color: Color::LIME_GREEN.into(),
style: Style {
// Size and position of text box
Expand All @@ -42,13 +66,7 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
background_color: Color::WHITE.into(),
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Placeholder".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(CosmicColor::rgb(88, 88, 88)),
)),
})
.id();
.insert(CosmicSource(editor));

commands.insert_resource(Focus(Some(editor)));

Expand Down
73 changes: 36 additions & 37 deletions examples/font_per_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
)],
];

let cosmic_edit_1 = (
CosmicEditBundle {
let cosmic_edit_1 = commands
.spawn(CosmicEditBundle {
text_position: bevy_cosmic_edit::CosmicTextPosition::Center,
attrs: CosmicAttrs(AttrsOwned::new(attrs)),
metrics: CosmicMetrics {
Expand All @@ -218,24 +218,15 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
},
text_setter: CosmicText::MultiStyle(lines),
..default()
},
ButtonBundle {
style: Style {
width: Val::Percent(50.),
height: Val::Percent(100.),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
},
);
})
.id();

let mut attrs_2 = Attrs::new();
attrs_2 = attrs_2.family(Family::Name("Times New Roman"));
attrs_2.color_opt = Some(bevy_color_to_cosmic(Color::PURPLE));

let cosmic_edit_2 = (
CosmicEditBundle {
let cosmic_edit_2 = commands
.spawn(CosmicEditBundle {
attrs: CosmicAttrs(AttrsOwned::new(attrs_2)),
metrics: CosmicMetrics {
font_size: 28.,
Expand All @@ -245,27 +236,38 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
text_position: CosmicTextPosition::Center,
text_setter: CosmicText::OneStyle("Widget 2.\nClick on me =>".to_string()),
..default()
},
ButtonBundle {
background_color: BackgroundColor(Color::WHITE.with_a(0.8)),
style: Style {
width: Val::Percent(50.),
height: Val::Percent(100.),
..default()
},
..default()
},
);
})
.id();

let mut id = None;
// Spawn the CosmicEditUiBundles as children of root
commands.entity(root).with_children(|parent| {
id = Some(parent.spawn(cosmic_edit_1).id());
parent.spawn(cosmic_edit_2);
parent
.spawn(ButtonBundle {
style: Style {
width: Val::Percent(50.),
height: Val::Percent(100.),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
})
.insert(CosmicSource(cosmic_edit_1));

parent
.spawn(ButtonBundle {
background_color: BackgroundColor(Color::WHITE.with_a(0.8)),
style: Style {
width: Val::Percent(50.),
height: Val::Percent(100.),
..default()
},
..default()
})
.insert(CosmicSource(cosmic_edit_2));
});

// Set active editor
commands.insert_resource(Focus(id));
commands.insert_resource(Focus(Some(cosmic_edit_1)));
}

fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
Expand All @@ -280,16 +282,13 @@ fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
fn change_active_editor_ui(
mut commands: Commands,
mut interaction_query: Query<
(&Interaction, Entity),
(
Changed<Interaction>,
(With<CosmicEditor>, Without<ReadOnly>),
),
(&Interaction, &CosmicSource),
(Changed<Interaction>, Without<ReadOnly>),
>,
) {
for (interaction, entity) in interaction_query.iter_mut() {
for (interaction, source) in interaction_query.iter_mut() {
if let Interaction::Pressed = interaction {
commands.insert_resource(Focus(Some(entity)));
commands.insert_resource(Focus(Some(source.0)));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/image_background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
background_image: CosmicBackground(Some(bg_image_handle)),
..default()
})
.insert(ButtonBundle {
.id();

commands
.spawn(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
Expand All @@ -26,7 +29,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
background_color: Color::WHITE.into(),
..default()
})
.id();
.insert(CosmicSource(editor));

commands.insert_resource(Focus(Some(editor)));
}

Expand Down
Loading