Added GUINumberInputs to the extra cargo menu, minor additions to GUINumberInput

This commit is contained in:
Joonas Rikkonen
2017-07-02 22:17:12 +03:00
parent 8ae2fb225c
commit 8e880b2ded
3 changed files with 20 additions and 41 deletions
@@ -6,6 +6,9 @@ namespace Barotrauma
{ {
class GUINumberInput : GUIComponent class GUINumberInput : GUIComponent
{ {
public delegate void OnValueChangedHandler(GUINumberInput numberInput, int number);
public OnValueChangedHandler OnValueChanged;
private GUITextBox textBox; private GUITextBox textBox;
private GUIButton plusButton, minusButton; private GUIButton plusButton, minusButton;
@@ -17,16 +20,22 @@ namespace Barotrauma
get { return value; } get { return value; }
set set
{ {
if (value == this.value) return;
this.value = value; this.value = value;
if (MinValue != null) if (MinValue != null)
{ {
this.value = Math.Max(this.value, (int)MinValue); this.value = Math.Max(this.value, (int)MinValue);
minusButton.Enabled = this.value > MinValue;
} }
if (MaxValue != null) if (MaxValue != null)
{ {
this.value = Math.Min(this.value, (int)MaxValue); this.value = Math.Min(this.value, (int)MaxValue);
plusButton.Enabled = this.value < MaxValue;
} }
textBox.Text = this.value.ToString(); textBox.Text = this.value.ToString();
if (OnValueChanged != null) OnValueChanged(this, this.value);
} }
} }
@@ -49,14 +58,15 @@ namespace Barotrauma
textBox.OnTextChanged += TextChanged; 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; 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; minusButton.OnClicked += ChangeValue;
MinValue = minValue; MinValue = minValue;
MaxValue = maxValue; MaxValue = maxValue;
value = int.MaxValue;
Value = minValue != null ? (int)minValue : 0; Value = minValue != null ? (int)minValue : 0;
} }
@@ -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); 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) if (value.Width != rect.Width || value.Height != rect.Height)
{ {
SetTextPos(); SetTextPos();
} }
rect = value;
} }
} }
@@ -271,50 +271,20 @@ namespace Barotrauma.Networking
int cargoVal = 0; int cargoVal = 0;
extraCargo.TryGetValue(pf.Name, out cargoVal); extraCargo.TryGetValue(pf.Name, out cargoVal);
var countText = new GUITextBlock( var amountInput = new GUINumberInput(new Rectangle(160, 0, 50, 20), "", 0, 100, textBlock);
new Rectangle(160, 0, 55, 25), amountInput.Value = cargoVal;
cargoVal.ToString(),
"",
Alignment.Left, Alignment.Center, textBlock);
var incButton = new GUIButton(new Rectangle(200, 5, 15, 15), ">", "", textBlock); amountInput.OnValueChanged += (numberInput, value) =>
incButton.UserData = countText;
incButton.OnClicked = (button, obj) =>
{ {
int val; if (extraCargo.ContainsKey(pf.Name))
if (extraCargo.TryGetValue(pf.Name, out val))
{ {
extraCargo[pf.Name]++; val = extraCargo[pf.Name]; extraCargo[pf.Name] = value;
} }
else 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;
};
} }