mirror of
https://github.com/Insality/druid
synced 2025-09-27 10:02:19 +02:00
Update annotations P.1
This commit is contained in:
@@ -1,65 +1,24 @@
|
||||
-- Copyright (c) 2023 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
|
||||
|
||||
--- Component with event on back and backspace button.
|
||||
-- <b># Overview #</b>
|
||||
--
|
||||
-- Back Handler is recommended to put in every game window to close it
|
||||
-- or in main screen to call settings window.
|
||||
--
|
||||
-- <b># Notes #</b>
|
||||
--
|
||||
-- • Back Handler inheritance @{BaseComponent}, you can use all of its methods in addition to those described here.
|
||||
--
|
||||
-- • Back Handler react on release action ACTION_BACK or ACTION_BACKSPACE
|
||||
-- @usage
|
||||
-- local callback = function(self, params) ... end
|
||||
--
|
||||
-- local params = {}
|
||||
-- local back_handler = self.druid:new_back_handler(callback, [params])
|
||||
-- @module BackHandler
|
||||
-- @within BaseComponent
|
||||
-- @alias druid.back_handler
|
||||
|
||||
--- The @{DruidEvent} Event on back handler action.
|
||||
--
|
||||
-- Trigger on input action ACTION_BACK or ACTION_BACKSPACE
|
||||
-- @usage
|
||||
-- -- Subscribe additional callbacks:
|
||||
-- back_handler.on_back:subscribe(callback)
|
||||
-- @tfield DruidEvent on_back @{DruidEvent}
|
||||
|
||||
--- Custom args to pass in the callback
|
||||
-- @usage
|
||||
-- -- Replace params on runtime:
|
||||
-- back_handler.params = { ... }
|
||||
-- @tfield any|nil params
|
||||
|
||||
---
|
||||
|
||||
local Event = require("druid.event")
|
||||
local event = require("druid.event")
|
||||
local const = require("druid.const")
|
||||
local component = require("druid.component")
|
||||
|
||||
local BackHandler = component.create("back_handler")
|
||||
---@class druid.back_handler: druid.base_component
|
||||
---@field on_back druid.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")
|
||||
|
||||
|
||||
--- The @{BackHandler} constructor
|
||||
-- @tparam BackHandler self @{BackHandler}
|
||||
-- @tparam function callback @The callback(self, custom_args) to call on back event
|
||||
-- @tparam any|nil custom_args Button events custom arguments
|
||||
-- @local
|
||||
function BackHandler.init(self, callback, custom_args)
|
||||
self.params = custom_args
|
||||
self.on_back = Event(callback)
|
||||
---@param callback function|nil
|
||||
---@param params any|nil
|
||||
function M:init(callback, params)
|
||||
self.params = params
|
||||
self.on_back = event.create(callback)
|
||||
end
|
||||
|
||||
|
||||
--- Component input handler
|
||||
-- @tparam BackHandler self @{BackHandler}
|
||||
-- @tparam string action_id on_input action id
|
||||
-- @tparam table action on_input action
|
||||
-- @local
|
||||
function BackHandler.on_input(self, action_id, action)
|
||||
---@param action_id string
|
||||
---@param action table
|
||||
function M:on_input(action_id, action)
|
||||
if not action.released then
|
||||
return false
|
||||
end
|
||||
@@ -73,4 +32,4 @@ function BackHandler.on_input(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
return BackHandler
|
||||
return M
|
||||
|
@@ -1,50 +1,22 @@
|
||||
-- Copyright (c) 2023 Maksim Tuprikov <insality@gmail.com>. This code is licensed under MIT license
|
||||
|
||||
--- Component to consume input in special zone defined by GUI node.
|
||||
-- <b># Overview #</b>
|
||||
--
|
||||
-- <b># Notes #</b>
|
||||
--
|
||||
-- Blocker consume input if `gui.pick_node` works on it.
|
||||
--
|
||||
-- • Blocker inheritance @{BaseComponent}, you can use all of its methods in addition to those described here.
|
||||
--
|
||||
-- • Blocker initial enabled state is `gui.is_enabled(node, true)`
|
||||
--
|
||||
-- • The Blocker node should be enabled to capture the input
|
||||
-- @usage
|
||||
-- local node = gui.get_node("blocker_node")
|
||||
-- local blocker = self.druid:new_blocker(node)
|
||||
-- @module Blocker
|
||||
-- @within BaseComponent
|
||||
-- @alias druid.blocker
|
||||
|
||||
---Blocker node
|
||||
-- @tfield node node
|
||||
|
||||
---
|
||||
|
||||
local const = require("druid.const")
|
||||
local component = require("druid.component")
|
||||
|
||||
local Blocker = component.create("blocker")
|
||||
---@class druid.blocker: druid.base_component
|
||||
---@field node node
|
||||
---@field private _is_enabled boolean
|
||||
local M = component.create("blocker")
|
||||
|
||||
|
||||
--- The @{Blocker} constructor
|
||||
-- @tparam Blocker self @{Blocker}
|
||||
-- @tparam node node Gui node
|
||||
function Blocker.init(self, node)
|
||||
---@param node node
|
||||
function M:init(node)
|
||||
self.node = self:get_node(node)
|
||||
self._is_enabled = gui.is_enabled(self.node, true)
|
||||
end
|
||||
|
||||
|
||||
--- Component input handler
|
||||
-- @tparam Blocker self @{Blocker}
|
||||
-- @tparam string action_id on_input action id
|
||||
-- @tparam table action on_input action
|
||||
-- @local
|
||||
function Blocker.on_input(self, action_id, action)
|
||||
---@param action_id string
|
||||
---@param action table
|
||||
function M:on_input(action_id, action)
|
||||
if action_id ~= const.ACTION_TOUCH and
|
||||
action_id ~= const.ACTION_MULTITOUCH and
|
||||
action_id ~= nil then
|
||||
@@ -67,22 +39,21 @@ function Blocker.on_input(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
--- Set enabled blocker component state.
|
||||
--
|
||||
-- Don't change node enabled state itself.
|
||||
-- @tparam Blocker self @{Blocker}
|
||||
-- @tparam boolean|nil state Enabled state
|
||||
function Blocker.set_enabled(self, state)
|
||||
---Set blocker enabled state
|
||||
---@param state boolean
|
||||
---@return druid.blocker self
|
||||
function M:set_enabled(state)
|
||||
self._is_enabled = state
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Return blocker enabled state
|
||||
-- @tparam Blocker self @{Blocker}
|
||||
-- @treturn boolean @True, if blocker is enabled
|
||||
function Blocker.is_enabled(self)
|
||||
---Get blocker enabled state
|
||||
---@return boolean
|
||||
function M:is_enabled()
|
||||
return self._is_enabled
|
||||
end
|
||||
|
||||
|
||||
return Blocker
|
||||
return M
|
||||
|
@@ -35,16 +35,16 @@
|
||||
-- @alias druid.button
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event on successful release action over button.
|
||||
--- The DruidEvent: 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 DruidEvent on_click @{DruidEvent}
|
||||
-- @tfield DruidEvent on_click DruidEvent
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event on repeated action over button.
|
||||
--- The DruidEvent: 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 DruidEvent on_repeated_click @{DruidEvent}
|
||||
-- @tfield DruidEvent on_repeated_click DruidEvent
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event on long tap action over button.
|
||||
--- The DruidEvent: 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 DruidEvent on_long_click @{DruidEvent}
|
||||
-- @tfield DruidEvent on_long_click DruidEvent
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event on double tap action over button.
|
||||
--- The DruidEvent: 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 DruidEvent on_double_click @{DruidEvent}
|
||||
-- @tfield DruidEvent on_double_click DruidEvent
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event calls every frame before on_long_click event.
|
||||
--- The DruidEvent: 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 DruidEvent on_hold_callback @{DruidEvent}
|
||||
-- @tfield DruidEvent on_hold_callback DruidEvent
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event calls if click event was outside of button.
|
||||
--- The DruidEvent: 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 DruidEvent on_click_outside @{DruidEvent}
|
||||
-- @tfield DruidEvent on_click_outside DruidEvent
|
||||
|
||||
|
||||
--- The @{DruidEvent}: Event triggered if button was pressed by user.
|
||||
--- The DruidEvent: 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 DruidEvent on_pressed @{DruidEvent}
|
||||
-- @tfield DruidEvent on_pressed DruidEvent
|
||||
|
||||
--- Button trigger node
|
||||
-- @tfield node node
|
||||
@@ -128,8 +128,8 @@
|
||||
---Custom args for any Button event. Setup in Button constructor
|
||||
-- @tfield any params
|
||||
|
||||
--- The @{Hover}: Button Hover component
|
||||
-- @tfield Hover hover @{Hover}
|
||||
--- The Hover: Button Hover component
|
||||
-- @tfield Hover hover Hover
|
||||
|
||||
--- Additional button click area, defined by another GUI node
|
||||
-- @tfield node|nil click_zone
|
||||
@@ -141,7 +141,26 @@ local const = require("druid.const")
|
||||
local helper = require("druid.helper")
|
||||
local component = require("druid.component")
|
||||
|
||||
local Button = component.create("button")
|
||||
---@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 node node
|
||||
---@field node_id hash
|
||||
---@field anim_node node
|
||||
---@field params any
|
||||
---@field hover druid.hover
|
||||
---@field click_zone node
|
||||
---@field start_scale vector3
|
||||
---@field start_pos vector3
|
||||
---@field disabled boolean
|
||||
---@field key_trigger hash
|
||||
---@field style table
|
||||
local M = component.create("button")
|
||||
|
||||
|
||||
local function is_input_match(self, action_id)
|
||||
@@ -271,7 +290,7 @@ end
|
||||
-- @tfield function on_hover function(self, node, hover_state)
|
||||
-- @tfield function on_mouse_hover function(self, node, hover_state)
|
||||
-- @tfield function on_set_enabled function(self, node, enabled_state)
|
||||
function Button.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
self.style.LONGTAP_TIME = style.LONGTAP_TIME or 0.4
|
||||
self.style.AUTOHOLD_TRIGGER = style.AUTOHOLD_TRIGGER or 0.8
|
||||
@@ -285,22 +304,21 @@ function Button.on_style_change(self, style)
|
||||
end
|
||||
|
||||
|
||||
--- The @{Button} constructor
|
||||
-- @tparam Button self @{Button}
|
||||
-- @tparam string|node node The node_id or gui.get_node(node_id)
|
||||
-- @tparam function callback On click button callback
|
||||
-- @tparam any|nil custom_args Button events custom arguments
|
||||
-- @tparam string|node|nil anim_node Node to animate instead of trigger node.
|
||||
function Button.init(self, node, callback, custom_args, anim_node)
|
||||
---Button constructor
|
||||
---@param node_or_node_id node|string Node name or GUI Node itself.
|
||||
---@param callback fun()|nil Callback on button click
|
||||
---@param custom_args any|nil Custom args for any Button event
|
||||
---@param anim_node node|string|nil Node to animate instead of trigger node.
|
||||
function M:init(node_or_node_id, callback, custom_args, anim_node)
|
||||
self.druid = self:get_druid()
|
||||
self.node = self:get_node(node)
|
||||
self.node = self:get_node(node_or_node_id)
|
||||
self.node_id = gui.get_id(self.node)
|
||||
|
||||
self.anim_node = anim_node and self:get_node(anim_node) or self.node
|
||||
self.start_scale = gui.get_scale(self.anim_node)
|
||||
self.start_pos = gui.get_position(self.anim_node)
|
||||
self.params = custom_args
|
||||
self.hover = self.druid:new_hover(node, on_button_hover)
|
||||
self.hover = self.druid:new_hover(node_or_node_id, on_button_hover)
|
||||
self.hover.on_mouse_hover:subscribe(on_button_mouse_hover)
|
||||
self.click_zone = nil
|
||||
self.is_repeated_started = false
|
||||
@@ -325,7 +343,7 @@ function Button.init(self, node, callback, custom_args, anim_node)
|
||||
end
|
||||
|
||||
|
||||
function Button.on_late_init(self)
|
||||
function M:on_late_init()
|
||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||
if stencil_node then
|
||||
@@ -335,7 +353,7 @@ function Button.on_late_init(self)
|
||||
end
|
||||
|
||||
|
||||
function Button.on_input(self, action_id, action)
|
||||
function M:on_input(action_id, action)
|
||||
if not is_input_match(self, action_id) then
|
||||
return false
|
||||
end
|
||||
@@ -416,14 +434,14 @@ function Button.on_input(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
function Button.on_input_interrupt(self)
|
||||
function M:on_input_interrupt()
|
||||
self.can_action = false
|
||||
self.hover:set_hover(false)
|
||||
self.hover:set_mouse_hover(false)
|
||||
end
|
||||
|
||||
|
||||
function Button.on_message_input(self, node_id, message)
|
||||
function M:on_message_input(node_id, message)
|
||||
if node_id ~= self.node_id or self.disabled or not gui.is_enabled(self.node) then
|
||||
return false
|
||||
end
|
||||
@@ -451,13 +469,13 @@ end
|
||||
--- Set button enabled state.
|
||||
-- The style.on_set_enabled will be triggered.
|
||||
-- Disabled button is not clickable.
|
||||
-- @tparam Button self @{Button}
|
||||
-- @tparam Button self Button
|
||||
-- @tparam boolean|nil state Enabled state
|
||||
-- @treturn Button Current button instance
|
||||
-- @usage
|
||||
-- button:set_enabled(false)
|
||||
-- button:set_enabled(true)
|
||||
function Button.set_enabled(self, state)
|
||||
function M:set_enabled(state)
|
||||
self.disabled = not state
|
||||
self.hover:set_enabled(state)
|
||||
self.style.on_set_enabled(self, self.node, state)
|
||||
@@ -469,11 +487,11 @@ end
|
||||
--- Get button enabled state.
|
||||
--
|
||||
-- By default all Buttons is enabled on creating.
|
||||
-- @tparam Button self @{Button}
|
||||
-- @tparam Button self Button
|
||||
-- @treturn boolean @True, if button is enabled now, False overwise
|
||||
-- @usage
|
||||
-- local is_enabled = button:is_enabled()
|
||||
function Button.is_enabled(self)
|
||||
function M:is_enabled()
|
||||
return not self.disabled
|
||||
end
|
||||
|
||||
@@ -482,12 +500,12 @@ end
|
||||
-- Useful to restrict click outside out stencil node or scrollable content.
|
||||
--
|
||||
-- This functions calls automatically if you don't disable it in game.project: druid.no_stencil_check
|
||||
-- @tparam Button self @{Button}
|
||||
-- @tparam Button self Button
|
||||
-- @tparam node|string|nil zone Gui node
|
||||
-- @treturn Button Current button instance
|
||||
-- @usage
|
||||
-- button:set_click_zone("stencil_node")
|
||||
function Button.set_click_zone(self, zone)
|
||||
function M:set_click_zone(zone)
|
||||
self.click_zone = self:get_node(zone)
|
||||
self.hover:set_click_zone(zone)
|
||||
|
||||
@@ -496,12 +514,12 @@ end
|
||||
|
||||
|
||||
--- Set key name to trigger this button by keyboard.
|
||||
-- @tparam Button self @{Button}
|
||||
-- @tparam Button self Button
|
||||
-- @tparam hash|string key The action_id of the input key
|
||||
-- @treturn Button Current button instance
|
||||
-- @usage
|
||||
-- button:set_key_trigger("key_space")
|
||||
function Button.set_key_trigger(self, key)
|
||||
function M:set_key_trigger(key)
|
||||
self.key_trigger = hash(key)
|
||||
|
||||
return self
|
||||
@@ -513,7 +531,7 @@ end
|
||||
-- @treturn hash The action_id of the input key
|
||||
-- @usage
|
||||
-- local key_hash = button:get_key_trigger()
|
||||
function Button.get_key_trigger(self)
|
||||
function M:get_key_trigger()
|
||||
return self.key_trigger
|
||||
end
|
||||
|
||||
@@ -523,7 +541,7 @@ end
|
||||
-- @tparam function|nil check_function Should return true or false. If true - button can be pressed.
|
||||
-- @tparam function|nil failure_callback Function will be called on button click, if check function return false
|
||||
-- @treturn Button Current button instance
|
||||
function Button.set_check_function(self, check_function, failure_callback)
|
||||
function M:set_check_function(check_function, failure_callback)
|
||||
self._check_function = check_function
|
||||
self._failure_callback = failure_callback
|
||||
end
|
||||
@@ -540,10 +558,10 @@ end
|
||||
-- @treturn Button Current button instance
|
||||
-- @usage
|
||||
-- button:set_web_user_interaction(true)
|
||||
function Button.set_web_user_interaction(self, is_web_mode)
|
||||
function M:set_web_user_interaction(is_web_mode)
|
||||
self._is_html5_mode = not not (is_web_mode and html5)
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
return Button
|
||||
return M
|
||||
|
@@ -14,19 +14,19 @@
|
||||
-- @tfield node node
|
||||
|
||||
--- Event on touch start callback(self)
|
||||
-- @tfield DruidEvent on_touch_start @{DruidEvent}
|
||||
-- @tfield DruidEvent on_touch_start DruidEvent
|
||||
|
||||
--- Event on touch end callback(self)
|
||||
-- @tfield DruidEvent on_touch_end @{DruidEvent}
|
||||
-- @tfield DruidEvent on_touch_end DruidEvent
|
||||
|
||||
--- Event on drag start callback(self, touch)
|
||||
-- @tfield DruidEvent on_drag_start @{DruidEvent}
|
||||
-- @tfield DruidEvent on_drag_start DruidEvent
|
||||
|
||||
--- on drag progress callback(self, dx, dy, total_x, total_y, touch)
|
||||
-- @tfield DruidEvent on_drag Event @{DruidEvent}
|
||||
-- @tfield DruidEvent on_drag Event DruidEvent
|
||||
|
||||
--- Event on drag end callback(self, total_x, total_y, touch)
|
||||
-- @tfield DruidEvent on_drag_end @{DruidEvent}
|
||||
-- @tfield DruidEvent on_drag_end DruidEvent
|
||||
|
||||
--- Is component now touching
|
||||
-- @tfield boolean is_touch
|
||||
@@ -62,7 +62,31 @@ local const = require("druid.const")
|
||||
local helper = require("druid.helper")
|
||||
local component = require("druid.component")
|
||||
|
||||
local Drag = component.create("drag", const.PRIORITY_INPUT_HIGH)
|
||||
---@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 style table
|
||||
---@field click_zone node
|
||||
---@field is_touch boolean
|
||||
---@field is_drag boolean
|
||||
---@field can_x boolean
|
||||
---@field can_y boolean
|
||||
---@field dx number
|
||||
---@field dy number
|
||||
---@field touch_id number
|
||||
---@field x number
|
||||
---@field y number
|
||||
---@field screen_x number
|
||||
---@field screen_y number
|
||||
---@field touch_start_pos vector3
|
||||
---@field private _is_enabled boolean
|
||||
---@field private _x_koef number
|
||||
---@field private _y_koef number
|
||||
local M = component.create("drag", const.PRIORITY_INPUT_HIGH)
|
||||
|
||||
|
||||
local function start_touch(self, touch)
|
||||
@@ -177,19 +201,18 @@ end
|
||||
-- @table style
|
||||
-- @tfield number|nil DRAG_DEADZONE Distance in pixels to start dragging. Default: 10
|
||||
-- @tfield boolean|nil NO_USE_SCREEN_KOEF If screen aspect ratio affects on drag values. Default: false
|
||||
function Drag.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
self.style.DRAG_DEADZONE = style.DRAG_DEADZONE or 10
|
||||
self.style.NO_USE_SCREEN_KOEF = style.NO_USE_SCREEN_KOEF or false
|
||||
end
|
||||
|
||||
|
||||
--- The @{Drag} constructor
|
||||
-- @tparam Drag self @{Drag}
|
||||
-- @tparam node node GUI node to detect dragging
|
||||
-- @tparam function on_drag_callback Callback for on_drag_event(self, dx, dy)
|
||||
function Drag.init(self, node, on_drag_callback)
|
||||
self.node = self:get_node(node)
|
||||
---Drag constructor
|
||||
---@param node_or_node_id node|string
|
||||
---@param on_drag_callback function
|
||||
function M:init(node_or_node_id, on_drag_callback)
|
||||
self.node = self:get_node(node_or_node_id)
|
||||
|
||||
self.dx = 0
|
||||
self.dy = 0
|
||||
@@ -219,7 +242,7 @@ function Drag.init(self, node, on_drag_callback)
|
||||
end
|
||||
|
||||
|
||||
function Drag.on_late_init(self)
|
||||
function M:on_late_init()
|
||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||
if stencil_node then
|
||||
@@ -229,7 +252,7 @@ function Drag.on_late_init(self)
|
||||
end
|
||||
|
||||
|
||||
function Drag.on_window_resized(self)
|
||||
function M:on_window_resized()
|
||||
local x_koef, y_koef = helper.get_screen_aspect_koef()
|
||||
self._x_koef = x_koef
|
||||
self._y_koef = y_koef
|
||||
@@ -237,14 +260,17 @@ function Drag.on_window_resized(self)
|
||||
end
|
||||
|
||||
|
||||
function Drag.on_input_interrupt(self)
|
||||
function M:on_input_interrupt()
|
||||
if self.is_drag or self.is_touch then
|
||||
end_touch(self)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Drag.on_input(self, action_id, action)
|
||||
---@local
|
||||
---@param action_id string
|
||||
---@param action table
|
||||
function M:on_input(action_id, action)
|
||||
if action_id ~= const.ACTION_TOUCH and action_id ~= const.ACTION_MULTITOUCH then
|
||||
return false
|
||||
end
|
||||
@@ -321,29 +347,31 @@ function Drag.on_input(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
--- Strict drag click area. Useful for
|
||||
-- restrict events outside stencil node
|
||||
-- @tparam Drag self @{Drag}
|
||||
-- @tparam node|string|nil node Gui node
|
||||
function Drag.set_click_zone(self, node)
|
||||
---Set Drag click zone
|
||||
---@param node node|string|nil
|
||||
---@return druid.drag self
|
||||
function M:set_click_zone(node)
|
||||
self.click_zone = self:get_node(node)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Set Drag input enabled or disabled
|
||||
-- @tparam Drag self @{Drag}
|
||||
-- @tparam boolean|nil is_enabled
|
||||
function Drag.set_enabled(self, is_enabled)
|
||||
---Set Drag component enabled state.
|
||||
---@param is_enabled boolean
|
||||
---@return druid.drag self
|
||||
function M:set_enabled(is_enabled)
|
||||
self._is_enabled = is_enabled
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Check if Drag component is enabled
|
||||
-- @tparam Drag self @{Drag}
|
||||
-- @treturn boolean
|
||||
function Drag.is_enabled(self)
|
||||
---Check if Drag component is enabled
|
||||
---@return boolean
|
||||
function M:is_enabled()
|
||||
return self._is_enabled
|
||||
end
|
||||
|
||||
|
||||
return Drag
|
||||
return M
|
||||
|
@@ -9,10 +9,10 @@
|
||||
-- @tfield node node
|
||||
|
||||
--- On hover callback(self, state, hover_instance)
|
||||
-- @tfield DruidEvent on_hover @{DruidEvent}
|
||||
-- @tfield DruidEvent on_hover DruidEvent
|
||||
|
||||
--- On mouse hover callback(self, state, hover_instance)
|
||||
-- @tfield DruidEvent on_mouse_hover @{DruidEvent}
|
||||
-- @tfield DruidEvent on_mouse_hover DruidEvent
|
||||
|
||||
---
|
||||
|
||||
@@ -21,15 +21,25 @@ local const = require("druid.const")
|
||||
local helper = require("druid.helper")
|
||||
local component = require("druid.component")
|
||||
|
||||
local Hover = component.create("hover")
|
||||
---@class druid.hover: druid.base_component
|
||||
---@field node node
|
||||
---@field on_hover druid.event
|
||||
---@field on_mouse_hover druid.event
|
||||
---@field style table
|
||||
---@field click_zone node
|
||||
---@field private _is_hovered boolean
|
||||
---@field private _is_mouse_hovered boolean
|
||||
---@field private _is_enabled boolean
|
||||
---@field private _is_mobile boolean
|
||||
local M = component.create("hover")
|
||||
|
||||
|
||||
--- The @{Hover} constructor
|
||||
-- @tparam Hover self @{Hover}
|
||||
--- The Hover constructor
|
||||
-- @tparam Hover self Hover
|
||||
-- @tparam node node Gui node
|
||||
-- @tparam function on_hover_callback Hover callback
|
||||
-- @tparam function on_mouse_hover On mouse hover callback
|
||||
function Hover.init(self, node, on_hover_callback, on_mouse_hover)
|
||||
function M:init(node, on_hover_callback, on_mouse_hover)
|
||||
self.node = self:get_node(node)
|
||||
|
||||
self._is_hovered = false
|
||||
@@ -42,7 +52,7 @@ function Hover.init(self, node, on_hover_callback, on_mouse_hover)
|
||||
end
|
||||
|
||||
|
||||
function Hover.on_late_init(self)
|
||||
function M:on_late_init()
|
||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||
if stencil_node then
|
||||
@@ -58,14 +68,14 @@ end
|
||||
-- @table style
|
||||
-- @tfield[opt] string ON_HOVER_CURSOR Mouse hover style on node hover
|
||||
-- @tfield[opt] string ON_MOUSE_HOVER_CURSOR Mouse hover style on node mouse hover
|
||||
function Hover.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
self.style.ON_HOVER_CURSOR = style.ON_HOVER_CURSOR or nil
|
||||
self.style.ON_MOUSE_HOVER_CURSOR = style.ON_MOUSE_HOVER_CURSOR or nil
|
||||
end
|
||||
|
||||
|
||||
function Hover.on_input(self, action_id, action)
|
||||
function M:on_input(action_id, action)
|
||||
if action_id ~= const.ACTION_TOUCH and action_id ~= nil then
|
||||
return false
|
||||
end
|
||||
@@ -99,15 +109,15 @@ function Hover.on_input(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
function Hover.on_input_interrupt(self)
|
||||
function M:on_input_interrupt()
|
||||
self:set_hover(false)
|
||||
end
|
||||
|
||||
|
||||
--- Set hover state
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @tparam boolean|nil state The hover state
|
||||
function Hover.set_hover(self, state)
|
||||
function M:set_hover(state)
|
||||
if self._is_hovered == state then
|
||||
return
|
||||
end
|
||||
@@ -122,17 +132,17 @@ end
|
||||
|
||||
|
||||
--- Return current hover state. True if touch action was on the node at current time
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @treturn boolean The current hovered state
|
||||
function Hover.is_hovered(self)
|
||||
function M:is_hovered()
|
||||
return self._is_hovered
|
||||
end
|
||||
|
||||
|
||||
--- Set mouse hover state
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @tparam boolean|nil state The mouse hover state
|
||||
function Hover.set_mouse_hover(self, state)
|
||||
function M:set_mouse_hover(state)
|
||||
if self._is_mouse_hovered == state then
|
||||
return
|
||||
end
|
||||
@@ -147,18 +157,18 @@ end
|
||||
|
||||
|
||||
--- Return current hover state. True if nil action_id (usually desktop mouse) was on the node at current time
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @treturn boolean The current hovered state
|
||||
function Hover.is_mouse_hovered(self)
|
||||
function M:is_mouse_hovered()
|
||||
return self._is_mouse_hovered
|
||||
end
|
||||
|
||||
|
||||
--- Strict hover click area. Useful for
|
||||
-- no click events outside stencil node
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @tparam node|string|nil zone Gui node
|
||||
function Hover.set_click_zone(self, zone)
|
||||
function M:set_click_zone(zone)
|
||||
self.click_zone = self:get_node(zone)
|
||||
end
|
||||
|
||||
@@ -166,9 +176,9 @@ end
|
||||
--- Set enable state of hover component.
|
||||
-- If hover is not enabled, it will not generate
|
||||
-- any hover events
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @tparam boolean|nil state The hover enabled state
|
||||
function Hover.set_enabled(self, state)
|
||||
function M:set_enabled(state)
|
||||
self._is_enabled = state
|
||||
|
||||
if not state then
|
||||
@@ -183,16 +193,17 @@ end
|
||||
|
||||
|
||||
--- Return current hover enabled state
|
||||
-- @tparam Hover self @{Hover}
|
||||
-- @tparam Hover self Hover
|
||||
-- @treturn boolean The hover enabled state
|
||||
function Hover.is_enabled(self)
|
||||
function M:is_enabled()
|
||||
return self._is_enabled
|
||||
end
|
||||
|
||||
|
||||
-- Internal cursor stack
|
||||
local cursor_stack = {}
|
||||
function Hover:_set_cursor(priority, cursor)
|
||||
---@local
|
||||
function M:_set_cursor(priority, cursor)
|
||||
if not defos then
|
||||
return
|
||||
end
|
||||
@@ -217,4 +228,4 @@ function Hover:_set_cursor(priority, cursor)
|
||||
end
|
||||
|
||||
|
||||
return Hover
|
||||
return M
|
||||
|
@@ -39,13 +39,13 @@
|
||||
|
||||
|
||||
--- On scroll move callback(self, position)
|
||||
-- @tfield DruidEvent on_scroll @{DruidEvent}
|
||||
-- @tfield DruidEvent on_scroll DruidEvent
|
||||
|
||||
--- On scroll_to function callback(self, target, is_instant)
|
||||
-- @tfield DruidEvent on_scroll_to @{DruidEvent}
|
||||
-- @tfield DruidEvent on_scroll_to DruidEvent
|
||||
|
||||
--- On scroll_to_index function callback(self, index, point)
|
||||
-- @tfield DruidEvent on_point_scroll @{DruidEvent}
|
||||
-- @tfield DruidEvent on_point_scroll DruidEvent
|
||||
|
||||
--- Scroll view node
|
||||
-- @tfield node view_node
|
||||
@@ -75,7 +75,7 @@
|
||||
-- @tfield vector3 available_size
|
||||
|
||||
--- Drag Druid component
|
||||
-- @tfield Drag drag @{Drag}
|
||||
-- @tfield Drag drag Drag
|
||||
|
||||
--- Current index of points of interests
|
||||
-- @tfield number|nil selected
|
||||
@@ -90,7 +90,16 @@ local const = require("druid.const")
|
||||
local helper = require("druid.helper")
|
||||
local component = require("druid.component")
|
||||
|
||||
local Scroll = component.create("scroll")
|
||||
---@class druid.scroll: druid.base_component
|
||||
---@field on_scroll druid.event
|
||||
---@field on_scroll_to druid.event
|
||||
---@field on_point_scroll druid.event
|
||||
---@field view_node node
|
||||
---@field view_border vector4
|
||||
---@field content_node node
|
||||
---@field view_size vector3
|
||||
---@field position vector3
|
||||
local M = component.create("scroll")
|
||||
|
||||
|
||||
local function inverse_lerp(min, max, current)
|
||||
@@ -138,7 +147,7 @@ end
|
||||
-- @tfield boolean|nil WHEEL_SCROLL_SPEED The scroll speed via mouse wheel scroll or touchpad. Set to 0 to disable wheel scrolling. Default: 0
|
||||
-- @tfield boolean|nil WHEEL_SCROLL_INVERTED If true, invert direction for touchpad and mouse wheel scroll. Default: false
|
||||
-- @tfield boolean|nil WHEEL_SCROLL_BY_INERTION If true, wheel will add inertion to scroll. Direct set position otherwise.. Default: false
|
||||
function Scroll.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
self.style.EXTRA_STRETCH_SIZE = style.EXTRA_STRETCH_SIZE or 0
|
||||
self.style.ANIM_SPEED = style.ANIM_SPEED or 0.2
|
||||
@@ -161,11 +170,11 @@ function Scroll.on_style_change(self, style)
|
||||
end
|
||||
|
||||
|
||||
--- The @{Scroll} constructor
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
--- The Scroll constructor
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam string|node view_node GUI view scroll node
|
||||
-- @tparam string|node content_node GUI content scroll node
|
||||
function Scroll.init(self, view_node, content_node)
|
||||
function M:init(view_node, content_node)
|
||||
self.druid = self:get_druid()
|
||||
|
||||
self.view_node = self:get_node(view_node)
|
||||
@@ -203,7 +212,7 @@ function Scroll.init(self, view_node, content_node)
|
||||
end
|
||||
|
||||
|
||||
function Scroll.on_late_init(self)
|
||||
function M:on_late_init()
|
||||
if not self.click_zone and const.IS_STENCIL_CHECK then
|
||||
local stencil_node = helper.get_closest_stencil_node(self.node)
|
||||
if stencil_node then
|
||||
@@ -213,12 +222,12 @@ function Scroll.on_late_init(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll.on_layout_change(self)
|
||||
function M:on_layout_change()
|
||||
gui.set_position(self.content_node, self.position)
|
||||
end
|
||||
|
||||
|
||||
function Scroll.update(self, dt)
|
||||
function M:update(dt)
|
||||
if self.is_animate then
|
||||
self.position.x = gui.get(self.content_node, "position.x")
|
||||
self.position.y = gui.get(self.content_node, "position.y")
|
||||
@@ -233,23 +242,23 @@ function Scroll.update(self, dt)
|
||||
end
|
||||
|
||||
|
||||
function Scroll.on_input(self, action_id, action)
|
||||
function M:on_input(action_id, action)
|
||||
return self:_process_scroll_wheel(action_id, action)
|
||||
end
|
||||
|
||||
|
||||
function Scroll.on_remove(self)
|
||||
function M:on_remove()
|
||||
self:bind_grid(nil)
|
||||
end
|
||||
|
||||
|
||||
--- Start scroll to target point.
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam vector3 point Target point
|
||||
-- @tparam boolean|nil is_instant Instant scroll flag
|
||||
-- @usage scroll:scroll_to(vmath.vector3(0, 50, 0))
|
||||
-- @usage scroll:scroll_to(vmath.vector3(0), true)
|
||||
function Scroll.scroll_to(self, point, is_instant)
|
||||
function M:scroll_to(point, is_instant)
|
||||
local b = self.available_pos
|
||||
local target = vmath.vector3(
|
||||
self._is_horizontal_scroll and -point.x or self.target_position.x,
|
||||
@@ -278,10 +287,10 @@ end
|
||||
|
||||
|
||||
--- Scroll to item in scroll by point index.
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam number index Point index
|
||||
-- @tparam boolean|nil skip_cb If true, skip the point callback
|
||||
function Scroll.scroll_to_index(self, index, skip_cb)
|
||||
function M:scroll_to_index(index, skip_cb)
|
||||
if not self.points then
|
||||
return
|
||||
end
|
||||
@@ -301,11 +310,11 @@ end
|
||||
|
||||
|
||||
--- Start scroll to target scroll percent
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam vector3 percent target percent
|
||||
-- @tparam boolean|nil is_instant instant scroll flag
|
||||
-- @usage scroll:scroll_to_percent(vmath.vector3(0.5, 0, 0))
|
||||
function Scroll.scroll_to_percent(self, percent, is_instant)
|
||||
function M:scroll_to_percent(percent, is_instant)
|
||||
local border = self.available_pos
|
||||
|
||||
local pos = vmath.vector3(
|
||||
@@ -327,9 +336,9 @@ end
|
||||
|
||||
--- Return current scroll progress status.
|
||||
-- Values will be in [0..1] interval
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @treturn vector3 New vector with scroll progress values
|
||||
function Scroll.get_percent(self)
|
||||
function M:get_percent()
|
||||
local x_perc = 1 - inverse_lerp(self.available_pos.x, self.available_pos.z, self.position.x)
|
||||
local y_perc = inverse_lerp(self.available_pos.w, self.available_pos.y, self.position.y)
|
||||
|
||||
@@ -339,11 +348,11 @@ end
|
||||
|
||||
--- Set scroll content size.
|
||||
-- It will change content gui node size
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam vector3 size The new size for content node
|
||||
-- @tparam vector3|nil offset Offset value to set, where content is starts
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_size(self, size, offset)
|
||||
function M:set_size(size, offset)
|
||||
if offset then
|
||||
self._offset = offset
|
||||
end
|
||||
@@ -355,10 +364,10 @@ end
|
||||
|
||||
|
||||
--- Set new scroll view size in case the node size was changed.
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam vector3 size The new size for view node
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_view_size(self, size)
|
||||
function M:set_view_size(size)
|
||||
gui.set_size(self.view_node, size)
|
||||
self.view_size = size
|
||||
self.view_border = helper.get_border(self.view_node)
|
||||
@@ -369,8 +378,8 @@ end
|
||||
|
||||
|
||||
--- Refresh scroll view size
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
function Scroll.update_view_size(self)
|
||||
-- @tparam Scroll self Scroll
|
||||
function M:update_view_size()
|
||||
self.view_size = helper.get_scaled_size(self.view_node)
|
||||
self.view_border = helper.get_border(self.view_node)
|
||||
self:_update_size()
|
||||
@@ -382,10 +391,10 @@ end
|
||||
--- Enable or disable scroll inert.
|
||||
-- If disabled, scroll through points (if exist)
|
||||
-- If no points, just simple drag without inertion
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam boolean|nil state Inert scroll state
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_inert(self, state)
|
||||
function M:set_inert(state)
|
||||
self._is_inert = state
|
||||
|
||||
return self
|
||||
@@ -393,19 +402,19 @@ end
|
||||
|
||||
|
||||
--- Return if scroll have inertion.
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @treturn boolean @If scroll have inertion
|
||||
function Scroll.is_inert(self)
|
||||
function M:is_inert()
|
||||
return self._is_inert
|
||||
end
|
||||
|
||||
|
||||
--- Set extra size for scroll stretching.
|
||||
-- Set 0 to disable stretching effect
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam number|nil stretch_size Size in pixels of additional scroll area
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_extra_stretch_size(self, stretch_size)
|
||||
function M:set_extra_stretch_size(stretch_size)
|
||||
self.style.EXTRA_STRETCH_SIZE = stretch_size or 0
|
||||
self:_update_size()
|
||||
|
||||
@@ -414,19 +423,19 @@ end
|
||||
|
||||
|
||||
--- Return vector of scroll size with width and height.
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @treturn vector3 Available scroll size
|
||||
function Scroll.get_scroll_size(self)
|
||||
function M:get_scroll_size()
|
||||
return self.available_size
|
||||
end
|
||||
|
||||
|
||||
--- Set points of interest.
|
||||
-- Scroll will always centered on closer points
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam table points Array of vector3 points
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_points(self, points)
|
||||
function M:set_points(points)
|
||||
self.points = points
|
||||
|
||||
table.sort(self.points, function(a, b)
|
||||
@@ -440,10 +449,10 @@ end
|
||||
|
||||
|
||||
--- Lock or unlock horizontal scroll
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam boolean|nil state True, if horizontal scroll is enabled
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_horizontal_scroll(self, state)
|
||||
function M:set_horizontal_scroll(state)
|
||||
self._is_horizontal_scroll = state
|
||||
self.drag.can_x = self.available_size.x > 0 and state
|
||||
return self
|
||||
@@ -451,10 +460,10 @@ end
|
||||
|
||||
|
||||
--- Lock or unlock vertical scroll
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam boolean|nil state True, if vertical scroll is enabled
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.set_vertical_scroll(self, state)
|
||||
function M:set_vertical_scroll(state)
|
||||
self._is_vertical_scroll = state
|
||||
self.drag.can_y = self.available_size.y > 0 and state
|
||||
return self
|
||||
@@ -463,10 +472,10 @@ end
|
||||
|
||||
--- Check node if it visible now on scroll.
|
||||
-- Extra border is not affected. Return true for elements in extra scroll zone
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam node node The node to check
|
||||
-- @treturn boolean True if node in visible scroll area
|
||||
function Scroll.is_node_in_view(self, node)
|
||||
function M:is_node_in_view(node)
|
||||
local node_offset_for_view = gui.get_position(node)
|
||||
local parent = gui.get_parent(node)
|
||||
local is_parent_of_view = false
|
||||
@@ -504,10 +513,10 @@ end
|
||||
|
||||
--- Bind the grid component (Static or Dynamic) to recalculate
|
||||
-- scroll size on grid changes
|
||||
-- @tparam Scroll self @{Scroll}
|
||||
-- @tparam Scroll self Scroll
|
||||
-- @tparam StaticGrid grid Druid grid component
|
||||
-- @treturn druid.scroll Current scroll instance
|
||||
function Scroll.bind_grid(self, grid)
|
||||
function M:bind_grid(grid)
|
||||
if self._grid_on_change then
|
||||
self._grid_on_change:unsubscribe(self._grid_on_change_callback)
|
||||
|
||||
@@ -535,12 +544,12 @@ end
|
||||
-- restrict events outside stencil node
|
||||
-- @tparam Drag self
|
||||
-- @tparam node|string node Gui node
|
||||
function Scroll.set_click_zone(self, node)
|
||||
function M:set_click_zone(node)
|
||||
self.drag:set_click_zone(node)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._on_scroll_drag(self, dx, dy)
|
||||
function M:_on_scroll_drag(dx, dy)
|
||||
local t = self.target_position
|
||||
local b = self.available_pos
|
||||
local eb = self.available_pos_extra
|
||||
@@ -581,7 +590,7 @@ function Scroll._on_scroll_drag(self, dx, dy)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._check_soft_zone(self)
|
||||
function M:_check_soft_zone()
|
||||
local target = self.target_position
|
||||
local border = self.available_pos
|
||||
local speed = self.style.BACK_SPEED
|
||||
@@ -610,7 +619,7 @@ end
|
||||
|
||||
|
||||
-- Cancel animation on other animation or input touch
|
||||
function Scroll._cancel_animate(self)
|
||||
function M:_cancel_animate()
|
||||
self.inertion.x = 0
|
||||
self.inertion.y = 0
|
||||
|
||||
@@ -624,7 +633,7 @@ function Scroll._cancel_animate(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._set_scroll_position(self, position_x, position_y)
|
||||
function M:_set_scroll_position(position_x, position_y)
|
||||
local available_extra = self.available_pos_extra
|
||||
position_x = helper.clamp(position_x, available_extra.x, available_extra.z)
|
||||
position_y = helper.clamp(position_y, available_extra.w, available_extra.y)
|
||||
@@ -643,7 +652,7 @@ end
|
||||
-- if no inert, scroll to next point by scroll direction
|
||||
-- if inert, find next point by scroll director
|
||||
-- @local
|
||||
function Scroll._check_points(self)
|
||||
function M:_check_points()
|
||||
if not self.points then
|
||||
return
|
||||
end
|
||||
@@ -699,7 +708,7 @@ function Scroll._check_points(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._check_threshold(self)
|
||||
function M:_check_threshold()
|
||||
local is_stopped = false
|
||||
|
||||
if self.drag.can_x and math.abs(self.inertion.x) < self.style.INERT_THRESHOLD then
|
||||
@@ -717,7 +726,7 @@ function Scroll._check_threshold(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._update_free_scroll(self, dt)
|
||||
function M:_update_free_scroll(dt)
|
||||
if self.is_animate then
|
||||
return
|
||||
end
|
||||
@@ -742,7 +751,7 @@ function Scroll._update_free_scroll(self, dt)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._update_hand_scroll(self, dt)
|
||||
function M:_update_hand_scroll(dt)
|
||||
if self.is_animate then
|
||||
self:_cancel_animate()
|
||||
end
|
||||
@@ -757,7 +766,7 @@ function Scroll._update_hand_scroll(self, dt)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._on_touch_start(self)
|
||||
function M:_on_touch_start()
|
||||
self.inertion.x = 0
|
||||
self.inertion.y = 0
|
||||
self.target_position.x = self.position.x
|
||||
@@ -765,12 +774,12 @@ function Scroll._on_touch_start(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._on_touch_end(self)
|
||||
function M:_on_touch_end()
|
||||
self:_check_threshold()
|
||||
end
|
||||
|
||||
|
||||
function Scroll._update_size(self)
|
||||
function M:_update_size()
|
||||
local content_border = helper.get_border(self.content_node)
|
||||
local content_size = helper.get_scaled_size(self.content_node)
|
||||
|
||||
@@ -808,7 +817,7 @@ function Scroll._update_size(self)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._process_scroll_wheel(self, action_id, action)
|
||||
function M:_process_scroll_wheel(action_id, action)
|
||||
if not self._is_mouse_hover or self.style.WHEEL_SCROLL_SPEED == 0 then
|
||||
return false
|
||||
end
|
||||
@@ -845,9 +854,9 @@ function Scroll._process_scroll_wheel(self, action_id, action)
|
||||
end
|
||||
|
||||
|
||||
function Scroll._on_mouse_hover(self, state)
|
||||
function M:_on_mouse_hover(state)
|
||||
self._is_mouse_hover = state
|
||||
end
|
||||
|
||||
|
||||
return Scroll
|
||||
return M
|
||||
|
@@ -37,19 +37,19 @@
|
||||
-- @alias druid.static_grid
|
||||
|
||||
--- On item add callback(self, node, index)
|
||||
-- @tfield DruidEvent on_add_item @{DruidEvent}
|
||||
-- @tfield DruidEvent on_add_item DruidEvent
|
||||
|
||||
--- On item remove callback(self, index)
|
||||
-- @tfield DruidEvent on_remove_item @{DruidEvent}
|
||||
-- @tfield DruidEvent on_remove_item DruidEvent
|
||||
|
||||
--- On item add, remove or change in_row callback(self, index|nil)
|
||||
-- @tfield DruidEvent on_change_items @{DruidEvent}
|
||||
-- @tfield DruidEvent on_change_items DruidEvent
|
||||
|
||||
--- On grid clear callback(self)
|
||||
-- @tfield DruidEvent on_clear @{DruidEvent}
|
||||
-- @tfield DruidEvent on_clear DruidEvent
|
||||
|
||||
--- On update item positions callback(self)
|
||||
-- @tfield DruidEvent on_update_positions @{DruidEvent}
|
||||
-- @tfield DruidEvent on_update_positions DruidEvent
|
||||
|
||||
--- Parent gui node
|
||||
-- @tfield node parent
|
||||
@@ -82,7 +82,23 @@ local Event = require("druid.event")
|
||||
local helper = require("druid.helper")
|
||||
local component = require("druid.component")
|
||||
|
||||
local StaticGrid = component.create("static_grid")
|
||||
---@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 parent node
|
||||
---@field nodes node[]
|
||||
---@field first_index number
|
||||
---@field last_index number
|
||||
---@field anchor vector3
|
||||
---@field pivot vector3
|
||||
---@field node_size vector3
|
||||
---@field border vector4
|
||||
---@field in_row number
|
||||
---@field style table
|
||||
local M = component.create("static_grid")
|
||||
|
||||
|
||||
local function _extend_border(border, pos, size, pivot)
|
||||
@@ -104,23 +120,23 @@ end
|
||||
-- @table style
|
||||
-- @tfield boolean|nil IS_DYNAMIC_NODE_POSES If true, always center grid content as grid pivot sets. Default: false
|
||||
-- @tfield boolean|nil IS_ALIGN_LAST_ROW If true, always align last row of the grid as grid pivot sets. Default: false
|
||||
function StaticGrid.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
self.style.IS_DYNAMIC_NODE_POSES = style.IS_DYNAMIC_NODE_POSES or false
|
||||
self.style.IS_ALIGN_LAST_ROW = style.IS_ALIGN_LAST_ROW or false
|
||||
end
|
||||
|
||||
|
||||
--- The @{StaticGrid} constructor
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
--- The StaticGrid constructor
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam string|node parent The GUI Node container, where grid's items will be placed
|
||||
-- @tparam node element Element prefab. Need to get it size
|
||||
-- @tparam number|nil in_row How many nodes in row can be placed. By default 1
|
||||
function StaticGrid.init(self, parent, element, in_row)
|
||||
function M:init(parent, element, in_row)
|
||||
self.parent = self:get_node(parent)
|
||||
self.nodes = {}
|
||||
|
||||
self.pivot = helper.get_pivot_offset(gui.get_pivot(self.parent))
|
||||
self.pivot = helper.get_pivot_offset(self.parent)
|
||||
self.anchor = vmath.vector3(0.5 + self.pivot.x, 0.5 - self.pivot.y, 0)
|
||||
|
||||
self.in_row = in_row or 1
|
||||
@@ -149,10 +165,10 @@ end
|
||||
|
||||
local _temp_pos = vmath.vector3(0)
|
||||
--- Return pos for grid node index
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam number index The grid element index
|
||||
-- @treturn vector3 @Node position
|
||||
function StaticGrid.get_pos(self, index)
|
||||
function M:get_pos(index)
|
||||
local row = math.ceil(index / self.in_row) - 1
|
||||
local col = (index - row * self.in_row) - 1
|
||||
|
||||
@@ -167,10 +183,10 @@ end
|
||||
|
||||
|
||||
--- Return index for grid pos
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam vector3 pos The node position in the grid
|
||||
-- @treturn number The node index
|
||||
function StaticGrid.get_index(self, pos)
|
||||
function M:get_index(pos)
|
||||
-- Offset to left-top corner from node pivot
|
||||
local node_offset_x = self.node_size.x * (-0.5 + self.node_pivot.x)
|
||||
local node_offset_y = self.node_size.y * (0.5 - self.node_pivot.y)
|
||||
@@ -187,10 +203,10 @@ end
|
||||
|
||||
|
||||
--- Return grid index by node
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam node node The gui node in the grid
|
||||
-- @treturn number The node index
|
||||
function StaticGrid.get_index_by_node(self, node)
|
||||
function M:get_index_by_node(node)
|
||||
for index, grid_node in pairs(self.nodes) do
|
||||
if node == grid_node then
|
||||
return index
|
||||
@@ -201,28 +217,28 @@ function StaticGrid.get_index_by_node(self, node)
|
||||
end
|
||||
|
||||
|
||||
function StaticGrid.on_layout_change(self)
|
||||
function M:on_layout_change()
|
||||
self:_update(true)
|
||||
end
|
||||
|
||||
|
||||
--- Set grid anchor. Default anchor is equal to anchor of grid parent node
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam vector3 anchor Anchor
|
||||
function StaticGrid.set_anchor(self, anchor)
|
||||
function M:set_anchor(anchor)
|
||||
self.anchor = anchor
|
||||
self:_update()
|
||||
end
|
||||
|
||||
|
||||
--- Update grid content
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
function StaticGrid.refresh(self)
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
function M:refresh()
|
||||
self:_update(true)
|
||||
end
|
||||
|
||||
|
||||
function StaticGrid.set_pivot(self, pivot)
|
||||
function M:set_pivot(pivot)
|
||||
local prev_pivot = helper.get_pivot_offset(gui.get_pivot(self.parent))
|
||||
self.pivot = helper.get_pivot_offset(pivot)
|
||||
|
||||
@@ -254,12 +270,12 @@ end
|
||||
|
||||
|
||||
--- Add new item to the grid
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam node item GUI node
|
||||
-- @tparam number|nil index The item position. By default add as last item
|
||||
-- @tparam number|nil shift_policy How shift nodes, if required. Default: const.SHIFT.RIGHT
|
||||
-- @tparam boolean|nil is_instant If true, update node positions instantly
|
||||
function StaticGrid.add(self, item, index, shift_policy, is_instant)
|
||||
function M:add(item, index, shift_policy, is_instant)
|
||||
index = index or ((self.last_index or 0) + 1)
|
||||
|
||||
helper.insert_with_shift(self.nodes, item, index, shift_policy)
|
||||
@@ -279,10 +295,10 @@ end
|
||||
|
||||
|
||||
--- Set new items to the grid. All previous items will be removed
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam node[] nodes The new grid nodes
|
||||
-- @tparam[opt=false] boolean is_instant If true, update node positions instantly
|
||||
function StaticGrid.set_items(self, nodes, is_instant)
|
||||
function M:set_items(nodes, is_instant)
|
||||
self.nodes = nodes
|
||||
for index = 1, #nodes do
|
||||
local item = nodes[index]
|
||||
@@ -296,12 +312,12 @@ end
|
||||
|
||||
|
||||
--- Remove the item from the grid. Note that gui node will be not deleted
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam number index The grid node index to remove
|
||||
-- @tparam number|nil shift_policy How shift nodes, if required. Default: const.SHIFT.RIGHT
|
||||
-- @tparam boolean|nil is_instant If true, update node positions instantly
|
||||
-- @treturn node The deleted gui node from grid
|
||||
function StaticGrid.remove(self, index, shift_policy, is_instant)
|
||||
function M:remove(index, shift_policy, is_instant)
|
||||
assert(self.nodes[index], "No grid item at given index " .. index)
|
||||
|
||||
local remove_node = self.nodes[index]
|
||||
@@ -317,9 +333,9 @@ end
|
||||
|
||||
|
||||
--- Return grid content size
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @treturn vector3 The grid content size
|
||||
function StaticGrid.get_size(self)
|
||||
function M:get_size()
|
||||
return vmath.vector3(
|
||||
self.border.z - self.border.x,
|
||||
self.border.y - self.border.w,
|
||||
@@ -327,7 +343,7 @@ function StaticGrid.get_size(self)
|
||||
end
|
||||
|
||||
|
||||
function StaticGrid.get_size_for(self, count)
|
||||
function M:get_size_for(count)
|
||||
if not count or count == 0 then
|
||||
return vmath.vector3(0)
|
||||
end
|
||||
@@ -350,17 +366,17 @@ end
|
||||
|
||||
|
||||
--- Return grid content borders
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @treturn vector3 The grid content borders
|
||||
function StaticGrid.get_borders(self)
|
||||
function M:get_borders()
|
||||
return self.border
|
||||
end
|
||||
|
||||
|
||||
--- Return array of all node positions
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @treturn vector3[] All grid node positions
|
||||
function StaticGrid.get_all_pos(self)
|
||||
function M:get_all_pos()
|
||||
local result = {}
|
||||
for i, node in pairs(self.nodes) do
|
||||
table.insert(result, gui.get_position(node))
|
||||
@@ -372,10 +388,10 @@ end
|
||||
|
||||
--- Change set position function for grid nodes. It will call on
|
||||
-- update poses on grid elements. Default: gui.set_position
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam function callback Function on node set position
|
||||
-- @treturn druid.static_grid Current grid instance
|
||||
function StaticGrid.set_position_function(self, callback)
|
||||
function M:set_position_function(callback)
|
||||
self._set_position_function = callback or gui.set_position
|
||||
|
||||
return self
|
||||
@@ -384,9 +400,9 @@ end
|
||||
|
||||
--- Clear grid nodes array. GUI nodes will be not deleted!
|
||||
-- If you want to delete GUI nodes, use static_grid.nodes array before grid:clear
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @treturn druid.static_grid Current grid instance
|
||||
function StaticGrid.clear(self)
|
||||
function M:clear()
|
||||
self.border.x = 0
|
||||
self.border.y = 0
|
||||
self.border.w = 0
|
||||
@@ -403,9 +419,9 @@ end
|
||||
|
||||
|
||||
--- Return StaticGrid offset, where StaticGrid content starts.
|
||||
-- @tparam StaticGrid self @{StaticGrid} The StaticGrid instance
|
||||
-- @tparam StaticGrid self StaticGrid The StaticGrid instance
|
||||
-- @treturn vector3 The StaticGrid offset
|
||||
function StaticGrid:get_offset()
|
||||
function M:get_offset()
|
||||
local borders = self:get_borders()
|
||||
local size = self:get_size()
|
||||
|
||||
@@ -419,10 +435,10 @@ end
|
||||
|
||||
|
||||
--- Set new in_row elements for grid
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam number in_row The new in_row value
|
||||
-- @treturn druid.static_grid Current grid instance
|
||||
function StaticGrid.set_in_row(self, in_row)
|
||||
function M:set_in_row(in_row)
|
||||
self.in_row = in_row
|
||||
self._grid_horizonal_offset = self.node_size.x * (self.in_row - 1) * self.anchor.x
|
||||
self._zero_offset = vmath.vector3(
|
||||
@@ -438,11 +454,11 @@ end
|
||||
|
||||
|
||||
--- Set new node size for grid
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam[opt] number width The new node width
|
||||
-- @tparam[opt] number height The new node height
|
||||
-- @treturn druid.static_grid Current grid instance
|
||||
function StaticGrid.set_item_size(self, width, height)
|
||||
function M:set_item_size(width, height)
|
||||
if width then
|
||||
self.node_size.x = width
|
||||
end
|
||||
@@ -463,20 +479,20 @@ end
|
||||
|
||||
|
||||
--- Sort grid nodes by custom comparator function
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam function comparator The comparator function. (a, b) -> boolean
|
||||
-- @treturn druid.static_grid Current grid instance
|
||||
function StaticGrid.sort_nodes(self, comparator)
|
||||
function M:sort_nodes(comparator)
|
||||
table.sort(self.nodes, comparator)
|
||||
self:_update(true)
|
||||
end
|
||||
|
||||
|
||||
--- Update grid inner state
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam boolean|nil is_instant If true, node position update instantly, otherwise with set_position_function callback
|
||||
-- @local
|
||||
function StaticGrid._update(self, is_instant)
|
||||
function M:_update(is_instant)
|
||||
self:_update_indexes()
|
||||
self:_update_borders()
|
||||
self:_update_pos(is_instant)
|
||||
@@ -484,9 +500,9 @@ end
|
||||
|
||||
|
||||
--- Update first and last indexes of grid nodes
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @local
|
||||
function StaticGrid._update_indexes(self)
|
||||
function M:_update_indexes()
|
||||
self.first_index = nil
|
||||
self.last_index = nil
|
||||
for index in pairs(self.nodes) do
|
||||
@@ -500,9 +516,9 @@ end
|
||||
|
||||
|
||||
--- Update grid content borders, recalculate min and max values
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @local
|
||||
function StaticGrid._update_borders(self)
|
||||
function M:_update_borders()
|
||||
if not self.first_index then
|
||||
self.border = vmath.vector4(0)
|
||||
return
|
||||
@@ -519,10 +535,10 @@ end
|
||||
|
||||
|
||||
--- Update grid nodes position
|
||||
-- @tparam StaticGrid self @{StaticGrid}
|
||||
-- @tparam StaticGrid self StaticGrid
|
||||
-- @tparam boolean|nil is_instant If true, node position update instantly, otherwise with set_position_function callback
|
||||
-- @local
|
||||
function StaticGrid._update_pos(self, is_instant)
|
||||
function M:_update_pos(is_instant)
|
||||
local zero_offset = self:_get_zero_offset()
|
||||
|
||||
for i, node in pairs(self.nodes) do
|
||||
@@ -545,7 +561,7 @@ end
|
||||
-- parent pivot node (0:0) with adjusting of node sizes and anchoring
|
||||
-- @treturn vector3 The offset vector
|
||||
-- @local
|
||||
function StaticGrid:_get_zero_offset()
|
||||
function M:_get_zero_offset()
|
||||
if not self.style.IS_DYNAMIC_NODE_POSES then
|
||||
return const.VECTOR_ZERO
|
||||
end
|
||||
@@ -562,7 +578,7 @@ end
|
||||
--- Return offset x for last row in grid. Used to align this row accorting to grid's anchor
|
||||
-- @treturn number The offset x value
|
||||
-- @local
|
||||
function StaticGrid:_get_zero_offset_x(row_index)
|
||||
function M:_get_zero_offset_x(row_index)
|
||||
if not self.style.IS_DYNAMIC_NODE_POSES or not self.style.IS_ALIGN_LAST_ROW then
|
||||
return self._zero_offset.x
|
||||
end
|
||||
@@ -580,4 +596,4 @@ function StaticGrid:_get_zero_offset_x(row_index)
|
||||
end
|
||||
|
||||
|
||||
return StaticGrid
|
||||
return M
|
||||
|
@@ -36,13 +36,13 @@
|
||||
-- @alias druid.text
|
||||
|
||||
--- On set text callback(self, text)
|
||||
-- @tfield DruidEvent on_set_text @{DruidEvent}
|
||||
-- @tfield DruidEvent on_set_text DruidEvent
|
||||
|
||||
--- On adjust text size callback(self, new_scale, text_metrics)
|
||||
-- @tfield DruidEvent on_update_text_scale @{DruidEvent}
|
||||
-- @tfield DruidEvent on_update_text_scale DruidEvent
|
||||
|
||||
--- On change pivot callback(self, pivot)
|
||||
-- @tfield DruidEvent on_set_pivot @{DruidEvent}
|
||||
-- @tfield DruidEvent on_set_pivot DruidEvent
|
||||
|
||||
--- Text node
|
||||
-- @tfield node node
|
||||
@@ -83,7 +83,16 @@ local utf8_lua = require("druid.system.utf8")
|
||||
local component = require("druid.component")
|
||||
local utf8 = utf8 or utf8_lua --[[@as utf8]]
|
||||
|
||||
local Text = component.create("text")
|
||||
---@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 style table
|
||||
---@field private start_pivot number
|
||||
---@field private start_scale vector3
|
||||
---@field private scale vector3
|
||||
local M = component.create("text")
|
||||
|
||||
local function update_text_size(self)
|
||||
if self.scale.x == 0 or self.scale.y == 0 then
|
||||
@@ -269,7 +278,7 @@ end
|
||||
-- @tfield string|nil DEFAULT_ADJUST The default adjust type for any text component. Default: DOWNSCALE
|
||||
-- @tfield string|nil ADJUST_STEPS Amount of iterations for text adjust by height. Default: 20
|
||||
-- @tfield string|nil ADJUST_SCALE_DELTA Scale step on each height adjust step. Default: 0.02
|
||||
function Text.on_style_change(self, style)
|
||||
function M:on_style_change(style)
|
||||
self.style = {}
|
||||
self.style.TRIM_POSTFIX = style.TRIM_POSTFIX or "..."
|
||||
self.style.DEFAULT_ADJUST = style.DEFAULT_ADJUST or const.TEXT_ADJUST.DOWNSCALE
|
||||
@@ -278,12 +287,12 @@ function Text.on_style_change(self, style)
|
||||
end
|
||||
|
||||
|
||||
--- The @{Text} constructor
|
||||
-- @tparam Text self @{Text}
|
||||
--- The Text constructor
|
||||
-- @tparam Text self Text
|
||||
-- @tparam string|node node Node name or GUI Text Node itself
|
||||
-- @tparam string|nil value Initial text. Default value is node text from GUI scene. Default: nil
|
||||
-- @tparam string|nil adjust_type Adjust type for text. By default is DOWNSCALE. Look const.TEXT_ADJUST for reference. Default: DOWNSCALE
|
||||
function Text.init(self, node, value, adjust_type)
|
||||
function M:init(node, value, adjust_type)
|
||||
self.node = self:get_node(node)
|
||||
self.pos = gui.get_position(self.node)
|
||||
self.node_id = gui.get_id(self.node)
|
||||
@@ -309,12 +318,12 @@ function Text.init(self, node, value, adjust_type)
|
||||
end
|
||||
|
||||
|
||||
function Text.on_layout_change(self)
|
||||
function M:on_layout_change()
|
||||
self:set_to(self.last_value)
|
||||
end
|
||||
|
||||
|
||||
function Text.on_message_input(self, node_id, message)
|
||||
function M:on_message_input(node_id, message)
|
||||
if node_id ~= self.node_id then
|
||||
return false
|
||||
end
|
||||
@@ -326,11 +335,11 @@ end
|
||||
|
||||
|
||||
--- Calculate text width with font with respect to trailing space
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam string text|nil
|
||||
-- @treturn number Width
|
||||
-- @treturn number Height
|
||||
function Text.get_text_size(self, text)
|
||||
function M:get_text_size(text)
|
||||
text = text or self.last_value
|
||||
local font_name = gui.get_font(self.node)
|
||||
local font = gui.get_font_resource(font_name)
|
||||
@@ -351,10 +360,10 @@ end
|
||||
|
||||
|
||||
--- Get chars count by width
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam number width
|
||||
-- @treturn number Chars count
|
||||
function Text.get_text_index_by_width(self, width)
|
||||
function M:get_text_index_by_width(width)
|
||||
local text = self.last_value
|
||||
local font_name = gui.get_font(self.node)
|
||||
local font = gui.get_font_resource(font_name)
|
||||
@@ -385,10 +394,10 @@ end
|
||||
|
||||
|
||||
--- Set text to text field
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam string set_to Text for node
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_to(self, set_to)
|
||||
function M:set_to(set_to)
|
||||
set_to = set_to or ""
|
||||
|
||||
self.last_value = set_to
|
||||
@@ -403,10 +412,10 @@ end
|
||||
|
||||
|
||||
--- Set text area size
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam vector3 size The new text area size
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_size(self, size)
|
||||
function M:set_size(size)
|
||||
self.start_size = size
|
||||
self.text_area = vmath.vector3(size)
|
||||
self.text_area.x = self.text_area.x * self.start_scale.x
|
||||
@@ -416,10 +425,10 @@ end
|
||||
|
||||
|
||||
--- Set color
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam vector4 color Color for node
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_color(self, color)
|
||||
function M:set_color(color)
|
||||
self.color = color
|
||||
gui.set_color(self.node, color)
|
||||
|
||||
@@ -428,10 +437,10 @@ end
|
||||
|
||||
|
||||
--- Set alpha
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam number alpha Alpha for node
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_alpha(self, alpha)
|
||||
function M:set_alpha(alpha)
|
||||
self.color.w = alpha
|
||||
gui.set_color(self.node, self.color)
|
||||
|
||||
@@ -440,10 +449,10 @@ end
|
||||
|
||||
|
||||
--- Set scale
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam vector3 scale Scale for node
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_scale(self, scale)
|
||||
function M:set_scale(scale)
|
||||
self.last_scale = scale
|
||||
gui.set_scale(self.node, scale)
|
||||
|
||||
@@ -452,10 +461,10 @@ end
|
||||
|
||||
|
||||
--- Set text pivot. Text will re-anchor inside text area
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam number pivot The gui.PIVOT_* constant
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_pivot(self, pivot)
|
||||
function M:set_pivot(pivot)
|
||||
local prev_pivot = gui.get_pivot(self.node)
|
||||
local prev_offset = const.PIVOTS[prev_pivot]
|
||||
|
||||
@@ -478,19 +487,19 @@ end
|
||||
|
||||
|
||||
--- Return true, if text with line break
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @treturn boolean Is text node with line break
|
||||
function Text.is_multiline(self)
|
||||
function M:is_multiline()
|
||||
return gui.get_line_break(self.node)
|
||||
end
|
||||
|
||||
|
||||
--- Set text adjust, refresh the current text visuals, if needed
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam string|nil adjust_type See const.TEXT_ADJUST. If pass nil - use current adjust type
|
||||
-- @tparam number|nil minimal_scale If pass nil - not use minimal scale
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_text_adjust(self, adjust_type, minimal_scale)
|
||||
function M:set_text_adjust(adjust_type, minimal_scale)
|
||||
self.adjust_type = adjust_type
|
||||
self._minimal_scale = minimal_scale
|
||||
self:set_to(self.last_value)
|
||||
@@ -500,10 +509,10 @@ end
|
||||
|
||||
|
||||
--- Set minimal scale for DOWNSCALE_LIMITED or SCALE_THEN_SCROLL adjust types
|
||||
-- @tparam Text self @{Text}
|
||||
-- @tparam Text self Text
|
||||
-- @tparam number minimal_scale If pass nil - not use minimal scale
|
||||
-- @treturn Text Current text instance
|
||||
function Text.set_minimal_scale(self, minimal_scale)
|
||||
function M:set_minimal_scale(minimal_scale)
|
||||
self._minimal_scale = minimal_scale
|
||||
|
||||
return self
|
||||
@@ -512,9 +521,9 @@ end
|
||||
|
||||
--- Return current text adjust type
|
||||
-- @treturn number The current text adjust type
|
||||
function Text.get_text_adjust(self, adjust_type)
|
||||
function M:get_text_adjust(adjust_type)
|
||||
return self.adjust_type
|
||||
end
|
||||
|
||||
|
||||
return Text
|
||||
return M
|
||||
|
Reference in New Issue
Block a user