Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture groups #165

Merged
merged 22 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions rmf_site_editor/src/interaction/select_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
interaction::*,
site::{
drawing_editor::CurrentEditDrawing, Anchor, AnchorBundle, Category, Dependents,
DrawingMarker, Original, PathBehavior, Pending,
DrawingMarker, Original, PathBehavior, Pending, TextureNeedsAssignment,
},
CurrentWorkspace,
};
Expand Down Expand Up @@ -377,6 +377,25 @@ impl EdgePlacement {
})
}

fn with_extra<F>(self: Arc<Self>, f: F) -> Arc<Self>
where
F: Fn(&mut SelectAnchorPlacementParams, Entity) + Send + Sync + 'static,
{
let mut result = match Arc::try_unwrap(self) {
Ok(r) => r,
Err(r) => (*r).clone(),
};
let base = result.create;
result.create = Arc::new(
move |params: &mut SelectAnchorPlacementParams, edge: Edge<Entity>| {
let entity = base(params, edge);
f(params, entity);
entity
},
);
Arc::new(result)
}

fn update_dependencies(
mut anchor_selection: Option<&mut AnchorSelection>,
target: Entity,
Expand Down Expand Up @@ -812,6 +831,25 @@ impl PathPlacement {
})
}

fn with_extra<F>(self: Arc<Self>, f: F) -> Arc<Self>
where
F: Fn(&mut SelectAnchorPlacementParams, Entity) + Send + Sync + 'static,
{
let mut result = match Arc::try_unwrap(self) {
Ok(r) => r,
Err(r) => (*r).clone(),
};
let base = result.create;
result.create = Arc::new(
move |params: &mut SelectAnchorPlacementParams, path: Path<Entity>| {
let entity = base(params, path);
f(params, entity);
entity
},
);
Arc::new(result)
}

