forked from MatterHackers/MatterControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditableNumberDisplay.cs
148 lines (122 loc) · 4.4 KB
/
EditableNumberDisplay.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using System;
namespace MatterHackers.MatterControl
{
public class EditableNumberDisplay : FlowLayoutWidget
{
protected ClickWidget clickableValueContainer;
protected Button setButton;
protected MHNumberEdit numberInputField;
protected TextWidget valueDisplay;
public event EventHandler EditComplete;
public event EventHandler EditEnabled;
public EditableNumberDisplay(TextImageButtonFactory textImageButtonFactory, string startingValue, string largestPossibleValue)
: base(Agg.UI.FlowDirection.LeftToRight)
{
this.Margin = new BorderDouble(3, 0);
this.VAnchor = VAnchor.ParentCenter;
clickableValueContainer = new ClickWidget();
clickableValueContainer.VAnchor = VAnchor.ParentBottomTop;
clickableValueContainer.Cursor = Cursors.Hand;
clickableValueContainer.BorderWidth = 1;
clickableValueContainer.BorderColor = new RGBA_Bytes(255, 255, 255, 140);
clickableValueContainer.MouseEnterBounds += (sender, e) =>
{
clickableValueContainer.BorderWidth = 2;
clickableValueContainer.BorderColor = new RGBA_Bytes(255, 255, 255, 255);
};
clickableValueContainer.MouseLeaveBounds += (sender, e) =>
{
clickableValueContainer.BorderWidth = 1;
clickableValueContainer.BorderColor = new RGBA_Bytes(255, 255, 255, 140);
};
valueDisplay = new TextWidget(largestPossibleValue, pointSize: 12);
valueDisplay.VAnchor = VAnchor.ParentCenter;
valueDisplay.HAnchor = HAnchor.ParentLeft;
valueDisplay.TextColor = ActiveTheme.Instance.PrimaryTextColor;
valueDisplay.Margin = new BorderDouble(6);
clickableValueContainer.Click += new EventHandler(editField_Click);
clickableValueContainer.AddChild(valueDisplay);
clickableValueContainer.SetBoundsToEncloseChildren();
valueDisplay.Text = startingValue;
numberInputField = new MHNumberEdit(0, pixelWidth: 40, allowDecimals: true);
numberInputField.VAnchor = VAnchor.ParentCenter;
numberInputField.Margin = new BorderDouble(left: 6);
numberInputField.Visible = false;
// This is a hack to make sure the control is tall enough.
// TODO: This hack needs a unit test and then pass and then remove this line.
this.MinimumSize = new VectorMath.Vector2(0, numberInputField.Height);
setButton = textImageButtonFactory.Generate("SET");
setButton.VAnchor = VAnchor.ParentCenter;
setButton.Margin = new BorderDouble(left: 6);
setButton.Visible = false;
numberInputField.ActuallNumberEdit.EnterPressed += new KeyEventHandler(ActuallNumberEdit_EnterPressed);
numberInputField.KeyDown += (sender, e) =>
{
if (e.KeyCode == Keys.Escape)
{
clickableValueContainer.Visible = true;
numberInputField.Visible = false;
setButton.Visible = false;
}
};
setButton.Click += new EventHandler(setButton_Click);
this.AddChild(clickableValueContainer);
this.AddChild(numberInputField);
this.AddChild(setButton);
}
private void editField_Click(object sender, EventArgs mouseEvent)
{
clickableValueContainer.Visible = false;
numberInputField.Visible = true;
// This is trying to get all the numbers to the left of the decimal. We could do a better
// job of finding the number. 6546431321654
string displayString = valueDisplay.Text.Split('.')[0];
if (displayString != null && displayString != "")
{
double displayStringAsValue;
double.TryParse(displayString, out displayStringAsValue);
numberInputField.ActuallNumberEdit.Value = displayStringAsValue;
}
numberInputField.ActuallNumberEdit.InternalNumberEdit.Focus();
numberInputField.ActuallNumberEdit.InternalNumberEdit.SelectAll();
setButton.Visible = true;
OnEditEnabled();
}
public void OnEditEnabled()
{
if (EditEnabled != null)
{
EditEnabled(this, null);
}
}
public void OnEditComplete()
{
if (EditComplete != null)
{
EditComplete(this, null);
}
}
private void setButton_Click(object sender, EventArgs mouseEvent)
{
OnEditComplete();
}
private void ActuallNumberEdit_EnterPressed(object sender, KeyEventArgs keyEvent)
{
OnEditComplete();
}
public void SetDisplayString(string displayString)
{
valueDisplay.Text = displayString;
clickableValueContainer.Visible = true;
numberInputField.Visible = false;
setButton.Visible = false;
}
public double GetValue()
{
double targetTemp = numberInputField.ActuallNumberEdit.Value;
return targetTemp;
}
}
}