Offsetter is almost done.

This commit is contained in:
That_One_Nerd 2024-03-23 08:04:41 -04:00
parent 24828e9922
commit 30529673d0
2 changed files with 425 additions and 5 deletions

View File

@ -28,19 +28,176 @@
/// </summary>
private void InitializeComponent()
{
TrackX = new System.Windows.Forms.TrackBar();
LabelX = new System.Windows.Forms.Label();
MinBoxX = new System.Windows.Forms.TextBox();
MaxBoxX = new System.Windows.Forms.TextBox();
ThisValueX = new System.Windows.Forms.TextBox();
ThisValueY = new System.Windows.Forms.TextBox();
MaxBoxY = new System.Windows.Forms.TextBox();
MinBoxY = new System.Windows.Forms.TextBox();
LabelY = new System.Windows.Forms.Label();
TrackY = new System.Windows.Forms.TrackBar();
TitleLabel = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)TrackX).BeginInit();
((System.ComponentModel.ISupportInitialize)TrackY).BeginInit();
SuspendLayout();
//
// TrackX
//
TrackX.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
TrackX.LargeChange = 250;
TrackX.Location = new System.Drawing.Point(15, 193);
TrackX.Margin = new System.Windows.Forms.Padding(0);
TrackX.Maximum = 1000;
TrackX.Name = "TrackX";
TrackX.Size = new System.Drawing.Size(644, 90);
TrackX.SmallChange = 50;
TrackX.TabIndex = 0;
TrackX.TabStop = false;
TrackX.TickFrequency = 50;
TrackX.TickStyle = System.Windows.Forms.TickStyle.Both;
TrackX.Value = 1;
TrackX.Scroll += TrackX_Scroll;
//
// LabelX
//
LabelX.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
LabelX.Location = new System.Drawing.Point(15, 157);
LabelX.Name = "LabelX";
LabelX.Size = new System.Drawing.Size(644, 36);
LabelX.TabIndex = 1;
LabelX.Text = "X Offset";
LabelX.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// MinBoxX
//
MinBoxX.Location = new System.Drawing.Point(15, 259);
MinBoxX.Name = "MinBoxX";
MinBoxX.Size = new System.Drawing.Size(100, 39);
MinBoxX.TabIndex = 2;
//
// MaxBoxX
//
MaxBoxX.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
MaxBoxX.Location = new System.Drawing.Point(556, 259);
MaxBoxX.Name = "MaxBoxX";
MaxBoxX.Size = new System.Drawing.Size(100, 39);
MaxBoxX.TabIndex = 3;
MaxBoxX.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// ThisValueX
//
ThisValueX.Anchor = System.Windows.Forms.AnchorStyles.Top;
ThisValueX.Location = new System.Drawing.Point(289, 259);
ThisValueX.Name = "ThisValueX";
ThisValueX.Size = new System.Drawing.Size(100, 39);
ThisValueX.TabIndex = 4;
ThisValueX.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// ThisValueY
//
ThisValueY.Anchor = System.Windows.Forms.AnchorStyles.Top;
ThisValueY.Location = new System.Drawing.Point(289, 449);
ThisValueY.Name = "ThisValueY";
ThisValueY.Size = new System.Drawing.Size(100, 39);
ThisValueY.TabIndex = 9;
ThisValueY.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// MaxBoxY
//
MaxBoxY.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
MaxBoxY.Location = new System.Drawing.Point(556, 449);
MaxBoxY.Name = "MaxBoxY";
MaxBoxY.Size = new System.Drawing.Size(100, 39);
MaxBoxY.TabIndex = 8;
MaxBoxY.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// MinBoxY
//
MinBoxY.Location = new System.Drawing.Point(15, 449);
MinBoxY.Name = "MinBoxY";
MinBoxY.Size = new System.Drawing.Size(100, 39);
MinBoxY.TabIndex = 7;
//
// LabelY
//
LabelY.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
LabelY.Location = new System.Drawing.Point(15, 347);
LabelY.Name = "LabelY";
LabelY.Size = new System.Drawing.Size(644, 36);
LabelY.TabIndex = 6;
LabelY.Text = "Y Offset";
LabelY.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// TrackY
//
TrackY.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
TrackY.LargeChange = 250;
TrackY.Location = new System.Drawing.Point(15, 383);
TrackY.Margin = new System.Windows.Forms.Padding(0);
TrackY.Maximum = 1000;
TrackY.Name = "TrackY";
TrackY.Size = new System.Drawing.Size(644, 90);
TrackY.SmallChange = 50;
TrackY.TabIndex = 5;
TrackY.TabStop = false;
TrackY.TickFrequency = 50;
TrackY.TickStyle = System.Windows.Forms.TickStyle.Both;
TrackY.Value = 1;
TrackY.Scroll += TrackY_Scroll;
//
// TitleLabel
//
TitleLabel.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
TitleLabel.Location = new System.Drawing.Point(12, 39);
TitleLabel.Name = "TitleLabel";
TitleLabel.Padding = new System.Windows.Forms.Padding(0, 0, 0, 18);
TitleLabel.Size = new System.Drawing.Size(644, 89);
TitleLabel.TabIndex = 10;
TitleLabel.Text = "Change the Location of %name%";
TitleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// TranslateForm
//
AutoScaleDimensions = new System.Drawing.SizeF(13F, 32F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(674, 629);
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
ClientSize = new System.Drawing.Size(674, 531);
Controls.Add(TitleLabel);
Controls.Add(ThisValueY);
Controls.Add(MaxBoxY);
Controls.Add(MinBoxY);
Controls.Add(LabelY);
Controls.Add(TrackY);
Controls.Add(ThisValueX);
Controls.Add(MaxBoxX);
Controls.Add(MinBoxX);
Controls.Add(LabelX);
Controls.Add(TrackX);
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
Name = "TranslateForm";
Text = "TranslateForm";
Padding = new System.Windows.Forms.Padding(15);
Text = "Herm";
((System.ComponentModel.ISupportInitialize)TrackX).EndInit();
((System.ComponentModel.ISupportInitialize)TrackY).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private System.Windows.Forms.TrackBar TrackX;
private System.Windows.Forms.Label LabelX;
private System.Windows.Forms.TextBox MinBoxX;
private System.Windows.Forms.TextBox MaxBoxX;
private System.Windows.Forms.TextBox ThisValueX;
private System.Windows.Forms.TextBox ThisValueY;
private System.Windows.Forms.TextBox MaxBoxY;
private System.Windows.Forms.TextBox MinBoxY;
private System.Windows.Forms.Label LabelY;
private System.Windows.Forms.TrackBar TrackY;
private System.Windows.Forms.Label TitleLabel;
}
}

