diff options
| author | Jacob Palecki <[email protected]> | 2020-08-12 19:22:21 -0700 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2020-08-12 19:22:21 -0700 |
| commit | cc531c79f2bd664551071ef315a54814bd9ab914 (patch) | |
| tree | e6d1db3477e8ba41299d1d92eac4748a648c960b /grapher/Models/Fields | |
| parent | Add ability to have x\y graphs (diff) | |
| download | rawaccel-cc531c79f2bd664551071ef315a54814bd9ab914.tar.xz rawaccel-cc531c79f2bd664551071ef315a54814bd9ab914.zip | |
Reorganized solution into directories
Diffstat (limited to 'grapher/Models/Fields')
| -rw-r--r-- | grapher/Models/Fields/Field.cs | 226 | ||||
| -rw-r--r-- | grapher/Models/Fields/FieldXY.cs | 130 |
2 files changed, 356 insertions, 0 deletions
diff --git a/grapher/Models/Fields/Field.cs b/grapher/Models/Fields/Field.cs new file mode 100644 index 0000000..8d75172 --- /dev/null +++ b/grapher/Models/Fields/Field.cs @@ -0,0 +1,226 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class Field + { + #region Constants + + public const string DefaultFormatString = "0.#########"; + + #endregion Constants + + #region Enums + + public enum FieldState + { + Undefined, + Default, + Typing, + Entered, + Unavailable, + } + + #endregion Enums + + + #region Constructors + + public Field(TextBox box, Form containingForm, double defaultData) + { + DefaultText = DecimalString(defaultData); + Box = box; + Data = defaultData; + DefaultData = defaultData; + State = FieldState.Undefined; + ContainingForm = containingForm; + FormatString = DefaultFormatString; + box.KeyDown += new System.Windows.Forms.KeyEventHandler(KeyDown); + box.Leave += new System.EventHandler(FocusLeave); + + SetToDefault(); + } + + #endregion Constructors + + #region Properties + + public TextBox Box { get; } + + private Form ContainingForm { get; } + + public double Data { get; private set; } + + public string FormatString { get; set; } + + public string DefaultText { get; } + + public FieldState State { get; private set; } + + public FieldState PreviousState { get; private set; } + + private double DefaultData { get; } + + #endregion Properties + + #region Methods + + public void SetToDefault() + { + if (State != FieldState.Default) + { + Box.BackColor = Color.White; + Box.ForeColor = Color.Gray; + State = FieldState.Default; + PreviousState = FieldState.Default; + } + + Data = DefaultData; + Box.Text = DefaultText; + ContainingForm.ActiveControl = null; + } + + public void SetToTyping() + { + if (State != FieldState.Typing) + { + Box.BackColor = Color.White; + Box.ForeColor = Color.Black; + + PreviousState = State; + State = FieldState.Typing; + } + + Box.Text = string.Empty; + } + + public void SetToEntered() + { + if (State != FieldState.Entered) + { + Box.BackColor = Color.AntiqueWhite; + Box.ForeColor = Color.DarkGray; + + PreviousState = State; + State = FieldState.Entered; + } + + ContainingForm.ActiveControl = null; + } + + public void SetToEntered(double value) + { + SetToEntered(); + + Data = value; + Box.Text = DecimalString(Data); + } + + public void SetToUnavailable() + { + if (State != FieldState.Unavailable) + { + Box.BackColor = Color.LightGray; + Box.ForeColor = Color.LightGray; + Box.Text = string.Empty; + + PreviousState = State; + State = FieldState.Unavailable; + } + } + + public void KeyDown(object sender, KeyEventArgs e) + { + switch(State) + { + case FieldState.Default: + if (e.KeyCode == Keys.Enter) + { + SetToDefault(); + } + else + { + SetToTyping(); + } + break; + + case FieldState.Entered: + if (e.KeyCode != Keys.Enter) + { + SetToTyping(); + } + break; + case FieldState.Typing: + HandleTyping(sender, e); + break; + case FieldState.Unavailable: + Box.Text = string.Empty; + ContainingForm.ActiveControl = null; + break; + default: + break; + } + } + + private void FocusLeave(object sender, EventArgs e) + { + if (State == FieldState.Typing) + { + TextToData(); + } + } + + private void HandleTyping(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Enter) + { + TextToData(); + + e.Handled = true; + e.SuppressKeyPress = true; + } + else if (e.KeyCode == Keys.Escape) + { + ContainingForm.ActiveControl = null; + e.Handled = true; + e.SuppressKeyPress = true; + } + } + + private void TextToData() + { + try + { + Data = Convert.ToDouble(Box.Text); + } + catch + { + } + + Box.Text = DecimalString(Data); + + if (string.Equals(Box.Text, DefaultText)) + { + SetToDefault(); + } + else + { + SetToEntered(); + } + } + + private string DecimalString(double value) + { + return value.ToString(FormatString); + } + + + #endregion Methods + } +} diff --git a/grapher/Models/Fields/FieldXY.cs b/grapher/Models/Fields/FieldXY.cs new file mode 100644 index 0000000..87e0b9c --- /dev/null +++ b/grapher/Models/Fields/FieldXY.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher +{ + public class FieldXY + { + public const int DefaultSeparation = 4; + + public const string ShortenedFormatString = "0.###"; + + public FieldXY(TextBox xBox, TextBox yBox, CheckBox lockCheckBox, Form containingForm, double defaultData) + { + XField = new Field(xBox, containingForm, defaultData); + YField = new Field(yBox, containingForm, defaultData); + YField.FormatString = ShortenedFormatString; + LockCheckBox = lockCheckBox; + LockCheckBox.CheckedChanged += new System.EventHandler(CheckChanged); + + XField.Box.Width = (YField.Box.Left + YField.Box.Width - XField.Box.Left - DefaultSeparation) / 2; + YField.Box.Width = XField.Box.Width; + + DefaultWidthX = XField.Box.Width; + DefaultWidthY = YField.Box.Width; + + YField.Box.Left = XField.Box.Left + XField.Box.Width + DefaultSeparation; + + CombinedWidth = DefaultWidthX + DefaultWidthY + YField.Box.Left - (XField.Box.Left + DefaultWidthX); + SetCombined(); + } + public double X + { + get => XField.Data; + } + + public double Y + { + get + { + if (Combined) + { + return X; + } + else + { + return YField.Data; + } + } + } + + public CheckBox LockCheckBox { get; } + + public Field XField { get; } + + public Field YField { get; } + + private bool Combined { get; set; } + + private int DefaultWidthX { get; } + + private int DefaultWidthY { get; } + + private int CombinedWidth { get; } + + private void CheckChanged(object sender, EventArgs e) + { + if (LockCheckBox.CheckState == CheckState.Checked) + { + SetCombined(); + } + else + { + SetSeparate(); + } + } + + public void SetCombined() + { + Combined = true; + YField.SetToUnavailable(); + YField.Box.Hide(); + XField.Box.Width = CombinedWidth; + XField.FormatString = Field.DefaultFormatString; + } + + public void SetSeparate() + { + Combined = false; + + XField.Box.Width = DefaultWidthX; + YField.Box.Width = DefaultWidthY; + + XField.FormatString = ShortenedFormatString; + + if (XField.State == Field.FieldState.Default) + { + YField.SetToDefault(); + } + else + { + YField.SetToEntered(XField.Data); + } + + if (XField.Box.Visible) + { + YField.Box.Show(); + } + } + + public void Show() + { + XField.Box.Show(); + + if (!Combined) + { + YField.Box.Show(); + } + } + + public void Hide() + { + XField.Box.Hide(); + YField.Box.Hide(); + } + } +} |