Basic system written. Now I need to add tools for it. Had to delete the SetZoomForm.

This commit is contained in:
That_One_Nerd 2024-04-08 09:56:28 -04:00
parent 41732d01e7
commit 3ab55b509a
5 changed files with 29 additions and 408 deletions

View File

@ -13,9 +13,6 @@
<Compile Update="Forms\GraphForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Update="Forms\SetZoomForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Update="Forms\TranslateForm.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -27,26 +27,16 @@ public partial class GraphForm : Form
public float DpiFloat { get; private set; }
public double ZoomLevel
public Float2 ZoomLevel
{
get => _zoomLevel;
set
{
double oldZoom = ZoomLevel;
_zoomLevel = Math.Clamp(value, 1e-5, 1e3);
int totalSegments = 0;
foreach (Graphable able in ables) totalSegments += able.GetItemsToRender(this).Count();
if (totalSegments > 10_000)
{
_zoomLevel = oldZoom;
return; // Too many segments, stop.
}
_zoomLevel = new(Math.Clamp(value.x, 1e-5, 1e3),
Math.Clamp(value.y, 1e-5, 1e3));
}
}
private double _zoomLevel;
private Float2 _zoomLevel;
private readonly Point initialWindowPos;
private readonly Size initialWindowSize;
@ -74,7 +64,7 @@ public partial class GraphForm : Form
DpiFloat = (float)((Dpi.x + Dpi.y) / 2);
ables = [];
ZoomLevel = 1;
ZoomLevel = new(1, 1);
initialWindowPos = Location;
initialWindowSize = Size;
@ -88,8 +78,8 @@ public partial class GraphForm : Form
graphPoint.x -= ScreenCenter.x;
graphPoint.y -= ScreenCenter.y;
graphPoint.x *= Dpi.x / ZoomLevel;
graphPoint.y *= Dpi.y / ZoomLevel;
graphPoint.x *= Dpi.x / ZoomLevel.x;
graphPoint.y *= Dpi.y / ZoomLevel.y;
graphPoint.x += ClientRectangle.Width / 2.0;
graphPoint.y += ClientRectangle.Height / 2.0;
@ -103,8 +93,8 @@ public partial class GraphForm : Form
result.x -= ClientRectangle.Width / 2.0;
result.y -= ClientRectangle.Height / 2.0;
result.x /= Dpi.x / ZoomLevel;
result.y /= Dpi.y / ZoomLevel;
result.x /= Dpi.x / ZoomLevel.x;
result.y /= Dpi.y / ZoomLevel.y;
result.x += ScreenCenter.x;
result.y += ScreenCenter.y;
@ -116,19 +106,20 @@ public partial class GraphForm : Form
protected virtual void PaintGrid(Graphics g)
{
double axisScale = Math.Pow(2, Math.Round(Math.Log2(ZoomLevel)));
double axisScaleX = Math.Pow(2, Math.Round(Math.Log2(ZoomLevel.x))),
axisScaleY = Math.Pow(2, Math.Round(Math.Log2(ZoomLevel.y)));
// Draw horizontal/vertical quarter-axis.
Brush quarterBrush = new SolidBrush(QuarterAxisColor);
Pen quarterPen = new(quarterBrush, DpiFloat * 2 / 192);
for (double x = Math.Ceiling(MinVisibleGraph.x * 4 / axisScale) * axisScale / 4; x <= Math.Floor(MaxVisibleGraph.x * 4 / axisScale) * axisScale / 4; x += axisScale / 4)
for (double x = Math.Ceiling(MinVisibleGraph.x * 4 / axisScaleX) * axisScaleX / 4; x <= Math.Floor(MaxVisibleGraph.x * 4 / axisScaleX) * axisScaleX / 4; x += axisScaleX / 4)
{
Int2 startPos = GraphSpaceToScreenSpace(new Float2(x, MinVisibleGraph.y)),
endPos = GraphSpaceToScreenSpace(new Float2(x, MaxVisibleGraph.y));
g.DrawLine(quarterPen, startPos, endPos);
}
for (double y = Math.Ceiling(MinVisibleGraph.y * 4 / axisScale) * axisScale / 4; y <= Math.Floor(MaxVisibleGraph.y * 4 / axisScale) * axisScale / 4; y += axisScale / 4)
for (double y = Math.Ceiling(MinVisibleGraph.y * 4 / axisScaleY) * axisScaleY / 4; y <= Math.Floor(MaxVisibleGraph.y * 4 / axisScaleY) * axisScaleY / 4; y += axisScaleY / 4)
{
Int2 startPos = GraphSpaceToScreenSpace(new Float2(MinVisibleGraph.x, y)),
endPos = GraphSpaceToScreenSpace(new Float2(MaxVisibleGraph.x, y));
@ -139,13 +130,13 @@ public partial class GraphForm : Form
Brush semiBrush = new SolidBrush(SemiAxisColor);
Pen semiPen = new(semiBrush, DpiFloat * 2 / 192);
for (double x = Math.Ceiling(MinVisibleGraph.x / axisScale) * axisScale; x <= Math.Floor(MaxVisibleGraph.x / axisScale) * axisScale; x += axisScale)
for (double x = Math.Ceiling(MinVisibleGraph.x / axisScaleX) * axisScaleX; x <= Math.Floor(MaxVisibleGraph.x / axisScaleX) * axisScaleX; x += axisScaleX)
{
Int2 startPos = GraphSpaceToScreenSpace(new Float2(x, MinVisibleGraph.y)),
endPos = GraphSpaceToScreenSpace(new Float2(x, MaxVisibleGraph.y));
g.DrawLine(semiPen, startPos, endPos);
}
for (double y = Math.Ceiling(MinVisibleGraph.y / axisScale) * axisScale; y <= Math.Floor(MaxVisibleGraph.y / axisScale) * axisScale; y += axisScale)
for (double y = Math.Ceiling(MinVisibleGraph.y / axisScaleY) * axisScaleY; y <= Math.Floor(MaxVisibleGraph.y / axisScaleY) * axisScaleY; y += axisScaleY)
{
Int2 startPos = GraphSpaceToScreenSpace(new Float2(MinVisibleGraph.x, y)),
endPos = GraphSpaceToScreenSpace(new Float2(MaxVisibleGraph.x, y));
@ -166,14 +157,15 @@ public partial class GraphForm : Form
}
protected virtual void PaintUnits(Graphics g)
{
double axisScale = Math.Pow(2, Math.Round(Math.Log(ZoomLevel, 2)));
double axisScaleX = Math.Pow(2, Math.Round(Math.Log2(ZoomLevel.x))),
axisScaleY = Math.Pow(2, Math.Round(Math.Log2(ZoomLevel.y)));
Brush textBrush = new SolidBrush(UnitsTextColor);
Font textFont = new(Font.Name, 9, FontStyle.Regular);
// X-axis
int minX = (int)(DpiFloat * 50 / 192),
maxX = ClientRectangle.Height - (int)(DpiFloat * 40 / 192);
for (double x = Math.Ceiling(MinVisibleGraph.x / axisScale) * axisScale; x <= MaxVisibleGraph.x; x += axisScale)
for (double x = Math.Ceiling(MinVisibleGraph.x / axisScaleX) * axisScaleX; x <= MaxVisibleGraph.x; x += axisScaleX)
{
if (x == 0) x = 0; // Fixes -0
@ -187,7 +179,7 @@ public partial class GraphForm : Form
// Y-axis
int minY = (int)(DpiFloat * 10 / 192);
for (double y = Math.Ceiling(MinVisibleGraph.y / axisScale) * axisScale; y <= MaxVisibleGraph.y; y += axisScale)
for (double y = Math.Ceiling(MinVisibleGraph.y / axisScaleY) * axisScaleY; y <= MaxVisibleGraph.y; y += axisScaleY)
{
if (y == 0) continue;
@ -304,7 +296,7 @@ public partial class GraphForm : Form
{
Int2 pixelDiff = new(initialMouseLocation.x - Cursor.Position.X,
initialMouseLocation.y - Cursor.Position.Y);
Float2 graphDiff = new(pixelDiff.x * ZoomLevel / Dpi.x, pixelDiff.y * ZoomLevel / Dpi.y);
Float2 graphDiff = new(pixelDiff.x * ZoomLevel.x / Dpi.x, pixelDiff.y * ZoomLevel.y / Dpi.y);
ScreenCenter = new(initialScreenCenter.x + graphDiff.x,
initialScreenCenter.y + graphDiff.y);
}
@ -318,7 +310,7 @@ public partial class GraphForm : Form
{
Int2 pixelDiff = new(initialMouseLocation.x - Cursor.Position.X,
initialMouseLocation.y - Cursor.Position.Y);
Float2 graphDiff = new(pixelDiff.x * ZoomLevel / Dpi.x, pixelDiff.y * ZoomLevel / Dpi.y);
Float2 graphDiff = new(pixelDiff.x * ZoomLevel.x / Dpi.x, pixelDiff.y * ZoomLevel.y / Dpi.y);
ScreenCenter = new(initialScreenCenter.x + graphDiff.x,
initialScreenCenter.y + graphDiff.y);
Invalidate(false);
@ -327,14 +319,18 @@ public partial class GraphForm : Form
}
protected override void OnMouseWheel(MouseEventArgs e)
{
ZoomLevel *= 1 - e.Delta * 0.00075; // Zoom factor.
Float2 newZoom = ZoomLevel;
newZoom.x *= 1 - e.Delta * 0.00075; // Zoom factor.
newZoom.y *= 1 - e.Delta * 0.00075;
ZoomLevel = newZoom;
Invalidate(false);
}
private void ResetViewportButton_Click(object? sender, EventArgs e)
{
ScreenCenter = new Float2(0, 0);
ZoomLevel = 1;
ZoomLevel = new(1, 1);
Invalidate(false);
}
private void GraphColorPickerButton_Click(Graphable able)
@ -449,17 +445,7 @@ public partial class GraphForm : Form
private void ButtonViewportSetZoom_Click(object? sender, EventArgs e)
{
SetZoomForm zoomer = new(this)
{
StartPosition = FormStartPosition.Manual,
};
zoomer.Location = new Point(Location.X + ClientRectangle.Width + 10,
Location.Y + (ClientRectangle.Height - zoomer.ClientRectangle.Height) / 2);
if (zoomer.Location.X + zoomer.Width > Screen.FromControl(this).WorkingArea.Width)
{
zoomer.StartPosition = FormStartPosition.WindowsDefaultLocation;
}
zoomer.ShowDialog();
MessageBox.Show("TODO", "Set Viewport Zoom", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void ButtonViewportSetCenter_Click(object? sender, EventArgs e)
{
@ -468,7 +454,7 @@ public partial class GraphForm : Form
private void ButtonViewportReset_Click(object? sender, EventArgs e)
{
ScreenCenter = new Float2(0, 0);
ZoomLevel = 1;
ZoomLevel = new(1, 1);
Invalidate(false);
}
private void ButtonViewportResetWindow_Click(object? sender, EventArgs e)

View File

@ -1,120 +0,0 @@
using System.Drawing;
using System.Windows.Forms;
namespace Graphing.Forms
{
partial class SetZoomForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
MessageLabel = new Label();
ZoomTrackBar = new TrackBar();
ValueLabel = new Label();
ZoomMinValue = new TextBox();
ZoomMaxValue = new TextBox();
((System.ComponentModel.ISupportInitialize)ZoomTrackBar).BeginInit();
SuspendLayout();
//
// MessageLabel
//
MessageLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
MessageLabel.Location = new Point(52, 20);
MessageLabel.Name = "MessageLabel";
MessageLabel.Size = new Size(413, 35);
MessageLabel.TabIndex = 0;
MessageLabel.Text = "Set the zoom level for the graph.";
MessageLabel.TextAlign = ContentAlignment.MiddleCenter;
//
// ZoomTrackBar
//
ZoomTrackBar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
ZoomTrackBar.LargeChange = 1000;
ZoomTrackBar.Location = new Point(12, 127);
ZoomTrackBar.Maximum = 10000;
ZoomTrackBar.Name = "ZoomTrackBar";
ZoomTrackBar.Size = new Size(489, 90);
ZoomTrackBar.TabIndex = 1;
ZoomTrackBar.TickStyle = TickStyle.None;
ZoomTrackBar.Scroll += ZoomTrackBar_Scroll;
//
// ValueLabel
//
ValueLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
ValueLabel.Location = new Point(52, 91);
ValueLabel.Name = "ValueLabel";
ValueLabel.Size = new Size(413, 33);
ValueLabel.TabIndex = 2;
ValueLabel.Text = "1.00x";
ValueLabel.TextAlign = ContentAlignment.TopCenter;
//
// ZoomMinValue
//
ZoomMinValue.Location = new Point(12, 178);
ZoomMinValue.Name = "ZoomMinValue";
ZoomMinValue.Size = new Size(83, 39);
ZoomMinValue.TabIndex = 3;
ZoomMinValue.Text = "0.50";
ZoomMinValue.TextChanged += ZoomMinValue_TextChanged;
//
// ZoomMaxValue
//
ZoomMaxValue.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ZoomMaxValue.Location = new Point(418, 178);
ZoomMaxValue.Name = "ZoomMaxValue";
ZoomMaxValue.Size = new Size(83, 39);
ZoomMaxValue.TabIndex = 4;
ZoomMaxValue.Text = "2.00";
ZoomMaxValue.TextAlign = HorizontalAlignment.Right;
ZoomMaxValue.TextChanged += ZoomMaxValue_TextChanged;
//
// SetZoomForm
//
AutoScaleDimensions = new SizeF(13F, 32F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(513, 230);
Controls.Add(ZoomMaxValue);
Controls.Add(ZoomMinValue);
Controls.Add(ValueLabel);
Controls.Add(ZoomTrackBar);
Controls.Add(MessageLabel);
FormBorderStyle = FormBorderStyle.FixedToolWindow;
Name = "SetZoomForm";
Text = "Zoom Level";
((System.ComponentModel.ISupportInitialize)ZoomTrackBar).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label MessageLabel;
private TrackBar ZoomTrackBar;
private Label ValueLabel;
private TextBox ZoomMinValue;
private TextBox ZoomMaxValue;
}
}

View File

@ -1,122 +0,0 @@
using System;
using System.Windows.Forms;
namespace Graphing.Forms;
public partial class SetZoomForm : Form
{
private double minZoomRange;
private double maxZoomRange;
private double zoomLevel;
private readonly GraphForm form;
public SetZoomForm(GraphForm form)
{
InitializeComponent();
minZoomRange = 1 / (form.ZoomLevel * 2);
maxZoomRange = 2 / form.ZoomLevel;
zoomLevel = 1 / form.ZoomLevel;
ZoomTrackBar.Value = (int)(ZoomToFactor(zoomLevel) * (ZoomTrackBar.Maximum - ZoomTrackBar.Minimum) + ZoomTrackBar.Minimum);
this.form = form;
}
protected override void OnPaint(PaintEventArgs e)
{
ZoomMaxValue.Text = maxZoomRange.ToString("0.00");
ZoomMinValue.Text = minZoomRange.ToString("0.00");
ValueLabel.Text = $"{zoomLevel:0.00}x";
base.OnPaint(e);
form.ZoomLevel = 1 / zoomLevel;
form.Invalidate(false);
}
private double FactorToZoom(double factor)
{
return minZoomRange + (factor * factor) * (maxZoomRange - minZoomRange);
}
private double ZoomToFactor(double zoom)
{
double sqrValue = (zoom - minZoomRange) / (maxZoomRange - minZoomRange);
return Math.Sign(sqrValue) * Math.Sqrt(Math.Abs(sqrValue));
}
private void ZoomTrackBar_Scroll(object? sender, EventArgs e)
{
double factor = (ZoomTrackBar.Value - ZoomTrackBar.Minimum) / (double)(ZoomTrackBar.Maximum - ZoomTrackBar.Minimum);
zoomLevel = FactorToZoom(factor);
Invalidate(true);
}
private void ZoomMinValue_TextChanged(object? sender, EventArgs e)
{
double original = minZoomRange;
try
{
double value;
if (string.IsNullOrWhiteSpace(ZoomMinValue.Text) ||
ZoomMinValue.Text.EndsWith('.'))
{
return;
}
else
{
value = double.Parse(ZoomMinValue.Text);
if (value < 1e-2 || value > 1e3 || value > maxZoomRange) throw new();
}
minZoomRange = value;
ZoomTrackBar.Value = (int)Math.Clamp(ZoomToFactor(zoomLevel) * (ZoomTrackBar.Maximum - ZoomTrackBar.Minimum) + ZoomTrackBar.Minimum, ZoomTrackBar.Minimum, ZoomTrackBar.Maximum);
double factor = (ZoomTrackBar.Value - ZoomTrackBar.Minimum) / (double)(ZoomTrackBar.Maximum - ZoomTrackBar.Minimum);
double newZoom = FactorToZoom(factor);
zoomLevel = newZoom;
if (newZoom != factor) Invalidate(true);
}
catch
{
minZoomRange = original;
ZoomMinValue.Text = minZoomRange.ToString("0.00");
}
}
private void ZoomMaxValue_TextChanged(object sender, EventArgs e)
{
double original = maxZoomRange;
try
{
double value;
if (string.IsNullOrWhiteSpace(ZoomMaxValue.Text) ||
ZoomMaxValue.Text.EndsWith('.'))
{
return;
}
else
{
value = double.Parse(ZoomMaxValue.Text);
if (value < 1e-2 || value > 1e3 || value < minZoomRange) throw new();
}
maxZoomRange = value;
ZoomTrackBar.Value = (int)Math.Clamp(ZoomToFactor(zoomLevel) * (ZoomTrackBar.Maximum - ZoomTrackBar.Minimum) + ZoomTrackBar.Minimum, ZoomTrackBar.Minimum, ZoomTrackBar.Maximum);
double factor = (ZoomTrackBar.Value - ZoomTrackBar.Minimum) / (double)(ZoomTrackBar.Maximum - ZoomTrackBar.Minimum);
double newZoom = FactorToZoom(factor);
zoomLevel = newZoom;
if (newZoom != factor) Invalidate(true);
}
catch
{
maxZoomRange = original;
ZoomMaxValue.Text = maxZoomRange.ToString("0.00");
}
}
}

View File

@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>