Skip to content

Commit

Permalink
event: Add a converting function from driver.Event to event.Event
Browse files Browse the repository at this point in the history
Update #926
  • Loading branch information
hajimehoshi committed Sep 12, 2019
1 parent 529ab5f commit 695c93e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 121 deletions.
6 changes: 4 additions & 2 deletions event/event.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

208 changes: 104 additions & 104 deletions event/keys.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions event/modifier.go
Expand Up @@ -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
)
43 changes: 38 additions & 5 deletions genevents.go
Expand Up @@ -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",
},
{
Expand Down Expand Up @@ -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}}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions genkeys.go
Expand Up @@ -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}}
)
`
Expand Down
6 changes: 4 additions & 2 deletions internal/driver/event.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 695c93e

Please sign in to comment.