Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Networking/ServerLog.cs
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 namespace Barotrauma.Networking
9 {
10  public partial class ServerLog
11  {
13  private GUIListBox listBox;
14  private GUIButton reverseButton;
15 
16  private string msgFilter;
17 
18  private bool reverseOrder = false;
19 
20  private bool OnReverseClicked(GUIButton btn, object obj)
21  {
22  SetMessageReversal(!reverseOrder);
23 
24  return false;
25  }
26 
27  public void CreateLogFrame()
28  {
29  LogFrame = new GUIButton(new RectTransform(Vector2.One, GUI.Canvas, Anchor.Center), style: null)
30  {
31  OnClicked = (btn, userdata) => { if (GUI.MouseOn == btn || GUI.MouseOn == btn.TextBlock) LogFrame = null; return true; }
32  };
33 
34  new GUIFrame(new RectTransform(GUI.Canvas.RelativeSize, LogFrame.RectTransform, Anchor.Center), style: "GUIBackgroundBlocker");
35 
36  new GUIButton(new RectTransform(Vector2.One, LogFrame.RectTransform), "", style: null).OnClicked += (btn, userData) =>
37  {
38  LogFrame = null;
39  return true;
40  };
41 
42  GUIFrame innerFrame = new GUIFrame(new RectTransform(new Vector2(0.5f, 0.5f), LogFrame.RectTransform, Anchor.Center) { MinSize = new Point(700, 500) });
43  GUIFrame paddedFrame = new GUIFrame(new RectTransform(new Vector2(0.95f, 0.9f), innerFrame.RectTransform, Anchor.Center), style: null);
44 
45  // left column ----------------
46 
47  var tickBoxContainer = new GUILayoutGroup(new RectTransform(new Vector2(0.25f, 1.0f), paddedFrame.RectTransform, Anchor.BottomLeft));
48  int y = 30;
49  List<GUITickBox> tickBoxes = new List<GUITickBox>();
50  foreach (MessageType msgType in Enum.GetValues(typeof(MessageType)))
51  {
52  var tickBox = new GUITickBox(new RectTransform(new Point(tickBoxContainer.Rect.Width, 30), tickBoxContainer.RectTransform), TextManager.Get("ServerLog." + messageTypeName[msgType]), font: GUIStyle.SmallFont)
53  {
54  Selected = true,
55  TextColor = messageColor[msgType],
56  OnSelected = (GUITickBox tb) =>
57  {
58  msgTypeHidden[(int)msgType] = !tb.Selected;
59  FilterMessages();
60  return true;
61  }
62  };
63  tickBox.TextBlock.SelectedTextColor = tickBox.TextBlock.TextColor;
64  tickBox.Selected = !msgTypeHidden[(int)msgType];
65  tickBoxes.Add(tickBox);
66 
67  y += 20;
68  }
69 
70  tickBoxes.Last().TextBlock.RectTransform.SizeChanged += () =>
71  {
72  GUITextBlock.AutoScaleAndNormalize(tickBoxes.Select(t => t.TextBlock), defaultScale: 1.0f);
73  };
74 
75  // right column ----------------
76 
77  var rightColumn = new GUILayoutGroup(new RectTransform(new Vector2(0.75f, 1.0f), paddedFrame.RectTransform, Anchor.CenterRight), childAnchor: Anchor.TopRight)
78  {
79  Stretch = true,
80  RelativeSpacing = 0.02f
81  };
82 
83  GUILayoutGroup filterArea = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.05f), rightColumn.RectTransform, Anchor.TopRight),
84  isHorizontal: true, childAnchor: Anchor.CenterLeft);
85 
86  new GUITextBlock(new RectTransform(new Vector2(0.2f, 1.0f), filterArea.RectTransform), TextManager.Get("ServerLog.Filter"),
87  font: GUIStyle.SubHeadingFont);
88  GUITextBox searchBox = new GUITextBox(new RectTransform(new Vector2(0.8f, 1.0f), filterArea.RectTransform), font: GUIStyle.SmallFont, createClearButton: true);
89  searchBox.OnTextChanged += (textBox, text) =>
90  {
91  msgFilter = text;
92  FilterMessages();
93  return true;
94  };
95  GUI.KeyboardDispatcher.Subscriber = searchBox;
96  filterArea.RectTransform.MinSize = new Point(0, filterArea.RectTransform.Children.Max(c => c.MinSize.Y));
97 
98  GUILayoutGroup listBoxLayout = new GUILayoutGroup(new RectTransform(new Vector2(1.0f, 0.95f), rightColumn.RectTransform))
99  {
100  Stretch = true,
101  RelativeSpacing = 0.0f
102  };
103 
104  reverseButton = new GUIButton(new RectTransform(new Vector2(1.0f, 0.05f), listBoxLayout.RectTransform), style: "UIToggleButtonVertical");
105  reverseButton.Children.ForEach(c => c.SpriteEffects = reverseOrder ? SpriteEffects.FlipVertically : SpriteEffects.None);
106  reverseButton.OnClicked = OnReverseClicked;
107 
108  listBox = new GUIListBox(new RectTransform(new Vector2(1.0f, 0.95f), listBoxLayout.RectTransform));
109 
110  GUIButton closeButton = new GUIButton(new RectTransform(new Vector2(0.25f, 0.05f), rightColumn.RectTransform), TextManager.Get("Close"))
111  {
112  OnClicked = (button, userData) =>
113  {
114  LogFrame = null;
115  return true;
116  }
117  };
118 
119  rightColumn.Recalculate();
120 
121  var currLines = lines.ToList();
122  foreach (LogMessage line in currLines)
123  {
124  AddLine(line);
125  }
126  FilterMessages();
127 
128  listBox.UpdateScrollBarSize();
129 
130  if (listBox.BarScroll == 0.0f || listBox.BarScroll == 1.0f) { listBox.BarScroll = 1.0f; }
131 
132  msgFilter = "";
133  }
134 
135  public void AssignLogFrame(GUIButton inReverseButton, GUIListBox inListBox, GUIComponent tickBoxContainer, GUITextBox searchBox)
136  {
137  searchBox.OnTextChanged += (textBox, text) =>
138  {
139  msgFilter = text;
140  FilterMessages();
141  return true;
142  };
143 
144  tickBoxContainer.ClearChildren();
145 
146  List<GUITickBox> tickBoxes = new List<GUITickBox>();
147  foreach (MessageType msgType in Enum.GetValues(typeof(MessageType)))
148  {
149  var tickBox = new GUITickBox(new RectTransform(new Point(tickBoxContainer.Rect.Width, (int)(25 * GUI.Scale)), tickBoxContainer.RectTransform),
150  TextManager.Get("ServerLog." + messageTypeName[msgType]).Fallback(messageTypeName[msgType]),
151  font: GUIStyle.SmallFont)
152  {
153  Selected = true,
154  TextColor = messageColor[msgType],
155  OnSelected = (GUITickBox tb) =>
156  {
157  msgTypeHidden[(int)msgType] = !tb.Selected;
158  FilterMessages();
159  return true;
160  }
161  };
162  tickBox.TextBlock.SelectedTextColor = tickBox.TextBlock.TextColor;
163  tickBox.Selected = !msgTypeHidden[(int)msgType];
164  tickBoxes.Add(tickBox);
165  }
166  tickBoxes.Last().TextBlock.RectTransform.SizeChanged += () =>
167  {
168  GUITextBlock.AutoScaleAndNormalize(tickBoxes.Select(t => t.TextBlock), defaultScale: 1.0f);
169  };
170 
171  inListBox.ClearChildren();
172  listBox = inListBox;
173 
174  reverseButton = inReverseButton;
175  reverseButton.Children.ForEach(c => c.SpriteEffects = reverseOrder ? SpriteEffects.FlipVertically : SpriteEffects.None);
176  reverseButton.OnClicked = OnReverseClicked;
177 
178  var currLines = lines.ToList();
179  foreach (LogMessage line in currLines)
180  {
181  AddLine(line);
182  }
183  FilterMessages();
184 
185  listBox.UpdateScrollBarSize();
186  }
187 
188  private void AddLine(LogMessage line)
189  {
190  float prevSize = listBox.BarSize;
191 
192  GUIFrame textContainer = null;
193 
194  Anchor anchor = Anchor.TopLeft;
195  Pivot pivot = Pivot.TopLeft;
196  RichString richString = line.Text as RichString;
197  if (richString != null && richString.RichTextData.HasValue)
198  {
199  foreach (var data in richString.RichTextData.Value)
200  {
201  Client client = data.ExtractClient();
202  if (client != null && client.Karma < 40.0f)
203  {
204  textContainer = new GUIFrame(new RectTransform(new Vector2(1.0f, 0.0f), listBox.Content.RectTransform),
205  style: null, color: new Color(0xff111155))
206  {
207  CanBeFocused = false
208  };
209  anchor = Anchor.CenterLeft;
210  pivot = Pivot.CenterLeft;
211  break;
212  }
213  }
214  }
215 
216  var textBlock = new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.0f), (textContainer ?? listBox.Content).RectTransform, anchor, pivot),
217  line.Text, wrap: true, font: GUIStyle.SmallFont)
218  {
219  TextColor = messageColor[line.Type],
220  Visible = !msgTypeHidden[(int)line.Type],
221  CanBeFocused = false,
222  UserData = line
223  };
224 
225  if (textContainer != null)
226  {
227  textContainer.RectTransform.NonScaledSize = new Point(textContainer.RectTransform.NonScaledSize.X, textBlock.RectTransform.NonScaledSize.Y + 5);
228  textBlock.SetTextPos();
229  textBlock.RectTransform.Resize(textContainer.RectTransform.NonScaledSize);
230  }
231 
232  if (reverseOrder)
233  {
234  textBlock.RectTransform.SetAsFirstChild();
235  }
236 
237  if (richString != null && richString.RichTextData.HasValue)
238  {
239  foreach (var data in richString.RichTextData.Value)
240  {
241  textBlock.ClickableAreas.Add(new GUITextBlock.ClickableArea()
242  {
243  Data = data,
244  OnClick = GameMain.NetLobbyScreen.SelectPlayer,
245  OnSecondaryClick = GameMain.NetLobbyScreen.ShowPlayerContextMenu
246  });
247  }
248  }
249 
250  if ((prevSize == 1.0f && listBox.BarScroll == 0.0f) || (prevSize < 1.0f && listBox.BarScroll == 1.0f)) listBox.BarScroll = 1.0f;
251  }
252 
253  private bool FilterMessages()
254  {
255  string filter = msgFilter == null ? "" : msgFilter.ToLower();
256 
257  foreach (GUIComponent child in listBox.Content.Children)
258  {
259  if (!(child is GUITextBlock textBlock)) { continue; }
260  child.Visible = true;
261  if (msgTypeHidden[(int)((LogMessage)child.UserData).Type])
262  {
263  child.Visible = false;
264  continue;
265  }
266 
267  textBlock.Visible = string.IsNullOrEmpty(filter) || textBlock.Text.ToLower().Contains(filter);
268  }
269  listBox.UpdateScrollBarSize();
270  listBox.BarScroll = 0.0f;
271 
272  return true;
273  }
274 
275  private void SetMessageReversal(bool reverse)
276  {
277  if (reverseOrder == reverse) { return; }
278 
279  reverseOrder = reverse;
280  reverseButton.Children.ForEach(c => c.SpriteEffects = reverseOrder ? SpriteEffects.FlipVertically : SpriteEffects.None);
281 
283  }
284 
285  public bool ClearFilter(GUIComponent button, object _)
286  {
287  var searchBox = button.UserData as GUITextBox;
288  if (searchBox != null) { searchBox.Text = ""; }
289 
290  msgFilter = "";
291  FilterMessages();
292 
293  return true;
294  }
295 
296  }
297 }
OnClickedHandler OnClicked
Definition: GUIButton.cs:16
virtual void ClearChildren()
RectTransform RectTransform
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
Definition: GUIListBox.cs:33
static void AutoScaleAndNormalize(params GUITextBlock[] textBlocks)
Set the text scale of the GUITextBlocks so that they all use the same scale and can fit the text with...
OnTextChangedHandler OnTextChanged
Don't set the Text property on delegates that register to this event, because modifying the Text will...
Definition: GUITextBox.cs:38
void AssignLogFrame(GUIButton inReverseButton, GUIListBox inListBox, GUIComponent tickBoxContainer, GUITextBox searchBox)
IEnumerable< RectTransform > Children
Point?? MinSize
Min size in pixels. Does not affect scaling.
Point NonScaledSize
Size before scale multiplications.
ImmutableArray< RichTextData >? RichTextData
Definition: RichString.cs:42