Skip to main content

fyrox_ui/
brush.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//! Brush defines a way to fill an arbitrary surface. See [`Brush`] docs for more info and usage examples.
22
23#![warn(missing_docs)]
24
25use crate::core::{algebra::Vector2, color::Color, reflect::prelude::*, visitor::prelude::*};
26use fyrox_core::uuid_provider;
27use strum_macros::{AsRefStr, EnumString, VariantNames};
28
29/// Gradient point defines a point on a surface with a color.
30#[derive(Clone, Debug, PartialEq, Reflect, Visit, Default)]
31pub struct GradientPoint {
32    /// A distance from the origin of the gradient.
33    pub stop: f32,
34    /// Color of the point.
35    pub color: Color,
36}
37
38uuid_provider!(GradientPoint = "e8503ec6-a1d0-4a9b-ab91-0d3f126254dd");
39
40/// Brush defines a way to fill an arbitrary surface.
41#[derive(Clone, Debug, PartialEq, Reflect, Visit, AsRefStr, EnumString, VariantNames)]
42pub enum Brush {
43    /// A brush, that fills a surface with a solid color.
44    Solid(Color),
45    /// A brush, that fills a surface with a linear gradient, which is defined by two points in local coordinates
46    /// and a set of stop points. See [`GradientPoint`] for more info.
47    LinearGradient {
48        /// Beginning of the gradient in local coordinates.
49        from: Vector2<f32>,
50        /// End of the gradient in local coordinates.
51        to: Vector2<f32>,
52        /// Stops of the gradient.
53        stops: Vec<GradientPoint>,
54    },
55    /// A brush, that fills a surface with a radial gradient, which is defined by a center point in local coordinates
56    /// and a set of stop points. See [`GradientPoint`] for more info.
57    RadialGradient {
58        /// Center of the gradient in local coordinates.
59        center: Vector2<f32>,
60        /// Stops of the gradient.
61        stops: Vec<GradientPoint>,
62    },
63}
64
65impl Brush {
66    /// Creates a new [`Brush::Solid`] using the given RGB color.
67    pub fn solid(r: u8, g: u8, b: u8) -> Self {
68        Self::Solid(Color::opaque(r, g, b))
69    }
70
71    /// Creates a new transparent [`Brush::Solid`].
72    pub fn transparent() -> Self {
73        Self::Solid(Color::TRANSPARENT)
74    }
75}
76
77impl From<Color> for Brush {
78    fn from(color: Color) -> Self {
79        Brush::Solid(color)
80    }
81}
82
83uuid_provider!(Brush = "eceb3805-73b6-47e0-8582-38a01f7b70e1");
84
85impl Default for Brush {
86    fn default() -> Self {
87        Self::Solid(Color::WHITE)
88    }
89}