diff --git a/CMakeLists.txt b/CMakeLists.txt index fc1d08d8..af238143 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 3.20) project(Caffeine VERSION 0.1.0 LANGUAGES CXX C) +# Third-party static libs (Bullet, Lua, ...) end up inside libcaffeine-capi.so, so every +# object must be position independent. +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) @@ -462,11 +466,19 @@ if(CAFFEINE_ENABLE_SCRIPTING) src/caffeine/script/RuntimeScriptBindings.cpp src/caffeine/script/AssetTextLoader.cpp src/caffeine/script/NativeModuleHost.cpp + src/caffeine/script/WorldApiBindings.cpp + src/caffeine/script/Physics3DBindings.cpp + src/caffeine/script/AudioSceneBindings.cpp + src/caffeine/audio/AudioService.cpp ) if(DRYAD_AVAILABLE) target_sources(caffeine-core PRIVATE src/caffeine/script/DryadHost.cpp src/caffeine/script/DryadComponentBindings.cpp + src/caffeine/script/DryadRuntimeBindings.cpp + src/caffeine/script/DryadWorldBindings.cpp + src/caffeine/script/DryadPhysics3DBindings.cpp + src/caffeine/script/DryadAudioSceneBindings.cpp src/caffeine/script/nodes/NodeGraph.cpp src/caffeine/script/nodes/NodeCatalog.cpp src/caffeine/script/nodes/NodeEmitter.cpp diff --git a/apps/runtime/main.cpp b/apps/runtime/main.cpp index 8d18a598..3faf7ff4 100644 --- a/apps/runtime/main.cpp +++ b/apps/runtime/main.cpp @@ -6,6 +6,8 @@ #include "events/Events.hpp" #include "input/InputManager.hpp" #include "physics/PhysicsSystem2D.hpp" +#include "audio/AudioService.hpp" +#include "physics/PhysicsSystem3D.hpp" #include "script/ScriptEngine.hpp" #include "script/ScriptSystem.hpp" #include "scene/NaturalFluidSystem.hpp" @@ -461,6 +463,10 @@ int main(int argc, char** argv) { Caffeine::Input::InputManager input; Caffeine::Events::EventBus eventBus; Caffeine::Physics2D::PhysicsSystem2D physics(&eventBus); + Caffeine::Physics3D::PhysicsSystem3D physics3d; + Caffeine::Audio::AudioService audio; + audio.setSearchRoot(buildRoot.string()); + if (!audio.init()) std::fprintf(stderr, "caffeine-runtime: no audio device, sound disabled\n"); Caffeine::Animation::AnimationSystem animation; Caffeine::UI::UISystem uiSystem(&eventBus); Caffeine::Script::ScriptEngine scriptEngine; @@ -469,8 +475,11 @@ int main(int argc, char** argv) { scriptParams.input = &input; scriptParams.events = &eventBus; scriptParams.ui = &uiSystem; + scriptParams.physics3d = &physics3d.world(); + scriptParams.audio = &audio; const bool scriptsReady = scriptEngine.init(scriptParams); scriptEngine.setSearchRoot(buildRoot.string()); + scriptEngine.sceneRequests().setCurrent(config.LastScene); Caffeine::Script::ScriptSystem scriptSystem(scriptsReady ? &scriptEngine : nullptr, &eventBus); if (!config.RootPath.empty()) { scriptSystem.setProjectRoot(config.RootPath.string()); @@ -549,11 +558,41 @@ int main(int argc, char** argv) { Caffeine::Scene::tickNaturalFluids(world, dt, false); Caffeine::Navigation::updateNavigation(world, dt); physics.onUpdate(world, dt); + physics3d.onUpdate(world, dt); if (scriptsReady) { scriptEngine.setWorld(&world); scriptEngine.setInput(&input); scriptSystem.onUpdate(world, dt); + + // A script asked for another scene: swap it now, between frames' scripts. + auto& sceneRequests = scriptEngine.sceneRequests(); + if (sceneRequests.hasPending()) { + const std::string requested = sceneRequests.take(); + std::filesystem::path next(requested); + if (!std::filesystem::exists(next)) next = buildRoot / requested; + if (std::filesystem::exists(next)) { + physics3d.reset(); + Caffeine::Editor::SceneSerializer serializer(world); + if (serializer.deserialize(next.string())) { + Caffeine::Scene::resetNaturalFluidRuntime(); + scriptSystem.resetPlayState(); + audio.stopAll(); + sceneRequests.setCurrent(requested); + scenePath = next; + sceneLoaded = true; +#ifdef CF_HAS_IMGUI + editorCtx.currentScenePath = scenePath.string(); +#endif + std::printf("Loaded scene: %s\n", next.string().c_str()); + } else { + std::fprintf(stderr, "caffeine-runtime: scene failed to load: %s\n", next.string().c_str()); + } + } else { + std::fprintf(stderr, "caffeine-runtime: scene not found: %s\n", requested.c_str()); + } + } } + audio.update(dt); Caffeine::Procedural::ProceduralWorldSystem::update(world); uiSystem.onUpdate(world, dt); } diff --git a/caf-pack b/caf-pack index 8a666aef..dbb2a3b5 160000 --- a/caf-pack +++ b/caf-pack @@ -1 +1 @@ -Subproject commit 8a666aef78855b1fdb15e647f9823b78ce51def8 +Subproject commit dbb2a3b5d8c382bdb0549cf93e818058d41e0825 diff --git a/caffeine-website b/caffeine-website index efd4718c..75fe8fc1 160000 --- a/caffeine-website +++ b/caffeine-website @@ -1 +1 @@ -Subproject commit efd4718c3f81150f84a8f5320a54afbe69db2752 +Subproject commit 75fe8fc17e9fd1d897ba73764a836919063ae860 diff --git a/doppio/Cargo.lock b/doppio/Cargo.lock index 9ee18833..cf7da8ed 100644 --- a/doppio/Cargo.lock +++ b/doppio/Cargo.lock @@ -1723,6 +1723,7 @@ dependencies = [ "iced_aw", "iced_font_awesome", "rust-i18n", + "serde_json", "syntect", "unicode-width", "web-time", diff --git a/doppio/crates/ui/Cargo.toml b/doppio/crates/ui/Cargo.toml index b881f57a..83d9d4c7 100644 --- a/doppio/crates/ui/Cargo.toml +++ b/doppio/crates/ui/Cargo.toml @@ -23,7 +23,7 @@ roxmltree = "0.20" serde = { version = "1", features = ["derive"] } serde_json.workspace = true zip = { version = "9.0", default-features = false, features = ["deflate"] } -iced-code-editor = "0.5.3" +iced-code-editor = { version = "0.5.3", features = ["lsp-process"] } iced_test = { workspace = true, optional = true } fontdue = { version = "0.9", optional = true } diff --git a/doppio/crates/ui/src/bin/doppio-material.rs b/doppio/crates/ui/src/bin/doppio-material.rs index 66deb042..b70d3274 100644 --- a/doppio/crates/ui/src/bin/doppio-material.rs +++ b/doppio/crates/ui/src/bin/doppio-material.rs @@ -19,7 +19,7 @@ fn usage() -> ! { doppio-material texset-export --out DIR [--preset NAME]\n \ doppio-material bake-preset --out DIR [--size 512] [--depth 0.04] [--list|--slugs]\n \ doppio-material nature --out DIR [--quality 0|1|2] [--seed N] [--list]\n \ - doppio-material bake-texture --out DIR [--size 512] [--bits 8|16] [--flipbook CxR] [--list|--slugs]" + doppio-material variants --out DIR [--count 4] [--seed 1] [--quality 0|1|2]\n doppio-material bake-texture --out DIR [--size 512] [--bits 8|16] [--flipbook CxR] [--list|--slugs]" ); std::process::exit(2); } @@ -41,6 +41,7 @@ fn main() { "bake-preset" => bake_preset(&args[1..]), "bake-texture" => bake_texture(&args[1..]), "nature" => nature_models(&args[1..]), + "variants" => recipe_variants(&args[1..]), _ => usage(), }; if let Err(error) = result { @@ -570,6 +571,37 @@ fn bake_texture(args: &[String]) -> Result<(), String> { } /// Exports Nature Generator presets as `.obj` files (one per part) plus their `.nature` recipe. +/// `variants --out DIR [--count 4] [--seed 1] [--quality 0|1|2]`: +/// bakes seeded variants of a nature recipe into a content-addressed cache folder. +fn recipe_variants(args: &[String]) -> Result<(), String> { + use doppio_ui::uikit::{nature, recipe_ref}; + let recipe = positional(args).ok_or_else(|| "name a .generator.nature file".to_owned())?; + let out = argument(args, "--out") + .map(PathBuf::from) + .ok_or_else(|| "expected --out DIR".to_owned())?; + let count: u32 = argument(args, "--count") + .and_then(|value| value.parse().ok()) + .unwrap_or(4); + let seed: u64 = argument(args, "--seed") + .and_then(|value| value.parse().ok()) + .unwrap_or(1); + let quality = argument(args, "--quality") + .and_then(|value| value.parse::().ok()) + .map(nature::Quality::from_index); + for baked in + recipe_ref::nature_variants(std::path::Path::new(recipe), seed, count, quality, &out)? + { + println!( + "{:016x} seed {:<5} {} {}", + baked.key, + baked.seed, + if baked.cached { "cached" } else { "baked " }, + baked.directory.display() + ); + } + Ok(()) +} + fn nature_models(args: &[String]) -> Result<(), String> { use doppio_ui::uikit::nature; let presets = nature::presets(); diff --git a/doppio/crates/ui/src/uikit/asset_wizard.rs b/doppio/crates/ui/src/uikit/asset_wizard.rs index 0af363b4..b2b17218 100644 --- a/doppio/crates/ui/src/uikit/asset_wizard.rs +++ b/doppio/crates/ui/src/uikit/asset_wizard.rs @@ -4,7 +4,7 @@ //! be tested with temp directories and never touches the engine. The wizard //! panel and `App` only translate messages into these calls. //! -//! Raw assets live under the project asset root (`assets/raw`). The engine +//! Raw assets live under the project asset root (`Assets/Raw`). The engine //! recognises them by extension (see `assetKind` in the C ABI); `caf-pack` //! turns that raw tree into the `.cap` archive shipped with a game. diff --git a/doppio/crates/ui/src/uikit/components/code_editor.rs b/doppio/crates/ui/src/uikit/components/code_editor.rs index fadb867b..836ee262 100644 --- a/doppio/crates/ui/src/uikit/components/code_editor.rs +++ b/doppio/crates/ui/src/uikit/components/code_editor.rs @@ -5,8 +5,9 @@ //! pane on a stable component boundary and gives the editor a queryable id. use crate::uikit::theme; -use iced::widget::container; -use iced::{Element, Length}; +use iced::widget::{container, stack}; +use iced::{Element, Length, Theme}; +use std::sync::OnceLock; pub fn view<'a, Message: Clone + 'a>( id: impl Into, @@ -21,3 +22,27 @@ pub fn view<'a, Message: Clone + 'a>( .style(theme::surface) .into() } + +fn overlay_theme() -> &'static Theme { + static THEME: OnceLock = OnceLock::new(); + THEME.get_or_init(|| Theme::Dark) +} + +/// The editor with a floating reference card (hover tooltip) above it. +pub fn view_with_reference<'a, Message: Clone + 'a>( + id: impl Into, + editor: &'a iced_code_editor::CodeEditor, + overlay: &'a iced_code_editor::LspOverlayState, + on_action: impl Fn(iced_code_editor::Message) -> Message + 'a, + on_overlay: impl Fn(iced_code_editor::LspOverlayMessage) -> Message + 'a, +) -> Element<'a, Message> { + let card = iced_code_editor::view_lsp_overlay( + overlay, + editor, + overlay_theme(), + 14.0, + 20.0, + on_overlay, + ); + stack![view(id, editor, on_action), card].into() +} diff --git a/doppio/crates/ui/src/uikit/components/gizmo.rs b/doppio/crates/ui/src/uikit/components/gizmo.rs index 6033682d..cc00dfc4 100644 --- a/doppio/crates/ui/src/uikit/components/gizmo.rs +++ b/doppio/crates/ui/src/uikit/components/gizmo.rs @@ -141,7 +141,7 @@ pub struct Target { pub scale: [f32; 3], } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum Edit { Move { axis: Axis, @@ -190,6 +190,73 @@ impl Edit { } } +/// Grid steps used when snapping is on: metres, degrees and a scale factor. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct SnapSteps { + pub translate: f32, + pub rotate: f32, + pub scale: f32, +} + +impl Default for SnapSteps { + fn default() -> Self { + Self { + translate: 0.25, + rotate: 15.0, + scale: 0.1, + } + } +} + +fn round_to(value: f32, step: f32) -> f32 { + if step <= 0.0 { + value + } else { + // `+ 0.0` turns a negative zero into zero so history keys and text stay clean. + (value / step).round() * step + 0.0 + } +} + +impl Edit { + /// The same edit with the handle's own values moved onto the grid. Only the coordinates the + /// handle controls are snapped, so dragging X never nudges Y or Z. + pub fn snapped(&self, steps: SnapSteps) -> Edit { + match *self { + Edit::Move { axis, mut position } => { + let i = axis.index(); + position[i] = round_to(position[i], steps.translate); + Edit::Move { axis, position } + } + Edit::MovePlane { + plane, + mut position, + } => { + let (a, b) = plane.spans(); + for axis in [a, b] { + position[axis.index()] = round_to(position[axis.index()], steps.translate); + } + Edit::MovePlane { plane, position } + } + Edit::Rotate { axis, mut rotation } => { + let i = axis.index(); + rotation[i] = round_to(rotation[i], steps.rotate); + Edit::Rotate { axis, rotation } + } + Edit::Scale { axis, mut scale } => { + let i = axis.index(); + scale[i] = round_to(scale[i], steps.scale).max(steps.scale.max(0.01)); + Edit::Scale { axis, scale } + } + Edit::ScaleUniform { scale } => { + let value = round_to(scale[0], steps.scale).max(steps.scale.max(0.01)); + Edit::ScaleUniform { + scale: [value, value, value], + } + } + } + } +} + #[derive(Debug, Clone)] pub enum Message { Changed(Edit), @@ -1370,3 +1437,78 @@ mod tests { ); } } + +#[cfg(test)] +mod snap_tests { + use super::*; + + #[test] + fn moving_one_axis_only_snaps_that_axis() { + let edit = Edit::Move { + axis: Axis::X, + position: [1.13, 2.37, -0.61], + }; + let Edit::Move { position, .. } = edit.snapped(SnapSteps::default()) else { + panic!("still a move"); + }; + assert_eq!(position, [1.25, 2.37, -0.61]); + } + + #[test] + fn plane_moves_snap_both_coordinates_of_the_plane() { + let edit = Edit::MovePlane { + plane: Plane::XZ, + position: [0.9, 5.55, -1.1], + }; + let Edit::MovePlane { position, .. } = edit.snapped(SnapSteps::default()) else { + panic!("still a plane move"); + }; + assert_eq!(position, [1.0, 5.55, -1.0]); + } + + #[test] + fn rotation_snaps_to_degrees_and_never_leaves_negative_zero() { + let edit = Edit::Rotate { + axis: Axis::Y, + rotation: [3.0, 37.4, -2.0], + }; + let Edit::Rotate { rotation, .. } = edit.snapped(SnapSteps::default()) else { + panic!("still a rotation"); + }; + assert_eq!(rotation, [3.0, 30.0, -2.0]); + assert!(round_to(-0.01, 15.0).is_sign_positive()); + } + + #[test] + fn scale_snaps_and_never_reaches_zero() { + let Edit::ScaleUniform { scale } = (Edit::ScaleUniform { + scale: [0.012, 0.012, 0.012], + }) + .snapped(SnapSteps::default()) else { + panic!("still uniform"); + }; + assert_eq!(scale, [0.1, 0.1, 0.1]); + let Edit::Scale { scale, .. } = (Edit::Scale { + axis: Axis::Z, + scale: [1.0, 1.0, 2.46], + }) + .snapped(SnapSteps::default()) else { + panic!("still a scale"); + }; + assert_eq!(scale, [1.0, 1.0, 2.5]); + } + + #[test] + fn a_zero_step_leaves_values_alone() { + let steps = SnapSteps { + translate: 0.0, + rotate: 0.0, + scale: 0.0, + }; + let edit = Edit::Move { + axis: Axis::Z, + position: [0.1, 0.2, 0.3], + }; + assert_eq!(edit.snapped(steps), edit); + } +} diff --git a/doppio/crates/ui/src/uikit/dryad.rs b/doppio/crates/ui/src/uikit/dryad.rs index 50c67883..5fc2a812 100644 --- a/doppio/crates/ui/src/uikit/dryad.rs +++ b/doppio/crates/ui/src/uikit/dryad.rs @@ -32,11 +32,59 @@ pub enum NodeKind { Log, Constant, Branch, + KeyDown, + ActionDown, + Axis, + TotalTime, + DestroySelf, + Add, + Subtract, + Multiply, + Divide, + Negate, + Less, + Greater, + Equal, + And, + Or, + Not, + SelfEntity, + IsAlive, + Distance, + RandomValue, + RandomRange, + RandomInt, + RandomChance, + MouseX, + MouseY, + MouseDeltaX, + MouseDeltaY, + MouseDown, + FixedDelta, + FrameCount, + Min, + Max, + Abs, + Floor, + Ceil, + Round, + Sqrt, + Pow, + Sin, + Cos, + Radians, + Degrees, + Sign, + Clamp, + Clamp01, + Lerp, + InverseLerp, + Smoothstep, } impl NodeKind { /// Every kind, in catalog order. - pub const ALL: [NodeKind; 10] = [ + pub const ALL: [NodeKind; 58] = [ NodeKind::Constant, NodeKind::Branch, NodeKind::OnUpdate, @@ -47,6 +95,54 @@ impl NodeKind { NodeKind::GetScale, NodeKind::DeltaTime, NodeKind::Log, + NodeKind::KeyDown, + NodeKind::ActionDown, + NodeKind::Axis, + NodeKind::TotalTime, + NodeKind::DestroySelf, + NodeKind::Add, + NodeKind::Subtract, + NodeKind::Multiply, + NodeKind::Divide, + NodeKind::Negate, + NodeKind::Less, + NodeKind::Greater, + NodeKind::Equal, + NodeKind::And, + NodeKind::Or, + NodeKind::Not, + NodeKind::SelfEntity, + NodeKind::IsAlive, + NodeKind::Distance, + NodeKind::RandomValue, + NodeKind::RandomRange, + NodeKind::RandomInt, + NodeKind::RandomChance, + NodeKind::MouseX, + NodeKind::MouseY, + NodeKind::MouseDeltaX, + NodeKind::MouseDeltaY, + NodeKind::MouseDown, + NodeKind::FixedDelta, + NodeKind::FrameCount, + NodeKind::Min, + NodeKind::Max, + NodeKind::Abs, + NodeKind::Floor, + NodeKind::Ceil, + NodeKind::Round, + NodeKind::Sqrt, + NodeKind::Pow, + NodeKind::Sin, + NodeKind::Cos, + NodeKind::Radians, + NodeKind::Degrees, + NodeKind::Sign, + NodeKind::Clamp, + NodeKind::Clamp01, + NodeKind::Lerp, + NodeKind::InverseLerp, + NodeKind::Smoothstep, ]; /// The `type` field written to `.node.json` (`"OnUpdate"`, `"Branch"`…). @@ -62,6 +158,26 @@ impl NodeKind { NodeKind::Log => "Log", NodeKind::Constant => "Constant", NodeKind::Branch => "Branch", + NodeKind::KeyDown => "KeyDown", + NodeKind::ActionDown => "ActionDown", + NodeKind::Axis => "Axis", + NodeKind::TotalTime => "TotalTime", + NodeKind::DestroySelf => "DestroySelf", + NodeKind::Add => "Add", + NodeKind::Subtract => "Subtract", + NodeKind::Multiply => "Multiply", + NodeKind::Divide => "Divide", + NodeKind::Negate => "Negate", + NodeKind::Less => "Less", + NodeKind::Greater => "Greater", + NodeKind::Equal => "Equal", + NodeKind::And => "And", + NodeKind::Or => "Or", + NodeKind::Not => "Not", + NodeKind::SelfEntity => "SelfEntity", + NodeKind::IsAlive => "IsAlive", + NodeKind::Distance => "Distance", + other => native_row(other).map_or("", |row| row.type_name), } } @@ -78,6 +194,12 @@ impl NodeKind { NodeKind::Log => "Log", NodeKind::Constant => "Constant", NodeKind::Branch => "If / Else", + NodeKind::KeyDown => "Key Down", + NodeKind::ActionDown => "Action Down", + NodeKind::Axis => "Input Axis", + NodeKind::TotalTime => "Total Time", + NodeKind::DestroySelf => "Destroy Self", + other => extra(other).1, } } @@ -94,6 +216,10 @@ impl NodeKind { | NodeKind::GetScale => "Engine / Transform", NodeKind::DeltaTime => "Engine / Time", NodeKind::Log => "Engine / Debug", + NodeKind::KeyDown | NodeKind::ActionDown | NodeKind::Axis => "Engine / Input", + NodeKind::TotalTime => "Engine / Time", + NodeKind::DestroySelf => "Engine / World", + other => extra(other).2, } } @@ -111,6 +237,12 @@ impl NodeKind { NodeKind::GetScale => "Read the entity scale.", NodeKind::DeltaTime => "Seconds elapsed since the previous frame.", NodeKind::Log => "Write a message to the Doppio console.", + NodeKind::KeyDown => "True while the named key is held (Space, W, LShift...).", + NodeKind::ActionDown => "True while the named action is held (Jump, Attack...).", + NodeKind::Axis => "Input axis in -1..1 (Horizontal, Vertical, LookX, LookY).", + NodeKind::TotalTime => "Scaled seconds since play started.", + NodeKind::DestroySelf => "Destroy the entity that runs this script.", + other => extra(other).3, } } @@ -127,6 +259,12 @@ impl NodeKind { NodeKind::Log => "console message", NodeKind::DeltaTime => "frame delta", NodeKind::Constant => "fixed value", + NodeKind::KeyDown => "keyboard", + NodeKind::ActionDown => "input action", + NodeKind::Axis => "input axis", + NodeKind::TotalTime => "time since play", + NodeKind::DestroySelf => "removes the entity", + other => extra(other).4, } } @@ -140,13 +278,538 @@ impl NodeKind { /// `true` for nodes that only produce data and have no exec pins. pub fn is_data_only(self) -> bool { - matches!( - self, - NodeKind::GetPosition | NodeKind::GetScale | NodeKind::DeltaTime | NodeKind::Constant - ) + self.call_native().is_some() + || matches!( + self, + NodeKind::GetPosition + | NodeKind::GetScale + | NodeKind::DeltaTime + | NodeKind::Constant + | NodeKind::KeyDown + | NodeKind::ActionDown + | NodeKind::Axis + | NodeKind::TotalTime + | NodeKind::Add + | NodeKind::Subtract + | NodeKind::Multiply + | NodeKind::Divide + | NodeKind::Negate + | NodeKind::Less + | NodeKind::Greater + | NodeKind::Equal + | NodeKind::And + | NodeKind::Or + | NodeKind::Not + | NodeKind::SelfEntity + | NodeKind::IsAlive + | NodeKind::Distance + ) + } + + /// Infix or prefix operator emitted for a math, compare or logic node. + pub fn operator(self) -> Option { + let (symbol, unary, input, output) = match self { + NodeKind::Add => ("+", false, PinKind::Number, PinKind::Number), + NodeKind::Subtract => ("-", false, PinKind::Number, PinKind::Number), + NodeKind::Multiply => ("*", false, PinKind::Number, PinKind::Number), + NodeKind::Divide => ("/", false, PinKind::Number, PinKind::Number), + NodeKind::Negate => ("-", true, PinKind::Number, PinKind::Number), + NodeKind::Less => ("<", false, PinKind::Number, PinKind::Bool), + NodeKind::Greater => (">", false, PinKind::Number, PinKind::Bool), + NodeKind::Equal => ("==", false, PinKind::Number, PinKind::Bool), + NodeKind::And => ("&&", false, PinKind::Bool, PinKind::Bool), + NodeKind::Or => ("||", false, PinKind::Bool, PinKind::Bool), + NodeKind::Not => ("!", true, PinKind::Bool, PinKind::Bool), + _ => return None, + }; + Some(Operator { + symbol, + unary, + input, + output, + }) + } + + /// Native called by a node that wraps one engine function, with its argument count. + pub fn call_native(self) -> Option<(&'static str, usize)> { + match self { + NodeKind::IsAlive => Some(("caffeine_world_is_alive", 1)), + NodeKind::Distance => Some(("caffeine_world_distance", 2)), + other => native_row(other).map(|row| (row.native, row.arity)), + } } } +/// A data node that wraps one engine native taking up to three number inputs `a`, `b`, `c`. +#[derive(Debug, Clone, Copy)] +struct NativeNode { + kind: NodeKind, + type_name: &'static str, + display_name: &'static str, + category: &'static str, + description: &'static str, + subtitle: &'static str, + native: &'static str, + arity: usize, + boolean: bool, +} + +fn native_row(kind: NodeKind) -> Option<&'static NativeNode> { + NATIVE_NODES.iter().find(|row| row.kind == kind) +} + +const NATIVE_NODES: [NativeNode; 29] = [ + NativeNode { + kind: NodeKind::RandomValue, + type_name: "RandomValue", + display_name: "Random Value", + category: "Engine / Random", + description: "A random number from 0 up to 1.", + subtitle: "random 0..1", + native: "caffeine_random_value", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::RandomRange, + type_name: "RandomRange", + display_name: "Random Range", + category: "Engine / Random", + description: "A random number between a and b.", + subtitle: "between a and b", + native: "caffeine_random_range", + arity: 2, + boolean: false, + }, + NativeNode { + kind: NodeKind::RandomInt, + type_name: "RandomInt", + display_name: "Random Int", + category: "Engine / Random", + description: "A random whole number between a and b, both included.", + subtitle: "whole number", + native: "caffeine_random_int", + arity: 2, + boolean: false, + }, + NativeNode { + kind: NodeKind::RandomChance, + type_name: "RandomChance", + display_name: "Random Chance", + category: "Engine / Random", + description: "True with the probability a (0 never, 1 always).", + subtitle: "coin flip", + native: "caffeine_random_chance", + arity: 1, + boolean: true, + }, + NativeNode { + kind: NodeKind::MouseX, + type_name: "MouseX", + display_name: "Mouse X", + category: "Engine / Input", + description: "Mouse horizontal position in pixels.", + subtitle: "pointer x", + native: "caffeine_input_mouse_x", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::MouseY, + type_name: "MouseY", + display_name: "Mouse Y", + category: "Engine / Input", + description: "Mouse vertical position in pixels.", + subtitle: "pointer y", + native: "caffeine_input_mouse_y", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::MouseDeltaX, + type_name: "MouseDeltaX", + display_name: "Mouse Delta X", + category: "Engine / Input", + description: "Horizontal mouse movement since the previous frame.", + subtitle: "pointer dx", + native: "caffeine_input_mouse_dx", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::MouseDeltaY, + type_name: "MouseDeltaY", + display_name: "Mouse Delta Y", + category: "Engine / Input", + description: "Vertical mouse movement since the previous frame.", + subtitle: "pointer dy", + native: "caffeine_input_mouse_dy", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::MouseDown, + type_name: "MouseDown", + display_name: "Mouse Button", + category: "Engine / Input", + description: "True while the mouse button a is held (1 left, 2 middle, 3 right).", + subtitle: "mouse button", + native: "caffeine_input_mouse_down", + arity: 1, + boolean: true, + }, + NativeNode { + kind: NodeKind::FixedDelta, + type_name: "FixedDelta", + display_name: "Fixed Delta", + category: "Engine / Time", + description: "The fixed simulation step in seconds.", + subtitle: "fixed step", + native: "caffeine_time_fixed_delta", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::FrameCount, + type_name: "FrameCount", + display_name: "Frame Count", + category: "Engine / Time", + description: "Frames elapsed since play started.", + subtitle: "frame number", + native: "caffeine_time_frame", + arity: 0, + boolean: false, + }, + NativeNode { + kind: NodeKind::Min, + type_name: "Min", + display_name: "Min", + category: "Language / Math", + description: "The smaller of a and b.", + subtitle: "smaller", + native: "caffeine_math_min", + arity: 2, + boolean: false, + }, + NativeNode { + kind: NodeKind::Max, + type_name: "Max", + display_name: "Max", + category: "Language / Math", + description: "The larger of a and b.", + subtitle: "larger", + native: "caffeine_math_max", + arity: 2, + boolean: false, + }, + NativeNode { + kind: NodeKind::Abs, + type_name: "Abs", + display_name: "Abs", + category: "Language / Math", + description: "Absolute value of a.", + subtitle: "absolute", + native: "caffeine_math_abs", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Floor, + type_name: "Floor", + display_name: "Floor", + category: "Language / Math", + description: "Rounds a down to a whole number.", + subtitle: "round down", + native: "caffeine_math_floor", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Ceil, + type_name: "Ceil", + display_name: "Ceil", + category: "Language / Math", + description: "Rounds a up to a whole number.", + subtitle: "round up", + native: "caffeine_math_ceil", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Round, + type_name: "Round", + display_name: "Round", + category: "Language / Math", + description: "Rounds a to the nearest whole number.", + subtitle: "nearest", + native: "caffeine_math_round", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Sqrt, + type_name: "Sqrt", + display_name: "Square Root", + category: "Language / Math", + description: "Square root of a; negative input gives 0.", + subtitle: "square root", + native: "caffeine_math_sqrt", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Pow, + type_name: "Pow", + display_name: "Power", + category: "Language / Math", + description: "a raised to the power b.", + subtitle: "a to the b", + native: "caffeine_math_pow", + arity: 2, + boolean: false, + }, + NativeNode { + kind: NodeKind::Sin, + type_name: "Sin", + display_name: "Sine", + category: "Language / Trigonometry", + description: "Sine of an angle a in radians.", + subtitle: "radians", + native: "caffeine_math_sin", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Cos, + type_name: "Cos", + display_name: "Cosine", + category: "Language / Trigonometry", + description: "Cosine of an angle a in radians.", + subtitle: "radians", + native: "caffeine_math_cos", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Radians, + type_name: "Radians", + display_name: "Degrees to Radians", + category: "Language / Trigonometry", + description: "Converts degrees a to radians.", + subtitle: "deg to rad", + native: "caffeine_math_radians", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Degrees, + type_name: "Degrees", + display_name: "Radians to Degrees", + category: "Language / Trigonometry", + description: "Converts radians a to degrees.", + subtitle: "rad to deg", + native: "caffeine_math_degrees", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Sign, + type_name: "Sign", + display_name: "Sign", + category: "Language / Math", + description: "-1, 0 or 1 by the sign of a.", + subtitle: "sign", + native: "caffeine_math_sign", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Clamp, + type_name: "Clamp", + display_name: "Clamp", + category: "Language / Math", + description: "Limits a to the range b..c.", + subtitle: "limit range", + native: "caffeine_math_clamp", + arity: 3, + boolean: false, + }, + NativeNode { + kind: NodeKind::Clamp01, + type_name: "Clamp01", + display_name: "Clamp 0..1", + category: "Language / Math", + description: "Limits a to 0..1.", + subtitle: "limit 0..1", + native: "caffeine_math_clamp01", + arity: 1, + boolean: false, + }, + NativeNode { + kind: NodeKind::Lerp, + type_name: "Lerp", + display_name: "Lerp", + category: "Language / Math", + description: "Blend from a to b by the factor c.", + subtitle: "blend", + native: "caffeine_math_lerp", + arity: 3, + boolean: false, + }, + NativeNode { + kind: NodeKind::InverseLerp, + type_name: "InverseLerp", + display_name: "Inverse Lerp", + category: "Language / Math", + description: "Where c sits between a and b (0 at a, 1 at b).", + subtitle: "position in range", + native: "caffeine_math_inverse_lerp", + arity: 3, + boolean: false, + }, + NativeNode { + kind: NodeKind::Smoothstep, + type_name: "Smoothstep", + display_name: "Smoothstep", + category: "Language / Math", + description: "Smooth 0..1 ramp of c between the edges a and b.", + subtitle: "smooth ramp", + native: "caffeine_math_smoothstep", + arity: 3, + boolean: false, + }, +]; + +/// How an operator node is written in Dryad source. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Operator { + pub symbol: &'static str, + pub unary: bool, + pub input: PinKind, + pub output: PinKind, +} + +/// `(type, display, category, description, subtitle)` of the nodes added after the first ten. +fn extra( + kind: NodeKind, +) -> ( + &'static str, + &'static str, + &'static str, + &'static str, + &'static str, +) { + if let Some(row) = native_row(kind) { + return ( + row.type_name, + row.display_name, + row.category, + row.description, + row.subtitle, + ); + } + EXTRA + .iter() + .copied() + .find(|row| row.0 == kind.type_name()) + .unwrap_or(("", "", "", "", "")) +} + +const EXTRA: [(&str, &str, &str, &str, &str); 14] = [ + ("Add", "Add", "Language / Math", "a + b", "add"), + ( + "Subtract", + "Subtract", + "Language / Math", + "a - b", + "subtract", + ), + ( + "Multiply", + "Multiply", + "Language / Math", + "a * b", + "multiply", + ), + ( + "Divide", + "Divide", + "Language / Math", + "a / b. Dividing by zero gives an error value, guard it with a Compare.", + "divide", + ), + ( + "Negate", + "Negate", + "Language / Math", + "The value with its sign flipped.", + "-a", + ), + ( + "Less", + "Less Than", + "Language / Compare", + "True when a is smaller than b.", + "a < b", + ), + ( + "Greater", + "Greater Than", + "Language / Compare", + "True when a is larger than b.", + "a > b", + ), + ( + "Equal", + "Equal", + "Language / Compare", + "True when a and b are the same value.", + "a == b", + ), + ( + "And", + "And", + "Language / Logic", + "True when both inputs are true.", + "a and b", + ), + ( + "Or", + "Or", + "Language / Logic", + "True when at least one input is true.", + "a or b", + ), + ( + "Not", + "Not", + "Language / Logic", + "The opposite of the input.", + "not a", + ), + ( + "SelfEntity", + "This Entity", + "Engine / World", + "The id of the entity that runs this script.", + "entity id", + ), + ( + "IsAlive", + "Is Alive", + "Engine / World", + "True while the entity with this id still exists.", + "entity exists", + ), + ( + "Distance", + "Distance", + "Engine / World", + "World distance between two entities.", + "between entities", + ), +]; + /// One catalog row shared by the palette, the canvas and the emitter. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct CatalogEntry { @@ -159,10 +822,21 @@ pub struct CatalogEntry { /// The catalog. It is static so the palette, tests and agents share one list. pub fn catalog() -> &'static [CatalogEntry] { - &CATALOG + static ALL_ENTRIES: std::sync::OnceLock> = std::sync::OnceLock::new(); + ALL_ENTRIES.get_or_init(|| { + let mut entries = CATALOG.to_vec(); + entries.extend(NATIVE_NODES.iter().map(|row| CatalogEntry { + kind: row.kind, + type_name: row.type_name, + display_name: row.display_name, + category: row.category, + description: row.description, + })); + entries + }) } -const CATALOG: [CatalogEntry; 10] = [ +const CATALOG: [CatalogEntry; 29] = [ CatalogEntry { kind: NodeKind::Constant, type_name: "Constant", @@ -233,6 +907,139 @@ const CATALOG: [CatalogEntry; 10] = [ category: "Engine / Debug", description: "Write a message to the Doppio console.", }, + CatalogEntry { + kind: NodeKind::KeyDown, + type_name: "KeyDown", + display_name: "Key Down", + category: "Engine / Input", + description: "True while the named key is held (Space, W, LShift...).", + }, + CatalogEntry { + kind: NodeKind::ActionDown, + type_name: "ActionDown", + display_name: "Action Down", + category: "Engine / Input", + description: "True while the named action is held (Jump, Attack...).", + }, + CatalogEntry { + kind: NodeKind::Axis, + type_name: "Axis", + display_name: "Input Axis", + category: "Engine / Input", + description: "Input axis in -1..1 (Horizontal, Vertical, LookX, LookY).", + }, + CatalogEntry { + kind: NodeKind::TotalTime, + type_name: "TotalTime", + display_name: "Total Time", + category: "Engine / Time", + description: "Scaled seconds since play started.", + }, + CatalogEntry { + kind: NodeKind::DestroySelf, + type_name: "DestroySelf", + display_name: "Destroy Self", + category: "Engine / World", + description: "Destroy the entity that runs this script.", + }, + CatalogEntry { + kind: NodeKind::Add, + type_name: "Add", + display_name: "Add", + category: "Language / Math", + description: "a + b", + }, + CatalogEntry { + kind: NodeKind::Subtract, + type_name: "Subtract", + display_name: "Subtract", + category: "Language / Math", + description: "a - b", + }, + CatalogEntry { + kind: NodeKind::Multiply, + type_name: "Multiply", + display_name: "Multiply", + category: "Language / Math", + description: "a * b", + }, + CatalogEntry { + kind: NodeKind::Divide, + type_name: "Divide", + display_name: "Divide", + category: "Language / Math", + description: "a / b. Dividing by zero gives an error value, guard it with a Compare.", + }, + CatalogEntry { + kind: NodeKind::Negate, + type_name: "Negate", + display_name: "Negate", + category: "Language / Math", + description: "The value with its sign flipped.", + }, + CatalogEntry { + kind: NodeKind::Less, + type_name: "Less", + display_name: "Less Than", + category: "Language / Compare", + description: "True when a is smaller than b.", + }, + CatalogEntry { + kind: NodeKind::Greater, + type_name: "Greater", + display_name: "Greater Than", + category: "Language / Compare", + description: "True when a is larger than b.", + }, + CatalogEntry { + kind: NodeKind::Equal, + type_name: "Equal", + display_name: "Equal", + category: "Language / Compare", + description: "True when a and b are the same value.", + }, + CatalogEntry { + kind: NodeKind::And, + type_name: "And", + display_name: "And", + category: "Language / Logic", + description: "True when both inputs are true.", + }, + CatalogEntry { + kind: NodeKind::Or, + type_name: "Or", + display_name: "Or", + category: "Language / Logic", + description: "True when at least one input is true.", + }, + CatalogEntry { + kind: NodeKind::Not, + type_name: "Not", + display_name: "Not", + category: "Language / Logic", + description: "The opposite of the input.", + }, + CatalogEntry { + kind: NodeKind::SelfEntity, + type_name: "SelfEntity", + display_name: "This Entity", + category: "Engine / World", + description: "The id of the entity that runs this script.", + }, + CatalogEntry { + kind: NodeKind::IsAlive, + type_name: "IsAlive", + display_name: "Is Alive", + category: "Engine / World", + description: "True while the entity with this id still exists.", + }, + CatalogEntry { + kind: NodeKind::Distance, + type_name: "Distance", + display_name: "Distance", + category: "Engine / World", + description: "World distance between two entities.", + }, ]; /// Look up a catalog entry by `type` name. @@ -310,6 +1117,62 @@ const VALUE_OUTPUT: [PinDef; 1] = [PinDef { label: "value", kind: PinKind::Number, }]; +const BOOL_VALUE_OUTPUT: [PinDef; 1] = [PinDef { + name: "value", + label: "value", + kind: PinKind::Bool, +}]; +const NUMBER_AB: [PinDef; 2] = [ + PinDef { + name: "a", + label: "a", + kind: PinKind::Number, + }, + PinDef { + name: "b", + label: "b", + kind: PinKind::Number, + }, +]; +const NUMBER_ABC: [PinDef; 3] = [ + PinDef { + name: "a", + label: "a", + kind: PinKind::Number, + }, + PinDef { + name: "b", + label: "b", + kind: PinKind::Number, + }, + PinDef { + name: "c", + label: "c", + kind: PinKind::Number, + }, +]; +const NUMBER_A: [PinDef; 1] = [PinDef { + name: "a", + label: "a", + kind: PinKind::Number, +}]; +const BOOL_AB: [PinDef; 2] = [ + PinDef { + name: "a", + label: "a", + kind: PinKind::Bool, + }, + PinDef { + name: "b", + label: "b", + kind: PinKind::Bool, + }, +]; +const BOOL_A: [PinDef; 1] = [PinDef { + name: "a", + label: "a", + kind: PinKind::Bool, +}]; const LOG_INPUTS: [PinDef; 1] = [EXEC_IN]; const BRANCH_INPUTS: [PinDef; 2] = [ EXEC_IN, @@ -335,28 +1198,82 @@ const BRANCH_OUTPUTS: [PinDef; 3] = [ /// Input pins for a kind, in canvas order. pub fn inputs(kind: NodeKind) -> &'static [PinDef] { + if let Some(row) = native_row(kind) { + return match row.arity { + 0 => &[], + 1 => &NUMBER_A, + 2 => &NUMBER_AB, + _ => &NUMBER_ABC, + }; + } match kind { NodeKind::SetPosition | NodeKind::Translate | NodeKind::SetScale => &VECTOR_INPUTS, - NodeKind::Log => &LOG_INPUTS, + NodeKind::Log | NodeKind::DestroySelf => &LOG_INPUTS, NodeKind::Branch => &BRANCH_INPUTS, - NodeKind::OnUpdate + NodeKind::Add + | NodeKind::Subtract + | NodeKind::Multiply + | NodeKind::Divide + | NodeKind::Less + | NodeKind::Greater + | NodeKind::Equal + | NodeKind::Distance => &NUMBER_AB, + NodeKind::Negate | NodeKind::IsAlive => &NUMBER_A, + NodeKind::And | NodeKind::Or => &BOOL_AB, + NodeKind::Not => &BOOL_A, + NodeKind::SelfEntity + | NodeKind::OnUpdate | NodeKind::GetPosition | NodeKind::DeltaTime | NodeKind::GetScale - | NodeKind::Constant => &[], + | NodeKind::Constant + | NodeKind::KeyDown + | NodeKind::ActionDown + | NodeKind::Axis + | NodeKind::TotalTime => &[], + // Native-call nodes returned above. + _ => &[], } } /// Output pins for a kind. `Constant` always reports a number here; the /// canvas and connection checks use [`outputs_for`] to honour `bool`. pub fn outputs(kind: NodeKind) -> &'static [PinDef] { + if let Some(row) = native_row(kind) { + return if row.boolean { + &BOOL_VALUE_OUTPUT + } else { + &VALUE_OUTPUT + }; + } match kind { NodeKind::OnUpdate => std::slice::from_ref(&EXEC_OUT), NodeKind::SetPosition | NodeKind::Translate | NodeKind::SetScale => &VECTOR_OUTPUTS, NodeKind::GetPosition | NodeKind::GetScale => &VECTOR_DATA_OUTPUTS, - NodeKind::DeltaTime | NodeKind::Constant => &VALUE_OUTPUT, - NodeKind::Log => std::slice::from_ref(&EXEC_OUT), + NodeKind::DeltaTime + | NodeKind::Constant + | NodeKind::Axis + | NodeKind::TotalTime + | NodeKind::Add + | NodeKind::Subtract + | NodeKind::Multiply + | NodeKind::Divide + | NodeKind::Negate + | NodeKind::SelfEntity + | NodeKind::Distance => &VALUE_OUTPUT, + NodeKind::KeyDown + | NodeKind::ActionDown + | NodeKind::Less + | NodeKind::Greater + | NodeKind::Equal + | NodeKind::And + | NodeKind::Or + | NodeKind::Not + | NodeKind::IsAlive => &BOOL_VALUE_OUTPUT, + NodeKind::Log | NodeKind::DestroySelf => std::slice::from_ref(&EXEC_OUT), NodeKind::Branch => &BRANCH_OUTPUTS, + // Native-call nodes returned above. + _ => &[], } } @@ -406,7 +1323,13 @@ impl GraphNode { input_y: 0.0, input_z: 0.0, input_condition: false, - text_value: "Hello from Dryad".to_owned(), + text_value: match kind { + NodeKind::KeyDown => "Space", + NodeKind::ActionDown => "Jump", + NodeKind::Axis => "Horizontal", + _ => "Hello from Dryad", + } + .to_owned(), } } @@ -865,6 +1788,16 @@ impl<'a> Emitter<'a> { } Ok(()) } + NodeKind::DestroySelf => { + Self::line(out, depth, &format!("// @node {}", node.id)); + Self::line(out, depth, "caffeine_world_destroy(entity);"); + if let Some(next) = self.exec_edge_from(&node.id, "out") { + let (next_id, _) = parse_pin_ref(&next.to) + .ok_or_else(|| "invalid exec edge from DestroySelf".to_owned())?; + self.emit_exec_chain(next_id, out, depth)?; + } + Ok(()) + } NodeKind::Log => { Self::line(out, depth, &format!("// @node {}", node.id)); Self::line( @@ -906,7 +1839,26 @@ impl<'a> Emitter<'a> { NodeKind::GetPosition | NodeKind::GetScale | NodeKind::DeltaTime + | NodeKind::KeyDown + | NodeKind::ActionDown + | NodeKind::Axis + | NodeKind::TotalTime + | NodeKind::Add + | NodeKind::Subtract + | NodeKind::Multiply + | NodeKind::Divide + | NodeKind::Negate + | NodeKind::Less + | NodeKind::Greater + | NodeKind::Equal + | NodeKind::And + | NodeKind::Or + | NodeKind::Not + | NodeKind::SelfEntity + | NodeKind::IsAlive + | NodeKind::Distance | NodeKind::Constant => Err(format!("data-only node in exec chain: {node_id}")), + _ => Err(format!("data-only node in exec chain: {node_id}")), } } @@ -917,6 +1869,13 @@ impl<'a> Emitter<'a> { return self.resolve_data_output(source_node, source_pin); } if let Some(node) = self.graph.find_node(node_id) { + if node + .kind + .operator() + .is_some_and(|op| op.input == PinKind::Bool) + { + return Ok("false".to_owned()); + } match pin { "x" => return Ok(format_number(node.input_x)), "y" => return Ok(format_number(node.input_y)), @@ -965,6 +1924,26 @@ impl<'a> Emitter<'a> { } "dt".to_owned() } + NodeKind::KeyDown | NodeKind::ActionDown | NodeKind::Axis => { + if pin != "value" { + return Err(format!( + "{} only exposes pin 'value'", + node.kind.type_name() + )); + } + let native = match node.kind { + NodeKind::KeyDown => "caffeine_input_key_down", + NodeKind::ActionDown => "caffeine_input_action_down", + _ => "caffeine_input_axis", + }; + format!("{native}(\"{}\")", json_escape(&node.text_value)) + } + NodeKind::TotalTime => { + if pin != "value" { + return Err("TotalTime only exposes pin 'value'".to_owned()); + } + "caffeine_time_total()".to_owned() + } NodeKind::GetPosition => match pin { "x" => "caffeine_get_position_x(entity)".to_owned(), "y" => "caffeine_get_position_y(entity)".to_owned(), @@ -977,6 +1956,29 @@ impl<'a> Emitter<'a> { "z" => "caffeine_get_scale_z(entity)".to_owned(), _ => return Err("GetScale exposes x, y, z pins".to_owned()), }, + NodeKind::SelfEntity => "entity".to_owned(), + kind if kind.operator().is_some() => { + let Some(op) = kind.operator() else { + return Err(format!("node {node_id} is not an operator")); + }; + let a = self.resolve_data_input(node_id, "a")?; + if op.unary { + format!("({}{a})", op.symbol) + } else { + let b = self.resolve_data_input(node_id, "b")?; + format!("({a} {} {b})", op.symbol) + } + } + kind if kind.call_native().is_some() => { + let Some((native, arity)) = kind.call_native() else { + return Err(format!("node {node_id} is not a call")); + }; + let mut arguments = Vec::new(); + for pin in ["a", "b", "c"].into_iter().take(arity) { + arguments.push(self.resolve_data_input(node_id, pin)?); + } + format!("{native}({})", arguments.join(", ")) + } _ => return Err(format!("node {node_id} is not a data source")), }; self.cache.insert(key, expression.clone()); @@ -984,6 +1986,140 @@ impl<'a> Emitter<'a> { } } +/// Split `(a + b)`, `(!a)` or `caffeine_world_distance(a, b)` into a node kind and its operand +/// expressions. The emitter parenthesises every operator, so each has exactly one at depth 0. +fn parse_operator_expression(expression: &str) -> Option<(NodeKind, Vec)> { + let expression = expression.trim(); + for kind in NodeKind::ALL { + let Some((native, arity)) = kind.call_native() else { + continue; + }; + let Some(rest) = expression + .strip_prefix(native) + .and_then(|rest| rest.strip_prefix('(')) + else { + continue; + }; + let inner = rest.strip_suffix(')')?; + let parts = if inner.trim().is_empty() { + Vec::new() + } else { + split_top_level(inner, ',') + }; + return (parts.len() == arity).then_some((kind, parts)); + } + let inner = expression.strip_prefix('(')?.strip_suffix(')')?; + // The leading `(` must close at the very end, not earlier (`(a) + (b)`). + let mut depth = 0i32; + for (index, character) in expression.char_indices() { + match character { + '(' => depth += 1, + ')' => { + depth -= 1; + if depth == 0 && index + 1 != expression.len() { + return None; + } + } + _ => {} + } + } + let inner = inner.trim(); + if let Some(operand) = inner.strip_prefix('!') { + return Some((NodeKind::Not, vec![operand.trim().to_owned()])); + } + if let Some(operand) = inner.strip_prefix('-') { + // `-5` is a constant; only `-` before an expression is a Negate node. + if operand.trim_start().starts_with(['(', '_']) + || operand.trim_start().starts_with(char::is_alphabetic) + { + return Some((NodeKind::Negate, vec![operand.trim().to_owned()])); + } + } + for (symbol, kind) in [ + (" && ", NodeKind::And), + (" || ", NodeKind::Or), + (" == ", NodeKind::Equal), + (" < ", NodeKind::Less), + (" > ", NodeKind::Greater), + (" + ", NodeKind::Add), + (" - ", NodeKind::Subtract), + (" * ", NodeKind::Multiply), + (" / ", NodeKind::Divide), + ] { + let parts = split_top_level(inner, symbol); + if parts.len() == 2 { + return Some((kind, parts)); + } + } + None +} + +/// Split `text` at the separator where it appears outside parentheses and quotes. +fn split_top_level(text: &str, separator: P) -> Vec { + let mut parts = Vec::new(); + let mut depth = 0i32; + let mut quoted = false; + let mut start = 0usize; + let mut index = 0usize; + while index < text.len() { + let rest = &text[index..]; + let character = rest.chars().next().unwrap_or(' '); + match character { + '"' => quoted = !quoted, + '(' if !quoted => depth += 1, + ')' if !quoted => depth -= 1, + _ => {} + } + if depth == 0 && !quoted { + if let Some(width) = separator.matches_at(rest) { + parts.push(text[start..index].trim().to_owned()); + index += width; + start = index; + continue; + } + } + index += character.len_utf8(); + } + parts.push(text[start..].trim().to_owned()); + parts +} + +trait SplitPattern { + fn matches_at(&self, rest: &str) -> Option; +} + +impl SplitPattern for char { + fn matches_at(&self, rest: &str) -> Option { + rest.starts_with(*self).then(|| self.len_utf8()) + } +} + +impl SplitPattern for &str { + fn matches_at(&self, rest: &str) -> Option { + rest.starts_with(*self).then_some(self.len()) + } +} + +/// Recognise `caffeine_input_key_down("Space")` style calls and return the node kind +/// plus the quoted name. +fn input_call(expression: &str) -> Option<(NodeKind, String)> { + for (native, kind) in [ + ("caffeine_input_key_down", NodeKind::KeyDown), + ("caffeine_input_action_down", NodeKind::ActionDown), + ("caffeine_input_axis", NodeKind::Axis), + ] { + let Some(rest) = expression.strip_prefix(native) else { + continue; + }; + let inner = rest.strip_prefix("(\"")?.strip_suffix("\")")?; + if inner.contains('"') { + return None; + } + return Some((kind, inner.replace("\\\\", "\\"))); + } + None +} + fn json_escape(text: &str) -> String { let mut out = String::with_capacity(text.len() + 8); for character in text.chars() { @@ -1056,7 +2192,10 @@ fn serialize_json(graph: &Graph) -> String { } )); } - if node.kind == NodeKind::Log { + if matches!( + node.kind, + NodeKind::Log | NodeKind::KeyDown | NodeKind::ActionDown | NodeKind::Axis + ) { json.push_str(&format!( ", \"text\": \"{}\"", json_escape(&node.text_value) @@ -1147,7 +2286,10 @@ fn parse_json(source: &str) -> Result { node.number_value = number; } } - if kind == NodeKind::Log { + if matches!( + kind, + NodeKind::Log | NodeKind::KeyDown | NodeKind::ActionDown | NodeKind::Axis + ) { if let Some(text) = entry.get("text").and_then(|text| text.as_str()) { node.text_value = text.to_owned(); } @@ -1402,6 +2544,8 @@ impl<'a> DydImporter<'a> { exec_out = self.parse_vector_call(&node_id, NodeKind::Translate, &exec_out)?; } else if self.consume("caffeine_set_scale") { exec_out = self.parse_vector_call(&node_id, NodeKind::SetScale, &exec_out)?; + } else if self.consume("caffeine_world_destroy") { + exec_out = self.parse_destroy(&node_id, &exec_out)?; } else if self.consume("caffeine_log") { exec_out = self.parse_log(&node_id, &exec_out)?; } else if self.consume("if") { @@ -1450,6 +2594,20 @@ impl<'a> DydImporter<'a> { Ok(format!("{node_id}.out")) } + fn parse_destroy(&mut self, node_id: &str, exec_out: &str) -> Result { + self.ensure_node(node_id, NodeKind::DestroySelf); + if exec_out.is_empty() { + return Err("DestroySelf without exec input".to_owned()); + } + self.add_edge(exec_out.to_owned(), format!("{node_id}.in")); + for expected in ["(", "entity", ")", ";"] { + if !self.consume(expected) { + return Err(format!("expected {expected} after caffeine_world_destroy")); + } + } + Ok(format!("{node_id}.out")) + } + fn parse_log(&mut self, node_id: &str, exec_out: &str) -> Result { self.ensure_node(node_id, NodeKind::Log); if exec_out.is_empty() { @@ -1625,6 +2783,24 @@ impl<'a> DydImporter<'a> { self.add_edge(format!("{source}.{pin_name}"), format!("{to_node}.{pin}")); return Ok(()); } + if expression == "entity" { + let id = format!("import_self{}", self.serial); + self.serial += 1; + self.ensure_node(&id, NodeKind::SelfEntity); + self.add_edge(format!("{id}.value"), format!("{to_node}.{pin}")); + return Ok(()); + } + if let Some((kind, parts)) = parse_operator_expression(expression) { + let id = format!("import_op{}", self.serial); + self.serial += 1; + self.ensure_node(&id, kind); + let inputs = ["a", "b", "c"]; + for (part, input) in parts.iter().zip(inputs) { + self.wire_data_expr(part, &id, input)?; + } + self.add_edge(format!("{id}.value"), format!("{to_node}.{pin}")); + return Ok(()); + } if expression == "dt" { if self.delta_time_id.is_none() { let id = format!("import_dt{}", self.serial); @@ -1636,6 +2812,37 @@ impl<'a> DydImporter<'a> { self.add_edge(format!("{source}.value"), format!("{to_node}.{pin}")); return Ok(()); } + if let Some((kind, text)) = input_call(expression) { + let source = match self.expr_to_constant.get(expression).cloned() { + Some(existing) => existing, + None => { + let id = format!("import_i{}", self.serial); + self.serial += 1; + let node = self.ensure_node(&id, kind); + node.text_value = text; + self.expr_to_constant + .insert(expression.to_owned(), id.clone()); + id + } + }; + self.add_edge(format!("{source}.value"), format!("{to_node}.{pin}")); + return Ok(()); + } + if expression == "caffeine_time_total()" { + let source = match self.expr_to_constant.get(expression).cloned() { + Some(existing) => existing, + None => { + let id = format!("import_tt{}", self.serial); + self.serial += 1; + self.ensure_node(&id, NodeKind::TotalTime); + self.expr_to_constant + .insert(expression.to_owned(), id.clone()); + id + } + }; + self.add_edge(format!("{source}.value"), format!("{to_node}.{pin}")); + return Ok(()); + } if let Some(constant) = self.expr_to_constant.get(expression).cloned() { self.add_edge(format!("{constant}.value"), format!("{to_node}.{pin}")); return Ok(()); @@ -2457,4 +3664,237 @@ mod tests { assert_eq!(format_f32(1234567.0), "1.23457e+06"); assert_eq!(format_f32(0.001), "0.001"); } + + #[test] + fn math_compare_and_logic_nodes_emit_and_round_trip() { + let mut graph = Graph::new("logic.dyd"); + let root = graph.add_node(NodeKind::OnUpdate, 0.0, 0.0); + let branch = graph.add_node(NodeKind::Branch, 100.0, 0.0); + let log = graph.add_node(NodeKind::Log, 200.0, 0.0); + let and = graph.add_node(NodeKind::And, 0.0, 100.0); + let key = graph.add_node(NodeKind::KeyDown, 0.0, 200.0); + let less = graph.add_node(NodeKind::Less, 0.0, 300.0); + let total = graph.add_node(NodeKind::TotalTime, 0.0, 400.0); + let add = graph.add_node(NodeKind::Add, 0.0, 500.0); + let dt = graph.add_node(NodeKind::DeltaTime, 0.0, 600.0); + let not = graph.add_node(NodeKind::Not, 0.0, 700.0); + let alive = graph.add_node(NodeKind::IsAlive, 0.0, 800.0); + let me = graph.add_node(NodeKind::SelfEntity, 0.0, 900.0); + let c = |g: &mut Graph, a: &str, b: &str| g.connect(a, b).unwrap(); + c(&mut graph, &format!("{root}.out"), &format!("{branch}.in")); + c(&mut graph, &format!("{branch}.then"), &format!("{log}.in")); + c( + &mut graph, + &format!("{and}.value"), + &format!("{branch}.condition"), + ); + c(&mut graph, &format!("{key}.value"), &format!("{and}.a")); + c(&mut graph, &format!("{less}.value"), &format!("{not}.a")); + c(&mut graph, &format!("{not}.value"), &format!("{and}.b")); + c(&mut graph, &format!("{total}.value"), &format!("{less}.a")); + c(&mut graph, &format!("{add}.value"), &format!("{less}.b")); + c(&mut graph, &format!("{dt}.value"), &format!("{add}.a")); + c(&mut graph, &format!("{me}.value"), &format!("{alive}.a")); + // A number cannot feed a bool input, and a bool cannot feed math. + assert!(graph + .connect(&format!("{total}.value"), &format!("{and}.a")) + .is_err()); + assert!(graph + .connect(&format!("{key}.value"), &format!("{add}.b")) + .is_err()); + + let out = graph.emit().unwrap(); + assert!( + out.dyd.contains( + "if ((caffeine_input_key_down(\"Space\") && (!(caffeine_time_total() < (dt + 0))))) {" + ), + "{}", + out.dyd + ); + let imported = import_from_dyd(&out.dyd, "logic.dyd", None).unwrap(); + let count = |kind: NodeKind| imported.nodes.iter().filter(|n| n.kind == kind).count(); + for kind in [NodeKind::And, NodeKind::Not, NodeKind::Less, NodeKind::Add] { + assert_eq!(count(kind), 1, "{kind:?}"); + } + assert_eq!(imported.emit().unwrap().dyd, out.dyd); + + // A graph with a call node and `entity` also survives the trip. + let mut calls = Graph::new("calls.dyd"); + let root = calls.add_node(NodeKind::OnUpdate, 0.0, 0.0); + let branch = calls.add_node(NodeKind::Branch, 100.0, 0.0); + let log = calls.add_node(NodeKind::Log, 200.0, 0.0); + let alive = calls.add_node(NodeKind::IsAlive, 0.0, 100.0); + let me = calls.add_node(NodeKind::SelfEntity, 0.0, 200.0); + c(&mut calls, &format!("{root}.out"), &format!("{branch}.in")); + c(&mut calls, &format!("{branch}.then"), &format!("{log}.in")); + c( + &mut calls, + &format!("{alive}.value"), + &format!("{branch}.condition"), + ); + c(&mut calls, &format!("{me}.value"), &format!("{alive}.a")); + let text = calls.emit().unwrap().dyd; + assert!( + text.contains("if (caffeine_world_is_alive(entity)) {"), + "{text}" + ); + let again = import_from_dyd(&text, "calls.dyd", None).unwrap(); + assert_eq!(again.emit().unwrap().dyd, text); + } + + #[test] + fn input_and_world_nodes_emit_and_round_trip() { + let mut graph = Graph::new("input.dyd"); + let root = graph.add_node(NodeKind::OnUpdate, 0.0, 0.0); + let branch = graph.add_node(NodeKind::Branch, 100.0, 0.0); + let key = graph.add_node(NodeKind::KeyDown, 0.0, 100.0); + let destroy = graph.add_node(NodeKind::DestroySelf, 200.0, 0.0); + let mv = graph.add_node(NodeKind::Translate, 200.0, 100.0); + let axis = graph.add_node(NodeKind::Axis, 0.0, 200.0); + let total = graph.add_node(NodeKind::TotalTime, 0.0, 300.0); + graph.find_node_mut(&axis).unwrap().text_value = "Vertical".to_owned(); + graph + .connect(&format!("{root}.out"), &format!("{branch}.in")) + .unwrap(); + graph + .connect(&format!("{key}.value"), &format!("{branch}.condition")) + .unwrap(); + graph + .connect(&format!("{branch}.then"), &format!("{destroy}.in")) + .unwrap(); + graph + .connect(&format!("{branch}.else"), &format!("{mv}.in")) + .unwrap(); + graph + .connect(&format!("{axis}.value"), &format!("{mv}.x")) + .unwrap(); + graph + .connect(&format!("{total}.value"), &format!("{mv}.y")) + .unwrap(); + + let out = graph.emit().unwrap(); + assert!(out + .dyd + .contains("if (caffeine_input_key_down(\"Space\")) {")); + assert!(out.dyd.contains("caffeine_world_destroy(entity);")); + assert!(out + .dyd + .contains("caffeine_translate(entity, caffeine_input_axis(\"Vertical\"), caffeine_time_total(), 0);")); + + // A bool pin cannot feed a number input. + assert!(graph + .connect(&format!("{key}.value"), &format!("{mv}.z")) + .is_err()); + + let imported = import_from_dyd(&out.dyd, "input.dyd", None).unwrap(); + let count = |kind: NodeKind| imported.nodes.iter().filter(|n| n.kind == kind).count(); + assert_eq!(count(NodeKind::KeyDown), 1); + assert_eq!(count(NodeKind::DestroySelf), 1); + assert_eq!(count(NodeKind::TotalTime), 1); + assert!(imported + .nodes + .iter() + .any(|n| n.kind == NodeKind::Axis && n.text_value == "Vertical")); + assert_eq!(imported.emit().unwrap().dyd, out.dyd); + + let reloaded = Graph::from_json(&out.node_json).unwrap(); + assert!(reloaded + .nodes + .iter() + .any(|n| n.kind == NodeKind::Axis && n.text_value == "Vertical")); + assert_eq!(reloaded.emit().unwrap().dyd, out.dyd); + } + + #[test] + fn every_native_node_emits_calls_and_round_trips() { + let natives: Vec = NodeKind::ALL + .iter() + .copied() + .filter(|kind| native_row(*kind).is_some()) + .collect(); + assert!(natives.len() >= 29, "the native table feeds the catalog"); + for kind in natives { + let row = native_row(kind).unwrap(); + let mut graph = Graph::new("native.dyd"); + let root = graph.add_node(NodeKind::OnUpdate, 0.0, 0.0); + let node = graph.add_node(kind, 0.0, 100.0); + let mv = graph.add_node(NodeKind::Translate, 200.0, 0.0); + if row.boolean { + let branch = graph.add_node(NodeKind::Branch, 100.0, 0.0); + graph + .connect(&format!("{root}.out"), &format!("{branch}.in")) + .unwrap(); + graph + .connect(&format!("{node}.value"), &format!("{branch}.condition")) + .unwrap(); + graph + .connect(&format!("{branch}.then"), &format!("{mv}.in")) + .unwrap(); + } else { + graph + .connect(&format!("{root}.out"), &format!("{mv}.in")) + .unwrap(); + graph + .connect(&format!("{node}.value"), &format!("{mv}.x")) + .unwrap(); + } + let out = graph.emit().unwrap_or_else(|e| panic!("{kind:?}: {e}")); + let zeros = vec!["0"; row.arity].join(", "); + assert!( + out.dyd.contains(&format!("{}({zeros})", row.native)), + "{kind:?} emits its native: {}", + out.dyd + ); + let imported = import_from_dyd(&out.dyd, "native.dyd", None) + .unwrap_or_else(|e| panic!("{kind:?} import: {e}")); + assert_eq!( + imported.nodes.iter().filter(|n| n.kind == kind).count(), + 1, + "{kind:?} survives the import" + ); + assert_eq!(imported.emit().unwrap().dyd, out.dyd, "{kind:?}"); + } + } + + #[test] + fn nested_native_nodes_keep_their_arguments() { + let mut graph = Graph::new("nested.dyd"); + let root = graph.add_node(NodeKind::OnUpdate, 0.0, 0.0); + let mv = graph.add_node(NodeKind::Translate, 300.0, 0.0); + let clamp = graph.add_node(NodeKind::Clamp, 200.0, 0.0); + let clamp01 = graph.add_node(NodeKind::Clamp01, 100.0, 0.0); + let sin = graph.add_node(NodeKind::Sin, 0.0, 0.0); + graph + .connect(&format!("{root}.out"), &format!("{mv}.in")) + .unwrap(); + graph + .connect(&format!("{sin}.value"), &format!("{clamp01}.a")) + .unwrap(); + graph + .connect(&format!("{clamp01}.value"), &format!("{clamp}.a")) + .unwrap(); + graph + .connect(&format!("{clamp}.value"), &format!("{mv}.y")) + .unwrap(); + let out = graph.emit().unwrap(); + assert!(out + .dyd + .contains("caffeine_math_clamp(caffeine_math_clamp01(caffeine_math_sin(0)), 0, 0)")); + let imported = import_from_dyd(&out.dyd, "nested.dyd", None).unwrap(); + assert!(imported.nodes.iter().any(|n| n.kind == NodeKind::Clamp)); + assert!(imported.nodes.iter().any(|n| n.kind == NodeKind::Clamp01)); + assert_eq!(imported.emit().unwrap().dyd, out.dyd); + } + + #[test] + fn catalog_lists_every_kind_once() { + assert_eq!(catalog().len(), NodeKind::ALL.len()); + for kind in NodeKind::ALL { + let entry = catalog_entry(kind.type_name()).expect("catalog row"); + assert_eq!(entry.kind, kind); + assert_eq!(entry.display_name, kind.display_name()); + assert_eq!(entry.category, kind.category()); + assert_eq!(entry.description, kind.description()); + } + } } diff --git a/doppio/crates/ui/src/uikit/generators.rs b/doppio/crates/ui/src/uikit/generators.rs index 66024998..021e41e6 100644 --- a/doppio/crates/ui/src/uikit/generators.rs +++ b/doppio/crates/ui/src/uikit/generators.rs @@ -922,6 +922,43 @@ impl Default for MaterialGraph { } impl MaterialGraph { + /// Gives every node that has a seed a new one derived from `seed`, so the same graph makes a + /// different but equally valid pattern. Returns how many nodes changed. Deterministic: the + /// same graph and `seed` always give the same result. + pub fn reseed(&mut self, seed: u32) -> usize { + let mut changed = 0; + for node in &mut self.nodes { + let next = |old: i32| { + let mix = crate::uikit::recipe_ref::derive_seed( + u64::from(seed), + 0x5EED, + node.id as i32, + old, + ); + (mix % 9_999) as i32 + 1 + }; + match &mut node.kind { + NodeKind::Noise { seed, .. } + | NodeKind::Voronoi { seed, .. } + | NodeKind::Warp { seed, .. } => { + *seed = next(*seed); + changed += 1; + } + NodeKind::Proc { op, params, .. } => { + let def = op.def(); + if let Some(index) = def.params.iter().position(|spec| spec.key == "seed") { + if let Some(value) = params.get_mut(index) { + *value = next(*value as i32) as f32; + changed += 1; + } + } + } + _ => {} + } + } + changed + } + pub fn add_node(&mut self, kind: NodeKind, position: Point) -> NodeId { let id = self.next_id; self.next_id += 1; diff --git a/doppio/crates/ui/src/uikit/id.rs b/doppio/crates/ui/src/uikit/id.rs index 60d74c94..135e0a2c 100644 --- a/doppio/crates/ui/src/uikit/id.rs +++ b/doppio/crates/ui/src/uikit/id.rs @@ -191,6 +191,7 @@ pub const DRYAD_EMPTY: UiId = UiId::fixed("dryad.nodes.empty"); pub const PANE_VIEWPORT: UiId = UiId::fixed("editor.pane.viewport"); pub const VIEWPORT_MARKERS: UiId = UiId::fixed("viewport.canvas.markers"); pub const TOGGLE_MARKERS: UiId = UiId::fixed("editor.view.toggle_markers"); +pub const TOGGLE_SNAP: UiId = UiId::fixed("editor.view.toggle_snap"); pub const VIEWPORT_GIZMO: UiId = UiId::fixed("viewport.canvas.gizmo"); pub const PANE_INSPECTOR: UiId = UiId::fixed("editor.pane.inspector"); pub const PANE_MATERIAL: UiId = UiId::fixed("editor.pane.material"); @@ -204,6 +205,7 @@ pub const TEXTUREGEN_SIZE: UiId = UiId::fixed("texturegen.size"); pub const TEXTUREGEN_UNDO: UiId = UiId::fixed("texturegen.toolbar.undo"); pub const TEXTUREGEN_REDO: UiId = UiId::fixed("texturegen.toolbar.redo"); pub const TEXTUREGEN_FIT: UiId = UiId::fixed("texturegen.toolbar.fit"); +pub const TEXTUREGEN_RESEED: UiId = UiId::fixed("texturegen.toolbar.reseed"); pub const TEXTUREGEN_SAVE: UiId = UiId::fixed("texturegen.toolbar.save"); pub const TEXTUREGEN_EXPORT: UiId = UiId::fixed("texturegen.toolbar.export"); pub const TEXTUREGEN_SEARCH: UiId = UiId::fixed("texturegen.search"); @@ -387,6 +389,7 @@ pub fn generator_channel(label: &str) -> UiId { pub const GENERATOR_UNDO: UiId = UiId::fixed("generator.toolbar.undo"); pub const GENERATOR_REDO: UiId = UiId::fixed("generator.toolbar.redo"); pub const GENERATOR_FIT: UiId = UiId::fixed("generator.toolbar.fit"); +pub const GENERATOR_RESEED: UiId = UiId::fixed("generator.toolbar.reseed"); pub const GENERATOR_SNAP: UiId = UiId::fixed("generator.toolbar.snap"); pub const GENERATOR_SEARCH: UiId = UiId::fixed("generator.toolbar.search"); pub const GENERATOR_DELETE_LINK: UiId = UiId::fixed("generator.link.delete"); @@ -932,6 +935,11 @@ pub const CATALOG: &[UiMeta] = &[ description: "Scene markers over the viewport: light rays, bulbs and cones, camera frustums, colliders and locators. Click one to select its entity.", kind: "canvas", }, + UiMeta { + id: TOGGLE_SNAP, + description: "Snaps gizmo edits to a grid: 0.25 m for Move, 15 degrees for Rotate, 0.1 for Scale.", + kind: "menu_item", + }, UiMeta { id: TOGGLE_MARKERS, description: "Shows or hides the scene markers (lights, cameras, colliders, empties).", @@ -982,6 +990,11 @@ pub const CATALOG: &[UiMeta] = &[ description: "Redoes the last undone graph edit.", kind: "button", }, + UiMeta { + id: TEXTUREGEN_RESEED, + description: "Gives every seeded node a new seed: a variation of the same texture graph.", + kind: "button", + }, UiMeta { id: TEXTUREGEN_FIT, description: "Frames the whole graph in the canvas.", @@ -1792,6 +1805,11 @@ pub const CATALOG: &[UiMeta] = &[ description: "Redoes the last undone material graph edit.", kind: "button", }, + UiMeta { + id: GENERATOR_RESEED, + description: "Gives every seeded node a new seed: a variation of the same material graph.", + kind: "button", + }, UiMeta { id: GENERATOR_FIT, description: "Fits the whole material graph in the canvas.", diff --git a/doppio/crates/ui/src/uikit/mod.rs b/doppio/crates/ui/src/uikit/mod.rs index e12134b3..a47313d7 100644 --- a/doppio/crates/ui/src/uikit/mod.rs +++ b/doppio/crates/ui/src/uikit/mod.rs @@ -15,7 +15,11 @@ pub mod panels; pub mod prefabs; pub mod procedural; pub mod project; +pub mod recipe; +pub mod recipe_ref; pub mod script; +pub mod script_assist; +pub mod script_reference; pub mod terrain; pub mod texture_graph; pub mod texture_set; diff --git a/doppio/crates/ui/src/uikit/panels/assetmanager.rs b/doppio/crates/ui/src/uikit/panels/assetmanager.rs index 904c4b15..e391fc90 100644 --- a/doppio/crates/ui/src/uikit/panels/assetmanager.rs +++ b/doppio/crates/ui/src/uikit/panels/assetmanager.rs @@ -116,6 +116,7 @@ fn entry_icon(kind: &str, is_directory: bool) -> Icon { "script" => Icon::Script, "asset" => Icon::Scene, "animation" => Icon::Player, + "generator" => Icon::Generic, _ => Icon::File, } } diff --git a/doppio/crates/ui/src/uikit/panels/dryad_nodes.rs b/doppio/crates/ui/src/uikit/panels/dryad_nodes.rs index 9909aad9..e8d5f5f4 100644 --- a/doppio/crates/ui/src/uikit/panels/dryad_nodes.rs +++ b/doppio/crates/ui/src/uikit/panels/dryad_nodes.rs @@ -554,12 +554,18 @@ fn properties_panel<'a, Message: Clone + 'a>( .color(theme::TEXT_MUTED), ); } - NodeKind::Log => { + NodeKind::Log | NodeKind::KeyDown | NodeKind::ActionDown | NodeKind::Axis => { let emit = emit_text.clone(); let id = node_id.clone(); + let label = match kind { + NodeKind::KeyDown => "Key", + NodeKind::ActionDown => "Action", + NodeKind::Axis => "Axis", + _ => "Message", + }; body = body.push(text_field::property( (ids.property)("text"), - "Message", + label, &node.text_value, move |value| emit(id.clone(), value), )); @@ -574,7 +580,14 @@ fn properties_panel<'a, Message: Clone + 'a>( body = body.push(status::chip("No exec link yet", theme::ChipTone::Warning)); } } - NodeKind::GetPosition | NodeKind::GetScale | NodeKind::DeltaTime => { + NodeKind::DestroySelf => { + body = body.push( + text("Removes the entity when this exec chain reaches it.") + .size(9) + .color(theme::TEXT_MUTED), + ); + } + _ => { body = body.push( text("Read-only data node: drag one of its pins into an input.") .size(9) @@ -652,6 +665,27 @@ fn accent(kind: NodeKind) -> iced::Color { NodeKind::Log => theme::TEXT_MUTED, NodeKind::Constant => theme::BORDER_STRONG, NodeKind::Branch => theme::WARNING, + NodeKind::KeyDown | NodeKind::ActionDown | NodeKind::Axis => theme::ICON_CYAN, + NodeKind::TotalTime => theme::ICON_PURPLE, + NodeKind::DestroySelf => theme::DANGER, + NodeKind::Add + | NodeKind::Subtract + | NodeKind::Multiply + | NodeKind::Divide + | NodeKind::Negate => theme::ICON_TEAL, + NodeKind::Less | NodeKind::Greater | NodeKind::Equal => theme::WARNING, + NodeKind::And | NodeKind::Or | NodeKind::Not => theme::ICON_PURPLE, + NodeKind::SelfEntity | NodeKind::IsAlive | NodeKind::Distance => theme::DANGER, + other => { + let category = other.category(); + if category.contains("Input") { + theme::ICON_CYAN + } else if category.contains("Random") || category.contains("Time") { + theme::ICON_PURPLE + } else { + theme::ICON_TEAL + } + } } } diff --git a/doppio/crates/ui/src/uikit/panels/material_generator.rs b/doppio/crates/ui/src/uikit/panels/material_generator.rs index a9e6adf9..4c5f55b2 100644 --- a/doppio/crates/ui/src/uikit/panels/material_generator.rs +++ b/doppio/crates/ui/src/uikit/panels/material_generator.rs @@ -234,6 +234,8 @@ pub enum Edit { Undo, Redo, Fit, + /// New random seeds for every node that has one (a variation of the same graph). + Reseed, SetSnap(bool), SetSearch(String), TogglePaletteGroup(String), @@ -358,6 +360,7 @@ pub fn view<'a, Message: Clone + 'a>( button::compact(id::GENERATOR_UNDO, "↶", emit(Edit::Undo)), button::compact(id::GENERATOR_REDO, "↷", emit(Edit::Redo)), button::compact(id::GENERATOR_FIT, "Fit", emit(Edit::Fit)), + button::compact(id::GENERATOR_RESEED, "Reseed", emit(Edit::Reseed)), container(resolution).width(Length::Fixed(138.0)), button::compact( id::GENERATOR_OPEN_MATERIAL, diff --git a/doppio/crates/ui/src/uikit/panels/project_manager.rs b/doppio/crates/ui/src/uikit/panels/project_manager.rs index bc55b9b8..0cf3280d 100644 --- a/doppio/crates/ui/src/uikit/panels/project_manager.rs +++ b/doppio/crates/ui/src/uikit/panels/project_manager.rs @@ -318,12 +318,7 @@ pub fn view<'a, Message: Clone + 'a>( )); } column![ - text_field::stacked( - id::PROJECT_NAME, - "Project name", - &state.name, - on_name, - ), + text_field::stacked(id::PROJECT_NAME, "Project name", &state.name, on_name,), row![location, default_location] .spacing(4) .align_y(iced::Alignment::End), @@ -335,10 +330,11 @@ pub fn view<'a, Message: Clone + 'a>( space().height(Length::Fixed(8.0)), button::primary(id::PROJECT_CREATE, "Create & Open", on_create), text(if state.name.trim().is_empty() { - "Enter a name to create project.caffeine with assets/raw, scenes, materials and texture_sets.".to_owned() + "Enter a name to create project.caffeine with Assets, Scripts and Scenes." + .to_owned() } else { format!( - "Creates {}/{}/project.caffeine with assets/raw, scenes, materials and texture_sets.", + "Creates {}/{}/project.caffeine with Assets, Scripts and Scenes.", state.location.trim_end_matches(['/', '\\']), state.name.trim() ) diff --git a/doppio/crates/ui/src/uikit/panels/script.rs b/doppio/crates/ui/src/uikit/panels/script.rs index 7ff1be34..1f862dff 100644 --- a/doppio/crates/ui/src/uikit/panels/script.rs +++ b/doppio/crates/ui/src/uikit/panels/script.rs @@ -46,6 +46,7 @@ pub struct Callbacks<'a, Message> { pub on_select: Box Message + 'a>, pub on_tab_close: Box Message + 'a>, pub on_action: Box Message + 'a>, + pub on_overlay: Box Message + 'a>, pub on_save: Message, pub on_revert: Message, pub on_close: Message, @@ -62,6 +63,7 @@ pub fn view<'a, Message: Clone + 'a>( on_select, on_tab_close, on_action, + on_overlay, on_save, on_revert, on_close, @@ -139,7 +141,13 @@ pub fn view<'a, Message: Clone + 'a>( } let body: Element<'a, Message> = match active { - Some(document) => code_editor::view(ids.editor, &document.editor, on_action), + Some(document) => code_editor::view_with_reference( + ids.editor, + &document.editor, + &document.overlay, + on_action, + on_overlay, + ), None => container( text("Select a script tab.") .size(10) diff --git a/doppio/crates/ui/src/uikit/panels/texture_generator.rs b/doppio/crates/ui/src/uikit/panels/texture_generator.rs index ce91beaa..a741b276 100644 --- a/doppio/crates/ui/src/uikit/panels/texture_generator.rs +++ b/doppio/crates/ui/src/uikit/panels/texture_generator.rs @@ -160,6 +160,11 @@ pub fn view<'a, Message: Clone + 'a>( "Fit", emit(Edit::Node(nodes_panel::Edit::Fit)) ), + button::compact( + id::TEXTUREGEN_RESEED, + "Reseed", + emit(Edit::Node(nodes_panel::Edit::Reseed)) + ), container(size).width(Length::Fixed(138.0)), button::compact(id::TEXTUREGEN_SAVE, "Save", emit(Edit::Save)), button::primary(id::TEXTUREGEN_EXPORT, "Export PNG", emit(Edit::Export)), diff --git a/doppio/crates/ui/src/uikit/project.rs b/doppio/crates/ui/src/uikit/project.rs index 3182b5e7..771eff7f 100644 --- a/doppio/crates/ui/src/uikit/project.rs +++ b/doppio/crates/ui/src/uikit/project.rs @@ -1,8 +1,9 @@ //! Project manifest and recent-project storage. //! //! The format matches the existing `project.caffeine` convention: a small JSON -//! file at the project root plus `assets/raw`, `assets/processed`, `scripts`, -//! `scenes`, `materials`, `texture_sets` and `build` folders. +//! file at the project root plus `Assets/Raw`, `Assets/Processed`, `Scripts`, +//! `Scenes` and `Build` folders. Folders the editor creates are capitalised +//! (`Materials`, `Textures`, `Generators`); a user may rename them freely. use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf}; @@ -30,15 +31,15 @@ impl Default for ProjectPaths { } fn default_assets_raw() -> String { - "assets/raw".to_owned() + "Assets/Raw".to_owned() } fn default_assets_processed() -> String { - "assets/processed".to_owned() + "Assets/Processed".to_owned() } fn default_scripts() -> String { - "scripts".to_owned() + "Scripts".to_owned() } fn default_engine_version() -> String { @@ -88,7 +89,7 @@ impl ProjectInfo { pub fn scene_path(&self) -> Option { if self.last_scene.is_empty() { - let default = self.root.join("scenes").join("main.caf"); + let default = scenes_dir(&self.root).join("main.caf"); return default.exists().then_some(default); } let relative = Path::new(&self.last_scene); @@ -101,6 +102,16 @@ impl ProjectInfo { } } +/// Scene folder of a project: `Scenes`, or a legacy lowercase `scenes` that already exists. +pub fn scenes_dir(root: &Path) -> PathBuf { + let legacy = root.join("scenes"); + if legacy.is_dir() && !root.join("Scenes").is_dir() { + legacy + } else { + root.join("Scenes") + } +} + /// `$CAFFEINE_PROJECTS_DIR`, then `~/Documents/CaffeineProjects`. pub fn default_projects_directory() -> PathBuf { if let Some(directory) = std::env::var_os("CAFFEINE_PROJECTS_DIR") { @@ -164,21 +175,20 @@ pub fn create_project(location: &Path, name: &str, template: &str) -> Result