public WriterFactory(MonoBehaviour mono, Action onPageWriten) { _animationControl = new TextAnimationControl(); var instant = new InstantWriter(mono, _animationControl); var charByChar = new CharByCharWriter(mono, _animationControl); instant.OnPageWritten += onPageWriten; charByChar.OnPageWritten += onPageWriten; _writers = new Dictionary <Type, WriterBase>() { { typeof(InstantWriter), instant }, { typeof(CharByCharWriter), charByChar }, }; }
protected override IEnumerator Write(TextControl control, TextPage page, TextAnimationControl animationControl) { var wordIndex = 0; var whiteSpacesCount = -1; var highlightedWord = -1; for (int i = 0; i < page.Text.Length; i++) { if (highlightedWord != wordIndex && page.Highlight.ContainsKey(wordIndex)) { highlightedWord = wordIndex; var highlight = page.Highlight[wordIndex]; var highlightLength = highlight.HighlighLength; var startChar = highlight.StartLocalChar; var prevWriteSpeed = WriteSpeedType; //Set default color to chars not colored by the highlight color. for (int j = 0; j < startChar; j++) { var charIndex = i + j; control.ShowChar(charIndex); } i += startChar; for (int j = 0; j < highlightLength; j++) { var charIndex = i + j; control.ShowChar(charIndex, highlight.Color); animationControl.HighlightedChar(charIndex, highlight); yield return(WriteSpeed); if (highlight.WriteSpeedType == Highlight.HighlightWriteSpeed.Default || WriteSpeedType == WriteSpeedType.Fast) { yield return(WriteSpeed); prevWriteSpeed = WriteSpeedType; continue; } else if (!_hightlightWriteSpeed) { _hightlightWriteSpeed = true; WriteSpeed = new WaitForSeconds(highlight.NormalWriteSpeed); } if (highlight.WriteSpeedType == Highlight.HighlightWriteSpeed.Custom) { yield return(WriteSpeed); } } SetWriteTypeSpeed(prevWriteSpeed); _hightlightWriteSpeed = false; var target = i + highlightLength; //When it could be possible to write normal chars in a highlighted word while (page.Text.ElementAtOrDefault(i).IsValidChar()) { if (i >= target) { control.ShowChar(i); animationControl.NormalChar(i); if (WriteSpeedType == WriteSpeedType.Normal || WriteSpeedType == WriteSpeedType.Fast && page.CharByCharInfo.FastWriteSpeed > 0) { yield return(WriteSpeed); } } i++; whiteSpacesCount = 0; } } else { //When is not in highlight while (page.Text.ElementAtOrDefault(i).IsValidChar()) { control.ShowChar(i); animationControl.NormalChar(i); if (WriteSpeedType == WriteSpeedType.Normal || WriteSpeedType == WriteSpeedType.Fast && page.CharByCharInfo.FastWriteSpeed > 0) { yield return(WriteSpeed); } i++; } //Only if the next char is the start of a word. if (page.Text.ElementAtOrDefault(i + 1).IsValidChar()) { whiteSpacesCount = 0; } } whiteSpacesCount++; if (whiteSpacesCount == 1) { wordIndex++; } } OnPageWritten?.Invoke(); }
public CharByCharWriter(MonoBehaviour mono, TextAnimationControl animationControl) : base(mono, animationControl) { }
public InstantWriter(MonoBehaviour mono, TextAnimationControl animationControl) : base(mono, animationControl) { }