fn at_index(&self, index: usize) -> Arc<Self> {
Arc::new(Self {
index: Some(index),
Expand Down Expand Up @@ -1188,7 +1226,14 @@ impl SelectAnchorEdgeBuilder {
pub fn for_wall(self) -> SelectAnchor {
SelectAnchor {
target: self.for_element,
placement: EdgePlacement::new::<Wall<Entity>>(self.placement),
placement: EdgePlacement::new::<Wall<Entity>>(self.placement).with_extra(
|params, entity| {
params
.commands
.entity(entity)
.insert(TextureNeedsAssignment);
},
),
continuity: self.continuity,
scope: Scope::General,
}
Expand Down Expand Up @@ -1304,7 +1349,14 @@ impl SelectAnchorPathBuilder {
pub fn for_floor(self) -> SelectAnchor {
SelectAnchor {
target: self.for_element,
placement: PathPlacement::new::<Floor<Entity>>(self.placement),
placement: PathPlacement::new::<Floor<Entity>>(self.placement).with_extra(
|params, entity| {
params
.commands
.entity(entity)
.insert(TextureNeedsAssignment);
},
),
continuity: self.continuity,
scope: Scope::General,
}
Expand Down
2 changes: 1 addition & 1 deletion rmf_site_editor/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Default for LogHistory {
{
let fmt_layer = tracing_subscriber::fmt::Layer::default();
let subscriber = subscriber.with(fmt_layer);
tracing::subscriber::set_global_default(subscriber);
tracing::subscriber::set_global_default(subscriber).ok();
}
#[cfg(target_arch = "wasm32")]
{
Expand Down
10 changes: 2 additions & 8 deletions rmf_site_editor/src/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,14 +1111,8 @@ pub(crate) fn make_closed_path_outline(mut initial_positions: Vec<[f32; 3]>) ->
let p0 = Vec3::new(p0[0], p0[1], 0.0);
let p1 = Vec3::new(p1[0], p1[1], 0.0);
let p2 = Vec3::new(p2[0], p2[1], 0.0);
let v0 = match (p1 - p0).try_normalize() {
Some(v) => v,
None => continue,
};
let v1 = match (p2 - p1).try_normalize() {
Some(v) => v,
None => continue,
};
let v0 = (p1 - p0).normalize_or_zero();
let v1 = (p2 - p1).normalize_or_zero();

// n: normal
let n = Vec3::Z;
Expand Down
67 changes: 35 additions & 32 deletions rmf_site_editor/src/site/floor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,30 +211,32 @@ pub fn add_floor_visuals(
(
Entity,
&Path<Entity>,
&Texture,
&Affiliation<Entity>,
Option<&RecencyRank<FloorMarker>>,
Option<&LayerVisibility>,
Option<&Parent>,
),
Added<FloorMarker>,
>,
anchors: AnchorParams,
textures: Query<(Option<&Handle<Image>>, &Texture)>,
mut dependents: Query<&mut Dependents, With<Anchor>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
default_floor_vis: Query<(&GlobalFloorVisibility, &RecencyRanking<DrawingMarker>)>,
asset_server: Res<AssetServer>,
) {
for (e, new_floor, texture, rank, vis, parent) in &floors {
let mesh = make_floor_mesh(e, new_floor, texture, &anchors);
for (e, new_floor, texture_source, rank, vis, parent) in &floors {
let (base_color_texture, texture) = from_texture_source(texture_source, &textures);

let mesh = make_floor_mesh(e, new_floor, &texture, &anchors);
let mut cmd = commands.entity(e);
let height = floor_height(rank);
let default_vis = parent
.map(|p| default_floor_vis.get(p.get()).ok())
.flatten();
let (base_color, alpha_mode) = floor_transparency(vis, default_vis);
let material = materials.add(StandardMaterial {
base_color_texture: Some(asset_server.load(&String::from(&texture.source))),
base_color_texture,
base_color,
alpha_mode,
..default()
Expand Down Expand Up @@ -268,34 +270,21 @@ pub fn add_floor_visuals(
}
}

pub fn update_changed_floor(
changed_path: Query<
(Entity, &FloorSegments, &Path<Entity>, &Texture),
(Changed<Path<Entity>>, With<FloorMarker>),
>,
pub fn update_changed_floor_ranks(
changed_rank: Query<(Entity, &RecencyRank<FloorMarker>), Changed<RecencyRank<FloorMarker>>>,
anchors: AnchorParams,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut transforms: Query<&mut Transform>,
mut mesh_handles: Query<&mut Handle<Mesh>>,
) {
for (e, segments, path, texture) in &changed_path {
if let Ok(mut mesh) = mesh_handles.get_mut(segments.mesh) {
*mesh = mesh_assets.add(make_floor_mesh(e, path, texture, &anchors));
}
// TODO(MXG): Update texture once we support textures
}

for (e, rank) in &changed_rank {
if let Ok(mut tf) = transforms.get_mut(e) {
tf.translation.z = floor_height(Some(rank));
}
}
}

pub fn update_floor_for_moved_anchors(
floors: Query<(Entity, &FloorSegments, &Path<Entity>, &Texture), With<FloorMarker>>,
pub fn update_floors_for_moved_anchors(
floors: Query<(Entity, &FloorSegments, &Path<Entity>, &Affiliation<Entity>), With<FloorMarker>>,
anchors: AnchorParams,
textures: Query<(Option<&Handle<Image>>, &Texture)>,
changed_anchors: Query<
&Dependents,
(
Expand All @@ -308,34 +297,48 @@ pub fn update_floor_for_moved_anchors(
) {
for dependents in &changed_anchors {
for dependent in dependents.iter() {
if let Some((e, segments, path, texture)) = floors.get(*dependent).ok() {
if let Some((e, segments, path, texture_source)) = floors.get(*dependent).ok() {
let (_, texture) = from_texture_source(texture_source, &textures);
if let Ok(mut mesh) = mesh_handles.get_mut(segments.mesh) {
*mesh = mesh_assets.add(make_floor_mesh(e, path, texture, &anchors));
*mesh = mesh_assets.add(make_floor_mesh(e, path, &texture, &anchors));
}
}
}
}
}

pub fn update_floor_for_changed_texture(
pub fn update_floors(
floors: Query<(&FloorSegments, &Path<Entity>, &Affiliation<Entity>), With<FloorMarker>>,
changed_floors: Query<
(Entity, &FloorSegments, &Path<Entity>, &Texture),
(Changed<Texture>, With<FloorMarker>),
Entity,
(
With<FloorMarker>,
Or<(Changed<Affiliation<Entity>>, Changed<Path<Entity>>)>,
),
>,
changed_texture_sources: Query<
&Members,
(With<Group>, Or<(Changed<Handle<Image>>, Changed<Texture>)>),
>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut mesh_handles: Query<&mut Handle<Mesh>>,
material_handles: Query<&Handle<StandardMaterial>>,
anchors: AnchorParams,
asset_server: Res<AssetServer>,
textures: Query<(Option<&Handle<Image>>, &Texture)>,
) {
for (e, segment, path, texture) in &changed_floors {
for e in changed_floors.iter().chain(
changed_texture_sources
.iter()
.flat_map(|members| members.iter().cloned()),
) {
let Ok((segment, path, texture_source)) = floors.get(e) else { continue };
let (base_color_texture, texture) = from_texture_source(texture_source, &textures);
if let Ok(mut mesh) = mesh_handles.get_mut(segment.mesh) {
if let Ok(material) = material_handles.get(segment.mesh) {
*mesh = meshes.add(make_floor_mesh(e, path, texture, &anchors));
*mesh = meshes.add(make_floor_mesh(e, path, &texture, &anchors));
if let Some(mut material) = materials.get_mut(material) {
material.base_color_texture =
Some(asset_server.load(&String::from(&texture.source)));
material.base_color_texture = base_color_texture;
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions rmf_site_editor/src/site/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

use bevy::{
ecs::system::{Command, EntityCommands},
prelude::*,
};
use rmf_site_format::{Affiliation, Group};

#[derive(Component, Deref)]
pub struct Members(Vec<Entity>);

#[derive(Component, Clone, Copy)]
struct LastAffiliation(Option<Entity>);

pub fn update_members_of_groups(
mut commands: Commands,
mut changed_affiliation: Query<(Entity, &Affiliation<Entity>), Changed<Affiliation<Entity>>>,
) {
for (e, affiliation) in &mut changed_affiliation {
commands.entity(e).set_membership(affiliation.0);
}
}

struct ChangeMembership {
member: Entity,
group: Option<Entity>,
}

impl Command for ChangeMembership {
fn write(self, world: &mut World) {
let last = world
.get_entity(self.member)
.map(|e| e.get::<LastAffiliation>())
.flatten()
.cloned();
if let Some(last) = last {
if last.0 == self.group {
// There is no effect from this change
return;
}

if let Some(last) = last.0 {
if let Some(mut e) = world.get_entity_mut(last) {
if let Some(mut members) = e.get_mut::<Members>() {
members.0.retain(|m| *m != self.member);
}
}
}
}

if let Some(new_group) = self.group {
if let Some(mut e) = world.get_entity_mut(new_group) {
if let Some(mut members) = e.get_mut::<Members>() {
members.0.push(self.member);
} else {
e.insert(Members(vec![self.member]));
}
}
}

if let Some(mut e) = world.get_entity_mut(self.member) {
e.insert(LastAffiliation(self.group));
}
}
}

pub trait SetMembershipExt {
fn set_membership(&mut self, group: Option<Entity>) -> &mut Self;
}

impl<'w, 's, 'a> SetMembershipExt for EntityCommands<'w, 's, 'a> {
fn set_membership(&mut self, group: Option<Entity>) -> &mut Self {
let member = self.id();
self.commands().add(ChangeMembership { member, group });
self
}
}
Loading