Client LuaCsForBarotrauma
SlideshowPlayer.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using System.Linq;
5 
6 namespace Barotrauma
7 {
9  {
10  private readonly SlideshowPrefab slideshowPrefab;
11  private readonly LocalizedString pressAnyKeyText;
12 
13  private int state;
14 
15  private Color overlayColor, textColor;
16 
17  private float timer;
18 
19  private LocalizedString currentText;
20 
21  public bool LastTextShown => state >= slideshowPrefab.Slides.Length;
22  public bool Finished => state > slideshowPrefab.Slides.Length;
23 
24  public SlideshowPlayer(RectTransform rectT, SlideshowPrefab prefab) : base(null, rectT)
25  {
26  slideshowPrefab = prefab;
27  overlayColor = Color.Black;
28  textColor = Color.Transparent;
29  pressAnyKeyText = TextManager.Get("pressanykey");
30  RefreshText();
31  }
32 
33  public void Restart()
34  {
35  state = 0;
36  }
37 
38  public void Finish()
39  {
40  state = slideshowPrefab.Slides.Length + 1;
41  }
42 
43  protected override void Update(float deltaTime)
44  {
45  var slide = slideshowPrefab.Slides[Math.Min(state, slideshowPrefab.Slides.Length - 1)];
46  if (!Visible || (Finished && timer > slide.FadeOutDuration)) { return; }
47 
48  timer += deltaTime;
49 
50  if (state == 0)
51  {
52  overlayColor = Color.Lerp(Color.Black, Color.White, Math.Min((timer - slide.FadeInDelay) / slide.FadeInDuration, 1.0f));
53  }
54  else
55  {
56  overlayColor = Color.Lerp(Color.Transparent, Color.White, Math.Min((timer - slide.FadeInDelay) / slide.FadeInDuration, 1.0f));
57  }
58 
59  if (timer > slide.TextFadeInDelay)
60  {
61  textColor = Color.Lerp(Color.Transparent, Color.White, Math.Min((timer - slide.TextFadeInDelay) / slide.TextFadeInDuration, 1.0f));
62  if (AnyKeyHit())
63  {
64  if (timer > slide.TextFadeInDelay + slide.FadeInDuration)
65  {
66  overlayColor = textColor = Color.Transparent;
67  timer = 0.0f;
68  state++;
69  RefreshText();
70  }
71  else
72  {
73  timer = slide.TextFadeInDelay + slide.TextFadeInDuration;
74  }
75  }
76  }
77  else
78  {
79  textColor = Color.Transparent;
80  if (AnyKeyHit())
81  {
82  timer = slide.TextFadeInDelay + slide.TextFadeInDuration;
83  }
84  }
85 
86  if (state >= slideshowPrefab.Slides.Length)
87  {
88  overlayColor = Color.Lerp(Color.White, Color.Transparent, Math.Min(timer / slide.FadeOutDuration, 1.0f));
89  textColor = Color.Lerp(Color.White, Color.Transparent, Math.Min(timer / slide.FadeOutDuration, 1.0f));
90  if (timer >= slide.FadeOutDuration)
91  {
92  state++;
93  RefreshText();
94  }
95  }
96 
97  static bool AnyKeyHit()
98  {
99  return
100  PlayerInput.GetKeyboardState.GetPressedKeys().Any(k => PlayerInput.KeyHit(k)) ||
102  }
103  }
104 
105  private void RefreshText()
106  {
107  var slide = slideshowPrefab.Slides[Math.Min(state, slideshowPrefab.Slides.Length - 1)];
108  currentText = slide.Text
109  .Replace("[submarine]", Submarine.MainSub?.Info.Name ?? GameMain.GameSession?.SubmarineInfo?.Name ?? "Unknown")
110  .Replace("[location]", Level.Loaded?.StartOutpost?.Info.Name ?? "Unknown");
111  }
112 
113  protected override void Draw(SpriteBatch spriteBatch)
114  {
115  if (slideshowPrefab.Slides.IsEmpty) { return; }
116 
117  var slide = slideshowPrefab.Slides[Math.Min(state, slideshowPrefab.Slides.Length - 1)];
118  if ((Finished && timer > slide.FadeOutDuration)) { return; }
119 
120  var overlaySprite = slide.Portrait;
121 
122  if (overlaySprite != null)
123  {
124  Sprite prevPortrait = null;
125  if (state > 0 && state < slideshowPrefab.Slides.Length)
126  {
127  prevPortrait = slideshowPrefab.Slides[state - 1].Portrait;
128  DrawOverlay(prevPortrait, Color.White);
129  }
130  if (prevPortrait?.Texture != overlaySprite.Texture)
131  {
132  DrawOverlay(overlaySprite, overlayColor);
133  }
134  }
135  else
136  {
137  GUI.DrawRectangle(spriteBatch, new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), overlayColor, isFilled: true);
138  }
139 
140  if (!currentText.IsNullOrEmpty() && textColor.A > 0)
141  {
142  var backgroundSprite = GUIStyle.GetComponentStyle("CommandBackground").GetDefaultSprite();
143  Vector2 centerPos = new Vector2(GameMain.GraphicsWidth, GameMain.GraphicsHeight) / 2;
144  LocalizedString wrappedText = ToolBox.WrapText(currentText, GameMain.GraphicsWidth / 3, GUIStyle.Font);
145  Vector2 textSize = GUIStyle.Font.MeasureString(wrappedText);
146  Vector2 textPos = centerPos - textSize / 2;
147  backgroundSprite.Draw(spriteBatch,
148  centerPos,
149  Color.White * (textColor.A / 255.0f),
150  origin: backgroundSprite.size / 2,
151  rotate: 0.0f,
152  scale: new Vector2(GameMain.GraphicsWidth / 2 / backgroundSprite.size.X, textSize.Y / backgroundSprite.size.Y * 2.0f));
153 
154  GUI.DrawString(spriteBatch, textPos + Vector2.One, wrappedText, Color.Black * (textColor.A / 255.0f));
155  GUI.DrawString(spriteBatch, textPos, wrappedText, textColor);
156 
157  if (timer > slide.TextFadeInDelay * 2)
158  {
159  float alpha = Math.Min(timer - slide.TextFadeInDelay * 2, 1.0f);
160  Vector2 bottomTextPos = centerPos + new Vector2(0.0f, textSize.Y / 2 + 40 * GUI.Scale) - GUIStyle.Font.MeasureString(pressAnyKeyText) / 2;
161  GUI.DrawString(spriteBatch, bottomTextPos + Vector2.One, pressAnyKeyText, Color.Black * (textColor.A / 255.0f) * alpha);
162  GUI.DrawString(spriteBatch, bottomTextPos, pressAnyKeyText, textColor * alpha);
163  }
164  }
165 
166  void DrawOverlay(Sprite sprite, Color color)
167  {
168  if (sprite.Texture == null) { return; }
169  GUI.DrawBackgroundSprite(spriteBatch, sprite, color);
170  }
171  }
172  }
173 }
static int GraphicsWidth
Definition: GameMain.cs:162
static GameSession?? GameSession
Definition: GameMain.cs:88
static int GraphicsHeight
Definition: GameMain.cs:168
LocalizedString Replace(Identifier find, LocalizedString replace, StringComparison stringComparison=StringComparison.Ordinal)
override void Update(float deltaTime)
SlideshowPlayer(RectTransform rectT, SlideshowPrefab prefab)
override void Draw(SpriteBatch spriteBatch)
static Submarine MainSub
Note that this can be null in some situations, e.g. editors and missions that don't load a submarine.