-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_cell.go
101 lines (80 loc) · 2.1 KB
/
board_cell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
)
var (
Black = color.Gray16{0x5500}
)
type BoardCell struct {
Scene
// Piece holds the piece that currently occupies the cell.
Piece *Piece
// Color of the cell when rendered
Color color.Color
// The position of the cell on the board
Row, Column int
// The drawing target shape
Image *ebiten.Image
// The cell dimentions
Size int
Hidden bool
opts ebiten.DrawImageOptions
Board *Board
}
func NewBoardCell(id int, row, column int, size int) *BoardCell {
return &BoardCell{
Scene: Scene{
ID: fmt.Sprintf("cell_%d", id),
},
Row: row,
Column: column,
Image: ebiten.NewImage(size, size),
Color: color.RGBA{0xbb, 0xad, 0xa0, 0xff},
Size: size,
Hidden: true,
}
}
func (c *BoardCell) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
opts.GeoM.Translate(
float64((c.Size+14)*c.Column),
float64((c.Size+14)*c.Row),
)
// Save the options in order for us to use them later and
// translate the image position back to screenspace.
c.opts = opts
if !c.Hidden {
c.Image.Fill(c.Color)
screen.DrawImage(c.Image, &opts)
}
c.Scene.Draw(screen, opts)
}
func (c *BoardCell) Update() error {
if c.Piece != nil && c.Board.Selected == nil && ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
// Grab the cursor absolute position.
mx, my := ebiten.CursorPosition()
point := image.Pt(mx, my)
// Translate the cell position to screenspace (absolute).
x0, y0 := c.opts.GeoM.Apply(
float64(c.Image.Bounds().Min.X),
float64(c.Image.Bounds().Min.Y),
)
x1, y1 := c.opts.GeoM.Apply(
float64(c.Image.Bounds().Max.X),
float64(c.Image.Bounds().Max.X),
)
bbox := image.Rect(int(x0), int(y0), int(x1), int(y1))
if point.In(bbox) {
c.SetDrawOrder(99)
c.Piece.Dragged = true
c.Board.Selected = c.Piece
fmt.Printf("Clicked cell %s => %s\n", c.ID, point)
}
} else if c.Piece != nil && c.Piece.Dragged && inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
c.SetDrawOrder(0)
c.Piece.Dragged = false
c.Board.Selected = nil
}
return c.Scene.Update()
}