bool FMatineePerformanceCaptureCommand::Update()
{
	//for every matinee actor in the level
	for (TObjectIterator<AMatineeActor> It; It; ++It)
	{
		AMatineeActor* MatineeActor = *It;
		FString MatineeFOOName = MatineeActor->GetName();
		if (MatineeActor->GetName().Equals(MatineeName,ESearchCase::IgnoreCase))
		{	


			//add latent action to execute this matinee
			ADD_LATENT_AUTOMATION_COMMAND(FPlayMatineeLatentCommand(MatineeActor));

			//Run the Stat FPS Chart command
			ADD_LATENT_AUTOMATION_COMMAND(FExecWorldStringLatentCommand(TEXT("StartFPSChart")));

			//add action to wait until matinee is complete
			ADD_LATENT_AUTOMATION_COMMAND(FWaitForMatineeToCompleteLatentCommand(MatineeActor));

			//Stop the Stat FPS Chart command
			ADD_LATENT_AUTOMATION_COMMAND(FExecWorldStringLatentCommand(TEXT("StopFPSChart")));
		}
		else
		{
			UE_LOG(LogEngineAutomationLatentCommand, Log, TEXT("'%s' is not the matinee name that is being searched for."), *MatineeActor->GetName())
		}
	}

	return true;

}
void ANetworkController::ReceivedStartMatinee(msgpack_object* Data) {
	FString name = Unpack<FString>(Data);

	for (TActorIterator<AMatineeActor> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		AMatineeActor* Matinee = *ObjIt;
		if (Matinee->GetName() == name) {
			Matinee->Play();
			break;
		}
	}
}
FText FCinematicOptionsCustomization::GetCurrentMatineeName() const
{
	FText SettingName;
	Matinee->GetValueAsDisplayText(SettingName);
	for (TObjectIterator<AMatineeActor> It; It; ++It)
	{
		AMatineeActor* MatineeActor = *It;
		if (SettingName.EqualTo(FText::FromString(MatineeActor->GetName())))
		{
			return SettingName;
		}
	}

	Matinee->SetValueFromFormattedString(FString("None"));
	return FText::FromString("None");
}
TSharedRef<SWidget> FCinematicOptionsCustomization::OnGetMatineeList() const
{
	FMenuBuilder MenuBuilder(true, NULL);

	FUIAction ItemAction(FExecuteAction::CreateSP(this, &FCinematicOptionsCustomization::OnSettingMatineeChange, FString("None")));
	MenuBuilder.AddMenuEntry(FText::FromString(TEXT("None")), TAttribute<FText>(), FSlateIcon(), ItemAction);

	for (TObjectIterator<AMatineeActor> It; It; ++It)
	{
		AMatineeActor* MatineeActor = *It;
		FUIAction Action(FExecuteAction::CreateSP(this, &FCinematicOptionsCustomization::OnSettingMatineeChange, MatineeActor->GetName()));
		MenuBuilder.AddMenuEntry(FText::FromString(MatineeActor->GetName()), TAttribute<FText>(), FSlateIcon(), Action);
	}

	return MenuBuilder.MakeWidget();
}
bool FEnqueuePerformanceCaptureCommands::Update()
{
	//for every matinee actor in the level
	for (TObjectIterator<AMatineeActor> It; It; ++It)
	{
		AMatineeActor* MatineeActor = *It;
		if(MatineeActor && MatineeActor->GetName().Contains(TEXT("Automation")) )
		{
			//add latent action to execute this matinee
			ADD_LATENT_AUTOMATION_COMMAND(FPlayMatineeLatentCommand(MatineeActor));

			//add action to wait until matinee is complete
			ADD_LATENT_AUTOMATION_COMMAND(FWaitForMatineeToCompleteLatentCommand(MatineeActor));
		}
	}

	return true;
}
void FLevelEditorContextMenuImpl::FillMatineeSelectActorMenu( FMenuBuilder& MenuBuilder )
{
	MenuBuilder.BeginSection("SelectMatinee", LOCTEXT("SelectMatineeHeading", "Matinee") );
	{
		// show list of Matinee Actors that controls this actor
		// this is ugly but we don't have good way of knowing which Matinee actor controls me
		// in the future this can be cached to TMap somewhere and use that list
		// for now we show only when 1 actor is selected
		if ( SelectionInfo.SharedLevel && SelectionInfo.NumSelected == 1 )
		{
			TArray<AMatineeActor*> MatineeActors;	
			// first collect all matinee actors
			for ( AActor* Actor : SelectionInfo.SharedLevel->Actors )
			{
				AMatineeActor * CurActor = Cast<AMatineeActor>(Actor);
				if ( CurActor )
				{
					MatineeActors.Add(CurActor);
				}
			}

			if ( MatineeActors.Num() > 0 )
			{
				FSelectionIterator ActorIter( GEditor->GetSelectedActorIterator() );
				AActor* SelectedActor = Cast<AActor>(*ActorIter);

				// now delete the matinee actors that don't control currently selected actor
				for (int32 MatineeActorIter=0; MatineeActorIter<MatineeActors.Num(); ++MatineeActorIter)
				{
					AMatineeActor * CurMatineeActor = MatineeActors[MatineeActorIter];
					TArray<AActor *> CutMatineeControlledActors;
					CurMatineeActor->GetControlledActors(CutMatineeControlledActors);
					bool bIsMatineeControlled=false;
					for ( AActor* ControlledActor : CutMatineeControlledActors )
					{
						if (ControlledActor == SelectedActor)
						{
							bIsMatineeControlled = true;
						}
					}

					// if not, remove it
					if (!bIsMatineeControlled)
					{
						MatineeActors.RemoveAt(MatineeActorIter);
						--MatineeActorIter;
					}
				}

				// if some matinee controls this, add to menu for direct selection
				if ( MatineeActors.Num() > 0 )
				{
					for (int32 MatineeActorIter=0; MatineeActorIter<MatineeActors.Num(); ++MatineeActorIter)
					{
						AMatineeActor * CurMatineeActor = MatineeActors[MatineeActorIter];
						const FText Text = FText::Format( LOCTEXT("SelectMatineeActor", "Select {0}"), FText::FromString( CurMatineeActor->GetName() ) );

						FUIAction CurMatineeActorAction( FExecuteAction::CreateStatic( &FLevelEditorActionCallbacks::OnSelectMatineeActor, CurMatineeActor ) );
						MenuBuilder.AddMenuEntry( Text, Text, FSlateIcon(), CurMatineeActorAction );

						// if matinee is opened, and if that is CurMatineeActor, show option to go to group
						if( GLevelEditorModeTools().IsModeActive( FBuiltinEditorModes::EM_InterpEdit ) )
						{
							const FEdModeInterpEdit* InterpEditMode = (const FEdModeInterpEdit*)GLevelEditorModeTools().GetActiveMode( FBuiltinEditorModes::EM_InterpEdit );

							if ( InterpEditMode && InterpEditMode->MatineeActor == CurMatineeActor )
							{
								FUIAction SelectedActorAction( FExecuteAction::CreateStatic( &FLevelEditorActionCallbacks::OnSelectMatineeGroup, SelectedActor ) );
								MenuBuilder.AddMenuEntry( LOCTEXT("SelectMatineeGroupForActorMenuTitle", "Select Matinee Group For This Actor"), LOCTEXT("SelectMatineeGroupForActorMenuTooltip", "Selects matinee group controlling this actor"), FSlateIcon(), SelectedActorAction );
							}
						}
					}
				}
			}
		}

		// if this class is Matinee Actor, add option to allow select all controlled actors
		if ( SelectionInfo.bHaveMatinee )
		{
			MenuBuilder.AddMenuEntry( FLevelEditorCommands::Get().SelectAllActorsControlledByMatinee );	
		}
	}
	MenuBuilder.EndSection();
}