Exemplo n.º 1
0
        public void AddItem(string text, int position = -1)
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            TextLabel textLabel = new TextLabel();

            textLabel.MaxWidth            = (int)Size.x;
            textLabel.AutoSize            = AutoSizeModes.None;
            textLabel.HorizontalAlignment = rowAlignment;
            textLabel.Font          = font;
            textLabel.MaxCharacters = maxCharacters;
            textLabel.Text          = text;
            textLabel.Parent        = this;

            if (position < 0)
            {
                listItems.Add(textLabel);
            }
            else
            {
                listItems.Insert(position, textLabel);
            }
        }
        /// <summary>
        /// Adds a single text item.
        /// Each subsequent text item will be appended to the previous text item position.
        /// Call NewLine() to start a new line.
        /// </summary>
        /// <param name="text">Text for this label.</param>
        /// <param name="font">Font for this label.</param>
        /// <returns>TextLabel.</returns>
        public TextLabel AddTextLabel(string text, PixelFont font = null)
        {
            if (font == null)
            {
                font = GetFont();
            }

            TextLabel textLabel = new TextLabel();

            textLabel.AutoSize = AutoSizeModes.None;
            textLabel.Font     = font;
            textLabel.Position = new Vector2(cursorX, cursorY);
            textLabel.Text     = text;
            textLabel.Parent   = this;

            textLabel.TextColor      = TextColor;
            textLabel.ShadowColor    = ShadowColor;
            textLabel.ShadowPosition = ShadowPosition;

            if (textAlignment != HorizontalAlignment.None)
            {
                textLabel.HorizontalAlignment = textAlignment;
            }

            labels.Add(textLabel);
            lastLabel = textLabel;

            cursorX += textLabel.TextWidth;

            return(textLabel);
        }
        /// <summary>
        /// Sets the formatted text from a token array.
        /// </summary>
        /// <param name="tokens">Format token array.</param>
        public virtual void SetTextTokens(TextFile.Token[] tokens)
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            LayoutTextElements(tokens);
        }
        PixelFont GetFont()
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            return(font);
        }
Exemplo n.º 5
0
        protected virtual void CreateLabelTexture()
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            // First pass encodes ASCII and calculates final dimensions
            int width = 0;

            asciiBytes = Encoding.ASCII.GetBytes(text);
            for (int i = 0; i < asciiBytes.Length; i++)
            {
                // Invalid ASCII bytes are cast to a space character
                if (!font.HasGlyph(asciiBytes[i]))
                {
                    asciiBytes[i] = PixelFont.SpaceASCII;
                }

                // Calculate total width
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                width += glyph.width + font.GlyphSpacing;
            }

            if (maxWidth > 0 && width > maxWidth)
            {
                width = maxWidth;
            }

            // Create target label texture
            totalWidth   = width;
            totalHeight  = font.GlyphHeight;
            labelTexture = CreateLabelTexture(totalWidth, totalHeight);
            if (labelTexture == null)
            {
                throw new Exception("TextLabel failed to create labelTexture.");
            }

            // Second pass adds glyphs to label texture
            int xpos = 0;

            for (int i = 0; i < asciiBytes.Length; i++)
            {
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                if (xpos + glyph.width >= totalWidth)
                {
                    break;
                }

                labelTexture.SetPixels32(xpos, 0, glyph.width, totalHeight, glyph.colors);
                xpos += glyph.width + font.GlyphSpacing;
            }
            labelTexture.Apply(false, true);
            labelTexture.filterMode = font.FilterMode;
            this.Size = new Vector2(totalWidth, totalHeight);
        }
        TextLabel AddTextLabel(PixelFont font, Vector2 position, string text)
        {
            TextLabel textLabel = new TextLabel();

            textLabel.ScalingMode = Scaling.None;
            textLabel.Font        = font;
            textLabel.Position    = position;
            textLabel.Text        = text;
            textLabel.Parent      = this;

            return(textLabel);
        }
Exemplo n.º 7
0
        public void AddItem(string text)
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            TextLabel textLabel = new TextLabel();

            textLabel.ScalingMode         = Scaling.None;
            textLabel.HorizontalAlignment = rowAlignment;
            textLabel.Font          = font;
            textLabel.MaxCharacters = maxCharacters;
            textLabel.Text          = text;
            textLabel.Parent        = this;
            listItems.Add(textLabel);
        }
