<commit_before>﻿using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class GUI_Paper : NetTab
{
	public InputField textField;
	public ContentSizeFitter contentSizeFitter;

	public override void OnEnable()
	{
		base.OnEnable();
		StartCoroutine(WaitForProvider());
		textField.interactable = false;
	}

	IEnumerator WaitForProvider()
	{
		while (Provider == null)
		{
			yield return WaitFor.EndOfFrame;
		}
		RefreshText();
	}

	public override void RefreshTab()
	{
		RefreshText();
		base.RefreshTab();
	}

	public void RefreshText()
	{
		if (Provider != null)
		{
			textField.text = Provider.GetComponent<Paper>().PaperString;
		}
	}

	public void ClosePaper()
	{
		ControlTabs.CloseTab(Type, Provider);
	}

	public void OnEditStart()
	{
		if (!IsPenInHand())
		{
			textField.interactable = false;
			return;
		}
		else
		{
			textField.interactable = true;
			textField.ActivateInputField();
		}
		UIManager.IsInputFocus = true;
		CheckForInput();
	}

	private bool IsPenInHand()
	{
		var pen = UIManager.Hands.CurrentSlot.Item?.GetComponent<Pen>();
		if (pen == null)
		{
			pen = UIManager.Hands.OtherSlot.Item?.GetComponent<Pen>();
			if (pen == null)
			{
				//no pen
				return false;
			}
		}
		return true;
	}

	//Safety measure:
	private async void CheckForInput()
	{
		await Task.Delay(500);
		if (!textField.isFocused)
		{
			UIManager.IsInputFocus = false;
		}
	}

	//Request an edit from server:
	public void OnTextEditEnd()
	{
		PlayerManager.LocalPlayerScript.playerNetworkActions.CmdRequestPaperEdit(Provider.gameObject, textField.text);
		UIManager.IsInputFocus = false;
	}

	public void OnTextValueChange()
	{
		//Only way to refresh it to get it to do its job (unity bug):
		contentSizeFitter.enabled = false;
		contentSizeFitter.enabled = true;
		if (!textField.placeholder.enabled)
		{
			CheckLineLimit();
		}
	}

	private void CheckLineLimit()
	{
		Canvas.ForceUpdateCanvases();
		if (textField.textComponent.cachedTextGenerator.lineCount > 20)
		{
			var sub = textField.text.Substring(0, textField.text.Length - 1);
			textField.text = sub;
		}
	}<commit_msg>fixes bug with paper input<commit_after>﻿using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class GUI_Paper : NetTab
{
	public InputField textField;
	public ContentSizeFitter contentSizeFitter;

	public override void OnEnable()
	{
		base.OnEnable();
		StartCoroutine(WaitForProvider());
		textField.interactable = false;
	}

	IEnumerator WaitForProvider()
	{
		while (Provider == null)
		{
			yield return WaitFor.EndOfFrame;
		}
		RefreshText();
	}

	public override void RefreshTab()
	{
		RefreshText();
		base.RefreshTab();
	}

	public void RefreshText()
	{
		if (Provider != null)
		{
			textField.text = Provider.GetComponent<Paper>().PaperString;
		}
	}

	public void ClosePaper()
	{
		ControlTabs.CloseTab(Type, Provider);
	}

	public void OnEditStart()
	{
		if (!IsPenInHand())
		{
			textField.interactable = false;
			return;
		}
		else
		{
			textField.interactable = true;
			textField.ActivateInputField();
		}
		UIManager.IsInputFocus = true;
		UIManager.PreventChatInput = true;
		CheckForInput();
	}

	private bool IsPenInHand()
	{
		var pen = UIManager.Hands.CurrentSlot.Item?.GetComponent<Pen>();
		if (pen == null)
		{
			pen = UIManager.Hands.OtherSlot.Item?.GetComponent<Pen>();
			if (pen == null)
			{
				//no pen
				return false;
			}
		}
		return true;
	}

	//Safety measure:
	private async void CheckForInput()
	{
		await Task.Delay(500);
		if (!textField.isFocused)
		{
			UIManager.IsInputFocus = false;
			UIManager.PreventChatInput = false;
		}
	}

	//Request an edit from server:
	public void OnTextEditEnd()
	{
		PlayerManager.LocalPlayerScript.playerNetworkActions.CmdRequestPaperEdit(Provider.gameObject, textField.text);
		UIManager.IsInputFocus = false;
		UIManager.PreventChatInput = false;
	}

	public void OnTextValueChange()
	{
		//Only way to refresh it to get it to do its job (unity bug):
		contentSizeFitter.enabled = false;
		contentSizeFitter.enabled = true;
		if (!textField.placeholder.enabled)
		{
			CheckLineLimit();
		}
	}

	private void CheckLineLimit()
	{
		Canvas.ForceUpdateCanvases();
		if (textField.textComponent.cachedTextGenerator.lineCount > 20)
		{
			var sub = textField.text.Substring(0, textField.text.Length - 1);
			textField.text = sub;
		}
	}
{'fraction_non_alphanumeric': 0.11473243778903325, 'fraction_numerical': 0.0030830213609337152, 'mean_word_length': 3.8525641025641026, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '972815', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1558, 'n_words': 320}