hedgewars/uChat.pas
changeset 990 dfa6a6fe1542
parent 988 cdff3f6f9b38
child 991 d17c71ee074e
equal deleted inserted replaced
989:49075d163d69 990:dfa6a6fe1542
    23 procedure AddChatString(s: shortstring);
    23 procedure AddChatString(s: shortstring);
    24 procedure DrawChat;
    24 procedure DrawChat;
    25 procedure KeyPressChat(Key: Longword);
    25 procedure KeyPressChat(Key: Longword);
    26 
    26 
    27 var UserNick: shortstring = '';
    27 var UserNick: shortstring = '';
    28 	ChatVisibleCount: Longword = 7;
    28 	showAll: boolean = true;
    29 
    29 
    30 implementation
    30 implementation
    31 uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys;
    31 uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys;
    32 
    32 
    33 const MaxStrIndex = 30;
    33 const MaxStrIndex = 27;
    34 
    34 
    35 type TChatLine = record
    35 type TChatLine = record
    36 		s: shortstring;
    36 		s: shortstring;
    37 		Time: Longword;
    37 		Time: Longword;
    38 		Tex: PTexture;
    38 		Tex: PTexture;
    43 	visibleCount: Longword = 0;
    43 	visibleCount: Longword = 0;
    44 	
    44 	
    45 	InputStr: TChatLine;
    45 	InputStr: TChatLine;
    46 	InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
    46 	InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
    47 
    47 
    48 procedure SetLine(var cl: TChatLine; str: shortstring);
    48 procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean);
    49 var strSurface, resSurface: PSDL_Surface;
    49 var strSurface, resSurface: PSDL_Surface;
    50     r: TSDL_Rect;
    50     r: TSDL_Rect;
    51     w, h: LongInt;
    51     w, h: LongInt;
    52 begin
    52 begin
    53 if cl.Tex <> nil then
    53 if cl.Tex <> nil then
    54 	FreeTexture(cl.Tex);
    54 	FreeTexture(cl.Tex);
       
    55 
       
    56 cl.s:= str;
       
    57 
       
    58 if isInput then str:= UserNick + '> ' + str + '_';
    55 
    59 
    56 TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h);
    60 TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h);
    57 
    61 
    58 resSurface:= SDL_CreateRGBSurface(0,
    62 resSurface:= SDL_CreateRGBSurface(0,
    59 		toPowerOf2(w + 2),
    63 		toPowerOf2(w + 2),
    69 strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF);
    73 strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF);
    70 SDL_UpperBlit(strSurface, nil, resSurface, nil);
    74 SDL_UpperBlit(strSurface, nil, resSurface, nil);
    71 
    75 
    72 SDL_FreeSurface(strSurface);
    76 SDL_FreeSurface(strSurface);
    73 
    77 
    74 cl.s:= str;
       
    75 cl.Time:= RealTicks + 12500;
    78 cl.Time:= RealTicks + 12500;
    76 cl.Tex:= Surface2Tex(resSurface);
    79 cl.Tex:= Surface2Tex(resSurface);
    77 SDL_FreeSurface(resSurface)
    80 SDL_FreeSurface(resSurface)
    78 end;
    81 end;
    79 
    82 
    80 procedure AddChatString(s: shortstring);
    83 procedure AddChatString(s: shortstring);
    81 begin
    84 begin
    82 lastStr:= (lastStr + 1) mod (MaxStrIndex + 1);
    85 lastStr:= (lastStr + 1) mod (MaxStrIndex + 1);
    83 
    86 
    84 SetLine(Strs[lastStr], s);
    87 SetLine(Strs[lastStr], s, false);
    85 
    88 
    86 inc(visibleCount)
    89 inc(visibleCount)
    87 end;
    90 end;
    88 
    91 
    89 procedure DrawChat;
    92 procedure DrawChat;
    90 var i, t, cnt: Longword;
    93 var i, t, cnt: Longword;
    91 begin
    94 begin
    92 cnt:= 0;
    95 cnt:= 0;
    93 t:= 0;
    96 t:= 0;
    94 i:= lastStr;
    97 i:= lastStr;
    95 while (t < ChatVisibleCount)
    98 while
    96 	and (Strs[i].Tex <> nil)
    99 	(
    97 	and (Strs[i].Time > RealTicks) do
   100 			((t < 7) and (Strs[i].Time > RealTicks))
       
   101 		or
       
   102 			((t < MaxStrIndex) and showAll)
       
   103 	)
       
   104 	and
       
   105 		(Strs[i].Tex <> nil) do
    98 	begin
   106 	begin
    99 	DrawTexture(8, (visibleCount - t) * 16 - 6, Strs[i].Tex);
   107 	DrawTexture(8, (visibleCount - t) * 16 - 6, Strs[i].Tex);
   100 	if i = 0 then i:= MaxStrIndex else dec(i);
   108 	if i = 0 then i:= MaxStrIndex else dec(i);
   101 	inc(cnt);
   109 	inc(cnt);
   102 	inc(t)
   110 	inc(t)
   117 if Key <> 0 then
   125 if Key <> 0 then
   118 	case Key of
   126 	case Key of
   119 		8: if Length(InputStr.s) > 0 then
   127 		8: if Length(InputStr.s) > 0 then
   120 				begin
   128 				begin
   121 				InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
   129 				InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
   122 				SetLine(InputStr, InputStr.s)
   130 				SetLine(InputStr, InputStr.s, true)
   123 				end;
   131 				end;
       
   132 		27: SetLine(InputStr, '', true);
   124 		13, 271: begin
   133 		13, 271: begin
   125 			if Length(InputStr.s) > 0 then
   134 			if Length(InputStr.s) > 0 then
   126 				begin
   135 				begin
   127 				ParseCommand('/say ' + InputStr.s, true);
   136 				ParseCommand('/say ' + InputStr.s, true);
   128 				SetLine(InputStr, '')
   137 				SetLine(InputStr, '', false)
   129 				end;
   138 				end;
   130 			FreezeEnterKey;
   139 			FreezeEnterKey;
   131 			GameState:= gsGame
   140 			GameState:= gsGame
   132 			end
   141 			end
   133 	else
   142 	else
   145 		end;
   154 		end;
   146 	
   155 	
   147 	utf8:= char(Key or firstByteMark[btw]) + utf8;
   156 	utf8:= char(Key or firstByteMark[btw]) + utf8;
   148 
   157 
   149 	InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
   158 	InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
   150 	SetLine(InputStr, InputStr.s + utf8)
   159 	SetLine(InputStr, InputStr.s + utf8, true)
   151 	end
   160 	end
   152 end;
   161 end;
   153 
   162 
   154 
   163 
   155 end.
   164 end.