Replace event with event library

This commit is contained in:
Insality 2024-11-20 23:52:48 +02:00
parent 6552ea5cac
commit c00fb3590c
39 changed files with 324 additions and 364 deletions

View File

@ -1,9 +1,9 @@
local event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local component = require("druid.component")
---@class druid.back_handler: druid.base_component
---@field on_back druid.event Trigger on back handler action, fun(self, params)
---@field on_back event Trigger on back handler action, fun(self, params)
---@field params any|nil Custom args to pass in the callback
local M = component.create("back_handler")

View File

@ -35,16 +35,16 @@
-- @alias druid.button
--- The druid.event: Event on successful release action over button.
--- The event: Event on successful release action over button.
-- @usage
-- -- Custom args passed in Button constructor
-- button.on_click:subscribe(function(self, custom_args, button_instance)
-- print("On button click!")
-- end)
-- @tfield druid.event on_click druid.event
-- @tfield event on_click event
--- The druid.event: Event on repeated action over button.
--- The event: Event on repeated action over button.
--
-- This callback will be triggered if user hold the button. The repeat rate pick from `input.repeat_interval` in game.project
-- @usage
@ -52,10 +52,10 @@
-- button.on_repeated_click:subscribe(function(self, custom_args, button_instance, click_count)
-- print("On repeated Button click!")
-- end)
-- @tfield druid.event on_repeated_click druid.event
-- @tfield event on_repeated_click event
--- The druid.event: Event on long tap action over button.
--- The event: Event on long tap action over button.
--
-- This callback will be triggered if user pressed the button and hold the some amount of time.
-- The amount of time picked from button style param: LONGTAP_TIME
@ -64,10 +64,10 @@
-- button.on_long_click:subscribe(function(self, custom_args, button_instance, hold_time)
-- print("On long Button click!")
-- end)
-- @tfield druid.event on_long_click druid.event
-- @tfield event on_long_click event
--- The druid.event: Event on double tap action over button.
--- The event: Event on double tap action over button.
--
-- If secondary click was too fast after previous one, the double
-- click will be called instead usual click (if on_double_click subscriber exists)
@ -76,10 +76,10 @@
-- button.on_double_click:subscribe(function(self, custom_args, button_instance, click_amount)
-- print("On double Button click!")
-- end)
-- @tfield druid.event on_double_click druid.event
-- @tfield event on_double_click event
--- The druid.event: Event calls every frame before on_long_click event.
--- The event: Event calls every frame before on_long_click event.
--
-- If long_click subscriber exists, the on_hold_callback will be called before long_click trigger.
--
@ -89,10 +89,10 @@
-- button.on_double_click:subscribe(function(self, custom_args, button_instance, time)
-- print("On hold Button callback!")
-- end)
-- @tfield druid.event on_hold_callback druid.event
-- @tfield event on_hold_callback event
--- The druid.event: Event calls if click event was outside of button.
--- The event: Event calls if click event was outside of button.
--
-- This event will be triggered for each button what was not clicked on user click action
--
@ -102,16 +102,16 @@
-- button.on_click_outside:subscribe(function(self, custom_args, button_instance)
-- print("On click Button outside!")
-- end)
-- @tfield druid.event on_click_outside druid.event
-- @tfield event on_click_outside event
--- The druid.event: Event triggered if button was pressed by user.
--- The event: Event triggered if button was pressed by user.
-- @usage
-- -- Custom args passed in Button constructor
-- button.on_pressed:subscribe(function(self, custom_args, button_instance)
-- print("On Button pressed!")
-- end)
-- @tfield druid.event on_pressed druid.event
-- @tfield event on_pressed event
--- Button trigger node
-- @tfield node node
@ -136,19 +136,19 @@
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.button: druid.base_component
---@field on_click druid.event
---@field on_pressed druid.event
---@field on_repeated_click druid.event
---@field on_long_click druid.event
---@field on_double_click druid.event
---@field on_hold_callback druid.event
---@field on_click_outside druid.event
---@field on_click event
---@field on_pressed event
---@field on_repeated_click event
---@field on_long_click event
---@field on_double_click event
---@field on_hold_callback event
---@field on_click_outside event
---@field node node
---@field node_id hash
---@field anim_node node
@ -229,6 +229,7 @@ local function on_button_hold(self, press_time)
end
---@param self druid.button
local function on_button_release(self)
if self.is_repeated_started then
return false
@ -253,10 +254,10 @@ local function on_button_release(self)
local time = socket.gettime()
local is_long_click = (time - self.last_pressed_time) >= self.style.LONGTAP_TIME
is_long_click = is_long_click and self.on_long_click:is_exist()
is_long_click = is_long_click and not self.on_long_click:is_empty()
local is_double_click = (time - self.last_released_time) < self.style.DOUBLETAP_TIME
is_double_click = is_double_click and self.on_double_click:is_exist()
is_double_click = is_double_click and not self.on_double_click:is_empty()
if is_long_click then
local is_hold_complete = (time - self.last_pressed_time) >= self.style.AUTOHOLD_TRIGGER
@ -335,13 +336,13 @@ function M:init(node_or_node_id, callback, custom_args, anim_node)
self._is_html5_listener_set = false
-- Events
self.on_click = Event(callback)
self.on_pressed = Event()
self.on_repeated_click = Event()
self.on_long_click = Event()
self.on_double_click = Event()
self.on_hold_callback = Event()
self.on_click_outside = Event()
self.on_click = event.create(callback)
self.on_pressed = event.create()
self.on_repeated_click = event.create()
self.on_long_click = event.create()
self.on_double_click = event.create()
self.on_hold_callback = event.create()
self.on_click_outside = event.create()
end
@ -408,7 +409,7 @@ function M:on_input(action_id, action)
-- While hold button, repeat rate pick from input.repeat_interval
if action.repeated then
if self.on_repeated_click:is_exist() and self.can_action then
if not self.on_repeated_click:is_empty() and self.can_action then
on_button_repeated_click(self)
return is_consume
end
@ -418,7 +419,7 @@ function M:on_input(action_id, action)
return on_button_release(self) and is_consume
end
if self.can_action and self.on_long_click:is_exist() then
if self.can_action and not self.on_long_click:is_empty() then
local press_time = socket.gettime() - self.last_pressed_time
if self.style.AUTOHOLD_TRIGGER <= press_time then

View File

@ -14,19 +14,19 @@
-- @tfield node node
--- Event on touch start callback(self)
-- @tfield druid.event on_touch_start druid.event
-- @tfield event on_touch_start event
--- Event on touch end callback(self)
-- @tfield druid.event on_touch_end druid.event
-- @tfield event on_touch_end event
--- Event on drag start callback(self, touch)
-- @tfield druid.event on_drag_start druid.event
-- @tfield event on_drag_start event
--- on drag progress callback(self, dx, dy, total_x, total_y, touch)
-- @tfield druid.event on_drag Event druid.event
-- @tfield event on_drag Event event
--- Event on drag end callback(self, total_x, total_y, touch)
-- @tfield druid.event on_drag_end druid.event
-- @tfield event on_drag_end event
--- Is component now touching
-- @tfield boolean is_touch
@ -57,18 +57,18 @@
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.drag: druid.base_component
---@field node node
---@field on_touch_start druid.event
---@field on_touch_end druid.event
---@field on_drag_start druid.event
---@field on_drag druid.event
---@field on_drag_end druid.event
---@field on_touch_start event
---@field on_touch_end event
---@field on_drag_start event
---@field on_drag event
---@field on_drag_end event
---@field style table
---@field click_zone node
---@field is_touch boolean
@ -234,11 +234,11 @@ function M:init(node_or_node_id, on_drag_callback)
self._scene_scale = helper.get_scene_scale(self.node)
self.click_zone = nil
self.on_touch_start = Event()
self.on_touch_end = Event()
self.on_drag_start = Event()
self.on_drag = Event(on_drag_callback)
self.on_drag_end = Event()
self.on_touch_start = event.create()
self.on_touch_end = event.create()
self.on_drag_start = event.create()
self.on_drag = event.create(on_drag_callback)
self.on_drag_end = event.create()
self:on_window_resized()
self:set_drag_cursors(true)

View File

@ -9,22 +9,22 @@
-- @tfield node node
--- On hover callback(self, state, hover_instance)
-- @tfield druid.event on_hover druid.event
-- @tfield event on_hover event
--- On mouse hover callback(self, state, hover_instance)
-- @tfield druid.event on_mouse_hover druid.event
-- @tfield event on_mouse_hover event
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.hover: druid.base_component
---@field node node
---@field on_hover druid.event
---@field on_mouse_hover druid.event
---@field on_hover event
---@field on_mouse_hover event
---@field style table
---@field click_zone node
---@field private _is_hovered boolean
@ -46,8 +46,8 @@ function M:init(node, on_hover_callback, on_mouse_hover)
self._is_enabled = true
self._is_mobile = helper.is_mobile()
self.on_hover = Event(on_hover_callback)
self.on_mouse_hover = Event(on_mouse_hover)
self.on_hover = event.create(on_hover_callback)
self.on_mouse_hover = event.create(on_mouse_hover)
end

View File

@ -39,13 +39,13 @@
--- On scroll move callback(self, position)
-- @tfield druid.event on_scroll druid.event
-- @tfield event on_scroll event
--- On scroll_to function callback(self, target, is_instant)
-- @tfield druid.event on_scroll_to druid.event
-- @tfield event on_scroll_to event
--- On scroll_to_index function callback(self, index, point)
-- @tfield druid.event on_point_scroll druid.event
-- @tfield event on_point_scroll event
--- Scroll view node
-- @tfield node view_node
@ -85,15 +85,15 @@
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.scroll: druid.base_component
---@field on_scroll druid.event
---@field on_scroll_to druid.event
---@field on_point_scroll druid.event
---@field on_scroll event
---@field on_scroll_to event
---@field on_point_scroll event
---@field view_node node
---@field view_border vector4
---@field content_node node
@ -194,9 +194,9 @@ function M:init(view_node, content_node)
self.hover.on_mouse_hover:subscribe(self._on_mouse_hover)
self._is_mouse_hover = false
self.on_scroll = Event()
self.on_scroll_to = Event()
self.on_point_scroll = Event()
self.on_scroll = event.create()
self.on_scroll_to = event.create()
self.on_point_scroll = event.create()
self.selected = nil
self.is_animate = false

View File

@ -37,19 +37,19 @@
-- @alias druid.grid
--- On item add callback(self, node, index)
-- @tfield druid.event on_add_item druid.event
-- @tfield event on_add_item event
--- On item remove callback(self, index)
-- @tfield druid.event on_remove_item druid.event
-- @tfield event on_remove_item event
--- On item add, remove or change in_row callback(self, index|nil)
-- @tfield druid.event on_change_items druid.event
-- @tfield event on_change_items event
--- On grid clear callback(self)
-- @tfield druid.event on_clear druid.event
-- @tfield event on_clear event
--- On update item positions callback(self)
-- @tfield druid.event on_update_positions druid.event
-- @tfield event on_update_positions event
--- Parent gui node
-- @tfield node parent
@ -78,16 +78,16 @@
---
local const = require("druid.const")
local Event = require("druid.event")
local event = require("event.event")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.grid: druid.base_component
---@field on_add_item druid.event
---@field on_remove_item druid.event
---@field on_change_items druid.event
---@field on_clear druid.event
---@field on_update_positions druid.event
---@field on_add_item event
---@field on_remove_item event
---@field on_change_items event
---@field on_clear event
---@field on_update_positions event
---@field parent node
---@field nodes node[]
---@field first_index number
@ -152,11 +152,11 @@ function M:init(parent, element, in_row)
self.border = vmath.vector4(0) -- Current grid content size
self.on_add_item = Event()
self.on_remove_item = Event()
self.on_change_items = Event()
self.on_clear = Event()
self.on_update_positions = Event()
self.on_add_item = event.create()
self.on_remove_item = event.create()
self.on_change_items = event.create()
self.on_clear = event.create()
self.on_update_positions = event.create()
self._set_position_function = gui.set_position
end

View File

@ -36,13 +36,13 @@
-- @alias druid.text
--- On set text callback(self, text)
-- @tfield druid.event on_set_text druid.event
-- @tfield event on_set_text event
--- On adjust text size callback(self, new_scale, text_metrics)
-- @tfield druid.event on_update_text_scale druid.event
-- @tfield event on_update_text_scale event
--- On change pivot callback(self, pivot)
-- @tfield druid.event on_set_pivot druid.event
-- @tfield event on_set_pivot event
--- Text node
-- @tfield node node
@ -76,7 +76,7 @@
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local utf8_lua = require("druid.system.utf8")
@ -85,9 +85,9 @@ local utf8 = utf8 or utf8_lua --[[@as utf8]]
---@class druid.text: druid.base_component
---@field node node
---@field on_set_text druid.event
---@field on_update_text_scale druid.event
---@field on_set_pivot druid.event
---@field on_set_text event
---@field on_update_text_scale event
---@field on_set_pivot event
---@field style table
---@field private start_pivot number
---@field private start_scale vector3
@ -343,9 +343,9 @@ function M:init(node, value, adjust_type)
self.adjust_type = adjust_type or self.style.DEFAULT_ADJUST
self.color = gui.get_color(self.node)
self.on_set_text = Event()
self.on_set_pivot = Event()
self.on_update_text_scale = Event()
self.on_set_text = event.create()
self.on_set_pivot = event.create()
self.on_update_text_scale = event.create()
self:set_text(value or gui.get_text(self.node))
return self

View File

