diff --git a/PowerCalc/MainForm.Designer.cs b/PowerCalc/MainForm.Designer.cs index dc69cea..427a497 100644 --- a/PowerCalc/MainForm.Designer.cs +++ b/PowerCalc/MainForm.Designer.cs @@ -38,6 +38,7 @@ this.calcLabel3 = new System.Windows.Forms.Label(); this.calcLabel4 = new System.Windows.Forms.Label(); this.splitContainer = new System.Windows.Forms.SplitContainer(); + this.coordLabel = new System.Windows.Forms.Label(); this.evalButton = new System.Windows.Forms.Button(); this.logExpandButton = new System.Windows.Forms.Button(); this.logBox = new System.Windows.Forms.TextBox(); @@ -54,18 +55,22 @@ | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.evalBox.BackColor = System.Drawing.Color.White; + this.evalBox.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.evalBox.Location = new System.Drawing.Point(3, 5); this.evalBox.Name = "evalBox"; - this.evalBox.Size = new System.Drawing.Size(231, 147); + this.evalBox.Size = new System.Drawing.Size(254, 146); this.evalBox.TabIndex = 0; + this.evalBox.Paint += new System.Windows.Forms.PaintEventHandler(this.evalBox_Paint); + this.evalBox.MouseLeave += new System.EventHandler(this.evalBox_MouseLeave); + this.evalBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.evalBox_MouseMove); // // calcBox1 // this.calcBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.calcBox1.Location = new System.Drawing.Point(27, 174); + this.calcBox1.Location = new System.Drawing.Point(27, 173); this.calcBox1.Name = "calcBox1"; - this.calcBox1.Size = new System.Drawing.Size(207, 20); + this.calcBox1.Size = new System.Drawing.Size(230, 20); this.calcBox1.TabIndex = 1; this.calcBox1.Text = "x"; // @@ -73,9 +78,9 @@ // this.calcBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.calcBox2.Location = new System.Drawing.Point(27, 200); + this.calcBox2.Location = new System.Drawing.Point(27, 199); this.calcBox2.Name = "calcBox2"; - this.calcBox2.Size = new System.Drawing.Size(207, 20); + this.calcBox2.Size = new System.Drawing.Size(230, 20); this.calcBox2.TabIndex = 2; this.calcBox2.Text = "x"; // @@ -83,9 +88,9 @@ // this.calcBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.calcBox3.Location = new System.Drawing.Point(27, 226); + this.calcBox3.Location = new System.Drawing.Point(27, 225); this.calcBox3.Name = "calcBox3"; - this.calcBox3.Size = new System.Drawing.Size(207, 20); + this.calcBox3.Size = new System.Drawing.Size(230, 20); this.calcBox3.TabIndex = 3; this.calcBox3.Text = "x"; // @@ -93,9 +98,9 @@ // this.calcBox4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.calcBox4.Location = new System.Drawing.Point(27, 252); + this.calcBox4.Location = new System.Drawing.Point(27, 251); this.calcBox4.Name = "calcBox4"; - this.calcBox4.Size = new System.Drawing.Size(207, 20); + this.calcBox4.Size = new System.Drawing.Size(230, 20); this.calcBox4.TabIndex = 4; this.calcBox4.Text = "x"; // @@ -104,7 +109,7 @@ this.calcLabel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.calcLabel1.AutoSize = true; this.calcLabel1.ForeColor = System.Drawing.Color.Red; - this.calcLabel1.Location = new System.Drawing.Point(3, 177); + this.calcLabel1.Location = new System.Drawing.Point(3, 176); this.calcLabel1.Name = "calcLabel1"; this.calcLabel1.Size = new System.Drawing.Size(27, 13); this.calcLabel1.TabIndex = 5; @@ -115,7 +120,7 @@ this.calcLabel2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.calcLabel2.AutoSize = true; this.calcLabel2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0))))); - this.calcLabel2.Location = new System.Drawing.Point(3, 203); + this.calcLabel2.Location = new System.Drawing.Point(3, 202); this.calcLabel2.Name = "calcLabel2"; this.calcLabel2.Size = new System.Drawing.Size(27, 13); this.calcLabel2.TabIndex = 6; @@ -126,7 +131,7 @@ this.calcLabel3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.calcLabel3.AutoSize = true; this.calcLabel3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0))))); - this.calcLabel3.Location = new System.Drawing.Point(3, 229); + this.calcLabel3.Location = new System.Drawing.Point(3, 228); this.calcLabel3.Name = "calcLabel3"; this.calcLabel3.Size = new System.Drawing.Size(27, 13); this.calcLabel3.TabIndex = 7; @@ -137,7 +142,7 @@ this.calcLabel4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.calcLabel4.AutoSize = true; this.calcLabel4.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); - this.calcLabel4.Location = new System.Drawing.Point(3, 255); + this.calcLabel4.Location = new System.Drawing.Point(3, 254); this.calcLabel4.Name = "calcLabel4"; this.calcLabel4.Size = new System.Drawing.Size(27, 13); this.calcLabel4.TabIndex = 8; @@ -153,6 +158,7 @@ // splitContainer.Panel1 // this.splitContainer.Panel1.AutoScroll = true; + this.splitContainer.Panel1.Controls.Add(this.coordLabel); this.splitContainer.Panel1.Controls.Add(this.evalButton); this.splitContainer.Panel1.Controls.Add(this.logExpandButton); this.splitContainer.Panel1.Controls.Add(this.evalBox); @@ -171,25 +177,34 @@ this.splitContainer.Panel2.Controls.Add(this.logBox); this.splitContainer.Panel2.Controls.Add(this.logCollapseButton); this.splitContainer.Panel2MinSize = 0; - this.splitContainer.Size = new System.Drawing.Size(407, 275); - this.splitContainer.SplitterDistance = 237; + this.splitContainer.Size = new System.Drawing.Size(448, 274); + this.splitContainer.SplitterDistance = 260; this.splitContainer.TabIndex = 9; // + // coordLabel + // + this.coordLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.coordLabel.AutoSize = true; + this.coordLabel.Location = new System.Drawing.Point(68, 155); + this.coordLabel.Name = "coordLabel"; + this.coordLabel.Size = new System.Drawing.Size(0, 13); + this.coordLabel.TabIndex = 10; + // // evalButton // this.evalButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.evalButton.Location = new System.Drawing.Point(3, 151); + this.evalButton.Location = new System.Drawing.Point(3, 150); this.evalButton.Name = "evalButton"; this.evalButton.Size = new System.Drawing.Size(59, 23); this.evalButton.TabIndex = 9; this.evalButton.Text = "Eval"; this.evalButton.UseVisualStyleBackColor = true; - this.evalButton.Click += new System.EventHandler(this.evalButton_Click); + this.evalButton.Click += new System.EventHandler(this.eval); // // logExpandButton // this.logExpandButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.logExpandButton.Location = new System.Drawing.Point(211, 151); + this.logExpandButton.Location = new System.Drawing.Point(234, 150); this.logExpandButton.Name = "logExpandButton"; this.logExpandButton.Size = new System.Drawing.Size(23, 23); this.logExpandButton.TabIndex = 0; @@ -204,15 +219,15 @@ this.logBox.Location = new System.Drawing.Point(0, 0); this.logBox.Multiline = true; this.logBox.Name = "logBox"; - this.logBox.Size = new System.Drawing.Size(166, 252); + this.logBox.Size = new System.Drawing.Size(184, 251); this.logBox.TabIndex = 0; // // logCollapseButton // this.logCollapseButton.Dock = System.Windows.Forms.DockStyle.Bottom; - this.logCollapseButton.Location = new System.Drawing.Point(0, 252); + this.logCollapseButton.Location = new System.Drawing.Point(0, 251); this.logCollapseButton.Name = "logCollapseButton"; - this.logCollapseButton.Size = new System.Drawing.Size(166, 23); + this.logCollapseButton.Size = new System.Drawing.Size(184, 23); this.logCollapseButton.TabIndex = 1; this.logCollapseButton.Text = "<<<"; this.logCollapseButton.UseVisualStyleBackColor = true; @@ -223,11 +238,12 @@ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224))))); - this.ClientSize = new System.Drawing.Size(407, 275); + this.ClientSize = new System.Drawing.Size(448, 274); this.Controls.Add(this.splitContainer); - this.MaximizeBox = false; + this.MinimumSize = new System.Drawing.Size(266, 243); this.Name = "MainForm"; this.Text = "PowerCalc"; + this.Resize += new System.EventHandler(this.eval); this.splitContainer.Panel1.ResumeLayout(false); this.splitContainer.Panel1.PerformLayout(); this.splitContainer.Panel2.ResumeLayout(false); @@ -254,6 +270,7 @@ private System.Windows.Forms.Button logCollapseButton; private System.Windows.Forms.Button logExpandButton; private System.Windows.Forms.Button evalButton; + private System.Windows.Forms.Label coordLabel; } } diff --git a/PowerCalc/MainForm.cs b/PowerCalc/MainForm.cs index a06a000..0f38f05 100644 --- a/PowerCalc/MainForm.cs +++ b/PowerCalc/MainForm.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; using System.Threading; using System.Windows.Forms; @@ -9,8 +10,6 @@ namespace PowerCalc { public partial class MainForm : Form { - private Thread evalThread; - public MainForm() => InitializeComponent(); private void logCollapseButton_Click(object sender, EventArgs e) @@ -25,94 +24,7 @@ namespace PowerCalc splitContainer.Panel2Collapsed = false; } - private void evalButton_Click(object sender, EventArgs e) - { - FormBorderStyle = FormBorderStyle.FixedSingle; - Enabled = false; - evalThread = new Thread(() => - { - Bitmap bmp = new Bitmap(evalBox.Width, evalBox.Height); - Graphics g = Graphics.FromImage(bmp); - List, Expression>> lines = new List, Expression>> - { - new Tuple, Expression>(Color.Red, new List(), new Expression(calcBox1.Text)), - new Tuple, Expression>(Color.FromArgb(255, 128, 0), new List(), new Expression(calcBox2.Text)), - new Tuple, Expression>(Color.FromArgb(0, 192, 0), new List(), new Expression(calcBox3.Text)), - new Tuple, Expression>(Color.FromArgb(0, 0, 192), new List(), new Expression(calcBox4.Text)) - }; - try - { - lines.ForEach(s => - { - for (double i = 0; i < evalBox.Width; i++) - { - try - { - s.Item3.Parameters.Clear(); - s.Item3.Parameters.Add("x", i / 10); - double val = -1; - object tmp = s.Item3.Evaluate(); - if (tmp.GetType() == typeof(bool)) - val = (bool)tmp ? 1 : 0; - else if (tmp.GetType() == typeof(byte)) - val = (byte)tmp; - else if (tmp.GetType() == typeof(sbyte)) - val = (sbyte)tmp; - else if (tmp.GetType() == typeof(short)) - val = (short)tmp; - else if (tmp.GetType() == typeof(ushort)) - val = (ushort)tmp; - else if (tmp.GetType() == typeof(int)) - val = (int)tmp; - else if (tmp.GetType() == typeof(uint)) - val = (uint)tmp; - else if (tmp.GetType() == typeof(long)) - val = (long)tmp; - else if (tmp.GetType() == typeof(ulong)) - val = (ulong)tmp; - else if (tmp.GetType() == typeof(float)) - val = (float)tmp; - else if (tmp.GetType() == typeof(double)) - val = (double)tmp; - else if (tmp.GetType() == typeof(decimal)) - val = (double)(decimal)tmp; - else - log("Type mismatch! (" + tmp.GetType().ToString() + ")"); - float val1 = Convert.ToSingle(val); - if (i >= 0 && i < evalBox.Width && val1 >= 0 && val1 < evalBox.Height) - s.Item2.Add(new PointF(Convert.ToSingle(i), evalBox.Height - val1)); - } - catch (Exception e1) - { -#if DEBUG - log(e1.ToString()); -#else - log(e1.Message); -#endif - break; - } - } - g.DrawLines(new Pen(s.Item1), s.Item2.ToArray()); - }); - g.Flush(); - g.Dispose(); - evalBox.BackgroundImage = bmp; - } - catch (Exception e1) - { - log("FATAL: " + e1.ToString()); - } - finally - { - Invoke((MethodInvoker)delegate () - { - FormBorderStyle = FormBorderStyle.Sizable; - Enabled = true; - }); - } - }); - evalThread.Start(); - } + private void eval(object sender, EventArgs e) => evalBox.Invalidate(); private void log(string text) { @@ -121,5 +33,103 @@ namespace PowerCalc Console.WriteLine(text); #endif } + + private void evalBox_Paint(object sender, PaintEventArgs e) + { + DateTime start = DateTime.Now; + try + { + Graphics g = e.Graphics; + List, Expression>> lines = new List, Expression>> + { + new Tuple, Expression>(Color.Red, new List(), new Expression(calcBox1.Text)), + new Tuple, Expression>(Color.FromArgb(255, 128, 0), new List(), new Expression(calcBox2.Text)), + new Tuple, Expression>(Color.FromArgb(0, 192, 0), new List(), new Expression(calcBox3.Text)), + new Tuple, Expression>(Color.FromArgb(0, 0, 192), new List(), new Expression(calcBox4.Text)) + }; + for (int i = 0; i < evalBox.Width; i += 10) + g.DrawLine(Pens.LightGray, i, 0, i, evalBox.Height); + for (int i = evalBox.Height; i > 0; i -= 10) + g.DrawLine(Pens.LightGray, 0, i, evalBox.Width, i); + lines.ForEach(s => + { + s.Item3.Parameters.Add("x", 0); + s.Item3.Parameters.Add("Pi", Math.PI); + s.Item3.Parameters.Add("pi", Math.PI); + s.Item3.Parameters.Add("PI", Math.PI); + s.Item3.Parameters.Add("e", Math.E); + s.Item3.Parameters.Add("E", Math.E); + for (double i = 0; i < evalBox.Width; i++) + { + try + { + s.Item3.Parameters["x"] = i / 10; + double val = -1; + object tmp = s.Item3.Evaluate(); + if (tmp.GetType() == typeof(bool)) + val = (bool)tmp ? 1 : 0; + else if (tmp.GetType() == typeof(byte)) + val = (byte)tmp; + else if (tmp.GetType() == typeof(sbyte)) + val = (sbyte)tmp; + else if (tmp.GetType() == typeof(short)) + val = (short)tmp; + else if (tmp.GetType() == typeof(ushort)) + val = (ushort)tmp; + else if (tmp.GetType() == typeof(int)) + val = (int)tmp; + else if (tmp.GetType() == typeof(uint)) + val = (uint)tmp; + else if (tmp.GetType() == typeof(long)) + val = (long)tmp; + else if (tmp.GetType() == typeof(ulong)) + val = (ulong)tmp; + else if (tmp.GetType() == typeof(float)) + val = (float)tmp; + else if (tmp.GetType() == typeof(double)) + val = (double)tmp; + else if (tmp.GetType() == typeof(decimal)) + val = (double)(decimal)tmp; + else + log("Type mismatch! (" + tmp.GetType().ToString() + ")"); + float val1 = Convert.ToSingle(val); + if (i >= 0 && i < evalBox.Width && val1 >= 0 && val1 < evalBox.Height) + s.Item2.Add(new PointF(Convert.ToSingle(i), evalBox.Height - (val1 * 10))); + } + catch (Exception e1) + { +#if DEBUG + log("Value error: " + e1.ToString()); +#else + log("Value error: " + e1.Message); +#endif + break; + } + } + g.DrawLines(new Pen(s.Item1), s.Item2.ToArray()); + }); + g.Flush(); + } + catch (Exception e1) + { + log("FATAL: " + e1.ToString()); + } + finally + { + log("Eval completed in: " + + string.Join(".", (DateTime.Now - start).ToString().Split('.') + .Select(s => + { + if (!s.Contains(":") && s.Length > 4) + s = s.Remove(4, s.Length - 4); + return s; + }) + )); + } + } + + private void evalBox_MouseMove(object sender, MouseEventArgs e) => coordLabel.Text = new Point(e.X, evalBox.Height - e.Y).ToString(); + + private void evalBox_MouseLeave(object sender, EventArgs e) => coordLabel.Text = ""; } } \ No newline at end of file