Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-vector

Source of release 0.6.0

0.6.0 / src / graph / nodes.lucb

#==============================================================================================
#
#   graph/nodes - What each kind of node is: its ports and its parameters
#
#   DESCRIPTION:
#       A vector layer's node graph (model.lucb) is made of nodes of a few kinds, each
#       a number the file keeps. A kind has a title, at most one output, a few inputs and a
#       row of parameters held in its node's `values`, by slot. An input is either data
#       (Shapes or Points, which only a wire gives) or a socket that drives one parameter
#       from a Number or Color wire, sampled per copy where that means something (a Clone's
#       scale, say): what a Random or Gradient node puts out is a value per index.
#
#       The catalog is built by functions, not tables, so the editor, the evaluator and the
#       files all ask the same place and a new kind is one more branch here.
#
#==============================================================================================

## What a wire carries.
let port_shapes: i64 = 0
let port_points: i64 = 1
let port_number: i64 = 2
let port_color: i64 = 3

## The kinds, by the number files keep; `node_kinds` counts them.
pub let node_output: i64 = 0
pub let node_rectangle: i64 = 1
pub let node_ellipse: i64 = 2
pub let node_polygon: i64 = 3
pub let node_star: i64 = 4
pub let node_path: i64 = 5
pub let node_grid: i64 = 6
pub let node_circle: i64 = 7
pub let node_line: i64 = 8
pub let node_scatter: i64 = 9
pub let node_outline: i64 = 10
pub let node_clone: i64 = 11
pub let node_transform: i64 = 12
pub let node_merge: i64 = 13
pub let node_number: i64 = 14
pub let node_random: i64 = 15
pub let node_gradient: i64 = 16
pub let node_kinds: i64 = 17

## How a parameter is edited: a number, a whole number, a toggle or a color (three
## slots of linear light).
let param_number: i64 = 0
let param_whole: i64 = 1
let param_toggle: i64 = 2
let param_color: i64 = 3

## Slots a node's values have.
pub let node_slots: usize = 24

## The slots every shape generator shares, then the kind's own two.
pub let slot_x: usize = 0
pub let slot_y: usize = 1
pub let slot_width: usize = 2
pub let slot_height: usize = 3
let slot_rotation: usize = 4
let slot_filled: usize = 5
pub let slot_fill: usize = 6
let slot_stroked: usize = 9
let slot_stroke: usize = 10
let slot_stroke_width: usize = 13
pub let slot_special: usize = 14
let slot_special_2: usize = 15
let slot_keep_paint: usize = 16

pub struct ParamInfo:
    pub var name: str
    pub var kind: i64
    pub var slot: usize
    pub var low: f64
    pub var high: f64

pub struct InputInfo:
    pub var name: str
    ## A port type.
    pub var port_type: i64
    ## The parameter slot a Number or Color wire drives; -1 for data, or for a
    ## socket with no value of its own (a Clone's Fill).
    pub var slot: i64

## One kind's catalog entry.
pub struct NodeInfo:
    pub var title: str
    ## Its output's port type; -1 for none (the Output node).
    pub var output: i64
    pub var inputs: InputInfo[6]
    pub var input_count: usize
    pub var params: ParamInfo[16]
    pub var param_count: usize
    pub var defaults: f64[24]

    pub func param(name: str, kind: i64, slot: usize, value: f64, low: f64, high: f64):
        self.params[self.param_count] = ParamInfo(name = name, kind = kind, slot = slot, low = low, high = high)
        self.param_count += 1
        self.defaults[slot] = value

    pub func color(name: str, slot: usize, red: f64, green: f64, blue: f64):
        self.param(name, param_color, slot, red, 0.0, 1.0)
        self.defaults[slot + 1] = green
        self.defaults[slot + 2] = blue

    pub func input(name: str, port_type: i64, slot: i64):
        self.inputs[self.input_count] = InputInfo(name = name, port_type = port_type, slot = slot)
        self.input_count += 1

