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::FileLoadError, math::curve::Curve, reflect::prelude::*, uuid::Uuid,
27        visitor::prelude::*, 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(FileLoadError),
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<FileLoadError> for CurveResourceError {
65    fn from(e: FileLoadError) -> 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, 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
98impl TypeUuidProvider for CurveResourceState {
99    fn type_uuid() -> Uuid {
100        CURVE_RESOURCE_UUID
101    }
102}
103
104impl CurveResourceState {
105    /// Load a curve resource from the specific file path.
106    pub async fn from_file(path: &Path, io: &dyn ResourceIo) -> Result<Self, CurveResourceError> {
107        let bytes = io.load_file(path).await?;
108        let mut visitor = Visitor::load_from_memory(&bytes)?;
109        let mut curve = Curve::default();
110        curve.visit("Curve", &mut visitor)?;
111        Ok(Self { curve })
112    }
113}
114
115/// Type alias for curve resources.
116pub type CurveResource = Resource<CurveResourceState>;