use crate::core::math::aabb::AxisAlignedBoundingBox;
use crate::{
core::{
color::Color,
inspect::{Inspect, PropertyInfo},
pool::Handle,
visitor::prelude::*,
},
resource::texture::Texture,
scene::{
base::{Base, BaseBuilder},
graph::Graph,
node::Node,
},
};
use std::ops::{Deref, DerefMut};
#[derive(Debug, Visit, Default, Inspect)]
pub struct Decal {
base: Base,
diffuse_texture: Option<Texture>,
normal_texture: Option<Texture>,
#[visit(optional)] color: Color,
#[visit(optional)] #[inspect(min_value = 0.0)]
layer: u8,
}
impl Deref for Decal {
type Target = Base;
fn deref(&self) -> &Self::Target {
&self.base
}
}
impl DerefMut for Decal {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.base
}
}
impl Decal {
pub fn raw_copy(&self) -> Self {
Self {
base: self.base.raw_copy(),
diffuse_texture: self.diffuse_texture.clone(),
normal_texture: self.normal_texture.clone(),
color: self.color,
layer: self.layer,
}
}
pub fn set_diffuse_texture(&mut self, diffuse_texture: Option<Texture>) -> Option<Texture> {
std::mem::replace(&mut self.diffuse_texture, diffuse_texture)
}
pub fn diffuse_texture(&self) -> Option<&Texture> {
self.diffuse_texture.as_ref()
}
pub fn diffuse_texture_value(&self) -> Option<Texture> {
self.diffuse_texture.clone()
}
pub fn set_normal_texture(&mut self, normal_texture: Option<Texture>) -> Option<Texture> {
std::mem::replace(&mut self.normal_texture, normal_texture)
}
pub fn normal_texture(&self) -> Option<&Texture> {
self.normal_texture.as_ref()
}
pub fn normal_texture_value(&self) -> Option<Texture> {
self.normal_texture.clone()
}
pub fn set_color(&mut self, color: Color) {
self.color = color;
}
pub fn color(&self) -> Color {
self.color
}
pub fn set_layer(&mut self, layer: u8) {
self.layer = layer;
}
pub fn layer(&self) -> u8 {
self.layer
}
#[inline]
pub fn local_bounding_box(&self) -> AxisAlignedBoundingBox {
self.base.local_bounding_box()
}
pub fn world_bounding_box(&self) -> AxisAlignedBoundingBox {
self.base.world_bounding_box()
}
}
pub struct DecalBuilder {
base_builder: BaseBuilder,
diffuse_texture: Option<Texture>,
normal_texture: Option<Texture>,
color: Color,
layer: u8,
}
impl DecalBuilder {
pub fn new(base_builder: BaseBuilder) -> Self {
Self {
base_builder,
diffuse_texture: None,
normal_texture: None,
color: Color::opaque(255, 255, 255),
layer: 0,
}
}
pub fn with_diffuse_texture(mut self, diffuse_texture: Texture) -> Self {
self.diffuse_texture = Some(diffuse_texture);
self
}
pub fn with_normal_texture(mut self, normal_texture: Texture) -> Self {
self.normal_texture = Some(normal_texture);
self
}
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn with_layer(mut self, layer: u8) -> Self {
self.layer = layer;
self
}
pub fn build_node(self) -> Node {
Node::Decal(Decal {
base: self.base_builder.build_base(),
diffuse_texture: self.diffuse_texture,
normal_texture: self.normal_texture,
color: self.color,
layer: self.layer,
})
}
pub fn build(self, graph: &mut Graph) -> Handle<Node> {
graph.add_node(self.build_node())
}
}