event: Add a converting function from driver.Event to event.Event

Update #926
This commit is contained in:
Hajime Hoshi 2019-09-12 20:59:32 +09:00
parent 529ab5f191
commit 695c93efd6
6 changed files with 158 additions and 121 deletions

View File

@ -16,6 +16,8 @@
package event package event
type Event interface{}
// KeyboardKeyCharacter is an event that occurs when a character is actually typed on the keyboard. This may be provided by an input method. // KeyboardKeyCharacter is an event that occurs when a character is actually typed on the keyboard. This may be provided by an input method.
type KeyboardKeyCharacter struct { type KeyboardKeyCharacter struct {
// Key is the key code of the key typed. // Key is the key code of the key typed.
@ -106,7 +108,7 @@ type MouseMove struct {
X float32 X float32
// Y is the Y position of the mouse pointer. This value is expressed in device independent pixels. // Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.
float32 Y float32
// DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels. // DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels.
DeltaX float32 DeltaX float32

View File

@ -20,110 +20,110 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
type Key int type Key = driver.Key
const ( const (
Key0 Key = Key(driver.Key0) Key0 = driver.Key0
Key1 Key = Key(driver.Key1) Key1 = driver.Key1
Key2 Key = Key(driver.Key2) Key2 = driver.Key2
Key3 Key = Key(driver.Key3) Key3 = driver.Key3
Key4 Key = Key(driver.Key4) Key4 = driver.Key4
Key5 Key = Key(driver.Key5) Key5 = driver.Key5
Key6 Key = Key(driver.Key6) Key6 = driver.Key6
Key7 Key = Key(driver.Key7) Key7 = driver.Key7
Key8 Key = Key(driver.Key8) Key8 = driver.Key8
Key9 Key = Key(driver.Key9) Key9 = driver.Key9
KeyA Key = Key(driver.KeyA) KeyA = driver.KeyA
KeyB Key = Key(driver.KeyB) KeyB = driver.KeyB
KeyC Key = Key(driver.KeyC) KeyC = driver.KeyC
KeyD Key = Key(driver.KeyD) KeyD = driver.KeyD
KeyE Key = Key(driver.KeyE) KeyE = driver.KeyE
KeyF Key = Key(driver.KeyF) KeyF = driver.KeyF
KeyG Key = Key(driver.KeyG) KeyG = driver.KeyG
KeyH Key = Key(driver.KeyH) KeyH = driver.KeyH
KeyI Key = Key(driver.KeyI) KeyI = driver.KeyI
KeyJ Key = Key(driver.KeyJ) KeyJ = driver.KeyJ
KeyK Key = Key(driver.KeyK) KeyK = driver.KeyK
KeyL Key = Key(driver.KeyL) KeyL = driver.KeyL
KeyM Key = Key(driver.KeyM) KeyM = driver.KeyM
KeyN Key = Key(driver.KeyN) KeyN = driver.KeyN
KeyO Key = Key(driver.KeyO) KeyO = driver.KeyO
KeyP Key = Key(driver.KeyP) KeyP = driver.KeyP
KeyQ Key = Key(driver.KeyQ) KeyQ = driver.KeyQ
KeyR Key = Key(driver.KeyR) KeyR = driver.KeyR
KeyS Key = Key(driver.KeyS) KeyS = driver.KeyS
KeyT Key = Key(driver.KeyT) KeyT = driver.KeyT
KeyU Key = Key(driver.KeyU) KeyU = driver.KeyU
KeyV Key = Key(driver.KeyV) KeyV = driver.KeyV
KeyW Key = Key(driver.KeyW) KeyW = driver.KeyW
KeyX Key = Key(driver.KeyX) KeyX = driver.KeyX
KeyY Key = Key(driver.KeyY) KeyY = driver.KeyY
KeyZ Key = Key(driver.KeyZ) KeyZ = driver.KeyZ
KeyApostrophe Key = Key(driver.KeyApostrophe) KeyApostrophe = driver.KeyApostrophe
KeyBackslash Key = Key(driver.KeyBackslash) KeyBackslash = driver.KeyBackslash
KeyBackspace Key = Key(driver.KeyBackspace) KeyBackspace = driver.KeyBackspace
KeyCapsLock Key = Key(driver.KeyCapsLock) KeyCapsLock = driver.KeyCapsLock
KeyComma Key = Key(driver.KeyComma) KeyComma = driver.KeyComma
KeyDelete Key = Key(driver.KeyDelete) KeyDelete = driver.KeyDelete
KeyDown Key = Key(driver.KeyDown) KeyDown = driver.KeyDown
KeyEnd Key = Key(driver.KeyEnd) KeyEnd = driver.KeyEnd
KeyEnter Key = Key(driver.KeyEnter) KeyEnter = driver.KeyEnter
KeyEqual Key = Key(driver.KeyEqual) KeyEqual = driver.KeyEqual
KeyEscape Key = Key(driver.KeyEscape) KeyEscape = driver.KeyEscape
KeyF1 Key = Key(driver.KeyF1) KeyF1 = driver.KeyF1
KeyF2 Key = Key(driver.KeyF2) KeyF2 = driver.KeyF2
KeyF3 Key = Key(driver.KeyF3) KeyF3 = driver.KeyF3
KeyF4 Key = Key(driver.KeyF4) KeyF4 = driver.KeyF4
KeyF5 Key = Key(driver.KeyF5) KeyF5 = driver.KeyF5
KeyF6 Key = Key(driver.KeyF6) KeyF6 = driver.KeyF6
KeyF7 Key = Key(driver.KeyF7) KeyF7 = driver.KeyF7
KeyF8 Key = Key(driver.KeyF8) KeyF8 = driver.KeyF8
KeyF9 Key = Key(driver.KeyF9) KeyF9 = driver.KeyF9
KeyF10 Key = Key(driver.KeyF10) KeyF10 = driver.KeyF10
KeyF11 Key = Key(driver.KeyF11) KeyF11 = driver.KeyF11
KeyF12 Key = Key(driver.KeyF12) KeyF12 = driver.KeyF12
KeyGraveAccent Key = Key(driver.KeyGraveAccent) KeyGraveAccent = driver.KeyGraveAccent
KeyHome Key = Key(driver.KeyHome) KeyHome = driver.KeyHome
KeyInsert Key = Key(driver.KeyInsert) KeyInsert = driver.KeyInsert
KeyKP0 Key = Key(driver.KeyKP0) KeyKP0 = driver.KeyKP0
KeyKP1 Key = Key(driver.KeyKP1) KeyKP1 = driver.KeyKP1
KeyKP2 Key = Key(driver.KeyKP2) KeyKP2 = driver.KeyKP2
KeyKP3 Key = Key(driver.KeyKP3) KeyKP3 = driver.KeyKP3
KeyKP4 Key = Key(driver.KeyKP4) KeyKP4 = driver.KeyKP4
KeyKP5 Key = Key(driver.KeyKP5) KeyKP5 = driver.KeyKP5
KeyKP6 Key = Key(driver.KeyKP6) KeyKP6 = driver.KeyKP6
KeyKP7 Key = Key(driver.KeyKP7) KeyKP7 = driver.KeyKP7
KeyKP8 Key = Key(driver.KeyKP8) KeyKP8 = driver.KeyKP8
KeyKP9 Key = Key(driver.KeyKP9) KeyKP9 = driver.KeyKP9
KeyKPAdd Key = Key(driver.KeyKPAdd) KeyKPAdd = driver.KeyKPAdd
KeyKPDecimal Key = Key(driver.KeyKPDecimal) KeyKPDecimal = driver.KeyKPDecimal
KeyKPDivide Key = Key(driver.KeyKPDivide) KeyKPDivide = driver.KeyKPDivide
KeyKPEnter Key = Key(driver.KeyKPEnter) KeyKPEnter = driver.KeyKPEnter
KeyKPEqual Key = Key(driver.KeyKPEqual) KeyKPEqual = driver.KeyKPEqual
KeyKPMultiply Key = Key(driver.KeyKPMultiply) KeyKPMultiply = driver.KeyKPMultiply
KeyKPSubtract Key = Key(driver.KeyKPSubtract) KeyKPSubtract = driver.KeyKPSubtract
KeyLeft Key = Key(driver.KeyLeft) KeyLeft = driver.KeyLeft
KeyLeftAlt Key = Key(driver.KeyLeftAlt) KeyLeftAlt = driver.KeyLeftAlt
KeyLeftBracket Key = Key(driver.KeyLeftBracket) KeyLeftBracket = driver.KeyLeftBracket
KeyLeftControl Key = Key(driver.KeyLeftControl) KeyLeftControl = driver.KeyLeftControl
KeyLeftShift Key = Key(driver.KeyLeftShift) KeyLeftShift = driver.KeyLeftShift
KeyMenu Key = Key(driver.KeyMenu) KeyMenu = driver.KeyMenu
KeyMinus Key = Key(driver.KeyMinus) KeyMinus = driver.KeyMinus
KeyNumLock Key = Key(driver.KeyNumLock) KeyNumLock = driver.KeyNumLock
KeyPageDown Key = Key(driver.KeyPageDown) KeyPageDown = driver.KeyPageDown
KeyPageUp Key = Key(driver.KeyPageUp) KeyPageUp = driver.KeyPageUp
KeyPause Key = Key(driver.KeyPause) KeyPause = driver.KeyPause
KeyPeriod Key = Key(driver.KeyPeriod) KeyPeriod = driver.KeyPeriod
KeyPrintScreen Key = Key(driver.KeyPrintScreen) KeyPrintScreen = driver.KeyPrintScreen
KeyRight Key = Key(driver.KeyRight) KeyRight = driver.KeyRight
KeyRightAlt Key = Key(driver.KeyRightAlt) KeyRightAlt = driver.KeyRightAlt
KeyRightBracket Key = Key(driver.KeyRightBracket) KeyRightBracket = driver.KeyRightBracket
KeyRightControl Key = Key(driver.KeyRightControl) KeyRightControl = driver.KeyRightControl
KeyRightShift Key = Key(driver.KeyRightShift) KeyRightShift = driver.KeyRightShift
KeyScrollLock Key = Key(driver.KeyScrollLock) KeyScrollLock = driver.KeyScrollLock
KeySemicolon Key = Key(driver.KeySemicolon) KeySemicolon = driver.KeySemicolon
KeySlash Key = Key(driver.KeySlash) KeySlash = driver.KeySlash
KeySpace Key = Key(driver.KeySpace) KeySpace = driver.KeySpace
KeyTab Key = Key(driver.KeyTab) KeyTab = driver.KeyTab
KeyUp Key = Key(driver.KeyUp) KeyUp = driver.KeyUp
) )

View File

@ -19,12 +19,12 @@ import (
) )
// Modifier is a bit set of modifier keys on a keyboard. // Modifier is a bit set of modifier keys on a keyboard.
type Modifier int type Modifier = driver.Modifier
const ( const (
ModifierShift Modifier = Modifier(driver.ModifierShift) ModifierShift = driver.ModifierShift
ModifierControl Modifier = Modifier(driver.ModifierControl) ModifierControl = driver.ModifierControl
ModifierAlt Modifier = Modifier(driver.ModifierAlt) ModifierAlt = driver.ModifierAlt
ModifierCapsLock Modifier = Modifier(driver.ModifierCapsLock) ModifierCapsLock = driver.ModifierCapsLock
ModifierNumLock Modifier = Modifier(driver.ModifierNumLock) ModifierNumLock = driver.ModifierNumLock
) )

View File

@ -325,6 +325,8 @@ var eventTmpl = template.Must(template.New("event.go").Parse(`{{.License}}
package {{.Package}} package {{.Package}}
type Event interface{}
{{range .Events}}// {{.Comment}} {{range .Events}}// {{.Comment}}
type {{.Name}} struct { type {{.Name}} struct {
{{range .Members}} // {{.Comment}} {{range .Members}} // {{.Comment}}
@ -336,10 +338,41 @@ type {{.Name}} struct {
{{end}} {{end}}
`)) `))
var chanTmpl = template.Must(template.New("chan.go").Parse(`{{.License}}
{{.DoNotEdit}}
package {{.Package}}
import (
"fmt"
"github.com/hajimehoshi/ebiten/internal/driver"
)
func convertCh(driverCh chan driver.Event) (chan Event) {
ch := make(chan Event)
go func() {
defer close(ch)
for v := range driverCh {
switch v := v.(type) {
{{range .Events}}case driver.{{.Name}}:
ch <- {{.Name}}(v)
{{end}}default:
panic(fmt.Sprintf("event: unknown event: %v", v))
}
}
}()
return ch
}
`))
func main() { func main() {
for _, path := range []string{ for path, tmpl := range map[string]*template.Template{
filepath.Join("event", "event.go"), filepath.Join("event", "event.go"): eventTmpl,
filepath.Join("internal", "driver", "event.go"), filepath.Join("event", "chan.go"): chanTmpl,
filepath.Join("internal", "driver", "event.go"): eventTmpl,
} { } {
f, err := os.Create(path) f, err := os.Create(path)
if err != nil { if err != nil {
@ -350,7 +383,7 @@ func main() {
tokens := strings.Split(path, string(filepath.Separator)) tokens := strings.Split(path, string(filepath.Separator))
pkg := tokens[len(tokens)-2] pkg := tokens[len(tokens)-2]
if err := eventTmpl.Execute(f, struct { if err := tmpl.Execute(f, struct {
License string License string
DoNotEdit string DoNotEdit string
Package string Package string

View File

@ -317,10 +317,10 @@ import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
type Key int type Key = driver.Key
const ( const (
{{range $index, $name := .DriverKeyNames}}Key{{$name}} Key = Key(driver.Key{{$name}}) {{range $index, $name := .DriverKeyNames}}Key{{$name}} = driver.Key{{$name}}
{{end}} {{end}}
) )
` `

View File

@ -16,6 +16,8 @@
package driver package driver
type Event interface{}
// KeyboardKeyCharacter is an event that occurs when a character is actually typed on the keyboard. This may be provided by an input method. // KeyboardKeyCharacter is an event that occurs when a character is actually typed on the keyboard. This may be provided by an input method.
type KeyboardKeyCharacter struct { type KeyboardKeyCharacter struct {
// Key is the key code of the key typed. // Key is the key code of the key typed.
@ -106,7 +108,7 @@ type MouseMove struct {
X float32 X float32
// Y is the Y position of the mouse pointer. This value is expressed in device independent pixels. // Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.
float32 Y float32
// DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels. // DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels.
DeltaX float32 DeltaX float32