Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PopupText.cs
Go to the documentation of this file.
1using System;
5
6namespace Terraria;
7
8public class PopupText
9{
11
13
14 public float alpha;
15
16 public int alphaDir = 1;
17
18 public string name;
19
20 public long stack;
21
22 public float scale = 1f;
23
24 public float rotation;
25
26 public Color color;
27
28 public bool active;
29
30 public int lifeTime;
31
32 public static int activeTime = 60;
33
34 public static int numActive;
35
36 public bool NoStack;
37
38 public bool coinText;
39
40 public long coinValue;
41
42 public static int sonarText = -1;
43
44 public bool expert;
45
46 public bool master;
47
48 public bool sonar;
49
51
52 public int npcNetID;
53
54 public bool freeAdvanced;
55
57 {
58 get
59 {
60 if (npcNetID == 0)
61 {
62 return freeAdvanced;
63 }
64 return true;
65 }
66 }
67
68 public static float TargetScale => Main.UIScale / Main.GameViewMatrix.Zoom.X;
69
70 public static void ClearSonarText()
71 {
72 if (sonarText >= 0 && Main.popupText[sonarText].sonar)
73 {
74 Main.popupText[sonarText].active = false;
75 sonarText = -1;
76 }
77 }
78
79 public static void ResetText(PopupText text)
80 {
81 text.NoStack = false;
82 text.coinText = false;
83 text.coinValue = 0L;
84 text.sonar = false;
85 text.npcNetID = 0;
86 text.expert = false;
87 text.master = false;
88 text.freeAdvanced = false;
89 text.scale = 0f;
90 text.rotation = 0f;
91 text.alpha = 1f;
92 text.alphaDir = -1;
93 }
94
95 public static int NewText(AdvancedPopupRequest request, Vector2 position)
96 {
97 if (!Main.showItemText)
98 {
99 return -1;
100 }
101 if (Main.netMode == 2)
102 {
103 return -1;
104 }
105 int num = FindNextItemTextSlot();
106 if (num >= 0)
107 {
108 string text = request.Text;
109 Vector2 vector = FontAssets.MouseText.Value.MeasureString(text);
111 ResetText(obj);
112 obj.active = true;
113 obj.position = position - vector / 2f;
114 obj.name = text;
115 obj.stack = 1L;
116 obj.velocity = request.Velocity;
117 obj.lifeTime = request.DurationInFrames;
118 obj.context = PopupTextContext.Advanced;
119 obj.freeAdvanced = true;
120 obj.color = request.Color;
121 }
122 return num;
123 }
124
125 public static int NewText(PopupTextContext context, int npcNetID, Vector2 position, bool stay5TimesLonger)
126 {
127 if (!Main.showItemText)
128 {
129 return -1;
130 }
131 if (npcNetID == 0)
132 {
133 return -1;
134 }
135 if (Main.netMode == 2)
136 {
137 return -1;
138 }
139 int num = FindNextItemTextSlot();
140 if (num >= 0)
141 {
142 NPC nPC = new NPC();
144 string typeName = nPC.TypeName;
145 Vector2 vector = FontAssets.MouseText.Value.MeasureString(typeName);
146 PopupText popupText = Main.popupText[num];
147 ResetText(popupText);
148 popupText.active = true;
149 popupText.position = position - vector / 2f;
150 popupText.name = typeName;
151 popupText.stack = 1L;
152 popupText.velocity.Y = -7f;
153 popupText.lifeTime = 60;
154 popupText.context = context;
155 if (stay5TimesLonger)
156 {
157 popupText.lifeTime *= 5;
158 }
159 popupText.npcNetID = npcNetID;
160 popupText.color = Color.White;
161 if (context == PopupTextContext.SonarAlert)
162 {
163 popupText.color = Color.Lerp(Color.White, Color.Crimson, 0.5f);
164 }
165 }
166 return num;
167 }
168
169 public static int NewText(PopupTextContext context, Item newItem, int stack, bool noStack = false, bool longText = false)
170 {
171 if (!Main.showItemText)
172 {
173 return -1;
174 }
175 if (newItem.Name == null || !newItem.active)
176 {
177 return -1;
178 }
179 if (Main.netMode == 2)
180 {
181 return -1;
182 }
183 bool flag = newItem.type >= 71 && newItem.type <= 74;
184 for (int i = 0; i < 20; i++)
185 {
186 PopupText popupText = Main.popupText[i];
187 if (!popupText.active || popupText.notActuallyAnItem || (!(popupText.name == newItem.AffixName()) && (!flag || !popupText.coinText)) || popupText.NoStack || noStack)
188 {
189 continue;
190 }
191 string text = newItem.Name + " (" + (popupText.stack + stack) + ")";
192 string text2 = newItem.Name;
193 if (popupText.stack > 1)
194 {
195 text2 = text2 + " (" + popupText.stack + ")";
196 }
197 Vector2 vector = FontAssets.MouseText.Value.MeasureString(text2);
198 vector = FontAssets.MouseText.Value.MeasureString(text);
199 if (popupText.lifeTime < 0)
200 {
201 popupText.scale = 1f;
202 }
203 if (popupText.lifeTime < 60)
204 {
205 popupText.lifeTime = 60;
206 }
207 if (flag && popupText.coinText)
208 {
209 long num = 0L;
210 if (newItem.type == 71)
211 {
212 num += stack;
213 }
214 else if (newItem.type == 72)
215 {
216 num += 100 * stack;
217 }
218 else if (newItem.type == 73)
219 {
220 num += 10000 * stack;
221 }
222 else if (newItem.type == 74)
223 {
224 num += 1000000 * stack;
225 }
226 popupText.AddToCoinValue(num);
227 text = ValueToName(popupText.coinValue);
228 vector = FontAssets.MouseText.Value.MeasureString(text);
229 popupText.name = text;
230 if (popupText.coinValue >= 1000000)
231 {
232 if (popupText.lifeTime < 300)
233 {
234 popupText.lifeTime = 300;
235 }
236 popupText.color = new Color(220, 220, 198);
237 }
238 else if (popupText.coinValue >= 10000)
239 {
240 if (popupText.lifeTime < 240)
241 {
242 popupText.lifeTime = 240;
243 }
244 popupText.color = new Color(224, 201, 92);
245 }
246 else if (popupText.coinValue >= 100)
247 {
248 if (popupText.lifeTime < 180)
249 {
250 popupText.lifeTime = 180;
251 }
252 popupText.color = new Color(181, 192, 193);
253 }
254 else if (popupText.coinValue >= 1)
255 {
256 if (popupText.lifeTime < 120)
257 {
258 popupText.lifeTime = 120;
259 }
260 popupText.color = new Color(246, 138, 96);
261 }
262 }
263 popupText.stack += stack;
264 popupText.scale = 0f;
265 popupText.rotation = 0f;
266 popupText.position.X = newItem.position.X + (float)newItem.width * 0.5f - vector.X * 0.5f;
267 popupText.position.Y = newItem.position.Y + (float)newItem.height * 0.25f - vector.Y * 0.5f;
268 popupText.velocity.Y = -7f;
269 popupText.context = context;
270 popupText.npcNetID = 0;
271 if (popupText.coinText)
272 {
273 popupText.stack = 1L;
274 }
275 return i;
276 }
277 int num2 = FindNextItemTextSlot();
278 if (num2 >= 0)
279 {
280 string text3 = newItem.AffixName();
281 if (stack > 1)
282 {
283 text3 = text3 + " (" + stack + ")";
284 }
285 Vector2 vector2 = FontAssets.MouseText.Value.MeasureString(text3);
286 PopupText popupText2 = Main.popupText[num2];
287 ResetText(popupText2);
288 popupText2.active = true;
289 popupText2.position.X = newItem.position.X + (float)newItem.width * 0.5f - vector2.X * 0.5f;
290 popupText2.position.Y = newItem.position.Y + (float)newItem.height * 0.25f - vector2.Y * 0.5f;
291 popupText2.color = Color.White;
292 if (newItem.rare == 1)
293 {
294 popupText2.color = new Color(150, 150, 255);
295 }
296 else if (newItem.rare == 2)
297 {
298 popupText2.color = new Color(150, 255, 150);
299 }
300 else if (newItem.rare == 3)
301 {
302 popupText2.color = new Color(255, 200, 150);
303 }
304 else if (newItem.rare == 4)
305 {
306 popupText2.color = new Color(255, 150, 150);
307 }
308 else if (newItem.rare == 5)
309 {
310 popupText2.color = new Color(255, 150, 255);
311 }
312 else if (newItem.rare == -13)
313 {
314 popupText2.master = true;
315 }
316 else if (newItem.rare == -11)
317 {
318 popupText2.color = new Color(255, 175, 0);
319 }
320 else if (newItem.rare == -1)
321 {
322 popupText2.color = new Color(130, 130, 130);
323 }
324 else if (newItem.rare == 6)
325 {
326 popupText2.color = new Color(210, 160, 255);
327 }
328 else if (newItem.rare == 7)
329 {
330 popupText2.color = new Color(150, 255, 10);
331 }
332 else if (newItem.rare == 8)
333 {
334 popupText2.color = new Color(255, 255, 10);
335 }
336 else if (newItem.rare == 9)
337 {
338 popupText2.color = new Color(5, 200, 255);
339 }
340 else if (newItem.rare == 10)
341 {
342 popupText2.color = new Color(255, 40, 100);
343 }
344 else if (newItem.rare >= 11)
345 {
346 popupText2.color = new Color(180, 40, 255);
347 }
348 popupText2.expert = newItem.expert;
349 popupText2.name = newItem.AffixName();
350 popupText2.stack = stack;
351 popupText2.velocity.Y = -7f;
352 popupText2.lifeTime = 60;
353 popupText2.context = context;
354 if (longText)
355 {
356 popupText2.lifeTime *= 5;
357 }
358 popupText2.coinValue = 0L;
359 popupText2.coinText = newItem.type >= 71 && newItem.type <= 74;
360 if (popupText2.coinText)
361 {
362 long num3 = 0L;
363 if (newItem.type == 71)
364 {
365 num3 += popupText2.stack;
366 }
367 else if (newItem.type == 72)
368 {
369 num3 += 100 * popupText2.stack;
370 }
371 else if (newItem.type == 73)
372 {
373 num3 += 10000 * popupText2.stack;
374 }
375 else if (newItem.type == 74)
376 {
377 num3 += 1000000 * popupText2.stack;
378 }
379 popupText2.AddToCoinValue(num3);
380 popupText2.ValueToName();
381 popupText2.stack = 1L;
382 if (popupText2.coinValue >= 1000000)
383 {
384 if (popupText2.lifeTime < 300)
385 {
386 popupText2.lifeTime = 300;
387 }
388 popupText2.color = new Color(220, 220, 198);
389 }
390 else if (popupText2.coinValue >= 10000)
391 {
392 if (popupText2.lifeTime < 240)
393 {
394 popupText2.lifeTime = 240;
395 }
396 popupText2.color = new Color(224, 201, 92);
397 }
398 else if (popupText2.coinValue >= 100)
399 {
400 if (popupText2.lifeTime < 180)
401 {
402 popupText2.lifeTime = 180;
403 }
404 popupText2.color = new Color(181, 192, 193);
405 }
406 else if (popupText2.coinValue >= 1)
407 {
408 if (popupText2.lifeTime < 120)
409 {
410 popupText2.lifeTime = 120;
411 }
412 popupText2.color = new Color(246, 138, 96);
413 }
414 }
415 }
416 return num2;
417 }
418
419 private void AddToCoinValue(long addedValue)
420 {
421 long val = coinValue + addedValue;
422 coinValue = Math.Min(999999999L, Math.Max(0L, val));
423 }
424
425 private static int FindNextItemTextSlot()
426 {
427 int num = -1;
428 for (int i = 0; i < 20; i++)
429 {
430 if (!Main.popupText[i].active)
431 {
432 num = i;
433 break;
434 }
435 }
436 if (num == -1)
437 {
438 double num2 = Main.bottomWorld;
439 for (int j = 0; j < 20; j++)
440 {
441 if (num2 > (double)Main.popupText[j].position.Y)
442 {
443 num = j;
444 num2 = Main.popupText[j].position.Y;
445 }
446 }
447 }
448 return num;
449 }
450
451 public static void AssignAsSonarText(int sonarTextIndex)
452 {
453 sonarText = sonarTextIndex;
454 if (sonarText > -1)
455 {
456 Main.popupText[sonarText].sonar = true;
457 }
458 }
459
460 public static string ValueToName(long coinValue)
461 {
462 int num = 0;
463 int num2 = 0;
464 int num3 = 0;
465 int num4 = 0;
466 string text = "";
467 long num5 = coinValue;
468 while (num5 > 0)
469 {
470 if (num5 >= 1000000)
471 {
472 num5 -= 1000000;
473 num++;
474 }
475 else if (num5 >= 10000)
476 {
477 num5 -= 10000;
478 num2++;
479 }
480 else if (num5 >= 100)
481 {
482 num5 -= 100;
483 num3++;
484 }
485 else if (num5 >= 1)
486 {
487 num5--;
488 num4++;
489 }
490 }
491 text = "";
492 if (num > 0)
493 {
494 text = text + num + string.Format(" {0} ", Language.GetTextValue("Currency.Platinum"));
495 }
496 if (num2 > 0)
497 {
498 text = text + num2 + string.Format(" {0} ", Language.GetTextValue("Currency.Gold"));
499 }
500 if (num3 > 0)
501 {
502 text = text + num3 + string.Format(" {0} ", Language.GetTextValue("Currency.Silver"));
503 }
504 if (num4 > 0)
505 {
506 text = text + num4 + string.Format(" {0} ", Language.GetTextValue("Currency.Copper"));
507 }
508 if (text.Length > 1)
509 {
510 text = text.Substring(0, text.Length - 1);
511 }
512 return text;
513 }
514
515 private void ValueToName()
516 {
517 int num = 0;
518 int num2 = 0;
519 int num3 = 0;
520 int num4 = 0;
521 long num5 = coinValue;
522 while (num5 > 0)
523 {
524 if (num5 >= 1000000)
525 {
526 num5 -= 1000000;
527 num++;
528 }
529 else if (num5 >= 10000)
530 {
531 num5 -= 10000;
532 num2++;
533 }
534 else if (num5 >= 100)
535 {
536 num5 -= 100;
537 num3++;
538 }
539 else if (num5 >= 1)
540 {
541 num5--;
542 num4++;
543 }
544 }
545 name = "";
546 if (num > 0)
547 {
548 name = name + num + string.Format(" {0} ", Language.GetTextValue("Currency.Platinum"));
549 }
550 if (num2 > 0)
551 {
552 name = name + num2 + string.Format(" {0} ", Language.GetTextValue("Currency.Gold"));
553 }
554 if (num3 > 0)
555 {
556 name = name + num3 + string.Format(" {0} ", Language.GetTextValue("Currency.Silver"));
557 }
558 if (num4 > 0)
559 {
560 name = name + num4 + string.Format(" {0} ", Language.GetTextValue("Currency.Copper"));
561 }
562 if (name.Length > 1)
563 {
564 name = name.Substring(0, name.Length - 1);
565 }
566 }
567
568 public void Update(int whoAmI)
569 {
570 if (!active)
571 {
572 return;
573 }
574 float targetScale = TargetScale;
575 alpha += (float)alphaDir * 0.01f;
576 if ((double)alpha <= 0.7)
577 {
578 alpha = 0.7f;
579 alphaDir = 1;
580 }
581 if (alpha >= 1f)
582 {
583 alpha = 1f;
584 alphaDir = -1;
585 }
586 if (expert)
587 {
588 color = new Color((byte)Main.DiscoR, (byte)Main.DiscoG, (byte)Main.DiscoB, Main.mouseTextColor);
589 }
590 else if (master)
591 {
592 color = new Color(255, (byte)(Main.masterColor * 200f), 0, Main.mouseTextColor);
593 }
594 bool flag = false;
595 Vector2 textHitbox = GetTextHitbox();
596 Rectangle rectangle = new Rectangle((int)(position.X - textHitbox.X / 2f), (int)(position.Y - textHitbox.Y / 2f), (int)textHitbox.X, (int)textHitbox.Y);
597 for (int i = 0; i < 20; i++)
598 {
599 PopupText popupText = Main.popupText[i];
600 if (!popupText.active || i == whoAmI)
601 {
602 continue;
603 }
604 Vector2 textHitbox2 = popupText.GetTextHitbox();
605 Rectangle value = new Rectangle((int)(popupText.position.X - textHitbox2.X / 2f), (int)(popupText.position.Y - textHitbox2.Y / 2f), (int)textHitbox2.X, (int)textHitbox2.Y);
606 if (rectangle.Intersects(value) && (position.Y < popupText.position.Y || (position.Y == popupText.position.Y && whoAmI < i)))
607 {
608 flag = true;
609 int num = numActive;
610 if (num > 3)
611 {
612 num = 3;
613 }
614 popupText.lifeTime = activeTime + 15 * num;
615 lifeTime = activeTime + 15 * num;
616 }
617 }
618 if (!flag)
619 {
620 velocity.Y *= 0.86f;
621 if (scale == targetScale)
622 {
623 velocity.Y *= 0.4f;
624 }
625 }
626 else if (velocity.Y > -6f)
627 {
628 velocity.Y -= 0.2f;
629 }
630 else
631 {
632 velocity.Y *= 0.86f;
633 }
634 velocity.X *= 0.93f;
636 lifeTime--;
637 if (lifeTime <= 0)
638 {
639 scale -= 0.03f * targetScale;
640 if ((double)scale < 0.1 * (double)targetScale)
641 {
642 active = false;
643 if (sonarText == whoAmI)
644 {
645 sonarText = -1;
646 }
647 }
648 lifeTime = 0;
649 }
650 else
651 {
652 if (scale < targetScale)
653 {
654 scale += 0.1f * targetScale;
655 }
656 if (scale > targetScale)
657 {
658 scale = targetScale;
659 }
660 }
661 }
662
664 {
665 string text = name;
666 if (stack > 1)
667 {
668 text = text + " (" + stack + ")";
669 }
670 Vector2 result = FontAssets.MouseText.Value.MeasureString(text);
671 result *= scale;
672 result.Y *= 0.8f;
673 return result;
674 }
675
676 public static void UpdateItemText()
677 {
678 int num = 0;
679 for (int i = 0; i < 20; i++)
680 {
681 if (Main.popupText[i].active)
682 {
683 num++;
684 Main.popupText[i].Update(i);
685 }
686 }
687 numActive = num;
688 }
689
690 public static void ClearAll()
691 {
692 for (int i = 0; i < 20; i++)
693 {
694 Main.popupText[i] = new PopupText();
695 }
696 numActive = 0;
697 }
698}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static Asset< DynamicSpriteFont > MouseText
Definition FontAssets.cs:10
bool expert
Definition Item.cs:113
string AffixName()
Definition Item.cs:1001
string Name
Definition Item.cs:326
static string GetTextValue(string key)
Definition Language.cs:15
static float bottomWorld
Definition Main.cs:1112
static float masterColor
Definition Main.cs:1338
static int DiscoR
Definition Main.cs:1062
static int netMode
Definition Main.cs:2095
static byte mouseTextColor
Definition Main.cs:1751
static SpriteViewMatrix GameViewMatrix
Definition Main.cs:227
static int DiscoG
Definition Main.cs:1066
static bool showItemText
Definition Main.cs:1238
static int DiscoB
Definition Main.cs:1064
static PopupText[] popupText
Definition Main.cs:1697
void SetDefaults(int Type, NPCSpawnParams spawnparams=default(NPCSpawnParams))
Definition NPC.cs:2523
string TypeName
Definition NPC.cs:732
static void ClearAll()
Definition PopupText.cs:690
static int sonarText
Definition PopupText.cs:42
static int NewText(PopupTextContext context, Item newItem, int stack, bool noStack=false, bool longText=false)
Definition PopupText.cs:169
static float TargetScale
Definition PopupText.cs:68
static void ResetText(PopupText text)
Definition PopupText.cs:79
static int numActive
Definition PopupText.cs:34
static int NewText(AdvancedPopupRequest request, Vector2 position)
Definition PopupText.cs:95
static void AssignAsSonarText(int sonarTextIndex)
Definition PopupText.cs:451
static void UpdateItemText()
Definition PopupText.cs:676
static string ValueToName(long coinValue)
Definition PopupText.cs:460
void Update(int whoAmI)
Definition PopupText.cs:568
Vector2 GetTextHitbox()
Definition PopupText.cs:663
static int NewText(PopupTextContext context, int npcNetID, Vector2 position, bool stay5TimesLonger)
Definition PopupText.cs:125
static int FindNextItemTextSlot()
Definition PopupText.cs:425
static int activeTime
Definition PopupText.cs:32
PopupTextContext context
Definition PopupText.cs:50
void AddToCoinValue(long addedValue)
Definition PopupText.cs:419
static void ClearSonarText()
Definition PopupText.cs:70
static Color Lerp(Color value1, Color value2, float amount)
Definition Color.cs:491
bool Intersects(Rectangle value)
Definition Rectangle.cs:129