@ -1,166 +0,0 @@
---Event system for Druid
---@class druid.event
local M = {}
M.COUNTER = 0
-- Forward declaration
local EVENT_METATABLE
-- Local versions
local pcall = pcall
local tinsert = table.insert
local tremove = table.remove
--- Return new event instance
---@param callback function|nil Subscribe the callback on new event, if callback exist
---@param callback_context any|nil Additional context as first param to callback call
---@return druid.event
---@nodiscard
function M.create(callback, callback_context)
local instance = setmetatable({}, EVENT_METATABLE)
if callback then
instance:subscribe(callback, callback_context)
end
M.COUNTER = M.COUNTER + 1
return instance
end
--- Check is event subscribed.
---@param callback function Callback itself
---@param callback_context any|nil Additional context as first param to callback call
---@return boolean, number|nil Is event subscribed, return index of callback in event as second param
function M:is_subscribed(callback, callback_context)
if #self == 0 then
return false, nil
end
for index = 1, #self do
local cb = self[index]
if cb[1] == callback and cb[2] == callback_context then
return true, index
end
end
return false, nil
end
---Subscribe callback on event
---@param callback function Callback itself
---@param callback_context any|nil Additional context as first param to callback call, usually it's self
---@return boolean
function M:subscribe(callback, callback_context)
assert(type(self) == "table", "You should subscribe to event with : syntax")
assert(callback, "A function must be passed to subscribe to an event")
if self:is_subscribed(callback, callback_context) then
return false
end
tinsert(self, { callback, callback_context })
return true
end
---Unsubscribe callback on event
---@param callback function Callback itself
---@param callback_context any|nil Additional context as first param to callback call
---@return boolean
function M:unsubscribe(callback, callback_context)
assert(callback, "A function must be passed to subscribe to an event")
local _, event_index = self:is_subscribed(callback, callback_context)
if not event_index then
return false
end
tremove(self, event_index)
return true
end
---Return true, if event have at lease one handler
---@return boolean
function M:is_exist()
return #self > 0
end
---Return true, if event not have handler
---@return boolean True if event not have handlers
function M:is_empty()
return #self == 0
end
---Clear the all event handlers
function M:clear()
for index = #self, 1, -1 do
self[index] = nil
end
end
--- Trigger the event and call all subscribed callbacks
---@param ... any All event params
---@return any result Last returned value from subscribers
function M:trigger(...)
if #self == 0 then
return
end
local result = nil
local call_callback = self.call_callback
for index = 1, #self do
result = call_callback(self, self[index], ...)
end
return result
end
---@param callback table Callback data {function, context}
---@param ... any All event params
---@return any result Result of the callback
function M:call_callback(callback, ...)
local event_callback = callback[1]
local event_callback_context = callback[2]
-- Call callback
local ok, result_or_error
if event_callback_context then
ok, result_or_error = pcall(event_callback, event_callback_context, ...)
else
ok, result_or_error = pcall(event_callback, ...)
end
-- Handle errors
if not ok then
local caller_info = debug.getinfo(2)
pprint("An error occurred during event processing", {
trigger = caller_info.short_src .. ":" .. caller_info.currentline,
error = result_or_error,
})
pprint("Traceback", debug.traceback())
return nil
end
return result_or_error
end
-- Construct event metatable
EVENT_METATABLE = {
__index = M,
__call = M.trigger,
}
return setmetatable(M, {
__call = function(_, callback)
return M.create(callback)
end,
})

View File

@ -13,7 +13,7 @@
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
local Event = require("druid.event")
local event = require("event.event")
---@class druid.container: druid.base_component
---@field node node
@ -29,7 +29,7 @@ local Event = require("druid.event")
---@field fit_size vector3
---@field min_size_x number|nil
---@field min_size_y number|nil
---@field on_size_changed druid.event @function on_size_changed(size)
---@field on_size_changed event @function on_size_changed(size)
---@field _parent_container druid.container
---@field _containers table
---@field _draggable_corners table
@ -82,7 +82,7 @@ function M:init(node, mode, callback)
gui.set_size_mode(self.node, gui.SIZE_MODE_MANUAL)
gui.set_adjust_mode(self.node, gui.ADJUST_FIT)
self.on_size_changed = Event(callback)
self.on_size_changed = event.create(callback)
self.pivot_offset = helper.get_pivot_offset(gui.get_pivot(self.node))
self.center_offset = -vmath.vector3(self.size.x * self.pivot_offset.x, self.size.y * self.pivot_offset.y, 0)

View File

@ -25,27 +25,27 @@
-- @tfield number last_index
--- Event triggered when scroll progress is changed; event(self, progress_value)
-- @tfield druid.event on_scroll_progress_change druid.event
-- @tfield event on_scroll_progress_change event
---On DataList visual element created Event callback(self, index, node, instance)
-- @tfield druid.event on_element_add druid.event
-- @tfield event on_element_add event
---On DataList visual element created Event callback(self, index)
-- @tfield druid.event on_element_remove druid.event
-- @tfield event on_element_remove event
---
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
local Event = require("druid.event")
local event = require("event.event")
---@class druid.data_list: druid.base_component
---@field scroll druid.scroll
---@field grid druid.grid
---@field on_scroll_progress_change druid.event
---@field on_element_add druid.event
---@field on_element_remove druid.event
---@field on_scroll_progress_change event
---@field on_element_add event
---@field on_element_remove event
---@field private _create_function function
---@field private _is_use_cache boolean
---@field private _cache table
@ -79,9 +79,9 @@ function M:init(scroll, grid, create_function)
self.scroll.on_scroll:subscribe(self._refresh, self)
self.on_scroll_progress_change = Event()
self.on_element_add = Event()
self.on_element_remove = Event()
self.on_scroll_progress_change = event.create()
self.on_element_add = event.create()
self.on_element_remove = event.create()
end

