hedgewars/uChat.pas
changeset 946 42c5cc87cbd1
parent 945 4ead9cde4e14
child 947 4e0c3ad89483
--- a/hedgewars/uChat.pas	Sat May 24 17:34:06 2008 +0000
+++ b/hedgewars/uChat.pas	Sat May 24 18:37:04 2008 +0000
@@ -22,29 +22,36 @@
 
 procedure AddChatString(s: shortstring);
 procedure DrawChat;
+procedure KeyPressChat(Key: Longword);
 
 implementation
 uses uMisc, uStore, uConsts, SDLh;
 
 const MaxStrIndex = 7;
 
-type TStr = record
+type TChatLine = record
+		s: shortstring;
 		Time: Longword;
 		Tex: PTexture;
 		end;
 
-var Strs: array[0 .. MaxStrIndex] of TStr;
+
+var Strs: array[0 .. MaxStrIndex] of TChatLine;
 	lastStr: Longword = 0;
 	visibleCount: Longword = 0;
+	
+	InputStr: TChatLine;
+	InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
 
-procedure AddChatString(s: shortstring);
+procedure SetLine(var cl: TChatLine; str: shortstring);
 var strSurface, resSurface: PSDL_Surface;
     r: TSDL_Rect;
     w, h: LongInt;
 begin
-lastStr:= (lastStr + 1) mod (MaxStrIndex + 1);
+if cl.Tex <> nil then
+	FreeTexture(cl.Tex);
 
-TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(s), w, h);
+TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h);
 
 resSurface:= SDL_CreateRGBSurface(0,
 		toPowerOf2(w + 2),
@@ -52,20 +59,27 @@
 		32,
 		RMask, GMask, BMask, AMask);
 
-strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(s), $202020);
+strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $202020);
 r.x:= 1;
 r.y:= 1;
 SDL_UpperBlit(strSurface, nil, resSurface, @r);
 
-strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(s), $FFFFFF);
+strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF);
 SDL_UpperBlit(strSurface, nil, resSurface, nil);
 
 SDL_FreeSurface(strSurface);
 
 
-Strs[lastStr].Time:= RealTicks + 7500;
-Strs[lastStr].Tex:= Surface2Tex(resSurface);
-SDL_FreeSurface(resSurface);
+cl.Time:= RealTicks + 7500;
+cl.Tex:= Surface2Tex(resSurface);
+SDL_FreeSurface(resSurface)
+end;
+
+procedure AddChatString(s: shortstring);
+begin
+lastStr:= (lastStr + 1) mod (MaxStrIndex + 1);
+
+SetLine(Strs[lastStr], s);
 
 inc(visibleCount)
 end;
@@ -80,7 +94,7 @@
 	and (Strs[i].Tex <> nil)
 	and (Strs[i].Time > RealTicks) do
 	begin
-	DrawTexture(8, (visibleCount - t) * 16 - 8, Strs[i].Tex);
+	DrawTexture(8, (visibleCount - t) * 16 - 6 + cConsoleYAdd, Strs[i].Tex);
 	if i = 0 then i:= MaxStrIndex else dec(i);
 	inc(cnt);
 	inc(t)
@@ -89,4 +103,43 @@
 visibleCount:= cnt
 end;
 
+procedure KeyPressChat(Key: Longword);
+const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0);
+var i, btw: integer;
+    utf8: shortstring;
+begin
+if Key <> 0 then
+	case Key of
+		8: if Length(InputStr.s) > 0 then
+				begin
+				InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
+				SetLine(InputStr, InputStr.s)
+				end;
+		13,271: begin
+			AddChatString(InputStr.s);
+			SetLine(InputStr, '');
+			GameState:= gsGame
+			end
+	else
+	if (Key < $80) then btw:= 1
+	else if (Key < $800) then btw:= 2
+	else if (Key < $10000) then btw:= 3
+	else btw:= 4;
+	
+	utf8:= '';
+
+	for i:= btw downto 2 do
+		begin
+		utf8:= char((Key or $80) and $BF) + utf8;
+		Key:= Key shr 6
+		end;
+	
+	utf8:= char(Key or firstByteMark[btw]) + utf8;
+
+	InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
+	SetLine(InputStr, InputStr.s + utf8)
+	end
+end;
+
+
 end.