Skip to content

Commit

Permalink
feat: select random connected identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Dec 11, 2023
1 parent 3440666 commit 66b660a
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "graph-view"
version = "0.1.13"
version = "0.1.14"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
13 changes: 7 additions & 6 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,28 @@ fn setup(
.unwrap(),
);
my_assets.identifier_connected_material_handle = color_materials.add(StandardMaterial {
emissive: Color::rgb(0.1, 0.1, 0.9),
base_color: Color::rgb(0.1, 0.1, 0.9),
..Default::default()
});
my_assets.identifier_selected_material_handle = color_materials.add(StandardMaterial {
emissive: Color::RED,
base_color: Color::RED,
..Default::default()
});
my_assets.identifier_material_handle = color_materials.add(StandardMaterial {
emissive: Color::GOLD,
base_color: Color::GOLD.with_a(0.6),
alpha_mode: AlphaMode::Blend,
..Default::default()
});

my_assets.connection_mesh_handle = meshes.add(Mesh::from(shape::Cylinder {
radius: 0.005,
radius: 0.02,
height: 1.0,
resolution: 5,
segments: 1,
..Default::default()
}));
my_assets.connection_material_handle = color_materials.add(StandardMaterial {
emissive: Color::rgb(0.1, 0.1, 0.9),
base_color: Color::rgb(0.1, 0.1, 0.9).with_a(0.7),
alpha_mode: AlphaMode::Blend,
..Default::default()
});
}
6 changes: 5 additions & 1 deletion src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ use bevy::prelude::*;
#[derive(Event, Debug)]
pub struct SelectRandomIdentifierEvent;

#[derive(Event, Debug)]
pub struct SelectRandomConnectedIdentifierEvent;

pub struct EventsPlugin;

impl Plugin for EventsPlugin {
fn build(&self, app: &mut App) {
app.add_event::<SelectRandomIdentifierEvent>();
app.add_event::<SelectRandomIdentifierEvent>()
.add_event::<SelectRandomConnectedIdentifierEvent>();
}
}
131 changes: 94 additions & 37 deletions src/identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use bevy_easings::*;
use bevy_panorbit_camera::PanOrbitCamera;
use rand::Rng;

use crate::{assets::MyAssets, events::SelectRandomIdentifierEvent, resources::Configuration};
use crate::{
assets::MyAssets,
events::{SelectRandomConnectedIdentifierEvent, SelectRandomIdentifierEvent},
resources::Configuration,
};

#[derive(Component)]
pub struct Identifier;
Expand All @@ -25,17 +29,54 @@ impl Plugin for IdentifiersPlugin {
app.init_resource::<SelectedIdentifier>()
.register_type::<SelectedIdentifier>()
.add_systems(Update, select_random_identifier)
.add_systems(Update, select_random_connected_identifier)
.add_systems(Update, update_identifiers_and_connections)
.add_systems(Update, zoom_camera_to_selected_identifier);
}
}

fn select_random_connected_identifier(
mut selected_identifier: ResMut<SelectedIdentifier>,
mut ev_rnd_c: EventReader<SelectRandomConnectedIdentifierEvent>,
connection_query: Query<(Entity, &Connection), With<Connection>>,
) {
#[allow(deprecated)]
for _ in ev_rnd_c.iter() {
// connections that have selected identifier as from or to
let connections: Vec<(Entity, &Connection)> = connection_query
.iter()
.filter(|(_, connection)| {
if let Some(selected_identifier) = selected_identifier.0 {
connection.from == selected_identifier || connection.to == selected_identifier
} else {
false
}
})
.collect();

if connections.is_empty() {
return;
}
// randomly select identifier from connections that is not the original selected identifier
let mut rng = rand::thread_rng();
if let Some(random_connection) = connections.get(rng.gen_range(0..connections.len())) {
if let Some(currently_selected_identifier) = selected_identifier.0 {
if random_connection.1.from == currently_selected_identifier {
selected_identifier.0 = Some(random_connection.1.to);
} else {
selected_identifier.0 = Some(random_connection.1.from);
}
}
};
}
}

fn select_random_identifier(
mut commands: Commands,
mut selected_identifier: ResMut<SelectedIdentifier>,
mut ev_rnd: EventReader<SelectRandomIdentifierEvent>,
query: Query<(Entity, &Identifier)>,
) {
#[allow(deprecated)]
for _ in ev_rnd.iter() {
let identifier_count = query.iter().count() as u32;
let mut rng = rand::thread_rng();
Expand All @@ -62,38 +103,58 @@ fn zoom_camera_to_selected_identifier(
};

if let Some(id) = selected_identifier.0 {
let &identifier_transform = identifier_query.get(id).unwrap();
if let Ok((camera_entity, &camera_transform)) = camera_query.get_single_mut() {
let direction = identifier_transform.translation - Vec3::ZERO;
let normalized_direction = direction.normalize();
let desired_distance = 3.0;

let camera_position =
identifier_transform.translation + normalized_direction * desired_distance;

commands.entity(camera_entity).insert(
camera_transform.ease_to(
Transform::from_xyz(camera_position.x, camera_position.y, camera_position.z)
.looking_at(identifier_transform.translation, Vec3::Y),
EaseFunction::QuarticOut,
bevy_easings::EasingType::Once {
duration: (std::time::Duration::from_secs(
configuration.animation_duration,
)),
},
),
);
if let Ok(&identifier_transform) = identifier_query.get(id) {
if let Ok((camera_entity, &camera_transform)) = camera_query.get_single_mut() {
let direction = identifier_transform.translation - Vec3::ZERO;
let normalized_direction = direction.normalize();
let desired_distance = 3.0;

let camera_position =
identifier_transform.translation + normalized_direction * desired_distance;

let mid_point = camera_transform
.translation
.lerp(identifier_transform.translation, 0.9);

commands.entity(camera_entity).insert(
camera_transform
.ease_to(
Transform::from_translation(mid_point)
.looking_at(identifier_transform.translation, Vec3::Y),
EaseFunction::QuinticInOut,
bevy_easings::EasingType::Once {
duration: (std::time::Duration::from_secs(
configuration.animation_duration / 2,
)),
},
)
.ease_to(
Transform::from_xyz(
camera_position.x,
camera_position.y,
camera_position.z,
)
.looking_at(identifier_transform.translation, Vec3::Y),
EaseFunction::QuarticInOut,
bevy_easings::EasingType::Once {
duration: (std::time::Duration::from_secs(
configuration.animation_duration / 2,
)),
},
),
);
};
};
}
}

fn update_identifiers_and_connections(
mut commands: Commands,
my_assets: ResMut<MyAssets>,
configuration: Res<Configuration>,
// configuration: Res<Configuration>,
selected_identifier: Res<SelectedIdentifier>,
identifier_query: Query<(Entity, &Transform), With<Identifier>>,
connection_query: Query<(Entity, &Connection, &Transform), With<Connection>>,
connection_query: Query<(Entity, &Connection), With<Connection>>,
) {
if !selected_identifier.is_changed() {
return;
Expand All @@ -104,46 +165,42 @@ fn update_identifiers_and_connections(
commands.entity(identifier).insert(MaterialMeshBundle {
mesh: my_assets.identifier_mesh_handle.clone(),
material: my_assets.identifier_selected_material_handle.clone(),
transform: identifier_transform
.clone()
.with_scale(Vec3::new(1.0, 1.0, 1.0)),
transform: identifier_transform.with_scale(Vec3::new(1.0, 1.0, 1.0)),
..Default::default()
});
}

// hide all other identifiers
// scale all other identifiers
for (identifier, &identifier_transform) in
identifier_query.iter().filter(|(entity, _)| *entity != id)
{
commands.entity(identifier).insert(MaterialMeshBundle {
mesh: my_assets.identifier_mesh_handle.clone(),
material: my_assets.identifier_material_handle.clone(),
transform: identifier_transform
.clone()
.with_scale(Vec3::new(0.5, 0.5, 0.5)),
transform: identifier_transform.with_scale(Vec3::new(0.5, 0.5, 0.5)),
..Default::default()
});
}

// show only connections that have from or to as selected identifier
for (connection_entity, &connection, &connection_transform) in connection_query.iter() {
for (connection_entity, &connection) in connection_query.iter() {
if connection.from == id || connection.to == id {
if let Ok((entity, &transform)) = identifier_query.get(connection.to.clone()) {
if let Ok((entity, &transform)) = identifier_query.get(connection.to) {
if entity != id {
commands.entity(entity).insert(MaterialMeshBundle {
mesh: my_assets.identifier_mesh_handle.clone(),
material: my_assets.identifier_connected_material_handle.clone(),
transform: transform.clone().with_scale(Vec3::new(1.0, 1.0, 1.0)),
transform: transform.with_scale(Vec3::new(1.0, 1.0, 1.0)),
..Default::default()
});
}
}
if let Ok((entity, &transform)) = identifier_query.get(connection.from.clone()) {
if let Ok((entity, &transform)) = identifier_query.get(connection.from) {
if entity != id {
commands.entity(entity).insert(MaterialMeshBundle {
mesh: my_assets.identifier_mesh_handle.clone(),
material: my_assets.identifier_connected_material_handle.clone(),
transform: transform.clone().with_scale(Vec3::new(1.0, 1.0, 1.0)),
transform: transform.with_scale(Vec3::new(1.0, 1.0, 1.0)),
..Default::default()
});
}
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ fn main() {

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
// mut meshes: ResMut<Assets<Mesh>>,
// mut materials: ResMut<Assets<StandardMaterial>>,
) {
let initial_camera_location = Vec3::new(-2.0, 2.5, 5.0);

Expand All @@ -77,4 +77,9 @@ fn setup(
// transform: Transform::from_xyz(0.0, 0.0, 0.0),
// ..default()
// });

commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0,
});
}
8 changes: 4 additions & 4 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub struct Configuration {
impl Default for Configuration {
fn default() -> Self {
Self {
identifiers: 10,
container_size: 2.0,
animation_duration: 2,
connections: 0,
identifiers: 8000,
container_size: 20.0,
animation_duration: 6,
connections: 30000,
}
}
}
Loading

0 comments on commit 66b660a

Please sign in to comment.