Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Centering XY Text

Smithy Zeno
ownes 9 pet cacti
Join date: 3 Aug 2006
Posts: 52
04-12-2008 13:12
Hey all,
I was trying to make it so that when I type in a word or two, XY Text takes that word and centers it. For example, if I were to type in 'pie', it would normally show up as |pie |. But I want it to do something like this: | pie |. I started to work on this code:

CODE

string word;
integer length;
integer free_spaces;

default
{
state_entry()
{
word = "Hello!";
length = llStringLength(word);
free_spaces = 10 - length;
llSay(0, (string)free_spaces);
}
}

All this would tell me how many free spaces there are. But I need to know how to add a space to the beginning or the end of the word. If someone can help me, that'd be great.
_____________________
-Smithy
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
04-12-2008 14:20
I'm sure, someone will come up with a much more clever idea :)

I did it like this:

CODE

string word;
integer length;
integer free_spaces;
integer total_chars = 10; // Total chars in your XY-Text
default
{
state_entry()
{
word = "Hello!";
length = llStringLength(word);
free_spaces = (total_chars - length)/2;

integer i;

for(i=0;i<free_spaces;i++){
word = " " + word + " ";
}

// Now, free spaces/2 might be a float, e.g. 5/2 = 2.5, so we'd have to add an extra space on one side

if((free_spaces * 2) + length < total_chars)
word += " ";

llSay(0, (string)free_spaces);
}
}


didn't test this inworld, though... hope there aren't any typos :)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
04-12-2008 14:34
CODE
string word;
string spaces = " ";

default
{
state_entry()
{
word = "hi!";
integer a = 10 - llStringLength(word);
integer b = a/2;
word = llGetSubString(spaces,1,b) + word + llGetSubString(spaces,1,a-b);
//For odd numbered strings, the extra space will be at the end
//Just reverse b & a-b if you want it the opposite way
llOwnerSay(word);
}
}
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Smithy Zeno
ownes 9 pet cacti
Join date: 3 Aug 2006
Posts: 52
04-12-2008 15:10
Thanks a bunch Haruki and Jesse, you've helped me a ton! :D
_____________________
-Smithy
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
04-12-2008 16:30
I was just wondering how Jesse's script should work... tried in inworld and didn't get the expected result...

... until I figured, that I don't have greasemonkey working with safari, so HTML renders multiple spaces just as one...

If someone else is having the same problem, the trick is to make the space string 10 long(or whatever length your XY-Text is).

With x's this would look like:

string spaces = "xxxxxxxxxx"

Then, Jesse's solution is - as expected - way smoother than mine :)
Jesse Barnett
500,000 scoville units
Join date: 21 May 2006
Posts: 4,160
04-12-2008 17:43
From: Haruki Watanabe
I was just wondering how Jesse's script should work... tried in inworld and didn't get the expected result...

... until I figured, that I don't have greasemonkey working with safari, so HTML renders multiple spaces just as one...

If someone else is having the same problem, the trick is to make the space string 10 long(or whatever length your XY-Text is).

With x's this would look like:

string spaces = "xxxxxxxxxx"

grrrrrr! Always forget that LL STILL hasn't fixed the forums. Easiest thing to do is hit the QOUTE button and the code is formatted correctly there. And also, you only need 5(oops! You need 6 spaces) spaces in the string.

From: Haruki Watanabe
Then, Jesse's solution is - as expected - way smoother than mine :)

Been buggin me all afternoon, should still be a better way.
_____________________
I (who is a she not a he) reserve the right to exercise selective comprehension of the OP's question at anytime.
From: someone
I am still around, just no longer here. See you across the aisle. Hope LL burns in hell for archiving this forum
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
04-12-2008 23:05
*lol* yeah - we're all waiting for the BBCode to come back...

what about:

CODE

// I use x's here instead of spaces. Use as many spaces as your XY-Text has chars
string space = "xxxxxxxxxx";
string word = "hi!";

integer wordLength = llGetStringLength(word);
integer spaceLength = llGetStringLength(space);

// if the extra space should be added at the end, use this
integer pos = (spaceLength - wordLength)/2;

// if the extra space should be added at the beginning, use this
integer pos = llCeil((spaceLength - wordLength)/2);

word = llInsertString(llGetSubString(space, 0, wordLength - 1), pos, word);

// you could wrap that up into one command (which makes it less readable, though)

word = llInsertString(llGetSubString(space, 0, wordLength - 1), (spaceLength - wordLength)/2, word);



man - it's very early and I didn't have any sleep yet - don't even know if this is working :)