## The catalog entry of `kind`; an unknown kind is an Output with no inputs.
pub func node_info(kind: i64) -> NodeInfo:
    var info: NodeInfo
    info.output = port_shapes
    if kind == node_rectangle or kind == node_ellipse or kind == node_polygon or kind == node_star:
        info.title = match kind:
            1 => "Rectangle"
            2 => "Ellipse"
            3 => "Polygon"
            _ => "Star"
        shape_params(&info, 160.0, 160.0 if kind != node_rectangle else 100.0)
        if kind == node_rectangle:
            info.param("Corner Radius", param_number, slot_special, 0.0, 0.0, 1000.0)
        elif kind == node_polygon:
            info.param("Sides", param_whole, slot_special, 6.0, 3.0, 64.0)
        elif kind == node_star:
            info.param("Points", param_whole, slot_special, 5.0, 2.0, 64.0)
            info.param("Inner Radius", param_number, slot_special_2, 0.45, 0.05, 1.0)
        info.input("Fill", port_color, (i64)slot_fill)
        return info
    if kind == node_path:
        info.title = "Path"
        info.param("Element", param_whole, slot_special, 0.0, 0.0, 9999.0)
        info.param("Move X", param_number, slot_x, 0.0, -100000.0, 100000.0)
        info.param("Move Y", param_number, slot_y, 0.0, -100000.0, 100000.0)
        info.param("Scale", param_number, slot_width, 1.0, 0.01, 100.0)
        info.param("Rotation", param_number, slot_rotation, 0.0, -360.0, 360.0)
        info.param("Keep Paint", param_toggle, slot_keep_paint, 1.0, 0.0, 1.0)
        paint_params(&info)
        info.input("Fill", port_color, (i64)slot_fill)
        return info
    info.output = port_points
    if kind == node_grid:
        info.title = "Grid"
        info.param("Columns", param_whole, 0, 6.0, 1.0, 1000.0)
        info.param("Rows", param_whole, 1, 4.0, 1.0, 1000.0)
        info.param("Spacing X", param_number, 2, 160.0, 0.0, 100000.0)
        info.param("Spacing Y", param_number, 3, 160.0, 0.0, 100000.0)
        info.param("Center X", param_number, 4, 0.0, -100000.0, 100000.0)
        info.param("Center Y", param_number, 5, 0.0, -100000.0, 100000.0)
        info.input("Columns", port_number, 0)
        info.input("Rows", port_number, 1)
        return info
    if kind == node_circle:
        info.title = "Circle"
        info.param("Count", param_whole, 0, 12.0, 1.0, 100000.0)
        info.param("Radius", param_number, 1, 240.0, 0.0, 100000.0)
        info.param("Rotation", param_number, 2, 0.0, -360.0, 360.0)
        info.param("Center X", param_number, 3, 0.0, -100000.0, 100000.0)
        info.param("Center Y", param_number, 4, 0.0, -100000.0, 100000.0)
        info.input("Count", port_number, 0)
        info.input("Radius", port_number, 1)
        return info
    if kind == node_line:
        info.title = "Line"
        info.param("Count", param_whole, 0, 8.0, 1.0, 100000.0)
        info.param("Start X", param_number, 1, 0.0, -100000.0, 100000.0)
        info.param("Start Y", param_number, 2, 0.0, -100000.0, 100000.0)
        info.param("End X", param_number, 3, 400.0, -100000.0, 100000.0)
        info.param("End Y", param_number, 4, 0.0, -100000.0, 100000.0)
        info.input("Count", port_number, 0)
        return info
    if kind == node_scatter:
        info.title = "Random Scatter"
        info.param("Count", param_whole, 0, 100.0, 0.0, 1000000.0)
        info.param("Seed", param_whole, 1, 1.0, 0.0, 1000000.0)
        info.param("Left", param_number, 2, 0.0, -100000.0, 100000.0)
        info.param("Top", param_number, 3, 0.0, -100000.0, 100000.0)
        info.param("Width", param_number, 4, 800.0, 0.0, 1000000.0)
        info.param("Height", param_number, 5, 600.0, 0.0, 1000000.0)
        info.input("Inside", port_shapes, -1)
        info.input("Count", port_number, 0)
        return info
    if kind == node_outline:
        info.title = "Points of Shape"
        info.param("Along Outline", param_toggle, 0, 0.0, 0.0, 1.0)
        info.param("Count", param_whole, 1, 24.0, 1.0, 100000.0)
        info.input("Shapes", port_shapes, -1)
        info.input("Count", port_number, 1)
        return info
    info.output = port_shapes
    if kind == node_clone:
        info.title = "Clone"
        info.param("Rotate to Point", param_toggle, 0, 1.0, 0.0, 1.0)
        info.param("Scale by Point", param_toggle, 1, 1.0, 0.0, 1.0)
        info.param("Scale", param_number, 6, 1.0, 0.0, 1000.0)
        info.param("Rotation", param_number, 7, 0.0, -360.0, 360.0)
        info.param("Jitter Rotation", param_number, 2, 0.0, 0.0, 360.0)
        info.param("Jitter Scale", param_number, 3, 0.0, 0.0, 1.0)
        info.param("Jitter Color", param_number, 4, 0.0, 0.0, 1.0)
        info.param("Seed", param_whole, 5, 1.0, 0.0, 1000000.0)
        info.input("Shapes", port_shapes, -1)
        info.input("Points", port_points, -1)
        info.input("Scale", port_number, 6)
        info.input("Rotation", port_number, 7)
        info.input("Fill", port_color, -1)
        return info
    if kind == node_transform:
        info.title = "Transform"
        info.param("Move X", param_number, 0, 0.0, -100000.0, 100000.0)
        info.param("Move Y", param_number, 1, 0.0, -100000.0, 100000.0)
        info.param("Rotation", param_number, 2, 0.0, -360.0, 360.0)
        info.param("Scale X", param_number, 3, 1.0, -100.0, 100.0)
        info.param("Scale Y", param_number, 4, 1.0, -100.0, 100.0)
        info.input("Shapes", port_shapes, -1)
        info.input("Rotation", port_number, 2)
        return info
    if kind == node_merge:
        info.title = "Merge"
        info.input("A", port_shapes, -1)
        info.input("B", port_shapes, -1)
        info.input("C", port_shapes, -1)
        info.input("D", port_shapes, -1)
        return info
    if kind == node_number:
        info.title = "Number"
        info.output = port_number
        info.param("Value", param_number, 0, 1.0, -1000000.0, 1000000.0)
        return info
    if kind == node_random:
        info.title = "Random"
        info.output = port_number
        info.param("Min", param_number, 0, 0.5, -1000000.0, 1000000.0)
        info.param("Max", param_number, 1, 1.5, -1000000.0, 1000000.0)
        info.param("Seed", param_whole, 2, 1.0, 0.0, 1000000.0)
        return info
    if kind == node_gradient:
        info.title = "Color Gradient"
        info.output = port_color
        info.color("Start", 0, 0.8069, 0.3515, 0.0497)
        info.color("End", 3, 0.05, 0.2, 0.8)
        info.param("Random", param_toggle, 6, 0.0, 0.0, 1.0)
        info.param("Seed", param_whole, 7, 1.0, 0.0, 1000000.0)
        return info
    info.title = "Output"
    info.output = -1
    info.input("Shapes", port_shapes, -1)
    return info

