//! Basic quad and text drawing inside a `Canvas`. use bevy::diagnostic::FrameTimeDiagnosticsPlugin; use bevy::diagnostic::LogDiagnosticsPlugin; use bevy::{ color::palettes::css::*, log::LogPlugin, math::Rect, prelude::*, sprite::Anchor, window::PrimaryWindow, }; use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_keith::*; fn main() { App::new() .add_plugins(FrameTimeDiagnosticsPlugin::default()) .add_plugins(LogDiagnosticsPlugin::default()) // Helper to exit with ESC key //.add_systems(Update, bevy::window::close_on_esc) // Default plugins .add_plugins( DefaultPlugins .set(LogPlugin { level: bevy::log::Level::WARN, filter: "quad=trace,bevy_keith=warn,bevy=info".to_string(), ..default() }) .set(WindowPlugin { primary_window: Some(Window { title: "bevy_keith - quad".to_string(), ..default() }), ..default() }), ) .insert_resource(ClearColor(DARK_GRAY.into())) .add_plugins(KeithPlugin) .add_plugins(WorldInspectorPlugin::default()) .add_systems(Startup, setup) .add_systems(Update, run) .run(); } #[derive(Component)] struct MyRes { font: Handle, image: Handle, } fn setup( mut commands: Commands, asset_server: Res, text_pipeline: Res, ) { let font = asset_server.load("FiraSans-Regular.ttf"); let image = asset_server.load("uvdev.png"); let mut canvas = Canvas::new(Rect { min: Vec2::splat(-400.), max: Vec2::splat(100.), }); canvas.background_color = Some(BEIGE.into()); commands .spawn(Camera2dBundle::default()) .insert(canvas) .insert(MyRes { font: font.clone(), image: image.clone(), }); // Display the text pipeline's glyph atlas as a debug visualization commands .spawn(SpriteBundle { sprite: Sprite { color: Color::srgba_u8(0, 0, 0, 128), custom_size: Some(Vec2::splat(512.)), ..default() }, transform: Transform::from_xyz(400., 0., 0.), ..default() }) .with_children(|p| { p.spawn(SpriteBundle { texture: text_pipeline.atlas_texture_handle.clone(), sprite: Sprite { custom_size: Some(Vec2::splat(512.)), ..default() }, ..default() }); }); // commands.spawn_bundle(Text2dBundle { // text: Text::from_section( // "Hello World!".to_string(), // TextStyle { // font, // font_size: 24.0, // color: Color::BLACK, // }, // ) // .with_alignment(TextAlignment { // vertical: VerticalAlign::Bottom, // horizontal: HorizontalAlign::Left, // }), // transform: Transform::from_translation(Vec3::new(0., -16., 0.)), // ..default() // }); // commands.spawn_bundle(SpriteBundle { // sprite: Sprite { // custom_size: Some(Vec2::splat(64.)), // ..default() // }, // texture: image, // transform: Transform::from_translation(Vec3::new(0., -128., 0.)), // ..default() // }); } fn draw_button( ctx: &mut RenderContext, rect: Rect, text: &str, font: Handle, cursor_pos: Vec2, ) { // Background let (brush, border_brush) = if rect.contains(cursor_pos) { let border_brush = ctx.solid_brush(Color::srgb(0.6, 0.6, 1.0)); (ctx.solid_brush(Color::srgb(0.7, 0.7, 0.7)), border_brush) } else { let border_brush = ctx.solid_brush(Color::srgb(0.8, 0.8, 0.9)); (ctx.solid_brush(Color::srgb(0.6, 0.6, 0.6)), border_brush) }; ctx.fill(rect, &brush).border(&border_brush, 2.); // Text let text = ctx .new_layout(text.to_owned()) .color(Color::srgb(0.2, 0.2, 0.2)) .font(font) .font_size(16.) .bounds(rect.size()) .alignment(JustifyText::Center) .build(); ctx.draw_text(text, (rect.min + rect.max) / 2.); } fn run( time: Res