---
-- @package   eg4-crank-ui
-- @file      menu-update.lua
-- @brief     Functionality for menu items.
--
-- @copyright Copyright 2019 Hanover Displays Limited.
-- @license   This program is the confidential and proprietary product of
--            Hanover Displays Limited. Any unauthorised use, reproduction or
--            transfer of this program is strictly prohibited. (Subject to 
--            limited distribution and restricted disclosure only.) All 
--            rights reserved.                                              
--
--


local FADE_TIME = 500
local FADE_START = 64
local FADE_INTERVAL = 50
local FADE_STEP = FADE_START * FADE_INTERVAL / FADE_TIME

local menu_item_fade_timer
local menu_item_fade = {}

---
--- @brief  Timer to fade the item.
---
local function CBMenuItemTouchFadeTimer()

  local data = {}
  local stop = true
  for alpha, value in pairs(menu_item_fade) do 
    value = value - FADE_STEP
    if value <= 0 then
      menu_item_fade[alpha] = nil
      data[alpha] = 0
    else
      stop = false
      menu_item_fade[alpha] = value
      data[alpha] = value
    end
  end
  gre.set_data(data)

  if stop then
    gre.timer_clear_interval(menu_item_fade_timer)
    menu_item_fade_timer = nil
  end
end

----
--- @brief  Fade a menu item.
---
function CBMenuItemTouchFade(mapargs)
  local alpha = mapargs.context_control..".fade"
  if mapargs.context_row then
    alpha = alpha.."."..mapargs.context_row..".1"
  end

  menu_item_fade[alpha] = FADE_START
  gre.set_value(alpha, FADE_START)
  
  if menu_item_fade_timer == nil then
    menu_item_fade_timer = gre.timer_set_interval(CBMenuItemTouchFadeTimer, FADE_INTERVAL)
  end
end

-- EOF --