View File

@ -8,10 +8,10 @@
-- @alias druid.hotkey
--- On hotkey released callback(self, argument)
-- @tfield druid.event on_hotkey_pressed druid.event
-- @tfield event on_hotkey_pressed event
--- On hotkey released callback(self, argument)
-- @tfield druid.event on_hotkey_released druid.event
-- @tfield event on_hotkey_released event
--- Visual node
-- @tfield node node
@ -26,11 +26,11 @@
local helper = require("druid.helper")
local component = require("druid.component")
local Event = require("druid.event")
local event = require("event.event")
---@class druid.hotkey: druid.base_component
---@field on_hotkey_pressed druid.event
---@field on_hotkey_released druid.event
---@field on_hotkey_pressed event
---@field on_hotkey_released event
---@field style table
---@field private _hotkeys table
---@field private _modificators table
@ -47,8 +47,8 @@ function M:init(keys, callback, callback_argument)
self._hotkeys = {}
self._modificators = {}
self.on_hotkey_pressed = Event()
self.on_hotkey_released = Event(callback)
self.on_hotkey_pressed = event.create()
self.on_hotkey_released = event.create(callback)
if keys then
self:add_hotkey(keys, callback_argument)

View File

@ -10,25 +10,25 @@
-- @alias druid.input
--- On input field select callback(self, input_instance)
-- @tfield druid.event on_input_select druid.event
-- @tfield event on_input_select event
--- On input field unselect callback(self, input_text, input_instance)
-- @tfield druid.event on_input_unselect druid.event
-- @tfield event on_input_unselect event
--- On input field text change callback(self, input_text)
-- @tfield druid.event on_input_text druid.event
-- @tfield event on_input_text event
--- On input field text change to empty string callback(self, input_text)
-- @tfield druid.event on_input_empty druid.event
-- @tfield event on_input_empty event
--- On input field text change to max length string callback(self, input_text)
-- @tfield druid.event on_input_full druid.event
-- @tfield event on_input_full event
--- On trying user input with not allowed character callback(self, params, input_text)
-- @tfield druid.event on_input_wrong druid.event
-- @tfield event on_input_wrong event
--- On cursor position change callback(self, cursor_index, start_index, end_index)
-- @tfield druid.event on_select_cursor_change druid.event
-- @tfield event on_select_cursor_change event
--- The cursor index. The index of letter cursor after. Leftmost cursor - 0
-- @tfield number cursor_index
@ -80,7 +80,7 @@
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
@ -88,13 +88,13 @@ local utf8_lua = require("druid.system.utf8")
local utf8 = utf8 or utf8_lua
---@class druid.input: druid.base_component
---@field on_input_select druid.event
---@field on_input_unselect druid.event
---@field on_input_text druid.event
---@field on_input_empty druid.event
---@field on_input_full druid.event
---@field on_input_wrong druid.event
---@field on_select_cursor_change druid.event
---@field on_input_select event
---@field on_input_unselect event
---@field on_input_text event
---@field on_input_empty event
---@field on_input_full event
---@field on_input_wrong event
---@field on_select_cursor_change event
---@field style table
---@field text druid.text
local M = component.create("input")
@ -200,13 +200,13 @@ function M:init(click_node, text_node, keyboard_type)
self.button:set_web_user_interaction(true)
end
self.on_input_select = Event()
self.on_input_unselect = Event()
self.on_input_text = Event()
self.on_input_empty = Event()
self.on_input_full = Event()
self.on_input_wrong = Event()
self.on_select_cursor_change = Event()
self.on_input_select = event.create()
self.on_input_unselect = event.create()
self.on_input_text = event.create()
self.on_input_empty = event.create()
self.on_input_full = event.create()
self.on_input_wrong = event.create()
self.on_select_cursor_change = event.create()
end

View File

@ -18,7 +18,7 @@
-- @alias druid.lang_text
--- On change text callback
-- @tfield druid.event on_change druid.event
-- @tfield event on_change event
--- The text component
-- @tfield Text text Text
@ -28,14 +28,14 @@
---
local Event = require("druid.event")
local event = require("event.event")
local settings = require("druid.system.settings")
local component = require("druid.component")
---@class druid.lang_text: druid.base_component
---@field text druid.text
---@field node node
---@field on_change druid.event
---@field on_change event
---@field private last_locale_args table
---@field private last_locale string
local M = component.create("lang_text")
@ -51,7 +51,7 @@ function M:init(node, locale_id, adjust_type)
self.node = self.text.node
self.last_locale_args = {}
self.on_change = Event()
self.on_change = event.create()
self:translate(locale_id or gui.get_text(self.node))
self.text.on_set_text:subscribe(self.on_change.trigger, self.on_change)

