Module Druid
Druid UI Component Framework.
# Overview #
Druid - powerful Defold component UI library. Use basic and extended Druid components or make your own game-specific components to make amazing GUI in your games.
To start using Druid, please refer to the Usage section below.
# Notes #
• Each Druid instance maintains the self context from the constructor and passes it to each Druid callback.
See next: DruidInstance
Usage:
local druid = require("druid.druid") local function on_play(self) print("Gonna play!") end function init(self) self.druid = druid.new(self) self.druid:new_button("button_play", on_play) end function final(self) self.druid:final() end function update(self, dt) self.druid:update(dt) end function on_message(self, message_id, message, sender) self.druid:on_message(message_id, message, sender) end function on_input(self, action_id, action) return self.druid:on_input(action_id, action) end
Functions
druid.new(context, style) | Create a new Druid instance for creating GUI components. |
druid.on_language_change() | Call this function when the game language changes. |
druid.on_window_callback(event) | Set the window callback to enable on_focus_gain and on_focus_lost functions. |
druid.register(name, module) | Register a new external Druid component. |
druid.set_default_style(style) | Set your own default style for all Druid instances. |
druid.set_sound_function(callback) | Set the Druid sound function to play UI sounds if used. |
druid.set_text_function(callback) | Set the text function for the LangText component. |
Functions
- druid.new(context, style)
-
Create a new Druid instance for creating GUI components.
Parameters:
- context table The Druid context. Usually, this is the self of the gui_script. It is passed into all Druid callbacks.
- style table or nil The Druid style table to override style parameters for this Druid instance.
Returns:
-
druid_instance
The Druid instance DruidInstance.
Usage:
local druid = require("druid.druid") function init(self) self.druid = druid.new(self) end
- druid.on_language_change()
-
Call this function when the game language changes.
This function will translate all current LangText components.
Usage:
druid.on_language_change()
- druid.on_window_callback(event)
-
Set the window callback to enable on_focus_gain and on_focus_lost functions.
This is used to trigger the on_focus_lost and on_focus_gain functions in Druid components.
Parameters:
- event string Event param from window listener
Usage:
window.set_listener(function(_, event) druid.on_window_callback(event) end)
- druid.register(name, module)
-
Register a new external Druid component.
You can register your own components to make new alias: the druid:new_{name} function. For example, if you want to register a component called "my_component", you can create it using druid:new_my_component(...). This can be useful if you have your own "basic" components that you don't want to re-create each time.
Parameters:
Usage:
local my_component = require("path.to.my.component") druid.register("my_component", my_component) ... local druid = druid.new(self) local component_instance = self.druid:new_my_component(...)
- druid.set_default_style(style)
-
Set your own default style for all Druid instances.
To create your own style file, copy the default style file and make changes to it. Register the new style before creating your Druid instances.
Parameters:
- style table Druid style module
Usage:
local my_style = require("path.to.my.style") druid.set_default_style(my_style)
- druid.set_sound_function(callback)
-
Set the Druid sound function to play UI sounds if used.
Set a function to play a sound given a sound_id. This function is used for button clicks to play the "click" sound. It can also be used to play sounds in your custom components (see the default Druid style file for an example).
Parameters:
- callback function Sound play callback
Usage:
druid.set_sound_function(function(sound_id) sound.play(sound_id) -- Replace with your real function end)
- druid.set_text_function(callback)
-
Set the text function for the LangText component.
The Druid locale component will call this function to get translated text. After setting the text function, all existing locale components will be updated.
Parameters:
- callback function Get localized text function
Usage:
druid.set_text_function(function(text_id) return lang_data[text_id] -- Replace with your real function end)