hedgewars/uChat.pas
changeset 10851 f25dad9c3136
parent 10850 c76ea22ea249
child 10852 9e5763cb805e
equal deleted inserted replaced
10850:c76ea22ea249 10851:f25dad9c3136
    34 
    34 
    35 implementation
    35 implementation
    36 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;
    37 
    37 
    38 const MaxStrIndex = 27;
    38 const MaxStrIndex = 27;
       
    39       MaxInputStrLen = 240;
    39 
    40 
    40 type TChatLine = record
    41 type TChatLine = record
    41     Tex: PTexture;
    42     Tex: PTexture;
    42     Time: Longword;
    43     Time: Longword;
    43     Width: LongInt;
    44     Width: LongInt;
   196 
   197 
   197 if isInput then
   198 if isInput then
   198     begin
   199     begin
   199     cl.s:= str;
   200     cl.s:= str;
   200     color:= colors[#6];
   201     color:= colors[#6];
       
   202     // TODO FIX render InputLinePrefix seperately so that it doesn't mess up max len
   201     str:= InputLinePrefix + str + ' ';
   203     str:= InputLinePrefix + str + ' ';
   202     end
   204     end
   203 else
   205 else
   204     begin
   206     begin
   205     color:= colors[str[1]];
   207     color:= colors[str[1]];
   666     end;
   668     end;
   667 end;
   669 end;
   668 
   670 
   669 procedure CopyToClipboard(var newContent: shortstring);
   671 procedure CopyToClipboard(var newContent: shortstring);
   670 begin
   672 begin
   671     SendIPC(_S'Y' + copy(newContent, 1, 253) + #0);
   673     SendIPC(_S'y' + copy(newContent, 1, 253) + #0);
   672 end;
   674 end;
   673 
   675 
   674 procedure CopySelectionToClipboard();
   676 procedure CopySelectionToClipboard();
   675 var selection: shortstring;
   677 var selection: shortstring;
   676 begin
   678 begin
   681         end;
   683         end;
   682 end;
   684 end;
   683 
   685 
   684 // TODO: honor utf8, don't break utf8 chars when shifting chars beyond limit
   686 // TODO: honor utf8, don't break utf8 chars when shifting chars beyond limit
   685 procedure InsertIntoInputStr(var s: shortstring);
   687 procedure InsertIntoInputStr(var s: shortstring);
   686 var i, l, lastc: integer;
   688 var i, l, il, lastc: integer;
   687 begin
   689 begin
   688     l:= Length(s);
   690     // safe length for string
       
   691     l:= min(MaxInputStrLen-cursorPos, Length(s));
   689 
   692 
   690     // if we insert rather than append, shift info in InputStrL accordingly
   693     // if we insert rather than append, shift info in InputStrL accordingly
   691     if cursorPos < Length(InputStr.s) then
   694     if cursorPos < Length(InputStr.s) then
   692         begin
   695         begin
   693         for i:= Length(InputStr.s) downto cursorPos + 1 do
   696         for i:= Length(InputStr.s) downto cursorPos + 1 do
   694             begin
   697             begin
   695             if InputStrL[i] <> InputStrLNoPred then
   698             if InputStrL[i] <> InputStrLNoPred then
   696                 begin
   699                 begin
   697                 InputStrL[i+l]:= InputStrL[i] + l;
   700                 il:= i + l;
       
   701                 // only shift if not overflowing
       
   702                 if il <= MaxInputStrLen then
       
   703                     InputStrL[il]:= InputStrL[i] + l;
   698                 InputStrL[i]:= InputStrLNoPred;
   704                 InputStrL[i]:= InputStrLNoPred;
   699                 end;
   705                 end;
   700             end;
   706             end;
   701         end;
   707         end;
   702 
   708 
   703     InputStrL[cursorPos + l]:= cursorPos;
   709     InputStrL[cursorPos + l]:= cursorPos;
   704     Insert(s, InputStr.s, cursorPos + 1);
   710     // insert string truncated to safe length
       
   711     Insert(copy(s,1,l), InputStr.s, cursorPos + 1);
       
   712     if Length(InputStr.s) > MaxInputStrLen then
       
   713         InputStr.s[0]:= char(MaxInputStrLen);
       
   714 
   705     SetLine(InputStr, InputStr.s, true);
   715     SetLine(InputStr, InputStr.s, true);
   706 
   716 
   707     // move cursor to end of inserted string
   717     // move cursor to end of inserted string
   708     lastc:= 255;
   718     lastc:= MaxInputStrLen;
   709     cursorPos:= min(lastc, cursorPos + l);
   719     cursorPos:= min(lastc, cursorPos + l);
   710     UpdateCursorCoords();
   720     UpdateCursorCoords();
   711 end;
   721 end;
   712 
   722 
   713 procedure PasteFromClipboard();
   723 procedure PasteFromClipboard();
   714 begin
   724 begin
   715     SendIPC(_S'P');
   725     SendIPC(_S'Y');
   716 end;
   726 end;
   717 
   727 
   718 procedure CheckPasteBuffer();
   728 procedure CheckPasteBuffer();
   719 begin
   729 begin
   720     if Length(ChatPasteBuffer) > 0 then
   730     if Length(ChatPasteBuffer) > 0 then
   981             Key:= Key shr 6
   991             Key:= Key shr 6
   982             end;
   992             end;
   983 
   993 
   984         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   994         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
   985 
   995 
   986         if Length(InputStr.s) + btw > 240 then
   996         if Length(InputStr.s) + btw > MaxInputStrLen then
   987             exit;
   997             exit;
   988 
   998 
   989         InsertIntoInputStr(utf8);
   999         InsertIntoInputStr(utf8);
   990         end
  1000         end
   991 end;
  1001 end;
  1069     missedCount:= 0;
  1079     missedCount:= 0;
  1070     liveLua:= false;
  1080     liveLua:= false;
  1071     ChatHidden:= false;
  1081     ChatHidden:= false;
  1072     firstDraw:= true;
  1082     firstDraw:= true;
  1073 
  1083 
  1074     InputLinePrefix:= UserNick + '> ';
  1084     InputLinePrefix:= '';
  1075     inputStr.s:= '';
  1085     inputStr.s:= '';
  1076     inputStr.Tex := nil;
  1086     inputStr.Tex := nil;
  1077     for i:= 0 to MaxStrIndex do
  1087     for i:= 0 to MaxStrIndex do
  1078         Strs[i].Tex := nil;
  1088         Strs[i].Tex := nil;
  1079 
  1089