aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2025-12-26 22:42:39 +0000
committers-ol <s+removethis@s-ol.nu>2025-12-26 22:42:39 +0000
commit00030c6b88dec5a86e3fc9518d626f2aa421141d (patch)
tree2687b2684957b69b2aee2eb4b4cd5eddd2290816
parentcompile and view! (diff)
downloadnodetoy-00030c6b88dec5a86e3fc9518d626f2aa421141d.tar.gz
nodetoy-00030c6b88dec5a86e3fc9518d626f2aa421141d.zip
working literals
-rw-r--r--Cargo.lock125
-rw-r--r--Cargo.toml2
-rw-r--r--src/library.rs329
-rw-r--r--src/main.rs71
-rw-r--r--src/preview.rs2
5 files changed, 276 insertions, 253 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6dd31b6..542f5ff 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -947,20 +947,6 @@ dependencies = [
]
[[package]]
-name = "egui_extras"
-version = "0.33.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d01d34e845f01c62e3fded726961092e70417d66570c499b9817ab24674ca4ed"
-dependencies = [
- "ahash",
- "egui",
- "enum-map",
- "log",
- "mime_guess2",
- "profiling",
-]
-
-[[package]]
name = "egui_glow"
version = "0.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1000,26 +986,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099"
[[package]]
-name = "enum-map"
-version = "2.7.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6866f3bfdf8207509a033af1a75a7b08abda06bbaaeae6669323fd5a097df2e9"
-dependencies = [
- "enum-map-derive",
-]
-
-[[package]]
-name = "enum-map-derive"
-version = "0.17.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f282cfdfe92516eb26c2af8589c274c7c17681f5ecc03c18255fe741c6aa64eb"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
-[[package]]
name = "enum_dispatch"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1829,24 +1795,6 @@ dependencies = [
]
[[package]]
-name = "mime"
-version = "0.3.17"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
-
-[[package]]
-name = "mime_guess2"
-version = "2.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1706dc14a2e140dec0a7a07109d9a3d5890b81e85bd6c60b906b249a77adf0ca"
-dependencies = [
- "mime",
- "phf",
- "phf_shared",
- "unicase",
-]
-
-[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1943,9 +1891,7 @@ dependencies = [
"bytemuck",
"eframe",
"egui",
- "egui-probe",
"egui-snarl",
- "egui_extras",
"enum_dispatch",
"itertools",
"log",
@@ -2360,50 +2306,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
-name = "phf"
-version = "0.11.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078"
-dependencies = [
- "phf_macros",
- "phf_shared",
-]
-
-[[package]]
-name = "phf_generator"
-version = "0.11.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
-dependencies = [
- "phf_shared",
- "rand",
-]
-
-[[package]]
-name = "phf_macros"
-version = "0.11.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f84ac04429c13a7ff43785d75ad27569f2951ce0ffd30a3321230db2fc727216"
-dependencies = [
- "phf_generator",
- "phf_shared",
- "proc-macro2",
- "quote",
- "syn",
- "unicase",
-]
-
-[[package]]
-name = "phf_shared"
-version = "0.11.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5"
-dependencies = [
- "siphasher",
- "unicase",
-]
-
-[[package]]
name = "pin-project"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2609,21 +2511,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
-name = "rand"
-version = "0.8.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
-dependencies = [
- "rand_core",
-]
-
-[[package]]
-name = "rand_core"
-version = "0.6.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
-
-[[package]]
name = "range-alloc"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2828,12 +2715,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
[[package]]
-name = "siphasher"
-version = "1.0.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
-
-[[package]]
name = "slab"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3153,12 +3034,6 @@ dependencies = [
]
[[package]]
-name = "unicase"
-version = "2.8.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
-
-[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 28aa7bf..010ca38 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,9 +7,7 @@ edition = "2024"
bytemuck = "1.24.0"
eframe = { version = "0.33", features = ["accesskit", "default_fonts", "wayland", "x11", "serde", "persistence", "wgpu"], default-features = false }
egui = "0.33"
-egui-probe = { version = "0.10", features = ["derive"] }
egui-snarl = { version = "0.9", features = ["serde", "egui-probe"] }
-egui_extras = { version = "0.33" }
enum_dispatch = "0.3.13"
itertools = "0.14.0"
log = { version = "0.4.29", features = ["serde", "std"] }
diff --git a/src/library.rs b/src/library.rs
index d20a133..ba03dd6 100644
--- a/src/library.rs
+++ b/src/library.rs
@@ -52,16 +52,16 @@ trait FixedNode: fmt::Debug {
outputs: Vec<String>,
f: &mut dyn fmt::Write,
) -> fmt::Result {
- for i in 1..signature.outputs.len() {
- write!(f, "{} {};\n", signature.outputs[i], outputs[i])?;
+ let mut pairs = signature.outputs.iter().zip(outputs.iter());
+ let (out_typ, out_name) = pairs.next().expect("no output");
+
+ for (typ, name) in pairs {
+ writeln!(f, "{typ} {name};")?;
}
let func = format!("{self:?}").to_lowercase();
- let out_typ = &signature.outputs[0];
- let mut outputs = outputs.into_iter();
- let out_name = outputs.next().expect("empty output");
- let params = inputs.into_iter().chain(outputs).join(", ");
- write!(f, "{out_typ} {out_name} = {func}({params});\n")
+ let params = inputs.iter().chain(outputs.iter().skip(1)).join(", ");
+ writeln!(f, "{out_typ} {out_name} = {func}({params});")
}
}
@@ -152,13 +152,7 @@ pub enum BinArithmetic {
}
impl BinArithmetic {
pub fn all() -> impl Iterator<Item = Self> {
- [
- BinArithmetic::Add,
- BinArithmetic::Subtract,
- BinArithmetic::Multiply,
- BinArithmetic::Divide,
- ]
- .into_iter()
+ [Self::Add, Self::Subtract, Self::Multiply, Self::Divide].into_iter()
}
}
impl FixedNode for BinArithmetic {
@@ -195,12 +189,12 @@ impl FixedNode for BinArithmetic {
let left = &inputs[0];
let right = &inputs[1];
let sym = match self {
- BinArithmetic::Add => "+",
- BinArithmetic::Subtract => "-",
- BinArithmetic::Multiply => "*",
- BinArithmetic::Divide => "/",
+ Self::Add => "+",
+ Self::Subtract => "-",
+ Self::Multiply => "*",
+ Self::Divide => "/",
};
- write!(f, "{out_typ} {out_name} = {left} {sym} {right};\n")
+ writeln!(f, "{out_typ} {out_name} = {left} {sym} {right};")
}
}
@@ -219,14 +213,14 @@ pub enum Thru1 {
impl Thru1 {
pub fn all() -> impl Iterator<Item = Self> {
[
- Thru1::Abs,
- Thru1::Sign,
- Thru1::Floor,
- Thru1::Trunc,
- Thru1::Round,
- Thru1::RoundEven,
- Thru1::Ceil,
- Thru1::Fract,
+ Self::Abs,
+ Self::Sign,
+ Self::Floor,
+ Self::Trunc,
+ Self::Round,
+ Self::RoundEven,
+ Self::Ceil,
+ Self::Fract,
]
.into_iter()
}
@@ -302,7 +296,7 @@ pub enum MinMax {
}
impl MinMax {
pub fn all() -> impl Iterator<Item = Self> {
- [MinMax::Min, MinMax::Max].into_iter()
+ [Self::Min, Self::Max].into_iter()
}
}
impl FixedNode for MinMax {
@@ -398,26 +392,26 @@ pub enum BuiltinFunction {
impl BuiltinFunction {
pub fn all() -> impl Iterator<Item = Self> {
BinArithmetic::all()
- .map(BuiltinFunction::from)
- .chain(Thru1::all().map(BuiltinFunction::from))
- .chain(Mod::all().map(BuiltinFunction::from))
- .chain(Modf::all().map(BuiltinFunction::from))
- .chain(MinMax::all().map(BuiltinFunction::from))
- .chain(Clamp::all().map(BuiltinFunction::from))
- .chain(Mix::all().map(BuiltinFunction::from))
+ .map(Self::from)
+ .chain(Thru1::all().map(Self::from))
+ .chain(Mod::all().map(Self::from))
+ .chain(Modf::all().map(Self::from))
+ .chain(MinMax::all().map(Self::from))
+ .chain(Clamp::all().map(Self::from))
+ .chain(Mix::all().map(Self::from))
}
}
impl fmt::Display for BuiltinFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
- BuiltinFunction::BinArithmetic(x) => fmt::Debug::fmt(x, f),
- BuiltinFunction::Thru1(x) => fmt::Debug::fmt(x, f),
- BuiltinFunction::Mod(x) => fmt::Debug::fmt(x, f),
- BuiltinFunction::Modf(x) => fmt::Debug::fmt(x, f),
- BuiltinFunction::MinMax(x) => fmt::Debug::fmt(x, f),
- BuiltinFunction::Clamp(x) => fmt::Debug::fmt(x, f),
- BuiltinFunction::Mix(x) => fmt::Debug::fmt(x, f),
+ Self::BinArithmetic(x) => fmt::Debug::fmt(x, f),
+ Self::Thru1(x) => fmt::Debug::fmt(x, f),
+ Self::Mod(x) => fmt::Debug::fmt(x, f),
+ Self::Modf(x) => fmt::Debug::fmt(x, f),
+ Self::MinMax(x) => fmt::Debug::fmt(x, f),
+ Self::Clamp(x) => fmt::Debug::fmt(x, f),
+ Self::Mix(x) => fmt::Debug::fmt(x, f),
}
}
}
@@ -449,10 +443,10 @@ impl FixedNode for Output {
f: &mut dyn fmt::Write,
) -> fmt::Result {
match signature.inputs[0] {
- Vector(_, D2) => write!(f, "out_color = vec4({}, 0.0, 1.0);\n", inputs[0]),
- Vector(_, D3) => write!(f, "out_color = vec4({}, 1.0);\n", inputs[0]),
- Vector(_, D4) => write!(f, "out_color = {};\n", inputs[0]),
- Scalar(_) => write!(f, "out_color = vec4(vec3({}), 1.0);\n", inputs[0]),
+ Vector(_, D2) => writeln!(f, "out_color = vec4({}, 0.0, 1.0);", inputs[0]),
+ Vector(_, D3) => writeln!(f, "out_color = vec4({}, 1.0);", inputs[0]),
+ Vector(_, D4) => writeln!(f, "out_color = {};", inputs[0]),
+ Scalar(_) => writeln!(f, "out_color = vec4(vec3({}), 1.0);", inputs[0]),
_ => unreachable!("invalid output type"),
}
}
@@ -468,13 +462,131 @@ impl fmt::Display for Output {
}
}
-#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
-pub struct Constant {
- typ: Type,
- value: String,
+trait Value: fmt::Display {
+ const TYPE: Type;
+}
+
+trait ScalarValue: fmt::Display {
+ type Raw;
+ const SCALAR_TYPE: crate::ScalarType;
+
+ fn make_widget(val: &mut Self::Raw) -> impl egui::Widget;
+}
+
+impl<T: ScalarValue> Value for T {
+ const TYPE: Type = Scalar(Self::SCALAR_TYPE);
+}
+
+trait ValueEditor: Value {
+ fn show_editor(&mut self, ui: &mut egui::Ui);
}
-impl ConcreteNode for Constant {
+#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
+pub struct Constant<T>(T);
+
+// scalar implementations
+impl ScalarValue for Constant<f32> {
+ type Raw = f32;
+ const SCALAR_TYPE: crate::ScalarType = Float(Single);
+ fn make_widget(val: &mut Self::Raw) -> impl egui::Widget {
+ egui::DragValue::new(val).speed(0.1)
+ }
+}
+impl ScalarValue for Constant<f64> {
+ type Raw = f64;
+ const SCALAR_TYPE: crate::ScalarType = Float(Double);
+ fn make_widget(val: &mut Self::Raw) -> impl egui::Widget {
+ egui::DragValue::new(val).speed(0.1)
+ }
+}
+impl ScalarValue for Constant<i32> {
+ type Raw = i32;
+ const SCALAR_TYPE: crate::ScalarType = Int;
+ fn make_widget(val: &mut Self::Raw) -> impl egui::Widget {
+ egui::DragValue::new(val)
+ }
+}
+impl ScalarValue for Constant<u32> {
+ type Raw = u32;
+ const SCALAR_TYPE: crate::ScalarType = UInt;
+ fn make_widget(val: &mut Self::Raw) -> impl egui::Widget {
+ egui::DragValue::new(val)
+ }
+}
+impl ScalarValue for Constant<bool> {
+ type Raw = bool;
+ const SCALAR_TYPE: crate::ScalarType = Bool;
+ fn make_widget(val: &mut Self::Raw) -> impl egui::Widget {
+ egui::Checkbox::without_text(val)
+ }
+}
+
+impl<T> fmt::Display for Constant<T>
+where
+ Constant<T>: ScalarValue<Raw = T>,
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
+impl<T> ValueEditor for Constant<T>
+where
+ Constant<T>: ScalarValue<Raw = T>,
+ T: fmt::Display,
+{
+ fn show_editor(&mut self, ui: &mut egui::Ui) {
+ ui.label("value");
+ ui.add(Self::make_widget(&mut self.0));
+ }
+}
+
+impl<T, const N: usize> Value for Constant<[T; N]>
+where
+ Constant<T>: ScalarValue,
+ T: fmt::Display,
+{
+ const TYPE: Type = Vector(Constant::<T>::SCALAR_TYPE, crate::Dimension::from(N));
+}
+
+impl<T, const N: usize> ValueEditor for Constant<[T; N]>
+where
+ Constant<T>: ScalarValue<Raw = T>,
+ T: fmt::Display,
+{
+ fn show_editor(&mut self, ui: &mut egui::Ui) {
+ const LABELS: [&str; 4] = ["x", "y", "z", "w"];
+ ui.vertical(|ui| {
+ for i in 0..self.0.len() {
+ ui.horizontal(|ui| {
+ ui.label(LABELS[i]);
+ ui.add(Constant::<T>::make_widget(&mut self.0[i]));
+ });
+ }
+ });
+ }
+}
+
+impl<T, const N: usize> fmt::Display for Constant<[T; N]>
+where
+ Constant<[T; N]>: Value,
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(
+ f,
+ "{}({})",
+ Self::TYPE,
+ self.0.iter().map(|v| format!("{}", v)).join(", ")
+ )
+ }
+}
+
+impl<T> ConcreteNode for Constant<T>
+where
+ Constant<T>: ValueEditor,
+{
fn inputs(&self) -> usize {
0
}
@@ -487,55 +599,102 @@ impl ConcreteNode for Constant {
}
fn signature(&self, _connected: &[Option<Type>]) -> TypeSignature {
- TypeSignature::new([], [self.typ])
+ TypeSignature::new([], [Self::TYPE])
}
fn compile(
&self,
- signature: TypeSignature,
+ _signature: TypeSignature,
_inputs: Vec<String>,
outputs: Vec<String>,
f: &mut dyn fmt::Write,
) -> fmt::Result {
- let out_typ = &signature.outputs[0];
+ let out_typ = Self::TYPE;
let out_name = &outputs[0];
- write!(f, "{out_typ} {out_name} = {};\n", self.value)
+ writeln!(f, "{out_typ} {out_name} = {};", self)
+ }
+
+ fn show_body(&mut self, ui: &mut egui::Ui) {
+ self.show_editor(ui);
}
}
-impl Constant {
- pub fn all() -> impl Iterator<Item = Constant> {
+
+#[enum_dispatch(ConcreteNode)]
+#[derive(Clone, PartialEq, Serialize, Deserialize)]
+pub enum AnyConstant {
+ Float(Constant<f32>),
+ Vec2(Constant<[f32; 2]>),
+ Vec3(Constant<[f32; 3]>),
+ Vec4(Constant<[f32; 4]>),
+ Double(Constant<f64>),
+ DVec2(Constant<[f64; 2]>),
+ DVec3(Constant<[f64; 3]>),
+ DVec4(Constant<[f64; 4]>),
+ Int(Constant<i32>),
+ IVec2(Constant<[i32; 2]>),
+ IVec3(Constant<[i32; 3]>),
+ IVec4(Constant<[i32; 4]>),
+ UInt(Constant<u32>),
+ UVec2(Constant<[u32; 2]>),
+ UVec3(Constant<[u32; 3]>),
+ UVec4(Constant<[u32; 4]>),
+ Bool(Constant<bool>),
+ BVec2(Constant<[bool; 2]>),
+ BVec3(Constant<[bool; 3]>),
+ BVec4(Constant<[bool; 4]>),
+}
+
+impl AnyConstant {
+ pub fn all() -> impl Iterator<Item = Self> {
[
- Constant {
- typ: Scalar(Float(Single)),
- value: "0.0".to_string(),
- },
- Constant {
- typ: Vector(Float(Single), D2),
- value: "vec2(0)".to_string(),
- },
- Constant {
- typ: Vector(Float(Single), D3),
- value: "vec3(0)".to_string(),
- },
- Constant {
- typ: Vector(Float(Single), D4),
- value: "vec4(0)".to_string(),
- },
- Constant {
- typ: Scalar(Int),
- value: "0".to_string(),
- },
- Constant {
- typ: Scalar(UInt),
- value: "0".to_string(),
- },
+ Self::Float(Default::default()),
+ Self::Vec2(Default::default()),
+ Self::Vec3(Default::default()),
+ Self::Vec4(Default::default()),
+ Self::Double(Default::default()),
+ Self::DVec2(Default::default()),
+ Self::DVec3(Default::default()),
+ Self::DVec4(Default::default()),
+ Self::Int(Default::default()),
+ Self::IVec2(Default::default()),
+ Self::IVec3(Default::default()),
+ Self::IVec4(Default::default()),
+ Self::UInt(Default::default()),
+ Self::UVec2(Default::default()),
+ Self::UVec3(Default::default()),
+ Self::UVec4(Default::default()),
+ Self::Bool(Default::default()),
+ Self::BVec2(Default::default()),
+ Self::BVec3(Default::default()),
+ Self::BVec4(Default::default()),
]
.into_iter()
}
}
-impl fmt::Display for Constant {
+impl fmt::Display for AnyConstant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "Constant {}", self.typ)
+ match self {
+ Self::Float(_) => write!(f, "Float"),
+ Self::Vec2(_) => write!(f, "Vec2"),
+ Self::Vec3(_) => write!(f, "Vec3"),
+ Self::Vec4(_) => write!(f, "Vec4"),
+ Self::Double(_) => write!(f, "Double"),
+ Self::DVec2(_) => write!(f, "DVec2"),
+ Self::DVec3(_) => write!(f, "DVec3"),
+ Self::DVec4(_) => write!(f, "DVec4"),
+ Self::Int(_) => write!(f, "Integer"),
+ Self::IVec2(_) => write!(f, "IVec2"),
+ Self::IVec3(_) => write!(f, "IVec3"),
+ Self::IVec4(_) => write!(f, "IVec4"),
+ Self::UInt(_) => write!(f, "Unsigned Integer"),
+ Self::UVec2(_) => write!(f, "UVec2"),
+ Self::UVec3(_) => write!(f, "UVec3"),
+ Self::UVec4(_) => write!(f, "UVec4"),
+ Self::Bool(_) => write!(f, "Boolean"),
+ Self::BVec2(_) => write!(f, "BVec2"),
+ Self::BVec3(_) => write!(f, "BVec3"),
+ Self::BVec4(_) => write!(f, "BVec4"),
+ }
}
}
@@ -570,9 +729,9 @@ impl ConcreteNode for Input {
) -> fmt::Result {
let out_typ = &signature.outputs[0];
let out_name = &outputs[0];
- write!(
+ writeln!(
f,
- "{out_typ} {out_name} = {};\n",
+ "{out_typ} {out_name} = {};",
match self {
Input::UV => "in_uv",
}
diff --git a/src/main.rs b/src/main.rs
index 5e1632b..3213501 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,9 +4,7 @@ use eframe::{App, CreationContext};
use egui::{Color32, Id, Ui};
use egui_snarl::{
InPin, InPinId, NodeId, OutPin, OutPinId, Snarl,
- ui::{
- NodeLayout, PinInfo, PinPlacement, SnarlStyle, SnarlViewer, SnarlWidget, get_selected_nodes,
- },
+ ui::{NodeLayout, PinInfo, PinPlacement, SnarlStyle, SnarlViewer, SnarlWidget},
};
use enum_dispatch::enum_dispatch;
use log::*;
@@ -38,6 +36,16 @@ impl fmt::Display for Dimension {
write!(f, "{}", *self as u8)
}
}
+impl Dimension {
+ const fn from(v: usize) -> Dimension {
+ match v {
+ 2 => Self::D2,
+ 3 => Self::D3,
+ 4 => Self::D4,
+ _ => panic!("invalid dimension value"),
+ }
+ }
+}
#[derive(Copy, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
enum Type {
@@ -129,6 +137,8 @@ trait ConcreteNode {
outputs: Vec<String>,
f: &mut dyn fmt::Write,
) -> fmt::Result;
+
+ fn show_body(&mut self, _ui: &mut egui::Ui) {}
}
impl TypeSignature {
@@ -157,7 +167,7 @@ const UNTYPED_COLOR: Color32 = Color32::from_rgb(0xb0, 0xb0, 0xb0);
#[enum_dispatch(ConcreteNode)]
#[derive(Clone, PartialEq, serde::Serialize, serde::Deserialize)]
enum DemoNode {
- Constant(library::Constant),
+ Constant(library::AnyConstant),
Input(library::Input),
Builtin(library::BuiltinFunction),
Output(library::Output),
@@ -211,7 +221,7 @@ impl DemoNode {
}
pub fn all() -> impl Iterator<Item = Self> {
- (library::Constant::all().map(DemoNode::from))
+ (library::AnyConstant::all().map(DemoNode::from))
.chain(library::Input::all().map(DemoNode::from))
.chain(library::Output::all().map(DemoNode::from))
.chain(library::BuiltinFunction::all().map(DemoNode::from))
@@ -262,7 +272,7 @@ void main() {{
while let Some(id) = order.pop() {
snarl[id].compile_node(id, snarl, f)?;
}
- write!(f, "}}\n")?;
+ writeln!(f, "}}")?;
Ok(())
}
@@ -327,6 +337,21 @@ impl SnarlViewer<DemoNode> for DemoViewer {
})
}
+ fn has_body(&mut self, _node: &DemoNode) -> bool {
+ true
+ }
+
+ fn show_body(
+ &mut self,
+ node: NodeId,
+ _inputs: &[InPin],
+ _outputs: &[OutPin],
+ ui: &mut Ui,
+ snarl: &mut Snarl<DemoNode>,
+ ) {
+ snarl[node].show_body(ui);
+ }
+
fn has_graph_menu(&mut self, _pos: egui::Pos2, _snarl: &mut Snarl<DemoNode>) -> bool {
true
}
@@ -399,8 +424,6 @@ const fn default_style() -> SnarlStyle {
impl DemoApp {
pub fn new(cx: &CreationContext) -> Self {
- egui_extras::install_image_loaders(&cx.egui_ctx);
-
cx.egui_ctx.style_mut(|style| style.animation_time *= 10.0);
let snarl = cx.storage.map_or_else(Snarl::new, |storage| {
@@ -444,38 +467,6 @@ impl App for DemoApp {
});
});
- egui::SidePanel::left("selected-list").show(ctx, |ui| {
- egui::ScrollArea::vertical().show(ui, |ui| {
- ui.strong("Selected nodes");
-
- let selected = get_selected_nodes(Id::new("snarl-demo"), ui.ctx());
-
- let mut selected = selected
- .into_iter()
- .map(|id| (id, &self.snarl[id]))
- .collect::<Vec<_>>();
-
- selected.sort_by_key(|(id, _)| *id);
-
- let mut remove = None;
-
- for (id, node) in selected {
- ui.horizontal(|ui| {
- ui.label(format!("{id:?}"));
- ui.label(format!("{node}"));
- ui.add_space(ui.spacing().item_spacing.x);
- if ui.button("Remove").clicked() {
- remove = Some(id);
- }
- });
- }
-
- if let Some(id) = remove {
- self.snarl.remove_node(id);
- }
- });
- });
-
egui::SidePanel::right("preview").show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
egui::Frame::canvas(ui.style()).show(ui, |ui| {
diff --git a/src/preview.rs b/src/preview.rs
index e4f7413..abd9611 100644
--- a/src/preview.rs
+++ b/src/preview.rs
@@ -78,7 +78,7 @@ impl egui_wgpu::CallbackTrait for CustomTriangleCallback {
if let Some(code) = &self.frag_shader {
let format: wgpu::TextureFormat = *resources.get().unwrap();
let triangle_resources: &mut TriangleRenderResources = resources.get_mut().unwrap();
- *triangle_resources = TriangleRenderResources::init_with_shader(device, format, &code);
+ *triangle_resources = TriangleRenderResources::init_with_shader(device, format, code);
triangle_resources
} else {
let triangle_resources: &mut TriangleRenderResources = resources.get_mut().unwrap();