View File

@ -1,10 +1,10 @@
local event = require("druid.event")
local event = require("event.event")
local helper = require("druid.helper")
local component = require("druid.component")
---@alias druid.layout.mode "horizontal"|"vertical"|"horizontal_wrap"
---@class druid.event.on_size_changed: druid.event
---@class event.on_size_changed: event
---@field subscribe fun(_, callback: fun(new_size: vector3), context: any|nil)
---@class druid.layout.row_data
@ -30,7 +30,7 @@ local component = require("druid.component")
---@field is_resize_width boolean
---@field is_resize_height boolean
---@field is_justify boolean
---@field on_size_changed druid.event.on_size_changed
---@field on_size_changed event.on_size_changed
local M = component.create("layout")
---Layout component constructor
@ -54,7 +54,7 @@ function M:init(node_or_node_id, layout_type)
self.is_resize_height = false
self.is_justify = false
self.on_size_changed = event.create() --[[@as druid.event.on_size_changed]]
self.on_size_changed = event.create() --[[@as event.on_size_changed]]
end

View File

@ -20,7 +20,7 @@
-- @alias druid.progress
--- On progress bar change callback(self, new_value)
-- @tfield druid.event on_change druid.event
-- @tfield event on_change event
--- Progress bar fill node
-- @tfield node node
@ -44,14 +44,14 @@
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.progress: druid.base_component
---@field node node
---@field on_change druid.event
---@field on_change event
---@field style table
---@field key string
---@field prop hash
@ -154,7 +154,7 @@ function M:init(node, key, init_value)
0
)
self.on_change = Event()
self.on_change = event.create()
self:set_to(self.last_value)
end

View File

@ -8,7 +8,7 @@
-- @alias druid.slider
--- On change value callback(self, value)
-- @tfield druid.event on_change_value druid.event
-- @tfield event on_change_value event
--- Slider pin node
-- @tfield node node
@ -37,14 +37,14 @@
---
local Event = require("druid.event")
local event = require("event.event")
local helper = require("druid.helper")
local const = require("druid.const")
local component = require("druid.component")
---@class druid.slider: druid.base_component
---@field node node
---@field on_change_value druid.event
---@field on_change_value event
---@field style table
---@field private start_pos vector3
---@field private pos vector3
@ -85,7 +85,7 @@ function M:init(node, end_pos, callback)
self.is_drag = false
self.value = 0
self.on_change_value = Event(callback)
self.on_change_value = event.create(callback)
self:on_window_resized()
assert(self.dist.x == 0 or self.dist.y == 0, "Slider for now can be only vertical or horizontal")

View File

@ -16,18 +16,18 @@
---@param click_zone node|nil
--- Trigger on swipe event(self, swipe_side, dist, delta_time)
-- @tfield druid.event on_swipe) druid.event
-- @tfield event on_swipe) event
---
local Event = require("druid.event")
local event = require("event.event")
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.swipe: druid.base_component
---@field node node
---@field on_swipe druid.event
---@field on_swipe event
---@field style table
---@field click_zone node
---@field private _trigger_on_move boolean
@ -106,7 +106,7 @@ function M:init(node_or_node_id, on_swipe_callback)
self._start_pos = vmath.vector3(0)
self.click_zone = nil
self.on_swipe = Event(on_swipe_callback)
self.on_swipe = event.create(on_swipe_callback)
end

View File

@ -1,11 +1,11 @@
local Event = require("druid.event")
local event = require("event.event")
local helper = require("druid.helper")
local component = require("druid.component")
---@class druid.timer: druid.base_component
---@field on_tick druid.event
---@field on_set_enabled druid.event
---@field on_timer_end druid.event
---@field on_tick event
---@field on_set_enabled event
---@field on_timer_end event
---@field style table
---@field node node
---@field from number
@ -31,9 +31,9 @@ function M:init(node, seconds_from, seconds_to, callback)
self.node = self:get_node(node)
seconds_to = math.max(seconds_to or 0, 0)
self.on_tick = Event()
self.on_set_enabled = Event()
self.on_timer_end = Event(callback)
self.on_tick = event.create()
self.on_set_enabled = event.create()
self.on_timer_end = event.create(callback)
if seconds_from then
seconds_from = math.max(seconds_from, 0)

View File

