------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Ian Eller
-- Updated: 03/11/2009, ice

dofile( "../../Assets/Scripts/key_name_mappings.lua" )

--------------------------------------------------------------------------------
-- Vars
--------------------------------------------------------------------------------

local GAMEMODENONE   = Global:GameModeNONE()
local GAMEMODECAMERA = Global:GameModeCAMERA()
local GAMEMODEFULL   = Global:GameModeFULL()

local myData = {}
myData.newXML  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 755, 0, 0, 0, 0 )
myData.opnXML  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 740, 0, 0, 0, 0 )
myData.setTop  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 710, 0, 0, 0, 0 )
myData.setBot  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 695, 0, 0, 0, 0 )
myData.refrsh  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 680, 0, 0, 0, 0 )
myData.yScale  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 650, 0, 0, 0, 0 )
myData.yarrwL  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 615, 0, 0, 0, 0 )
myData.yarrwR  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 600, 0, 0, 0, 0 )
myData.materl  = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   0, 570, 0, 0, 0, 0 )
myData.yScaleB = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   100, 650, 0, 0, 0, 0 )
myData.yarrwLB = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   100, 615, 0, 0, 0, 0 )
myData.yarrwRB = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   100, 600, 0, 0, 0, 0 )
myData.nameTxt = Global:CreateEntityAt( "../../Assets/XML/entities/editor_Button.xml",   100, 755, 0, 0, 0, 0 )
myData.name = "Default"


local materialTable = { [ 0 ] = { text = "zone1grass" --, material = ?
                                }, 
                        [ 1 ] = { text = "zone1bare"  --, material = ?
                                },  
                        [ 2 ] = { text = "zone2grass" --, material = ?
                                },  
                        [ 3 ] = { text = "zone2bare"  --, material = ?
                                },  
                        [ 4 ] = { text = "zone3grass" --, material = ?
                                },  
                        [ 5 ] = { text = "zone3bare"  --, material = ?
                                }
                      }

local NEW_XML      = myData.newXML:GetID()
local OPEN_XML     = myData.opnXML:GetID()
local SET_TOP      = myData.setTop:GetID()
local SET_BOTTOM   = myData.setBot:GetID()
local REFRESH      = myData.refrsh:GetID()
local Y_SCALE      = myData.yScale:GetID()
local Y_ARROWLEFT  = myData.yarrwL:GetID()
local Y_ARROWRIGHT = myData.yarrwR:GetID()
local MATERIAL     = myData.materl:GetID()
local Y_SCALEB     = myData.yScaleB:GetID()
local Y_ARROWLEFTB = myData.yarrwLB:GetID()
local Y_ARROWRIGHTB= myData.yarrwRB:GetID()

local yScale  = 30
local yScaleB = 30

--------------------------------------------------------------------------------
-- Main execution loop
--------------------------------------------------------------------------------

