------------------------------------------------------------------------------
class 'l_OptionsScreen' (BaseScreen)
------------------------------------------------------------------------------
local customCursorCheckbox = nil
local fullscreenCheckbox = nil
local widescreenCheckbox = nil
local testSound = nil
local testVoice = nil
------------------------------------------------------------------------------
function l_OptionsScreen:__init(name, screen)
	BaseScreen.__init(self, name, screen)
	
	-- create text
	self:CreateText('OPTIONS',       512, 110, 0.90)
    self:CreateText('MUSIC_VOLUME',  512, screen:GetObject('SliderBack_1'):GetY() - 45, 0.62)
    self:CreateText('SOUND_VOLUME',  512, screen:GetObject('SliderBack_2'):GetY() - 45, 0.62)
    self:CreateText('VOICE_VOLUME',  512, screen:GetObject('SliderBack_3'):GetY() - 45, 0.62)
    self:CreateText('CUSTOM_CURSOR', screen:GetObject('CheckboxBack_1'):GetX() + 40, screen:GetObject('CheckboxBack_1'):GetY(), 0.62, HGETEXT_LEFT + HGETEXT_MIDDLE)
    self:CreateText('FULLSCREEN',    screen:GetObject('CheckboxBack_2'):GetX() + 40, screen:GetObject('CheckboxBack_2'):GetY(), 0.62, HGETEXT_LEFT + HGETEXT_MIDDLE)
    local text_wide_screen = self:CreateText('WIDE_SCREEN', screen:GetObject('CheckboxBack_3'):GetX() + 40, screen:GetObject('CheckboxBack_3'):GetY(), 0.62, HGETEXT_LEFT + HGETEXT_MIDDLE)
	
	-- create buttons
	self:CreateButtonByName('Cancel', KEY_ESCAPE)
	self:CreateButtonByName('Ok',     KEY_ENTER)
	self:CreateButtonByName('Mode')
	
	if GM:GetCurrPlayer() and not GM:GetCurrPlayer().gameState:HasState(PLAY_MODE) then
        --bttn_mode:Hide()
         self.buttons['OnModeButtonClick']:SetActive(false)
         self.buttons['OnModeButtonClick']:SetPressed(true)
         self.buttons['OnModeButtonClick']:SetOpacity(0.3)
    end

    local zOffset = 150
    	
	-- create slider music
	do
	    self.musicSlider = CreateSlider()
	    self.musicSlider:SetRect(310, screen:GetObject('SliderBack_1'):GetY() - 15, 420, 30)
	    self.musicSlider:SetZ(zOffset)
	    self.musicSlider:SetKnobTexture("data/Texture/GUI/Options/SliderButton.png")
	    self.musicSlider:SetNumbersTexture("data/Texture/GUI/Options/SliderNumbers.png", 29, 36)
	    self.musicSlider:SetKnobSize(77)
	    self.musicSlider:AttachTo(screen)
	    self.musicSlider:SetValue( GM:GetMusicVolume() )
	    self.musicSlider:SetAction("SetMusicVolume", self)
	end
	
	-- create slider sound
	do
	    self.soundSlider = CreateSlider()
	    self.soundSlider:SetRect(310, screen:GetObject('SliderBack_2'):GetY() - 15, 420, 30)
	    self.soundSlider:SetZ(zOffset)
	    self.soundSlider:SetKnobTexture("data/Texture/GUI/Options/SliderButton.png")
	    self.soundSlider:SetNumbersTexture("data/Texture/GUI/Options/SliderNumbers.png", 29, 36)
	    self.soundSlider:SetKnobSize(77)
	    self.soundSlider:AttachTo(screen)
	    self.soundSlider:SetValue( GM:GetSoundVolume() )
	    self.soundSlider:SetAction("SetSoundVolume", self)
	end
	
	-- create slider voice
	do
	    self.voiceSlider = CreateSlider()
	    self.voiceSlider:SetRect(310, screen:GetObject('SliderBack_3'):GetY() - 15, 420, 30)
	    self.voiceSlider:SetZ(zOffset)
	    self.voiceSlider:SetKnobTexture("data/Texture/GUI/Options/SliderButton.png")
	    self.voiceSlider:SetNumbersTexture("data/Texture/GUI/Options/SliderNumbers.png", 29, 36)
	    self.voiceSlider:SetKnobSize(77)
	    self.voiceSlider:AttachTo(screen)
	    self.voiceSlider:SetValue( GM:GetVoiceVolume() )
	    self.voiceSlider:SetAction("SetVoiceVolume", self)
	end
	
	-- create checkbox "custom cursor"
	local checkBoxsSize = 50
	local correctionX   = 2
	local correctionY   = 2
	
	do
	    local anchor = screen:GetObject('CheckboxBack_1')
	    customCursorCheckbox = CreateCheckBox()
	    customCursorCheckbox:SetRect(anchor:GetX() - checkBoxsSize/2 + correctionX,
                                     anchor:GetY() - checkBoxsSize/2 + correctionY, checkBoxsSize, checkBoxsSize)
	    customCursorCheckbox:SetZ(zOffset)
	    customCursorCheckbox:SetOnTexture("data/Texture/GUI/Options/CheckboxTick.png")
	    customCursorCheckbox:AttachTo(screen)
	    customCursorCheckbox:SetState(GM:GetShowCustomCursor() and 1 or 0)
	    customCursorCheckbox:SetAction("ToggleCustomCursorVisible", self)
	end
	
	-- create checkbox "fullscreen"
	do
	    local anchor = screen:GetObject('CheckboxBack_2')
	    
	    fullscreenCheckbox = CreateCheckBox()
	    fullscreenCheckbox:SetRect(anchor:GetX() - checkBoxsSize/2 + correctionX,
                                   anchor:GetY() - checkBoxsSize/2 + correctionY, checkBoxsSize, checkBoxsSize)
	    fullscreenCheckbox:SetZ(zOffset)
	    fullscreenCheckbox:SetOnTexture("data/Texture/GUI/Options/CheckboxTick.png")
	    fullscreenCheckbox:AttachTo(screen)
	    fullscreenCheckbox:SetState(APP:IsFullScreen() and 1 or 0)
	    fullscreenCheckbox:SetAction("SetFullscreen", self)
	end

	-- create checkbox "widescreen"
	if GM:IsWidescreenModeSupported() then
	    local anchor = screen:GetObject('CheckboxBack_3')
	    
	    widescreenCheckbox = CreateCheckBox()
	    widescreenCheckbox:SetRect(anchor:GetX() - checkBoxsSize/2 + correctionX,
                                   anchor:GetY() - checkBoxsSize/2 + correctionY, checkBoxsSize, checkBoxsSize)
	    widescreenCheckbox:SetZ(zOffset)
	    widescreenCheckbox:SetOnTexture("data/Texture/GUI/Options/CheckboxTick.png")
	    widescreenCheckbox:AttachTo(screen)
	    widescreenCheckbox:SetState(GM:GetWidescreen() and 1 or 0)
	    widescreenCheckbox:SetAction("SetWidescreen", self)
    else
        text_wide_screen:SetColor('0x4FFFFFFF')
	end

	-- init test sound
	testSound = CreateSoundEntity('item_found')
	testSound:AttachTo(screen)
	
	testVoice = CreateVoiceEntity('testVoice')
	testVoice:AttachTo(screen)
	
	
	-- save base state
	self.music_value            = GM:GetMusicVolume()
	self.sound_value            = GM:GetSoundVolume()
	self.voice_value			= GM:GetVoiceVolume()
	self.custom_cursor_value    = GM:GetShowCustomCursor()
