mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Old value node migrations
This commit is contained in:
@@ -128,11 +128,11 @@ impl<'i, Root: Node<'i, I>, I: 'i + From<()>> ConsNode<I, Root> {
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::generic::FnNode;
|
||||
use crate::value::ValueNode;
|
||||
use crate::value::ClonedNode;
|
||||
|
||||
#[test]
|
||||
fn compose() {
|
||||
let value = ValueNode::new(4u32);
|
||||
let value = ClonedNode::new(4u32);
|
||||
let compose = value.then(FnNode::new(|x| x));
|
||||
assert_eq!(compose.eval(()), &4u32);
|
||||
let type_erased = &compose as &dyn Node<'_, (), Output = &'_ u32>;
|
||||
@@ -141,7 +141,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn test_ref_eval() {
|
||||
let value = ValueNode::new(5);
|
||||
let value = ClonedNode::new(5);
|
||||
|
||||
assert_eq!(value.eval(()), &5);
|
||||
let id = FnNode::new(|x| x);
|
||||
|
||||
@@ -13,29 +13,6 @@ impl<'i, const N: u32, I> Node<'i, I> for IntNode<N> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
pub struct ValueNode<T>(pub T);
|
||||
|
||||
impl<'i, T: 'i, I> Node<'i, I> for ValueNode<T> {
|
||||
type Output = &'i T;
|
||||
#[inline(always)]
|
||||
fn eval(&'i self, _input: I) -> Self::Output {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ValueNode<T> {
|
||||
pub const fn new(value: T) -> ValueNode<T> {
|
||||
ValueNode(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for ValueNode<T> {
|
||||
fn from(value: T) -> Self {
|
||||
ValueNode::new(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy)]
|
||||
pub struct AsRefNode<T: AsRef<U>, U>(pub T, PhantomData<U>);
|
||||
|
||||
@@ -192,13 +169,6 @@ mod test {
|
||||
assert_eq!(node.eval(()), 5);
|
||||
}
|
||||
#[test]
|
||||
fn test_value_node() {
|
||||
let node = ValueNode::new(5);
|
||||
assert_eq!(node.eval(()), &5);
|
||||
let type_erased = &node as &dyn for<'a> Node<'a, (), Output = &'a i32>;
|
||||
assert_eq!(type_erased.eval(()), &5);
|
||||
}
|
||||
#[test]
|
||||
fn test_default_node() {
|
||||
let node = DefaultNode::<u32>::new();
|
||||
assert_eq!(node.eval(42), 0);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use glam::{DAffine2, DVec2};
|
||||
use graphene_core::gradient::GradientStops;
|
||||
use graphene_core::registry::types::Fraction;
|
||||
use graphene_core::registry::types::{Fraction, Percentage, PixelSize, TextArea};
|
||||
use graphene_core::transform::Footprint;
|
||||
use graphene_core::{Color, Ctx, num_traits};
|
||||
@@ -633,35 +634,7 @@ fn logical_not(
|
||||
!input
|
||||
}
|
||||
|
||||
/// Constructs a bool value which may be set to true or false.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn bool_value(_: impl Ctx, _primary: (), #[name("Bool")] bool_value: bool) -> bool {
|
||||
bool_value
|
||||
}
|
||||
|
||||
/// Constructs a number value which may be set to any real number.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn number_value(_: impl Ctx, _primary: (), number: f64) -> f64 {
|
||||
number
|
||||
}
|
||||
|
||||
/// Constructs a number value which may be set to any value from 0% to 100% by dragging the slider.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn percentage_value(_: impl Ctx, _primary: (), percentage: Percentage) -> f64 {
|
||||
percentage
|
||||
}
|
||||
|
||||
/// Constructs a two-dimensional vector value which may be set to any XY coordinate.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn coordinate_value(_: impl Ctx, _primary: (), x: f64, y: f64) -> DVec2 {
|
||||
DVec2::new(x, y)
|
||||
}
|
||||
|
||||
/// Constructs a color value which may be set to any color, or no color.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn color_value(_: impl Ctx, _primary: (), #[default(Color::BLACK)] color: Option<Color>) -> Option<Color> {
|
||||
color
|
||||
}
|
||||
|
||||
/// Gets the color at the specified position along the gradient, given a position from 0 (left) to 1 (right).
|
||||
#[node_macro::node(category("Color"))]
|
||||
@@ -670,26 +643,9 @@ fn sample_gradient(_: impl Ctx, _primary: (), gradient: GradientStops, position:
|
||||
gradient.evaluate(position)
|
||||
}
|
||||
|
||||
/// Constructs a gradient value which may be set to any sequence of color stops to represent the transition between colors.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn gradient_value(_: impl Ctx, _primary: (), gradient: GradientStops) -> GradientStops {
|
||||
gradient
|
||||
}
|
||||
|
||||
/// Constructs a string value which may be set to any plain text.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn string_value(_: impl Ctx, _primary: (), string: TextArea) -> String {
|
||||
string
|
||||
}
|
||||
|
||||
/// Constructs a footprint value which may be set to any transformation of a unit square describing a render area, and a render resolution at least 1x1 integer pixels.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn footprint_value(_: impl Ctx, _primary: (), transform: DAffine2, #[default(100., 100.)] resolution: PixelSize) -> Footprint {
|
||||
Footprint {
|
||||
transform,
|
||||
resolution: resolution.max(DVec2::ONE).as_uvec2(),
|
||||
..Default::default()
|
||||
}
|
||||
#[node_macro::node(category("Math: Vector"))]
|
||||
fn coordinate_from_numbers(_: impl Ctx, _primary: (), #[expose] x: f64, #[expose] y: f64) -> DVec2 {
|
||||
DVec2::new(x, y)
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Math: Vector"))]
|
||||
|
||||
@@ -9,6 +9,7 @@ use graphene_brush::brush_cache::BrushCache;
|
||||
use graphene_brush::brush_stroke::BrushStroke;
|
||||
use graphene_core::raster::Image;
|
||||
use graphene_core::raster_types::CPU;
|
||||
use graphene_core::registry::types::Percentage;
|
||||
use graphene_core::registry::{ChoiceTypeStatic, ChoiceWidgetHint, VariantMetadata};
|
||||
use graphene_core::transform::ReferencePoint;
|
||||
use graphene_core::uuid::NodeId;
|
||||
@@ -21,7 +22,6 @@ use std::hash::Hash;
|
||||
use std::marker::PhantomData;
|
||||
use std::str::FromStr;
|
||||
pub use std::sync::Arc;
|
||||
|
||||
pub struct TaggedValueTypeError;
|
||||
|
||||
/// Macro to generate the tagged value enum.
|
||||
@@ -32,6 +32,7 @@ macro_rules! tagged_value {
|
||||
#[allow(clippy::large_enum_variant)] // TODO(TrueDoctor): Properly solve this disparity between the size of the largest and next largest variants
|
||||
pub enum TaggedValue {
|
||||
None,
|
||||
Percentage(Percentage),
|
||||
$( $(#[$meta] ) *$identifier( $ty ), )*
|
||||
RenderOutput(RenderOutput),
|
||||
SurfaceFrame(SurfaceFrame),
|
||||
@@ -43,6 +44,7 @@ macro_rules! tagged_value {
|
||||
#[repr(u32)]
|
||||
pub enum TaggedValueChoice {
|
||||
None,
|
||||
Percentage,
|
||||
$($identifier,)*
|
||||
}
|
||||
|
||||
@@ -50,12 +52,14 @@ macro_rules! tagged_value {
|
||||
pub fn to_tagged_value(&self) -> TaggedValue {
|
||||
match self {
|
||||
TaggedValueChoice::None => TaggedValue::None,
|
||||
TaggedValueChoice::Percentage => TaggedValue::Percentage(0.),
|
||||
$(TaggedValueChoice::$identifier => TaggedValue::$identifier(Default::default()),)*
|
||||
}
|
||||
}
|
||||
pub fn from_tagged_value(value: &TaggedValue) -> Option<Self> {
|
||||
match value {
|
||||
TaggedValue::None => Some(TaggedValueChoice::None),
|
||||
TaggedValue::Percentage(_) => Some(TaggedValueChoice::Percentage),
|
||||
$( TaggedValue::$identifier(_) => Some(TaggedValueChoice::$identifier), )*
|
||||
_ => None
|
||||
}
|
||||
@@ -71,7 +75,7 @@ macro_rules! tagged_value {
|
||||
|
||||
const COUNT: usize = 0 $( + one!($identifier) )*;
|
||||
// Define static array of (choice, metadata) tuples
|
||||
static VALUES: [(TaggedValueChoice, VariantMetadata); 1 + COUNT] = [
|
||||
static VALUES: [(TaggedValueChoice, VariantMetadata); 2 + COUNT] = [
|
||||
|
||||
(TaggedValueChoice::None, VariantMetadata {
|
||||
name: Cow::Borrowed(stringify!(None)),
|
||||
@@ -79,6 +83,12 @@ macro_rules! tagged_value {
|
||||
docstring: None,
|
||||
icon: None,
|
||||
}),
|
||||
(TaggedValueChoice::Percentage, VariantMetadata {
|
||||
name: Cow::Borrowed(stringify!(Percentage)),
|
||||
label: Cow::Borrowed(stringify!(Percentage)),
|
||||
docstring: None,
|
||||
icon: None,
|
||||
}),
|
||||
$(
|
||||
(TaggedValueChoice::$identifier, VariantMetadata {
|
||||
name: Cow::Borrowed(stringify!($identifier)),
|
||||
@@ -105,6 +115,7 @@ macro_rules! tagged_value {
|
||||
core::mem::discriminant(self).hash(state);
|
||||
match self {
|
||||
Self::None => {}
|
||||
Self::Percentage(x) => {x.hash(state)},
|
||||
$( Self::$identifier(x) => {x.hash(state)}),*
|
||||
Self::RenderOutput(x) => x.hash(state),
|
||||
Self::SurfaceFrame(x) => x.hash(state),
|
||||
@@ -117,6 +128,7 @@ macro_rules! tagged_value {
|
||||
pub fn to_dynany(self) -> DAny<'a> {
|
||||
match self {
|
||||
Self::None => Box::new(()),
|
||||
Self::Percentage(x) => Box::new(x),
|
||||
$( Self::$identifier(x) => Box::new(x), )*
|
||||
Self::RenderOutput(x) => Box::new(x),
|
||||
Self::SurfaceFrame(x) => Box::new(x),
|
||||
@@ -127,6 +139,7 @@ macro_rules! tagged_value {
|
||||
pub fn to_any(self) -> Arc<dyn std::any::Any + Send + Sync + 'static> {
|
||||
match self {
|
||||
Self::None => Arc::new(()),
|
||||
Self::Percentage(x) => Arc::new(x),
|
||||
$( Self::$identifier(x) => Arc::new(x), )*
|
||||
Self::RenderOutput(x) => Arc::new(x),
|
||||
Self::SurfaceFrame(x) => Arc::new(x),
|
||||
@@ -137,6 +150,7 @@ macro_rules! tagged_value {
|
||||
pub fn ty(&self) -> Type {
|
||||
match self {
|
||||
Self::None => concrete!(()),
|
||||
Self::Percentage(_) => concrete!(Percentage),
|
||||
$( Self::$identifier(_) => concrete!($ty), )*
|
||||
Self::RenderOutput(_) => concrete!(RenderOutput),
|
||||
Self::SurfaceFrame(_) => concrete!(SurfaceFrame),
|
||||
@@ -153,8 +167,6 @@ macro_rules! tagged_value {
|
||||
$( x if x == TypeId::of::<$ty>() => Ok(TaggedValue::$identifier(*downcast(input).unwrap())), )*
|
||||
x if x == TypeId::of::<RenderOutput>() => Ok(TaggedValue::RenderOutput(*downcast(input).unwrap())),
|
||||
x if x == TypeId::of::<SurfaceFrame>() => Ok(TaggedValue::SurfaceFrame(*downcast(input).unwrap())),
|
||||
|
||||
|
||||
_ => Err(format!("Cannot convert {:?} to TaggedValue", DynAny::type_name(input.as_ref()))),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user