Styles currently in Druid are general settings for all components. They can have only access to elements to itself. So the idea was to adjust general animations and later to adjust them if required
In case the “button + text” component I suggest the best way is to create a custom component for it.
Example for your case:
-- The button_text.lua file
local component = require("druid.component")
---@class button_text: druid.base_component
---@field druid druid_instance
---@field button druid.button
---@field text druid.text
local M = component.create("button_text")
---@param template string
---@param nodes table<hash, node>
---@param callback function
function M:init(template, nodes, callback)
self:set_template(template)
self:set_nodes(nodes)
self.druid = self:get_druid()
self.callback = callback
self.button = self.druid:new_button("button", self.on_button)
self.button:set_style(nil)
self.button.on_pressed:subscribe(self.on_button_pressed)
-- We need to pass "self" here, the context of inner hover component is a button
self.button.hover.on_mouse_hover:subscribe(self.on_button_hover, self)
self.text = self.druid:new_text("text")
end
-- The _ arg here is a button instance from hover event, we skip it
function M:on_button_hover(_, is_hover)
local scale = is_hover and 1.2 or 1
gui.animate(self.button.node, gui.PROP_SCALE, vmath.vector3(scale, scale, 1), gui.EASING_OUTSINE, 0.2)
local font = is_hover and "text_bold" or "text_regular"
gui.set_font(self.text.node, font)
end
function M:on_button_pressed()
gui.animate(self.button.node, gui.PROP_SCALE, vmath.vector3(1.4), gui.EASING_OUTBACK, 0.15)
end
function M:on_button()
if self.callback then
self.callback()
end
gui.animate(self.button.node, gui.PROP_SCALE, vmath.vector3(1), gui.EASING_OUTBACK, 0.15)
end
function M:my_function()
print("My function")
end
return M
In the place of gui_script instead of druid:new_button
now you can create this component:
-- GUI file
local druid = require("druid.druid")
local button_text = require("components.button_text")
function init(self)
self.druid = druid.new(self)
-- The "button_text" here is a template name on this GUI scene with nodes "button" and "text"
-- If you are not using templates, just remove it
-- Nodes is nil, since we don't create it with `gui.clone_tree`, picking nodes from the GUI scene
self.button_text = self.druid:new(button_text, "button_text", nil, callback)
-- Now we can access to the button instance
self.button_text:my_function() -- Prints "My function"
end