protected void CreateButtonText(RectTransform buttonTransform, string textContent, ref GameObject textObject, ref RectTransform textTransform, ref SpritedStringUI textComponent, bool replicateScale) { RectTransform areaParentTrans = null; if ((buttonTransform != null) && (textContent != null)) { areaParentTrans = (RectTransform)buttonTransform.parent; if (areaParentTrans != null) { textObject = new GameObject("ButtonText", typeof(RectTransform)); textTransform = textObject.GetComponent <RectTransform>(); textTransform.SetParent(areaParentTrans, false); textComponent = textObject.AddComponent <SpritedStringUI>(); textComponent.SetSymbolSource(font); textComponent.SetValue(textContent); textComponent.ToggleRaycastTargeting(false); textTransform.anchoredPosition = buttonTransform.anchoredPosition; if (replicateScale) { textTransform.localScale = buttonTransform.localScale; } } } }
protected void UpdateTextDisplayObject(string objectContent, Vector2 scaleChange, Vector2 positionRates, TextAlignment anchorAlign, RectTransform readyTransform, SpritedStringUI readyText) { Vector2 originalUIDimensions = Vector2.zero; if (readyText != null) { readyText.SetValue(objectContent); originalUIDimensions = readyText.GetUIDimensions(); PlaceTransformProperly(readyTransform, originalUIDimensions, scaleChange, positionRates, anchorAlign); } }
protected void CreateTextDisplayObject(string objectName, string objectContent, Vector2 scaleChange, Vector2 positionRates, TextAlignment anchorAlign, ref GameObject newObject, ref RectTransform newTransform, ref SpritedStringUI newText) { Vector2 originalUIDimensions = Vector2.zero; if (font != null) { newObject = new GameObject(objectName, typeof(RectTransform)); newTransform = newObject.GetComponent <RectTransform>(); newText = newObject.AddComponent <SpritedStringUI>(); newText.SetSymbolSource(font); newText.SetValue(objectContent); newText.ToggleRaycastTargeting(false); originalUIDimensions = newText.GetUIDimensions(); PlaceTransformProperly(newTransform, originalUIDimensions, scaleChange, positionRates, anchorAlign); } }
private bool AddCanvasText(string textString, SymbolDatabase textFont, Color textColor, Vector4 textColorGradient, int textEvolution, Transform originTransform, Vector2 originWorldPosition, bool workDuringPause) { GameObject textObject = null; RectTransform textTrans = null; SpritedStringUI text = null; int textIndex = -1; Vector3 completeWorldPos = Vector3.zero; Vector2 currentWorldPos = Vector2.zero; Vector2 canvasPosition = Vector2.zero; Vector2 textSpeed = Vector2.zero; Vector2 textIncreasedScale = Vector2.zero; Color textGeneralColor = Color.white; float textDurationShow = 0f; float textDurationPlain = 0f; float textDurationHide = 0f; int textStage = INVALID_TEXT_STAGE; if (originTransform != null) { completeWorldPos = originTransform.position; currentWorldPos = new Vector2(completeWorldPos.x, completeWorldPos.y); /*halmeida - in case there is an origin transform, the originWorldPosition vector is interpreted as an offset * from the transform's position.*/ currentWorldPos += originWorldPosition; } else { currentWorldPos = originWorldPosition; } if (TransformWorldToCanvasPosition(currentWorldPos, ref canvasPosition)) { textIndex = (textObjects == null) ? 0 : textObjects.Length; textObject = new GameObject("UIText" + textIndex, typeof(RectTransform)); if (textObject != null) { textTrans = textObject.GetComponent <RectTransform>(); text = textObject.AddComponent <SpritedStringUI>(); if (text != null) { text.SetSymbolSource(textFont); text.ToggleRaycastTargeting(false); text.SetValue(textString); textTrans.SetParent(interfaceCanvasTrans, false); textTrans.anchoredPosition = canvasPosition; switch (textEvolution) { case TEXT_EVOLUTION_ASCENT: textSpeed.y = TEXT_ASCENT_SPEED; textGeneralColor = text.GetGeneralColor(); textGeneralColor.a = 0f; text.SetGeneralColor(textGeneralColor); textStage = TEXT_STAGE_SHOW; textIncreasedScale = new Vector2(TEXT_ASCENT_START_SCALE_X, TEXT_ASCENT_START_SCALE_Y); textTrans.localScale = new Vector3(textIncreasedScale.x, textIncreasedScale.y, 1f); textDurationShow = TEXT_ASCENT_DURATION_SHOW; textDurationPlain = TEXT_ASCENT_DURATION_PLAIN; textDurationHide = TEXT_ASCENT_DURATION_HIDE; break; case TEXT_EVOLUTION_STAY: textGeneralColor = text.GetGeneralColor(); textGeneralColor.a = 0f; text.SetGeneralColor(textGeneralColor); textStage = TEXT_STAGE_SHOW; textIncreasedScale = new Vector2(TEXT_STAY_START_SCALE_X, TEXT_STAY_START_SCALE_Y); textTrans.localScale = new Vector3(textIncreasedScale.x, textIncreasedScale.y, 1f); textDurationShow = TEXT_STAY_DURATION_SHOW; textDurationPlain = TEXT_STAY_DURATION_PLAIN; textDurationHide = TEXT_STAY_DURATION_HIDE; break; case TEXT_EVOLUTION_NONE_WAIT: case TEXT_EVOLUTION_NONE_GO: textGeneralColor = text.GetGeneralColor(); textGeneralColor.a = 0f; text.SetGeneralColor(textGeneralColor); textStage = TEXT_STAGE_SHOW; textIncreasedScale = new Vector2(TEXT_NONE_START_SCALE_X, TEXT_NONE_START_SCALE_Y); textTrans.localScale = new Vector3(textIncreasedScale.x, textIncreasedScale.y, 1f); textDurationShow = TEXT_NONE_DURATION_SHOW; textDurationPlain = TEXT_NONE_DURATION_PLAIN; textDurationHide = TEXT_NONE_DURATION_HIDE; break; } text.SetColor(textColor, textColorGradient); UsefulFunctions.IncreaseArray <GameObject>(ref textObjects, textObject); UsefulFunctions.IncreaseArray <RectTransform>(ref textTransforms, textTrans); UsefulFunctions.IncreaseArray <SpritedStringUI>(ref texts, text); UsefulFunctions.IncreaseArray <int>(ref textEvolutions, textEvolution); UsefulFunctions.IncreaseArray <Transform>(ref textOriginTransforms, originTransform); UsefulFunctions.IncreaseArray <Vector2>(ref textOriginPositions, originWorldPosition); UsefulFunctions.IncreaseArray <Vector2>(ref textSpeeds, textSpeed); UsefulFunctions.IncreaseArray <Vector2>(ref textIncreasedScales, textIncreasedScale); UsefulFunctions.IncreaseArray <float>(ref textDurationsShow, textDurationShow); UsefulFunctions.IncreaseArray <float>(ref textDurationsPlain, textDurationPlain); UsefulFunctions.IncreaseArray <float>(ref textDurationsHide, textDurationHide); UsefulFunctions.IncreaseArray <int>(ref textStages, textStage); UsefulFunctions.IncreaseArray <float>(ref textStageElapseds, 0f); UsefulFunctions.IncreaseArray <float>(ref textElapseds, 0f); UsefulFunctions.IncreaseArray <bool>(ref textPauseds, workDuringPause); return(true); } } textTrans = null; if (textObject != null) { Destroy(textObject); } } return(false); }
private void SplitTextIntoLines() { GameObject textLineObject = null; RectTransform textLineTrans = null; SpritedStringUI textLineComponent = null; string textLine = null; int textLineLength = 0; int textLineIndex = -1; float textLineWidth = 0f; float interval = 0f; float position = 0f; int lineFirstIndex = -1; int lineLastIndex = -1; int lineLastIndexValid = -1; int lineLastIndexNext = -1; int lineLastIndexPrevious = -1; bool lineReady = false; bool allLinesReady = false; bool spaceFound = false; /*halmeida - to get one line I gotta create a long enough SpritedStringUI that fits * exactly into the maxTextWidth. If I don't have enough characters to fill the width, * I will just make a shorter line. The problem is that I'm able to see the width of a * sprited string only after I set its value with all the characters it is supposed to * have. Adding character by character to the SpritedString would be too slow, so I use * a sort of bynary search, looking for the maximum valid size for each line.*/ if ((text != null) && (textLines == null) && (ownTransform != null)) { textLineIndex = 0; lineFirstIndex = 0; lineLastIndex = text.Length - 1; while (!allLinesReady) { lineLastIndexValid = -1; interval = lineLastIndex - lineFirstIndex + 1; position = lineFirstIndex + interval; while (!lineReady) { if (textLineObject == null) { textLineObject = new GameObject("TextBoxUILine" + textLineIndex, typeof(RectTransform)); textLineTrans = textLineObject.GetComponent <RectTransform>(); /*halmeida - we should not add the lines as children of the object yet because the box * image itself has not yet been added as a child of the object. The lines would get behind * it if they were added now.*/ textLineComponent = textLineObject.AddComponent <SpritedStringUI>(); textLineComponent.SetSymbolSource(symbolDatabase); } textLineLength = lineLastIndex - lineFirstIndex + 1; textLine = text.Substring(lineFirstIndex, textLineLength); textLineWidth = symbolDatabase.GetStringWidthUI(textLine); //Debug.Log("Debug : TextBoxUI : attempting text line width "+textLineWidth+"."); interval = interval / 2f; if (textLineWidth > maxTextWidth) { position -= interval; } else { lineLastIndexValid = lineLastIndex; position += interval; } lineLastIndexNext = (int)position; /*halmeida - the position value itself should never be the same, but since it is rounded * to an integer index, we may end up falling back to a previously checked index. When that * happens, it means the interval has become small enough to stop the search.*/ if ((lineLastIndexNext == lineLastIndexPrevious) || (lineLastIndexNext == lineLastIndex) || (lineLastIndexNext > text.Length - 1)) { if (lineLastIndexValid == -1) { /*halmeida - after all the searching, no valid size was found. This probably means * the maxTextWidth is just too small to fit even one character. Even so we will * forcibly accept a one character wide line.*/ lineLastIndexValid = lineFirstIndex; } if (lineLastIndexValid > lineFirstIndex) { /*halmeida - if there is more than one character in the line, we can check for * word integrity. We cannot break a word into two lines. This means that the last * character in a line that is not the last line has to be an empty space or the * space has to be the first character in the next line.*/ if ((lineLastIndexValid + 1) < text.Length) { spaceFound = false; for (int i = (lineLastIndexValid + 1); i > lineFirstIndex; i--) { if (text[i] == ' ') { lineLastIndexValid = i - 1; spaceFound = true; } else { if (spaceFound) { break; } } } } } lineReady = true; /*halmeida - we didn't necessarily end the search at a valid size, but the last valid * size found is the biggest possible one. So we use that value to build the line.*/ textLineLength = lineLastIndexValid - lineFirstIndex + 1; textLine = text.Substring(lineFirstIndex, textLineLength); textLineComponent.SetValue(textLine); textLineComponent.ToggleAllSymbolVisuals(false); //Debug.Log("Debug : TextBoxUI : line "+textLineIndex+" is \""+textLine+"\"."); textLineWidth = textLineComponent.GetUIDimensions().x; //Debug.Log("Debug : TextBoxUI : final text line width "+textLineWidth+"."); if (textLineWidth > maxLineWidth) { maxLineWidth = textLineWidth; } } else { lineLastIndexPrevious = lineLastIndex; lineLastIndex = lineLastIndexNext; } } UsefulFunctions.IncreaseArray <string>(ref textLines, textLine); UsefulFunctions.IncreaseArray <GameObject>(ref textLineObjects, textLineObject); UsefulFunctions.IncreaseArray <RectTransform>(ref textLineTransforms, textLineTrans); UsefulFunctions.IncreaseArray <SpritedStringUI>(ref textLineComponents, textLineComponent); textLine = null; textLineObject = null; textLineTrans = null; textLineComponent = null; if (lineLastIndexValid == (text.Length - 1)) { allLinesReady = true; } else { textLineIndex++; lineFirstIndex = lineLastIndexValid + 1; for (int i = lineFirstIndex; i < text.Length; i++) { if (text[i] == ' ') { lineFirstIndex++; if (lineFirstIndex == text.Length) { allLinesReady = true; } } else { break; } } lineLastIndex = text.Length - 1; lineReady = false; } } } }