@ -3,7 +3,7 @@
--
-- Helper - A useful set of functions for working with GUI nodes, such as centering nodes, get GUI scale ratio, etc
--
-- druid.event - The core event system in Druid. Learn how to subscribe to any event in every Druid component.
-- event - The core event system in Druid. Learn how to subscribe to any event in every Druid component.
--
-- BaseComponent - The parent class of all Druid components. You can find all default component methods there.
--

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
---@class widget.property_left_right_selector: druid.widget
---@field root node

View File

@ -13,7 +13,7 @@
local const = require("druid.const")
local helper = require("druid.helper")
local component = require("druid.component")
local Event = require("druid.event")
local event = require("event.event")
---@class druid.container: druid.base_component
---@field node node
@ -29,7 +29,7 @@ local Event = require("druid.event")
---@field fit_size vector3
---@field min_size_x number|nil
---@field min_size_y number|nil
---@field on_size_changed druid.event @function on_size_changed(size)
---@field on_size_changed event @function on_size_changed(size)
---@field _parent_container druid.container
---@field _containers table
---@field _draggable_corners table
@ -82,7 +82,7 @@ function M:init(node, mode, callback)
gui.set_size_mode(self.node, gui.SIZE_MODE_MANUAL)
gui.set_adjust_mode(self.node, gui.ADJUST_FIT)
self.on_size_changed = Event(callback)
self.on_size_changed = event.create(callback)
self.pivot_offset = helper.get_pivot_offset(gui.get_pivot(self.node))
self.center_offset = -vmath.vector3(self.size.x * self.pivot_offset.x, self.size.y * self.pivot_offset.y, 0)

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local component = require("druid.component")
local container = require("example.components.container.container")
local lang_text = require("druid.extended.lang_text")
@ -39,9 +39,9 @@ function M:init(template, nodes)
self.selected_example = nil
self.examples = {}
self.on_debug_info = event()
self.on_set_information = event()
self.add_log_text = event()
self.on_debug_info = event.create()
self.on_set_information = event.create()
self.add_log_text = event.create()
timer.delay(0.1, true, function()
self:update_debug_info()

View File

@ -6,7 +6,7 @@ local lang_text = require("druid.extended.lang_text")
---@field root druid.container
---@field text druid.lang_text
---@field druid druid_instance
---@field on_click druid.event
---@field on_click event
local M = component.create("examples_list_view_item")
---@param template string

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local helper = require("druid.helper")
local component = require("druid.component")
local container = require("example.components.container.container")

View File

@ -1,5 +1,5 @@
local component = require("druid.component")
local event = require("druid.event")
local event = require("event.event")
---@class checkbox: druid.base_component
---@field druid druid_instance

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local component = require("druid.component")
-- Require checkbox component from checkbox example

View File

@ -1,5 +1,5 @@
local component = require("druid.component")
local event = require("druid.event")
local event = require("event.event")
-- Require checkbox component from checkbox example
local checkbox = require("example.examples.basic.checkbox.checkbox")

View File

@ -1,7 +1,7 @@
local component = require("druid.component")
local rich_text = require("druid.custom.rich_text.rich_text")
local helper = require("druid.helper")
local event = require("druid.event")
local event = require("event.event")
---@class rich_text_tags_custom: druid.base_component
---@field druid druid_instance

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local timer = require("druid.extended.timer")
local component = require("druid.component")

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local component = require("druid.component")
local data_list = require("druid.extended.data_list")
@ -26,7 +26,7 @@ function M:init(template, nodes)
end
self.data_list:set_data(data)
self.on_item_click = event()
self.on_item_click = event.create()
end

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local component = require("druid.component")
local data_list = require("druid.extended.data_list")
@ -25,7 +25,7 @@ function M:init(template, nodes)
end
self.data_list:set_data(data)
self.on_item_click = event()
self.on_item_click = event.create()
end

View File

@ -1,4 +1,4 @@
local event = require("druid.event")
local event = require("event.event")
local component = require("druid.component")
local data_list = require("druid.extended.data_list")
@ -30,7 +30,7 @@ function M:init(template, nodes)
end
self.data_list:set_data(data)
self.on_item_click = event()
self.on_item_click = event.create()
end

View File

@ -1,13 +1,13 @@
local const = require("druid.const")
local event = require("druid.event")
local event = require("event.event")
local helper = require("druid.helper")
local component = require("druid.component")
---@class on_screen_input: druid.base_component
---@field druid druid_instance
---@field on_action druid.event @()
---@field on_movement druid.event @(x: number, y: number, dt: number) X/Y values are in range -1..1
---@field on_movement_stop druid.event @()
---@field on_action event @()
---@field on_movement event @(x: number, y: number, dt: number) X/Y values are in range -1..1
---@field on_movement_stop event @()
local M = component.create("on_screen_input")
local STICK_DISTANCE = 80
@ -23,9 +23,9 @@ function M:init(template, nodes)
self.stick_root = self:get_node("on_screen_stick/stick_root")
self.stick_position = gui.get_position(self.stick_root)
self.on_action = event()
self.on_movement = event()
self.on_movement_stop = event()
self.on_action = event.create()
self.on_movement = event.create()
self.on_movement_stop = event.create()
self.is_multitouch = helper.is_multitouch_supported()
end

View File

@ -1,7 +1,7 @@
local panthera = require("panthera.panthera")
local component = require("druid.component")
local helper = require("druid.helper")
local event = require("druid.event")
local event = require("event.event")
local lang_text = require("druid.extended.lang_text")
local rich_text = require("druid.custom.rich_text.rich_text")
@ -31,7 +31,7 @@ function M:init(template, nodes)
})
self:setup_rich_text()
self.on_update = event()
self.on_update = event.create()
end

