Skip to content

Commit

Permalink
ebiten: add Key.MarshalText and Key.UnmarshalText
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Jun 23, 2022
1 parent 7f26562 commit a4d782d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions genkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ const ebitenKeysTmpl = `{{.License}}
package ebiten
import (
"fmt"
"strings"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
Expand Down Expand Up @@ -522,6 +523,21 @@ func keyNameToKeyCode(name string) (Key, bool) {
{{end}}}
return 0, false
}
// MarshalText implements encoding.TextMarshaler.
func (k Key) MarshalText() ([]byte, error) {
return []byte(k.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler
func (k *Key) UnmarshalText(text []byte) error {
key, ok := keyNameToKeyCode(string(text))
if !ok {
return fmt.Errorf("ebiten: unexpected key name: %s", string(text))
}
*k = key
return nil
}
`

const uiKeysTmpl = `{{.License}}
Expand Down
16 changes: 16 additions & 0 deletions keys.go

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

0 comments on commit a4d782d

Please sign in to comment.