GithubHelp home page GithubHelp logo

cx-gd's Introduction

A godot 4 declarative and reactive scene management library

This is made for personal projects, don't expect anything.

This project abuses the new lambda feature of gdscript to declaritively manage the scene tree via concepts you might expect from modern UI libraries:

  • Reactive values
  • Mapping values to children
  • Selective scene rebuilding

and there is 0 (rounded down) magic trickery going on to achieve this and extends beyond just UI applications.

"Simple" example introducing key concepts:

class_name Demo
extends Control

# simple named widget constructor, note how it returns a function
var button_widget = func(index: Tracked, text: String): return func(c: Ctx):
    # create a Button node
    c.inherits(Widgets.button())
    # concatinate tracked index with static text value
    var concat = Cx.map(index, func(i): return str(i) + ". " + text)
    # set the text property of the Button with the reactive label
    c.with("text", concat)
    # hide the node when text is empty by mapping the text value
    c.with("visible", !text.is_empty())

func _ready():
    # considered "inside node" thus we need to create or get a context
    var c: Ctx = Cx.get_or_init(self) 
    # create a reactive array of values
    var labels := Cx.array(["hello", "world", "", "last"])
    # add a child, this can be a plain Node deriving type,
    # or more interestingly a function taking Ctx
    c.child(func(c: Ctx):
        # "inherit" from container, this will be the node in the scene tree
        c.inherits(VBoxContainer)
        # for each label instance a label widget
        # note how `v` is NOT tracked but `i` is
        c.child_opt(Cx.map_children(labels, func(i, v): return func(c: Ctx):
            # inherit external widget
            c.inherits(button_widget.call(i, v))
            # add a click handler to remove clickee,
            # this will automatically remove this node from the tree as we "declared" it to be so
            c.on("pressed", func(): labels.remove_at(i.value()))
        ))
    )

cx-gd's People

Contributors

ruthgerd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.