From 8e880b2dedba7e26f9df86b6f90f3e9e691d6306 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Sun, 2 Jul 2017 22:17:12 +0300 Subject: [PATCH] Added GUINumberInputs to the extra cargo menu, minor additions to GUINumberInput --- .../Source/GUI/GUINumberInput.cs | 14 +++++- .../Source/GUI/GUITextBlock.cs | 3 +- .../Source/Networking/GameServerSettings.cs | 44 +++---------------- 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/GUI/GUINumberInput.cs b/Barotrauma/BarotraumaClient/Source/GUI/GUINumberInput.cs index cb2c220c1..3f4f7eb7e 100644 --- a/Barotrauma/BarotraumaClient/Source/GUI/GUINumberInput.cs +++ b/Barotrauma/BarotraumaClient/Source/GUI/GUINumberInput.cs @@ -6,6 +6,9 @@ namespace Barotrauma { class GUINumberInput : GUIComponent { + public delegate void OnValueChangedHandler(GUINumberInput numberInput, int number); + public OnValueChangedHandler OnValueChanged; + private GUITextBox textBox; private GUIButton plusButton, minusButton; @@ -17,16 +20,22 @@ namespace Barotrauma get { return value; } set { + if (value == this.value) return; + this.value = value; if (MinValue != null) { this.value = Math.Max(this.value, (int)MinValue); + minusButton.Enabled = this.value > MinValue; } if (MaxValue != null) { this.value = Math.Min(this.value, (int)MaxValue); + plusButton.Enabled = this.value < MaxValue; } textBox.Text = this.value.ToString(); + + if (OnValueChanged != null) OnValueChanged(this, this.value); } } @@ -49,14 +58,15 @@ namespace Barotrauma textBox.OnTextChanged += TextChanged; - plusButton = new GUIButton(new Rectangle(0, 0, 15, rect.Height / 2), "+", Alignment.TopRight, style, this); + plusButton = new GUIButton(new Rectangle(0, 0, 15, rect.Height / 2), "+", null, Alignment.TopRight, Alignment.Center, style, this); plusButton.OnClicked += ChangeValue; - minusButton = new GUIButton(new Rectangle(0, 0, 15, rect.Height / 2), "-", Alignment.BottomRight, style, this); + minusButton = new GUIButton(new Rectangle(0, 0, 15, rect.Height / 2), "-", null, Alignment.BottomRight, Alignment.Center, style, this); minusButton.OnClicked += ChangeValue; MinValue = minValue; MaxValue = maxValue; + value = int.MaxValue; Value = minValue != null ? (int)minValue : 0; } diff --git a/Barotrauma/BarotraumaClient/Source/GUI/GUITextBlock.cs b/Barotrauma/BarotraumaClient/Source/GUI/GUITextBlock.cs index 42a37d8e9..402b29a3d 100644 --- a/Barotrauma/BarotraumaClient/Source/GUI/GUITextBlock.cs +++ b/Barotrauma/BarotraumaClient/Source/GUI/GUITextBlock.cs @@ -73,12 +73,11 @@ namespace Barotrauma child.Rect = new Rectangle(child.Rect.X + value.X - rect.X, child.Rect.Y + value.Y - rect.Y, child.Rect.Width, child.Rect.Height); } + rect = value; if (value.Width != rect.Width || value.Height != rect.Height) { SetTextPos(); } - - rect = value; } } diff --git a/Barotrauma/BarotraumaClient/Source/Networking/GameServerSettings.cs b/Barotrauma/BarotraumaClient/Source/Networking/GameServerSettings.cs index e6bab34d4..6a96813bb 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/GameServerSettings.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/GameServerSettings.cs @@ -271,50 +271,20 @@ namespace Barotrauma.Networking int cargoVal = 0; extraCargo.TryGetValue(pf.Name, out cargoVal); - var countText = new GUITextBlock( - new Rectangle(160, 0, 55, 25), - cargoVal.ToString(), - "", - Alignment.Left, Alignment.Center, textBlock); + var amountInput = new GUINumberInput(new Rectangle(160, 0, 50, 20), "", 0, 100, textBlock); + amountInput.Value = cargoVal; - var incButton = new GUIButton(new Rectangle(200, 5, 15, 15), ">", "", textBlock); - incButton.UserData = countText; - incButton.OnClicked = (button, obj) => + amountInput.OnValueChanged += (numberInput, value) => { - int val; - if (extraCargo.TryGetValue(pf.Name, out val)) + if (extraCargo.ContainsKey(pf.Name)) { - extraCargo[pf.Name]++; val = extraCargo[pf.Name]; + extraCargo[pf.Name] = value; } else { - extraCargo.Add(pf.Name, 1); val = 1; + extraCargo.Add(pf.Name, value); } - ((GUITextBlock)obj).Text = val.ToString(); - ((GUITextBlock)obj).SetTextPos(); - return true; - }; - - var decButton = new GUIButton(new Rectangle(160, 5, 15, 15), "<", "", textBlock); - decButton.UserData = countText; - decButton.OnClicked = (button, obj) => - { - int val; - if (extraCargo.TryGetValue(pf.Name, out val)) - { - extraCargo[pf.Name]--; - val = extraCargo[pf.Name]; - if (val <= 0) - { - extraCargo.Remove(pf.Name); - val = 0; - } - ((GUITextBlock)obj).Text = val.ToString(); - ((GUITextBlock)obj).SetTextPos(); - } - - return true; - }; + }; }