View File

@ -1,4 +1,5 @@
using Graphing.Abstract;
using System;
using System.Windows.Forms;
namespace Graphing.Forms;
@ -10,16 +11,278 @@ public partial class TranslateForm : Form
// These variables both represent the same graphable.
private readonly Graphable ableRaw;
private readonly ITranslatable ableTrans;
private readonly ITranslatableX? ableTransX;
private readonly ITranslatableY? ableTransY;
private readonly bool useX;
private readonly bool useY;
private double minX, maxX, curX, minY, maxY, curY;
public TranslateForm(GraphForm graph, Graphable ableRaw, ITranslatable ableTrans)
{
InitializeComponent();
Text = $"Translate {ableRaw.Name}";
TitleLabel.Text = $"Adjust Location for {ableRaw.Name}";
MinBoxX.Leave += (o, e) => UpdateFromMinBoxY();
MinBoxX.KeyDown += (o, e) =>
{
if (e.KeyCode == Keys.Enter) UpdateFromMinBoxY();
};
MaxBoxX.Leave += (o, e) => UpdateFromMaxBoxY();
MaxBoxX.KeyDown += (o, e) =>
{
if (e.KeyCode == Keys.Enter) UpdateFromMaxBoxY();
};
ThisValueX.Leave += (o, e) => UpdateFromThisBoxY();
ThisValueX.KeyDown += (o, e) =>
{
if (e.KeyCode == Keys.Enter) UpdateFromThisBoxY();
};
MinBoxY.Leave += (o, e) => UpdateFromMinBoxY();
MinBoxY.KeyDown += (o, e) =>
{
if (e.KeyCode == Keys.Enter) UpdateFromMinBoxY();
};
MaxBoxY.Leave += (o, e) => UpdateFromMaxBoxY();
MaxBoxY.KeyDown += (o, e) =>
{
if (e.KeyCode == Keys.Enter) UpdateFromMaxBoxY();
};
ThisValueY.Leave += (o, e) => UpdateFromThisBoxY();
ThisValueY.KeyDown += (o, e) =>
{
if (e.KeyCode == Keys.Enter) UpdateFromThisBoxY();
};
refForm = graph;
this.ableRaw = ableRaw;
this.ableTrans = ableTrans;
if (ableTrans is ITranslatableX transX) transX.OffsetX = 1;
if (ableTrans is ITranslatableY transY) transY.OffsetY = 1;
double curX = 0, curY = 0;
if (ableTrans is ITranslatableX transX)
{
useX = true;
ableTransX = transX;
curX = transX.OffsetX;
}
else
{
LabelY.Location = LabelX.Location;
TrackY.Location = TrackX.Location;
MinBoxY.Location = MinBoxX.Location;
MaxBoxY.Location = MaxBoxX.Location;
ThisValueY.Location = ThisValueX.Location;
graph.Invalidate(false);
LabelX.Dispose();
TrackX.Dispose();
MinBoxX.Dispose();
MaxBoxX.Dispose();
ThisValueX.Dispose();
}
if (ableTrans is ITranslatableY transY)
{
useY = true;
ableTransY = transY;
curY = transY.OffsetY;
}
else
{
LabelY.Dispose();
TrackY.Dispose();
MinBoxY.Dispose();
MaxBoxY.Dispose();
ThisValueY.Dispose();
}
if (!useX && !useY)
{
TitleLabel.Text = $"There doesn't seem to be anything you can translate for {ableRaw.Name}.";
}
minX = -10;
maxX = 10;
minY = -10;
maxY = 10;
UpdateFromCurX(curX, false);
UpdateFromCurY(curY, false);
}
private void UpdateFromCurX(double newCurX, bool invalidate)
{
curX = newCurX;
if (curX < minX) minX = curX;
else if (curX > maxX) maxX = curX;
int step = (int)(1000 * InverseLerp(minX, maxX, curX));
TrackX.Value = step;
MinBoxX.Text = $"{minX:0.00}";
MaxBoxX.Text = $"{maxX:0.00}";
ThisValueX.Text = $"{curX:0.00}";
if (invalidate) refForm.Invalidate(false);
}
private void UpdateFromSliderX(bool invalidate)
{
double t = InverseLerp(0, 1000, TrackX.Value);
curX = Lerp(minX, maxX, t);
ThisValueX.Text = $"{curX:0.00}";
ableTransX!.OffsetX = curX;
if (invalidate) refForm.Invalidate(false);
}
private void UpdateFromMinBoxX()
{
if (!double.TryParse(MinBoxX.Text, out double newMin))
{
MinBoxX.Text = $"{minX:0.00}";
return;
}
minX = newMin;
MinBoxX.Text = $"{minX:0.00}";
if (minX > curX)
{
curX = minX;
ThisValueX.Text = $"{curX:0.00}";
ableTransX!.OffsetX = curX;
}
int step = (int)(1000 * InverseLerp(minX, maxX, curX));
TrackX.Value = step;
refForm.Invalidate(false);
}
private void UpdateFromMaxBoxX()
{
if (!double.TryParse(MaxBoxX.Text, out double newMax))
{
MaxBoxX.Text = $"{maxX:0.00}";
return;
}
maxX = newMax;
MaxBoxX.Text = $"{maxX:0.00}";
if (maxX < curX)
{
curX = maxX;
ThisValueX.Text = $"{curX:0.00}";
ableTransX!.OffsetX = curX;
}
int step = (int)(1000 * InverseLerp(minX, maxX, curX));
TrackX.Value = step;
refForm.Invalidate(false);
}
private void UpdateFromThisBoxX()
{
if (!double.TryParse(ThisValueX.Text, out double newCur))
{
ThisValueX.Text = $"{curX:0.00}";
return;
}
ableTransX!.OffsetX = newCur;
UpdateFromCurX(newCur, true);
}
private void UpdateFromCurY(double newCurY, bool invalidate)
{
curY = newCurY;
if (curY < minY) minY = curY;
else if (curY > maxY) maxY = curY;
int step = (int)(1000 * InverseLerp(minY, maxY, curY));
TrackY.Value = step;
MinBoxY.Text = $"{minY:0.00}";
MaxBoxY.Text = $"{maxY:0.00}";
ThisValueY.Text = $"{curY:0.00}";
if (invalidate) refForm.Invalidate(false);
}
private void UpdateFromSliderY(bool invalidate)
{
double t = InverseLerp(0, 1000, TrackY.Value);
curY = Lerp(minY, maxY, t);
ThisValueY.Text = $"{curY:0.00}";
ableTransY!.OffsetY = curY;
if (invalidate) refForm.Invalidate(false);
}
private void UpdateFromMinBoxY()
{
if (!double.TryParse(MinBoxY.Text, out double newMin))
{
MinBoxY.Text = $"{minY:0.00}";
return;
}
minY = newMin;
MinBoxY.Text = $"{minY:0.00}";
if (minY > curY)
{
curY = minY;
ThisValueY.Text = $"{curY:0.00}";
ableTransY!.OffsetY = curY;
}
int step = (int)(1000 * InverseLerp(minY, maxY, curY));
TrackY.Value = step;
refForm.Invalidate(false);
}
private void UpdateFromMaxBoxY()
{
if (!double.TryParse(MaxBoxY.Text, out double newMax))
{
MaxBoxY.Text = $"{maxY:0.00}";
return;
}
maxY = newMax;
MaxBoxY.Text = $"{maxY:0.00}";
if (maxY < curY)
{
curY = maxY;
ThisValueY.Text = $"{curY:0.00}";
ableTransY!.OffsetY = curY;
}
int step = (int)(1000 * InverseLerp(minY, maxY, curY));
TrackY.Value = step;
refForm.Invalidate(false);
}
private void UpdateFromThisBoxY()
{
if (!double.TryParse(ThisValueY.Text, out double newCur))
{
ThisValueY.Text = $"{curY:0.00}";
return;
}
ableTransY!.OffsetY = newCur;
UpdateFromCurY(newCur, true);
}
private static double Lerp(double a, double b, double t) => a + t * (b - a);
private static double InverseLerp(double a, double b, double c) => (c - a) / (b - a);
private void TrackX_Scroll(object? sender, EventArgs e)
{
UpdateFromSliderX(true);
}
private void TrackY_Scroll(object sender, EventArgs e)
{
UpdateFromSliderY(true);
}
}