Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UIWorldListItem.cs
Go to the documentation of this file.
4using ReLogic.OS;
7using Terraria.IO;
10using Terraria.UI;
11
13
15{
17
19
21
23
25
27
29
31
33
35
37
39
41
43
44 private int _orderInList;
45
46 private bool _canBePlayed;
47
48 public bool IsFavorite => _data.IsFavorite;
49
50 public UIWorldListItem(WorldFileData data, int orderInList, bool canBePlayed)
51 {
52 _orderInList = orderInList;
53 _data = data;
54 _canBePlayed = canBePlayed;
58 _worldIcon.OnLeftDoubleClick += PlayGame;
61 {
62 UIImage element = new UIImage(Main.Assets.Request<Texture2D>("Images/UI/IconCompletion", (AssetRequestMode)1))
63 {
64 HAlign = 0.5f,
65 VAlign = 0.5f,
66 Top = new StyleDimension(-10f, 0f),
67 Left = new StyleDimension(-3f, 0f),
69 };
70 _worldIcon.Append(element);
71 }
72 float num = 4f;
74 uIImageButton.VAlign = 1f;
75 uIImageButton.Left.Set(num, 0f);
76 uIImageButton.OnLeftClick += PlayGame;
77 base.OnLeftDoubleClick += PlayGame;
78 uIImageButton.OnMouseOver += PlayMouseOver;
79 uIImageButton.OnMouseOut += ButtonMouseOut;
80 Append(uIImageButton);
81 num += 24f;
83 uIImageButton2.VAlign = 1f;
84 uIImageButton2.Left.Set(num, 0f);
85 uIImageButton2.OnLeftClick += FavoriteButtonClick;
86 uIImageButton2.OnMouseOver += FavoriteMouseOver;
87 uIImageButton2.OnMouseOut += ButtonMouseOut;
88 uIImageButton2.SetVisibility(1f, _data.IsFavorite ? 0.8f : 0.4f);
89 Append(uIImageButton2);
90 num += 24f;
91 if (SocialAPI.Cloud != null)
92 {
94 uIImageButton3.VAlign = 1f;
95 uIImageButton3.Left.Set(num, 0f);
96 uIImageButton3.OnLeftClick += CloudButtonClick;
97 uIImageButton3.OnMouseOver += CloudMouseOver;
98 uIImageButton3.OnMouseOut += ButtonMouseOut;
99 uIImageButton3.SetSnapPoint("Cloud", orderInList);
100 Append(uIImageButton3);
101 num += 24f;
102 }
104 {
105 UIImageButton uIImageButton4 = new UIImageButton(_buttonSeedTexture);
106 uIImageButton4.VAlign = 1f;
107 uIImageButton4.Left.Set(num, 0f);
108 uIImageButton4.OnLeftClick += SeedButtonClick;
109 uIImageButton4.OnMouseOver += SeedMouseOver;
110 uIImageButton4.OnMouseOut += ButtonMouseOut;
111 uIImageButton4.SetSnapPoint("Seed", orderInList);
112 Append(uIImageButton4);
113 num += 24f;
114 }
116 uIImageButton5.VAlign = 1f;
117 uIImageButton5.Left.Set(num, 0f);
118 uIImageButton5.OnLeftClick += RenameButtonClick;
119 uIImageButton5.OnMouseOver += RenameMouseOver;
120 uIImageButton5.OnMouseOut += ButtonMouseOut;
121 uIImageButton5.SetSnapPoint("Rename", orderInList);
122 Append(uIImageButton5);
123 num += 24f;
125 {
126 VAlign = 1f,
127 HAlign = 1f
128 };
129 if (!_data.IsFavorite)
130 {
131 uIImageButton6.OnLeftClick += DeleteButtonClick;
132 }
133 uIImageButton6.OnMouseOver += DeleteMouseOver;
134 uIImageButton6.OnMouseOut += DeleteMouseOut;
135 _deleteButton = uIImageButton6;
136 Append(uIImageButton6);
137 num += 4f;
138 _buttonLabel = new UIText("");
139 _buttonLabel.VAlign = 1f;
140 _buttonLabel.Left.Set(num, 0f);
141 _buttonLabel.Top.Set(-3f, 0f);
143 _deleteButtonLabel = new UIText("");
144 _deleteButtonLabel.VAlign = 1f;
145 _deleteButtonLabel.HAlign = 1f;
146 _deleteButtonLabel.Left.Set(-30f, 0f);
147 _deleteButtonLabel.Top.Set(-3f, 0f);
149 uIImageButton.SetSnapPoint("Play", orderInList);
150 uIImageButton2.SetSnapPoint("Favorite", orderInList);
151 uIImageButton5.SetSnapPoint("Rename", orderInList);
152 uIImageButton6.SetSnapPoint("Delete", orderInList);
153 }
154
155 private void LoadTextures()
156 {
157 _dividerTexture = Main.Assets.Request<Texture2D>("Images/UI/Divider", (AssetRequestMode)1);
158 _innerPanelTexture = Main.Assets.Request<Texture2D>("Images/UI/InnerPanelBackground", (AssetRequestMode)1);
159 _buttonCloudActiveTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonCloudActive", (AssetRequestMode)1);
160 _buttonCloudInactiveTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonCloudInactive", (AssetRequestMode)1);
161 _buttonFavoriteActiveTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonFavoriteActive", (AssetRequestMode)1);
162 _buttonFavoriteInactiveTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonFavoriteInactive", (AssetRequestMode)1);
163 _buttonPlayTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonPlay", (AssetRequestMode)1);
164 _buttonSeedTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonSeed", (AssetRequestMode)1);
165 _buttonRenameTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonRename", (AssetRequestMode)1);
166 _buttonDeleteTexture = Main.Assets.Request<Texture2D>("Images/UI/ButtonDelete", (AssetRequestMode)1);
167 }
168
169 private void InitializeAppearance()
170 {
171 Height.Set(96f, 0f);
172 Width.Set(0f, 1f);
173 SetPadding(6f);
175 }
176
177 private void SetColorsToHovered()
178 {
179 BackgroundColor = new Color(73, 94, 171);
180 BorderColor = new Color(89, 116, 213);
181 if (!_canBePlayed)
182 {
183 BorderColor = new Color(150, 150, 150) * 1f;
184 BackgroundColor = Color.Lerp(BackgroundColor, new Color(120, 120, 120), 0.5f) * 1f;
185 }
186 }
187
189 {
190 BackgroundColor = new Color(63, 82, 151) * 0.7f;
191 BorderColor = new Color(89, 116, 213) * 0.7f;
192 if (!_canBePlayed)
193 {
194 BorderColor = new Color(127, 127, 127) * 0.7f;
195 BackgroundColor = Color.Lerp(new Color(63, 82, 151), new Color(80, 80, 80), 0.5f) * 0.7f;
196 }
197 }
198
199 private void RenameMouseOver(UIMouseEvent evt, UIElement listeningElement)
200 {
202 }
203
204 private void FavoriteMouseOver(UIMouseEvent evt, UIElement listeningElement)
205 {
206 if (_data.IsFavorite)
207 {
208 _buttonLabel.SetText(Language.GetTextValue("UI.Unfavorite"));
209 }
210 else
211 {
213 }
214 }
215
216 private void CloudMouseOver(UIMouseEvent evt, UIElement listeningElement)
217 {
218 if (_data.IsCloudSave)
219 {
220 _buttonLabel.SetText(Language.GetTextValue("UI.MoveOffCloud"));
221 }
222 else
223 {
224 _buttonLabel.SetText(Language.GetTextValue("UI.MoveToCloud"));
225 }
226 }
227
228 private void PlayMouseOver(UIMouseEvent evt, UIElement listeningElement)
229 {
231 }
232
233 private void SeedMouseOver(UIMouseEvent evt, UIElement listeningElement)
234 {
235 _buttonLabel.SetText(Language.GetTextValue("UI.CopySeed", _data.GetFullSeedText(allowCropping: true)));
236 }
237
238 private void DeleteMouseOver(UIMouseEvent evt, UIElement listeningElement)
239 {
240 if (_data.IsFavorite)
241 {
242 _deleteButtonLabel.SetText(Language.GetTextValue("UI.CannotDeleteFavorited"));
243 }
244 else
245 {
247 }
248 }
249
250 private void DeleteMouseOut(UIMouseEvent evt, UIElement listeningElement)
251 {
253 }
254
255 private void ButtonMouseOut(UIMouseEvent evt, UIElement listeningElement)
256 {
258 }
259
260 private void CloudButtonClick(UIMouseEvent evt, UIElement listeningElement)
261 {
262 if (_data.IsCloudSave)
263 {
265 }
266 else
267 {
269 }
271 if (_data.IsCloudSave)
272 {
273 _buttonLabel.SetText(Language.GetTextValue("UI.MoveOffCloud"));
274 }
275 else
276 {
277 _buttonLabel.SetText(Language.GetTextValue("UI.MoveToCloud"));
278 }
279 }
280
281 private void DeleteButtonClick(UIMouseEvent evt, UIElement listeningElement)
282 {
283 for (int i = 0; i < Main.WorldList.Count; i++)
284 {
285 if (Main.WorldList[i] == _data)
286 {
288 Main.selectedWorld = i;
289 Main.menuMode = 9;
290 break;
291 }
292 }
293 }
294
295 private void PlayGame(UIMouseEvent evt, UIElement listeningElement)
296 {
297 if (listeningElement == evt.Target && !TryMovingToRejectionMenuIfNeeded(_data.GameMode))
298 {
301 Main.clrInput();
302 Main.GetInputText("");
303 if (Main.menuMultiplayer && SocialAPI.Network != null)
304 {
305 Main.menuMode = 889;
306 }
307 else if (Main.menuMultiplayer)
308 {
309 Main.menuMode = 30;
310 }
311 else
312 {
313 Main.menuMode = 10;
314 }
316 {
318 }
319 }
320 }
321
322 private bool TryMovingToRejectionMenuIfNeeded(int worldGameMode)
323 {
324 if (!Main.RegisteredGameModes.TryGetValue(worldGameMode, out var value))
325 {
327 Main.statusText = Language.GetTextValue("UI.WorldCannotBeLoadedBecauseItHasAnInvalidGameMode");
328 Main.menuMode = 1000000;
329 return true;
330 }
331 bool flag = Main.ActivePlayerFileData.Player.difficulty == 3;
332 bool isJourneyMode = value.IsJourneyMode;
333 if (flag && !isJourneyMode)
334 {
336 Main.statusText = Language.GetTextValue("UI.PlayerIsCreativeAndWorldIsNotCreative");
337 Main.menuMode = 1000000;
338 return true;
339 }
340 if (!flag && isJourneyMode)
341 {
343 Main.statusText = Language.GetTextValue("UI.PlayerIsNotCreativeAndWorldIsCreative");
344 Main.menuMode = 1000000;
345 return true;
346 }
347 return false;
348 }
349
350 private void RenameButtonClick(UIMouseEvent evt, UIElement listeningElement)
351 {
353 Main.clrInput();
354 UIVirtualKeyboard uIVirtualKeyboard = new UIVirtualKeyboard(Lang.menu[48].Value, "", OnFinishedSettingName, GoBackHere, 0, allowEmpty: true);
355 uIVirtualKeyboard.SetMaxInputLength(27);
356 Main.MenuUI.SetState(uIVirtualKeyboard);
357 if (base.Parent.Parent is UIList uIList)
358 {
359 uIList.UpdateOrder();
360 }
361 }
362
363 private void OnFinishedSettingName(string name)
364 {
365 string newDisplayName = name.Trim();
366 Main.menuMode = 10;
367 _data.Rename(newDisplayName);
368 }
369
370 private void GoBackHere()
371 {
373 }
374
375 private void FavoriteButtonClick(UIMouseEvent evt, UIElement listeningElement)
376 {
379 ((UIImageButton)evt.Target).SetVisibility(1f, _data.IsFavorite ? 0.8f : 0.4f);
380 if (_data.IsFavorite)
381 {
382 _buttonLabel.SetText(Language.GetTextValue("UI.Unfavorite"));
383 _deleteButton.OnLeftClick -= DeleteButtonClick;
384 }
385 else
386 {
388 _deleteButton.OnLeftClick += DeleteButtonClick;
389 }
390 if (base.Parent.Parent is UIList uIList)
391 {
392 uIList.UpdateOrder();
393 }
394 }
395
396 private void SeedButtonClick(UIMouseEvent evt, UIElement listeningElement)
397 {
398 Platform.Get<IClipboard>().Value = _data.GetFullSeedText();
399 _buttonLabel.SetText(Language.GetTextValue("UI.SeedCopied"));
400 }
401
402 public override int CompareTo(object obj)
403 {
404 if (obj is UIWorldListItem uIWorldListItem)
405 {
406 return _orderInList.CompareTo(uIWorldListItem._orderInList);
407 }
408 return base.CompareTo(obj);
409 }
410
411 public override void MouseOver(UIMouseEvent evt)
412 {
413 base.MouseOver(evt);
415 }
416
417 public override void MouseOut(UIMouseEvent evt)
418 {
419 base.MouseOut(evt);
421 }
422
423 private void DrawPanel(SpriteBatch spriteBatch, Vector2 position, float width)
424 {
425 spriteBatch.Draw(_innerPanelTexture.Value, position, new Rectangle(0, 0, 8, _innerPanelTexture.Height()), Color.White);
426 spriteBatch.Draw(_innerPanelTexture.Value, new Vector2(position.X + 8f, position.Y), new Rectangle(8, 0, 8, _innerPanelTexture.Height()), Color.White, 0f, Vector2.Zero, new Vector2((width - 16f) / 8f, 1f), SpriteEffects.None, 0f);
427 spriteBatch.Draw(_innerPanelTexture.Value, new Vector2(position.X + width - 8f, position.Y), new Rectangle(16, 0, 8, _innerPanelTexture.Height()), Color.White);
428 }
429
430 protected override void DrawSelf(SpriteBatch spriteBatch)
431 {
432 base.DrawSelf(spriteBatch);
433 CalculatedStyle innerDimensions = GetInnerDimensions();
435 float num = dimensions.X + dimensions.Width;
436 Color color = (_data.IsValid ? Color.White : Color.Gray);
437 string worldName = _data.GetWorldName(allowCropping: true);
438 Utils.DrawBorderString(spriteBatch, worldName, new Vector2(num + 6f, dimensions.Y - 2f), color);
439 spriteBatch.Draw(_dividerTexture.Value, new Vector2(num, innerDimensions.Y + 21f), null, Color.White, 0f, Vector2.Zero, new Vector2((GetDimensions().X + GetDimensions().Width - num) / 8f, 1f), SpriteEffects.None, 0f);
440 Vector2 vector = new Vector2(num + 6f, innerDimensions.Y + 29f);
441 float num2 = 100f;
442 DrawPanel(spriteBatch, vector, num2);
443 GetDifficulty(out var expertText, out var gameModeColor);
444 float x = FontAssets.MouseText.Value.MeasureString(expertText).X;
445 float x2 = num2 * 0.5f - x * 0.5f;
446 Utils.DrawBorderString(spriteBatch, expertText, vector + new Vector2(x2, 3f), gameModeColor);
447 vector.X += num2 + 5f;
448 float num3 = 150f;
449 if (!GameCulture.FromCultureName(GameCulture.CultureName.English).IsActive)
450 {
451 num3 += 40f;
452 }
453 DrawPanel(spriteBatch, vector, num3);
454 string textValue = Language.GetTextValue("UI.WorldSizeFormat", _data.WorldSizeName);
455 float x3 = FontAssets.MouseText.Value.MeasureString(textValue).X;
456 float x4 = num3 * 0.5f - x3 * 0.5f;
457 Utils.DrawBorderString(spriteBatch, textValue, vector + new Vector2(x4, 3f), Color.White);
458 vector.X += num3 + 5f;
459 float num4 = innerDimensions.X + innerDimensions.Width - vector.X;
460 DrawPanel(spriteBatch, vector, num4);
461 string arg = ((!GameCulture.FromCultureName(GameCulture.CultureName.English).IsActive) ? _data.CreationTime.ToShortDateString() : _data.CreationTime.ToString("d MMMM yyyy"));
462 string textValue2 = Language.GetTextValue("UI.WorldCreatedFormat", arg);
463 float x5 = FontAssets.MouseText.Value.MeasureString(textValue2).X;
464 float x6 = num4 * 0.5f - x5 * 0.5f;
465 Utils.DrawBorderString(spriteBatch, textValue2, vector + new Vector2(x6, 3f), Color.White);
466 vector.X += num4 + 5f;
467 }
468}
void Draw(Texture2D texture, Vector2 position, Color color)
static void PlaySound(int type, Vector2 position, int style=1)
static Asset< DynamicSpriteFont > MouseText
Definition FontAssets.cs:10
void GetDifficulty(out string expertText, out Color gameModeColor)
void SetVisibility(float whenActive, float whenInactive)
void PlayMouseOver(UIMouseEvent evt, UIElement listeningElement)
void DeleteMouseOver(UIMouseEvent evt, UIElement listeningElement)
void DeleteButtonClick(UIMouseEvent evt, UIElement listeningElement)
void SeedMouseOver(UIMouseEvent evt, UIElement listeningElement)
void CloudMouseOver(UIMouseEvent evt, UIElement listeningElement)
void RenameButtonClick(UIMouseEvent evt, UIElement listeningElement)
void DeleteMouseOut(UIMouseEvent evt, UIElement listeningElement)
void ButtonMouseOut(UIMouseEvent evt, UIElement listeningElement)
void SeedButtonClick(UIMouseEvent evt, UIElement listeningElement)
void DrawPanel(SpriteBatch spriteBatch, Vector2 position, float width)
void FavoriteButtonClick(UIMouseEvent evt, UIElement listeningElement)
void PlayGame(UIMouseEvent evt, UIElement listeningElement)
override void DrawSelf(SpriteBatch spriteBatch)
UIWorldListItem(WorldFileData data, int orderInList, bool canBePlayed)
void RenameMouseOver(UIMouseEvent evt, UIElement listeningElement)
void CloudButtonClick(UIMouseEvent evt, UIElement listeningElement)
void FavoriteMouseOver(UIMouseEvent evt, UIElement listeningElement)
string GetWorldName(bool allowCropping=false)
override void SetAsActive()
override void MoveToCloud()
string GetFullSeedText(bool allowCropping=false)
void Rename(string newDisplayName)
override void MoveToLocal()
static LocalizedText[] menu
Definition Lang.cs:19
static GameCulture FromCultureName(CultureName name)
static string GetTextValue(string key)
Definition Language.cs:15
static string GetInputText(string oldString, bool allowMultiLine=false)
Definition Main.cs:18593
static List< WorldFileData > WorldList
Definition Main.cs:1944
static UserInterface MenuUI
Definition Main.cs:381
static void GoToWorldSelect()
Definition Main.cs:45201
static void clrInput()
Definition Main.cs:18584
static IAssetRepository Assets
Definition Main.cs:209
static bool menuMultiplayer
Definition Main.cs:2091
static Dictionary< int, GameModeData > RegisteredGameModes
Definition Main.cs:315
static Terraria.Social.Base.CloudSocialModule Cloud
Definition SocialAPI.cs:18
static Terraria.Social.Base.NetSocialModule Network
Definition SocialAPI.cs:20
StyleDimension Height
Definition UIElement.cs:29
void Append(UIElement element)
Definition UIElement.cs:166
CalculatedStyle GetInnerDimensions()
Definition UIElement.cs:377
StyleDimension Left
Definition UIElement.cs:25
void SetSnapPoint(string name, int id, Vector2? anchor=null, Vector2? offset=null)
Definition UIElement.cs:122
StyleDimension Width
Definition UIElement.cs:27
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
void SetPadding(float pixels)
Definition UIElement.cs:361
StyleDimension Top
Definition UIElement.cs:23
readonly UIElement Target
Definition UIEvent.cs:5
static Vector2 DrawBorderString(SpriteBatch sb, string text, Vector2 pos, Color color, float scale=1f, float anchorx=0f, float anchory=0f, int maxCharactersDisplayed=-1)
Definition Utils.cs:1891
static void playWorld()
Definition WorldGen.cs:3288
static Color Lerp(Color value1, Color value2, float amount)
Definition Color.cs:491
void Set(float pixels, float precent)