hedgewars/uChat.pas
branchsdl2transition
changeset 11365 b8b208501475
parent 11364 b0df3f2fcafc
child 11372 208bc571f949
equal deleted inserted replaced
11364:b0df3f2fcafc 11365:b8b208501475
    27 procedure freeModule;
    27 procedure freeModule;
    28 procedure ReloadLines;
    28 procedure ReloadLines;
    29 procedure CleanupInput;
    29 procedure CleanupInput;
    30 procedure AddChatString(s: shortstring);
    30 procedure AddChatString(s: shortstring);
    31 procedure DrawChat;
    31 procedure DrawChat;
    32 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
    32 procedure KeyPressChat(keysym: TSDL_Keysym);
    33 procedure SendHogSpeech(s: shortstring);
    33 procedure SendHogSpeech(s: shortstring);
    34 procedure CopyToClipboard(var newContent: shortstring);
    34 procedure CopyToClipboard(var newContent: shortstring);
    35 
       
    36 procedure TextInput(var event: TSDL_TextInputEvent);
    35 procedure TextInput(var event: TSDL_TextInputEvent);
    37 
    36 
    38 implementation
    37 implementation
    39 uses uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO, uScript, uRenderUtils;
    38 uses uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO, uScript, uRenderUtils;
    40 
    39 
   570 procedure CleanupInput;
   569 procedure CleanupInput;
   571 begin
   570 begin
   572     FreezeEnterKey;
   571     FreezeEnterKey;
   573     history:= 0;
   572     history:= 0;
   574     SDL_StopTextInput();
   573     SDL_StopTextInput();
       
   574     //SDL_EnableKeyRepeat(0,0);
   575     GameState:= gsGame;
   575     GameState:= gsGame;
   576     ResetKbd;
   576     ResetKbd;
   577 end;
   577 end;
   578 
   578 
   579 procedure DelBytesFromInputStrBack(endIdx: integer; count: byte);
   579 procedure DelBytesFromInputStrBack(endIdx: integer; count: byte);
   778         inc(cursorPos, Length(s));
   778         inc(cursorPos, Length(s));
   779         UpdateCursorCoords();
   779         UpdateCursorCoords();
   780         end;
   780         end;
   781 end;
   781 end;
   782 
   782 
   783 procedure TextInput(var event: TSDL_TextInputEvent);
       
   784 var s: shortstring;
       
   785     l: byte;
       
   786 begin
       
   787     DeleteSelected();
       
   788 
       
   789     l:= 0;
       
   790     while event.text[l] <> #0 do
       
   791         begin
       
   792         s[l + 1]:= event.text[l];
       
   793         inc(l)
       
   794         end;
       
   795     s[0]:= char(l);
       
   796 
       
   797     if byte(InputStr.s[0]) + l > 240 then exit;
       
   798 
       
   799     InsertIntoInputStr(s);
       
   800 end;
       
   801 
       
   802 procedure PasteFromClipboard();
   783 procedure PasteFromClipboard();
   803 var clip: PChar;
   784 var clip: PChar;
   804 begin
   785 begin
   805     // use SDL2 clipboard functions
   786     // use SDL2 clipboard functions
   806     if SDL_HasClipboardText() then
   787     if SDL_HasClipboardText() then
   813             SDL_free(Pointer(clip));
   794             SDL_free(Pointer(clip));
   814             end;
   795             end;
   815         end;
   796         end;
   816 end;
   797 end;
   817 
   798 
   818 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
   799 procedure KeyPressChat(keysym: TSDL_Keysym);
   819 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   800 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   820       nonStateMask = (not (KMOD_NUM or KMOD_CAPS));
   801       nonStateMask = (not (KMOD_NUM or KMOD_CAPS));
   821 var i, btw, index: integer;
   802 var i, btw, index: integer;
   822     utf8: shortstring;
   803     utf8: shortstring;
   823     action, selMode, ctrl, ctrlonly: boolean;
   804     action, selMode, ctrl, ctrlonly: boolean;
   824     skip: TCharSkip;
   805     skip: TCharSkip;
   825 begin
   806     Scancode: TSDL_Scancode;
       
   807     Sym: TSDL_Keycode;
       
   808     Modifier: Word;
       
   809 begin
       
   810     Scancode:= keysym.scancode;
       
   811     Sym:= keysym.sym;
       
   812     Modifier:= keysym.modifier;
       
   813 
   826     LastKeyPressTick:= RealTicks;
   814     LastKeyPressTick:= RealTicks;
   827     action:= true;
   815     action:= true;
   828 
   816 
   829     selMode:= (modifier and (KMOD_LSHIFT or KMOD_RSHIFT)) <> 0;
   817     selMode:= (modifier and (KMOD_LSHIFT or KMOD_RSHIFT)) <> 0;
   830     ctrl:= (modifier and (KMOD_LCTRL or KMOD_RCTRL)) <> 0;
   818     ctrl:= (modifier and (KMOD_LCTRL or KMOD_RCTRL)) <> 0;
   831     ctrlonly:= ctrl and ((modifier and nonStateMask and (not (KMOD_LCTRL or KMOD_RCTRL))) = 0);
   819     ctrlonly:= ctrl and ((modifier and nonStateMask and (not (KMOD_LCTRL or KMOD_RCTRL))) = 0);
   832     skip:= none;
   820     skip:= none;
   833 
   821 
   834     case Sym of
   822     case Scancode of
   835         SDLK_BACKSPACE:
   823         SDL_SCANCODE_BACKSPACE:
   836             begin
   824             begin
   837             if selectedPos < 0 then
   825             if selectedPos < 0 then
   838                 begin
   826                 begin
   839                 HandleSelection(true);
   827                 HandleSelection(true);
   840 
   828 
   847                 end;
   835                 end;
   848 
   836 
   849             DeleteSelected();
   837             DeleteSelected();
   850             UpdateCursorCoords();
   838             UpdateCursorCoords();
   851             end;
   839             end;
   852         SDLK_DELETE:
   840         SDL_SCANCODE_DELETE:
   853             begin
   841             begin
   854             if selectedPos < 0 then
   842             if selectedPos < 0 then
   855                 begin
   843                 begin
   856                 HandleSelection(true);
   844                 HandleSelection(true);
   857 
   845 
   864                 end;
   852                 end;
   865 
   853 
   866             DeleteSelected();
   854             DeleteSelected();
   867             UpdateCursorCoords();
   855             UpdateCursorCoords();
   868             end;
   856             end;
   869         SDLK_ESCAPE:
   857         SDL_SCANCODE_ESCAPE:
   870             begin
   858             begin
   871             if Length(InputStr.s) > 0 then
   859             if Length(InputStr.s) > 0 then
   872                 begin
   860                 begin
   873                 SetLine(InputStr, '', true);
   861                 SetLine(InputStr, '', true);
   874                 ResetCursor();
   862                 ResetCursor();
   875                 end
   863                 end
   876             else CleanupInput
   864             else CleanupInput
   877             end;
   865             end;
   878         SDLK_RETURN, SDLK_KP_ENTER:
   866         SDL_SCANCODE_RETURN, SDL_SCANCODE_KP_ENTER:
   879             begin
   867             begin
   880             if Length(InputStr.s) > 0 then
   868             if Length(InputStr.s) > 0 then
   881                 begin
   869                 begin
   882                 AcceptChatString(InputStr.s);
   870                 AcceptChatString(InputStr.s);
   883                 SetLine(InputStr, '', false);
   871                 SetLine(InputStr, '', false);
   884                 ResetCursor();
   872                 ResetCursor();
   885                 end;
   873                 end;
   886             CleanupInput
   874             CleanupInput
   887             end;
   875             end;
   888         SDLK_UP, SDLK_DOWN:
   876         SDL_SCANCODE_UP, SDL_SCANCODE_DOWN:
   889             begin
   877             begin
   890             if (Sym = SDLK_UP) and (history < localLastStr) then inc(history);
   878             if (Scancode = SDL_SCANCODE_UP) and (history < localLastStr) then inc(history);
   891             if (Sym = SDLK_DOWN) and (history > 0) then dec(history);
   879             if (Scancode = SDL_SCANCODE_DOWN) and (history > 0) then dec(history);
   892             index:= localLastStr - history + 1;
   880             index:= localLastStr - history + 1;
   893             if (index > localLastStr) then
   881             if (index > localLastStr) then
   894                 begin
   882                 begin
   895                 SetLine(InputStr, '', true);
   883                 SetLine(InputStr, '', true);
   896                 end
   884                 end
   900                 end;
   888                 end;
   901             cursorPos:= Length(InputStr.s);
   889             cursorPos:= Length(InputStr.s);
   902             ResetSelection();
   890             ResetSelection();
   903             UpdateCursorCoords();
   891             UpdateCursorCoords();
   904             end;
   892             end;
   905         SDLK_HOME:
   893         SDL_SCANCODE_HOME:
   906             begin
   894             begin
   907             if cursorPos > 0 then
   895             if cursorPos > 0 then
   908                 begin
   896                 begin
   909                 HandleSelection(selMode);
   897                 HandleSelection(selMode);
   910                 cursorPos:= 0;
   898                 cursorPos:= 0;
   912             else if (not selMode) then
   900             else if (not selMode) then
   913                 ResetSelection();
   901                 ResetSelection();
   914 
   902 
   915             UpdateCursorCoords();
   903             UpdateCursorCoords();
   916             end;
   904             end;
   917         SDLK_END:
   905         SDL_SCANCODE_END:
   918             begin
   906             begin
   919             i:= Length(InputStr.s);
   907             i:= Length(InputStr.s);
   920             if cursorPos < i then
   908             if cursorPos < i then
   921                 begin
   909                 begin
   922                 HandleSelection(selMode);
   910                 HandleSelection(selMode);
   925             else if (not selMode) then
   913             else if (not selMode) then
   926                 ResetSelection();
   914                 ResetSelection();
   927 
   915 
   928             UpdateCursorCoords();
   916             UpdateCursorCoords();
   929             end;
   917             end;
   930         SDLK_LEFT:
   918         SDL_SCANCODE_LEFT:
   931             begin
   919             begin
   932             if cursorPos > 0 then
   920             if cursorPos > 0 then
   933                 begin
   921                 begin
   934 
   922 
   935                 if ctrl then
   923                 if ctrl then
   954             else if (not selMode) then
   942             else if (not selMode) then
   955                 ResetSelection();
   943                 ResetSelection();
   956 
   944 
   957             UpdateCursorCoords();
   945             UpdateCursorCoords();
   958             end;
   946             end;
   959         SDLK_RIGHT:
   947         SDL_SCANCODE_RIGHT:
   960             begin
   948             begin
   961             if cursorPos < Length(InputStr.s) then
   949             if cursorPos < Length(InputStr.s) then
   962                 begin
   950                 begin
   963 
   951 
   964                 if selMode or (selectedPos < 0) then
   952                 if selMode or (selectedPos < 0) then
   979             else if (not selMode) then
   967             else if (not selMode) then
   980                 ResetSelection();
   968                 ResetSelection();
   981 
   969 
   982             UpdateCursorCoords();
   970             UpdateCursorCoords();
   983             end;
   971             end;
   984         SDLK_PAGEUP, SDLK_PAGEDOWN:
   972         SDL_SCANCODE_PAGEUP, SDL_SCANCODE_PAGEDOWN:
   985             begin
   973             begin
   986             // ignore me!!!
   974             // ignore me!!!
   987             end;
   975             end;
   988         SDLK_a:
   976         // TODO: figure out how to determine those keys better
       
   977         SDL_SCANCODE_a:
   989             begin
   978             begin
   990             // select all
   979             // select all
   991             if ctrlonly then
   980             if ctrlonly then
   992                 begin
   981                 begin
   993                 ResetSelection();
   982                 ResetSelection();
   997                 UpdateCursorCoords();
   986                 UpdateCursorCoords();
   998                 end
   987                 end
   999             else
   988             else
  1000                 action:= false;
   989                 action:= false;
  1001             end;
   990             end;
  1002         SDLK_c:
   991         SDL_SCANCODE_c:
  1003             begin
   992             begin
  1004             // copy
   993             // copy
  1005             if ctrlonly then
   994             if ctrlonly then
  1006                 CopySelectionToClipboard()
   995                 CopySelectionToClipboard()
  1007             else
   996             else
  1008                 action:= false;
   997                 action:= false;
  1009             end;
   998             end;
  1010         SDLK_v:
   999         SDL_SCANCODE_v:
  1011             begin
  1000             begin
  1012             // paste
  1001             // paste
  1013             if ctrlonly then
  1002             if ctrlonly then
  1014                 begin
  1003                 begin
  1015                 DeleteSelected();
  1004                 DeleteSelected();
  1016                 PasteFromClipboard();
  1005                 PasteFromClipboard();
  1017                 end
  1006                 end
  1018             else
  1007             else
  1019                 action:= false;
  1008                 action:= false;
  1020             end;
  1009             end;
  1021         SDLK_x:
  1010         SDL_SCANCODE_x:
  1022             begin
  1011             begin
  1023             // cut
  1012             // cut
  1024             if ctrlonly then
  1013             if ctrlonly then
  1025                 begin
  1014                 begin
  1026                 CopySelectionToClipboard();
  1015                 CopySelectionToClipboard();
  1030                 action:= false;
  1019                 action:= false;
  1031             end;
  1020             end;
  1032         else
  1021         else
  1033             action:= false;
  1022             action:= false;
  1034         end;
  1023         end;
  1035 
  1024     (*
  1036         // TODO: ctrl+c etc. probably won't work anymore while in text input mode
  1025     if (not action) and (Sym <> SDLK_UNKNOWN) and ((Sym and SDLK_SCANCODE_MASK) = 0) then
  1037 end;
  1026         begin
       
  1027         DeleteSelected();
       
  1028 
       
  1029         if (Sym < $80) then
       
  1030             btw:= 1
       
  1031         else if (Sym < $800) then
       
  1032             btw:= 2
       
  1033         else if (Sym < $10000) then
       
  1034             btw:= 3
       
  1035         else
       
  1036             btw:= 4;
       
  1037 
       
  1038         utf8:= '';
       
  1039 
       
  1040         for i:= btw downto 2 do
       
  1041             begin
       
  1042             utf8:= char((Sym or $80) and $BF) + utf8;
       
  1043             Sym:= Sym shr 6
       
  1044             end;
       
  1045 
       
  1046         utf8:= char(Sym or firstByteMark[Pred(btw)]) + utf8;
       
  1047 
       
  1048         if Length(InputStr.s) + btw > MaxInputStrLen then
       
  1049             exit;
       
  1050 
       
  1051         // if speech bubble quotes are used as first input, add the closing quote and place cursor inbetween
       
  1052         if (Length(InputStr.s) = 0) and (Length(utf8) = 1) and (charIsForHogSpeech(utf8[1])) then
       
  1053             begin
       
  1054             InsertIntoInputStr(utf8);
       
  1055             InsertIntoInputStr(utf8);
       
  1056             cursorPos:= 1;
       
  1057             UpdateCursorCoords();
       
  1058             end
       
  1059         else
       
  1060             InsertIntoInputStr(utf8);
       
  1061         end
       
  1062         *)
       
  1063 end;
       
  1064 
       
  1065 procedure TextInput(var event: TSDL_TextInputEvent);
       
  1066 var s: shortstring;
       
  1067     l: byte;
       
  1068 begin
       
  1069     DeleteSelected();
       
  1070 
       
  1071     l:= 0;
       
  1072     while event.text[l] <> #0 do
       
  1073         begin
       
  1074         s[l + 1]:= event.text[l];
       
  1075         inc(l)
       
  1076         end;
       
  1077 
       
  1078     if l > 0 then
       
  1079         begin
       
  1080         if byte(InputStr.s[0]) + l > 240 then exit;
       
  1081         s[0]:= char(l);
       
  1082         InsertIntoInputStr(s);
       
  1083         end
       
  1084 end;
       
  1085 
  1038 
  1086 
  1039 procedure chChatMessage(var s: shortstring);
  1087 procedure chChatMessage(var s: shortstring);
  1040 begin
  1088 begin
  1041     AddChatString(s)
  1089     AddChatString(s)
  1042 end;
  1090 end;
  1079 
  1127 
  1080 procedure chChat(var s: shortstring);
  1128 procedure chChat(var s: shortstring);
  1081 begin
  1129 begin
  1082     s:= s; // avoid compiler hint
  1130     s:= s; // avoid compiler hint
  1083     GameState:= gsChat;
  1131     GameState:= gsChat;
       
  1132     SDL_StopTextInput();
  1084     SDL_StartTextInput();
  1133     SDL_StartTextInput();
       
  1134     //SDL_EnableKeyRepeat(200,45);
  1085     if length(s) = 0 then
  1135     if length(s) = 0 then
  1086         SetLine(InputStr, '', true)
  1136         SetLine(InputStr, '', true)
  1087     else
  1137     else
  1088         begin
  1138         begin
  1089         SetLine(InputStr, '/team ', true);
  1139         SetLine(InputStr, '/team ', true);
  1119     for i:= 0 to MaxStrIndex do
  1169     for i:= 0 to MaxStrIndex do
  1120         Strs[i].Tex := nil;
  1170         Strs[i].Tex := nil;
  1121 
  1171 
  1122     LastKeyPressTick:= 0;
  1172     LastKeyPressTick:= 0;
  1123     ResetCursor();
  1173     ResetCursor();
       
  1174     SDL_StopTextInput();
  1124 end;
  1175 end;
  1125 
  1176 
  1126 procedure freeModule;
  1177 procedure freeModule;
  1127 var i: ShortInt;
  1178 var i: ShortInt;
  1128 begin
  1179 begin