Improve code a bit

This commit is contained in:
CreepyCrafter24 2020-04-07 22:04:21 +02:00
parent 92e82700d6
commit 6bf16775ed
3 changed files with 68 additions and 57 deletions

View File

@ -214,7 +214,7 @@
this.evalButton.TabIndex = 9; this.evalButton.TabIndex = 9;
this.evalButton.Text = "Eval"; this.evalButton.Text = "Eval";
this.evalButton.UseVisualStyleBackColor = true; this.evalButton.UseVisualStyleBackColor = true;
this.evalButton.Click += new System.EventHandler(this.eval); this.evalButton.Click += new System.EventHandler(this.Eval);
// //
// logExpandButton // logExpandButton
// //
@ -258,7 +258,7 @@
this.MinimumSize = new System.Drawing.Size(266, 243); this.MinimumSize = new System.Drawing.Size(266, 243);
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "PowerCalc"; this.Text = "PowerCalc";
this.Resize += new System.EventHandler(this.eval); this.Resize += new System.EventHandler(this.Eval);
this.splitContainer.Panel1.ResumeLayout(false); this.splitContainer.Panel1.ResumeLayout(false);
this.splitContainer.Panel1.PerformLayout(); this.splitContainer.Panel1.PerformLayout();
this.splitContainer.Panel2.ResumeLayout(false); this.splitContainer.Panel2.ResumeLayout(false);

View File

