Exemplo n.º 1
0
        /// <summary>Use this every time you need to render text that needs to be multiline</summary>
        /// <param name="text">The string to be rendered.</param>
        /// <param name="x">the left portion of the text rect</param>
        /// <param name="y">the vertical pivot of the text rect</param>
        /// <param name="max_width"> maximum width of the paragraph</param>
        /// <param name="horizontal_alignment">Where the bounding box is given by x, x+max_width</param>
        /// <param name="vertical_alignment"> In this overload, verticality hinges on the supplied y</param>
        /// <returns>An array of text sprites that were created to render this text</returns>
        public static Graphics.Sprite[] render_text_multiline(string text, float x, float y, float max_width,
                                                                HorizontalAlignment horizontal_alignment = HorizontalAlignment.left,
                                                                VerticalAlignment vertical_alignment = VerticalAlignment.top)
        {
            var broken_text = break_text(text, max_width, Graphics.current_font);

            var y_zero = 0f;
            if (vertical_alignment == VerticalAlignment.center) y_zero -= broken_text.total_height / 2;
            if (vertical_alignment == VerticalAlignment.bottom) y_zero -= broken_text.total_height;
            var _sprs = new Graphics.Sprite[broken_text.num_lines];

            for (int l = 0; l < broken_text.lines.Length; l++)
            {
                var line = broken_text.lines[l];

                var x_zero = 0f;

                if (horizontal_alignment == HorizontalAlignment.center) x_zero = (max_width - line.width) * 0.5f;
                if (horizontal_alignment == HorizontalAlignment.right) x_zero = (max_width - line.width);

                _sprs[l] = Graphics.add_text(x + x_zero, y + y_zero, line.text);

                // JUSTIFY ALGORITHM:
                if (horizontal_alignment == HorizontalAlignment.justify) if (l != broken_text.lines.Length - 1)
                        _sprs[l].txt.additional_spacing = (max_width - line.width) / count_spaces(line.text);

                y_zero += broken_text.font.v_spacing;
            }
            return _sprs;
        }
Exemplo n.º 2
0
        /// <summary>Renders a text in a given box, with given alignments. Will break the text if necessary</summary>
        /// <param name="text">the string to be broken. The character | is used as a line break. </param>
        /// <param name="x0">Left coordinate of the box</param> <param name="y0"> Top coordinate of the box</param>
        /// <param name="w">width of the box</param><param name="h">height of the box</param>
        /// <param name="horizontal_alignment">Self-explanatory. Justified included! </param>
        /// <param name="vertical_alignment">Self-explanatory.</param>
        /// <returns>An array of sprites that comprise the new letters</returns>
        public static Graphics.Sprite[] render_text_in_box(string text, float x0, float y0, float w, float h,
                                                                HorizontalAlignment horizontal_alignment = HorizontalAlignment.left,
                                                                VerticalAlignment vertical_alignment = VerticalAlignment.top)
        {
            var font = Graphics.current_font;

            var broken_text = break_text(text, w, font);
            var _sprs = new Graphics.Sprite[broken_text.num_lines];

            var total_h = Graphics.current_font.v_spacing * broken_text.lines.Length;

            #region Determine Y coord by alignment
            var zero_y = 0f;
            switch (vertical_alignment)
            {
                case VerticalAlignment.top: zero_y = y0; break;
                case VerticalAlignment.center: zero_y = y0 + h / 2 - total_h / 2; break;
                case VerticalAlignment.bottom: zero_y = y0 + h - Graphics.current_font.v_spacing; break;
            }
            #endregion

            for (int l = 0; l < broken_text.lines.Length; l++)
            {
                var line = broken_text.lines[l];

                #region Determine X coord by alignment
                var x_zero = 0f;
                switch (horizontal_alignment)
                {
                    case HorizontalAlignment.left: x_zero = x0; break;
                    case HorizontalAlignment.center: x_zero = x0 + (w - line.width) / 2; break;
                    case HorizontalAlignment.right: x_zero = x0 + (w - line.width); break;
                    default: break; // we manage the other later
                }
                #endregion

                _sprs[l] = Graphics.add_text(x_zero, zero_y, line.text);

                if (horizontal_alignment == HorizontalAlignment.justify)
                if (l != broken_text.lines.Length - 1)
                _sprs[l].txt.additional_spacing = (w - line.width) / count_spaces(line.text);
                zero_y += broken_text.font.v_spacing;
            }

            return _sprs;
        }