hedgewars/uChat.pas
branchwebgl
changeset 8833 c13ebed437cb
parent 8330 aaefa587e277
parent 8745 b3756c3f65e5
child 8850 ae8a957c69fd
equal deleted inserted replaced
8450:404ddce27b23 8833:c13ebed437cb
    23 interface
    23 interface
    24 
    24 
    25 procedure initModule;
    25 procedure initModule;
    26 procedure freeModule;
    26 procedure freeModule;
    27 procedure ReloadLines;
    27 procedure ReloadLines;
    28 
    28 procedure CleanupInput;
    29 procedure AddChatString(s: shortstring);
    29 procedure AddChatString(s: shortstring);
    30 procedure DrawChat;
    30 procedure DrawChat;
    31 procedure KeyPressChat(Key: Longword);
    31 procedure KeyPressChat(Key, Sym: Longword);
    32 
    32 
    33 implementation
    33 implementation
    34 uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO;
    34 uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO;
    35 
    35 
    36 const MaxStrIndex = 27;
    36 const MaxStrIndex = 27;
    43     end;
    43     end;
    44     TChatCmd = (quit, pause, finish, fullscreen);
    44     TChatCmd = (quit, pause, finish, fullscreen);
    45 
    45 
    46 var Strs: array[0 .. MaxStrIndex] of TChatLine;
    46 var Strs: array[0 .. MaxStrIndex] of TChatLine;
    47     MStrs: array[0 .. MaxStrIndex] of shortstring;
    47     MStrs: array[0 .. MaxStrIndex] of shortstring;
       
    48     LocalStrs: array[0 .. MaxStrIndex] of shortstring;
    48     missedCount: LongWord;
    49     missedCount: LongWord;
    49     lastStr: LongWord;
    50     lastStr: LongWord;
       
    51     localLastStr: LongWord;
       
    52     history: LongWord;
    50     visibleCount: LongWord;
    53     visibleCount: LongWord;
    51     InputStr: TChatLine;
    54     InputStr: TChatLine;
    52     InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
    55     InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
    53     ChatReady: boolean;
    56     ChatReady: boolean;
    54     showAll: boolean;
    57     showAll: boolean;
   289     end
   292     end
   290     else
   293     else
   291         ParseCommand('/say ' + s, true);
   294         ParseCommand('/say ' + s, true);
   292 end;
   295 end;
   293 
   296 
   294 procedure KeyPressChat(Key: Longword);
   297 procedure CleanupInput;
       
   298 begin
       
   299     FreezeEnterKey;
       
   300     history:= 0;
       
   301     SDL_EnableKeyRepeat(0,0);
       
   302     GameState:= gsGame;
       
   303     ResetKbd;
       
   304 end;
       
   305 
       
   306 procedure KeyPressChat(Key, Sym: Longword);
   295 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   307 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   296 var i, btw: integer;
   308 var i, btw, index: integer;
   297     utf8: shortstring;
   309     utf8: shortstring;
   298 begin
   310     action: boolean;
   299 if Key <> 0 then
   311 begin
   300     case Key of
   312     action:= true;
   301         {Backspace}
   313     case Sym of
   302         8, 127: if Length(InputStr.s) > 0 then
   314         SDLK_BACKSPACE:
       
   315             begin
       
   316             if Length(InputStr.s) > 0 then
   303                 begin
   317                 begin
   304                 InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
   318                 InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
   305                 SetLine(InputStr, InputStr.s, true)
   319                 SetLine(InputStr, InputStr.s, true)
   306                 end;
   320                 end
   307         {Esc}
   321             end;
   308         27: if Length(InputStr.s) > 0 then SetLine(InputStr, '', true)
   322         SDLK_ESCAPE:
   309             else
   323             begin
   310                 begin
   324             if Length(InputStr.s) > 0 then
   311                 FreezeEnterKey;
   325                 SetLine(InputStr, '', true)
   312                 SDL_EnableKeyRepeat(0,0);
   326             else CleanupInput
   313                 GameState:= gsGame;
   327             end;
   314                 ResetKbd;
   328         SDLK_RETURN:
   315                 end;
   329             begin
   316         {Return}
       
   317         3, 13, 271: begin
       
   318             if Length(InputStr.s) > 0 then
   330             if Length(InputStr.s) > 0 then
   319                 begin
   331                 begin
   320                 AcceptChatString(InputStr.s);
   332                 AcceptChatString(InputStr.s);
   321                 SetLine(InputStr, '', false)
   333                 SetLine(InputStr, '', false)
   322                 end;
   334                 end;
   323             FreezeEnterKey;
   335             CleanupInput
   324             SDL_EnableKeyRepeat(0,0);
   336             end;
   325             GameState:= gsGame;
   337         SDLK_UP, SDLK_DOWN:
   326             ResetKbd;
   338             begin
   327             end;
   339             if (Sym = SDLK_UP) and (history < localLastStr) then inc(history);
   328     else
   340             if (Sym = SDLK_DOWN) and (history > 0) then dec(history);
       
   341             index:= localLastStr - history + 1;
       
   342             if (index > localLastStr) then
       
   343                  SetLine(InputStr, '', true)
       
   344             else SetLine(InputStr, LocalStrs[index], true)
       
   345             end;
       
   346         SDLK_RIGHT, SDLK_LEFT, SDLK_DELETE,
       
   347         SDLK_HOME, SDLK_END,
       
   348         SDLK_PAGEUP, SDLK_PAGEDOWN:
       
   349             begin
       
   350             // ignore me!!!
       
   351             end;
       
   352         else
       
   353             action:= false;
       
   354         end;
       
   355     if not action and (Key <> 0) then
       
   356         begin
   329         if (Key < $80) then
   357         if (Key < $80) then
   330             btw:= 1
   358             btw:= 1
   331         else if (Key < $800) then
   359         else if (Key < $800) then
   332             btw:= 2
   360             btw:= 2
   333         else if (Key < $10000) then
   361         else if (Key < $10000) then
   334             btw:= 3
   362             btw:= 3
   335         else
   363         else
   336             btw:= 4;
   364             btw:= 4;
   337 
   365 
   338     utf8:= '';
   366         utf8:= '';
   339 
   367 
   340     for i:= btw downto 2 do
   368         for i:= btw downto 2 do
   341         begin
   369             begin
   342         utf8:= char((Key or $80) and $BF) + utf8;
   370             utf8:= char((Key or $80) and $BF) + utf8;
   343         Key:= Key shr 6
   371             Key:= Key shr 6
   344         end;
   372             end;
   345 
   373 
   346     utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   374         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   347 
   375 
   348     if byte(InputStr.s[0]) + btw > 240 then
   376         if byte(InputStr.s[0]) + btw > 240 then
   349         exit;
   377             exit;
   350 
   378 
   351     InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
   379         InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
   352     SetLine(InputStr, InputStr.s + utf8, true)
   380         SetLine(InputStr, InputStr.s + utf8, true)
   353     end
   381         end
   354 end;
   382 end;
   355 
   383 
   356 procedure chChatMessage(var s: shortstring);
   384 procedure chChatMessage(var s: shortstring);
   357 begin
   385 begin
   358     AddChatString(s)
   386     AddChatString(s)
   363     SendIPC('s' + s);
   391     SendIPC('s' + s);
   364 
   392 
   365     if copy(s, 1, 4) = '/me ' then
   393     if copy(s, 1, 4) = '/me ' then
   366         s:= #2 + '* ' + UserNick + ' ' + copy(s, 5, Length(s) - 4)
   394         s:= #2 + '* ' + UserNick + ' ' + copy(s, 5, Length(s) - 4)
   367     else
   395     else
       
   396         begin
       
   397         localLastStr:= (localLastStr + 1) mod MaxStrIndex;
       
   398         LocalStrs[localLastStr]:= s;
   368         s:= #1 + UserNick + ': ' + s;
   399         s:= #1 + UserNick + ': ' + s;
       
   400         end;
   369 
   401 
   370     AddChatString(s)
   402     AddChatString(s)
   371 end;
   403 end;
   372 
   404 
   373 procedure chTeamSay(var s: shortstring);
   405 procedure chTeamSay(var s: shortstring);
   391     GameState:= gsChat;
   423     GameState:= gsChat;
   392     SDL_EnableKeyRepeat(200,45);
   424     SDL_EnableKeyRepeat(200,45);
   393     if length(s) = 0 then
   425     if length(s) = 0 then
   394         SetLine(InputStr, '', true)
   426         SetLine(InputStr, '', true)
   395     else
   427     else
   396         begin
   428         SetLine(InputStr, '/team ', true)
   397         // err, does anyone have any documentation on this sequence?
       
   398         // ^^ isn't it obvious? 27 is esc, 32 is space, inbetween is "/team"
       
   399         KeyPressChat(27);
       
   400         KeyPressChat(47);
       
   401         KeyPressChat(116);
       
   402         KeyPressChat(101);
       
   403         KeyPressChat(97);
       
   404         KeyPressChat(109);
       
   405         KeyPressChat(32)
       
   406         end
       
   407 end;
   429 end;
   408 
   430 
   409 procedure initModule;
   431 procedure initModule;
   410 var i: ShortInt;
   432 var i: ShortInt;
   411 begin
   433 begin
   414     RegisterVariable('team', @chTeamSay, true);
   436     RegisterVariable('team', @chTeamSay, true);
   415     RegisterVariable('history', @chHistory, true );
   437     RegisterVariable('history', @chHistory, true );
   416     RegisterVariable('chat', @chChat, true );
   438     RegisterVariable('chat', @chChat, true );
   417 
   439 
   418     lastStr:= 0;
   440     lastStr:= 0;
       
   441     localLastStr:= 0;
       
   442     history:= 0;
   419     visibleCount:= 0;
   443     visibleCount:= 0;
   420     showAll:= false;
   444     showAll:= false;
   421     ChatReady:= false;
   445     ChatReady:= false;
   422     missedCount:= 0;
   446     missedCount:= 0;
   423 
   447