function OnSceneLoad(screen)
    LoadData(screen)
    
    g_screen = screen
    g_timer = screen:GetTimer()
    
    Columns = {}
    COLUMN_AMOUNT = 8
    ScrWidth = 1024
    Delta = 15.0
    sound_x, sound_y = false, false
    DragObject = nil
    
    for i = 1, COLUMN_AMOUNT do
        Columns[i] = g_screen:GetObject("Column_"..i)
        Columns[i].RX = math.random(0, 2 * ScrWidth)
        --Columns[i].RX = math.random(0, 40)
        Columns[i]:GetAnimation():SetTextureRect(0, 0, ScrWidth, Columns[i]:GetAnimation():GetHeight())
        Columns[i]:GetAnimation():ChangeTextureRect(Columns[i].RX, 0)
    end
    
    screen:IgnoreOnTestPoint('light')
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function OnHiddenComics()
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    if DragObject then
        StopDrag()
    end
    
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local is_wrong_click = true

    if string.find(obj_name, "Column_") then
        local num = tonumber(string.sub(obj_name, 8))
        DragObject = Columns[num]
        DragObject.MX = GetMousePos().x
        sound_x, sound_y = GetMousePos().x, GetMousePos().y
    end
    
    return is_wrong_click
end

function OnObjectMouseUp(object)
    if DragObject then
        StopDrag()
    end
end

function StopDrag()
    DragObject = nil
    if IsComplete() then
        local sum = 0
        for i = 1, COLUMN_AMOUNT do
            g_screen:IgnoreOnTestPoint('Column_'..i)
            sum = sum + Columns[i].RX
        end
        local mid = sum / COLUMN_AMOUNT
        for i = 1, COLUMN_AMOUNT do
            g_screen:GetObject('Column_'..i):GetAnimation():ChangeTextureRect(mid - Columns[i].RX, 0)
        end
        MiniGameCompleted(0.25)
    end
end

function IsComplete()
    local sum = 0
    for i = 1, COLUMN_AMOUNT do
        sum = sum + Columns[i].RX
    end
    
    local mid = sum / COLUMN_AMOUNT
    for i = 1, COLUMN_AMOUNT do
        if math.abs(mid - Columns[i].RX) > Delta then
            return false
        end
    end
    
    return true
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound()
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnSkipMiniGame()
    if game_state.minigame_complete then
        return
    end
    
    for i = 1, COLUMN_AMOUNT do
        g_screen:IgnoreOnTestPoint('Column_'..i)
        g_screen:GetObject('Column_'..i):GetAnimation():SetTextureRect(0, 0, 2 * ScrWidth, Columns[i]:GetAnimation():GetHeight())
        --g_screen:GetObject('Column_'..i):SetX(g_screen:GetObject('Column_'..i):GetX() - Columns[i].RX)
    end
    for i = 1, COLUMN_AMOUNT do
        g_screen:GetObject('Column_'..i):MoveTo(ScrWidth, g_screen:GetObject('Column_'..i):GetY(), 0.5)            
    end
    MiniGameCompleted(0.5)
end

function OnButtonPress(title)
    DefaultMinigameOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    if DragObject then
        local mouse_x = GetMousePos().x
        local delta = DragObject.MX - mouse_x
        DragObject.RX = DragObject.RX + delta
        
        if sound_x ~= false and sound_y ~= false then
            if sound_x ~= GetMousePos().x or sound_y ~= GetMousePos().y then
                TimerDoString('PlaySound("28_circle_rotate")', 0.08, g_timer)
                sound_x, sound_y = false, false
            end
        end
        
        --[[while DragObject.RX < 0 do
    	    DragObject.RX = DragObject.RX + 2 * ScrWidth
    	end
    	while DragObject.RX >= 2 * ScrWidth do
            DragObject.RX = DragObject.RX - 2 * ScrWidth
        end]]
        
        local round = math.floor(DragObject.RX / (2 * ScrWidth));
	    DragObject.RX = DragObject.RX - round * (2 * ScrWidth);
	                    
        DragObject:GetAnimation():ChangeTextureRect(delta, 0)
        DragObject.MX = mouse_x
    end
end

function OnSetTool(tool)
end

function OnMinigameCompleted()
end

function OnLostFocus()
	DefaultMinigameOnLostFocus()
end

function OnGainFocus()
	DefaultMinigameOnGainFocus()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_COLUMN')
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_1.jpg',0,0,2048,90,1,0)
	anim:SetHotSpot(1024,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_1',anim,1024,45,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_2.jpg',0,0,2048,126,1,0)
	anim:SetHotSpot(1024,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_2',anim,1024,153,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_3.jpg',0,0,2048,87,1,0)
	anim:SetHotSpot(1024,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_3',anim,1024,259,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_4.jpg',0,0,2048,120,1,0)
	anim:SetHotSpot(1024,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_4',anim,1024,363,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_5.jpg',0,0,2048,114,1,0)
	anim:SetHotSpot(1024,57)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_5',anim,1024,480,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_6.jpg',0,0,2048,66,1,0)
	anim:SetHotSpot(1024,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_6',anim,1024,570,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_7.jpg',0,0,2048,90,1,0)
	anim:SetHotSpot(1024,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_7',anim,1024,648,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/Column_8.jpg',0,0,2048,75,1,0)
	anim:SetHotSpot(1024,37)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Column_8',anim,1024,730,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/7_column/light.png',0,0,1024,1,1,0)
	anim:SetHotSpot(512,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light',anim,512,0,200,0,1,768)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
