Skip to content

Commit

Permalink
Update timer & text
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Oct 14, 2024
1 parent a0113d3 commit 54345b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion druid/annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ function druid_instance.new_text(self, node, value, no_adjust) end
--- Create @{Timer} component
---@param self druid_instance
---@param node string|node Gui text node
---@param seconds_from number Start timer value in seconds
---@param seconds_from number|nil Start timer value in seconds
---@param seconds_to number|nil End timer value in seconds
---@param callback function|nil Function on timer end
---@return druid.timer @{Timer} component
Expand Down
9 changes: 9 additions & 0 deletions druid/base/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ local utf8 = utf8 or utf8_lua --[[@as utf8]]
local Text = component.create("text")

local function update_text_size(self)
if self.scale.x == 0 or self.scale.y == 0 then
return
end
if self.start_scale.x == 0 or self.start_scale.y == 0 then
return
end

local size = vmath.vector3(
self.start_size.x * (self.start_scale.x / self.scale.x),
self.start_size.y * (self.start_scale.y / self.scale.y),
Expand Down Expand Up @@ -207,6 +214,8 @@ local function update_text_with_trim(self, trim_postfix)
end

gui.set_text(self.node, new_text .. trim_postfix)
else
gui.set_text(self.node, self.last_value)
end
end

Expand Down
16 changes: 9 additions & 7 deletions druid/extended/timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,26 @@ end
--- The @{Timer} constructor
-- @tparam Timer self @{Timer}
-- @tparam node node Gui text node
-- @tparam number seconds_from Start timer value in seconds
-- @tparam number|nil seconds_from Start timer value in seconds
-- @tparam number|nil seconds_to End timer value in seconds
-- @tparam function|nil callback Function on timer end
function Timer.init(self, node, seconds_from, seconds_to, callback)
self.node = self:get_node(node)
seconds_from = math.max(seconds_from, 0)
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:set_to(seconds_from)
self:set_interval(seconds_from, seconds_to)
if seconds_from then
seconds_from = math.max(seconds_from, 0)
self:set_to(seconds_from)
self:set_interval(seconds_from, seconds_to)

if seconds_to - seconds_from == 0 then
self:set_state(false)
self.on_timer_end:trigger(self:get_context(), self)
if seconds_to - seconds_from == 0 then
self:set_state(false)
self.on_timer_end:trigger(self:get_context(), self)
end
end

return self
Expand Down

0 comments on commit 54345b9

Please sign in to comment.