end
-------------------------------------------------------------------------------

function l_OptionsScreen:CreateText(text, x, y, scale, alignment)
    alignment = alignment or (HGETEXT_MIDDLE + HGETEXT_CENTER)
    
    local text_entity = CreateTextEntity(MENU_FONT, text)
    text_entity:AttachTo(self.screen)
	text_entity:SetXY(x, y)
	text_entity:SetZ(120)
	text_entity:SetScale(scale)
    text_entity:SetTracking(-2)
	text_entity:SetSpacing(0.9)
	text_entity:SetTextAlignment(alignment)
	
	return text_entity
end
-------------------------------------------------------------------------------

function CreateOptionsScreen(hideScreen)
    hideScreen = hideScreen or false

    local screen = CreateScriptedScreen('data/Scripts/screens/OptionsScreen.lua', 'l_OptionsScreen')
    PopUpScreensMgr:Push(screen, hideScreen, nil) 
end
--------------------------------------------------------------------------------

function l_OptionsScreen:OnCancelButtonClick()
    PopUpScreensMgr:Pop()
    
    GM:SetMusicVolume( self.music_value )
    GM:SetSoundVolume( self.sound_value )
    GM:SetVoiceVolume( self.voice_value )
    GM:SetShowCustomCursor( self.custom_cursor_value )
