Blame mono/Contours/MainForm.cs

80bc9b
/*
80bc9b
    ......... 2015 Ivan Mahonin
80bc9b
80bc9b
    This program is free software: you can redistribute it and/or modify
80bc9b
    it under the terms of the GNU General Public License as published by
80bc9b
    the Free Software Foundation, either version 3 of the License, or
80bc9b
    (at your option) any later version.
80bc9b
80bc9b
    This program is distributed in the hope that it will be useful,
80bc9b
    but WITHOUT ANY WARRANTY; without even the implied warranty of
80bc9b
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80bc9b
    GNU General Public License for more details.
80bc9b
80bc9b
    You should have received a copy of the GNU General Public License
80bc9b
    along with this program.  If not, see <http: licenses="" www.gnu.org="">.</http:>
80bc9b
*/
80bc9b
d33d2a
using System;
d33d2a
using System.Windows.Forms;
d33d2a
using System.Collections.Generic;
d33d2a
using System.Linq;
d33d2a
using System.Drawing;
d33d2a
d33d2a
namespace Contours {
d33d2a
    public class MainForm: Form {
7c6265
        Button bTest;
7c6265
        ComboBox cbTests;
7c6265
        ComboBox cbViews;
7c6265
    
d33d2a
        public MainForm() {
05068e
            Width = 800;
05068e
            Height = 600;
05068e
        
7c6265
            bTest = new Button();
7c6265
            bTest.Left = 20;
7c6265
            bTest.Top = 20;
7c6265
            bTest.Text = "test";
7c6265
            bTest.Click += bTestClicked;
7c6265
            Controls.Add(bTest);
7c6265
7c6265
            cbTests = new ComboBox();
7c6265
            cbTests.Left = bTest.Right + 20;
7c6265
            cbTests.Top = 20;
7c6265
            cbTests.SelectedIndexChanged += cbTestsChanged;
7c6265
            Controls.Add(cbTests);
7c6265
7c6265
            cbViews = new ComboBox();
7c6265
            cbViews.Left = cbTests.Right + 20;
7c6265
            cbViews.Top = 20;
7c6265
            cbViews.SelectedIndexChanged += cbViewsChanged;
7c6265
            Controls.Add(cbViews);
7c6265
                        
d33d2a
            MouseDown += mouseDown;
d33d2a
            MouseMove += mouseMove;
d33d2a
            MouseUp += mouseUp;
d33d2a
            Paint += paint;
d33d2a
        }
d33d2a
d33d2a
        bool drawing = false;
f6cc12
        List<list<pointf>> contours = new List<list<pointf>>();</list<pointf></list<pointf>
d33d2a
7c6265
        void bTestClicked(object sender, EventArgs e) {
7c6265
            Test.loadTestsFromFile("tests.txt");
7c6265
            bool success = Test.runAll();
7c6265
            Test.saveReport("report.txt");
7c6265
            
7c6265
            foreach(Test test in Test.tests)
7c6265
                cbTests.Items.Add(test.name);
7c6265
            
30455f
            if (success) MessageBox.Show("Tests passed");
30455f
                    else MessageBox.Show("Tests failed");
7c6265
        }
7c6265
7c6265
        void cbTestsChanged(object sender, EventArgs e) {
7c6265
            cbViews.Items.Clear();
7c6265
            foreach(Test test in Test.tests) {
7c6265
                if (cbTests.Text == test.name) {
7c6265
                    foreach(string name in test.input.Keys)
7c6265
                        cbViews.Items.Add("i: " + name);
7c6265
                    foreach(string name in test.output.Keys)
7c6265
                        cbViews.Items.Add("o: " + name);
7c6265
                }
7c6265
            }
7c6265
        }
7c6265
7c6265
        void cbViewsChanged(object sender, EventArgs e) {
7c6265
            Refresh();
7c6265
        }
7c6265
d33d2a
        private void mouseDown(object sender, MouseEventArgs e) {
d33d2a
            if (e.Button == MouseButtons.Left) {
f6cc12
                contours.Add(new List<pointf>());</pointf>
d33d2a
                drawing = true;
d33d2a
                mouseMove(sender, e);
d33d2a
            }
d33d2a
        }
d33d2a
d33d2a
        private void mouseMove(object sender, MouseEventArgs e) {
d33d2a
            if (drawing) {
f6cc12
                contours.Last().Add(new PointF(e.Location.X, e.Location.Y));
d33d2a
                Refresh();
d33d2a
            }
d33d2a
        }
d33d2a
d33d2a
        private void mouseUp(object sender, MouseEventArgs e) {
d33d2a
            if (e.Button == MouseButtons.Left) {
d33d2a
                mouseMove(sender, e);
d33d2a
                drawing = false;
d33d2a
            }
d33d2a
            if (e.Button == MouseButtons.Right) {
d33d2a
                drawing = false;
f6cc12
                contours.Clear();
d33d2a
                Refresh();
d33d2a
            }
d33d2a
        }
d33d2a
7c6265
        void drawContour(Graphics g, Color color, List<pointf> c) {</pointf>
7c6265
            if (c != null && c.Count >= 3) {
7c6265
                g.DrawLines(new Pen(new SolidBrush(color)), c.ToArray());
7c6265
                g.DrawLine(new Pen(new SolidBrush(color)), c.First(), c.Last());
7c6265
            }
7c6265
        }
7c6265
7c6265
        void drawContour(Graphics g, Color color, List<point> c) {</point>
7c6265
            if (c != null && c.Count >= 3) {
7c6265
                g.DrawLines(new Pen(new SolidBrush(color)), c.ToArray());
7c6265
                g.DrawLine(new Pen(new SolidBrush(color)), c.First(), c.Last());
7c6265
            }
7c6265
        }
7c6265
d33d2a
        private void paint(object sender, PaintEventArgs e) {
d33d2a
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
7c6265
            foreach(List<pointf> c in contours)</pointf>
7c6265
                drawContour(e.Graphics, Color.Gray, c);
7c6265
            
7c6265
            List<list<list<point>>> testContours = null;</list<list<point>
7c6265
            foreach(Test test in Test.tests) {
7c6265
                if (cbTests.Text == test.name) {
7c6265
                    foreach(KeyValuePair<string, list<list<list<point="">>>> pair in test.input)</string,>
7c6265
                        if (cbViews.Text == "i: " + pair.Key)
7c6265
                            testContours = pair.Value;
7c6265
                    foreach(KeyValuePair<string, list<list<list<point="">>>> pair in test.output)</string,>
7c6265
                        if (cbViews.Text == "o: " + pair.Key)
7c6265
                            testContours = pair.Value;
7c6265
                }
7c6265
            }
7c6265
            
05068e
            System.Drawing.Drawing2D.Matrix m = e.Graphics.Transform;
05068e
            e.Graphics.TranslateTransform(50, 100);
7c6265
            if (testContours != null) {
7c6265
                foreach(List<list<point>> group in testContours) {</list<point>
7c6265
                    Color color = Color.Black;
7c6265
                    foreach(List<point> c in group) {</point>
7c6265
                        drawContour(e.Graphics, color, c);
7c6265
                        color = Color.Blue;
7c6265
                    }
d33d2a
                }
d33d2a
            }
05068e
            e.Graphics.Transform = m;
d33d2a
        }
d33d2a
    }
d33d2a
}
d33d2a