Exemplo n.º 8
0
        public void AddItem(string text, out ListItem itemOut, int position = -1)
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            TextLabel textLabel = new TextLabel();

            if (useRestrictedRenderArea)
            {
                textLabel.RectRestrictedRenderArea = this.rectRestrictedRenderArea;
            }
            if (horizontalScrollMode == HorizontalScrollModes.CharWise)
            {
                textLabel.MaxWidth = (int)Size.x;
            }
            else if (horizontalScrollMode == HorizontalScrollModes.PixelWise)
            {
                textLabel.MaxWidth = -1;
            }
            textLabel.AutoSize            = AutoSizeModes.None;
            textLabel.HorizontalAlignment = rowAlignment;
            textLabel.Font          = font;
            textLabel.MaxCharacters = maxCharacters;
            textLabel.Text          = text;
            textLabel.TextScale     = textScale;
            textLabel.Parent        = this;
            textLabel.WrapText      = wrapTextItems;
            textLabel.WrapWords     = wrapWords;

            itemOut                     = new ListItem(textLabel);
            itemOut.textColor           = textColor;
            itemOut.selectedTextColor   = selectedTextColor;
            itemOut.shadowColor         = shadowColor;
            itemOut.selectedShadowColor = selectedShadowColor;
            if (position < 0)
            {
                listItems.Add(itemOut);
            }
            else
            {
                listItems.Insert(position, itemOut);
            }
        }
        /// <summary>
        /// Adds a single text item.
        /// Each subsequent text item will be appended to the previous text item position.
        /// Call NewLine() to start a new line.
        /// </summary>
        /// <param name="text">Text for this label.</param>
        /// <param name="font">Font for this label.</param>
        /// <returns>TextLabel.</returns>
        public TextLabel AddTextLabel(string text, PixelFont font = null)
        {
            if (font == null)
            {
                font = GetFont();
            }

            TextLabel textLabel = new TextLabel();

            textLabel.AutoSize      = AutoSizeModes.None;
            textLabel.MinTextureDim = minTextureDimTextLabel;
            textLabel.Font          = font;
            textLabel.Position      = new Vector2(cursorX, cursorY);
            textLabel.WrapText      = wrapText;
            textLabel.WrapWords     = wrapWords;
            textLabel.TextScale     = TextScale;

            // Use max width if it has been specified
            if (maxTextWidth > 0)
            {
                textLabel.MaxWidth = maxTextWidth;
            }
            textLabel.Text           = text;
            textLabel.Parent         = this;
            textLabel.TextColor      = TextColor;
            textLabel.ShadowColor    = ShadowColor;
            textLabel.ShadowPosition = ShadowPosition;

            if (textAlignment != HorizontalAlignment.None)
            {
                textLabel.HorizontalAlignment = textAlignment;
            }

            labels.Add(textLabel);
            lastLabel = textLabel;

            cursorX += textLabel.TextWidth;

            return(textLabel);
        }
Exemplo n.º 10
0
        TextLabel AddTextLabel(PixelFont font, Vector2 position, string text)
        {
            TextLabel textLabel = new TextLabel();
            textLabel.ScalingMode = Scaling.None;
            textLabel.Font = font;
            textLabel.Position = position;
            textLabel.Text = text;
            textLabel.Parent = this;

            return textLabel;
        }
        protected virtual void CreateLabelTexture()
        {
            if (font == null)
                font = DaggerfallUI.DefaultFont;

            // First pass encodes ASCII and calculates final dimensions
            int width = 0;
            asciiBytes = Encoding.ASCII.GetBytes(text);
            for (int i = 0; i < asciiBytes.Length; i++)
            {
                // Invalid ASCII bytes are cast to a space character
                if (!font.HasGlyph(asciiBytes[i]))
                    asciiBytes[i] = PixelFont.SpaceASCII;

                // Calculate total width
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                width += glyph.width + font.GlyphSpacing;
            }

            if (maxWidth > 0 && width > maxWidth)
                width = maxWidth;

            // Create target label texture
            totalWidth = width;
            totalHeight = font.GlyphHeight;
            labelTexture = CreateLabelTexture(totalWidth, totalHeight);
            if (labelTexture == null)
                throw new Exception("TextLabel failed to create labelTexture.");

            // Second pass adds glyphs to label texture
            int xpos = 0;
            for (int i = 0; i < asciiBytes.Length; i++)
            {
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                if (xpos + glyph.width >= totalWidth)
                    break;

                labelTexture.SetPixels32(xpos, 0, glyph.width, totalHeight, glyph.colors);
                xpos += glyph.width + font.GlyphSpacing;
            }
            labelTexture.Apply(false, true);
            labelTexture.filterMode = font.FilterMode;
            this.Size = new Vector2(totalWidth, totalHeight);
        }
        /// <summary>
        /// Adds a single text item.
        /// Each subsequent text item will be appended to the previous text item position.
        /// Call NewLine() to start a new line.
        /// </summary>
        /// <param name="text">Text for this label.</param>
        /// <param name="font">Font for this label.</param>
        /// <returns>TextLabel.</returns>
        public TextLabel AddTextLabel(string text, PixelFont font = null)
        {
            if (font == null)
                font = GetFont();

            TextLabel textLabel = new TextLabel();
            textLabel.AutoSize = AutoSizeModes.None;
            textLabel.Font = font;
            textLabel.Position = new Vector2(cursorX, cursorY);
            textLabel.Text = text;
            textLabel.Parent = this;

            textLabel.TextColor = TextColor;
            textLabel.ShadowColor = ShadowColor;
            textLabel.ShadowPosition = ShadowPosition;

            if (textAlignment != HorizontalAlignment.None)
                textLabel.HorizontalAlignment = textAlignment;

            labels.Add(textLabel);
            lastLabel = textLabel;

            cursorX += textLabel.TextWidth;

            return textLabel;
        }
        PixelFont GetFont()
        {
            if (font == null)
                font = DaggerfallUI.DefaultFont;

            return font;
        }
