Preparing to have gsChat gamestate
authorunc0rr
Sat, 24 May 2008 18:37:04 +0000
changeset 946 42c5cc87cbd1
parent 945 4ead9cde4e14
child 947 4e0c3ad89483
Preparing to have gsChat gamestate
hedgewars/CCHandlers.inc
hedgewars/hwengine.dpr
hedgewars/uChat.pas
hedgewars/uConsole.pas
hedgewars/uConsts.pas
hedgewars/uIO.pas
--- a/hedgewars/CCHandlers.inc	Sat May 24 17:34:06 2008 +0000
+++ b/hedgewars/CCHandlers.inc	Sat May 24 18:37:04 2008 +0000
@@ -522,3 +522,8 @@
 begin
 isSpeed:= false
 end;
+
+procedure chChat(var s: shortstring);
+begin
+GameState:= gsChat
+end;
--- a/hedgewars/hwengine.dpr	Sat May 24 17:34:06 2008 +0000
+++ b/hedgewars/hwengine.dpr	Sat May 24 18:37:04 2008 +0000
@@ -149,19 +149,7 @@
 repeat
 while SDL_PollEvent(@event) <> 0 do
       case event.type_ of
-           SDL_KEYDOWN: case GameState of
-                             gsGame: if event.key.keysym.sym = 96 then
-                                        begin
-                                        cConsoleYAdd:= cConsoleHeight;
-                                        GameState:= gsConsole
-                                        end;
-                          gsConsole: if event.key.keysym.sym = 96 then
-                                        begin
-                                        GameState:= gsGame;
-                                        cConsoleYAdd:= 0;
-                                        ResetKbd
-                                        end else KeyPressConsole(event.key.keysym.unicode);
-                             end;
+           SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode);
            SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
                                cHasFocus:= event.active.gain = 1;
            SDL_QUITEV: isTerminated:= true
--- 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.
--- a/hedgewars/uConsole.pas	Sat May 24 17:34:06 2008 +0000
+++ b/hedgewars/uConsole.pas	Sat May 24 18:37:04 2008 +0000
@@ -27,7 +27,6 @@
 procedure DrawConsole(Surface: PSDL_Surface);
 procedure WriteToConsole(s: shortstring);
 procedure WriteLnToConsole(s: shortstring);
-procedure KeyPressConsole(Key: Longword);
 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
 procedure StopMessages(Message: Longword);
 function  GetLastConsoleLine: shortstring;
@@ -57,8 +56,6 @@
 
 var   ConsoleLines: array[byte] of TTextLine;
       CurrLine: LongInt = 0;
-      InputStr: TTextLine;
-      InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
       Variables: PVariable = nil;
 
 procedure SetLine(var tl: TTextLine; str: shortstring);
@@ -151,9 +148,7 @@
         DrawSprite(sprConsoleBG, x * 256, cConsoleHeight - 256 - y * 256, 0);
 
 for y:= 0 to cConsoleHeight div Fontz[fnt16].Height do
-    DrawLine(ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], 4, cConsoleHeight - (y + 2) * (Fontz[fnt16].Height + 2));
-
-DrawLine(InputStr, 4, cConsoleHeight - Fontz[fnt16].Height - 2);
+    DrawLine(ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], 4, cConsoleHeight - (y + 1) * (Fontz[fnt16].Height + 2));
 
 glDisable(GL_BLEND);
 glDisable(GL_TEXTURE_2D);
@@ -249,68 +244,6 @@
      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
 end;
 
-procedure AutoComplete;
-var t: PVariable;
-    c: char;
-begin
-if InputStr.s[0] = #0 then exit;
-c:= InputStr.s[1];
-if c in ['/', '$'] then Delete(InputStr.s, 1, 1) else c:= #0;
-
-if InputStr.s[byte(InputStr.s[0])] = #32 then dec(InputStr.s[0]);
-t:= Variables;
-while t <> nil do
-      begin
-      if (c=#0) or ((t^.VType =  vtCommand) and (c='/'))or
-                   ((t^.VType <> vtCommand) and (c='$'))then
-         if copy(t^.Name, 1, Length(InputStr.s)) = InputStr.s then
-            begin
-            if t^.VType = vtCommand then SetLine(InputStr, '/' + t^.Name + ' ')
-                                    else SetLine(InputStr, '$' + t^.Name + ' ');
-            exit
-            end;
-      t:= t^.Next
-      end
-end;
-
-procedure KeyPressConsole(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;
-      9: AutoComplete;
- 13,271: begin
-         if InputStr.s[1] in ['/', '$'] then
-            ParseCommand(InputStr.s, false)
-         else
-            ParseCommand('/say ' + InputStr.s, false);
-         SetLine(InputStr, '')
-         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;
-
 function GetLastConsoleLine: shortstring;
 begin
 if CurrLine = 0 then GetLastConsoleLine:= ConsoleLines[Pred(cLinesCount)].s
@@ -355,6 +288,7 @@
 RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
 RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
 RegisterVariable('skip'    , vtCommand, @chSkip         , false);
+RegisterVariable('chat'    , vtCommand, @chChat         , true );
 RegisterVariable('say'     , vtCommand, @chSay          , true );
 RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
 RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
--- a/hedgewars/uConsts.pas	Sat May 24 17:34:06 2008 +0000
+++ b/hedgewars/uConsts.pas	Sat May 24 18:37:04 2008 +0000
@@ -23,7 +23,7 @@
 {$INCLUDE proto.inc}
 type
 
-     TGameState = (gsLandGen, gsStart, gsGame, gsConsole, gsExit);
+     TGameState = (gsLandGen, gsStart, gsGame, gsConsole, gsChat, gsExit);
 
      TGameType  = (gmtLocal, gmtDemo, gmtNet, gmtSave, gmtLandPreview);
 
--- a/hedgewars/uIO.pas	Sat May 24 17:34:06 2008 +0000
+++ b/hedgewars/uIO.pas	Sat May 24 18:37:04 2008 +0000
@@ -216,10 +216,9 @@
 begin
 while (headcmd <> nil) and (headcmd^.cmd = 's') do
       begin
-      s:= '> ' + copy(headcmd^.str, 2, Pred(headcmd^.len));
+      s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
       AddChatString(s);
       WriteLnToConsole(s);
-      //AddCaption('> ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), $FFFFFF, capgrpNetSay);
       RemoveCmd
       end;