OSDN Git Service

b71caebd683423b5a48acfbcd454cb0744c7403b
[psychlops/silverlight.git] / dev4 / psychlops / core / device / hid.cs
1 using System;\r
2 using System.Windows;\r
3 using System.Windows.Controls;\r
4 using System.Windows.Documents;\r
5 using System.Windows.Input;\r
6 \r
7 \r
8 namespace Psychlops\r
9 {\r
10         public class HID\r
11         {\r
12                 public struct Button\r
13                 {\r
14                         private static object locker;\r
15                         static Button()\r
16                         {\r
17                                 locker = new object(); \r
18                         }\r
19                         private bool pushed_, released_, pressed_;\r
20                         public void down()\r
21                         {\r
22                                 lock (locker)\r
23                                 {\r
24                                         pushed_ = true;\r
25                                         pressed_ = true;\r
26                                 }\r
27                         }\r
28                         public void up()\r
29                         {\r
30                                 lock (locker)\r
31                                 {\r
32                                         released_ = true;\r
33                                         pressed_ = false;\r
34                                 }\r
35                         }\r
36                         public bool pushed() { lock (locker) { bool tmp = pushed_; pushed_ = false; return tmp; } }\r
37                         public bool released() { lock (locker) { bool tmp = released_; released_ = false; return tmp; } }\r
38                         public bool pressed() { return pressed_; }\r
39                 }\r
40         }\r
41 \r
42 \r
43         public static class Mouse\r
44         {\r
45                 static internal Point position_;\r
46                 static internal System.Windows.UIElement _prime;\r
47                 static public HID.Button left, right, middle;\r
48                 static internal Point wheelDelta_;\r
49 \r
50                 static public Point position { get { return position_; } }\r
51                 static public int x { get { return (int)position_.x; } }\r
52                 static public int y { get { return (int)position_.y; } }\r
53                 static public Point wheelDelta { get { return wheelDelta_; } }\r
54 \r
55                 static public void Canvas_MousePos(object sender, MouseEventArgs e)\r
56                 {\r
57                         System.Windows.Point p = e.GetPosition(_prime);\r
58                         position_.set(p.X, p.Y);\r
59                 }\r
60                 static public void Canvas_LDown(object sender, MouseEventArgs e)\r
61                 {\r
62                         left.down();\r
63                         System.Windows.Point p = e.GetPosition(_prime);\r
64                         position_.set(p.X, p.Y);\r
65                 }\r
66                 static public void Canvas_LUp(object sender, MouseEventArgs e)\r
67                 {\r
68                         left.up();\r
69                         System.Windows.Point p = e.GetPosition(_prime);\r
70                         position_.set(p.X, p.Y);\r
71                 }\r
72                 static public void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)\r
73                 {\r
74                         wheelDelta_.y += e.Delta;\r
75                         System.Windows.Point p = e.GetPosition(_prime);\r
76                         position_.set(p.X, p.Y);\r
77                 }\r
78         }\r
79 \r
80 \r
81         public static class Keyboard\r
82         {\r
83                 public class IKey\r
84                 {\r
85                         //internal static HID.Button[] states;\r
86                         //int p;\r
87                         internal HID.Button state;\r
88                         public void down() { state.down(); }\r
89                         public void up() { state.up(); }\r
90                         public bool pushed() { return state.pushed(); }\r
91                         public bool released() { return state.released(); }\r
92                         public bool pressed() { return state.pressed(); }\r
93                 }\r
94                 static public IKey\r
95                         alt, shift, ctrl,\r
96                         esc, spc, rtn,\r
97                         left, right, up, down,\r
98                         tab,\r
99                         comma, period,\r
100                         q, w, e, r, t, y, u, i, o, p,\r
101                         a, s, d, f, g, h, j, k, l,\r
102                         z, x, c, v, b, n, m,\r
103                         one, two, three, four, five, six, seven, eight, nine, zero,\r
104                         pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9;\r
105                 static private System.Collections.Generic.Dictionary<System.Windows.Input.Key, IKey> map;\r
106                 static public void Canvas_KeyUp(object sender, KeyEventArgs e)\r
107                 {\r
108                         if (map.ContainsKey(e.Key)) map[e.Key].up();\r
109                 }\r
110                 static public void Canvas_KeyDown(object sender, KeyEventArgs e)\r
111                 {\r
112                         if (map.ContainsKey(e.Key)) map[e.Key].down();\r
113                 }\r
114 \r
115 \r
116                 static Keyboard()\r
117                 {\r
118                         /*\r
119                         esc = new Key(0); spc = new Key(1); rtn = new Key(2);\r
120                         left = new Key(3); right = new Key(4); up = new Key(5); down = new Key(6);\r
121                         comma = new Key(7); period = new Key(8);\r
122                         q = new Key(32); w = new Key(33); e = new Key(34); r = new Key(35); t = new Key(36); y = new Key(37); u = new Key(38); i = new Key(39); o = new Key(40); p = new Key(41);\r
123                         a = new Key(42); s = new Key(43); d = new Key(44); f = new Key(45); g = new Key(46); h = new Key(47); j = new Key(48); k = new Key(49); l = new Key(50);\r
124                         z = new Key(51); x = new Key(52); c = new Key(53); v = new Key(54); b = new Key(55); n = new Key(56); m = new Key(57);\r
125                         one = new Key(80); two = new Key(81); three = new Key(82); four = new Key(83); five = new Key(84); six = new Key(85); seven = new Key(86); eight = new Key(87); nine = new Key(88); zero = new Key(96);\r
126                         pad0 = new Key(96); pad1 = new Key(97); pad2 = new Key(98); pad3 = new Key(99); pad4 = new Key(100); pad5 = new Key(101); pad6 = new Key(102); pad7 = new Key(103); pad8 = new Key(104); pad9 = new Key(105);\r
127                         */\r
128                         alt = new IKey(); shift = new IKey(); ctrl = new IKey();\r
129                         esc = new IKey(); spc = new IKey(); rtn = new IKey();\r
130                         left = new IKey(); right = new IKey(); up = new IKey(); down = new IKey();\r
131                         comma = new IKey(); period = new IKey();\r
132                         q = new IKey(); w = new IKey(); e = new IKey(); r = new IKey(); t = new IKey(); y = new IKey(); u = new IKey(); i = new IKey(); o = new IKey(); p = new IKey();\r
133                         a = new IKey(); s = new IKey(); d = new IKey(); f = new IKey(); g = new IKey(); h = new IKey(); j = new IKey(); k = new IKey(); l = new IKey();\r
134                         z = new IKey(); x = new IKey(); c = new IKey(); v = new IKey(); b = new IKey(); n = new IKey(); m = new IKey();\r
135                         one = new IKey(); two = new IKey(); three = new IKey(); four = new IKey(); five = new IKey(); six = new IKey(); seven = new IKey(); eight = new IKey(); nine = new IKey(); zero = new IKey();\r
136                         pad0 = new IKey(); pad1 = new IKey(); pad2 = new IKey(); pad3 = new IKey(); pad4 = new IKey(); pad5 = new IKey(); pad6 = new IKey(); pad7 = new IKey(); pad8 = new IKey(); pad9 = new IKey();\r
137 \r
138                         map = new System.Collections.Generic.Dictionary<System.Windows.Input.Key, IKey>();\r
139                         map.Add(Key.Alt, alt); map.Add(Key.Shift, shift); map.Add(Key.Ctrl, ctrl);\r
140                         map.Add(Key.Escape, esc); map.Add(Key.Space, spc); map.Add(Key.Enter, rtn);\r
141                         map.Add(Key.Left, left); map.Add(Key.Right, right); map.Add(Key.Up, up); map.Add(Key.Down, down);\r
142                         map.Add(Key.Tab, tab);\r
143                         map.Add(Key.PageDown, comma); map.Add(Key.PageUp, period);\r
144                         map.Add(Key.Q, q); map.Add(Key.W, w); map.Add(Key.E, e); map.Add(Key.R, r); map.Add(Key.T, t); map.Add(Key.Y, y); map.Add(Key.U, u); map.Add(Key.I, i); map.Add(Key.O, o); map.Add(Key.P, p);\r
145                         map.Add(Key.A, a); map.Add(Key.S, s); map.Add(Key.D, d); map.Add(Key.F, f); map.Add(Key.G, g); map.Add(Key.H, h); map.Add(Key.J, j); map.Add(Key.K, k); map.Add(Key.L, l);\r
146                         map.Add(Key.Z, z); map.Add(Key.X, x); map.Add(Key.C, c); map.Add(Key.V, v); map.Add(Key.B, b); map.Add(Key.N, n); map.Add(Key.M, m);\r
147                         map.Add(Key.D1, one); map.Add(Key.D2, two); map.Add(Key.D3, three); map.Add(Key.D4, four); map.Add(Key.D5, five); map.Add(Key.D6, six); map.Add(Key.D7, seven); map.Add(Key.D8, eight); map.Add(Key.D9, nine); map.Add(Key.D0, zero);\r
148                         map.Add(Key.NumPad0, pad0); map.Add(Key.NumPad1, pad1); map.Add(Key.NumPad2, pad2); map.Add(Key.NumPad3, pad3); map.Add(Key.NumPad4, pad4); map.Add(Key.NumPad5, pad5); map.Add(Key.NumPad6, pad6); map.Add(Key.NumPad7, pad7); map.Add(Key.NumPad8, pad8); map.Add(Key.NumPad9, pad9);\r
149                 }\r
150         }\r
151 \r
152 }