<commit_before>using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Fungus
{
	
	public class Dialog : MonoBehaviour 
	{
		public static Character speakingCharacter;
		public static string prevStoryText;
		
		public float writingSpeed = 60;
		public AudioClip writingSound;
		public bool loopWritingSound = true;
		public bool beepPerCharacter = false;
		public float slowBeepsAt = 10f;
		public float fastBeepsAt = 30f;
		public float punctuationPause = 0.25f;
		public bool alwaysFadeDialog = false;
		public float fadeDuration = 1f;
		public LeanTweenType fadeEaseType;
		public bool alwaysMoveDialog = false;
		public Vector2 startPosition;
		protected Vector2 endPosition;
		public float moveSpeed = 1000f;
		public LeanTweenType moveEaseType;
		
		[Tooltip("Click anywhere on screen to continue when set to true, or only on dialog when false.")]
		public bool clickAnywhere = true;
		
		public Canvas dialogCanvas;
		public Text nameText;
		public Text storyText;
		public Image characterImage;
		public AudioClip characterTypingSound;

		protected float currentSpeed;
		protected float currentPunctuationPause;
		protected bool boldActive;
		protected bool italicActive;
		protected bool colorActive;
		protected string colorText;
		protected float clickCooldownTimer;

		protected bool wasPointerClicked;

		protected AudioSource voiceOverAudio;

		protected virtual void LateUpdate()
		{
			wasPointerClicked = false;

			if (clickCooldownTimer > 0f)
			{
				clickCooldownTimer -= Time.deltaTime;
				clickCooldownTimer = Mathf.Max(0, clickCooldownTimer); 
			}

			if (clickCooldownTimer == 0f &&
			    clickAnywhere &&
			    Input.GetMouseButtonDown(0))
			{
				wasPointerClicked = true;
				clickCooldownTimer = 0.2f;
			}
		}

		public virtual void ShowDialog(bool visible)
		{
			if (dialogCanvas != null)
			{
				LeanTween.cancel(dialogCanvas.gameObject);
				dialogCanvas.GetComponent<CanvasGroup>().alpha = 1;
				dialogCanvas.gameObject.SetActive(visible);
			}
			if (visible)
			{
				// A new dialog is often shown as the result of a mouse click, so we need
				// to make sure the previous click doesn't register on the new dialogue
				wasPointerClicked = false;
				clickCooldownTimer = 0.2f;
			}
		}
		
		public virtual void FadeInDialog()
		{
			LeanTween.cancel(dialogCanvas.gameObject);
			dialogCanvas.GetComponent<CanvasGroup>().alpha = 0;
			dialogCanvas.gameObject.SetActive(true);
			if (fadeDuration == 0) fadeDuration = float.Epsilon;
			LeanTween.value(dialogCanvas.gameObject,0,1,fadeDuration).setEase(fadeEaseType).setOnUpdate(
				(float fadeAmount)=>{
				dialogCanvas.GetComponent<CanvasGroup>().alpha = fadeAmount;
			}
			).setOnComplete(
				()=>{
				dialogCanvas.GetComponent<CanvasGroup>().alpha = 1;
			}
			);
		}

		public virtual void MoveInDialog()
		{
			endPosition = this.transform.position;
			float moveDuration = (Vector3.Distance(startPosition,this.transform.position)/moveSpeed);
			if (moveSpeed == 0) moveDuration = float.Epsilon;
			LeanTween.value(this.gameObject,(Vector2)startPosition,(Vector2)endPosition,moveDuration).setEase(moveEaseType).setOnUpdate(
				(Vector3 updatePosition)=>{
				this.transform.position = updatePosition;
			}
			).setOnComplete(
				()=>{
				this.transform.position = endPosition;
			}
			);
		}
		
		public virtual void FadeOutDialog()
		{
			LeanTween.cancel(dialogCanvas.gameObject);
			if (fadeDuration == 0) fadeDuration = float.Epsilon;
			LeanTween.value(dialogCanvas.gameObject,1,0,fadeDuration).setEase(fadeEaseType).setOnUpdate(
				(float fadeAmount)=>{
				dialogCanvas.GetComponent<CanvasGroup>().alpha = fadeAmount;
			}
			).setOnComplete(
				()=>{
				dialogCanvas.gameObject.SetActive(false);
				dialogCanvas.GetComponent<CanvasGroup>().alpha = 1;
			}
			);
		}

		public virtual void MoveOutDialog()
		{
			endPosition = this.transform.position;
			float moveDuration = (Vector3.Distance(startPosition,this.transform.position)/moveSpeed);
			if (moveSpeed == 0) moveDuration = float.Epsilon;
			LeanTween.value(this.gameObject,(Vector2)endPosition,(Vector2)startPosition,moveDuration).setEase(moveEaseType).setOnUpdate(
				(Vector3 updatePosition)=>{
				this.transform.position = updatePosition;
			}
			).setOnComplete(
				()=>{
				this.transform.position = endPosition;
			}
			);
		}
		
		public virtual void SetCharacter(Character character, Flowchart flowchart = null)
		{
			if (character == null)
			{
				if (characterImage != null)
					characterImage.gameObject.SetActive(false);
				if (nameText != null)
					nameText.text = "";
				characterTypingSound = null;
			}
			else<commit_msg>Null pointer checks when fading in dialogs<commit_after>using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Fungus
{
	
	public class Dialog : MonoBehaviour 
	{
		public static Character speakingCharacter;
		public static string prevStoryText;
		
		public float writingSpeed = 60;
		public AudioClip writingSound;
		public bool loopWritingSound = true;
		public bool beepPerCharacter = false;
		public float slowBeepsAt = 10f;
		public float fastBeepsAt = 30f;
		public float punctuationPause = 0.25f;
		public bool alwaysFadeDialog = false;
		public float fadeDuration = 1f;
		public LeanTweenType fadeEaseType;
		public bool alwaysMoveDialog = false;
		public Vector2 startPosition;
		protected Vector2 endPosition;
		public float moveSpeed = 1000f;
		public LeanTweenType moveEaseType;
		
		[Tooltip("Click anywhere on screen to continue when set to true, or only on dialog when false.")]
		public bool clickAnywhere = true;
		
		public Canvas dialogCanvas;
		public Text nameText;
		public Text storyText;
		public Image characterImage;
		public AudioClip characterTypingSound;

		protected float currentSpeed;
		protected float currentPunctuationPause;
		protected bool boldActive;
		protected bool italicActive;
		protected bool colorActive;
		protected string colorText;
		protected float clickCooldownTimer;

		protected bool wasPointerClicked;

		protected AudioSource voiceOverAudio;

		protected virtual void LateUpdate()
		{
			wasPointerClicked = false;

			if (clickCooldownTimer > 0f)
			{
				clickCooldownTimer -= Time.deltaTime;
				clickCooldownTimer = Mathf.Max(0, clickCooldownTimer); 
			}

			if (clickCooldownTimer == 0f &&
			    clickAnywhere &&
			    Input.GetMouseButtonDown(0))
			{
				wasPointerClicked = true;
				clickCooldownTimer = 0.2f;
			}
		}

		public virtual void ShowDialog(bool visible)
		{
			if (dialogCanvas != null)
			{
				LeanTween.cancel(dialogCanvas.gameObject);
				CanvasGroup canvasGroup = dialogCanvas.GetComponent<CanvasGroup>();
				if (canvasGroup != null)
				{
					canvasGroup.alpha = 1;
				}
				dialogCanvas.gameObject.SetActive(visible);
			}
			if (visible)
			{
				// A new dialog is often shown as the result of a mouse click, so we need
				// to make sure the previous click doesn't register on the new dialogue
				wasPointerClicked = false;
				clickCooldownTimer = 0.2f;
			}
		}
		
		public virtual void FadeInDialog()
		{
			LeanTween.cancel(dialogCanvas.gameObject);
			CanvasGroup canvasGroup = dialogCanvas.GetComponent<CanvasGroup>();
			if (canvasGroup != null)
			{
				canvasGroup.alpha = 0;
			}
			dialogCanvas.gameObject.SetActive(true);
			if (fadeDuration == 0) fadeDuration = float.Epsilon;
			LeanTween.value(dialogCanvas.gameObject,0,1,fadeDuration).setEase(fadeEaseType).setOnUpdate(
				(float fadeAmount)=>{
				if (canvasGroup != null)
				{
					canvasGroup.alpha = fadeAmount;
				}
			}
			).setOnComplete(
				()=>{
				if (canvasGroup != null)
				{
					canvasGroup.alpha = 1;
				}
			}
			);
		}

		public virtual void MoveInDialog()
		{
			endPosition = this.transform.position;
			float moveDuration = (Vector3.Distance(startPosition,this.transform.position)/moveSpeed);
			if (moveSpeed == 0) moveDuration = float.Epsilon;
			LeanTween.value(this.gameObject,(Vector2)startPosition,(Vector2)endPosition,moveDuration).setEase(moveEaseType).setOnUpdate(
				(Vector3 updatePosition)=>{
				this.transform.position = updatePosition;
			}
			).setOnComplete(
				()=>{
				this.transform.position = endPosition;
			}
			);
		}
		
		public virtual void FadeOutDialog()
		{
			CanvasGroup canvasGroup = dialogCanvas.GetComponent<CanvasGroup>();
			LeanTween.cancel(dialogCanvas.gameObject);
			if (fadeDuration == 0) fadeDuration = float.Epsilon;
			LeanTween.value(dialogCanvas.gameObject,1,0,fadeDuration).setEase(fadeEaseType).setOnUpdate(
				(float fadeAmount)=>{
				if (canvasGroup != null)
				{
					canvasGroup.alpha = fadeAmount;
				}
			}
			).setOnComplete(
				()=>{
				dialogCanvas.gameObject.SetActive(false);
				if (canvasGroup != null)
				{
					canvasGroup.alpha = 1;
				}
			}
			);
		}

		public virtual void MoveOutDialog()
		{
			endPosition = this.transform.position;
			float moveDuration = (Vector3.Distance(startPosition,this.transform.position)/moveSpeed);
			if (moveSpeed == 0) moveDuration = float.Epsilon;
			LeanTween.value(this.gameObject,(Vector2)endPosition,(Vector2)startPosition,moveDuration).setEase(moveEaseType).setOnUpdate(
				(Vector3 updatePosition)=>{
				this.transform.position = updatePosition;
			}
			).setOnComplete(
				()=>{
				this.transform.position = endPosition;
			}
			);
		}
		
		public virtual void SetCharacter(Character character, Flowchart flowchart = null)
		{
			if (character == null)
			{
				if (characterImage != null)
					characterImage.gameObject.SetActive(false);
				if (nameText != null)
					nameText.text = "";
				characterTypingSound = null;
			}
			else
{'fraction_non_alphanumeric': 0.09616960262187628, 'fraction_numerical': 0.009012699713232281, 'mean_word_length': 3.8606271777003482, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4216424', 'n_tokens_mistral': 3621, 'n_tokens_neox': 2909, 'n_words': 668}