Skip to content

Commit

Permalink
Merge pull request #41 from nixpulvis/labels
Browse files Browse the repository at this point in the history
Add system name labels
  • Loading branch information
nixpulvis authored Sep 19, 2024
2 parents 4be942b + b7d279e commit a5c2622
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions starmap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ inspector = ["bevy-inspector-egui"]
tracy = ["bevy/trace_tracy"]

[dependencies]
async-std = "*"
bevy = { version = "*", features = ["shader_format_glsl"] }
bevy_panorbit_camera = { version = "*", features = ["bevy_egui"] }
bevy-inspector-egui = { version = "*", optional = true }
bevy_egui = "*"
bevy_mod_billboard = "*"
bevy_mod_picking = "*"
async-std = "*"
galos_db = { path = "../galos_db" }
bevy_panorbit_camera = { version = "*", features = ["bevy_egui"] }
elite_journal = { path = "../elite_journal" }
bevy-inspector-egui = { version = "*", optional = true }
galos_db = { path = "../galos_db" }
Binary file added starmap/assets/neuropolitical.otf
Binary file not shown.
15 changes: 12 additions & 3 deletions starmap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy::{prelude::*, window::WindowMode};
use bevy_egui::EguiPlugin;
#[cfg(feature = "inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_mod_billboard::prelude::*;
use bevy_mod_picking::prelude::*;
use bevy_panorbit_camera::PanOrbitCameraPlugin;
use galos_db::Database;
Expand Down Expand Up @@ -33,17 +34,20 @@ fn main() {
}));
app.add_plugins(PanOrbitCameraPlugin);
app.add_plugins(DefaultPickingPlugins);
app.add_plugins(BillboardPlugin);
app.add_plugins(EguiPlugin);

app.insert_resource(ClearColor(Color::BLACK));
app.insert_resource(AmbientLight {
color: Color::default(),
brightness: 1000.0,
});
app.insert_resource(Db(db));

app.insert_resource(systems::View::Systems);
app.insert_resource(systems::ColorBy::Allegiance);
app.insert_resource(systems::ScalePopulation(false));

