// Script to use pad Leds to show transport and section status 
// Arguments:  
//		none
//
// Author: LoopToGo
//
// Description  : 	Will turn on different leds based on LoopToGoState
//						Row 1 (bottom)
//							Column 1 (Blue):	Rewind All
//							Column 2 (White):	Go to last start tick
//							Column 3 (Yellow):	Stop
//							Column 4 (Green):	Play/Pause
//							Column 5 (Red):		Record/Pause
//							Column 6 ():	
//							Column 7 (Green):	Load selected file from playlist
//							Column 8 (Yellow):	Select next file in playlist
//						Row 2
//							Column 1 (Blue):	Go to start of song					
//							Column 2 (Purple):	Previous start/end of loop					
//							Column 3 (Purple):	Next track					
//							Column 4 (Purple):	Next start/end of loop					
//							Column 5 (Blue):	Go to end of song
//							Column 6 ():	
//							Column 7 ():	
//							Column 8 (Yellow):	Select previous file in playlist					
//						Row 3
//							Column 1 (Yellow):	Load Preset A	
//							Column 2 (Yellow):	Load Preset B	
//							Column 3 (Purple):	Previous track	
//							Column 4 (Yellow):	Load Preset C	
//							Column 5 (Yellow):	Load Preset D	
//							Column 6 ():	
//							Column 7 (Blue):	Toggle Chain Editor/Looper	
//							Column 8 (Purple):	Show/Hide mixing console
//						Row 4
//							Column 1 ():	
//							Column 2 (Pink):	Set playhead to current cursor
//							Column 3 ():	
//							Column 2 (Pink):	Set current cursor to playhead
//							Column 5 ():	
//							Column 6 ():	
//							Column 7 (Purple):	Toggle zoom horizontal (F2)
//							Column 8 (Purple):	Toggle zoom vertical (T)
//						Row 6
//							Column 1 ():	
//							Column 2 ():	
//							Column 3 ():	
//							Column 4 ():	
//							Column 5 ():	
//							Column 6 ():	
//							Column 7 (Blue):	Toggle Song structure view
//							Column 8 (Purple):	Toggle Plugin/Chain/Song/Playlist Browser
//						Row 7 and 8
//							Section leds. Colour will change when a section name is different from the previous one
//							Current section will flash and next jump section will pulse	
//							Some section have predefined color: Intro, Verse, Chorus, etc.  			

alias aDeviceOut LaunchPadMini 

loadScript ..\aliases.txt

setScriptName "Transport Led"
setScriptStopMode 1

loadScript ..\libs\Novation\novationLaunchPadMiniUtilities.ltgl


while 1

	callFunction updateTransportLed
	waitDelta .2

endWhile

