AviiNL
January 9, 2018, 6:58am
1
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.
README.md
# Scrolling text
**Made by Vespura**
Note that this is not the best way to acchieve this effect. Rather it's a simple solution that works.
You can view a preview of it here: ![](https://www.devtesting.pizza/hi/i/01ed834.mp4)
(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
client.lua
-----[[ SCROLLING TEXT EXAMPLE BY VESPURA ]]
-----[[ NOTE THIS IS NOT THE MOST EFFICIENT ]]
-----[[ WAY TO CREATE THIS EFFECT, BUT IT ]]
-----[[ SEEMS TO WORK ALRIGHT FOR NOW. ]]
--[[CONFIG PART]]
-- Enter your message here
local message = "Hello world this is a test message."
-- Enter the amount/length of characters to be shown at a time.
This file has been truncated. show original
You can view a preview of it here:
(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
AviiNL
January 9, 2018, 8:44pm
3
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
AviiNL
January 9, 2018, 9:15pm
5
It’s probably far from perfect, but it suits my needs for now
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