fyrox_impl/resource/curve/
mod.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Curve resource holds a [`Curve`]
22
23use crate::{
24    asset::{io::ResourceIo, Resource, ResourceData, CURVE_RESOURCE_UUID},
25    core::{
26        io::FileError, math::curve::Curve, reflect::prelude::*, uuid::Uuid, visitor::prelude::*,
27        TypeUuidProvider,
28    },
29};
30use std::error::Error;
31use std::{
32    fmt::{Display, Formatter},
33    path::Path,
34};
35
36pub mod loader;
37
38/// An error that may occur during curve resource loading.
39#[derive(Debug)]
40pub enum CurveResourceError {
41    /// An i/o error has occurred.
42    Io(FileError),
43
44    /// An error that may occur due to version incompatibilities.
45    Visit(VisitError),
46}
47
48impl Display for CurveResourceError {
49    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50        match self {
51            CurveResourceError::Io(v) => {
52                write!(f, "A file load error has occurred {v:?}")
53            }
54            CurveResourceError::Visit(v) => {
55                write!(
56                    f,
57                    "An error that may occur due to version incompatibilities. {v:?}"
58                )
59            }
60        }
61    }
62}
63
64impl From<FileError> for CurveResourceError {
65    fn from(e: FileError) -> Self {
66        Self::Io(e)
67    }
68}
69
70impl From<VisitError> for CurveResourceError {
71    fn from(e: VisitError) -> Self {
72        Self::Visit(e)
73    }
74}
75
76/// State of the [`CurveResource`]
77#[derive(Debug, Clone, Visit, Default, Reflect)]
78pub struct CurveResourceState {
79    /// Actual curve.
80    pub curve: Curve,
81}
82
83impl ResourceData for CurveResourceState {
84    fn type_uuid(&self) -> Uuid {
85        <Self as TypeUuidProvider>::type_uuid()
86    }
87
88    fn save(&mut self, _path: &Path) -> Result<(), Box<dyn Error>> {
89        // TODO: Add saving.
90        Err("Saving is not supported!".to_string().into())
91    }
92
93    fn can_be_saved(&self) -> bool {
94        false
95    }
96
97    fn try_clone_box(&self) -> Option<Box<dyn ResourceData>> {
98        Some(Box::new(self.clone()))
99    }
100}
101
102impl TypeUuidProvider for CurveResourceState {
103    fn type_uuid() -> Uuid {
104        CURVE_RESOURCE_UUID
105    }
106}
107
108impl CurveResourceState {
109    /// Load a curve resource from the specific file path.
110    pub async fn from_file(path: &Path, io: &dyn ResourceIo) -> Result<Self, CurveResourceError> {
111        let bytes = io.load_file(path).await?;
112        let mut visitor = Visitor::load_from_memory(&bytes)?;
113        let mut curve = Curve::default();
114        curve.visit("Curve", &mut visitor)?;
115        Ok(Self { curve })
116    }
117}
118
119/// Type alias for curve resources.
120pub type CurveResource = Resource<CurveResourceState>;