-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
103 lines (92 loc) · 2.07 KB
/
layer.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
102
103
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
// Layer contains data for layers
type Layer struct {
Hidden bool
Canvas rl.RenderTexture2D
Name string
Width, Height int32
BlendMode rl.BlendMode
// PixelData is the "raw" pixels map
PixelData map[IntVec2]rl.Color
}
// Redraw redraws the layer
func (l *Layer) Redraw() {
rl.BeginTextureMode(l.Canvas)
rl.ClearBackground(rl.Blank)
// rl.BeginBlendMode(l.BlendMode)
for p, color := range l.PixelData {
rl.DrawPixel(p.X, p.Y, color)
}
// rl.EndBlendMode()
rl.EndTextureMode()
}
// Resize the layer to the specified width, height and direction
func (l *Layer) Resize(width, height int32, direction ResizeDirection) {
l.Canvas = rl.LoadRenderTexture(width, height)
w := CurrentFile.CanvasWidth
h := CurrentFile.CanvasHeight
nw := width
nh := height
// offsets
var dx int32
var dy int32
switch CurrentFile.CanvasDirectionResizePreview {
case ResizeTL:
dx = 0
dy = 0
case ResizeTC:
dx = (w - nw) / 2
dy = 0
case ResizeTR:
dx = w - nw
dy = 0
case ResizeCL:
dx = 0
dy = (h - nh) / 2
case ResizeCC:
dx = (w - nw) / 2
dy = (h - nh) / 2
case ResizeCR:
dx = w - nw
dy = (h - nh) / 2
case ResizeBL:
dx = 0
dy = h - nh
case ResizeBC:
dx = (w - nw) / 2
dy = h - nh
case ResizeBR:
dx = w - nw
dy = h - nh
}
newPixelData := make(map[IntVec2]rl.Color)
rl.BeginTextureMode(l.Canvas)
rl.ClearBackground(rl.Blank)
for x := dx; x < w; x++ {
for y := dy; y < h; y++ {
if color, ok := l.PixelData[IntVec2{x, y}]; ok {
rl.DrawPixel(x-dx, y-dy, color)
newPixelData[IntVec2{x - dx, y - dy}] = color
}
}
}
rl.EndTextureMode()
l.PixelData = newPixelData
l.Width = width
l.Height = height
}
// NewLayer returns a pointer to a new Layer
func NewLayer(width, height int32, name string, fillColor rl.Color, shouldFill bool) *Layer {
return &Layer{
Canvas: rl.LoadRenderTexture(width, height),
PixelData: make(map[IntVec2]rl.Color),
Name: name,
Hidden: false,
Width: width,
Height: height,
BlendMode: rl.BlendAlpha,
}
}