use crate::{
core::{
pool::Handle, reflect::prelude::*, uuid_provider, variable, visitor::prelude::*,
ComponentProvider, NameProvider,
},
widget::Widget,
Control, ControlAsAny, UserInterface,
};
use fyrox_graph::SceneGraphNode;
use fyrox_resource::{untyped::UntypedResource, Resource};
use std::{
any::{Any, TypeId},
fmt::{Debug, Formatter},
ops::{Deref, DerefMut},
};
use uuid::Uuid;
pub mod constructor;
pub mod container;
pub struct UiNode(pub Box<dyn Control>);
impl<T: Control> From<T> for UiNode {
fn from(value: T) -> Self {
Self(Box::new(value))
}
}
uuid_provider!(UiNode = "d9b45ecc-91b0-40ea-a92a-4a7dee4667c9");
impl ComponentProvider for UiNode {
#[inline]
fn query_component_ref(&self, type_id: TypeId) -> Option<&dyn Any> {
self.0.query_component_ref(type_id)
}
#[inline]
fn query_component_mut(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
self.0.query_component_mut(type_id)
}
}
impl Clone for UiNode {
#[inline]
fn clone(&self) -> Self {
Self(self.0.clone_boxed())
}
}
impl SceneGraphNode for UiNode {
type Base = Widget;
type SceneGraph = UserInterface;
type ResourceData = UserInterface;
fn base(&self) -> &Self::Base {
self.0.deref()
}
fn set_base(&mut self, base: Self::Base) {
***self = base;
}
fn is_resource_instance_root(&self) -> bool {
self.is_resource_instance_root
}
fn original_handle_in_resource(&self) -> Handle<Self> {
self.original_handle_in_resource
}
fn set_original_handle_in_resource(&mut self, handle: Handle<Self>) {
self.original_handle_in_resource = handle;
}
fn resource(&self) -> Option<Resource<Self::ResourceData>> {
self.resource.clone()
}
fn self_handle(&self) -> Handle<Self> {
self.handle
}
fn parent(&self) -> Handle<Self> {
self.parent
}
fn children(&self) -> &[Handle<Self>] {
&self.children
}
fn children_mut(&mut self) -> &mut [Handle<Self>] {
&mut self.children
}
fn instance_id(&self) -> Uuid {
self.id
}
}
impl NameProvider for UiNode {
fn name(&self) -> &str {
&self.0.name
}
}
impl Debug for UiNode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Deref for UiNode {
type Target = dyn Control;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl DerefMut for UiNode {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0.deref_mut()
}
}
impl UiNode {
pub fn new<T>(widget: T) -> Self
where
T: Control,
{
Self(Box::new(widget))
}
pub fn cast<T>(&self) -> Option<&T>
where
T: Control,
{
ControlAsAny::as_any(&*self.0).downcast_ref::<T>()
}
pub fn cast_mut<T>(&mut self) -> Option<&mut T>
where
T: Control,
{
ControlAsAny::as_any_mut(&mut *self.0).downcast_mut::<T>()
}
pub fn query_component<T>(&self) -> Option<&T>
where
T: 'static,
{
self.0
.query_component_ref(TypeId::of::<T>())
.and_then(|c| c.downcast_ref::<T>())
}
pub fn has_component<T>(&self) -> bool
where
T: 'static,
{
self.query_component::<T>().is_some()
}
pub(crate) fn set_inheritance_data(
&mut self,
original_handle: Handle<UiNode>,
model: Resource<UserInterface>,
) {
self.resource = Some(model.clone());
self.is_resource_instance_root = false;
self.as_reflect_mut(&mut |reflect| {
variable::mark_inheritable_properties_non_modified(
reflect,
&[TypeId::of::<UntypedResource>()],
)
});
self.original_handle_in_resource = original_handle;
}
}
impl Visit for UiNode {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
self.0.visit(name, visitor)
}
}
impl Reflect for UiNode {
fn source_path() -> &'static str {
file!()
}
fn derived_types() -> &'static [TypeId] {
&[]
}
fn try_clone_box(&self) -> Option<Box<dyn Reflect>> {
Some(Box::new(self.clone()))
}
fn query_derived_types(&self) -> &'static [TypeId] {
Self::derived_types()
}
fn type_name(&self) -> &'static str {
Reflect::type_name(self.0.deref())
}
fn doc(&self) -> &'static str {
self.0.deref().doc()
}
fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef])) {
self.0.deref().fields_ref(func)
}
fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut])) {
self.0.deref_mut().fields_mut(func)
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
Reflect::into_any(self.0)
}
fn as_any(&self, func: &mut dyn FnMut(&dyn Any)) {
Reflect::as_any(self.0.deref(), func)
}
fn as_any_mut(&mut self, func: &mut dyn FnMut(&mut dyn Any)) {
Reflect::as_any_mut(self.0.deref_mut(), func)
}
fn as_reflect(&self, func: &mut dyn FnMut(&dyn Reflect)) {
self.0.deref().as_reflect(func)
}
fn as_reflect_mut(&mut self, func: &mut dyn FnMut(&mut dyn Reflect)) {
self.0.deref_mut().as_reflect_mut(func)
}
fn set(&mut self, value: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>> {
self.0.deref_mut().set(value)
}
fn assembly_name(&self) -> &'static str {
self.0.deref().assembly_name()
}
fn type_assembly_name() -> &'static str {
env!("CARGO_PKG_NAME")
}
fn set_field(
&mut self,
field: &str,
value: Box<dyn Reflect>,
func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldError>),
) {
self.0.deref_mut().set_field(field, value, func)
}
fn field(&self, name: &str, func: &mut dyn FnMut(Option<&dyn Reflect>)) {
self.0.deref().field(name, func)
}
fn field_mut(&mut self, name: &str, func: &mut dyn FnMut(Option<&mut dyn Reflect>)) {
self.0.deref_mut().field_mut(name, func)
}
}