ebiten/examples/keyboard/main.go

105 lines
2.5 KiB
Go
Raw Normal View History

2015-01-06 20:27:57 +01:00
// Copyright 2015 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build example jsgo
2015-01-06 20:27:57 +01:00
package main
import (
"bytes"
"image"
_ "image/png"
2016-02-15 17:13:04 +01:00
"log"
"strings"
2016-02-15 17:13:04 +01:00
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/keyboard/keyboard"
rkeyabord "github.com/hajimehoshi/ebiten/v2/examples/resources/images/keyboard"
2015-01-06 20:27:57 +01:00
)
const (
screenWidth = 320
screenHeight = 240
)
2015-01-18 13:11:03 +01:00
var keyboardImage *ebiten.Image
func init() {
img, _, err := image.Decode(bytes.NewReader(rkeyabord.Keyboard_png))
2015-01-18 13:11:03 +01:00
if err != nil {
log.Fatal(err)
}
keyboardImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
2015-01-18 13:11:03 +01:00
}
2020-05-09 22:34:06 +02:00
type Game struct {
pressed []ebiten.Key
}
func (g *Game) Update(screen *ebiten.Image) error {
g.pressed = nil
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
if ebiten.IsKeyPressed(k) {
2020-05-09 22:34:06 +02:00
g.pressed = append(g.pressed, k)
2015-01-06 20:27:57 +01:00
}
}
2020-05-09 22:34:06 +02:00
return nil
}
2015-01-18 13:11:03 +01:00
2020-05-09 22:34:06 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
2018-01-28 17:35:14 +01:00
const (
offsetX = 24
offsetY = 40
)
// Draw the base (grayed) keyboard image.
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(offsetX, offsetY)
op.ColorM.Scale(0.5, 0.5, 0.5, 1)
screen.DrawImage(keyboardImage, op)
// Draw the highlighted keys.
2017-05-27 19:24:23 +02:00
op = &ebiten.DrawImageOptions{}
2020-05-09 22:34:06 +02:00
for _, p := range g.pressed {
2017-05-27 19:24:23 +02:00
op.GeoM.Reset()
r, ok := keyboard.KeyRect(p)
if !ok {
continue
}
op.GeoM.Translate(float64(r.Min.X), float64(r.Min.Y))
op.GeoM.Translate(offsetX, offsetY)
screen.DrawImage(keyboardImage.SubImage(r).(*ebiten.Image), op)
2015-01-18 13:11:03 +01:00
}
keyStrs := []string{}
2020-05-09 22:34:06 +02:00
for _, p := range g.pressed {
keyStrs = append(keyStrs, p.String())
}
ebitenutil.DebugPrint(screen, strings.Join(keyStrs, ", "))
2020-05-09 22:34:06 +02:00
}
2020-05-09 22:34:06 +02:00
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
2015-01-06 20:27:57 +01:00
}
func main() {
2020-05-09 22:34:06 +02:00
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Keyboard (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
2015-01-06 20:27:57 +01:00
log.Fatal(err)
}
}