Giter VIP home page Giter VIP logo

lib-blockly's Introduction

About

You love creating fantastic worlds and stories in Foundry, but you're afraid of macros? Would you rather plan your campaign than lear how to program?

LibBlockly offers a visual macro editor integrated with Foundry using Google Blockly technology. This type of editor is generally used for the initiation to programming, so it is easy to access: only the understanding of the algorithmic logic is necessary.

LibBlockly is also extensible framework which allow developers to create new blocks for general purposes or to help people to use their own modules.

Minimum Core Version: 0.9.245

Tutorials

You can find first steps guides here : English Français

Demo

How to create new Blockly Macro : new_macro

Hello, World !

HelloWorld

Let's prepare a battle ! image

Drag&Drop support

drag&drop

(more to come 😊)

Roadmap

The current roadmap is here : https://github.com/MiahNelah/lib-blockly/projects/1

Extensibility

You can easily add new custom blocks using Google Blockly documentation and Blockly Developer Tools. To create a new custom block, create a new class on the following template, then register the new block with Block Manager.

First, we need to specify our own custom block definition:

// "message0", "colour", "tooltip" and "helpUrl" will be pulled from translation file for you.
// Because we often need to translate strings inside definition, we expose defnitions as function to grant access to game.i18n.localise() helper.
const blocksDefinition = function() {
    return {
        "Foundry.Utils.Wait": {            
            "args0": [
                {
                    "type": "field_number",
                    "name": "delay",
                    "value": 1,
                    "min": 0
                },
                {
                    "type": "field_dropdown",
                    "name": "units",
                    "options": [
                        [game.i18n.localize("LibBlockly.Blocks.Foundry.Utils.Wait.Seconds"), "s"],
                        [game.i18n.localize("LibBlockly.Blocks.Foundry.Utils.Wait.Milliseconds"), "ms"]
                    ]
                }
            ],
            "previousStatement": null,
            "nextStatement": null,
        }
    }
}

Then, we need to add required translations. "Title", "Tooltip", "HelpUrl" and "Colour" are required. If you want to custimise "LibBlockly.Blocks" prefix, please have a look at CustomBlock constructor's parameters.

{
  "LibBlockly.Blocks.Foundry.Utils.Wait.Title": "Wait %1 %2",
  "LibBlockly.Blocks.Foundry.Utils.Wait.Tooltip": "",
  "LibBlockly.Blocks.Foundry.Utils.Wait.HelpUrl": "",
  "LibBlockly.Blocks.Foundry.Utils.Wait.Colour": "230",
}

We finally define a custom class to handle code generation:

class WaitCustomBlock extends CustomBlock {
    constructor() {
        super("Wait", "Foundry.Utils");
    }
  
    // The generateCode() describe how code is generated for your new block.
    // Use Blockly Developer Tools to get parameter resolution statements.
    generateCode(block) {
        const number_delay = block.getFieldValue('delay');
        const dropdown_units = block.getFieldValue('units');

        // In this specific block, we dynamically define a helper method to handle setTimeout promise.
        // This method will only be generated once if needed, and won't be if block is never used.
        // Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ is a placeholder to reference method hersel (useful for recursive calls).
        const delayHelper = Blockly.JavaScript.provideFunction_("blockly_delay_helper", [
            `function ${Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_}(delay) {`,
            `  return new Promise(resolve => {`,
            `    setTimeout(() => resolve(2), delay);`,
            `  });`,
            `}`
        ]);

        return `await ${delayHelper}(${dropdown_units === "s" ? number_delay * 1000 : number_delay});\n`;
    }
}

Let's stick all together !

Hooks.once("ready", () => {
    libBlockly.registerDefinitions(blocksDefinition());    

    libBlockly.registerBlockTypes([
        new WaitCustomBlock()
    ]);
 })

lib-blockly's People

Contributors

miahnelah avatar

Stargazers

 avatar  avatar Joe Shea avatar Gridrunner64 avatar  avatar  avatar Tobias Franz avatar Hugo Prudente avatar

Watchers

 avatar

lib-blockly's Issues

« Convert to JavaScript » make workspace blank

When converting workspace to JavaScript, macro config switch to script type. When doing so, sometimes blockly’s workspace become blank.
Workaround: workspace is still here and not corrupted. Switch to Blockly type then save your macro. Once you reopen it, workspace should be correctly loaded.

"Variables" are empty

Hi there,

I tried this module, but I even lack basic variables like in the Tutorial-GIFs.
image
Edit: "create variables" does nothing btw.

How can I update this?

macro.execute wrapper does not seem to return the macro.execute promise

I was having a play - great work by the way.

I noticed that with libBlockly installed modules that await macro.execute are not blocking, this came to my notice when testing chris's premade items. It seems to be the line

  libBlockly._handleMacroExecution(this, wrapped, ...args);

if I change that to

  return libBlockly._handleMacroExecution(this, wrapped, ...args);

things work as expected.

Create "Journal" custom blocks

  • Get journal from world
  • Get journal from compendium
  • Get all journals in scene
  • Open journal
  • Show journal to players as image
  • Show journal to players as text

"Toggle combat state" create duplicates

When using "Toggle combat state" on multiple tokens, blocks create one encounter per token selected. Moreoever, in each encounter, tokens are added multiple times.

Prevent blocks corruption

There are certain use cases in which a macro can be corrupted. This happens very often when a block has been removed from the module but the macro continues to use that block. The corruption is only effective when the definition of the block changes, the modification of the code generator has no impact.

The idea is therefore to analyze the workspace when opening a macro to check if all blocks are declared, and possibly remove the problematic blocks without deleting the entire workspace.

Keyboard shortcut are sent to canvas

When pressing keys or shortcut inside a Blockly editor, all key presses are sent to loaded canvas behind. It’s so possible to break something in the scene.

Create "Playlist" custom blocks

  • Play/Stop a playlist
  • Next/Previous track
  • Change global volume
  • Change playlist volume
  • Set/unset loop mode
  • Change fade duration

Create "Encounter" custom blocks

  • Start encounter
  • End encounter
  • Next turn
  • Previous turn
  • Roll initiative for non-player characters
  • Roll initiative for everyone
  • Add/Remove token from combat

Create "Item" custom blocks

  • Get item from world
  • Get item by ID
  • Get item from compendium
  • Get token's items
  • Add/Remove item to actor/token

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.