Blame mono/Contours/MainForm.cs

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