Author Topic: DrawTextCenter  (Read 3287 times)

0 Members and 1 Guest are viewing this topic.

Offline J-Factor

  • Major(1)
  • Posts: 27
DrawTextCenter
« on: July 27, 2007, 10:55:39 am »
Script Name: DrawTextCenter
Script Description: Just like DrawText but automatically centers the text.
Original Author(s): Me
Core Version: 2.6.1
Code:
In order to center the text I used a script to find the width of each character in the default font (Bank Gothic Lt BT) and stored it in a global array called TextWidth.
Code: [Select]
var
TextWidth : Array[32..127] of Single;

procedure ActivateServer();
begin
TextWidth[32] := 103.20;
TextWidth[33] := 114.67;
TextWidth[34] := 74.53;
TextWidth[35] := 5.73;
TextWidth[36] := 206.40;
TextWidth[37] := 235.07;
TextWidth[38] := 212.13;
TextWidth[39] := 40.13;
TextWidth[40] := 103.20;
TextWidth[41] := 103.20;
TextWidth[42] := 137.60;
TextWidth[43] := 229.33;
TextWidth[44] := 103.20;
TextWidth[45] := 91.73;
TextWidth[46] := 103.20;
TextWidth[47] := 97.47;
TextWidth[48] := 206.40;
TextWidth[49] := 206.40;
TextWidth[50] := 206.40;
TextWidth[51] := 206.40;
TextWidth[52] := 206.40;
TextWidth[53] := 206.40;
TextWidth[54] := 206.40;
TextWidth[55] := 206.40;
TextWidth[56] := 206.40;
TextWidth[57] := 206.40;
TextWidth[58] := 103.20;
TextWidth[59] := 103.20;
TextWidth[60] := 229.33;
TextWidth[61] := 229.33;
TextWidth[62] := 229.33;
TextWidth[63] := 177.73;
TextWidth[64] := 275.20;
TextWidth[65] := 200.67;
TextWidth[66] := 212.13;
TextWidth[67] := 206.40;
TextWidth[68] := 223.60;
TextWidth[69] := 194.93;
TextWidth[70] := 177.73;
TextWidth[71] := 223.60;
TextWidth[72] := 235.07;
TextWidth[73] := 97.47;
TextWidth[74] := 183.47;
TextWidth[75] := 200.67;
TextWidth[76] := 177.73;
TextWidth[77] := 246.53;
TextWidth[78] := 229.33;
TextWidth[79] := 235.07;
TextWidth[80] := 200.67;
TextWidth[81] := 235.07;
TextWidth[82] := 212.13;
TextWidth[83] := 212.13;
TextWidth[84] := 160.53;
TextWidth[85] := 223.60;
TextWidth[86] := 189.20;
TextWidth[87] := 235.07;
TextWidth[88] := 194.93;
TextWidth[89] := 183.47;
TextWidth[90] := 189.20;
TextWidth[91] := 103.20;
TextWidth[92] := 97.47;
TextWidth[93] := 103.20;
TextWidth[94] := 275.20;
TextWidth[95] := 137.60;
TextWidth[96] := 137.60;
TextWidth[97] := 172;
TextWidth[98] := 172;
TextWidth[99] := 172;
TextWidth[100] := 200.67;
TextWidth[101] := 160.53;
TextWidth[102] := 149.07;
TextWidth[103] := 183.47;
TextWidth[104] := 189.20;
TextWidth[105] := 74.53;
TextWidth[106] := 154.80;
TextWidth[107] := 166.27;
TextWidth[108] := 149.07;
TextWidth[109] := 206.40;
TextWidth[110] := 189.20;
TextWidth[111] := 189.20;
TextWidth[112] := 166.27;
TextWidth[113] := 189.20;
TextWidth[114] := 166.27;
TextWidth[115] := 177.73;
TextWidth[116] := 143.33;
TextWidth[117] := 183.47;
TextWidth[118] := 160.53;
TextWidth[119] := 200.67;
TextWidth[120] := 160.53;
TextWidth[121] := 154.80;
TextWidth[122] := 172;
TextWidth[123] := 137.60;
TextWidth[124] := 137.60;
TextWidth[125] := 137.60;
TextWidth[126] := 229.33;
TextWidth[127] := 166.27;
end;

procedure DrawTextCenter(ID : Byte; Text : String; Delay : Integer; Color : Longint; Scale : Single);
var
i, c : Byte;
TotalWidth, Width, Height : Single;
begin
TotalWidth := 0;
Width := 0;
Height := 202;

for i := 1 to length(Text) do
begin
c := ord(Text[i]);

if ((c > 31) and (c < 128)) then
begin
Width := Width + TextWidth[c];
if (Width > TotalWidth) then TotalWidth := Width;
end
else
if (c = 13) then
begin
Height := Height + 202;
Width := 0;
end;
end;

DrawText(ID, Text, Delay, Color, Scale, 320 - Round(TotalWidth*Scale/2), 240 - Round(Height*Scale/2));
end;

Example:
Code: [Select]
DrawTextCenter(ID, 'nob', 300, $FFFF0000, 1);
Edited into a single script.
« Last Edit: July 27, 2007, 11:00:41 am by J-Factor »

Offline Master Chief43

  • Soldier
  • **
  • Posts: 224
  • When life gives you lemons, eat them :D
Re: DrawTextCenter
« Reply #1 on: July 27, 2007, 12:42:06 pm »
Um... Do you mean center of the screen? I think it can already do that... Just add some spaces... But I guess this could be more useful...
In-Game Name: "Master Chief43"
Favorite Map: ctf_Ash
Favorite Mods: Special Operation 2.0, Original Soldier 1.9
Favorite Guns: [MP5][AK74][socom][Barret]

Offline J-Factor

  • Major(1)
  • Posts: 27
Re: DrawTextCenter
« Reply #2 on: July 27, 2007, 12:49:37 pm »
Quote
Um... Do you mean center of the screen? I think it can already do that... Just add some spaces... But I guess this could be more useful...

I wrote this script for dynamic messages that vary in length. For example the names of player classes in my script range from extremely small ("Spy") to much longer ("Magician") - adding a few static spaces isn't going to center these two messages.

Plus it's a lot easier than second guessing a few extra spaces, more precise and flexible (if you used spaces and you changed the text you'd have to modify the number of spaces).

Offline Iq Unlimited

  • Flagrunner
  • ****
  • Posts: 864
  • mr. foobar2000
Re: DrawTextCenter
« Reply #3 on: July 27, 2007, 01:54:48 pm »
Yeah, this is a huge script that has no real point :P.

Iq Unlimited

Offline ManSoft|Warlord

  • Major
  • *
  • Posts: 70
  • Soldat.IDE Creator
Re: DrawTextCenter
« Reply #4 on: January 12, 2008, 09:43:59 am »
nice script - exactly what i need :)

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: DrawTextCenter
« Reply #5 on: January 15, 2008, 02:54:47 pm »
I like the idea but, to be honest, I don't like the script. Use only one functions and no globals and get rid of that HUGE list and I may just use it
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai