Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/GameAnalytics/GameAnalyticsManager.cs
1 using Barotrauma.IO;
2 using Microsoft.Xna.Framework;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace Barotrauma
7 {
8  static partial class GameAnalyticsManager
9  {
10  static partial void CreateConsentPrompt()
11  {
12  if (ConsentTextAvailable)
13  {
14  var background = new GUIFrame(new RectTransform(GUI.Canvas.RelativeSize, GUI.Canvas), style: "GUIBackgroundBlocker");
15  var frame = new GUIFrame(new RectTransform(new Vector2(0.5f, 0.7f), background.RectTransform, Anchor.Center) { MinSize = new Point(800, 0), MaxSize = new Point(1500, int.MaxValue) });
16 
17  var content = new GUILayoutGroup(new RectTransform(new Vector2(0.95f), frame.RectTransform, Anchor.Center))
18  {
19  Stretch = true,
20  AbsoluteSpacing = GUI.IntScale(15)
21  };
22 
23  string consentTextTag = "statisticsconsenttext";
24  if (EosInterface.IdQueries.IsLoggedIntoEosConnect)
25  {
26  consentTextTag = "statisticsconsenteostext";
27  }
28  new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), content.RectTransform), TextManager.Get("statisticsconsentheader"), font: GUIStyle.SubHeadingFont, textColor: Color.White);
29  var mainText = new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), content.RectTransform), RichString.Rich(TextManager.Get(consentTextTag)), wrap: true);
30 
31  foreach (var data in mainText.RichTextData)
32  {
33  mainText.ClickableAreas.Add(new GUITextBlock.ClickableArea()
34  {
35  Data = data,
36  OnClick = (GUITextBlock component, GUITextBlock.ClickableArea area) =>
37  {
38  GameMain.ShowOpenUriPrompt("https://gameanalytics.com/privacy/");
39  }
40  });
41  }
42 
43  string privacyPolicyText = File.ReadAllText("daedalic_privacypolicy.txt");
44  var privacyPolicyBox = new GUIListBox(new RectTransform(new Vector2(1.0f, 0.5f), content.RectTransform) { MaxSize = new Point(int.MaxValue, GUI.IntScale(200)) });
45  var privacyPolicy = new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), privacyPolicyBox.Content.RectTransform), privacyPolicyText, wrap: true)
46  {
47  CanBeFocused = false
48  };
49  privacyPolicy.RectTransform.MinSize = new Point(0, (int)privacyPolicy.TextSize.Y);
50 
51  new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), content.RectTransform), TextManager.Get("statisticsconsentstatement"), wrap: true);
52 
53  var buttonContainer = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.0f), content.RectTransform), isHorizontal: true);
54 
55  void buttonContainerSpacing(float width)
56  => new GUIFrame(new RectTransform(new Vector2(width, 1.0f), buttonContainer.RectTransform), style: null);
57 
58  buttonContainerSpacing(0.1f);
59  var yesBtn = new GUIButton(new RectTransform(new Vector2(0.3f, 1.0f), buttonContainer.RectTransform), TextManager.Get("Yes"));
60  yesBtn.OnClicked += (btn, userdata) =>
61  {
62  GUIMessageBox.MessageBoxes.Remove(background);
63  var loadingBox = GUIMessageBox.CreateLoadingBox(TextManager.Get("PleaseWait"));
64  SetConsentInternal(Consent.Yes, onAnswerSent: loadingBox.Close);
65  return true;
66  };
67  yesBtn.Enabled = false;
68 
69  IEnumerable<CoroutineStatus> enableAfterTime(WaitForSeconds time, params GUIComponent[] components)
70  {
71  yield return time;
72  foreach (var c in components)
73  {
74  c.Enabled = true;
75  }
76  yield return CoroutineStatus.Success;
77  }
78 
79  buttonContainerSpacing(0.2f);
80 
81  var noBtn = new GUIButton(new RectTransform(new Vector2(0.3f, 1.0f), buttonContainer.RectTransform), TextManager.Get("No"));
82  noBtn.OnClicked += (btn, userdata) =>
83  {
84  GUIMessageBox.MessageBoxes.Remove(background);
85  var loadingBox = GUIMessageBox.CreateLoadingBox(TextManager.Get("PleaseWait"));
86  SetConsent(Consent.No, onAnswerSent: loadingBox.Close);
87  return true;
88  };
89  noBtn.Enabled = false;
90 
91  CoroutineManager.StartCoroutine(enableAfterTime(new WaitForSeconds(0.3f), yesBtn, noBtn));
92 
93  buttonContainerSpacing(0.1f);
94 
95  buttonContainer.RectTransform.MinSize = new Point(0, yesBtn.RectTransform.MinSize.Y);
96  buttonContainer.RectTransform.MaxSize = new Point(int.MaxValue, yesBtn.RectTransform.MinSize.Y);
97 
98  content.Recalculate();
99  foreach (var child in content.Children)
100  {
101  if (child is GUITextBlock textBlock)
102  {
103  textBlock.TextScale = MathHelper.Min(1.0f, 1.0f / GameSettings.CurrentConfig.Graphics.TextScale);
104  textBlock.RectTransform.MinSize = new Point(0, (int)textBlock.TextSize.Y);
105  textBlock.RectTransform.MaxSize = new Point(int.MaxValue, (int)textBlock.TextSize.Y);
106  }
107  }
108 
109  int contentHeight = content.Children.Sum(c => c.RectTransform.MaxSize.Y + content.AbsoluteSpacing);
110  frame.RectTransform.MinSize = new Point(frame.RectTransform.MinSize.X, (int)(contentHeight / content.RectTransform.RelativeSize.Y));
111  frame.RectTransform.MaxSize = new Point(frame.RectTransform.MaxSize.X, (int)(contentHeight / content.RectTransform.RelativeSize.Y));
112 
113  GUIMessageBox.MessageBoxes.Add(background);
114  }
115  else
116  {
117  //user statistics disabled by default if the prompt cannot be shown in the user's language
118  SetConsent(Consent.Unknown);
119  }
120  }
121  }
122 }