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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-13 20:04:29 +01:00
|
|
|
"bytes"
|
2018-03-14 04:11:11 +01:00
|
|
|
"image"
|
|
|
|
_ "image/png"
|
2016-02-15 17:13:04 +01:00
|
|
|
"log"
|
2018-04-13 21:59:06 +02:00
|
|
|
"strings"
|
2016-02-15 17:13:04 +01:00
|
|
|
|
2023-05-30 04:16:25 +02:00
|
|
|
"github.com/hajimehoshi/bitmapfont/v3"
|
2022-08-13 06:33:52 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/keyboard/keyboard"
|
2020-12-15 03:55:17 +01:00
|
|
|
rkeyboard "github.com/hajimehoshi/ebiten/v2/examples/resources/images/keyboard"
|
2021-04-06 18:48:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2023-11-11 11:04:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
2015-01-06 20:27:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
|
|
|
)
|
|
|
|
|
2023-11-11 11:04:13 +01:00
|
|
|
var fontFace = text.NewStdFace(bitmapfont.Face)
|
|
|
|
|
2015-01-18 13:11:03 +01:00
|
|
|
var keyboardImage *ebiten.Image
|
|
|
|
|
|
|
|
func init() {
|
2020-12-15 03:55:17 +01:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(rkeyboard.Keyboard_png))
|
2015-01-18 13:11:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-13 20:04:29 +01:00
|
|
|
|
2020-10-05 18:03:30 +02:00
|
|
|
keyboardImage = ebiten.NewImageFromImage(img)
|
2015-01-18 13:11:03 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 22:34:06 +02:00
|
|
|
type Game struct {
|
2021-04-06 18:54:47 +02:00
|
|
|
keys []ebiten.Key
|
2020-05-09 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2021-07-10 15:18:13 +02:00
|
|
|
g.keys = inpututil.AppendPressedKeys(g.keys[:0])
|
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)
|
2022-11-01 18:43:42 +01:00
|
|
|
op.ColorScale.Scale(0.5, 0.5, 0.5, 1)
|
2018-01-28 17:35:14 +01:00
|
|
|
screen.DrawImage(keyboardImage, op)
|
|
|
|
|
|
|
|
// Draw the highlighted keys.
|
2017-05-27 19:24:23 +02:00
|
|
|
op = &ebiten.DrawImageOptions{}
|
2021-04-06 18:54:47 +02:00
|
|
|
for _, p := range g.keys {
|
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)
|
2018-10-23 16:27:27 +02:00
|
|
|
screen.DrawImage(keyboardImage.SubImage(r).(*ebiten.Image), op)
|
2015-01-18 13:11:03 +01:00
|
|
|
}
|
|
|
|
|
2022-08-13 06:33:52 +02:00
|
|
|
var keyStrs []string
|
|
|
|
var keyNames []string
|
2022-08-13 06:37:01 +02:00
|
|
|
for _, k := range g.keys {
|
|
|
|
keyStrs = append(keyStrs, k.String())
|
2022-08-13 06:33:52 +02:00
|
|
|
if name := ebiten.KeyName(k); name != "" {
|
|
|
|
keyNames = append(keyNames, name)
|
|
|
|
}
|
2018-04-13 21:59:06 +02:00
|
|
|
}
|
2022-08-13 06:33:52 +02:00
|
|
|
|
|
|
|
// Use bitmapfont.Face instead of ebitenutil.DebugPrint, since some key names might not be printed with DebugPrint.
|
2023-11-11 11:04:13 +01:00
|
|
|
textOp := &text.DrawOptions{}
|
2023-11-16 17:17:29 +01:00
|
|
|
textOp.LineSpacingInPixels = fontFace.Metrics().Height
|
2023-11-11 11:04:13 +01:00
|
|
|
text.Draw(screen, strings.Join(keyStrs, ", ")+"\n"+strings.Join(keyNames, ", "), fontFace, textOp)
|
2020-05-09 22:34:06 +02:00
|
|
|
}
|
2018-04-13 21:59: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)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Keyboard (Ebitengine Demo)")
|
2020-05-09 22:34:06 +02:00
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
2015-01-06 20:27:57 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|