Debian Codegraph @

vendor/github.com/xyproto/gfx/layer.go

raw
1 package gfx
2
3 import (
4 "image"
5 "image/color"
6 )
7
8 // Layer represents a layer of paletted tiles.
9 type Layer struct {
10 Tileset *Tileset
11 Width int // Width of the layer in number of tiles.
12 Data LayerData
13 }
14
15 // LayerData is the data for a layer.
16 type LayerData []int
17
18 // Size returns the size of the layer data given the number of columns.
19 func (ld LayerData) Size(cols int) image.Point {
20 l := len(ld)
21
22 if l < cols {
23 return Pt(cols, 1)
24 }
25
26 rows := l / cols
27
28 if rows*cols == l {
29 return Pt(cols, rows)
30 }
31
32 if rows%cols > 0 {
33 rows++
34 }
35
36 return Pt(cols, rows)
37 }
38
39 // NewLayer creates a new layer.
40 func NewLayer(tileset *Tileset, width int, data LayerData) *Layer {
41 return &Layer{Tileset: tileset, Width: width, Data: data}
42 }
43
44 // At returns the color at (x, y).
45 func (l *Layer) At(x, y int) color.Color {
46 return l.NRGBAAt(x, y)
47 }
48
49 // NRGBAAt returns the color.RGBA at (x, y).
50 func (l *Layer) NRGBAAt(x, y int) color.NRGBA {
51 if i := l.TileIndexAt(x, y); i > -1 {
52 s := l.Tileset.Size
53
54 return l.Tileset.Tiles[i].NRGBAAt(x%s.X, y%s.Y)
55 }
56
57 return ColorTransparent
58 }
59
60 // AlphaAt returns the alpha value at (x, y).
61 func (l *Layer) AlphaAt(x, y int) uint8 {
62 if i := l.TileIndexAt(x, y); i > -1 {
63 tx, ty := x%l.Tileset.Size.X, y%l.Tileset.Size.Y
64 return l.Tileset.Tiles[i].AlphaAt(tx, ty)
65 }
66
67 return 0
68 }
69
70 // Bounds returns the bounds of the paletted layer.
71 func (l *Layer) Bounds() image.Rectangle {
72 lpix := len(l.Data)
73
74 switch {
75 case l.Width < 1, lpix == 0,
76 l.Tileset == nil,
77 l.Tileset.Size.X < 1, l.Tileset.Size.Y < 1:
78 return ZR
79 case lpix < l.Width:
80 return IR(0, 0, l.Width, 1)
81 }
82
83 s := l.Data.Size(l.Width)
84
85 w := s.X * l.Tileset.Size.X
86 h := s.Y * l.Tileset.Size.Y
87
88 return IR(0, 0, w, h)
89 }
90
91 // ColorModel returns the color model for the paletted layer.
92 func (l *Layer) ColorModel() color.Model {
93 return color.RGBAModel
94 }
95
96 // ColorIndexAt returns the palette index of the pixel at (x, y).
97 func (l *Layer) ColorIndexAt(x, y int) uint8 {
98 if t := l.TileAt(x, y); t != nil {
99 ts := l.Tileset.Size
100 return t.ColorIndexAt(x%ts.X, y%ts.Y)
101 }
102
103 return 0
104 }
105
106 // TileAt returns the tile image at (x, y).
107 func (l *Layer) TileAt(x, y int) image.PalettedImage {
108 if i := l.TileIndexAt(x, y); i >= 0 && i < len(l.Tileset.Tiles) {
109 return l.Tileset.Tiles[i]
110 }
111
112 return nil
113 }
114
115 // TileSize returns the tileset tile size.
116 func (l *Layer) TileSize() image.Point {
117 return l.Tileset.Size
118 }
119
120 // GfxPalette retrieves the layer palette.
121 func (l *Layer) GfxPalette() Palette {
122 return l.Tileset.Palette
123 }
124
125 // ColorPalette retrieves the layer palette.
126 func (l *Layer) ColorPalette() color.Palette {
127 return l.Tileset.Palette.AsColorPalette()
128 }
129
130 // Index returns the tile index at (x, y). (Short for TileIndexAt)
131 func (l *Layer) Index(x, y int) int {
132 return l.TileIndexAt(x, y)
133 }
134
135 // TileIndexAt returns the tile index at (x, y).
136 func (l *Layer) TileIndexAt(x, y int) int {
137 s := l.Tileset.Size
138 o := y/s.Y*l.Width + x/s.X
139
140 if o >= 0 && o < len(l.Data) {
141 return l.Data[o]
142 }
143
144 return -1
145 }
146
147 // DataAt returns the data at (dx, dy).
148 func (l *Layer) DataAt(dx, dy int) int {
149 return l.Data[l.dataOffset(dx, dy)]
150 }
151
152 // Put changes the tile index at (dx, dy). (Short for SetTileIndex)
153 func (l *Layer) Put(dx, dy, index int) {
154 l.SetTileIndex(dx, dy, index)
155 }
156
157 // SetTileIndex changes the tile index at (dx, dy).
158 func (l *Layer) SetTileIndex(dx, dy, index int) {
159 if o := l.dataOffset(dx, dy); o >= 0 && o < len(l.Data) {
160 l.Data[o] = index
161 }
162 }
163
164 func (l *Layer) dataOffset(dx, dy int) int {
165 return dy*l.Width + dx
166 }