Client LuaCsForBarotrauma
PanelAnimator.cs
1 using System;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
5 
6 namespace Barotrauma
7 {
8  public class PanelAnimator
9  {
10  private readonly GUIScissorComponent container;
11 
12  private readonly GUIFrame leftFrame;
13  private readonly GUIComponent middleFrame;
14  private readonly GUIFrame rightFrame;
15 
16  private readonly GUIButton leftButton;
17  private readonly GUIButton rightButton;
18 
19  private float leftAnimState = 1.0f;
20  private float rightAnimState = 0.0f;
21 
22  public bool LeftEnabled
23  {
24  get => leftButton.Enabled;
25  set => leftButton.Enabled = value;
26  }
27  public bool RightEnabled
28  {
29  get => rightButton.Enabled;
30  set => rightButton.Enabled = value;
31  }
32 
33  public bool LeftVisible = true;
34  public bool RightVisible = false;
35 
36  public PanelAnimator(RectTransform rectTransform, GUIFrame leftFrame, GUIComponent middleFrame, GUIFrame rightFrame)
37  {
38  container = new GUIScissorComponent(rectTransform);
39 
40  this.leftFrame = leftFrame;
41  this.middleFrame = middleFrame;
42  this.rightFrame = rightFrame;
43 
44  void own(GUIComponent component)
45  {
46  component.RectTransform.Parent = container.Content.RectTransform;
47  component.RectTransform.Anchor = Anchor.TopLeft;
48  component.RectTransform.Pivot = Pivot.TopLeft;
49 
50  component.GetAllChildren<GUIDropDown>().ForEach(dd => dd.RefreshListBoxParent());
51  }
52 
53  GUIButton makeButton(Action action)
54  => new GUIButton(new RectTransform(new Vector2(0.01f, 1.0f), container.Content.RectTransform)
55  { MinSize = new Point(20, 0), MaxSize = new Point(int.MaxValue, (int)(150 * GUI.Scale)) },
56  style: "UIToggleButton")
57  {
58  OnClicked = (_, __) =>
59  {
60  action();
61  return false;
62  }
63  };
64 
65  own(leftFrame);
66  this.leftButton = makeButton(() => LeftVisible = !LeftVisible);
67 
68  own(middleFrame);
69 
70  this.rightButton = makeButton(() => RightVisible = !RightVisible);
71  own(rightFrame);
72  }
73 
74  public void Update()
75  {
76  if (!LeftEnabled) { LeftVisible = false; }
77  if (!RightEnabled) { RightVisible = false; }
78 
79  static void updateState(ref float state, bool visible)
80  => state = MathHelper.Lerp(state, visible ? 0.0f : 1.0f, 0.5f);
81  updateState(ref leftAnimState, LeftVisible);
82  updateState(ref rightAnimState, RightVisible);
83 
84  static int width(GUIComponent c)
86 
87  int height = container.RectTransform.NonScaledSize.Y;
88  int buttonY = height/2 - leftButton.RectTransform.NonScaledSize.Y/2;
89 
90  leftFrame.RectTransform.AbsoluteOffset = new Point((int)(-width(leftFrame) * leftAnimState), 0);
91  leftButton.RectTransform.AbsoluteOffset = leftFrame.RectTransform.AbsoluteOffset
92  + new Point(width(leftFrame), buttonY);
93  leftButton.Children.ForEach(c => c.SpriteEffects = LeftVisible
94  ? SpriteEffects.FlipHorizontally
95  : SpriteEffects.None);
96 
97  rightFrame.RectTransform.AbsoluteOffset = new Point((int)(width(container) + width(rightFrame) * (rightAnimState-1f)), 0);
98  rightButton.RectTransform.AbsoluteOffset = rightFrame.RectTransform.AbsoluteOffset
99  + new Point(-width(rightButton), buttonY);
100  rightButton.Children.ForEach(c => c.SpriteEffects = RightVisible
101  ? SpriteEffects.None
102  : SpriteEffects.FlipHorizontally);
103 
104  middleFrame.RectTransform.AbsoluteOffset = new Point(
105  leftButton.RectTransform.AbsoluteOffset.X + width(leftButton),
106  0);
107  middleFrame.RectTransform.NonScaledSize = new Point(
108  rightButton.RectTransform.AbsoluteOffset.X - middleFrame.RectTransform.AbsoluteOffset.X,
109  height);
110  }
111  }
112 }
IEnumerable< GUIComponent > GetAllChildren()
Returns all child elements in the hierarchy.
Definition: GUIComponent.cs:49
RectTransform RectTransform
SpriteEffects SpriteEffects
PanelAnimator(RectTransform rectTransform, GUIFrame leftFrame, GUIComponent middleFrame, GUIFrame rightFrame)
Anchor Anchor
Does not automatically calculate children. Note also that if you change the anchor point with this pr...
RectTransform?? Parent
Pivot Pivot
Does not automatically calculate children. Note also that if you change the pivot point with this pro...
RectTransform(Vector2 relativeSize, RectTransform parent, Anchor anchor=Anchor.TopLeft, Pivot? pivot=null, Point? minSize=null, Point? maxSize=null, ScaleBasis scaleBasis=ScaleBasis.Normal)
Point NonScaledSize
Size before scale multiplications.