首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CE Lua中带自定义进度条的倒计时TImer

CE Lua中带自定义进度条的倒计时TImer
EN

Stack Overflow用户
提问于 2019-06-06 20:47:57
回答 1查看 702关注 0票数 0

对不起,我的英语不好。我做了一个倒计时计时器使用CE图形用户界面自定义进度条使用CE面板宽度。

对于倒计时计时器是不成问题的,用下面的函数就可以正常工作了。

代码语言:javascript
运行
复制
    function performWithDelay(delay,onFinish,onTick,onTickInterval)

    if type(delay)~='number' -- mandatory
    then error('delay is not a number') end

    if type(onFinish)~='function'  -- mandatory
    then error('onFinish is not a function') end

    if onTick and type(onTick)~='function' -- optional
    then error('onTick is not a function') end

    if onTickInterval and type(onTickInterval)~='number'  -- optional, default 1 second
    then error('onTickInterval is not a number') end
    onTickInterval = onTickInterval or 1000 -- default 1 second

    local f = function (t) -- thread function start
    local getTickCount = getTickCount
    local startTick = getTickCount()
    local endTick = startTick + delay
    local nextOnTick = startTick + onTickInterval
    local ticks

    if onTick then
     while true do
     ticks=getTickCount()
      if nextOnTick<ticks then
       nextOnTick=ticks+onTickInterval
       synchronize(onTick,endTick-ticks)
      end
     if endTick<ticks then break end
     sleep(1)
     end
     else
    while true do
    ticks=getTickCount()
    if endTick<ticks then break end
    sleep(1)
    end
   end

  if onFinish then synchronize(onFinish) end
  end -- thread function end

  local t = createNativeThreadSuspended(f)
  t.name = 'performWithDelay thread'
  t.resume()
  end

   function showTimeLeft(millisecondsLeft)
    local totalSeconds = millisecondsLeft // 1000
    local deciseconds = (millisecondsLeft % 1000) // 100
    LabelTimer.Caption = os.date("!%M:%S",totalSeconds)..'.'..deciseconds
   end

   function whenFinished()
    LabelTimer.Caption = "00:00.0"
    -- do something
    ButtonTimer.Enabled = true
   end

   function startCountDown()
    -- do something
    performWithDelay(20000,whenFinished,showTimeLeft,10)
    --- 20000 = 20 seconds
    ButtonTimer.Enabled = false
   end

   ButtonTimer.onClick = startCountDown

现在,对于自定义进度条,我已经创建了一个计时器和变量:

代码语言:javascript
运行
复制
  progressbar.width = 0  -- at start
  progessbar max. width = 208  -- at the end
  time = 20 seconds
  timer interval = 100  (1/10 seconds)
  progess bar step = ??

如何使用纯Lua脚本计算进度条宽度,使其在倒计时计时器=0时达到最大宽度?

EN

回答 1

Stack Overflow用户

发布于 2019-06-08 10:20:23

已解决:

代码语言:javascript
运行
复制
   function start()
    st = edtSpTimer.Text  --- input how many seconds from user
    if tonumber(st) == nil then return nil end  -- accept only number
    sec = tonumber(st) -- convert text to number
    pnPBar.Width = 208 -- set panel width as maximum progrees bar value
    step = 208 / sec   -- set step by value
    tmr.Enabled = true -- triggering a timer
    performWithDelay(st*1000,whenFinished,showTimeLeft,10)
   end

   function bar()
    sec = sec - 1   -- counter down the seconds value
    pnPBar.width = pnPBar.width - step   -- decrease progress bar width
   end

   tmr = createTimer()   -- a timer
   tmr.interval = 1000   -- set interval to 1000 = 1 second
   tmr.enabled = false
   tmr.OnTimer = bar
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56477948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档