Client LuaCsForBarotrauma
GUIScissorComponent.cs
1 using System;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 
5 namespace Barotrauma
6 {
7  public sealed class GUIScissorComponent : GUIComponent
8  {
10 
11  public GUIScissorComponent(RectTransform rectT) : base(null, rectT)
12  {
13  Content = new GUIFrame(new RectTransform(Vector2.One, rectT), style: null)
14  {
15  CanBeFocused = false
16  };
17 
18  rectT.ChildrenChanged += CheckForChildren;
19  }
20 
21  private void CheckForChildren(RectTransform rectT)
22  {
23  if (rectT == Content.RectTransform) { return; }
24  throw new InvalidOperationException($"Children were found in {nameof(GUIScissorComponent)}, Add them to {nameof(GUIScissorComponent)}.{nameof(Content)} instead.");
25  }
26 
27  public override void DrawChildren(SpriteBatch spriteBatch, bool recursive)
28  {
29  //do nothing (the children have to be drawn in the Draw method after the ScissorRectangle has been set)
30  return;
31  }
32 
33  protected override void Draw(SpriteBatch spriteBatch)
34  {
35  if (!Visible) { return; }
36 
37  Rectangle prevScissorRect = spriteBatch.GraphicsDevice.ScissorRectangle;
38  RasterizerState prevRasterizerState = spriteBatch.GraphicsDevice.RasterizerState;
39 
40  spriteBatch.End();
41  spriteBatch.GraphicsDevice.ScissorRectangle = Rectangle.Intersect(prevScissorRect, Rect);
42  spriteBatch.Begin(SpriteSortMode.Deferred, samplerState: GUI.SamplerState, rasterizerState: GameMain.ScissorTestEnable);
43 
44  foreach (GUIComponent child in Content.Children)
45  {
46  if (!child.Visible) { continue; }
47  child.DrawManually(spriteBatch, alsoChildren: true, recursive: true);
48  }
49 
50  spriteBatch.End();
51  spriteBatch.GraphicsDevice.ScissorRectangle = prevScissorRect;
52  spriteBatch.Begin(SpriteSortMode.Deferred, samplerState: GUI.SamplerState, rasterizerState: prevRasterizerState);
53  }
54 
55  protected override void Update(float deltaTime)
56  {
57  base.Update(deltaTime);
58  ClampChildMouseRects(Content);
59  }
60 
61  private static void ClampChildMouseRects(GUIComponent child)
62  {
63  child.ClampMouseRectToParent = true;
64 
65  if (child is GUIListBox) { return; }
66 
67  foreach (GUIComponent grandChild in child.Children)
68  {
69  ClampChildMouseRects(grandChild);
70  }
71  }
72 
73  public override void AddToGUIUpdateList(bool ignoreChildren = false, int order = 0)
74  {
75  if (!Visible) { return; }
76 
77  UpdateOrder = order;
78  GUI.AddToUpdateList(this);
79 
80  if (ignoreChildren)
81  {
82  OnAddedToGUIUpdateList?.Invoke(this);
83  return;
84  }
85 
86  foreach (GUIComponent child in Content.Children)
87  {
88  if (!child.Visible) { continue; }
89  child.AddToGUIUpdateList(false, order);
90  }
91  OnAddedToGUIUpdateList?.Invoke(this);
92  }
93  }
94 }
GUIComponent(string style, RectTransform rectT)
This is the new constructor.
virtual void AddToGUIUpdateList(bool ignoreChildren=false, int order=0)
Action< GUIComponent > OnAddedToGUIUpdateList
virtual void DrawManually(SpriteBatch spriteBatch, bool alsoChildren=false, bool recursive=true)
By default, all the gui elements are drawn automatically in the same order they appear on the update ...
virtual Rectangle Rect
RectTransform RectTransform
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
override void AddToGUIUpdateList(bool ignoreChildren=false, int order=0)
override void Update(float deltaTime)
override void DrawChildren(SpriteBatch spriteBatch, bool recursive)
Draws all the children manually.
override void Draw(SpriteBatch spriteBatch)
GUIScissorComponent(RectTransform rectT)
static RasterizerState ScissorTestEnable
Definition: GameMain.cs:195
Action< RectTransform > ChildrenChanged
The element provided as the argument is the changed child. It may be new in the hierarchy or just rep...