local visible         = false
local showDebug       = false
local checkForPicked  = 0
local materialCounter = 0
local MODE = -1
local enteringText = false
while( 1 ) 
do
  coroutine.yield( 1 )

  --[[
  if( Global:IsTriggered( key_names_to_values( "F4" ) ) ) 
  then
    visible = not visible
    
    -- Show only our debug lines
    Global:SetDebug( visible )
    Global:SetDebugLineLayer( visible, 1 )
    
    -- give us our cursor
    Global:ShowCursor( visible )
    Global:ContainCursor( not visible )
    
    -- toggle game mode
    Global:SetGameMode( not visible )
  
  end
  --]]
  
  
  ------------------------------------------------------------------------------
  -- Interested
  ------------------------------------------------------------------------------
  
  if( visible )
  then
  
  
    ----------------------------------------------------------------------------
    -- Are we naming a new island?
    ----------------------------------------------------------------------------
  
    if( enteringText == true ) 
    then
    
      --------------------------------------------------------------------------
      -- Created a new island; reading user input
      --------------------------------------------------------------------------
      
      if( Global:IsTriggered( key_names_to_values( "Enter" ) ) ) then
        enteringText = false
      
      else
        myData.name = Global:GetCurrentInput()
        
      end
  
  
    ----------------------------------------------------------------------------
    -- We're editing
    ----------------------------------------------------------------------------
  
    else
      
      
      --------------------------------------------------------------------------
      -- Switching camera
      --------------------------------------------------------------------------
      
      if( Global:IsMouseTriggered( mouse_names_to_values( "Mouse Middle" ) ) ) 
      then
        local isInGameMode = Global:IsInGameMode()
        if( isInGameMode ~= GAMEMODECAMERA ) then
          Global:SetGameMode( GAMEMODECAMERA )
          
        else
          Global:SetGameMode( GAMEMODENONE )
          
        end
      
        local showAndContain = ( isInGameMode == GAMEMODECAMERA )
        Global:ShowCursor   ( showAndContain  )
        Global:ContainCursor( not showAndContain )
        
      end
    
    
      --------------------------------------------------------------------------
      -- Picking
      --------------------------------------------------------------------------
      
      if( Global:IsMousePressed( mouse_names_to_values( "Mouse Left" ) ) ) 
      and ( Global:IsInGameMode() == GAMEMODENONE )
      and ( checkForPicked == 0 )
      then
          print( "Picking" )
          checkForPicked = 1
          Editor:PickEntity( Global:gMouseXY() )
        
      end
      
      -- So, this is sort of a hack, but if we check the currently picked entity
      -- right away, it sometimes comes up as the old one; no idea why
      if( checkForPicked > 0 )
      then
        if( checkForPicked == 5 )
        then
          checkForPicked = 0
          MODE = Editor:GetCurrentPicked()
          Editor:ResetCurrentPicked()
          
        else
          checkForPicked = checkForPicked + 1
          print( checkForPicked )
          
        end
      
      end
    
      
      if( checkForPicked == 0 )
      then
      
        ------------------------------------------------------------------------
        -- New
        ------------------------------------------------------------------------
      
        if( MODE == NEW_XML )
        then
          MODE = -1
      
          Editor:IslandNewXML()
          enteringText = true
          myData.name = ""
      
        ------------------------------------------------------------------------
        -- Opening
        ------------------------------------------------------------------------
        
        elseif( MODE == OPEN_XML )
        then
          MODE = -1
      
          Editor:IslandOpenXML()
      
        ------------------------------------------------------------------------
        -- Setting top of island
        ------------------------------------------------------------------------
        
        elseif( MODE == SET_TOP )
        then
          MODE = -1
      
          Editor:IslandSetTop()
      
        ------------------------------------------------------------------------
        -- Setting bottom of island
        ------------------------------------------------------------------------
        
        elseif( MODE == SET_BOTTOM )
        then
          MODE = -1
       
          Editor:IslandSetBottom()
         
        ------------------------------------------------------------------------
        -- Refreshing island
        ------------------------------------------------------------------------
        
        elseif( MODE == REFRESH )
        then     
          MODE = -1
      
          Editor:IslandRefresh( myData.name )
      
        ------------------------------------------------------------------------
        -- Decreasing Top Y scale
        ------------------------------------------------------------------------
        
        elseif( MODE == Y_ARROWLEFT )
        then
          MODE = -1
      
          yScale = yScale - 1
          Editor:IslandYScaleSeparate( yScale, yScaleB )
              
        ------------------------------------------------------------------------
        -- Increasing Top Y scale
        ------------------------------------------------------------------------
        
        elseif( MODE == Y_ARROWRIGHT )
        then
          MODE = -1
      
          yScale = yScale + 1
          Editor:IslandYScaleSeparate( yScale, yScaleB )
          
          
        ------------------------------------------------------------------------
        -- Decreasing Bottom Y scale
        ------------------------------------------------------------------------
        
        elseif( MODE == Y_ARROWLEFTB )
        then
          MODE = -1
      
          yScaleB = yScaleB - 1
          Editor:IslandYScaleSeparate( yScale, yScaleB )
             
        ------------------------------------------------------------------------
        -- Increasing Bottom Y scale
        ------------------------------------------------------------------------
        
        elseif( MODE == Y_ARROWRIGHTB )
        then
          MODE = -1
      
          yScaleB = yScaleB + 1
          Editor:IslandYScaleSeparate( yScale, yScaleB )
        --[[   --]]
        ------------------------------------------------------------------------
        -- Change material
        ------------------------------------------------------------------------
        
        elseif( MODE == MATERIAL )
        then
          MODE = -1
          
          materialCounter = ( materialCounter + 1 ) % ( table.maxn( materialTable ) + 1 )
          Editor:IslandMaterial( materialCounter )
        
        end
        
      end
      
    end
    
    
    --------------------------------------------------------------------------
    -- Set Text
    --------------------------------------------------------------------------
    
    myData.newXML:VIS_SetText( "New" );
    myData.opnXML:VIS_SetText( "Open" );
    myData.setTop:VIS_SetText( "Set Top" );
    myData.setBot:VIS_SetText( "Set Bottom" );
    myData.refrsh:VIS_SetText( "Refresh" );
    myData.yScale:VIS_SetText( "Y Top:\n" .. tostring( yScale ) );
    myData.yarrwL:VIS_SetText( "<--" );
    myData.yarrwR:VIS_SetText( "-->" );
    myData.yScaleB:VIS_SetText( "Y Bottom:\n" .. tostring( yScaleB ) );
    myData.yarrwLB:VIS_SetText( "<--" );
    myData.yarrwRB:VIS_SetText( "-->" );
    myData.materl:VIS_SetText( "Material:\n" .. materialTable[ materialCounter ].text );
    myData.nameTxt:VIS_SetText( "Name: " .. myData.name );
      
      
    ----------------------------------------------------------------------------
    -- Disinterested
    ----------------------------------------------------------------------------
  
  else
    myData.newXML:VIS_SetText( "" );
    myData.opnXML:VIS_SetText( "" );
    myData.setTop:VIS_SetText( "" );
    myData.setBot:VIS_SetText( "" );
    myData.refrsh:VIS_SetText( "" );
    myData.yScale:VIS_SetText( "" );
    myData.yarrwL:VIS_SetText( "" );
    myData.yarrwR:VIS_SetText( "" );
    myData.yScaleB:VIS_SetText( "" );
    myData.yarrwLB:VIS_SetText( "" );
    myData.yarrwRB:VIS_SetText( "" );
    myData.materl:VIS_SetText( "" );
    myData.nameTxt:VIS_SetText( "" );
    
  end

end