end
--------------------------------------------------------------------------------

function l_OptionsScreen:OnOkButtonClick()
    PopUpScreensMgr:Pop()
    
    -- set "fullscreen"
    if fullscreenCheckbox:GetState() == 1 then
        GM:SetFullScreen(true)
    else
        GM:SetFullScreen(false)
    end
    
    -- set "widescreen"
    if widescreenCheckbox and widescreenCheckbox:GetState() == 1 then
        GM:SetWidescreen(true)
    else
        GM:SetWidescreen(false)
    end
end
--------------------------------------------------------------------------------

function l_OptionsScreen:OnModeButtonClick()
	CreatePlayingModeScreen()
end
--------------------------------------------------------------------------------

function l_OptionsScreen:SetMusicVolume()
    local vol = self.musicSlider:GetValue()
    if vol then
        GM:SetMusicVolume(vol)
    end
end
--------------------------------------------------------------------------------

function l_OptionsScreen:SetSoundVolume(slider)
    local vol = self.soundSlider:GetValue()
    if vol then
        GM:SetSoundVolume(vol)
          if not self.soundSlider:IsDragging() then
               testSound:StopSound()
          end
        testSound:PlaySound(false)
    end
end
--------------------------------------------------------------------------------

function l_OptionsScreen:SetVoiceVolume()
    local vol = self.voiceSlider:GetValue()

    if vol then
        GM:SetVoiceVolume(vol)
          if not self.voiceSlider:IsDragging() then
               testVoice:StopSound()
          end
        testVoice:PlaySound(false)
    end
end
--------------------------------------------------------------------------------

function l_OptionsScreen:ToggleCustomCursorVisible()
    local state = customCursorCheckbox:GetState()
    GM:SetShowCustomCursor(state == 1)
end
--------------------------------------------------------------------------------

function l_OptionsScreen:SetFullscreen()
end
--------------------------------------------------------------------------------

function l_OptionsScreen:SetWidescreen()
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_OptionsScreen:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/GUI/Options/OptionsScreenBackground.png',0,0,498,641,1,0)
	anim:SetHotSpot(249,320)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('OptionsScreenBackground',anim,512,384,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Buttons/bttn_ok.png',0,0,162,104,3,0)
	anim:SetHotSpot(81,52)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_ok',anim,422,673,120,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Buttons/bttn_cancel.png',0,0,162,104,3,0)
	anim:SetHotSpot(81,52)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_cancel',anim,592,673,120,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/ChangeMode.png',0,0,290,58,3,0)
	anim:SetHotSpot(145,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_mode',anim,512,589,120,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/CheckboxBack.png',0,0,60,60,1,0)
	anim:SetHotSpot(30,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('CheckboxBack_1',anim,324,524,101,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/CheckboxBack.png',0,0,60,60,1,0)
	anim:SetHotSpot(30,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('CheckboxBack_2',anim,324,458,101,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/CheckboxBack.png',0,0,60,60,1,0)
	anim:SetHotSpot(30,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('CheckboxBack_3',anim,537,458,101,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/SliderBack.png',0,0,378,28,1,0)
	anim:SetHotSpot(189,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('SliderBack_1',anim,513,190,101,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/SliderBack.png',0,0,378,28,1,0)
	anim:SetHotSpot(189,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('SliderBack_2',anim,513,285,101,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Options/SliderBack.png',0,0,378,28,1,0)
	anim:SetHotSpot(189,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('SliderBack_3',anim,513,380,101,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
