mirror of
https://github.com/FyroxEngine/Fyrox.git
synced 2026-10-12 02:02:20 +00:00
use typed handles for scripts + script messages
This commit is contained in:
@@ -67,6 +67,7 @@ use crate::{
|
||||
},
|
||||
};
|
||||
use fxhash::FxHashMap;
|
||||
use fyrox_core::pool::ObjectOrVariant;
|
||||
use fyrox_ui::{UiNode, UserInterface};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
@@ -388,7 +389,7 @@ pub trait ModelResourceExtension: Sized {
|
||||
fn instantiate_and_attach(
|
||||
&self,
|
||||
scene: &mut Scene,
|
||||
parent: Handle<Node>,
|
||||
parent: Handle<impl ObjectOrVariant<Node>>,
|
||||
position: Vector3<f32>,
|
||||
face_towards: Vector3<f32>,
|
||||
scale: Vector3<f32>,
|
||||
@@ -537,11 +538,12 @@ impl ModelResourceExtension for ModelResource {
|
||||
fn instantiate_and_attach(
|
||||
&self,
|
||||
scene: &mut Scene,
|
||||
parent: Handle<Node>,
|
||||
parent: Handle<impl ObjectOrVariant<Node>>,
|
||||
position: Vector3<f32>,
|
||||
face_towards: Vector3<f32>,
|
||||
scale: Vector3<f32>,
|
||||
) -> Handle<Node> {
|
||||
let parent = parent.to_base();
|
||||
let parent_scale = scene.graph.global_scale(parent);
|
||||
|
||||
let parent_inv_transform = scene.graph[parent]
|
||||
@@ -566,7 +568,7 @@ impl ModelResourceExtension for ModelResource {
|
||||
.with_scale(local_scale)
|
||||
.finish();
|
||||
|
||||
scene.graph.link_nodes(instance, parent);
|
||||
scene.graph.link_nodes(instance, parent.transmute());
|
||||
|
||||
instance
|
||||
}
|
||||
|
||||
@@ -1731,11 +1731,12 @@ impl Graph {
|
||||
|
||||
/// Returns global scale of a node.
|
||||
#[inline]
|
||||
pub fn global_scale(&self, mut node: Handle<Node>) -> Vector3<f32> {
|
||||
pub fn global_scale(&self, handle: Handle<impl ObjectOrVariant<Node>>) -> Vector3<f32> {
|
||||
let mut handle = handle.to_base();
|
||||
let mut global_scale = Vector3::repeat(1.0);
|
||||
while let Ok(node_ref) = self.try_get_node(node) {
|
||||
while let Ok(node_ref) = self.try_get_node(handle) {
|
||||
global_scale = global_scale.component_mul(node_ref.local_transform().scale());
|
||||
node = node_ref.parent;
|
||||
handle = node_ref.parent;
|
||||
}
|
||||
global_scale
|
||||
}
|
||||
@@ -1743,10 +1744,14 @@ impl Graph {
|
||||
/// Tries to borrow a node using the given handle and searches the script buffer for a script
|
||||
/// of type T and cast the first script, that could be found to the specified type.
|
||||
#[inline]
|
||||
pub fn try_get_script_of<T>(&self, handle: Handle<Node>) -> Result<&T, GraphError>
|
||||
pub fn try_get_script_of<T>(
|
||||
&self,
|
||||
handle: Handle<impl ObjectOrVariant<Node>>,
|
||||
) -> Result<&T, GraphError>
|
||||
where
|
||||
T: ScriptTrait,
|
||||
{
|
||||
let handle = handle.to_base();
|
||||
let node = self.try_get_node(handle)?;
|
||||
node.try_get_script::<T>()
|
||||
.ok_or_else(|| GraphError::NoScript {
|
||||
@@ -1761,8 +1766,9 @@ impl Graph {
|
||||
#[inline]
|
||||
pub fn try_get_scripts_of<T: ScriptTrait>(
|
||||
&self,
|
||||
handle: Handle<Node>,
|
||||
handle: Handle<impl ObjectOrVariant<Node>>,
|
||||
) -> Result<impl Iterator<Item = &T>, GraphError> {
|
||||
let handle = handle.to_base();
|
||||
let node = self.try_get_node(handle)?;
|
||||
Ok(node.try_get_scripts())
|
||||
}
|
||||
@@ -1770,10 +1776,14 @@ impl Graph {
|
||||
/// Tries to borrow a node using the given handle and searches the script buffer for a script
|
||||
/// of type T and cast the first script, that could be found to the specified type.
|
||||
#[inline]
|
||||
pub fn try_get_script_of_mut<T>(&mut self, handle: Handle<Node>) -> Result<&mut T, GraphError>
|
||||
pub fn try_get_script_of_mut<T>(
|
||||
&mut self,
|
||||
handle: Handle<impl ObjectOrVariant<Node>>,
|
||||
) -> Result<&mut T, GraphError>
|
||||
where
|
||||
T: ScriptTrait,
|
||||
{
|
||||
let handle = handle.to_base();
|
||||
let node = self.try_get_node_mut(handle)?;
|
||||
node.try_get_script_mut::<T>()
|
||||
.ok_or_else(|| GraphError::NoScript {
|
||||
@@ -1788,11 +1798,12 @@ impl Graph {
|
||||
#[inline]
|
||||
pub fn try_get_scripts_of_mut<T>(
|
||||
&mut self,
|
||||
handle: Handle<Node>,
|
||||
handle: Handle<impl ObjectOrVariant<Node>>,
|
||||
) -> Result<impl Iterator<Item = &mut T>, GraphError>
|
||||
where
|
||||
T: ScriptTrait,
|
||||
{
|
||||
let handle = handle.to_base();
|
||||
let node = self.try_get_node_mut(handle)?;
|
||||
Ok(node.try_get_scripts_mut())
|
||||
}
|
||||
@@ -1801,10 +1812,14 @@ impl Graph {
|
||||
/// scripts of the node. If you want to search a component `C` in a particular script, then use
|
||||
/// [`Self::try_get_script_of`] and then search for component in it.
|
||||
#[inline]
|
||||
pub fn try_get_script_component_of<C>(&self, handle: Handle<Node>) -> Result<&C, GraphError>
|
||||
pub fn try_get_script_component_of<C>(
|
||||
&self,
|
||||
handle: Handle<impl ObjectOrVariant<Node>>,
|
||||
) -> Result<&C, GraphError>
|
||||
where
|
||||
C: Any,
|
||||
{
|
||||
let handle = handle.to_base();
|
||||
let node = self.try_get_node(handle)?;
|
||||
node.try_get_script_component()
|
||||
.ok_or_else(|| GraphError::NoScriptComponent {
|
||||
@@ -1819,11 +1834,12 @@ impl Graph {
|
||||
#[inline]
|
||||
pub fn try_get_script_component_of_mut<C>(
|
||||
&mut self,
|
||||
handle: Handle<Node>,
|
||||
handle: Handle<impl ObjectOrVariant<Node>>,
|
||||
) -> Result<&mut C, GraphError>
|
||||
where
|
||||
C: Any,
|
||||
{
|
||||
let handle = handle.to_base();
|
||||
let node = self.try_get_node_mut(handle)?;
|
||||
node.try_get_script_component_mut()
|
||||
.ok_or_else(|| GraphError::NoScriptComponent {
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use super::collider::BitMask;
|
||||
use crate::{
|
||||
asset::{untyped::UntypedResource, Resource},
|
||||
core::{
|
||||
@@ -69,8 +70,6 @@ use std::{
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
||||
use super::collider::BitMask;
|
||||
|
||||
pub mod constructor;
|
||||
pub mod container;
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ use crate::{
|
||||
plugin::{Plugin, PluginContainer},
|
||||
scene::{base::NodeScriptMessage, node::Node, Scene},
|
||||
};
|
||||
use fyrox_core::pool::ObjectOrVariant;
|
||||
pub use fyrox_core_derive::ScriptMessagePayload;
|
||||
use fyrox_graph::BaseSceneGraph;
|
||||
use std::{
|
||||
@@ -156,13 +157,13 @@ impl ScriptMessageSender {
|
||||
}
|
||||
|
||||
/// Sends a targeted script message with the given payload.
|
||||
pub fn send_to_target<T>(&self, target: Handle<Node>, payload: T)
|
||||
pub fn send_to_target<T>(&self, target: Handle<impl ObjectOrVariant<Node>>, payload: T)
|
||||
where
|
||||
T: ScriptMessagePayload,
|
||||
{
|
||||
self.send(ScriptMessage {
|
||||
payload: Box::new(payload),
|
||||
kind: ScriptMessageKind::Targeted(target),
|
||||
kind: ScriptMessageKind::Targeted(target.transmute()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -178,13 +179,20 @@ impl ScriptMessageSender {
|
||||
}
|
||||
|
||||
/// Sends a hierarchical script message with the given payload.
|
||||
pub fn send_hierarchical<T>(&self, root: Handle<Node>, routing: RoutingStrategy, payload: T)
|
||||
where
|
||||
pub fn send_hierarchical<T>(
|
||||
&self,
|
||||
root: Handle<impl ObjectOrVariant<Node>>,
|
||||
routing: RoutingStrategy,
|
||||
payload: T,
|
||||
) where
|
||||
T: ScriptMessagePayload,
|
||||
{
|
||||
self.send(ScriptMessage {
|
||||
payload: Box::new(payload),
|
||||
kind: ScriptMessageKind::Hierarchical { root, routing },
|
||||
kind: ScriptMessageKind::Hierarchical {
|
||||
root: root.transmute(),
|
||||
routing,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user