# Defold Druid UI Library

## Custom components

Add your custom components via druid.register

    local druid = require("druid.druid")
    local my_component = require("my.amazing.component")

    local function init(self)
        druid.register("my_component", my_component)
    end

Basic custom component template looks like this:

    local const = require("druid.const")

    local M = {}
    M.interest = { const.ON_INPUT }

    function M.init(self, ...)
        -- Component constructor
    end

    -- Call only if exist interest: const.ON_UPDATE
    function M.update(self, dt)

    end

    -- Call only if exist interest: const.ON_INPUT or const.ON_SWIPE
    function M.on_input(self, action_id, action)

    end

    -- Call only if exist interest: const.ON_MESSAGE
    function M.on_message(self, message_id, message, sender)

    end

    -- Call only if swipe was started on another component (ex. scroll)
    function M.on_swipe(self)

    end

    return M

## Best practice on custom components

On each component recomended describe component schema in next way:

    -- Component module
    local helper = require("druid.helper")

    local M = {}

    local SCHEME = {
        ROOT = "/root",
        ITEM = "/item",
        TITLE = "/title"
    }

    -- TODO: Rework self.template/self.nodes
    -- Make self._inner_data? { component_name, template, nodes }
    function M.init(self, template_name, node_table)
        -- If component use template, setup it:
     self.template = template_name

        -- If component was cloned with gui.clone_tree, pass his nodes
     self.nodes = node_table

        -- helper can get node from gui/template/table
     local root = helper.node(self, SCHEME.ROOT)

        -- This component can spawn another druid components:
     local druid = helper.get_druid(self)
        -- Button self on callback is self of _this_ component
     local button = druid:new_button(...)

        -- helper can return you the component style
     local my_style = helper.get_style(self, "component_name")
    end

generated by LDoc 1.4.6 Last updated 2020-03-21 22:00:04