[Help] Hide part of drawing text

OK so basically I’m trying to create a scrolling text, but I need it to stay within a certain rectangle, so whenever the text gets outside of the region, that part needs to get hidden.

This is the code I have right now, but the text should only be visible between x:5 and x:203

private void DrawRadio()
{
    var stationId = Function.Call<int>(Hash.GET_PLAYER_RADIO_STATION_INDEX);
    Radio.Caption = ((RadioStation)stationId).ToString().ToSpaceCase();
            
    RadioPosition.X -= 20f * Game.LastFrameTime;
    if(RadioPosition.X < 5 - Radio.ScaledWidth)
    {
        RadioPosition.X = 203;
    }
    Radio.Position = RadioPosition;
            
    Radio.ScaledDraw();
}

Anyone know if this is possible? if so, how?

Thanks in advance.

I don’t think there’s a default native that can do this. However this is a snippet in Lua I’ve just made that seems to create a scrolling text effect. It’s not the most pretty solution out there for sure but it does what it’s supposed to do.

You can view a preview of it here:
Video Preview
(note that the numbers show above the scrolling text are just there for demonstration purposes, they won’t be there if yo use the example code from the file above)
If the link above doesn’t work, copy it from here: https://www.devtesting.pizza/hi/i/01ed834.mp4

Thanks! I kind of not wanted to result to the removing / adding of characters method, but that script did gave me some inspiration on how to achive this, since my surrounding container is way wider than the text I had to do it pixel based, but I’ve got something that works now, Thanks!

Mind sharing what you ended up with, interested to see what you’ve come up with :slight_smile:

It’s probably far from perfect, but it suits my needs for now :slight_smile:

class TextScroll
{
    private string tempText;
    public string Text {
        get { return text.Caption; }
        set {
            if (tempText != value)
            {
                tempText = value;
                reset = false;
                var temp = new Text(value, PointF.Empty, scale);
                textPos.X = (container.Size.Width / 2) - (temp.Width / 2);
                text.Caption = tempText;
            }
        }
    }

    Container container;
    Text text;
    Text currentCharacter;

    PointF textPos;
    float speed;
    float scale;
    private bool reset;

    public TextScroll(string message, PointF position, SizeF size, PointF textOffset, float scale, float speed = 20f)
    {
        this.speed = speed;
        this.scale = scale;
        tempText = message;
        container = new Container(position, size, Color.FromArgb(255, 0, 0, 0));
        currentCharacter = new Text("", PointF.Empty, scale);

        textPos = textOffset;

        text = new Text(message, textPos, scale);

        container.Items.Add(text);
    }

    public void Draw()
    {
        float charWidth = 0;

        if (text.Caption.Length > 0)
        {
            currentCharacter.Caption = text.Caption[0].ToString();
            charWidth = currentCharacter.Width;
        }

        textPos.X -= speed * Game.LastFrameTime;

        if (textPos.X < 0)
        {
            // remove one character
            if (text.Caption.Length > 0)
            {
                text.Caption = text.Caption.Substring(1);
            }

            textPos.X += charWidth / 2;
        }

        if (textPos.X < -text.Width)
        {
            textPos.X = container.Size.Width;
            reset = true;
        }
            
        if (text.Caption.Length < tempText.Length && reset)
        {
            if (text.Caption.Length >= 0)
            {
                currentCharacter.Caption = tempText[text.Caption.Length].ToString();
                charWidth = currentCharacter.Width;
            }

            if (textPos.X + text.Width < container.Size.Width - (charWidth / 2))
            {
                currentCharacter.Caption = tempText[text.Caption.Length].ToString();
                charWidth = currentCharacter.Width;

                text.Caption += tempText[text.Caption.Length];

                if (text.Caption.Length == tempText.Length)
                {
                    reset = false;
                }
            }
        }
        text.Position = textPos;

        container.ScaledDraw();
    }
}
1 Like