From bb7c2132f8cb8fd79150c40549e6d797a8c35fe1 Mon Sep 17 00:00:00 2001 From: sam edelsten <samedelsten1@gmail.com> Date: Mon, 18 Sep 2023 10:32:36 +0100 Subject: [PATCH] move click fns to userland --- examples/bevy_api_testing.rs | 51 ++++++++++++++++++++++++++-- examples/every_option.rs | 2 -- examples/font_per_widget.rs | 20 +++++++++-- examples/image_background.rs | 2 -- examples/multiple_sprites.rs | 33 +++++++++++++++++-- examples/restricted_input.rs | 2 -- src/lib.rs | 16 +++++++-- src/utils.rs | 64 ------------------------------------ 8 files changed, 111 insertions(+), 79 deletions(-) delete mode 100644 src/utils.rs diff --git a/examples/bevy_api_testing.rs b/examples/bevy_api_testing.rs index 75f7d4d..0b98f53 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 c522c70..f09282f 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 7f7a336..e8871b9 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 e3d2f87..cdc9d7d 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 d93018b..f088f80 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 8d73526..a5b8bd8 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 cb9c033..f61d42d 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 0e519db..0000000 --- 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))) - }; - } - }; - } - } -} -- GitLab