summaryrefslogtreecommitdiff
path: root/Backup/Anoto/Form1.cs
blob: c3f97a968dda9747aca4b1cc8363bb16be206048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Anoto;
using System.Threading;


namespace Anoto
{
    public struct Stroke
    {
        List<Point> Points;
    }

    public partial class Form1 : Form
    {

        Anoto.GenericStreamer.PenManagerClass PenManager;

        Dictionary<string, Stroke> PensStrocks;

        Dictionary<string, List<Point>> PensPoints;
        Dictionary<string, Brush> PensColor;
        Mutex mutex;
        Random Rnd;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PenManager = new Anoto.GenericStreamer.PenManagerClass();

            PenManager.PenConnected += new Anoto.GenericStreamer._IPenManagerEvents_PenConnectedEventHandler(pm_PenConnected);
            PenManager.NewCoordinate += new Anoto.GenericStreamer._IPenManagerEvents_NewCoordinateEventHandler(PenManager_NewCoordinate);
            PenManager.PenDown += new Anoto.GenericStreamer._IPenManagerEvents_PenDownEventHandler(PenManager_PenDown);
            PenManager.PenUp += new Anoto.GenericStreamer._IPenManagerEvents_PenUpEventHandler(PenManager_PenUp);
            PenManager.Start();

            mutex = new Mutex();

            InitDictionary();

            Rnd = new Random();
        }

        private void InitDictionary()
        {
            PensPoints = new Dictionary<string, List<Point>>();
            PensColor = new Dictionary<string, Brush>();

            PensStrocks = new Dictionary<string, Stroke>();
        }

        void PenManager_PenUp(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, byte penDownSeqNbr, int isSpcdGenerated)
        {
            Console.WriteLine("Pen Up " + penSerial + " time " + time);
        }

        void PenManager_PenDown(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, byte penDownSeqNbr, Anoto.GenericStreamer.PenTipType PenTipType, int isValidColor, byte r, byte g, byte b, int isSpcdGenerated)
        {
            Console.WriteLine("Pen Down " + penSerial + " time " + time);
           // PensStrocks[penSerial].
        }

        void PenManager_NewCoordinate(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, string page, int x, int y, byte imgSeqNbr, byte force)
        {
            mutex.WaitOne();
            PensPoints[penSerial].Add(new Point(x, y));
            mutex.ReleaseMutex();
            Console.WriteLine("Pen NewCoordinate " + penSerial + " time " + time + " x " + x + " y " + y);
           pictureBox1.Invalidate();
        }

        void pm_PenConnected(string penSerial, Anoto.GenericStreamer.PenType PenType, ulong time, string productName, ushort pid)
        {
          
            //  Consol("Pen connected ");
            //    listBox1.Items.Add("Pen connected " + penSerial + " " + PenType.ToString() + " " + productName + "\n\r");
            Console.WriteLine("Pen connected " + penSerial + " " + PenType.ToString() + " " + productName);
            PensPoints.Add(penSerial, new List<Point>());
            PensColor.Add(penSerial,new SolidBrush( Color.FromArgb(255,Rnd.Next(255),Rnd.Next(255),Rnd.Next(255))));
            // listBox1.Invalidate();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {


            PenManager.Stop();

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //draw the dots
            Graphics g = e.Graphics;
            g.FillRectangle(Brushes.White, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            mutex.WaitOne();
            foreach (var pen in PensPoints)
            {
                foreach (var point in pen.Value)
                {
                    g.FillEllipse(PensColor[pen.Key], (float)(point.X) / 10.0f, (float)(point.Y) / 10.0f, 2, 2);
                }
            }
            mutex.ReleaseMutex();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Invalidate();
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mutex.WaitOne();

            foreach (var pen in PensPoints)
            {
     //           pen.Value = new List<Point>();
               
            }

            mutex.ReleaseMutex();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show();
            }
        }
    }
}