Client LuaCsForBarotrauma
IMEPreviewText.cs
1 #nullable enable
2 
3 using System.Collections.Immutable;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
6 
7 namespace Barotrauma
8 {
9 
10  public sealed class IMEPreviewTextHandler
11  {
12  public bool HasText => !string.IsNullOrEmpty(previewText);
13 
14  // This has to be settable because for some reason we update the font of GUITextBox in some places
15  public GUIFont Font { get; set; }
16 
17  private string previewText = string.Empty;
18  private Vector2 textSize;
19 
20  private bool isSectioned;
21  private ImmutableArray<RichTextData>? richTextData;
22 
24  {
25  Font = font;
26  }
27 
28  public void Reset()
29  {
30  textSize = Vector2.Zero;
31  previewText = string.Empty;
32  richTextData = null;
33  isSectioned = false;
34  }
35 
36  public void UpdateText(string text, int start, int length)
37  {
38  isSectioned = start >= 0 && length > 0;
39  richTextData = null;
40 
41  if (string.IsNullOrEmpty(text))
42  {
43  Reset();
44  return;
45  }
46 
47  previewText = text;
48 
49  textSize = Font.MeasureString(text);
50 
51  if (!isSectioned) { return; }
52 
53  string coloredText = ToolBox.ColorSectionOfString(text, start, length, GUIStyle.Orange);
54 
55  RichString richString = RichString.Rich(coloredText);
56 
57  previewText = richString.SanitizedValue;
58  richTextData = richString.RichTextData;
59  }
60 
61  public void DrawIMEPreview(SpriteBatch spriteBatch, Vector2 position, GUITextBlock textBlock)
62  {
63  if (!HasText) { return; }
64 
65  int inflate = GUI.IntScale(3);
66 
67  RectangleF rect = new RectangleF(position, textSize);
68  rect.Inflate(inflate, inflate);
69 
70  RectangleF borderRect = rect;
71  borderRect.Inflate(1, 1);
72 
73  GUI.DrawFilledRectangle(spriteBatch, borderRect, Color.White, depth: 0.02f);
74  GUI.DrawFilledRectangle(spriteBatch, rect, Color.Black, depth: 0.01f);
75 
76  Font.DrawStringWithColors(spriteBatch,
77  text: previewText,
78  position: position,
79  color: isSectioned ? GUIStyle.TextColorNormal : GUIStyle.Orange,
80  rotation: 0.0f,
81  origin: Vector2.Zero,
82  scale: 1f,
83  spriteEffects: SpriteEffects.None,
84  layerDepth: 0,
85  richTextData: richTextData,
86  alignment: textBlock.TextAlignment,
87  forceUpperCase: textBlock.ForceUpperCase);
88  }
89  }
90 }
Vector2 MeasureString(LocalizedString str, bool removeExtraSpacing=false)
Definition: GUIPrefab.cs:284
void DrawStringWithColors(SpriteBatch sb, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects spriteEffects, float layerDepth, in ImmutableArray< RichTextData >? richTextData, int rtdOffset=0, Alignment alignment=Alignment.TopLeft, ForceUpperCase forceUpperCase=Barotrauma.ForceUpperCase.Inherit)
Definition: GUIPrefab.cs:279
ForceUpperCase ForceUpperCase
void UpdateText(string text, int start, int length)
void DrawIMEPreview(SpriteBatch spriteBatch, Vector2 position, GUITextBlock textBlock)
ImmutableArray< RichTextData >? RichTextData
Definition: RichString.cs:42
static RichString Rich(LocalizedString str, Func< string, string >? postProcess=null)
Definition: RichString.cs:67