From 695c93efd617821f79462557272cbaa1a6733927 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 12 Sep 2019 20:59:32 +0900 Subject: [PATCH] event: Add a converting function from driver.Event to event.Event Update #926 --- event/event.go | 6 +- event/keys.go | 208 +++++++++++++++++++-------------------- event/modifier.go | 12 +-- genevents.go | 43 +++++++- genkeys.go | 4 +- internal/driver/event.go | 6 +- 6 files changed, 158 insertions(+), 121 deletions(-) diff --git a/event/event.go b/event/event.go index a544920c6..c8929fc63 100644 --- a/event/event.go +++ b/event/event.go @@ -16,6 +16,8 @@ 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. type KeyboardKeyCharacter struct { // Key is the key code of the key typed. @@ -105,8 +107,8 @@ type MouseMove struct { // X is the X position of the mouse pointer. This value is expressed in device independent pixels. X float32 - // Y is the Y position of the mouse pointer. This value is expressed in device independent pixels. - float32 + // Y is the Y position of the mouse pointer. This value is expressed in device independent pixels. + Y float32 // DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels. DeltaX float32 diff --git a/event/keys.go b/event/keys.go index 4d6687a58..18dfc250c 100644 --- a/event/keys.go +++ b/event/keys.go @@ -20,110 +20,110 @@ import ( "github.com/hajimehoshi/ebiten/internal/driver" ) -type Key int +type Key = driver.Key const ( - Key0 Key = Key(driver.Key0) - Key1 Key = Key(driver.Key1) - Key2 Key = Key(driver.Key2) - Key3 Key = Key(driver.Key3) - Key4 Key = Key(driver.Key4) - Key5 Key = Key(driver.Key5) - Key6 Key = Key(driver.Key6) - Key7 Key = Key(driver.Key7) - Key8 Key = Key(driver.Key8) - Key9 Key = Key(driver.Key9) - KeyA Key = Key(driver.KeyA) - KeyB Key = Key(driver.KeyB) - KeyC Key = Key(driver.KeyC) - KeyD Key = Key(driver.KeyD) - KeyE Key = Key(driver.KeyE) - KeyF Key = Key(driver.KeyF) - KeyG Key = Key(driver.KeyG) - KeyH Key = Key(driver.KeyH) - KeyI Key = Key(driver.KeyI) - KeyJ Key = Key(driver.KeyJ) - KeyK Key = Key(driver.KeyK) - KeyL Key = Key(driver.KeyL) - KeyM Key = Key(driver.KeyM) - KeyN Key = Key(driver.KeyN) - KeyO Key = Key(driver.KeyO) - KeyP Key = Key(driver.KeyP) - KeyQ Key = Key(driver.KeyQ) - KeyR Key = Key(driver.KeyR) - KeyS Key = Key(driver.KeyS) - KeyT Key = Key(driver.KeyT) - KeyU Key = Key(driver.KeyU) - KeyV Key = Key(driver.KeyV) - KeyW Key = Key(driver.KeyW) - KeyX Key = Key(driver.KeyX) - KeyY Key = Key(driver.KeyY) - KeyZ Key = Key(driver.KeyZ) - KeyApostrophe Key = Key(driver.KeyApostrophe) - KeyBackslash Key = Key(driver.KeyBackslash) - KeyBackspace Key = Key(driver.KeyBackspace) - KeyCapsLock Key = Key(driver.KeyCapsLock) - KeyComma Key = Key(driver.KeyComma) - KeyDelete Key = Key(driver.KeyDelete) - KeyDown Key = Key(driver.KeyDown) - KeyEnd Key = Key(driver.KeyEnd) - KeyEnter Key = Key(driver.KeyEnter) - KeyEqual Key = Key(driver.KeyEqual) - KeyEscape Key = Key(driver.KeyEscape) - KeyF1 Key = Key(driver.KeyF1) - KeyF2 Key = Key(driver.KeyF2) - KeyF3 Key = Key(driver.KeyF3) - KeyF4 Key = Key(driver.KeyF4) - KeyF5 Key = Key(driver.KeyF5) - KeyF6 Key = Key(driver.KeyF6) - KeyF7 Key = Key(driver.KeyF7) - KeyF8 Key = Key(driver.KeyF8) - KeyF9 Key = Key(driver.KeyF9) - KeyF10 Key = Key(driver.KeyF10) - KeyF11 Key = Key(driver.KeyF11) - KeyF12 Key = Key(driver.KeyF12) - KeyGraveAccent Key = Key(driver.KeyGraveAccent) - KeyHome Key = Key(driver.KeyHome) - KeyInsert Key = Key(driver.KeyInsert) - KeyKP0 Key = Key(driver.KeyKP0) - KeyKP1 Key = Key(driver.KeyKP1) - KeyKP2 Key = Key(driver.KeyKP2) - KeyKP3 Key = Key(driver.KeyKP3) - KeyKP4 Key = Key(driver.KeyKP4) - KeyKP5 Key = Key(driver.KeyKP5) - KeyKP6 Key = Key(driver.KeyKP6) - KeyKP7 Key = Key(driver.KeyKP7) - KeyKP8 Key = Key(driver.KeyKP8) - KeyKP9 Key = Key(driver.KeyKP9) - KeyKPAdd Key = Key(driver.KeyKPAdd) - KeyKPDecimal Key = Key(driver.KeyKPDecimal) - KeyKPDivide Key = Key(driver.KeyKPDivide) - KeyKPEnter Key = Key(driver.KeyKPEnter) - KeyKPEqual Key = Key(driver.KeyKPEqual) - KeyKPMultiply Key = Key(driver.KeyKPMultiply) - KeyKPSubtract Key = Key(driver.KeyKPSubtract) - KeyLeft Key = Key(driver.KeyLeft) - KeyLeftAlt Key = Key(driver.KeyLeftAlt) - KeyLeftBracket Key = Key(driver.KeyLeftBracket) - KeyLeftControl Key = Key(driver.KeyLeftControl) - KeyLeftShift Key = Key(driver.KeyLeftShift) - KeyMenu Key = Key(driver.KeyMenu) - KeyMinus Key = Key(driver.KeyMinus) - KeyNumLock Key = Key(driver.KeyNumLock) - KeyPageDown Key = Key(driver.KeyPageDown) - KeyPageUp Key = Key(driver.KeyPageUp) - KeyPause Key = Key(driver.KeyPause) - KeyPeriod Key = Key(driver.KeyPeriod) - KeyPrintScreen Key = Key(driver.KeyPrintScreen) - KeyRight Key = Key(driver.KeyRight) - KeyRightAlt Key = Key(driver.KeyRightAlt) - KeyRightBracket Key = Key(driver.KeyRightBracket) - KeyRightControl Key = Key(driver.KeyRightControl) - KeyRightShift Key = Key(driver.KeyRightShift) - KeyScrollLock Key = Key(driver.KeyScrollLock) - KeySemicolon Key = Key(driver.KeySemicolon) - KeySlash Key = Key(driver.KeySlash) - KeySpace Key = Key(driver.KeySpace) - KeyTab Key = Key(driver.KeyTab) - KeyUp Key = Key(driver.KeyUp) + Key0 = driver.Key0 + Key1 = driver.Key1 + Key2 = driver.Key2 + Key3 = driver.Key3 + Key4 = driver.Key4 + Key5 = driver.Key5 + Key6 = driver.Key6 + Key7 = driver.Key7 + Key8 = driver.Key8 + Key9 = driver.Key9 + KeyA = driver.KeyA + KeyB = driver.KeyB + KeyC = driver.KeyC + KeyD = driver.KeyD + KeyE = driver.KeyE + KeyF = driver.KeyF + KeyG = driver.KeyG + KeyH = driver.KeyH + KeyI = driver.KeyI + KeyJ = driver.KeyJ + KeyK = driver.KeyK + KeyL = driver.KeyL + KeyM = driver.KeyM + KeyN = driver.KeyN + KeyO = driver.KeyO + KeyP = driver.KeyP + KeyQ = driver.KeyQ + KeyR = driver.KeyR + KeyS = driver.KeyS + KeyT = driver.KeyT + KeyU = driver.KeyU + KeyV = driver.KeyV + KeyW = driver.KeyW + KeyX = driver.KeyX + KeyY = driver.KeyY + KeyZ = driver.KeyZ + KeyApostrophe = driver.KeyApostrophe + KeyBackslash = driver.KeyBackslash + KeyBackspace = driver.KeyBackspace + KeyCapsLock = driver.KeyCapsLock + KeyComma = driver.KeyComma + KeyDelete = driver.KeyDelete + KeyDown = driver.KeyDown + KeyEnd = driver.KeyEnd + KeyEnter = driver.KeyEnter + KeyEqual = driver.KeyEqual + KeyEscape = driver.KeyEscape + KeyF1 = driver.KeyF1 + KeyF2 = driver.KeyF2 + KeyF3 = driver.KeyF3 + KeyF4 = driver.KeyF4 + KeyF5 = driver.KeyF5 + KeyF6 = driver.KeyF6 + KeyF7 = driver.KeyF7 + KeyF8 = driver.KeyF8 + KeyF9 = driver.KeyF9 + KeyF10 = driver.KeyF10 + KeyF11 = driver.KeyF11 + KeyF12 = driver.KeyF12 + KeyGraveAccent = driver.KeyGraveAccent + KeyHome = driver.KeyHome + KeyInsert = driver.KeyInsert + KeyKP0 = driver.KeyKP0 + KeyKP1 = driver.KeyKP1 + KeyKP2 = driver.KeyKP2 + KeyKP3 = driver.KeyKP3 + KeyKP4 = driver.KeyKP4 + KeyKP5 = driver.KeyKP5 + KeyKP6 = driver.KeyKP6 + KeyKP7 = driver.KeyKP7 + KeyKP8 = driver.KeyKP8 + KeyKP9 = driver.KeyKP9 + KeyKPAdd = driver.KeyKPAdd + KeyKPDecimal = driver.KeyKPDecimal + KeyKPDivide = driver.KeyKPDivide + KeyKPEnter = driver.KeyKPEnter + KeyKPEqual = driver.KeyKPEqual + KeyKPMultiply = driver.KeyKPMultiply + KeyKPSubtract = driver.KeyKPSubtract + KeyLeft = driver.KeyLeft + KeyLeftAlt = driver.KeyLeftAlt + KeyLeftBracket = driver.KeyLeftBracket + KeyLeftControl = driver.KeyLeftControl + KeyLeftShift = driver.KeyLeftShift + KeyMenu = driver.KeyMenu + KeyMinus = driver.KeyMinus + KeyNumLock = driver.KeyNumLock + KeyPageDown = driver.KeyPageDown + KeyPageUp = driver.KeyPageUp + KeyPause = driver.KeyPause + KeyPeriod = driver.KeyPeriod + KeyPrintScreen = driver.KeyPrintScreen + KeyRight = driver.KeyRight + KeyRightAlt = driver.KeyRightAlt + KeyRightBracket = driver.KeyRightBracket + KeyRightControl = driver.KeyRightControl + KeyRightShift = driver.KeyRightShift + KeyScrollLock = driver.KeyScrollLock + KeySemicolon = driver.KeySemicolon + KeySlash = driver.KeySlash + KeySpace = driver.KeySpace + KeyTab = driver.KeyTab + KeyUp = driver.KeyUp ) diff --git a/event/modifier.go b/event/modifier.go index b44c14d85..604e9d6da 100644 --- a/event/modifier.go +++ b/event/modifier.go @@ -19,12 +19,12 @@ import ( ) // Modifier is a bit set of modifier keys on a keyboard. -type Modifier int +type Modifier = driver.Modifier const ( - ModifierShift Modifier = Modifier(driver.ModifierShift) - ModifierControl Modifier = Modifier(driver.ModifierControl) - ModifierAlt Modifier = Modifier(driver.ModifierAlt) - ModifierCapsLock Modifier = Modifier(driver.ModifierCapsLock) - ModifierNumLock Modifier = Modifier(driver.ModifierNumLock) + ModifierShift = driver.ModifierShift + ModifierControl = driver.ModifierControl + ModifierAlt = driver.ModifierAlt + ModifierCapsLock = driver.ModifierCapsLock + ModifierNumLock = driver.ModifierNumLock ) diff --git a/genevents.go b/genevents.go index 8857a8a8e..bfe2c52cd 100644 --- a/genevents.go +++ b/genevents.go @@ -211,7 +211,7 @@ var ( Type: "float32", }, { - Comment: " Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.", + Comment: "Y is the Y position of the mouse pointer. This value is expressed in device independent pixels.", Type: "float32", }, { @@ -325,6 +325,8 @@ var eventTmpl = template.Must(template.New("event.go").Parse(`{{.License}} package {{.Package}} +type Event interface{} + {{range .Events}}// {{.Comment}} type {{.Name}} struct { {{range .Members}} // {{.Comment}} @@ -336,10 +338,41 @@ type {{.Name}} struct { {{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() { - for _, path := range []string{ - filepath.Join("event", "event.go"), - filepath.Join("internal", "driver", "event.go"), + for path, tmpl := range map[string]*template.Template{ + filepath.Join("event", "event.go"): eventTmpl, + filepath.Join("event", "chan.go"): chanTmpl, + filepath.Join("internal", "driver", "event.go"): eventTmpl, } { f, err := os.Create(path) if err != nil { @@ -350,7 +383,7 @@ func main() { tokens := strings.Split(path, string(filepath.Separator)) pkg := tokens[len(tokens)-2] - if err := eventTmpl.Execute(f, struct { + if err := tmpl.Execute(f, struct { License string DoNotEdit string Package string diff --git a/genkeys.go b/genkeys.go index 716b46130..e28e7e6eb 100644 --- a/genkeys.go +++ b/genkeys.go @@ -317,10 +317,10 @@ import ( "github.com/hajimehoshi/ebiten/internal/driver" ) -type Key int +type Key = driver.Key const ( -{{range $index, $name := .DriverKeyNames}}Key{{$name}} Key = Key(driver.Key{{$name}}) +{{range $index, $name := .DriverKeyNames}}Key{{$name}} = driver.Key{{$name}} {{end}} ) ` diff --git a/internal/driver/event.go b/internal/driver/event.go index eae876acc..1aefa37ef 100644 --- a/internal/driver/event.go +++ b/internal/driver/event.go @@ -16,6 +16,8 @@ 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. type KeyboardKeyCharacter struct { // Key is the key code of the key typed. @@ -105,8 +107,8 @@ type MouseMove struct { // X is the X position of the mouse pointer. This value is expressed in device independent pixels. X float32 - // Y is the Y position of the mouse pointer. This value is expressed in device independent pixels. - float32 + // Y is the Y position of the mouse pointer. This value is expressed in device independent pixels. + Y float32 // DeltaX is the change in X since the last MouseMove event. This value is expressed in device independent pixels. DeltaX float32