hedgewars/uChat.pas
changeset 10849 a772d342066a
parent 10846 c76fd416eff6
child 10850 c76ea22ea249
equal deleted inserted replaced
10847:b68e35d6e376 10849:a772d342066a
    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, Sym: Longword; Modifier: Word);
    31 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
    32 procedure SendHogSpeech(s: shortstring);
    32 procedure SendHogSpeech(s: shortstring);
       
    33 procedure CopyToClipboard(var newContent: shortstring);
    33 
    34 
    34 implementation
    35 implementation
    35 uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO, uScript, uRenderUtils;
    36 uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO, uScript, uRenderUtils;
    36 
    37 
    37 const MaxStrIndex = 27;
    38 const MaxStrIndex = 27;
   563         begin
   564         begin
   564         DelBytesFromInputStrBack(max(cursorPos, selectedPos), abs(selectedPos-cursorPos));
   565         DelBytesFromInputStrBack(max(cursorPos, selectedPos), abs(selectedPos-cursorPos));
   565         cursorPos:= min(cursorPos, selectedPos);
   566         cursorPos:= min(cursorPos, selectedPos);
   566         ResetSelection();
   567         ResetSelection();
   567         end;
   568         end;
       
   569     UpdateCursorCoords();
   568 end;
   570 end;
   569 
   571 
   570 procedure HandleSelection(enabled: boolean);
   572 procedure HandleSelection(enabled: boolean);
   571 begin
   573 begin
   572 if enabled then
   574 if enabled then
   656             cursorPos:= InputStrL[cursorPos];
   658             cursorPos:= InputStrL[cursorPos];
   657             break;
   659             break;
   658             end;
   660             end;
   659         end;
   661         end;
   660     end;
   662     end;
       
   663 end;
       
   664 
       
   665 var clipboardBuffer: shortstring;
       
   666 
       
   667 procedure CopyToClipboard(var newContent: shortstring);
       
   668 begin
       
   669     clipboardBuffer:= newContent;
       
   670 end;
       
   671 
       
   672 procedure CopySelectionToClipboard();
       
   673 var selection: shortstring;
       
   674 begin
       
   675     if selectedPos >= 0 then
       
   676         begin
       
   677         selection:= copy(InputStr.s, min(CursorPos, selectedPos) + 1, abs(CursorPos - selectedPos));
       
   678         CopyToClipboard(selection);
       
   679         end;
       
   680 end;
       
   681 
       
   682 // TODO: honor utf8, don't break utf8 chars when shifting chars beyond limit
       
   683 procedure InsertIntoInputStr(var s: shortstring);
       
   684 var i, l, lastc: integer;
       
   685 begin
       
   686     l:= Length(s);
       
   687 
       
   688     // if we insert rather than append, shift info in InputStrL accordingly
       
   689     if cursorPos < Length(InputStr.s) then
       
   690         begin
       
   691         for i:= Length(InputStr.s) downto cursorPos + 1 do
       
   692             begin
       
   693             if InputStrL[i] <> InputStrLNoPred then
       
   694                 begin
       
   695                 InputStrL[i+l]:= InputStrL[i] + l;
       
   696                 InputStrL[i]:= InputStrLNoPred;
       
   697                 end;
       
   698             end;
       
   699         end;
       
   700 
       
   701     InputStrL[cursorPos + l]:= cursorPos;
       
   702     Insert(s, InputStr.s, cursorPos + 1);
       
   703     SetLine(InputStr, InputStr.s, true);
       
   704 
       
   705     // move cursor to end of inserted string
       
   706     lastc:= 255;
       
   707     cursorPos:= min(lastc, cursorPos + l);
       
   708     UpdateCursorCoords();
       
   709 end;
       
   710 
       
   711 procedure PasteFromClipboard();
       
   712 begin
       
   713     DeleteSelected();
       
   714     InsertIntoInputStr(clipboardBuffer);
   661 end;
   715 end;
   662 
   716 
   663 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
   717 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
   664 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   718 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   665 var i, btw, index: integer;
   719 var i, btw, index: integer;
   689                 if ctrl and (selectedPos < 0) then
   743                 if ctrl and (selectedPos < 0) then
   690                     begin
   744                     begin
   691                     HandleSelection(true);
   745                     HandleSelection(true);
   692                     SkipInputChars(skip, true);
   746                     SkipInputChars(skip, true);
   693                     DeleteSelected();
   747                     DeleteSelected();
   694                     end;
   748                     end
       
   749                 else
       
   750                     UpdateCursorCoords();
       
   751 
   695                 end
   752                 end
   696             else
   753             else
   697                 DeleteSelected();
   754                 DeleteSelected();
   698             UpdateCursorCoords();
       
   699             end;
   755             end;
   700         SDLK_DELETE:
   756         SDLK_DELETE:
   701             begin
   757             begin
   702             if selectedPos < 0 then
   758             if selectedPos < 0 then
   703                 begin
   759                 begin
   716                         begin
   772                         begin
   717                         HandleSelection(true);
   773                         HandleSelection(true);
   718                         SkipInputChars(skip, false);
   774                         SkipInputChars(skip, false);
   719                         DeleteSelected();
   775                         DeleteSelected();
   720                         end;
   776                         end;
   721                     end;
   777                     end
       
   778                 else
       
   779                     UpdateCursorCoords();
   722                 end
   780                 end
   723             else
   781             else
   724                 DeleteSelected();
   782                 DeleteSelected();
   725 
       
   726             UpdateCursorCoords();
       
   727             end;
   783             end;
   728         SDLK_ESCAPE:
   784         SDLK_ESCAPE:
   729             begin
   785             begin
   730             if Length(InputStr.s) > 0 then
   786             if Length(InputStr.s) > 0 then
   731                 begin
   787                 begin
   860                 UpdateCursorCoords();
   916                 UpdateCursorCoords();
   861                 end
   917                 end
   862             else
   918             else
   863                 action:= false;
   919                 action:= false;
   864             end;
   920             end;
       
   921         SDLK_c:
       
   922             begin
       
   923             // copy
       
   924             if ctrl then
       
   925                 CopySelectionToClipboard()
       
   926             else
       
   927                 action:= false;
       
   928             end;
       
   929         SDLK_v:
       
   930             begin
       
   931             // paste
       
   932             if ctrl then
       
   933                 PasteFromClipboard()
       
   934             else
       
   935                 action:= false;
       
   936             end;
       
   937         SDLK_x:
       
   938             begin
       
   939             // cut
       
   940             if ctrl then
       
   941                 begin
       
   942                 CopySelectionToClipboard();
       
   943                 DeleteSelected();
       
   944                 end
       
   945             else
       
   946                 action:= false;
       
   947             end;
   865         else
   948         else
   866             action:= false;
   949             action:= false;
   867         end;
   950         end;
   868     if not action and (Key <> 0) then
   951     if not action and (Key <> 0) then
   869         begin
   952         begin
   889         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   972         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   890 
   973 
   891         if Length(InputStr.s) + btw > 240 then
   974         if Length(InputStr.s) + btw > 240 then
   892             exit;
   975             exit;
   893 
   976 
   894         // if we insert rather than append, shift info in InputStrL accordingly
   977         InsertIntoInputStr(utf8);
   895         if cursorPos < Length(InputStr.s) then
       
   896             begin
       
   897             for i:= Length(InputStr.s) downto cursorPos + 1 do
       
   898                 begin
       
   899                 if InputStrL[i] <> InputStrLNoPred then
       
   900                     begin
       
   901                     InputStrL[i+btw]:= InputStrL[i] + btw;
       
   902                     InputStrL[i]:= InputStrLNoPred;
       
   903                     end;
       
   904                 end;
       
   905             end;
       
   906 
       
   907         InputStrL[cursorPos + btw]:= cursorPos;
       
   908         Insert(utf8, InputStr.s, cursorPos + 1);
       
   909         SetLine(InputStr, InputStr.s, true);
       
   910 
       
   911         cursorPos:= cursorPos + btw;
       
   912         UpdateCursorCoords();
       
   913         end
   978         end
   914 end;
   979 end;
   915 
   980 
   916 procedure chChatMessage(var s: shortstring);
   981 procedure chChatMessage(var s: shortstring);
   917 begin
   982 begin
  1002 
  1067 
  1003     FillChar(InputStrL, sizeof(InputStrL), InputStrLNoPred);
  1068     FillChar(InputStrL, sizeof(InputStrL), InputStrLNoPred);
  1004 
  1069 
  1005     LastKeyPressTick:= 0;
  1070     LastKeyPressTick:= 0;
  1006     ResetCursor();
  1071     ResetCursor();
       
  1072 
       
  1073     clipboardBuffer:= '';
  1007 end;
  1074 end;
  1008 
  1075 
  1009 procedure freeModule;
  1076 procedure freeModule;
  1010 var i: ShortInt;
  1077 var i: ShortInt;
  1011 begin
  1078 begin