-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
219 lines (192 loc) · 4.82 KB
/
events.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package contraption
import (
"encoding/gob"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/h2non/filetype"
"github.com/neputevshina/geom"
)
type EventPoint struct {
E interface{}
Pt geom.Point
T time.Time
z, zc int
}
func (ev EventPoint) valuestring() string {
t := strings.Replace(fmt.Sprintf("%T", ev.E), "contraption.", "", -1)
return fmt.Sprintf("%s(%v)", t, ev.E)
}
func (ev EventPoint) String() string {
if ev.E == nil {
return "<empty>"
}
return fmt.Sprintf("%v: %s@%v", ev.T, ev.valuestring(), ev.Pt)
}
// Click is a mouse click event, representing a button number.
type Click int
type Hover struct{}
//type Message func() interface{}
// Unclick is a mouse button release event, representing a button number.
type Unclick int
// Scroll is a vertical mouse scroll event.
// Positive values represent scrolling up, negative — scrolling down.
type Scroll int
// Sweep is a horizontal mouse scroll event.
// Negative values represent scrolling left, positive — scrolling right.
type Sweep int
// Press is a keyboard key press event, representing the key code
// with Rune.
//
// Rune part is not matched in regular expressions.
type Press struct {
Key
Rune rune
}
func (p Press) key() Key { return p.Key }
// Release is a keyboard key release event, representing the key code with
// optional Rune if releasing emitted a text enter.
//
// Rune part is not matched in regular expressions.
type Release struct {
Key
Rune rune
}
func (p Release) key() Key { return p.Key }
// Drag is an event of dragged into application file.
// TODO: change events backend to one that supports Drag events — GLFW does not.
// type Drag struct {
// Paths []string
// mime string
// }
// Drag is an event of dragged and dropped into application file.
type Drop struct {
Paths []string
mime string
}
// Every event type above must be registered in Gob for working record-replay functionality.
func init() {
gob.Register(Click(0))
gob.Register(Hover{})
gob.Register(Unclick(0))
gob.Register(Scroll(0))
gob.Register(Sweep(0))
gob.Register(Press{})
gob.Register(Release{})
gob.Register(Drop{})
}
// Special values of rune.
//
// If one needs to enter those code points, they either writing
// a terminal emulator and should insert such special characters
// using Ctrl+Shift+<Key> instead, or they are plainly doing
// something wrong.
const (
RuneDelete = '\x7f' // ASCII Delete
RuneBackspace = '\b' // ASCII Backspace
RuneLeft = '\x11' // ASCII Device Control 1 — Left cursor key
RuneDown = '\x12' // ASCII Device Control 2 — Down ...
RuneUp = '\x13' // ASCII Device Control 3 — Up ...
RuneRight = '\x14' // ASCII Device Control 4 — Right ...
)
type dummyerr struct{}
func (*dummyerr) Error() string {
return ""
}
const (
anyShift = 1000000
anyCtrl = 1000001
)
// nameevent converts type name and value stirng into event value.
// May be later replaced with reflection and type registry.
func nameevent(typ string, value string) any {
rechar := regexp.MustCompile("'.'")
renum := regexp.MustCompile("[-+]?(0|[1-9][0-9]*)")
intv := 0
var keyv glfw.Key
var err error = &dummyerr{}
if value == "" {
// Skip this whole else chain.
err = nil
} else if typ == `Drag` || typ == `Drop` {
if !filetype.IsMIMESupported(value) {
panic(`unsupported MIME type: ` + value)
}
err = nil
} else if rechar.FindString(value) == value {
// runev = []rune(value)[0]
err = nil
} else if renum.FindString(value) == value {
intv, err = strconv.Atoi(value)
} else {
err = nil
ok := false
keyv, ok = keynames[value]
if !ok {
panic("no such constant “" + value + "”")
}
}
if err != nil {
panic("check parser for errors: " + err.Error())
}
v := any(nil)
switch typ {
case "Click":
v = Click(intv)
case "Unclick":
v = Unclick(intv)
// case "Rune":
// v = Rune(runev)
case "Hover":
v = Hover{}
case "Press":
v = Press{Key: Key(keyv)}
case "Release":
v = Release{Key: Key(keyv)}
case "Scroll":
v = Scroll(intv)
case "Sweep":
v = Sweep(intv)
// case "Drag":
// v = Drag{mime: value}
case "Drop":
v = Drop{mime: strings.TrimSpace(value)}
default:
panic("unknown type: " + typ)
}
return v
}
// holdable returns true on every type that is meant to be held until
// it's complement value goes to the bottom of trace
func holdable(v any) bool {
switch v.(type) {
// case Drag:
// return true
case Click:
return true
case Press:
return true
}
return false
}
// complement returns for the value of holdable type a value of a type that is complement to it
func complement(v any) any {
switch v := v.(type) {
// case Drag:
// return Drop(v)
// case Drop:
// return Drag(v)
case Click:
return Unclick(v)
case Unclick:
return Click(v)
case Press:
return Release(v)
case Release:
return Press(v)
}
return nil
}