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.
|
|
|
|
|
2018-03-14 17:26:21 +01:00
|
|
|
// +build example jsgo
|
2016-08-25 17:40:39 +02:00
|
|
|
|
2015-01-06 20:27:57 +01:00
|
|
|
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
|
|
|
|
2015-01-06 20:27:57 +01:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2018-04-13 21:59:06 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2016-02-10 19:07:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/examples/keyboard/keyboard"
|
2018-03-13 20:04:29 +01:00
|
|
|
rkeyabord "github.com/hajimehoshi/ebiten/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() {
|
2018-03-14 04:11:11 +01:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(rkeyabord.Keyboard_png))
|
2015-01-18 13:11:03 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-13 20:04:29 +01:00
|
|
|
|
|
|
|
keyboardImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
2015-01-18 13:11:03 +01:00
|
|
|
}
|
|
|
|
|
2015-01-06 20:27:57 +01:00
|
|
|
func update(screen *ebiten.Image) error {
|
2018-04-13 21:48:07 +02:00
|
|
|
pressed := []ebiten.Key{}
|
|
|
|
for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ {
|
|
|
|
if ebiten.IsKeyPressed(k) {
|
|
|
|
pressed = append(pressed, k)
|
2015-01-06 20:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-18 13:11:03 +01:00
|
|
|
|
2018-07-09 17:36:43 +02:00
|
|
|
if ebiten.IsDrawingSkipped() {
|
2018-01-28 17:35:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
|
|
|
for _, p := range pressed {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-13 21:59:06 +02:00
|
|
|
keyStrs := []string{}
|
|
|
|
for _, p := range pressed {
|
|
|
|
keyStrs = append(keyStrs, p.String())
|
|
|
|
}
|
|
|
|
ebitenutil.DebugPrint(screen, strings.Join(keyStrs, ", "))
|
|
|
|
|
2015-01-06 20:27:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Keyboard (Ebiten Demo)"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|