## The parameters every shape generator has: where its middle is, its size, its
## turn, and its paint.
func shape_params(info: NodeInfo*, width: f64, height: f64):
    info.param("Position X", param_number, slot_x, 0.0, -100000.0, 100000.0)
    info.param("Position Y", param_number, slot_y, 0.0, -100000.0, 100000.0)
    info.param("Width", param_number, slot_width, width, 0.0, 100000.0)
    info.param("Height", param_number, slot_height, height, 0.0, 100000.0)
    info.param("Rotation", param_number, slot_rotation, 0.0, -360.0, 360.0)
    paint_params(info)

## A fill in the application's orange, no stroke until asked for.
func paint_params(info: NodeInfo*):
    info.param("Fill", param_toggle, slot_filled, 1.0, 0.0, 1.0)
    info.color("Fill Color", slot_fill, 0.8069, 0.3515, 0.0497)
    info.param("Stroke", param_toggle, slot_stroked, 0.0, 0.0, 1.0)
    info.color("Stroke Color", slot_stroke, 0.0, 0.0, 0.0)
    info.param("Stroke Width", param_number, slot_stroke_width, 2.0, 0.0, 1000.0)

## Whether an output of type `from` may feed an input of type `to`: the same
## type, or a Number where a Color goes (a gray).
func ports_fit(source_id: i64, to: i64) -> bool:
    return source_id == to or (source_id == port_number and to == port_color)

## A port type's name, as the editor says it.
func port_name(port_type: i64) -> str:
    return match port_type:
        0 => "Shapes"
        1 => "Points"
        2 => "Number"
        _ => "Color"