View File

@ -59,7 +59,15 @@ function M.get_examples()
template = "example_memory_panel",
root = "example_memory_panel/root",
code_url = "example/examples/widgets/memory_panel/example_memory_panel.lua",
component_class = require("example.examples.widgets.memory_panel.example_memory_panel"),
widget_class = require("example.examples.widgets.memory_panel.example_memory_panel"),
},
{
name_id = "ui_example_widget_fps_panel",
information_text_id = "ui_example_widget_fps_panel_description",
template = "example_fps_panel",
root = "example_fps_panel/root",
code_url = "example/examples/widgets/fps_panel/example_fps_panel.lua",
widget_class = require("example.examples.widgets.fps_panel.example_fps_panel"),
},
}
end

View File

@ -0,0 +1,104 @@
nodes {
size {
x: 200.0
y: 100.0
}
type: TYPE_BOX
id: "root"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
visible: false
}
nodes {
type: TYPE_TEMPLATE
id: "memory_panel"
parent: "root"
inherit_alpha: true
template: "/druid/widget/fps_panel/fps_panel.gui"
}
nodes {
type: TYPE_BOX
id: "memory_panel/root"
parent: "memory_panel"
template_node_child: true
}
nodes {
type: TYPE_TEMPLATE
id: "memory_panel/mini_graph"
parent: "memory_panel/root"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/mini_graph/root"
parent: "memory_panel/mini_graph"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/mini_graph/header"
parent: "memory_panel/mini_graph/root"
template_node_child: true
}
nodes {
type: TYPE_TEXT
id: "memory_panel/mini_graph/text_header"
parent: "memory_panel/mini_graph/header"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/mini_graph/icon_drag"
parent: "memory_panel/mini_graph/header"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/mini_graph/content"
parent: "memory_panel/mini_graph/root"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/mini_graph/prefab_line"
parent: "memory_panel/mini_graph/content"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/mini_graph/color_low"
parent: "memory_panel/mini_graph/content"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/content"
parent: "memory_panel/root"
template_node_child: true
}
nodes {
type: TYPE_TEXT
id: "memory_panel/text_min_fps"
parent: "memory_panel/content"
template_node_child: true
}
nodes {
type: TYPE_TEXT
id: "memory_panel/text_fps"
parent: "memory_panel/content"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/line_second_1"
parent: "memory_panel/content"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "memory_panel/line_second_2"
parent: "memory_panel/content"
template_node_child: true
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT

View File

@ -0,0 +1,12 @@
local fps_panel = require("druid.widget.fps_panel.fps_panel")
---@class widget.example_fps_panel: druid.widget
local M = {}
function M:init()
self.fps_panel = self.druid:new_widget(fps_panel, "fps_panel")
end
return M

View File

@ -1,6 +1,6 @@
local lang = require("lang.lang")
local druid = require("druid.druid")
local event = require("druid.event")
local event = require("event.event")
local component = require("druid.component")
local lang_text = require("druid.extended.lang_text")
local panthera = require("panthera.panthera")
@ -13,7 +13,7 @@ local window_animation_panthera = require("example.examples.windows.window_anima
---@field druid druid_instance
---@field lang_buttons table<string, druid.button>
---@field grid druid.grid
---@field on_language_change druid.event
---@field on_language_change event
local M = component.create("window_language")
---Color: #F0FBFF
@ -44,7 +44,7 @@ function M:init(template, nodes)
self:load_langs()
self.on_language_change = event()
self.on_language_change = event.create()
end

View File

@ -22,6 +22,7 @@ dependencies#1 = https://github.com/Insality/defold-saver/archive/refs/tags/1.zi
dependencies#2 = https://github.com/Insality/defold-tweener/archive/refs/tags/3.zip
dependencies#3 = https://github.com/Insality/panthera/archive/refs/tags/runtime.4.zip
dependencies#4 = https://github.com/Insality/defold-lang/archive/refs/tags/3.zip
dependencies#5 = https://github.com/Insality/defold-event/archive/refs/tags/10.zip
[library]
include_dirs = druid