Ejemplo n.º 1
0
        public float[] GetLetterProgressions(TextFxAnimationManager anim_manager, ref LetterSetup[] letters, TextAlignment alignment = TextAlignment.Left)
        {
            float[] letter_progressions = new float[letters.Length];

            if (letters.Length == 0)
            {
                return(letter_progressions);
            }

            if (m_pointData.m_numActiveCurvePoints < 2)
            {
                for (int idx = 0; idx < letters.Length; idx++)
                {
                    letter_progressions[idx] = 0;
                }
                return(letter_progressions);
            }

            float       progress_inc = 1f / NUM_CURVE_SAMPLE_SUBSECTIONS;
            float       progress;
            Vector3     new_point = new Vector3();
            Vector3     last_point = new Vector3();
            int         letter_idx = 0, line_number = 0;
            float       base_letters_offset = 0, letters_offset = 0;
            LetterSetup letter = null;
            float       last_line_length = 0, line_length = 0;
            float       curve_length = 0;
            float       renderedTextWidth;

            // Method to handle offsetting the letter_offset value based on the text alignment setting
            System.Action offsetBasedOnTextAlignment = () => {
                if (alignment == TextAlignment.Center || alignment == TextAlignment.Right)
                {
                    renderedTextWidth = (anim_manager.TextWidthScaled(letter.m_progression_variables.LineValue) < curve_length ? anim_manager.TextWidthScaled(letter.m_progression_variables.LineValue) : curve_length);

                    if (alignment == TextAlignment.Center)
                    {
                        letters_offset += (curve_length / 2) - (renderedTextWidth / 2);
                    }
                    else if (alignment == TextAlignment.Right)
                    {
                        letters_offset += curve_length - renderedTextWidth;
                    }
                }
            };


            // Grab reference to first letter setup
            letter = letters[0];

            // Calculate the total length of the belzier curve if the text alignment is set to center or right.
            if (alignment == TextAlignment.Center || alignment == TextAlignment.Right)
            {
                for (int curve_idx = 0; curve_idx < m_pointData.m_numActiveCurvePoints - 1; curve_idx++)
                {
                    curve_length += GetCurveLength(curve_idx);
                }
            }


            // Assign base letter offset value using first letters offset value
            base_letters_offset = letter.BaseVerticesCenter.x / anim_manager.AnimationInterface.MovementScale - (letter.Width / anim_manager.AnimationInterface.MovementScale) / 2;

            // Setup letter offset value
            letters_offset = (letter.Width / anim_manager.AnimationInterface.MovementScale) / 2;

            // Handle alignment-specific offset values
            offsetBasedOnTextAlignment();


            bool reachedEndOfLetters = false;

            while (!reachedEndOfLetters)
            {
                for (int curve_idx = 0; curve_idx < m_pointData.m_numActiveCurvePoints - 1; curve_idx++)
                {
                    for (int idx = 0; idx <= NUM_CURVE_SAMPLE_SUBSECTIONS; idx++)
                    {
                        progress = idx * progress_inc;

                        new_point = GetCurvePoint(progress, curve_idx: curve_idx);

                        if (idx > 0)
                        {
                            line_length += (new_point - last_point).magnitude;

                            while (letter_idx < letters.Length && line_length > letters_offset)
                            {
                                // calculate relative progress between the last two points which would represent the next letters offset distance
                                progress = curve_idx + ((idx - 1) * progress_inc) + (((letters_offset - last_line_length) / (line_length - last_line_length)) * progress_inc);

                                letter_progressions [letter_idx] = progress;

                                letter_idx++;

                                // Work out offset value for next letter
                                if (letter_idx < letters.Length && !letters [letter_idx].StubInstance && letters [letter_idx].VisibleCharacter)
                                {
                                    letter = letters [letter_idx];

                                    if (letter.m_progression_variables.LineValue > line_number)
                                    {
                                        line_number = letter.m_progression_variables.LineValue;

                                        // Set a new base offset value to that of the first letter of this new line
                                        base_letters_offset = letter.BaseVerticesCenter.x / anim_manager.AnimationInterface.MovementScale - (letter.Width / anim_manager.AnimationInterface.MovementScale) / 2;

                                        curve_idx   = 0;
                                        idx         = -1;
                                        line_length = 0;
                                    }

                                    // Setup letter offset value
                                    letters_offset = ((letter.BaseVerticesCenter.x / anim_manager.AnimationInterface.MovementScale) - base_letters_offset);

                                    // Handle alignment-specific offset values
                                    offsetBasedOnTextAlignment();
                                }
                            }

                            if (letter_idx == letters.Length)
                            {
                                reachedEndOfLetters = true;
                                break;
                            }
                        }

                        last_point       = new_point;
                        last_line_length = line_length;
                    }
                }

                // Handle any letters which didn't have room to fit on the line
                for (int idx = letter_idx; idx < letters.Length; idx++)
                {
                    letter = letters[letter_idx];

                    if (letter.m_progression_variables.LineValue == line_number)
                    {
                        // Force remaining letters to the end of the line
                        letter_progressions [idx] = m_pointData.m_numActiveCurvePoints - 1.001f;
                    }
                    else if (letter.VisibleCharacter && !letter.StubInstance)
                    {
                        line_number = letter.m_progression_variables.LineValue;

                        line_length = 0;

                        // Set a new base offset value to that of the first letter of this new line
                        base_letters_offset = letter.BaseVerticesCenter.x / anim_manager.AnimationInterface.MovementScale - (letter.Width / anim_manager.AnimationInterface.MovementScale) / 2;

                        // Setup letter offset value
                        letters_offset = ((letter.BaseVerticesCenter.x / anim_manager.AnimationInterface.MovementScale) - base_letters_offset);

                        // Handle alignment-specific offset values
                        offsetBasedOnTextAlignment();

                        break;
                    }

                    letter_idx++;
                }

                if (letter_idx == letters.Length)
                {
                    reachedEndOfLetters = true;
                    break;
                }
            }

//			string progressionsString = "";
//			for (int i = 0; i < letter_progressions.Length; i++)
//				progressionsString += letter_progressions [i] + "\n";
//			Debug.Log("Curve Letter Progressions:\n" + progressionsString);

            return(letter_progressions);
        }