private static int RoundCurrentCharacterIndex(GameUi gameUi) { int rounded = Math.Min( Mathf.FloorToInt(gameUi.currentCharacterIndex), gameUi.storyText.textInfo.characterCount ); return(rounded); }
private static bool ChoiceButtonsAreShowing(GameUi gameUi) { bool choiceButtonsAreShowing = (gameUi.choice1Button.activeInHierarchy && gameUi.choice2Button.activeInHierarchy) || gameUi.clickToContinueText.gameObject.activeInHierarchy; return(choiceButtonsAreShowing); }
internal static bool GetChoicesShowing(GameUi gameUi) { if (gameUi.stillRevealingText) { return(false); } // AHHH the offset here (the -1 part in lineCount - 1) is kind of fragile, unfortunately. // You've been warned. int bottomLine = Math.Max(0, gameUi.storyText.textInfo.lineCount - 1); bool lastLineIsHighEnough = gameUi.scrollContainer.anchoredPosition.y > ScrollYForLine(gameUi, bottomLine, Line.AtBottom); return(lastLineIsHighEnough); }
internal static void AddText(GameUi gameUi, string[] newParagraphs, bool isLoading) { int oldCharacterCount = gameUi.storyText.textInfo.characterCount; gameUi.stillRevealingText = true; string twoBlankLines = Environment.NewLine + Environment.NewLine; string newText = string.Join(twoBlankLines, newParagraphs); if (!string.IsNullOrWhiteSpace(gameUi.storyText.text)) { gameUi.storyText.text += twoBlankLines; } // ADD THE TEXT AND UPDATE THE TEXT COMPONENT. gameUi.storyText.text += newText; gameUi.storyText.ForceMeshUpdate(); if (isLoading) { gameUi.currentCharacterIndex = gameUi.storyText.textInfo.characterInfo.Reverse() .SkipWhile(c => c.character == '\0') .SkipWhile(c => c.style != FontStyles.Italic) .First().index; } // SKIP THE ACTION TEXT gameUi.currentCharacterIndex += gameUi.storyText.textInfo.characterInfo .Skip(RoundCurrentCharacterIndex(gameUi)) .Count(c => c.style == FontStyles.Italic); // START FADING IN **ALL** THE NEW LETTERS. // TO START, THIS HIDES THEM, HOPEFULLY BEFORE THE NEXT UPDATE. var newLetterIndexes = gameUi.storyText.textInfo.characterInfo .Select((characterInfo, index) => index) // NOTE: You HAVE to Select BEFORE you Skip on this command. // If you Skip first, all of the indexes will be off. :/ .Skip(oldCharacterCount) .Take(gameUi.storyText.textInfo.characterCount - oldCharacterCount) .ToArray(); foreach (var newLetterIndex in newLetterIndexes) { gameUi.StartCoroutine(FadeInLetter(gameUi, newLetterIndex)); } gameUi.storyText.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32); }
private static void ScrollToNow(GameUi gameUi, float newScrollY) { int bottomLine = Math.Max(0, gameUi.storyText.textInfo.lineCount - 3); float scrollYForBottomLine = ScrollYForLine(gameUi, bottomLine, Line.AtTop); if (scrollYForBottomLine > 0 && newScrollY > scrollYForBottomLine) { newScrollY = scrollYForBottomLine; } if (newScrollY < 0) { newScrollY = 0; } gameUi.scrollContainer.anchoredPosition = new Vector2(gameUi.scrollContainer.anchoredPosition.x, newScrollY); }
private static IEnumerator ScrollToSmooth(GameUi gameUi, float targetScrollY) { gameUi.StopCoroutine("ScrollToSmooth"); float startingY = gameUi.scrollContainer.anchoredPosition.y; float startingTime = Time.time; const float secondsToScrollFor = 1F; float currentY = startingY; while (!Mathf.Approximately(currentY, targetScrollY)) { currentY = Mathf.SmoothStep(startingY, targetScrollY, (Time.time - startingTime) / secondsToScrollFor); ScrollToNow(gameUi, currentY); yield return(null); } ScrollToNow(gameUi, targetScrollY); }
// ************ // TEXT // ************ private static IEnumerator FadeInLetter(GameUi gameUi, int characterIndex) { TMP_CharacterInfo letter = gameUi.storyText.textInfo.characterInfo[characterIndex]; if (!letter.isVisible) { yield break; } // Get the index of the first vertex used by this text element. int vertexIndex = letter.vertexIndex; // UPDATE THE VERTEX COLORS ARRAY. // Get the vertex colors of the mesh used by this text element (character or sprite). int materialIndex = letter.materialReferenceIndex; // MAKE CLEAR TO START. var vertexColors = gameUi.storyText.textInfo.meshInfo[materialIndex].colors32; var color = vertexColors[vertexIndex + 0]; color.a = 0; void UpdateVertexColor(Color32 newColor) { vertexColors = gameUi.storyText.textInfo.meshInfo[materialIndex].colors32; vertexColors[vertexIndex + 0] = newColor; vertexColors[vertexIndex + 1] = newColor; vertexColors[vertexIndex + 2] = newColor; vertexColors[vertexIndex + 3] = newColor; } // NOTE TO FUTURE SELF: // NEVER call UpdateVertexData in this function. // It makes the scrolling lag a ton if you skip forward about 4-5 choices in. // NEVER CALL THIS storyText.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32); do { UpdateVertexColor(color); yield return(null); }while (characterIndex >= gameUi.currentCharacterIndex && gameUi.stillRevealingText); // This part of the condition makes sure that // ALL the letters fade in at once when you // skip to the end. // FADE IN AND MOVE DOWN. var alphaPerSecond = 255F / (30F / gameUi.lettersPerSecond); // TODO: REPLACE THE "3" ABOVE WITH AN ACTUAL CALCULATED letterFadeInDuration VARIABLE while (color.a < 255) { int previousAlpha = color.a; color.a += (byte)Mathf.RoundToInt(alphaPerSecond * Time.deltaTime); if (previousAlpha > color.a) { // We've looped around, break out of this loop. break; } UpdateVertexColor(color); yield return(null); } // SET BACK TO ORIGINAL COLOR TO END. color.a = 255; UpdateVertexColor(color); yield return(null); }
internal static void ScrollToSmooth(GameUi gameUi, int lineNumber, Line linePos) { var targetScrollY = ScrollYForLine(gameUi, lineNumber, linePos); gameUi.StartCoroutine(ScrollToSmooth(gameUi, targetScrollY)); }
private static void ScrollToNow(GameUi gameUi, int lineNumber, Line linePos) { var newY = ScrollYForLine(gameUi, lineNumber, linePos); ScrollToNow(gameUi, newY); }
internal static void ContinueRevealing(GameUi gameUi) { //ScrollToNow(storyText.textInfo.lineCount - 2, Line.AtTop); }
internal static string GetScrollText(GameUi gameUi) { return(gameUi.storyText.text); }
internal static void AddText(GameUi gameUi, params string[] newParagraphs) { AddText(gameUi, newParagraphs, isLoading: false); }