Exemplo n.º 14
0
        public void AddItem(string text, int position = -1)
        {
            if (font == null)
                font = DaggerfallUI.DefaultFont;

            TextLabel textLabel = new TextLabel();
            textLabel.MaxWidth = (int)Size.x;
            textLabel.AutoSize = AutoSizeModes.None;
            textLabel.HorizontalAlignment = rowAlignment;
            textLabel.Font = font;
            textLabel.MaxCharacters = maxCharacters;
            textLabel.Text = text;
            textLabel.Parent = this;

            if (position < 0)
                listItems.Add(textLabel);
            else
                listItems.Insert(position, textLabel);
        }
Exemplo n.º 15
0
        void CreateLabelTextureWrapped()
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            // set a local maxWidth that compensates for textScale
            int maxWidth = (int)(this.maxWidth / textScale);

            // First pass encodes ASCII and calculates final dimensions
            int width = 0;
            int greatestWidthFound = 0;
            int lastEndOfRowByte   = 0;

            asciiBytes = Encoding.ASCII.GetBytes(text);
            List <byte[]> rows     = new List <byte[]>();
            List <int>    rowWidth = new List <int>();

            for (int i = 0; i < asciiBytes.Length; i++)
            {
                // Invalid ASCII bytes are cast to a space character
                if (!font.HasGlyph(asciiBytes[i]))
                {
                    asciiBytes[i] = PixelFont.SpaceASCII;
                }

                // Calculate total width
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);

                // If maxWidth is set, don't allow the label texture to exceed it
                if ((maxWidth <= 0) || ((width + glyph.width + font.GlyphSpacing) <= maxWidth))
                {
                    width += glyph.width + font.GlyphSpacing;
                }
                else
                {
                    int rowLength;
                    if (wrapWords)
                    {
                        int j;
                        for (j = i; j >= lastEndOfRowByte; j--)
                        {
                            glyph = font.GetGlyph(asciiBytes[j]);
                            if (j < i) // glyph i has not been added to width
                            {
                                width -= glyph.width + font.GlyphSpacing;
                                if (width <= maxWidth && asciiBytes[j] == PixelFont.SpaceASCII)
                                {
                                    break;
                                }
                            }
                        }

                        if (j > lastEndOfRowByte)             // space found in row at position that is not exceeding maxWidth for all summed glyph's widths before this position
                        {
                            i         = j;                    // set new processing position (j is the space position, i will be increased on next loop iteration to point to next chat after the space char)
                            rowLength = j - lastEndOfRowByte; // set length from row start to position before space
                        }
                        else
                        {
                            rowLength = i - lastEndOfRowByte;
                        }

                        // compute width of text-wrapped line
                        width = 0;
                        for (int k = lastEndOfRowByte; k < j; k++)
                        {
                            if (k < j - 1 || (k == j - 1 && asciiBytes[k] != PixelFont.SpaceASCII)) // all expect last character if it is a space
                            {
                                glyph  = font.GetGlyph(asciiBytes[k]);
                                width += glyph.width + font.GlyphSpacing;
                            }
                        }
                    }
                    else
                    {
                        rowLength = i - lastEndOfRowByte;
                    }
                    // The row of glyphs exceeded maxWidth. Add it to the list of rows and start
                    // counting width again with the remainder of the ASCII bytes.
                    List <byte> content = new List <byte>(asciiBytes).GetRange(lastEndOfRowByte, rowLength);
                    if (content[content.Count - 1] == PixelFont.SpaceASCII)
                    {
                        content.RemoveAt(content.Count - 1);
                    }
                    byte[] trimmed = content.ToArray();

                    rows.Add(trimmed);
                    rowWidth.Add(width);

                    // update greatest width found so far
                    if (greatestWidthFound < width)
                    {
                        greatestWidthFound = width;
                    }

                    // reset width for next line
                    width = 0;

                    // Resume interation over remainder of ASCII bytes
                    //asciiBytes = new List<byte>(asciiBytes).GetRange(i, asciiBytes.Length - i).ToArray();
                    //i = 0;

                    lastEndOfRowByte = i + 1; // position after space for next line, note: i will be increased on next loop iteration anyway - so no need to increase i
                }
            }

            if (lastEndOfRowByte > 0)
            {
                asciiBytes = new List <byte>(asciiBytes).GetRange(lastEndOfRowByte, asciiBytes.Length - lastEndOfRowByte).ToArray();
            }

            // also get width of last line
            width = 0;
            for (int i = 0; i < asciiBytes.Length; i++)
            {
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                width += glyph.width + font.GlyphSpacing;
            }

            // update greatest width found so far
            if (width <= maxWidth && greatestWidthFound < width) // width should always be <= maxWidth here
            {
                greatestWidthFound = width;
            }

            rows.Add(asciiBytes);
            rowWidth.Add(width);

            //width = greatestWidthFound;

            // Destroy old texture
            if (labelTexture)
            {
                UnityEngine.Object.Destroy(labelTexture);
            }

            // Create target label texture
            totalWidth   = maxWidth;
            totalHeight  = (int)(rows.Count * font.GlyphHeight);
            numTextLines = rows.Count;
            labelTexture = CreateLabelTexture(totalWidth, totalHeight);
            if (labelTexture == null)
            {
                throw new Exception("TextLabel failed to create labelTexture.");
            }

            // Second pass adds glyphs to label texture
            int xpos = 0;
            int ypos = totalHeight - font.GlyphHeight;

            //foreach (byte[] row in rows)
            for (int r = 0; r < rows.Count; r++)
            {
                byte[] row = rows[r];
                float  alignmentOffset;
                switch (horizontalTextAlignment)
                {
                default:
                case HorizontalTextAlignmentSetting.None:
                case HorizontalTextAlignmentSetting.Left:
                case HorizontalTextAlignmentSetting.Justify:
                    alignmentOffset = 0.0f;
                    break;

                case HorizontalTextAlignmentSetting.Center:
                    alignmentOffset = (totalWidth - rowWidth[r]) * 0.5f;
                    break;

                case HorizontalTextAlignmentSetting.Right:
                    alignmentOffset = totalWidth - rowWidth[r];
                    break;
                }

                int numSpaces = 0;              // needed to compute extra offset between words for HorizontalTextAlignmentSetting.Justify
                //int widthTextCharacters = 0; // needed to compute extra offset between words for HorizontalTextAlignmentSetting.Justify
                int extraSpaceToDistribute = 0; // needed to compute extra offset between words for HorizontalTextAlignmentSetting.Justify
                if (horizontalTextAlignment == HorizontalTextAlignmentSetting.Justify)
                {
                    for (int i = 0; i < row.Length; i++)
                    {
                        if (row[i] == PixelFont.SpaceASCII)
                        {
                            numSpaces++;
                        }
                        //else
                        //widthTextCharacters += font.GetGlyph(row[i]).width + font.GlyphSpacing;
                    }

                    extraSpaceToDistribute = maxWidth - rowWidth[r]; // +numSpaces * font.GetGlyph(PixelFont.SpaceASCII).width + font.GlyphSpacing;
                }

                xpos = (int)alignmentOffset;
                for (int i = 0; i < row.Length; i++)
                {
                    PixelFont.GlyphInfo glyph = font.GetGlyph(row[i]);
                    if (xpos + glyph.width > totalWidth)
                    {
                        break;
                    }

                    if (row[i] == PixelFont.SpaceASCII)
                    {
                        if (numSpaces > 1)
                        {
                            int currentPortionExtraSpaceToDistribute = (int)Mathf.Round((float)extraSpaceToDistribute / (float)numSpaces);
                            xpos += currentPortionExtraSpaceToDistribute;
                            extraSpaceToDistribute -= currentPortionExtraSpaceToDistribute;
                            numSpaces--;
                        }
                        else if (numSpaces == 1)
                        {
                            xpos += extraSpaceToDistribute;
                        }
                    }

                    labelTexture.SetPixels32(xpos, ypos, glyph.width, font.GlyphHeight, glyph.colors);
                    xpos += glyph.width + font.GlyphSpacing;
                }
                ypos -= font.GlyphHeight;
            }

            labelTexture.Apply(false, makeTextureNoLongerReadable);
            labelTexture.filterMode = font.FilterMode;
            this.Size = new Vector2(totalWidth * textScale, totalHeight * textScale);
        }
