hedgewars/uChat.pas
changeset 10852 9e5763cb805e
parent 10851 f25dad9c3136
child 10853 287634baf7ba
equal deleted inserted replaced
10851:f25dad9c3136 10852:9e5763cb805e
    63     ChatReady: boolean;
    63     ChatReady: boolean;
    64     showAll: boolean;
    64     showAll: boolean;
    65     liveLua: boolean;
    65     liveLua: boolean;
    66     ChatHidden: boolean;
    66     ChatHidden: boolean;
    67     firstDraw: boolean;
    67     firstDraw: boolean;
    68     InputLinePrefix: shortstring;
    68     InputLinePrefix: TChatLine;
    69     // cursor
    69     // cursor
    70     cursorPos, cursorX, selectedPos, selectionDx: LongInt;
    70     cursorPos, cursorX, selectedPos, selectionDx: LongInt;
    71     LastKeyPressTick: LongWord;
    71     LastKeyPressTick: LongWord;
       
    72 
    72 
    73 
    73 const
    74 const
    74     InputStrLNoPred: byte = 255;
    75     InputStrLNoPred: byte = 255;
    75 
    76 
    76     colors: array[#0..#6] of TSDL_Color = (
    77     colors: array[#0..#6] of TSDL_Color = (
   110     if cursorPos = selectedPos then
   111     if cursorPos = selectedPos then
   111         ResetSelection();
   112         ResetSelection();
   112 
   113 
   113     // calculate cursor offset
   114     // calculate cursor offset
   114 
   115 
   115     str:= InputLinePrefix + InputStr.s;
   116     str:= InputStr.s;
   116     font:= CheckCJKFont(ansistring(str), fnt16);
   117     font:= CheckCJKFont(ansistring(str), fnt16);
   117 
   118 
   118     // get only substring before cursor to determine length
   119     // get only substring before cursor to determine length
   119     // SetLength(str, Length(InputLinePrefix) + cursorPos); // makes pas2c unhappy
   120     // SetLength(str, cursorPos); // makes pas2c unhappy
   120     str[0]:= char(Length(InputLinePrefix) + cursorPos);
   121     str[0]:= char(cursorPos);
   121     // get render size of text
   122     // get render size of text
   122     TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), @coff, nil);
   123     TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), @coff, nil);
   123 
   124 
   124     cursorX:= 2 + coff;
   125     cursorX:= 2 + coff;
   125 
   126 
   126     // calculate selection width on screen
   127     // calculate selection width on screen
   127     if selectedPos >= 0 then
   128     if selectedPos >= 0 then
   128         begin
   129         begin
   129         if selectedPos > cursorPos then
   130         if selectedPos > cursorPos then
   130             str:= InputLinePrefix + InputStr.s;
   131             str:= InputStr.s;
   131         // SetLength(str, Length(InputLinePrefix) + selectedPos); // makes pas2c unhappy
   132         // SetLength(str, selectedPos); // makes pas2c unhappy
   132         str[0]:= char(Length(InputLinePrefix) + selectedPos);
   133         str[0]:= char(selectedPos);
   133         TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), @soff, nil);
   134         TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), @soff, nil);
   134         selectionDx:= soff - coff;
   135         selectionDx:= soff - coff;
   135         end
   136         end
   136     else
   137     else
   137         selectionDx:= 0;
   138         selectionDx:= 0;
   197 
   198 
   198 if isInput then
   199 if isInput then
   199     begin
   200     begin
   200     cl.s:= str;
   201     cl.s:= str;
   201     color:= colors[#6];
   202     color:= colors[#6];
   202     // TODO FIX render InputLinePrefix seperately so that it doesn't mess up max len
   203     str:= str + ' ';
   203     str:= InputLinePrefix + str + ' ';
       
   204     end
   204     end
   205 else
   205 else
   206     begin
   206     begin
   207     color:= colors[str[1]];
   207     color:= colors[str[1]];
   208     delete(str, 1, 1);
   208     delete(str, 1, 1);
   251 inc(visibleCount)
   251 inc(visibleCount)
   252 end;
   252 end;
   253 
   253 
   254 procedure CheckPasteBuffer(); forward;
   254 procedure CheckPasteBuffer(); forward;
   255 
   255 
       
   256 procedure UpdateInputLinePrefix();
       
   257 begin
       
   258 if liveLua then
       
   259     begin
       
   260     InputLinePrefix.color:= colors[#1];
       
   261     InputLinePrefix.s:= '[Lua] >';
       
   262     end
       
   263 else
       
   264     begin
       
   265     InputLinePrefix.color:= colors[#6];
       
   266     InputLinePrefix.s:= UserNick + '>';
       
   267     end;
       
   268 
       
   269 FreeAndNilTexture(InputLinePrefix.Tex);
       
   270 end;
       
   271 
   256 procedure DrawChat;
   272 procedure DrawChat;
   257 var i, t, left, top, cnt: LongInt;
   273 var i, t, left, top, cnt: LongInt;
   258     selRect: TSDL_Rect;
   274     selRect: TSDL_Rect;
   259 begin
   275 begin
   260 ChatReady:= true; // maybe move to somewhere else?
   276 ChatReady:= true; // maybe move to somewhere else?
   269 // draw chat input line first and under all other lines
   285 // draw chat input line first and under all other lines
   270 if (GameState = gsChat) and (InputStr.Tex <> nil) then
   286 if (GameState = gsChat) and (InputStr.Tex <> nil) then
   271     begin
   287     begin
   272     CheckPasteBuffer();
   288     CheckPasteBuffer();
   273 
   289 
       
   290     if InputLinePrefix.Tex = nil then
       
   291         RenderChatLineTex(InputLinePrefix, InputLinePrefix.s);
       
   292 
       
   293     DrawTexture(left, top, InputLinePrefix.Tex);
       
   294     inc(left, InputLinePrefix.Width);
       
   295     DrawTexture(left, top, InputStr.Tex);
       
   296 
   274     if firstDraw then
   297     if firstDraw then
   275         begin
   298         begin
   276         UpdateCursorCoords();
   299         UpdateCursorCoords();
   277         firstDraw:= false;
   300         firstDraw:= false;
   278         end;
   301         end;
   279 
   302 
   280     DrawTexture(left, top, InputStr.Tex);
       
   281     if selectedPos < 0 then
   303     if selectedPos < 0 then
   282         begin
   304         begin
   283         // draw cursor
   305         // draw cursor
   284         if ((RealTicks - LastKeyPressTick) and 512) < 256 then
   306         if ((RealTicks - LastKeyPressTick) and 512) < 256 then
   285             DrawLineOnScreen(left + cursorX, top + 2, left + cursorX, top + ClHeight - 2, 2.0, $00, $FF, $FF, $FF);
   307             DrawLineOnScreen(left + cursorX, top + 2, left + cursorX, top + ClHeight - 2, 2.0, $00, $FF, $FF, $FF);
   299             selRect.w:= selectionDx;
   321             selRect.w:= selectionDx;
   300             end;
   322             end;
   301 
   323 
   302         DrawRect(selRect, $FF, $FF, $FF, $40, true);
   324         DrawRect(selRect, $FF, $FF, $FF, $40, true);
   303         end;
   325         end;
       
   326 
       
   327     dec(left, InputLinePrefix.Width);
   304     end;
   328     end;
   305 
       
   306 
   329 
   307 // draw chat lines
   330 // draw chat lines
   308 if ((not ChatHidden) or showAll) and (UIDisplay <> uiNone) then
   331 if ((not ChatHidden) or showAll) and (UIDisplay <> uiNone) then
   309     begin
   332     begin
   310     if MissedCount <> 0 then // there are chat strings we missed, so print them now
   333     if MissedCount <> 0 then // there are chat strings we missed, so print them now
   463             else
   486             else
   464                 begin
   487                 begin
   465                 AddFileLog('[Lua] chat input string parsing disabled');
   488                 AddFileLog('[Lua] chat input string parsing disabled');
   466                 AddChatString(#3 + 'Lua parsing: OFF');
   489                 AddChatString(#3 + 'Lua parsing: OFF');
   467                 end;
   490                 end;
       
   491             UpdateInputLinePrefix();
   468             end;
   492             end;
   469         exit
   493         exit
   470         end;
   494         end;
   471 
   495 
   472     // hedghog animations/taunts and engine commands
   496     // hedghog animations/taunts and engine commands
  1079     missedCount:= 0;
  1103     missedCount:= 0;
  1080     liveLua:= false;
  1104     liveLua:= false;
  1081     ChatHidden:= false;
  1105     ChatHidden:= false;
  1082     firstDraw:= true;
  1106     firstDraw:= true;
  1083 
  1107 
  1084     InputLinePrefix:= '';
  1108     InputLinePrefix.Tex:= nil;
       
  1109     UpdateInputLinePrefix();
  1085     inputStr.s:= '';
  1110     inputStr.s:= '';
  1086     inputStr.Tex := nil;
  1111     inputStr.Tex := nil;
  1087     for i:= 0 to MaxStrIndex do
  1112     for i:= 0 to MaxStrIndex do
  1088         Strs[i].Tex := nil;
  1113         Strs[i].Tex := nil;
  1089 
  1114 
  1094 end;
  1119 end;
  1095 
  1120 
  1096 procedure freeModule;
  1121 procedure freeModule;
  1097 var i: ShortInt;
  1122 var i: ShortInt;
  1098 begin
  1123 begin
       
  1124     FreeAndNilTexture(InputLinePrefix.Tex);
  1099     FreeAndNilTexture(InputStr.Tex);
  1125     FreeAndNilTexture(InputStr.Tex);
  1100     for i:= 0 to MaxStrIndex do
  1126     for i:= 0 to MaxStrIndex do
  1101         FreeAndNilTexture(Strs[i].Tex);
  1127         FreeAndNilTexture(Strs[i].Tex);
  1102 end;
  1128 end;
  1103 
  1129