diff --git a/examples/bevy_api_testing.rs b/examples/bevy_api_testing.rs index 75f7d4d2562f37d688262b322956817f72b38b16..0b98f531e8da785167ebae92b822a4aab1ce7c80 100644 --- a/examples/bevy_api_testing.rs +++ b/examples/bevy_api_testing.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{prelude::*, window::PrimaryWindow}; use bevy_cosmic_edit::*; fn setup(mut commands: Commands) { @@ -36,7 +36,7 @@ fn setup(mut commands: Commands) { commands.insert_resource(Focus(Some(sprite_editor))); } -pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { +fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { cosmic_text::Color::rgba( (color.r() * 255.) as u8, (color.g() * 255.) as u8, @@ -45,6 +45,53 @@ pub 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>), + ), + >, +) { + for (interaction, entity) in interaction_query.iter_mut() { + if let Interaction::Pressed = interaction { + commands.insert_resource(Focus(Some(entity))); + } + } +} + +fn change_active_editor_sprite( + mut commands: Commands, + windows: Query<&Window, With<PrimaryWindow>>, + buttons: Res<Input<MouseButton>>, + mut cosmic_edit_query: Query< + (&mut Sprite, &GlobalTransform, 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.)); + 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.; + let y_max = node_transform.affine().translation.y + size.y / 2.; + if let Some(pos) = window.cursor_position() { + if let Some(pos) = camera.viewport_to_world_2d(camera_transform, pos) { + if x_min < pos.x && pos.x < x_max && y_min < pos.y && pos.y < y_max { + commands.insert_resource(Focus(Some(entity))) + }; + } + }; + } + } +} + fn main() { App::new() .add_plugins(DefaultPlugins) diff --git a/examples/every_option.rs b/examples/every_option.rs index c522c701bf48a8e4e595f54d954c7684e1d34ccf..f09282f3dc5547a52b62d7a9f427ff3bac5f7a2b 100644 --- a/examples/every_option.rs +++ b/examples/every_option.rs @@ -92,8 +92,6 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugins(CosmicEditPlugin::default()) .add_systems(Startup, setup) - .add_systems(Update, change_active_editor_ui) - .add_systems(Update, change_active_editor_sprite) .add_systems(Update, text_swapper) .run(); } diff --git a/examples/font_per_widget.rs b/examples/font_per_widget.rs index 7f7a336e77bf7c3054a2ab8182b361695a711b3d..e8871b99f63603cc2d2c6fe5679292b982533616 100644 --- a/examples/font_per_widget.rs +++ b/examples/font_per_widget.rs @@ -258,7 +258,7 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) { commands.insert_resource(Focus(id)); } -pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { +fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { cosmic_text::Color::rgba( (color.r() * 255.) as u8, (color.g() * 255.) as u8, @@ -267,6 +267,23 @@ pub 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>), + ), + >, +) { + for (interaction, entity) in interaction_query.iter_mut() { + if let Interaction::Pressed = interaction { + commands.insert_resource(Focus(Some(entity))); + } + } +} + fn main() { let font_config = CosmicFontConfig { fonts_dir_path: None, @@ -279,6 +296,5 @@ fn main() { .add_plugins(CosmicEditPlugin { font_config }) .add_systems(Startup, setup) .add_systems(Update, change_active_editor_ui) - .add_systems(Update, change_active_editor_sprite) .run(); } diff --git a/examples/image_background.rs b/examples/image_background.rs index e3d2f87fa8d4382c06846abeb1e80eaa80ba4404..cdc9d7d7e9de22dc87cd0a785e34ad108d51de14 100644 --- a/examples/image_background.rs +++ b/examples/image_background.rs @@ -40,7 +40,5 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugins(CosmicEditPlugin::default()) .add_systems(Startup, setup) - .add_systems(Update, change_active_editor_ui) - .add_systems(Update, change_active_editor_sprite) .run(); } diff --git a/examples/multiple_sprites.rs b/examples/multiple_sprites.rs index d93018b9c87b0c9e60a4d8ce362ca98a819d8d8a..f088f8089ded2c88c47247b364db82cb391f9f0f 100644 --- a/examples/multiple_sprites.rs +++ b/examples/multiple_sprites.rs @@ -65,7 +65,7 @@ fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) { commands.spawn(cosmic_edit_2); } -pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { +fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { cosmic_text::Color::rgba( (color.r() * 255.) as u8, (color.g() * 255.) as u8, @@ -74,6 +74,36 @@ pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor { ) } +fn change_active_editor_sprite( + mut commands: Commands, + windows: Query<&Window, With<PrimaryWindow>>, + buttons: Res<Input<MouseButton>>, + mut cosmic_edit_query: Query< + (&mut Sprite, &GlobalTransform, 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.)); + 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.; + let y_max = node_transform.affine().translation.y + size.y / 2.; + if let Some(pos) = window.cursor_position() { + if let Some(pos) = camera.viewport_to_world_2d(camera_transform, pos) { + if x_min < pos.x && pos.x < x_max && y_min < pos.y && pos.y < y_max { + commands.insert_resource(Focus(Some(entity))) + }; + } + }; + } + } +} + fn main() { let font_bytes: &[u8] = include_bytes!("../assets/fonts/VictorMono-Regular.ttf"); let font_config = CosmicFontConfig { @@ -86,7 +116,6 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugins(CosmicEditPlugin { font_config }) .add_systems(Startup, setup) - .add_systems(Update, change_active_editor_ui) .add_systems(Update, change_active_editor_sprite) .run(); } diff --git a/examples/restricted_input.rs b/examples/restricted_input.rs index 8d7352694b89fd3ee0e40856d6a13a23404f8eb8..a5b8bd8d9586c5dad4ada415cf25627c3fbeb452 100644 --- a/examples/restricted_input.rs +++ b/examples/restricted_input.rs @@ -43,7 +43,5 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugins(CosmicEditPlugin::default()) .add_systems(Startup, setup) - .add_systems(Update, change_active_editor_ui) - .add_systems(Update, change_active_editor_sprite) .run(); } diff --git a/src/lib.rs b/src/lib.rs index cb9c0332743edf47a22f29a838ead1c82ef1c408..f61d42d0f0521c7bcbc13b6a411c7753e83435e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,6 @@ #![allow(clippy::type_complexity)] use std::{collections::VecDeque, path::PathBuf, time::Duration}; -#[path = "utils.rs"] -pub mod utils; -pub use utils::*; use bevy::{ asset::HandleId, @@ -1508,6 +1505,19 @@ fn draw_pixel( buffer[offset + 3] = (current >> 24) as u8; } +#[cfg(target_arch = "wasm32")] +pub fn get_timestamp() -> f64 { + js_sys::Date::now() +} + +#[cfg(not(target_arch = "wasm32"))] +pub fn get_timestamp() -> f64 { + use std::time::SystemTime; + use std::time::UNIX_EPOCH; + let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + duration.as_millis() as f64 +} + #[cfg(test)] mod tests { use crate::*; diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index 0e519db91829e49572a71000c60aefee0c61815e..0000000000000000000000000000000000000000 --- a/src/utils.rs +++ /dev/null @@ -1,64 +0,0 @@ -#[cfg(target_arch = "wasm32")] -pub fn get_timestamp() -> f64 { - js_sys::Date::now() -} - -#[cfg(not(target_arch = "wasm32"))] -pub fn get_timestamp() -> f64 { - use std::time::SystemTime; - use std::time::UNIX_EPOCH; - let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); - duration.as_millis() as f64 -} - -use crate::{CosmicEditor, Focus, ReadOnly}; -use bevy::{prelude::*, ui::Interaction, window::PrimaryWindow}; - -// util fns for examples -// -pub fn change_active_editor_ui( - mut commands: Commands, - mut interaction_query: Query< - (&Interaction, Entity), - ( - Changed<Interaction>, - (With<CosmicEditor>, Without<ReadOnly>), - ), - >, -) { - for (interaction, entity) in interaction_query.iter_mut() { - if let Interaction::Pressed = interaction { - commands.insert_resource(Focus(Some(entity))); - } - } -} - -pub fn change_active_editor_sprite( - mut commands: Commands, - windows: Query<&Window, With<PrimaryWindow>>, - buttons: Res<Input<MouseButton>>, - mut cosmic_edit_query: Query< - (&mut Sprite, &GlobalTransform, 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.)); - 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.; - let y_max = node_transform.affine().translation.y + size.y / 2.; - if let Some(pos) = window.cursor_position() { - if let Some(pos) = camera.viewport_to_world_2d(camera_transform, pos) { - if x_min < pos.x && pos.x < x_max && y_min < pos.y && pos.y < y_max { - commands.insert_resource(Focus(Some(entity))) - }; - } - }; - } - } -}