Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UISearchBar.cs
Go to the documentation of this file.
1using System;
7using Terraria.UI;
8
10
11public class UISearchBar : UIElement
12{
14
16
17 private string actualContents;
18
19 private float _textScale;
20
21 private bool isWritingText;
22
23 public bool HasContents => !string.IsNullOrWhiteSpace(actualContents);
24
26
27 public event Action<string> OnContentsChanged;
28
29 public event Action OnStartTakingInput;
30
31 public event Action OnEndTakingInput;
32
33 public event Action OnCanceledTakingInput;
34
35 public event Action OnNeedingVirtualKeyboard;
36
37 public UISearchBar(LocalizedText emptyContentText, float scale)
38 {
39 _textToShowWhenEmpty = emptyContentText;
40 _textScale = scale;
41 UITextBox uITextBox = new UITextBox("", scale)
42 {
43 HAlign = 0f,
44 VAlign = 0.5f,
45 BackgroundColor = Color.Transparent,
46 BorderColor = Color.Transparent,
47 Width = new StyleDimension(0f, 1f),
48 Height = new StyleDimension(0f, 1f),
49 TextHAlign = 0f,
50 ShowInputTicker = false
51 };
52 uITextBox.SetTextMaxLength(50);
53 Append(uITextBox);
54 _text = uITextBox;
55 }
56
57 public void SetContents(string contents, bool forced = false)
58 {
59 if (!(actualContents == contents) || forced)
60 {
61 actualContents = contents;
62 if (string.IsNullOrEmpty(actualContents))
63 {
64 _text.TextColor = Color.Gray;
66 }
67 else
68 {
69 _text.TextColor = Color.White;
72 }
74 if (this.OnContentsChanged != null)
75 {
76 this.OnContentsChanged(contents);
77 }
78 }
79 }
80
81 public void TrimDisplayIfOverElementDimensions(int padding)
82 {
83 CalculatedStyle dimensions = GetDimensions();
84 if (dimensions.Width != 0f || dimensions.Height != 0f)
85 {
86 Point point = new Point((int)dimensions.X, (int)dimensions.Y);
87 Point point2 = new Point(point.X + (int)dimensions.Width, point.Y + (int)dimensions.Height);
88 Rectangle rectangle = new Rectangle(point.X, point.Y, point2.X - point.X, point2.Y - point.Y);
89 CalculatedStyle dimensions2 = _text.GetDimensions();
90 Point point3 = new Point((int)dimensions2.X, (int)dimensions2.Y);
91 Point point4 = new Point(point3.X + (int)_text.MinWidth.Pixels, point3.Y + (int)_text.MinHeight.Pixels);
92 Rectangle rectangle2 = new Rectangle(point3.X, point3.Y, point4.X - point3.X, point4.Y - point3.Y);
93 int num = 0;
94 while (rectangle2.Right > rectangle.Right - padding && _text.Text.Length > 0)
95 {
96 _text.SetText(_text.Text.Substring(0, _text.Text.Length - 1));
97 num++;
99 dimensions2 = _text.GetDimensions();
100 point3 = new Point((int)dimensions2.X, (int)dimensions2.Y);
101 point4 = new Point(point3.X + (int)_text.MinWidth.Pixels, point3.Y + (int)_text.MinHeight.Pixels);
102 rectangle2 = new Rectangle(point3.X, point3.Y, point4.X - point3.X, point4.Y - point3.Y);
104 }
105 }
106 }
107
108 public override void LeftMouseDown(UIMouseEvent evt)
109 {
110 base.LeftMouseDown(evt);
111 }
112
113 public override void MouseOver(UIMouseEvent evt)
114 {
115 base.MouseOver(evt);
117 }
118
119 public override void Update(GameTime gameTime)
120 {
121 if (isWritingText)
122 {
124 {
125 if (this.OnNeedingVirtualKeyboard != null)
126 {
128 }
129 return;
130 }
131 PlayerInput.WritingText = true;
132 Main.CurrentInputTextTakerOverride = this;
133 }
134 base.Update(gameTime);
135 }
136
137 private bool NeedsVirtualkeyboard()
138 {
140 }
141
142 protected override void DrawSelf(SpriteBatch spriteBatch)
143 {
144 base.DrawSelf(spriteBatch);
145 if (!isWritingText)
146 {
147 return;
148 }
149 PlayerInput.WritingText = true;
150 Main.instance.HandleIME();
151 Vector2 position = new Vector2(Main.screenWidth / 2, _text.GetDimensions().ToRectangle().Bottom + 32);
152 Main.instance.DrawWindowsIMEPanel(position, 0.5f);
153 string inputText = Main.GetInputText(actualContents);
155 {
157 }
158 else if (Main.inputTextEscape)
159 {
161 if (this.OnCanceledTakingInput != null)
162 {
164 }
165 }
166 SetContents(inputText);
167 position = new Vector2(Main.screenWidth / 2, _text.GetDimensions().ToRectangle().Bottom + 32);
168 Main.instance.DrawWindowsIMEPanel(position, 0.5f);
169 }
170
171 public void ToggleTakingText()
172 {
174 _text.ShowInputTicker = isWritingText;
175 Main.clrInput();
176 if (isWritingText)
177 {
178 if (this.OnStartTakingInput != null)
179 {
180 this.OnStartTakingInput();
181 }
182 }
183 else if (this.OnEndTakingInput != null)
184 {
185 this.OnEndTakingInput();
186 }
187 }
188}
static void PlaySound(int type, Vector2 position, int style=1)
void SetContents(string contents, bool forced=false)
UISearchBar(LocalizedText emptyContentText, float scale)
override void LeftMouseDown(UIMouseEvent evt)
override void Update(GameTime gameTime)
override void DrawSelf(SpriteBatch spriteBatch)
override void MouseOver(UIMouseEvent evt)
override void SetText(string text, float textScale, bool large)
Definition UITextBox.cs:31
static string GetInputText(string oldString, bool allowMultiLine=false)
Definition Main.cs:18593
static bool inputTextEscape
Definition Main.cs:1747
static Main instance
Definition Main.cs:283
static void clrInput()
Definition Main.cs:18584
static int screenWidth
Definition Main.cs:1719
static bool inputTextEnter
Definition Main.cs:1745
StyleDimension Height
Definition UIElement.cs:29
void Append(UIElement element)
Definition UIElement.cs:166
virtual void RecalculateChildren()
Definition UIElement.cs:369
StyleDimension Width
Definition UIElement.cs:27
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
static Color Transparent
Definition Color.cs:76