hedgewars/uChat.pas
changeset 15954 6a985a680c44
parent 15953 416bca0a172c
child 15955 a4630009e733
--- a/hedgewars/uChat.pas	Wed May 24 21:23:33 2023 +0200
+++ b/hedgewars/uChat.pas	Wed May 24 23:54:58 2023 +0200
@@ -28,6 +28,7 @@
 procedure ReloadLines;
 procedure CleanupInput;
 procedure CloseChat;
+procedure RestoreChat;
 procedure AddChatString(s: shortstring);
 procedure DrawChat;
 procedure KeyPressChat(keysym: TSDL_Keysym);
@@ -55,6 +56,7 @@
 var Strs: array[0 .. MaxStrIndex] of TChatLine;
     MStrs: array[0 .. MaxStrIndex] of shortstring;
     LocalStrs: array[0 .. MaxStrIndex] of shortstring;
+    oldInput: shortstring;
     missedCount: LongWord;
     lastStr: LongWord;
     localLastStr: LongInt;
@@ -783,13 +785,43 @@
     ResetKbd;
 end;
 
+procedure OpenChat(s: shortstring);
+var i: Integer;
+begin
+    isInChatMode:= true;
+    SDL_StopTextInput();
+    SDL_StartTextInput();
+    //Make REALLY sure unexpected events are flushed (1 time is insufficient as of SDL 2.0.7)
+    for i := 1 to 2 do
+    begin
+        SDL_PumpEvents();
+        SDL_FlushEvent(SDL_TEXTINPUT);
+    end;
+    if length(s) = 0 then
+        SetLine(InputStr, '', true)
+    else
+        begin
+        SetLine(InputStr, s, true);
+        cursorPos:= length(s);
+        UpdateCursorCoords();
+        end;
+end;
+
 procedure CloseChat;
 begin
+    oldInput:= InputStr.s;
     SetLine(InputStr, '', true);
     ResetCursor();
     CleanupInput();
 end;
 
+procedure RestoreChat;
+begin
+    if length(oldInput) > 0 then
+        OpenChat(oldInput);
+    oldInput:= '';
+end;
+
 procedure DelBytesFromInputStrBack(endIdx: integer; count: byte);
 var startIdx: integer;
 begin
@@ -1070,7 +1102,9 @@
                 SetLine(InputStr, '', true);
                 ResetCursor();
                 end
-            else CleanupInput
+            else
+                CleanupInput;
+            oldInput:= '';
             end;
         SDL_SCANCODE_RETURN, SDL_SCANCODE_KP_ENTER:
             begin
@@ -1345,27 +1379,12 @@
 end;
 
 procedure chChat(var s: shortstring);
-var i: Integer;
 begin
     s:= s; // avoid compiler hint
-    isInChatMode:= true;
-    SDL_StopTextInput();
-    SDL_StartTextInput();
-    //Make REALLY sure unexpected events are flushed (1 time is insufficient as of SDL 2.0.7)
-    for i := 1 to 2 do
-    begin
-        SDL_PumpEvents();
-        SDL_FlushEvent(SDL_TEXTINPUT);
-    end;
-    //SDL_EnableKeyRepeat(200,45);
     if length(s) = 0 then
-        SetLine(InputStr, '', true)
+        OpenChat('')
     else
-        begin
-        SetLine(InputStr, '/clan ', true);
-        cursorPos:= 6;
-        UpdateCursorCoords();
-        end;
+        OpenChat('/clan ');
 end;
 
 procedure initModule;