app.insert_resource(systems::ShowNames(false));
app.insert_resource(systems::Spyglass {
radius: 50.,
fetch: true,
Expand All @@ -52,13 +56,15 @@ fn main() {

app.insert_resource(systems::Fetched(HashSet::new()));
app.insert_resource(systems::FetchTasks { fetched: HashMap::new() });

app.add_event::<camera::MoveCamera>();
app.add_event::<search::Searched>();
app.add_systems(Startup, camera::spawn_camera);
app.add_systems(Update, camera::move_camera);
app.add_systems(Update, camera::keyboard);

app.add_systems(Update, systems::fetch);
app.add_systems(Update, systems::spawn.after(camera::move_camera));
app.add_systems(Update, systems::update.after(systems::spawn));
app.add_systems(
Update,
systems::scale_systems
Expand All @@ -71,10 +77,13 @@ fn main() {
.after(systems::spawn)
.run_if(resource_equals(systems::View::Stars)),
);

app.add_event::<search::Searched>();
app.add_systems(Update, search::system);

app.add_systems(Update, ui::settings);
app.add_systems(Update, ui::search.after(ui::settings));
app.add_systems(Update, ui::route.after(ui::search));
app.add_systems(Update, search::system);

#[cfg(feature = "inspector")]
app.add_plugins(WorldInspectorPlugin::new());
Expand Down
57 changes: 56 additions & 1 deletion starmap/src/systems/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::pbr::NotShadowCaster;
use bevy::prelude::*;
use bevy::tasks::block_on;
use bevy::tasks::futures_lite::future;
use bevy_mod_billboard::{BillboardLockAxis, BillboardTextBundle};
use bevy_mod_picking::prelude::*;
use elite_journal::{system::Security, Allegiance, Government};
use galos_db::systems::System as DbSystem;
Expand All @@ -19,12 +20,18 @@ pub enum ColorBy {
Security,
}

/// Determains whether or not to show system name labels
#[derive(Resource)]
pub struct ShowNames(pub bool);

/// Polls the tasks in `FetchTasks` and spawns entities for each of the
/// resulting star systems
pub fn spawn(
systems_query: Query<(Entity, &System)>,
route_query: Query<Entity, With<Route>>,
color_by: Res<ColorBy>,
show_names: Res<ShowNames>,
asset_server: Res<AssetServer>,
mut move_camera_events: EventWriter<MoveCamera>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
Expand All @@ -42,6 +49,8 @@ pub fn spawn(
&new_systems,
&systems_query,
&color_by,
&show_names,
&asset_server,
&mut commands,
&mut meshes,
&mut materials,
Expand Down Expand Up @@ -82,6 +91,8 @@ pub(crate) fn spawn_systems(
new_systems: &[DbSystem],
systems_query: &Query<(Entity, &System)>,
color_by: &Res<ColorBy>,
show_names: &Res<ShowNames>,
asset_server: &Res<AssetServer>,
commands: &mut Commands,
mesh_asset: &mut ResMut<Assets<Mesh>>,
material_assets: &mut ResMut<Assets<StandardMaterial>>,
Expand All @@ -93,6 +104,7 @@ pub(crate) fn spawn_systems(

let mesh = init_meshes(mesh_asset);
let materials = init_materials(material_assets);
let font = asset_server.load("neuropolitical.otf");

for new_system in new_systems {
let color_idx = match color_by.deref() {
Expand All @@ -103,7 +115,7 @@ pub(crate) fn spawn_systems(
if let Some(_enitity) = existing_systems.remove(&new_system.address) {
// TODO: update
} else {
commands.spawn((
let mut entity = commands.spawn((
PbrBundle {
transform: Transform {
translation: Vec3::new(
Expand Down Expand Up @@ -141,8 +153,51 @@ pub(crate) fn spawn_systems(
},
),
));

if show_names.0 {
entity.with_children(|parent| {
parent.spawn((
BillboardTextBundle {
transform: Transform::from_scale(Vec3::splat(0.01))
.with_translation(Vec3::new(5., 0., 0.)),
text: Text::from_section(
new_system.name.clone(),
TextStyle {
font_size: 64.0,
font: font.clone(),
color: Color::WHITE,
},
)
.with_justify(JustifyText::Left),
..default()
},
BillboardLockAxis::default(),
));
});
}
}
}
}

/// Checks for updated resources and updates the systems.
pub fn update(
systems_query: Query<(&System, &Children)>,
show_names: Res<ShowNames>,
mut commands: Commands,
) {
if show_names.is_changed() {
for (_, children) in systems_query.iter() {
for &child in children.iter() {
if show_names.0 {
commands.entity(child).insert(Visibility::Visible);
} else {
commands.entity(child).insert(Visibility::Hidden);
}
}
}
}

// TODO: update ColorBy
}

fn init_meshes(assets: &mut Assets<Mesh>) -> Handle<Mesh> {
Expand Down
5 changes: 4 additions & 1 deletion starmap/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::search::Searched;
use crate::systems::{ColorBy, ScalePopulation, Spyglass, View};
use crate::systems::{ColorBy, ScalePopulation, ShowNames, Spyglass, View};
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts};

Expand All @@ -12,6 +12,7 @@ pub fn settings(
mut view: ResMut<View>,
mut color_by: ResMut<ColorBy>,
mut population_scale: ResMut<ScalePopulation>,
mut show_names: ResMut<ShowNames>,
) {
if let Some(ctx) = contexts.try_ctx_mut() {
// TODO: We really need to figure out how to trigger a re-fetch after
Expand Down Expand Up @@ -54,6 +55,8 @@ pub fn settings(
}
View::Stars => {}
}

ui.checkbox(&mut show_names.0, "Show System Names");
});
}
}
Expand Down

0 comments on commit a5c2622

Please sign in to comment.