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
235 changes: 132 additions & 103 deletions examples/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ fn setup(mut commands: Commands, window: Query<&Window, With<PrimaryWindow>>) {

commands.spawn(Camera2dBundle::default());

let mut login_id = None;
let mut password_id = None;
let mut submit_id = None;
let mut output_id = None;

commands
.spawn(NodeBundle {
style: Style {
Expand All @@ -34,119 +39,143 @@ fn setup(mut commands: Commands, window: Query<&Window, With<PrimaryWindow>>) {
..default()
})
.with_children(|root| {
root.spawn(CosmicEditBundle {
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
},
..default()
})
.insert(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
login_id = Some(
root.spawn(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Username".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert(UsernameTag);

root.spawn(CosmicEditBundle {
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
})
.id(),
);

password_id = Some(
root.spawn(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
},
..default()
})
.insert(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
})
.id(),
);

submit_id = Some(
root.spawn(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(150.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
border: UiRect::all(Val::Px(3.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
border_color: Color::DARK_GREEN.into(),

..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Password".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert(PasswordTag)
.insert(PasswordInput::default());

root.spawn(CosmicEditBundle {
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
font_size: 25.0,
line_height: 25.0,
scale_factor: window.scale_factor() as f32,
})
.insert(SubmitButton)
.id(),
);

output_id = Some(
root.spawn(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(100.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
},
attrs: CosmicAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::WHITE)),
)),
text_setter: CosmicText::OneStyle("Submit".into()),
fill_color: FillColor(Color::GREEN),
})
.id(),
);
});

commands
.spawn(CosmicEditBundle {
target: CosmicTarget(login_id),
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
})
.insert(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(150.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
border: UiRect::all(Val::Px(3.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
border_color: Color::DARK_GREEN.into(),
},
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Username".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert(UsernameTag);

commands
.spawn(CosmicEditBundle {
target: CosmicTarget(password_id),
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
})
.insert(SubmitButton)
.insert(ReadOnly);
},
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Password".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert(PasswordTag)
.insert(PasswordInput::default());

root.spawn(CosmicEditBundle {
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
},
commands
.spawn(CosmicEditBundle {
target: CosmicTarget(submit_id),
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
font_size: 25.0,
line_height: 25.0,
scale_factor: window.scale_factor() as f32,
..default()
})
.insert(ButtonBundle {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(100.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
},
attrs: CosmicAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::WHITE)),
)),
text_setter: CosmicText::OneStyle("Submit".into()),
fill_color: FillColor(Color::GREEN),
..default()
})
.insert(ReadOnly);

commands
.spawn(CosmicEditBundle {
target: CosmicTarget(output_id),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Output".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert((ReadOnly, DisplayTag));
});
},
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Output".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert((ReadOnly, DisplayTag));
}

fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
Expand Down
47 changes: 32 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod render;

use std::{collections::VecDeque, path::PathBuf};

use bevy::{prelude::*, render::texture::DEFAULT_IMAGE_HANDLE, transform::TransformSystem};
use bevy::{prelude::*, transform::TransformSystem};
pub use cosmic_text::{
Action, Attrs, AttrsOwned, Color as CosmicColor, Cursor, Edit, Family, Style as FontStyle,
Weight as FontWeight,
Expand Down Expand Up @@ -220,16 +220,10 @@ impl Default for PasswordInput {
}
}

#[derive(Component)]
pub struct CosmicCanvas(pub Handle<Image>);

impl Default for CosmicCanvas {
fn default() -> Self {
CosmicCanvas(DEFAULT_IMAGE_HANDLE.typed())
}
}
#[derive(Component, Default)]
pub struct CosmicTarget(pub Option<Entity>);

#[derive(Bundle, Default)]
#[derive(Bundle)]
pub struct CosmicEditBundle {
// cosmic bits
pub fill_color: FillColor,
Expand All @@ -241,7 +235,33 @@ pub struct CosmicEditBundle {
pub max_chars: CosmicMaxChars,
pub text_setter: CosmicText,
pub mode: CosmicMode,
pub canvas: CosmicCanvas,
pub sprite_bundle: SpriteBundle,
pub target: CosmicTarget,
}

impl Default for CosmicEditBundle {
fn default() -> Self {
CosmicEditBundle {
fill_color: Default::default(),
text_position: Default::default(),
metrics: Default::default(),
attrs: Default::default(),
background_image: Default::default(),
max_lines: Default::default(),
max_chars: Default::default(),
text_setter: Default::default(),
mode: Default::default(),
sprite_bundle: SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::ONE * 128.0),
..default()
},
visibility: Visibility::Hidden,
..default()
},
target: Default::default(),
}
}
}

#[derive(Bundle)]
Expand Down Expand Up @@ -305,17 +325,14 @@ impl Plugin for CosmicEditPlugin {
freeze_cursor_blink,
hide_inactive_or_readonly_cursor,
clear_inactive_selection,
render::update_handle_ui,
render::update_handle_sprite,
);

app.add_systems(
First,
(
set_initial_scale,
(cosmic_editor_builder, on_scale_factor_change).after(set_initial_scale),
render::cosmic_ui_to_canvas,
render::cosmic_sprite_to_canvas,
render::swap_target_handle,
),
)
.add_systems(
Expand Down
Loading
Loading