-
Notifications
You must be signed in to change notification settings - Fork 3
/
graphicsevents.go
118 lines (107 loc) · 3.11 KB
/
graphicsevents.go
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
package gohotdraw
import (
"fmt"
"os"
"x-go-binding.googlecode.com/hg/xgb"
)
func (this *XGBGraphics) AddInputListener(l InputListener) {
this.listeners.Push(l)
}
func (this *XGBGraphics) RemoveInputListener(l InputListener) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
if currentListener == l {
this.listeners.Delete(i)
return
}
}
}
func (this *XGBGraphics) StartListening() {
for {
reply := this.GetEventReply()
switch xgbEvent := reply.(type) {
case xgb.ExposeEvent:
event := &ExposeEvent{}
event.X = int(xgbEvent.X)
event.Y = int(xgbEvent.Y)
event.Width = int(xgbEvent.Width)
event.Height = int(xgbEvent.Height)
this.fireExposeHappened(event)
case xgb.ButtonPressEvent:
event := &MouseEvent{}
event.X = int(xgbEvent.EventX)
event.Y = int(xgbEvent.EventY)
event.Button = int(xgbEvent.Detail)
event.KeyModifier = int(xgbEvent.State)
this.fireMouseDown(event)
case xgb.ButtonReleaseEvent:
event := &MouseEvent{}
event.X = int(xgbEvent.EventX)
event.Y = int(xgbEvent.EventY)
event.Button = int(xgbEvent.Detail)
event.KeyModifier = int(xgbEvent.State)
this.fireMouseUp(event)
case xgb.MotionNotifyEvent:
event := &MouseEvent{}
event.X = int(xgbEvent.EventX)
event.Y = int(xgbEvent.EventY)
event.Button = int(xgbEvent.Detail)
event.KeyModifier = int(xgbEvent.State)
this.fireMouseDrag(event)
case xgb.KeyPressEvent:
event := &KeyEvent{}
event.KeyCode = int(xgbEvent.Detail)
event.KeyModifier = int(xgbEvent.State)
this.fireKeyDown(event)
case xgb.KeyReleaseEvent:
event := &KeyEvent{}
event.KeyCode = int(xgbEvent.Detail)
event.KeyModifier = int(xgbEvent.State)
this.fireKeyUp(event)
}
}
}
func (this *XGBGraphics) fireMouseUp(event *MouseEvent) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
currentListener.MouseUp(event)
}
}
func (this *XGBGraphics) fireMouseDown(event *MouseEvent) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
currentListener.MouseDown(event)
}
}
func (this *XGBGraphics) fireMouseDrag(event *MouseEvent) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
currentListener.MouseDrag(event)
}
}
func (this *XGBGraphics) fireKeyUp(event *KeyEvent) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
currentListener.KeyUp(event)
}
}
func (this *XGBGraphics) fireKeyDown(event *KeyEvent) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
currentListener.KeyDown(event)
}
}
func (this *XGBGraphics) fireExposeHappened(event *ExposeEvent) {
for i := 0; i < this.listeners.Len(); i++ {
currentListener := this.listeners.At(i).(InputListener)
currentListener.ExposeHappened(event)
}
}
func (this *XGBGraphics) GetEventReply() xgb.Event {
reply, err := this.connection.WaitForEvent()
if err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
return reply
}