--gfx option menu
--------------------------------------------------------------------

local Settings = nil
local OldSettings = {}
local SettingsMap = {}
local Buttons = {sys_gfx_shadows = nil, sys_gfx_animations = nil, sys_ratio = nil, sys_subtitles = nil }
local Scrolls = { scroll_details = nil, scroll_gamma = nil}

function OnShow()
    s.sys_menu_opt_gfx=1
    Init()
end

function OnCreate()
    s.sys_menu_opt_gfx=1
    Init()
end

function OnDestroy()
    s.sys_menu_opt_gfx=0
end

function OnHide()
    s.sys_menu_opt_gfx=0
end

function Init()
   if not Settings then
      Settings = Game:GetSettings() end
   Buttons["sys_gfx_shadows"]    = Button("sys_gfx_shadows")
   Buttons["sys_gfx_animations"] = Button("sys_gfx_animations")
   Buttons["sys_ratio"]          = Button("sys_ratio")
   Buttons["sys_subtitles"]      = Button("sys_subtitles")   
   Scrolls["scroll_details"]     = HScroll("scroll_details")
   Scrolls["scroll_gamma"]       = HScroll("scroll_gamma")
   
   SettingsMap["sys_gfx_shadows"]    = "gfx_shadows"
   SettingsMap["sys_gfx_animations"] = "gfx_animations"
   SettingsMap["sys_ratio"]          = "gfx_ratio"
   SettingsMap["sys_subtitles"]      = "gfx_subtitles"
   SettingsMap["scroll_details"]     = "gfx_details"
   SettingsMap["scroll_gamma"]       = "gfx_gamma"
   
   for k,v in pairs(Buttons) do 
      SetButtonState(v, SettingsMap[k])
   end
   for k,v in pairs(Scrolls) do 
      SetScrollPos(v, SettingsMap[k])
   end
   
   for k,v in pairs(SettingsMap) do 
      OldSettings[v] = Settings:GetParameter(v)
   end   
end
--------------------------------------------------------------------
--button
function btnBackOpt()
  MenuSettings:SetVisible(true)
  self:SetVisible(false)
  for k,v in pairs(OldSettings) do
    Settings:SetParameter(k, v)
  end
end

function OnOKClick()
  MenuSettings:SetVisible(true)
  self:SetVisible(false)
  for k,v in pairs(OldSettings) do
    print(Settings:GetParameter(v))
  end
  Settings:Save()
end
--------------------------------------------------------------------
-- checkboxes
function OnCheckboxClick(_sender)
  local btn = Button(_sender)
  btn:SetState( not btn:GetState() )
  Settings:SetParameter(SettingsMap[btn:GetName()], tostring(btn:GetState()) )
end

function OnScrollChange(_sender, _pos)
  local scroll = Scrolls[_sender:GetName()]
  Settings:SetParameter(SettingsMap[_sender:GetName()], scroll:GetScrollPosition() / scroll:GetScrollRange() )
end

function SetButtonState(Control, optName)
  if Settings:GetParameter(optName) == "false" then
    Control:SetState(false)
  else
    Control:SetState(true)
  end
end

function SetScrollPos(Control, optName)
  local v, x, r
  v = Settings:GetParameter(optName)
  if v == "" then v = 0.5 end
  r = Control:GetScrollRange()
  x = r * v
  Control:SetScrollPosition(x)
end
