Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/Signal/ButtonTerminal.cs
3 using Microsoft.Xna.Framework;
4 using System;
5 using System.Linq;
6 using System.Xml.Linq;
7 
9 {
10  partial class ButtonTerminal : ItemComponent, IClientSerializable, IServerSerializable
11  {
12  private string[] terminalButtonStyles;
13  private GUIFrame containerHolder;
14  private GUIImage containerIndicator;
15  private GUIComponentStyle indicatorStyleRed, indicatorStyleGreen;
16 
17  partial void InitProjSpecific(ContentXElement element)
18  {
19  terminalButtonStyles = new string[RequiredSignalCount];
20  int i = 0;
21  foreach (var childElement in element.GetChildElements("TerminalButton"))
22  {
23  string style = childElement.GetAttributeString("style", null);
24  if (style == null) { continue; }
25  terminalButtonStyles[i++] = style;
26  }
27  indicatorStyleRed = GUIStyle.GetComponentStyle("IndicatorLightRed");
28  indicatorStyleGreen = GUIStyle.GetComponentStyle("IndicatorLightGreen");
29  CreateGUI();
30  }
31 
32  protected override void CreateGUI()
33  {
34  var paddedFrame = new GUILayoutGroup(new RectTransform(new Vector2(0.9f, 0.8f), GuiFrame.RectTransform, Anchor.Center), isHorizontal: true, childAnchor: Anchor.CenterLeft)
35  {
36  Stretch = true,
37  RelativeSpacing = 0.08f
38  };
39  paddedFrame.OnAddedToGUIUpdateList += (component) =>
40  {
41  bool buttonsEnabled = AllowUsingButtons;
42  foreach (var child in component.Children)
43  {
44  if (!(child is GUIButton)) { continue; }
45  if (!(child.UserData is int)) { continue; }
46  child.Enabled = buttonsEnabled;
47  child.Children.ForEach(c => c.Enabled = buttonsEnabled);
48  }
49  if (Container == null) { return; }
50  bool itemsContained = Container.Inventory.AllItems.Any();
51  if (itemsContained)
52  {
53  var indicatorStyle = buttonsEnabled ? indicatorStyleGreen : indicatorStyleRed;
54  if (containerIndicator.Style != indicatorStyle)
55  {
56  containerIndicator.ApplyStyle(indicatorStyle);
57  }
58  }
59  containerIndicator.OverrideState = itemsContained ? GUIComponent.ComponentState.Selected : GUIComponent.ComponentState.None;
60  };
61 
62  float x = 1.0f / (1 + RequiredSignalCount);
63  float y = Math.Min((x * paddedFrame.Rect.Width) / paddedFrame.Rect.Height, 0.5f);
64  Vector2 relativeSize = new Vector2(x, y);
65 
66  var containerSection = new GUIFrame(new RectTransform(new Vector2(x, 1.0f), paddedFrame.RectTransform), style: null);
67  var containerSlot = new GUIFrame(new RectTransform(new Vector2(1.0f, y), containerSection.RectTransform, anchor: Anchor.Center), style: null);
68  containerHolder = new GUIFrame(new RectTransform(new Vector2(1f, 1.2f), containerSlot.RectTransform, Anchor.BottomCenter), style: null);
69  containerIndicator = new GUIImage(new RectTransform(new Vector2(0.5f, 0.5f * (1.0f - y)), containerSection.RectTransform, anchor: Anchor.BottomCenter),
70  style: "IndicatorLightRed", scaleToFit: true);
71 
72  for (int i = 0; i < RequiredSignalCount; i++)
73  {
74  var button = new GUIButton(new RectTransform(relativeSize, paddedFrame.RectTransform), style: null)
75  {
76  UserData = i,
77  OnClicked = (button, userData) =>
78  {
79  int signalIndex = (int)userData;
81  {
82  SendSignal(signalIndex, Character.Controlled);
83  }
84  else
85  {
86  item.CreateClientEvent(this, new EventData(signalIndex));
87  }
88  return true;
89  }
90  };
91  var image = new GUIImage(new RectTransform(Vector2.One, button.RectTransform), terminalButtonStyles[i], scaleToFit: true);
92  }
93  }
94 
95  protected override void OnResolutionChanged()
96  {
97  OnItemLoadedProjSpecific();
98  }
99 
100  partial void OnItemLoadedProjSpecific()
101  {
102  if (Container == null) { return; }
103  Container.AllowUIOverlap = true;
104  Container.Inventory.RectTransform = containerHolder.RectTransform;
105  }
106 
107  public void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData = null)
108  {
109  Write(msg, extraData);
110  }
111 
112  public void ClientEventRead(IReadMessage msg, float sendingTime)
113  {
114  SendSignal(msg.ReadRangedInteger(0, Signals.Length - 1), sender: null, isServerMessage: true);
115  }
116  }
117 }
Action< GUIComponent > OnAddedToGUIUpdateList
RectTransform RectTransform
static bool IsSingleplayer
Definition: GameMain.cs:34
RectTransform RectTransform
If set, the inventory is automatically positioned inside the rect
virtual IEnumerable< Item > AllItems
All items contained in the inventory. Stacked items are returned as individual instances....
override void CreateGUI()
Overload this method and implement. The method is automatically called when the resolution changes.
RectTransform(Vector2 relativeSize, RectTransform parent, Anchor anchor=Anchor.TopLeft, Pivot? pivot=null, Point? minSize=null, Point? maxSize=null, ScaleBasis scaleBasis=ScaleBasis.Normal)
Interface for entities that the clients can send events to the server
int ReadRangedInteger(int min, int max)
Interface for entities that the server can send events to the clients