hedgewars/uConsole.pas
changeset 946 42c5cc87cbd1
parent 936 ba582673db7d
child 950 feb18ec0c5c2
equal deleted inserted replaced
945:4ead9cde4e14 946:42c5cc87cbd1
    25      TCommandHandler = procedure (var params: shortstring);
    25      TCommandHandler = procedure (var params: shortstring);
    26 
    26 
    27 procedure DrawConsole(Surface: PSDL_Surface);
    27 procedure DrawConsole(Surface: PSDL_Surface);
    28 procedure WriteToConsole(s: shortstring);
    28 procedure WriteToConsole(s: shortstring);
    29 procedure WriteLnToConsole(s: shortstring);
    29 procedure WriteLnToConsole(s: shortstring);
    30 procedure KeyPressConsole(Key: Longword);
       
    31 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    30 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    32 procedure StopMessages(Message: Longword);
    31 procedure StopMessages(Message: Longword);
    33 function  GetLastConsoleLine: shortstring;
    32 function  GetLastConsoleLine: shortstring;
    34 
    33 
    35 procedure doPut(putX, putY: LongInt; fromAI: boolean);
    34 procedure doPut(putX, putY: LongInt; fromAI: boolean);
    55                   updatetex: boolean;
    54                   updatetex: boolean;
    56                   end;
    55                   end;
    57 
    56 
    58 var   ConsoleLines: array[byte] of TTextLine;
    57 var   ConsoleLines: array[byte] of TTextLine;
    59       CurrLine: LongInt = 0;
    58       CurrLine: LongInt = 0;
    60       InputStr: TTextLine;
       
    61       InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char
       
    62       Variables: PVariable = nil;
    59       Variables: PVariable = nil;
    63 
    60 
    64 procedure SetLine(var tl: TTextLine; str: shortstring);
    61 procedure SetLine(var tl: TTextLine; str: shortstring);
    65 begin
    62 begin
    66 with tl do
    63 with tl do
   149 for y:= 0 to cConsoleHeight div 256 + 1 do
   146 for y:= 0 to cConsoleHeight div 256 + 1 do
   150     for x:= 0 to cScreenWidth div 256 + 1 do
   147     for x:= 0 to cScreenWidth div 256 + 1 do
   151         DrawSprite(sprConsoleBG, x * 256, cConsoleHeight - 256 - y * 256, 0);
   148         DrawSprite(sprConsoleBG, x * 256, cConsoleHeight - 256 - y * 256, 0);
   152 
   149 
   153 for y:= 0 to cConsoleHeight div Fontz[fnt16].Height do
   150 for y:= 0 to cConsoleHeight div Fontz[fnt16].Height do
   154     DrawLine(ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], 4, cConsoleHeight - (y + 2) * (Fontz[fnt16].Height + 2));
   151     DrawLine(ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], 4, cConsoleHeight - (y + 1) * (Fontz[fnt16].Height + 2));
   155 
       
   156 DrawLine(InputStr, 4, cConsoleHeight - Fontz[fnt16].Height - 2);
       
   157 
   152 
   158 glDisable(GL_BLEND);
   153 glDisable(GL_BLEND);
   159 glDisable(GL_TEXTURE_2D);
   154 glDisable(GL_TEXTURE_2D);
   160 end;
   155 end;
   161 
   156 
   247 case c of
   242 case c of
   248      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   243      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
   249      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   244      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
   250 end;
   245 end;
   251 
   246 
   252 procedure AutoComplete;
       
   253 var t: PVariable;
       
   254     c: char;
       
   255 begin
       
   256 if InputStr.s[0] = #0 then exit;
       
   257 c:= InputStr.s[1];
       
   258 if c in ['/', '$'] then Delete(InputStr.s, 1, 1) else c:= #0;
       
   259 
       
   260 if InputStr.s[byte(InputStr.s[0])] = #32 then dec(InputStr.s[0]);
       
   261 t:= Variables;
       
   262 while t <> nil do
       
   263       begin
       
   264       if (c=#0) or ((t^.VType =  vtCommand) and (c='/'))or
       
   265                    ((t^.VType <> vtCommand) and (c='$'))then
       
   266          if copy(t^.Name, 1, Length(InputStr.s)) = InputStr.s then
       
   267             begin
       
   268             if t^.VType = vtCommand then SetLine(InputStr, '/' + t^.Name + ' ')
       
   269                                     else SetLine(InputStr, '$' + t^.Name + ' ');
       
   270             exit
       
   271             end;
       
   272       t:= t^.Next
       
   273       end
       
   274 end;
       
   275 
       
   276 procedure KeyPressConsole(Key: Longword);
       
   277 const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0);
       
   278 var i, btw: integer;
       
   279     utf8: shortstring;
       
   280 begin
       
   281 if Key <> 0 then
       
   282   case Key of
       
   283       8: if Length(InputStr.s) > 0 then
       
   284             begin
       
   285             InputStr.s[0]:= InputStrL[byte(InputStr.s[0])];
       
   286             SetLine(InputStr, InputStr.s)
       
   287             end;
       
   288       9: AutoComplete;
       
   289  13,271: begin
       
   290          if InputStr.s[1] in ['/', '$'] then
       
   291             ParseCommand(InputStr.s, false)
       
   292          else
       
   293             ParseCommand('/say ' + InputStr.s, false);
       
   294          SetLine(InputStr, '')
       
   295          end
       
   296      else
       
   297      if (Key < $80) then btw:= 1
       
   298      else if (Key < $800) then btw:= 2
       
   299      else if (Key < $10000) then btw:= 3
       
   300      else btw:= 4;
       
   301      utf8:= '';
       
   302      for i:= btw downto 2 do
       
   303          begin
       
   304          utf8:= char((Key or $80) and $BF) + utf8;
       
   305          Key:= Key shr 6
       
   306          end;
       
   307      utf8:= char(Key or firstByteMark[btw]) + utf8;
       
   308 
       
   309      InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
       
   310      SetLine(InputStr, InputStr.s + utf8)
       
   311      end
       
   312 end;
       
   313 
       
   314 function GetLastConsoleLine: shortstring;
   247 function GetLastConsoleLine: shortstring;
   315 begin
   248 begin
   316 if CurrLine = 0 then GetLastConsoleLine:= ConsoleLines[Pred(cLinesCount)].s
   249 if CurrLine = 0 then GetLastConsoleLine:= ConsoleLines[Pred(cLinesCount)].s
   317                 else GetLastConsoleLine:= ConsoleLines[Pred(CurrLine)].s
   250                 else GetLastConsoleLine:= ConsoleLines[Pred(CurrLine)].s
   318 end;
   251 end;
   353 RegisterVariable('hhcoords', vtCommand, @chSetHHCoords  , false);
   286 RegisterVariable('hhcoords', vtCommand, @chSetHHCoords  , false);
   354 RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
   287 RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
   355 RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
   288 RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
   356 RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
   289 RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
   357 RegisterVariable('skip'    , vtCommand, @chSkip         , false);
   290 RegisterVariable('skip'    , vtCommand, @chSkip         , false);
       
   291 RegisterVariable('chat'    , vtCommand, @chChat         , true );
   358 RegisterVariable('say'     , vtCommand, @chSay          , true );
   292 RegisterVariable('say'     , vtCommand, @chSay          , true );
   359 RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
   293 RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
   360 RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
   294 RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
   361 RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
   295 RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
   362 RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
   296 RegisterVariable('+right'  , vtCommand, @chRight_p      , false);