hedgewars/uChat.pas
branchqmlfrontend
changeset 11071 3851ce4f2061
parent 11046 47a8c19ecb60
child 11317 62287d4044e7
equal deleted inserted replaced
11050:9b7c8c5a94e0 11071:3851ce4f2061
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    66     cursorPos, cursorX, selectedPos, selectionDx: LongInt;
    66     cursorPos, cursorX, selectedPos, selectionDx: LongInt;
    67     LastKeyPressTick: LongWord;
    67     LastKeyPressTick: LongWord;
    68 
    68 
    69 
    69 
    70 const
    70 const
    71     colors: array[#0..#6] of TSDL_Color = (
    71     colors: array[#0..#9] of TSDL_Color = (
    72             (r:$FF; g:$FF; b:$FF; a:$FF), // unused, feel free to take it for anything
    72             (r:$FF; g:$FF; b:$FF; a:$FF), // #0 unused, feel free to take it for anything
    73             (r:$FF; g:$FF; b:$FF; a:$FF), // chat message [White]
    73             (r:$FF; g:$FF; b:$FF; a:$FF), // #1 chat message [White]
    74             (r:$FF; g:$00; b:$FF; a:$FF), // action message [Purple]
    74             (r:$FF; g:$00; b:$FF; a:$FF), // #2 action message [Purple]
    75             (r:$90; g:$FF; b:$90; a:$FF), // join/leave message [Lime]
    75             (r:$90; g:$FF; b:$90; a:$FF), // #3 join/leave message [Lime]
    76             (r:$FF; g:$FF; b:$A0; a:$FF), // team message [Light Yellow]
    76             (r:$FF; g:$FF; b:$A0; a:$FF), // #4 team message [Light Yellow]
    77             (r:$FF; g:$00; b:$00; a:$FF), // error messages [Red]
    77             (r:$FF; g:$00; b:$00; a:$FF), // #5 error messages [Red]
    78             (r:$00; g:$FF; b:$FF; a:$FF)  // input line [Light Blue]
    78             (r:$00; g:$FF; b:$FF; a:$FF), // #6 input line [Light Blue]
       
    79             (r:$FF; g:$80; b:$80; a:$FF), // #7 team gone [Light Red]
       
    80             (r:$FF; g:$D0; b:$80; a:$FF), // #8 team back [Light Orange]
       
    81             (r:$DF; g:$DF; b:$DF; a:$FF)  // #9 hog speech [Light Gray]
    79             );
    82             );
    80     ChatCommandz: array [TChatCmd] of record
    83     ChatCommandz: array [TChatCmd] of record
    81             ChatCmd: string[31];
    84             ChatCmd: string[31];
    82             ProcedureCallChatCmd: string[31];
    85             ProcedureCallChatCmd: string[31];
    83             end = (
    86             end = (
   150     ResetSelection();
   153     ResetSelection();
   151     cursorPos:= 0;
   154     cursorPos:= 0;
   152     UpdateCursorCoords();
   155     UpdateCursorCoords();
   153 end;
   156 end;
   154 
   157 
       
   158 (* This procedure [re]renders a texture showing str for the chat line cl.
       
   159  * It will use the color stored in cl and update width
       
   160  *)
   155 procedure RenderChatLineTex(var cl: TChatLine; var str: shortstring);
   161 procedure RenderChatLineTex(var cl: TChatLine; var str: shortstring);
   156 var strSurface,
   162 var strSurface,
   157     resSurface: PSDL_Surface;
   163     resSurface: PSDL_Surface;
   158     dstrect   : TSDL_Rect; // destination rectangle for blitting
   164     dstrect   : TSDL_Rect; // destination rectangle for blitting
   159     font      : THWFont;
   165     font      : THWFont;
   160 const
   166 const
   161     shadowint  = $80 shl AShift;
   167     shadowint  = $80 shl AShift;
   162 begin
   168 begin
   163 
   169 
       
   170 FreeAndNilTexture(cl.Tex);
       
   171 
   164 font:= CheckCJKFont(ansistring(str), fnt16);
   172 font:= CheckCJKFont(ansistring(str), fnt16);
   165 
   173 
   166 // get render size of text
   174 // get render size of text
   167 TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), @cl.Width, nil);
   175 TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(str), @cl.Width, nil);
   168 
   176 
   197 const ClDisplayDuration = 12500;
   205 const ClDisplayDuration = 12500;
   198 
   206 
   199 procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean);
   207 procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean);
   200 var color  : TSDL_Color;
   208 var color  : TSDL_Color;
   201 begin
   209 begin
   202 if cl.Tex <> nil then
       
   203     FreeAndNilTexture(cl.Tex);
       
   204 
       
   205 if isInput then
   210 if isInput then
   206     begin
   211     begin
   207     cl.s:= str;
   212     cl.s:= str;
   208     color:= colors[#6];
   213     color:= colors[#6];
   209     str:= str + ' ';
   214     str:= str + ' ';
   230 cl.Time:= RealTicks + ClDisplayDuration;
   235 cl.Time:= RealTicks + ClDisplayDuration;
   231 end;
   236 end;
   232 
   237 
   233 // For uStore texture recreation
   238 // For uStore texture recreation
   234 procedure ReloadLines;
   239 procedure ReloadLines;
   235 var i, t: LongWord;
   240 var i: LongWord;
   236 begin
   241 begin
   237     if InputStr.s <> '' then
   242     if InputStr.s <> '' then
   238         SetLine(InputStr, InputStr.s, true);
   243         SetLine(InputStr, InputStr.s, true);
   239     for i:= 0 to MaxStrIndex do
   244     for i:= 0 to MaxStrIndex do
   240         if Strs[i].s <> '' then
   245         if Strs[i].s <> '' then
   241             begin
   246             begin
   242             t:= Strs[i].Time;
   247             RenderChatLineTex(Strs[i], Strs[i].s);
   243             SetLine(Strs[i], Strs[i].s, false);
       
   244             Strs[i].Time:= t
       
   245             end;
   248             end;
   246 end;
   249 end;
   247 
   250 
   248 procedure AddChatString(s: shortstring);
   251 procedure AddChatString(s: shortstring);
   249 begin
   252 begin
   792         end;
   795         end;
   793 end;
   796 end;
   794 
   797 
   795 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
   798 procedure KeyPressChat(Key, Sym: Longword; Modifier: Word);
   796 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
   799 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
       
   800       nonStateMask = (not (KMOD_NUM or KMOD_CAPS));
   797 var i, btw, index: integer;
   801 var i, btw, index: integer;
   798     utf8: shortstring;
   802     utf8: shortstring;
   799     action, selMode, ctrl: boolean;
   803     action, selMode, ctrl, ctrlonly: boolean;
   800     skip: TCharSkip;
   804     skip: TCharSkip;
   801 begin
   805 begin
   802     LastKeyPressTick:= RealTicks;
   806     LastKeyPressTick:= RealTicks;
   803     action:= true;
   807     action:= true;
   804 
   808 
   805     CheckPasteBuffer();
   809     CheckPasteBuffer();
   806 
   810 
   807     selMode:= (modifier and (KMOD_LSHIFT or KMOD_RSHIFT)) <> 0;
   811     selMode:= (modifier and (KMOD_LSHIFT or KMOD_RSHIFT)) <> 0;
   808     ctrl:= (modifier and (KMOD_LCTRL or KMOD_RCTRL)) <> 0;
   812     ctrl:= (modifier and (KMOD_LCTRL or KMOD_RCTRL)) <> 0;
       
   813     ctrlonly:= ctrl and ((modifier and nonStateMask and (not (KMOD_LCTRL or KMOD_RCTRL))) = 0);
   809     skip:= none;
   814     skip:= none;
   810 
   815 
   811     case Sym of
   816     case Sym of
   812         SDLK_BACKSPACE:
   817         SDLK_BACKSPACE:
   813             begin
   818             begin
   963             // ignore me!!!
   968             // ignore me!!!
   964             end;
   969             end;
   965         SDLK_a:
   970         SDLK_a:
   966             begin
   971             begin
   967             // select all
   972             // select all
   968             if ctrl then
   973             if ctrlonly then
   969                 begin
   974                 begin
   970                 ResetSelection();
   975                 ResetSelection();
   971                 cursorPos:= 0;
   976                 cursorPos:= 0;
   972                 HandleSelection(true);
   977                 HandleSelection(true);
   973                 cursorPos:= Length(InputStr.s);
   978                 cursorPos:= Length(InputStr.s);
   977                 action:= false;
   982                 action:= false;
   978             end;
   983             end;
   979         SDLK_c:
   984         SDLK_c:
   980             begin
   985             begin
   981             // copy
   986             // copy
   982             if ctrl then
   987             if ctrlonly then
   983                 CopySelectionToClipboard()
   988                 CopySelectionToClipboard()
   984             else
   989             else
   985                 action:= false;
   990                 action:= false;
   986             end;
   991             end;
   987         SDLK_v:
   992         SDLK_v:
   988             begin
   993             begin
   989             // paste
   994             // paste
   990             if ctrl then
   995             if ctrlonly then
   991                 begin
   996                 begin
   992                 DeleteSelected();
   997                 DeleteSelected();
   993                 PasteFromClipboard();
   998                 PasteFromClipboard();
   994                 end
   999                 end
   995             else
  1000             else
   996                 action:= false;
  1001                 action:= false;
   997             end;
  1002             end;
   998         SDLK_x:
  1003         SDLK_x:
   999             begin
  1004             begin
  1000             // cut
  1005             // cut
  1001             if ctrl then
  1006             if ctrlonly then
  1002                 begin
  1007                 begin
  1003                 CopySelectionToClipboard();
  1008                 CopySelectionToClipboard();
  1004                 DeleteSelected();
  1009                 DeleteSelected();
  1005                 end
  1010                 end
  1006             else
  1011             else