Exemplo n.º 16
0
        void CreateLabelTextureSingleLine()
        {
            if (font == null)
            {
                font = DaggerfallUI.DefaultFont;
            }

            // set a local maxWidth that compensates for textScale
            int maxWidth = (int)(this.maxWidth / textScale);

            // First pass encodes ASCII and calculates final dimensions
            int width = 0;

            asciiBytes = Encoding.ASCII.GetBytes(text);
            for (int i = startCharacterIndex; i < asciiBytes.Length; i++)
            {
                // Invalid ASCII bytes are cast to a space character
                if (!font.HasGlyph(asciiBytes[i]))
                {
                    asciiBytes[i] = PixelFont.SpaceASCII;
                }

                // Calculate total width
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                width += glyph.width + font.GlyphSpacing;
            }

            if (maxWidth > 0 && width > maxWidth)
            {
                width = maxWidth;
            }

            // Destroy old texture
            if (labelTexture)
            {
                UnityEngine.Object.Destroy(labelTexture);
            }

            // Create target label texture
            totalWidth   = width;
            totalHeight  = (int)(font.GlyphHeight);
            numTextLines = 1;
            labelTexture = CreateLabelTexture(totalWidth, totalHeight);
            if (labelTexture == null)
            {
                throw new Exception("TextLabel failed to create labelTexture.");
            }

            float alignmentOffset;

            switch (horizontalTextAlignment)
            {
            default:
            case HorizontalTextAlignmentSetting.None:
            case HorizontalTextAlignmentSetting.Left:
            case HorizontalTextAlignmentSetting.Justify:
                alignmentOffset = 0.0f;
                break;

            case HorizontalTextAlignmentSetting.Center:
                alignmentOffset = (totalWidth - width) * 0.5f;
                break;

            case HorizontalTextAlignmentSetting.Right:
                alignmentOffset = totalWidth - width;
                break;
            }

            // Second pass adds glyphs to label texture
            int xpos = (int)alignmentOffset;

            for (int i = startCharacterIndex; i < asciiBytes.Length; i++)
            {
                PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]);
                if (xpos + glyph.width >= totalWidth)
                {
                    break;
                }

                labelTexture.SetPixels32(xpos, 0, glyph.width, totalHeight, glyph.colors);
                xpos += glyph.width + font.GlyphSpacing;
            }
            labelTexture.Apply(false, makeTextureNoLongerReadable);
            labelTexture.filterMode = font.FilterMode;
            this.Size = new Vector2(totalWidth * textScale, totalHeight * textScale);
        }
Exemplo n.º 17
0
        public void AddItem(string text)
        {
            if (font == null)
                font = DaggerfallUI.DefaultFont;

            TextLabel textLabel = new TextLabel();
            textLabel.ScalingMode = Scaling.None;
            textLabel.HorizontalAlignment = rowAlignment;
            textLabel.Font = font;
            textLabel.MaxCharacters = maxCharacters;
            textLabel.Text = text;
            textLabel.Parent = this;
            listItems.Add(textLabel);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Sets the formatted text from a token array.
        /// </summary>
        /// <param name="tokens">Format token array.</param>
        public virtual void SetTextTokens(TextFile.Token[] tokens)
        {
            if (font == null)
                font = DaggerfallUI.DefaultFont;

            LayoutTextElements(tokens);
        }
Exemplo n.º 19
0
        public static TextLabel AddTextLabel(PixelFont font, Vector2 position, string text, Panel panel = null, int glyphSpacing = 1)
        {
            TextLabel textLabel = new TextLabel();
            textLabel.ScalingMode = Scaling.None;
            textLabel.Font = font;
            textLabel.Position = position;
            textLabel.Text = text;
            if (panel != null)
                panel.Components.Add(textLabel);

            return textLabel;
        }