function updateTransportLed


	variable cond1 $isPlaying = $gLastIsPlaying
	variable cond2 $isRecording = $gLastIsRecording
	variable cond3 $isPaused = $gLastIsPaused
	variable cond4 $nbSections = $gLastNbSections
	variable cond5 $currentSection = $gLastCurrentSection
	variable cond6 $nextSection = $gLastNextSection
	variable cond7 $gInitTransportLed = false
	
	if $gInitTransportLed
		callFunction clearAllColorNames
	endIf
	
	// print $cond1  $cond2  $cond3  $cond4 $cond5  $cond6  $cond7
		
	ifNot $cond1 * $cond2 * $cond3 * $cond7   // Transport buttons that can be pulsing (Stop, Play Record)

		// Row 1 (bottom)
		
		if $isPaused
			callFunction setPadPulsing aDeviceOut 3 aColorPaletteYellow	// Stop pulsing
		else	
			callFunction setPadColor aDeviceOut 3 aColorPaletteYellow		// Stop
		endIf
		
		if $isPlaying
			callFunction setPadPulsing aDeviceOut 4 aColorPaletteGreen		// Play/Pause pulsing
		else
			callFunction setPadColor aDeviceOut 4 aColorPaletteGreen		// Play/Pause
		endIf	
		
		if $isRecording
			callFunction setPadPulsing aDeviceOut 5 aColorPaletteRed		// Record/Pause	pulsing
		else	
			callFunction setPadColor aDeviceOut 5 aColorPaletteRed			// Record/Pause	
		endIf	
	endIf
	
	if $gInitTransportLed	// Buttons that are never pulsing (Rewind all, rewind, etc.)
	
		// Row 1 (bottom)
		callFunction setPadColor aDeviceOut 1 aColorPaletteBlue			// Rewind all
		callFunction setPadColor aDeviceOut 2 aColorPaletteWhite		// Go to last start tick
		callFunction setPadColor aDeviceOut 7 aColorPaletteGreen		// Load selected file from playlist
		callFunction setPadColor aDeviceOut 8 aColorPaletteYellow		// Select next file in playlist
		
		

		// Row 2
		callFunction setPadColor aDeviceOut 10 aColorPaletteBlue		// Beginning of song
		callFunction setPadColor aDeviceOut 11 aColorPalettePurple		// Previous start or end of loop
		callFunction setPadColor aDeviceOut 12 aColorPalettePurple		// Next track
		callFunction setPadColor aDeviceOut 13 aColorPalettePurple		// Next start or end of loop
		callFunction setPadColor aDeviceOut 14 aColorPaletteBlue		// End of song
		callFunction setPadColor aDeviceOut 17 aColorPaletteYellow		// Select previous file in playlist

		// Row 3
		callFunction setPadColor aDeviceOut 19 aColorPaletteYellow		// Load Preset A
		callFunction setPadColor aDeviceOut 20 aColorPaletteYellow		// Load Preset A
		callFunction setPadColor aDeviceOut 21 aColorPalettePurple		// Previous track
		callFunction setPadColor aDeviceOut 22 aColorPaletteYellow		// Load Preset A
		callFunction setPadColor aDeviceOut 23 aColorPaletteYellow		// Load Preset A
		
		
		// Row 3
		callFunction setPadColor aDeviceOut 25 aColorPaletteBlue		// Toggle structure view
		callFunction setPadColor aDeviceOut 26 aColorPalettePurple		// Toggle mixing console view

		// Row 4
		callFunction setPadColor aDeviceOut 29 aColorPalettePink		// Set playhead to current cursor
		callFunction setPadColor aDeviceOut 31 aColorPalettePink		// Set playhead to current cursor
		callFunction setPadColor aDeviceOut 34 aColorPalettePurple		// Toggle zoom horizontal
		callFunction setPadColor aDeviceOut 35 aColorPalettePurple		// Toggle zoom vertical

		// Row 5
		callFunction setPadColor aDeviceOut 43 aColorPaletteBlue		// Toggle structure view
		callFunction setPadColor aDeviceOut 44 aColorPalettePurple		// Toggle Plugins/Chains/Songs/Playlits view

	endIf


	ifNot $cond4 * $cond5 * $cond6 * $cond7  // Sections buttons
	
		//print $gInitTransportLed  $cond4  $cond5 $cond6 : $cond4 * $cond5 * $cond6
	
		//print currentSection : $currentSection gLastCurrentSection : $gLastCurrentSection nextSection : $nextSection
		//print $currentSection $currentSectionName $currentSectionStart $currentSectionEnd
		
		for row 6 8 // Sections   Row 8 (sections 1-8) Row 7 (sections 9-16) Row 6 (sections 17-24)
		
			variable startSection 65 - $row*8
			variable endSection $startSection + 7
			variable offStartSection $startSection
			variable offEndSection $endSection
					
			// Row 8 (63) Row 7 ()
			variable firstPad $row*9 - $startSection - 8
								
			if $endSection > $nbSections
				variable endSection $nbSections
			endIf	
			
			if $offStartSection < $endSection + 1
				variable offStartSection  $endSection + 1
			endIf
				
			for i $startSection $endSection
				
				variable sectionName sectionName & $i
				
				callFunction selectColor color $$sectionName
								
				if $i = $currentSection
					callFunction setPadFlashing aDeviceOut $firstPad + $i $color	0 // Current section is flashing!				
				else
					if $i = $nextSection
						callFunction setPadPulsing aDeviceOut $firstPad + $i $color // Next section is pulsing!
					else
						callFunction setPadColor aDeviceOut $firstPad + $i $color		// Normal section						
					endIf
				endIf

			endFor
		
			ifNot $cond4
				for i $offStartSection $offEndSection
					callFunction setPadColor aDeviceOut $firstPad + $i 0 // Turn off unused pad
				endFor
			endIf
		endFor
		
	endIf
	
	
	globalVariable gInitTransportLed false
	
	globalVariable gLastIsPlaying $isPlaying
	globalVariable gLastIsRecording $isRecording 
	globalVariable gLastIsPaused $isPaused 
	globalVariable gLastNbSections $nbSections 
	globalVariable gLastCurrentSection $currentSection 
	globalVariable gLastNextSection $nextSection 

	
endFunction	


function selectColor // color [out]  sectionName [In]

	// First we try to set the color based on known section name such as Verse, Chorus, etc.

	
	regexReplace firstWord $arg2 "^(\S+).*" "$1"
	
	//print sectionName : [ $arg3 ] [ $firstWord ]
	
	if $firstWord = Intro
		globalVariable gSectionColor aColorPaletteGreen
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf	
	
	if $firstWord = Verse
		globalVariable gSectionColor aColorPalettePurple
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf	

	if $firstWord = Chorus
		globalVariable gSectionColor aColorPaletteOrange
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf	

	if $firstWord = Bridge
		globalVariable gSectionColor aColorPaletteYellow
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf	

	if $firstWord = Solo
		globalVariable gSectionColor aColorPaletteRed
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf

	
	if $firstWord = Outro
		globalVariable gSectionColor aColorPaletteDarkGreen
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf

	

	if $firstWord = End
		globalVariable gSectionColor aColorPaletteGreen
		parentVariable $arg1 $gSectionColor
		exitFunction
	endIf	

	// This section is for unrecognized section name
	
	
	variable colorName $firstWord & SectionColor  
	
	ifNot $$colorName 
	
		globalVariable gColorNumberForCustomSectionName $gColorNumberForCustomSectionName + 1
		
		if $gColorNumberForCustomSectionName = 1
			globalVariable $colorName aColorPaletteWhite
		else			
			if $gColorNumberForCustomSectionName = 2
				globalVariable $colorName aColorPaletteLightPink			
			else			
				if $gColorNumberForCustomSectionName = 3
					globalVariable $colorName aColorPaletteBeige
				else	
					if $gColorNumberForCustomSectionName = 4
						globalVariable $colorName aColorPaletteLightGreen
					else	
						globalVariable $colorName aColorPaletteLightOrange
						globalVariable gColorNumberForCustomSectionName 0  // We will start at 1 next time!
					endIf
				endIf
			endIf
		endIf		
	endIf
		
	//print $colorName has been defined to $$colorName
	
	globalVariable gSectionColor $$colorName
	parentVariable $arg1 $gSectionColor

endFunction

function clearAllColorNames

	for i 1 $nbSections
		variable sectionName sectionName & $i
 
		regexReplace firstWord $$sectionName "^(\S+).*" "$1"
		
		variable colorName $firstWord & SectionColor  
		
		if $$colorName		
			//print cleaning $$colorName	
			globalVariable $colorName 0
		endIf
	endFor	

	globalVariable gColorNumberForCustomSectionName 0
		
endFunction

