-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGirafficFormEventsWrapper.cs
179 lines (146 loc) · 5.45 KB
/
GirafficFormEventsWrapper.cs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Giraffics
{
// Custom Event Args
public class MoveArgs : EventArgs
{
public Point oldPosition;
public Point newPosition;
}
public class ResizeArgs : EventArgs
{
public Size oldSize;
public Size newSize;
}
// A digestible wrapper for the BufferedWindow's input-related events.
public partial class Giraffic
{
// Window Events
public event FormClosingEventHandler WindowClosing;
public event FormClosedEventHandler WindowClosed;
public event EventHandler WindowFocused;
public event EventHandler WindowUnfocused;
public event EventHandler<MoveArgs> WindowMoved;
public event EventHandler<ResizeArgs> WindowResized;
// Keyboard Events
public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;
// Mouse Events
public event MouseEventHandler MouseDoubleClick;
public event MouseEventHandler MouseDown;
public event MouseEventHandler MouseUp;
public event EventHandler MouseEnter;
public event EventHandler MouseLeave;
public event MouseEventHandler MouseMove;
public event MouseEventHandler MouseWheel;
// Tracking window properties for extra event info
private Point windowPosition;
private Size windowSize;
private List<Keys> keysDown = new List<Keys>();
private void ListenToWindowEvents()
{
// Initially track some of the window's properties
windowPosition = window.Location;
windowSize = window.Size;
// Window Events
window.FormClosing += Form_FormClosing; // WindowClosing
window.FormClosed += Form_FormClosed; // WindowClosed
window.GotFocus += Form_GotFocus; // WindowFocused
window.LostFocus += Form_LostFocus; // WindowUnfocused
window.Move += Form_Move; // WindowMoved
window.Resize += Form_Resize; // WindowResized
// Keyboard Events
window.KeyDown += Form_KeyDown; // KeyDown
window.KeyUp += Form_KeyUp; // KeyUp
// Mouse Events
window.MouseDoubleClick += Form_MouseDoubleClick; // MouseDoubleClick
window.MouseDown += Form_MouseDown; // MouseDown
window.MouseUp += Form_MouseUp; // MouseUp
window.MouseEnter += Form_MouseEnter; // MouseEnter
window.MouseLeave += Form_MouseLeave; // MouseLeave
window.MouseMove += Form_MouseMove; // MouseMove
window.MouseWheel += Form_MouseWheel; // MouseWheel
}
#region Window Events
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
WindowClosing?.Invoke(sender, e);
}
private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
WindowClosed?.Invoke(sender, e);
}
private void Form_GotFocus(object sender, EventArgs e)
{
WindowFocused?.Invoke(sender, e);
}
private void Form_LostFocus(object sender, EventArgs e)
{
WindowUnfocused?.Invoke(sender, e);
}
private void Form_Move(object sender, EventArgs e)
{
Point oldPos = new Point(windowPosition.X, windowPosition.Y);
windowPosition = window.Location;
MoveArgs args = new MoveArgs() { oldPosition = oldPos, newPosition = windowPosition };
WindowMoved?.Invoke(sender, args);
}
private void Form_Resize(object sender, EventArgs e)
{
Size oldSize = new Size(windowSize.Width, windowSize.Height);
windowSize = window.Size;
ResizeArgs args = new ResizeArgs() { oldSize = oldSize, newSize = windowSize };
WindowResized?.Invoke(sender, args);
}
#endregion
#region Keyboard Events
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if (!keysDown.Contains(e.KeyCode))
{
keysDown.Add(e.KeyCode);
KeyDown?.Invoke(sender, e);
}
}
private void Form_KeyUp(object sender, KeyEventArgs e)
{
if (keysDown.Contains(e.KeyCode))
keysDown.Remove(e.KeyCode);
KeyUp?.Invoke(sender, e);
}
#endregion
#region Mouse Events
private void Form_MouseDoubleClick(object sender, MouseEventArgs e)
{
MouseDoubleClick?.Invoke(sender, e);
}
private void Form_MouseDown(object sender, MouseEventArgs e)
{
MouseDown?.Invoke(sender, e);
}
private void Form_MouseUp(object sender, MouseEventArgs e)
{
MouseUp?.Invoke(sender, e);
}
private void Form_MouseEnter(object sender, System.EventArgs e)
{
MouseEnter?.Invoke(sender, e);
}
private void Form_MouseLeave(object sender, System.EventArgs e)
{
MouseLeave?.Invoke(sender, e);
}
private void Form_MouseMove(object sender, MouseEventArgs e)
{
MouseMove?.Invoke(sender, e);
}
private void Form_MouseWheel(object sender, MouseEventArgs e)
{
MouseWheel?.Invoke(sender, e);
}
#endregion
}
}