Skip to main content

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},
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};
35use uuid::uuid;
36
37pub mod loader;
38
39/// An error that may occur during curve resource loading.
40#[derive(Debug)]
41pub enum CurveResourceError {
42    /// An i/o error has occurred.
43    Io(FileError),
44
45    /// An error that may occur due to version incompatibilities.
46    Visit(VisitError),
47}
48
49impl Display for CurveResourceError {
50    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51        match self {
52            CurveResourceError::Io(v) => {
53                write!(f, "A file load error has occurred {v:?}")
54            }
55            CurveResourceError::Visit(v) => {
56                write!(
57                    f,
58                    "An error that may occur due to version incompatibilities. {v:?}"
59                )
60            }
61        }
62    }
63}
64
65impl From<FileError> for CurveResourceError {
66    fn from(e: FileError) -> Self {
67        Self::Io(e)
68    }
69}
70
71impl From<VisitError> for CurveResourceError {
72    fn from(e: VisitError) -> Self {
73        Self::Visit(e)
74    }
75}
76
77/// State of the [`CurveResource`]
78#[derive(Debug, Clone, Visit, Default, Reflect)]
79pub struct CurveResourceState {
80    /// Actual curve.
81    pub curve: Curve,
82}
83
84impl ResourceData for CurveResourceState {
85    fn type_uuid(&self) -> Uuid {
86        <Self as TypeUuidProvider>::type_uuid()
87    }
88
89    fn save(&mut self, _path: &Path) -> Result<(), Box<dyn Error>> {
90        // TODO: Add saving.
91        Err("Saving is not supported!".to_string().into())
92    }
93
94    fn can_be_saved(&self) -> bool {
95        false
96    }
97
98    fn try_clone_box(&self) -> Option<Box<dyn ResourceData>> {
99        Some(Box::new(self.clone()))
100    }
101}
102
103impl TypeUuidProvider for CurveResourceState {
104    fn type_uuid() -> Uuid {
105        uuid!("f28b949f-28a2-4b68-9089-59c234f58b6b")
106    }
107}
108
109impl CurveResourceState {
110    /// Load a curve resource from the specific file path.
111    pub async fn from_file(path: &Path, io: &dyn ResourceIo) -> Result<Self, CurveResourceError> {
112        let bytes = io.load_file(path).await?;
113        let mut visitor = Visitor::load_from_memory(&bytes)?;
114        let mut curve = Curve::default();
115        curve.visit("Curve", &mut visitor)?;
116        Ok(Self { curve })
117    }
118}
119
120/// Type alias for curve resources.
121pub type CurveResource = Resource<CurveResourceState>;