[][src]Crate gazpatcho

Simple node-based graph editor for Rust. Register nodes, let the user mingle with them, read the result.

Example

The following example initializes the application with a single node type. Eeach node will have one input and one output pin, and a "switch" widget that can be set on or off by the user.

First we need to define the config for our application. The config describes all the node templates available for the user.

use gazpatcho::config::*;
use gazpatcho::request::*;
use gazpatcho::report::*;

let config = Config {
    node_templates: vec![
        NodeTemplate {
            label: "Example node".to_owned(),
            class: "example_node".to_owned(),
            display_heading: true,
            pins: vec![
                Pin {
                    label: "Input".to_owned(),
                    class: "in".to_owned(),
                    direction: Input,
                },
                Pin {
                    label: "Output".to_owned(),
                    class: "out".to_owned(),
                    direction: Output,
                },
            ],
            widgets: vec![Switch {
                label: "Switch".to_owned(),
                key: "switch".to_owned(),
            }],
        }
    ],
};

The we start the application, this will open a new window with the canvas. We are passing the previously defined config and also a callback function. This callback will be executed every time the user updates the graph represented in the UI. The response of the callback can initiate additional changes on the graph.

gazpatcho::run_with_callback("Application Name", config, |report| {
    // Act upon the current report
    dbg!(report);

    // Respond with change requests
    vec![
        // Request::SetValue { ... }
    ]
});

An alternative method is to receive reports and send requests over a mpsc channel. This method allows for asynchronous updated of the graph initiated by the user code.

use std::sync::mpsc;
use std::thread;

let (report_tx, report_rx) = mpsc::channel::<Report>();
let (request_tx, request_rx) = mpsc::channel::<Request>();

thread::spawn(move || {
    // Act upon the current report
    for report in report_rx {
        dbg!(report);

        // Respond with change request
        // request_tx.send(Request::SetValue { ... }).unwrap();
    }
});

gazpatcho::run_with_mpsc("Application Name", config, report_tx, request_rx);

The dbg! output of such a configuration would return something like:

Report {
    nodes: [
        Node {
            id: "example_node:0",
            class: "example_node",
            data: {
                "switch": Bool(
                    false,
                ),
            },
        },
        Node {
            id: "example_node:1",
            class: "example_node",
            data: {
                "switch": Bool(
                    true,
                ),
            },
        },
    ],
    patches: [
        Patch {
            source: PinAddress {
                node_id: "example_node:0",
                pin_class: "out",
            },
            destination: PinAddress {
                node_id: "example_node:1",
                pin_class: "in",
            },
        },
    ],
}

To see the list of all available widgets and detailed documentation of the state, read the config documentation. To learn more about the reported state, read the report documentation.

If you prefer to go directly for a code examples, take a look at the examples folder.

Modules

config

Configuration defining available node types, pins and widgets that will be available in given application instance.

model

Types repesenting components of the graph.

report

Definition of the current state of the graph modeled in the UI.

request

Control running graph instance from the code.

Functions

runDeprecated

Launch the user interface, feed updates to the given callback.

run_with_callback

Launch the user interface, use a callback to broadcast updates and accept additional requests.

run_with_mpsc

Launch the user interface, use mpsc to broadcast updates and accept additional requests.