2013-07-02 18:27:04 +02:00
|
|
|
// Copyright 2013 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
2013-07-04 16:57:53 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/hajimehoshi/go.ebiten"
|
2013-07-02 18:27:04 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/graphics"
|
2013-07-04 16:57:53 +02:00
|
|
|
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"os"
|
2013-07-02 18:27:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Input struct {
|
2013-07-04 16:57:53 +02:00
|
|
|
textTexture graphics.Texture
|
|
|
|
inputState ebiten.InputState
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Input {
|
|
|
|
return &Input{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Input) Init(tf graphics.TextureFactory) {
|
2013-07-04 16:57:53 +02:00
|
|
|
file, err := os.Open("images/text.png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if game.textTexture, err = tf.NewTextureFromImage(img); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 14:02:17 +02:00
|
|
|
func (game *Input) Update(context ebiten.GameContext) {
|
|
|
|
game.inputState = context.InputState()
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-04 16:57:53 +02:00
|
|
|
func (game *Input) Draw(g graphics.Context) {
|
|
|
|
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
|
|
|
|
str := fmt.Sprintf(`Input State:
|
|
|
|
X: %d
|
|
|
|
Y: %d`, game.inputState.X, game.inputState.Y)
|
|
|
|
game.drawText(g, str, 5, 5)
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
2013-07-04 16:57:53 +02:00
|
|
|
func (game *Input) drawText(g graphics.Context, text string, x, y int) {
|
|
|
|
const letterWidth = 6
|
|
|
|
const letterHeight = 16
|
|
|
|
|
|
|
|
parts := []graphics.TexturePart{}
|
|
|
|
textX := 0
|
|
|
|
textY := 0
|
|
|
|
for _, c := range text {
|
|
|
|
if c == '\n' {
|
|
|
|
textX = 0
|
|
|
|
textY += letterHeight
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
code := int(c)
|
|
|
|
x := (code % 32) * letterWidth
|
|
|
|
y := (code / 32) * letterHeight
|
|
|
|
source := graphics.Rect{x, y, letterWidth, letterHeight}
|
|
|
|
parts = append(parts, graphics.TexturePart{
|
|
|
|
LocationX: textX,
|
|
|
|
LocationY: textY,
|
|
|
|
Source: source,
|
|
|
|
})
|
|
|
|
textX += letterWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
geometryMatrix := matrix.IdentityGeometry()
|
|
|
|
geometryMatrix.Translate(float64(x), float64(y))
|
|
|
|
colorMatrix := matrix.IdentityColor()
|
2013-07-12 18:36:01 +02:00
|
|
|
g.DrawTextureParts(game.textTexture.ID(), parts,
|
2013-07-04 16:57:53 +02:00
|
|
|
geometryMatrix, colorMatrix)
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|