Skip to content

Commit b6d9246

Browse files
committed
progress with editor
1 parent e3e501b commit b6d9246

14 files changed

Lines changed: 554 additions & 522 deletions

File tree

crates/bevy_animation_graph/src/core/animation_graph_player.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use super::{
24
animation_graph::{AnimationGraph, DEFAULT_OUTPUT_POSE, InputOverlay, PinId, TimeUpdate},
35
context::{DeferredGizmos, PassContext},
@@ -9,14 +11,16 @@ use super::{
911
};
1012
use crate::{
1113
core::ragdoll::{bone_mapping::RagdollBoneMap, definition::Ragdoll, spawning::SpawnedRagdoll},
12-
prelude::{CustomRelativeDrawCommand, SystemResources},
14+
prelude::{CustomRelativeDrawCommand, CustomRelativeDrawCommandReference, SystemResources},
1315
};
1416
use bevy::{
1517
asset::prelude::*,
1618
color::{Color, palettes::css::WHITE},
1719
ecs::prelude::*,
20+
gizmos::gizmos::Gizmos,
1821
platform::collections::HashMap,
1922
reflect::prelude::*,
23+
transform::components::Transform,
2024
};
2125

2226
#[derive(Default, Reflect, Clone, Copy)]
@@ -227,6 +231,16 @@ impl AnimationGraphPlayer {
227231
self.debug_draw_custom.push(gizmo);
228232
}
229233

234+
pub fn gizmo_relative_to_root(
235+
&mut self,
236+
f: impl Fn(Transform, &mut Gizmos) + Send + Sync + 'static,
237+
) {
238+
self.debug_draw_custom.push(CustomRelativeDrawCommand {
239+
reference: CustomRelativeDrawCommandReference::Root,
240+
f: Arc::new(f),
241+
});
242+
}
243+
230244
pub(crate) fn debug_draw_bones(
231245
&mut self,
232246
system_resources: &SystemResources,

crates/bevy_animation_graph/src/core/context/deferred_gizmos.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,13 @@ impl DeferredGizmos {
7474
}
7575
}
7676

77+
pub enum CustomRelativeDrawCommandReference {
78+
Root,
79+
Bone(BoneId),
80+
}
81+
7782
pub struct CustomRelativeDrawCommand {
78-
pub bone_id: BoneId,
83+
pub reference: CustomRelativeDrawCommandReference,
7984
#[allow(clippy::type_complexity)]
8085
pub f: Arc<dyn Fn(Transform, &mut Gizmos) + Send + Sync + 'static>,
8186
}
@@ -203,9 +208,17 @@ impl DeferredGizmosContext<'_> {
203208
let default_pose = Pose::default();
204209
let pose = pose.unwrap_or(&default_pose);
205210

206-
let global_bone_transform =
207-
self.space_conversion
208-
.global_transform_of_bone(pose, skeleton, command.bone_id);
211+
let global_bone_transform = match command.reference {
212+
CustomRelativeDrawCommandReference::Root => self
213+
.space_conversion
214+
.pose_fallback
215+
.root_global_transform(skeleton)
216+
.unwrap()
217+
.compute_transform(),
218+
CustomRelativeDrawCommandReference::Bone(bone_id) => self
219+
.space_conversion
220+
.global_transform_of_bone(pose, skeleton, bone_id),
221+
};
209222

210223
self.gizmos
211224
.queue(DeferredGizmoCommand::Custom(Arc::new(move |gizmos| {

crates/bevy_animation_graph/src/core/context/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ mod pose_fallback;
66
mod spec_context;
77
mod system_resources;
88

9-
pub use deferred_gizmos::{CustomRelativeDrawCommand, DeferredGizmos, DeferredGizmosContext};
9+
pub use deferred_gizmos::{
10+
CustomRelativeDrawCommand, CustomRelativeDrawCommandReference, DeferredGizmos,
11+
DeferredGizmosContext,
12+
};
1013
pub use graph_context::{CacheReadFilter, CacheWriteFilter, GraphContext};
1114
pub use graph_context_arena::{GraphContextArena, GraphContextId};
1215
pub use pass_context::{FsmContext, PassContext, StateRole, StateStack};

crates/bevy_animation_graph/src/core/ragdoll/definition.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ impl Ragdoll {
2020
self.bodies.iter().find(|b| b.id == id)
2121
}
2222

23+
pub fn get_body_mut(&mut self, id: BodyId) -> Option<&mut Body> {
24+
self.bodies.iter_mut().find(|b| b.id == id)
25+
}
26+
2327
pub fn get_collider(&self, id: ColliderId) -> Option<&Collider> {
2428
self.bodies
2529
.iter()

crates/bevy_animation_graph_editor/src/tree.rs

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use bevy_animation_graph::core::{
66
};
77
use egui::Sense;
88

9-
use crate::{icons, ui::utils::collapsing::Collapser};
10-
119
pub struct Tree<I, L>(pub Vec<TreeInternal<I, L>>);
1210
impl<I, T> Default for Tree<I, T> {
1311
fn default() -> Self {
@@ -355,83 +353,3 @@ impl Tree<RagdollNode, RagdollNode> {
355353
TreeInternal::Leaf(format!("{:?}", joint.id), RagdollNode::Joint(joint_id))
356354
}
357355
}
358-
359-
#[derive(Clone, Copy)]
360-
pub struct RagdollTreeRenderer {}
361-
362-
impl RagdollTreeRenderer {}
363-
364-
pub struct RagdollResponse {
365-
pub hovered: Option<RagdollNode>,
366-
pub clicked: Option<RagdollNode>,
367-
}
368-
369-
impl TreeResponse for RagdollResponse {
370-
fn combine(&self, other: &Self) -> Self {
371-
RagdollResponse {
372-
hovered: self.hovered.or(other.hovered),
373-
clicked: self.clicked.or(other.clicked),
374-
}
375-
}
376-
}
377-
378-
impl TreeRenderer<RagdollNode, RagdollNode, RagdollResponse> for RagdollTreeRenderer {
379-
fn render_inner(
380-
&self,
381-
label: &str,
382-
data: &RagdollNode,
383-
children: &[TreeInternal<RagdollNode, RagdollNode>],
384-
ui: &mut egui::Ui,
385-
render_child: impl Fn(
386-
&TreeInternal<RagdollNode, RagdollNode>,
387-
&mut egui::Ui,
388-
) -> TreeResult<RagdollNode, RagdollNode, RagdollResponse>,
389-
) -> TreeResult<RagdollNode, RagdollNode, RagdollResponse> {
390-
let collapsing_response = Collapser::new()
391-
.with_default_open(true)
392-
.with_id_salt(data)
393-
.show(
394-
ui,
395-
|ui| self.render_leaf(label, data, ui),
396-
|ui| {
397-
children
398-
.iter()
399-
.map(|c| render_child(c, ui))
400-
.reduce(|l, r| l.or(r))
401-
.unwrap_or_default()
402-
},
403-
);
404-
405-
collapsing_response
406-
.head
407-
.or(collapsing_response.body.unwrap_or_default())
408-
}
409-
410-
fn render_leaf(
411-
&self,
412-
label: &str,
413-
data: &RagdollNode,
414-
ui: &mut egui::Ui,
415-
) -> TreeResult<RagdollNode, RagdollNode, RagdollResponse> {
416-
let response = ui
417-
.horizontal(|ui| {
418-
let image = match data {
419-
RagdollNode::Body(_) => icons::BONE,
420-
RagdollNode::Collider(_) => icons::BOX,
421-
RagdollNode::Joint(_) => icons::JOINT,
422-
};
423-
let color = ui.visuals().text_color();
424-
ui.add(egui::Image::new(image).tint(color).sense(Sense::click()))
425-
| ui.add(egui::Label::new(label).sense(Sense::click()))
426-
})
427-
.inner;
428-
429-
TreeResult::Leaf(
430-
data.clone(),
431-
RagdollResponse {
432-
hovered: response.hovered().then_some(*data),
433-
clicked: response.clicked().then_some(*data),
434-
},
435-
)
436-
}
437-
}

crates/bevy_animation_graph_editor/src/ui/actions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod colliders;
88
pub mod event_tracks;
99
pub mod fsm;
1010
pub mod graph;
11+
pub mod ragdoll;
1112
pub mod saving;
1213
pub mod window;
1314

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use bevy::{
2+
asset::{Assets, Handle},
3+
ecs::{
4+
system::{In, ResMut},
5+
world::World,
6+
},
7+
};
8+
use bevy_animation_graph::core::ragdoll::definition::{Body, Ragdoll};
9+
10+
use crate::ui::actions::{DynamicAction, run_handler, saving::DirtyAssets};
11+
12+
pub struct EditRagdollBody {
13+
pub ragdoll: Handle<Ragdoll>,
14+
pub body: Body,
15+
}
16+
17+
impl DynamicAction for EditRagdollBody {
18+
fn handle(self: Box<Self>, world: &mut World) {
19+
run_handler(world, "Could not edit body")(Self::system, *self)
20+
}
21+
}
22+
23+
impl EditRagdollBody {
24+
pub fn system(
25+
In(input): In<Self>,
26+
mut ragdoll_assets: ResMut<Assets<Ragdoll>>,
27+
mut dirty_assets: ResMut<DirtyAssets>,
28+
) {
29+
let Some(ragdoll) = ragdoll_assets.get_mut(&input.ragdoll) else {
30+
return;
31+
};
32+
33+
dirty_assets.add(input.ragdoll);
34+
35+
if let Some(body) = ragdoll.get_body_mut(input.body.id) {
36+
*body = input.body.clone()
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)