use crate::command::make_command;
use crate::fyrox::core::reflect::Reflect;
use crate::fyrox::{
core::pool::Handle,
gui::inspector::{CollectionChanged, FieldKind, PropertyChanged},
scene::{node::Node, terrain::Terrain},
};
use crate::scene::commands::{GameSceneContext, RevertSceneNodePropertyCommand};
use crate::{
scene::commands::terrain::{AddTerrainLayerCommand, DeleteTerrainLayerCommand},
Command,
};
use fyrox::graph::SceneGraph;
pub struct SceneNodePropertyChangedHandler;
impl SceneNodePropertyChangedHandler {
fn try_get_command(
&self,
args: &PropertyChanged,
handle: Handle<Node>,
node: &mut Node,
) -> Option<Command> {
if args.path() == Terrain::LAYERS && node.is_terrain() {
match args.value {
FieldKind::Collection(ref collection_changed) => match **collection_changed {
CollectionChanged::Add(_) => {
Some(Command::new(AddTerrainLayerCommand::new(handle)))
}
CollectionChanged::Remove(index) => {
Some(Command::new(DeleteTerrainLayerCommand::new(handle, index)))
}
CollectionChanged::ItemChanged { .. } => None,
},
_ => None,
}
} else {
None
}
}
}
impl SceneNodePropertyChangedHandler {
pub fn handle(
&self,
args: &PropertyChanged,
handle: Handle<Node>,
node: &mut Node,
) -> Option<Command> {
self.try_get_command(args, handle, node).or_else(|| {
if args.is_inheritable() {
if node.resource().is_some() {
Some(Command::new(RevertSceneNodePropertyCommand::new(
args.path(),
handle,
)))
} else {
None
}
} else {
make_command(args, move |ctx| {
ctx.get_mut::<GameSceneContext>()
.scene
.graph
.try_get_node_mut(handle)
.ok()
.map(|n| n as &mut dyn Reflect)
})
}
})
}
}