@ -11,9 +11,9 @@ namespace PowerCalc
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
private Point offStart = Point.Empty; private Point _offStart = Point.Empty;
public decimal offX; private decimal _offX;
public decimal offY; private decimal _offY;
public MainForm() public MainForm()
{ {
@ -38,9 +38,9 @@ namespace PowerCalc
splitContainer.Panel2Collapsed = false; splitContainer.Panel2Collapsed = false;
} }
private void eval(object sender, EventArgs e) => evalBox.Invalidate(); private void Eval(object sender, EventArgs e) => evalBox.Invalidate();
private void log(object text) private void Log(object text)
{ {
logBox.Lines = new[] {text.ToString()}.Concat(logBox.Lines).Where((s, i) => i < 30).ToArray(); logBox.Lines = new[] {text.ToString()}.Concat(logBox.Lines).Where((s, i) => i < 30).ToArray();
#if DEBUG #if DEBUG
@ -70,10 +70,10 @@ namespace PowerCalc
new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(0, 0, 192), new List<PointF>(), new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(0, 0, 192), new List<PointF>(),
new Expression(calcBox4.Text)) new Expression(calcBox4.Text))
}; };
for (int i = (int) (offX % 10); i < evalBox.Width; i += 10) for (int i = (int) (_offX % 10); i < evalBox.Width; i += 10)
g.DrawLine(offX - i == 0 ? Pens.Gray : Pens.LightGray, i, 0, i, evalBox.Height); g.DrawLine(_offX - i == 0 ? Pens.Gray : Pens.LightGray, i, 0, i, evalBox.Height);
for (int i = evalBox.Height + (int) (offY % 10); i > 0; i -= 10) for (int i = evalBox.Height + (int) (_offY % 10); i > 0; i -= 10)
g.DrawLine(evalBox.Height + (offY - i) == 0 ? Pens.Gray : Pens.LightGray, 0, i, evalBox.Width, i); g.DrawLine(evalBox.Height + (_offY - i) == 0 ? Pens.Gray : Pens.LightGray, 0, i, evalBox.Width, i);
lines.ForEach(s => lines.ForEach(s =>
{ {
s.Item3.Parameters.Add("Pi", Math.PI); s.Item3.Parameters.Add("Pi", Math.PI);
@ -85,46 +85,64 @@ namespace PowerCalc
{ {
try try
{ {
s.Item3.Parameters["x"] = (i - (double) offX) / 10; s.Item3.Parameters["x"] = (i - (double) _offX) / 10;
double val = -1; double val = -1;
object tmp = s.Item3.Evaluate(); object tmp = s.Item3.Evaluate();
if (tmp.GetType() == typeof(bool)) switch (tmp)
val = (bool) tmp ? 1 : 0; {
else if (tmp.GetType() == typeof(byte)) case bool b:
val = (byte) tmp; val = b ? 1 : 0;
else if (tmp.GetType() == typeof(sbyte)) break;
val = (sbyte) tmp; case byte b:
else if (tmp.GetType() == typeof(short)) val = b;
val = (short) tmp; break;
else if (tmp.GetType() == typeof(ushort)) case sbyte o:
val = (ushort) tmp; val = o;
else if (tmp.GetType() == typeof(int)) break;
val = (int) tmp; case short o:
else if (tmp.GetType() == typeof(uint)) val = o;
val = (uint) tmp; break;
else if (tmp.GetType() == typeof(long)) case ushort o:
val = (long) tmp; val = o;
else if (tmp.GetType() == typeof(ulong)) break;
val = (ulong) tmp; case int o:
else if (tmp.GetType() == typeof(float)) val = o;
val = (float) tmp; break;
else if (tmp.GetType() == typeof(double)) case uint u:
val = (double) tmp; val = u;
else if (tmp.GetType() == typeof(decimal)) break;
val = (double) (decimal) tmp; case long l:
else val = l;
log("Type mismatch! (" + tmp.GetType() + ")"); break;
case ulong o:
val = o;
break;
case float f:
val = f;
break;
case double d:
val = d;
break;
case decimal o:
val = (double) o;
break;
default:
Log("Type mismatch! (" + tmp.GetType() + ")");
break;
}
float val1 = Convert.ToSingle(val); float val1 = Convert.ToSingle(val);
float loc = Convert.ToSingle(evalBox.Height - (val1 * 10 - (float) offY)); float loc = Convert.ToSingle(evalBox.Height - (val1 * 10 - (float) _offY));
if (loc >= 0 && loc < evalBox.Height) PointF tmp2 = new PointF(Convert.ToSingle(i), loc);
s.Item2.Add(new PointF(Convert.ToSingle(i), loc)); if (!new[] {tmp2.X, tmp2.Y}.Any(f => float.IsInfinity(f) || float.IsNaN(f)))
s.Item2.Add(tmp2);
} }
catch (Exception e1) catch (Exception e1)
{ {
#if DEBUG #if DEBUG
log("Value error: " + e1); log("Value error: " + e1);
#else #else
log("Value error: " + e1.Message); Log("Value error: " + e1.Message);
#endif #endif
break; break;
} }
@ -136,11 +154,11 @@ namespace PowerCalc
} }
catch (Exception e1) catch (Exception e1)
{ {
log(e1); Log(e1);
} }
finally finally
{ {
log("Eval completed in: " + Log("Eval completed in: " +
string.Join(".", (DateTime.Now - start).ToString().Split('.') string.Join(".", (DateTime.Now - start).ToString().Split('.')
.Select(s => .Select(s =>
{ {
@ -154,13 +172,13 @@ namespace PowerCalc
private void evalBox_MouseMove(object sender, MouseEventArgs e) private void evalBox_MouseMove(object sender, MouseEventArgs e)
{ {
coordLabel.Text = new Point((int) Math.Round((double) (e.X - offX) / 10d), coordLabel.Text = new Point((int) Math.Round((double) (e.X - _offX) / 10d),
(int) Math.Round((double) (evalBox.Height + offY - e.Y) / 10d)).ToString(); (int) Math.Round((double) (evalBox.Height + _offY - e.Y) / 10d)).ToString();
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{ {
offX += e.X - offStart.X; _offX += e.X - _offStart.X;
offY += e.Y - offStart.Y; _offY += e.Y - _offStart.Y;
offStart = e.Location; _offStart = e.Location;
evalBox.Invalidate(); evalBox.Invalidate();
} }
} }
@ -168,7 +186,7 @@ namespace PowerCalc
private void evalBox_MouseLeave(object sender, EventArgs e) => private void evalBox_MouseLeave(object sender, EventArgs e) =>
coordLabel.Text = Point.Empty.ToString().Replace("0", ""); coordLabel.Text = Point.Empty.ToString().Replace("0", "");
private void evalBox_MouseDown(object sender, MouseEventArgs e) => offStart = e.Location; private void evalBox_MouseDown(object sender, MouseEventArgs e) => _offStart = e.Location;
private void saveButton_Click(object sender, EventArgs e) private void saveButton_Click(object sender, EventArgs e)
{ {

View File

@ -12,13 +12,6 @@
<PostBuildEvent>if exist "$(SolutionDir)Data\pkgtool.exe" ($(SolutionDir)Data\pkgtool.exe build --noLogo --binDir .) else if exist "%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" ("%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" build --noLogo --binDir .) else echo Cound not find Package build tools, skipping</PostBuildEvent> <PostBuildEvent>if exist "$(SolutionDir)Data\pkgtool.exe" ($(SolutionDir)Data\pkgtool.exe build --noLogo --binDir .) else if exist "%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" ("%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" build --noLogo --binDir .) else echo Cound not find Package build tools, skipping</PostBuildEvent>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Antlr4" Version="4.6.6" />
<PackageReference Include="Antlr4.CodeGenerator" Version="4.6.6" />
<PackageReference Include="Antlr4.Runtime" Version="4.6.6" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="NCalc2" Version="2.1.0" /> <PackageReference Include="NCalc2" Version="2.1.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>