hedgewars/uChat.pas
changeset 8742 b7b8bd398c1b
parent 8741 c98ec6865631
child 8743 0097855532f6
equal deleted inserted replaced
8741:c98ec6865631 8742:b7b8bd398c1b
    26 procedure freeModule;
    26 procedure freeModule;
    27 procedure ReloadLines;
    27 procedure ReloadLines;
    28 procedure CleanupInput;
    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;
   301     SDL_EnableKeyRepeat(0,0);
   301     SDL_EnableKeyRepeat(0,0);
   302     GameState:= gsGame;
   302     GameState:= gsGame;
   303     ResetKbd;
   303     ResetKbd;
   304 end;
   304 end;
   305 
   305 
   306 procedure KeyPressChat(Key: Longword);
   306 procedure KeyPressChat(Key, Sym: Longword);
   307 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   307 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   308 var i, btw, index: integer;
   308 var i, btw, index: integer;
   309     utf8: shortstring;
   309     utf8: shortstring;
   310 begin
   310     action: boolean;
   311     if Key <> 0 then
   311 begin
   312     case Key of
   312     action:= false;
   313         {Backspace}
   313     if Sym <> 0 then
   314         8, 127: if Length(InputStr.s) > 0 then
   314         case Sym of
       
   315             SDLK_BACKSPACE:
   315                 begin
   316                 begin
   316                 InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
   317                 action:= true;
   317                 SetLine(InputStr, InputStr.s, true)
   318                 if Length(InputStr.s) > 0 then
       
   319                     begin
       
   320                     InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
       
   321                     SetLine(InputStr, InputStr.s, true)
       
   322                     end
   318                 end;
   323                 end;
   319         {Esc}
   324             SDLK_ESCAPE: 
   320         27: if Length(InputStr.s) > 0 then SetLine(InputStr, '', true)
       
   321             else CleanupInput;
       
   322         {Return}
       
   323         3, 13, 271: begin
       
   324             if Length(InputStr.s) > 0 then
       
   325                 begin
   325                 begin
   326                 AcceptChatString(InputStr.s);
   326                 action:= true;
   327                 SetLine(InputStr, '', false)
   327                 if Length(InputStr.s) > 0 then 
       
   328                     SetLine(InputStr, '', true)
       
   329                 else CleanupInput
   328                 end;
   330                 end;
   329             CleanupInput;
   331             SDLK_RETURN:
       
   332                 begin
       
   333                 action:= true;
       
   334                 if Length(InputStr.s) > 0 then
       
   335                     begin
       
   336                     AcceptChatString(InputStr.s);
       
   337                     SetLine(InputStr, '', false)
       
   338                     end;
       
   339                 CleanupInput
       
   340                 end;
       
   341             SDLK_UP, SDLK_DOWN:
       
   342                 begin
       
   343                 action:= true;
       
   344                 if (Sym = SDLK_UP) and (history < localLastStr) then inc(history);
       
   345                 if (Sym = SDLK_DOWN) and (history > 0) then dec(history);
       
   346                 index:= localLastStr - history + 1;
       
   347                 if (index > localLastStr) then
       
   348                      SetLine(InputStr, '', true)
       
   349                 else SetLine(InputStr, LocalStrs[index], true)
       
   350                 end
       
   351         end;
       
   352     if not action and (Key <> 0) then
       
   353         begin
       
   354         if (Key < $80) then
       
   355             btw:= 1
       
   356         else if (Key < $800) then
       
   357             btw:= 2
       
   358         else if (Key < $10000) then
       
   359             btw:= 3
       
   360         else
       
   361             btw:= 4;
       
   362 
       
   363         utf8:= '';
       
   364 
       
   365         for i:= btw downto 2 do
       
   366             begin
       
   367             utf8:= char((Key or $80) and $BF) + utf8;
       
   368             Key:= Key shr 6
   330             end;
   369             end;
   331         {arrow keys (up, down)}
   370 
   332         111, 116,     //linux
   371         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   333         63232, 63233: //osx
   372 
   334             begin
   373         if byte(InputStr.s[0]) + btw > 240 then
   335 
   374             exit;
   336             if ((Key = 63232) or (Key = 111)) and (history < localLastStr) then inc(history);
   375 
   337             if ((Key = 63233) or (Key = 116)) and (history > 0) then dec(history);
   376         InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
   338 
   377         SetLine(InputStr, InputStr.s + utf8, true)
   339             index:= localLastStr - history + 1;
       
   340             if (index > localLastStr) then
       
   341                 SetLine(InputStr, '', true)
       
   342             else
       
   343                 SetLine(InputStr, LocalStrs[index], true);
       
   344             end;
       
   345         {arrow keys (left, right)}
       
   346         63234, 63235: begin end;
       
   347         else
       
   348             if (Key < $80) then
       
   349                 btw:= 1
       
   350             else if (Key < $800) then
       
   351                 btw:= 2
       
   352             else if (Key < $10000) then
       
   353                 btw:= 3
       
   354             else
       
   355                 btw:= 4;
       
   356 
       
   357             utf8:= '';
       
   358 
       
   359             for i:= btw downto 2 do
       
   360                 begin
       
   361                 utf8:= char((Key or $80) and $BF) + utf8;
       
   362                 Key:= Key shr 6
       
   363                 end;
       
   364 
       
   365             utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
       
   366 
       
   367             if byte(InputStr.s[0]) + btw > 240 then
       
   368                 exit;
       
   369 
       
   370             InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
       
   371             SetLine(InputStr, InputStr.s + utf8, true)
       
   372         end
   378         end
   373 end;
   379 end;
   374 
   380 
   375 procedure chChatMessage(var s: shortstring);
   381 procedure chChatMessage(var s: shortstring);
   376 begin
   382 begin