-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_renderer.go
100 lines (85 loc) · 2.62 KB
/
game_renderer.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
package main
import (
"image"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"github.com/zeraye/polygon-editor/pkg/draw"
)
type gameRenderer struct {
raster *canvas.Raster
objects []fyne.CanvasObject
game *Game
}
func (gr *gameRenderer) Destroy() {
}
func (gr *gameRenderer) Layout(size fyne.Size) {
gr.raster.Resize(size)
}
func (gr *gameRenderer) MinSize() fyne.Size {
return fyne.NewSize(float32(gr.game.config.UI.RasterWidth), float32(gr.game.config.UI.RasterHeight))
}
func (gr *gameRenderer) Objects() []fyne.CanvasObject {
return gr.objects
}
func (gr *gameRenderer) Refresh() {
canvas.Refresh(gr.raster)
}
// Draw game raster (polygon editor area, not menus)
func (gr *gameRenderer) Draw(width, height int) image.Image {
img := image.NewRGBA(image.Rect(0, 0, int(gr.game.config.UI.RasterWidth), int(gr.game.config.UI.RasterHeight)))
// draw raster background
for y := 0; y < img.Bounds().Dy(); y++ {
for x := 0; x < img.Bounds().Dx(); x++ {
img.Set(x, y, draw.RGBAToColor(gr.game.config.UI.BackgroundColorRGBA))
}
}
// draw raster border
for x := 0; x < img.Bounds().Dx(); x++ {
img.Set(x, 0, draw.RGBAToColor(gr.game.config.UI.RasterBorderColorRGBA))
img.Set(x, img.Bounds().Dy()-1, draw.RGBAToColor(gr.game.config.UI.RasterBorderColorRGBA))
}
for y := 0; y < img.Bounds().Dx(); y++ {
img.Set(0, y, draw.RGBAToColor(gr.game.config.UI.RasterBorderColorRGBA))
img.Set(img.Bounds().Dx()-1, y, draw.RGBAToColor(gr.game.config.UI.RasterBorderColorRGBA))
}
// draw offset polygon
if len(gr.game.offsetPolygons) > 0 {
for _, poly := range gr.game.offsetPolygons {
draw.DrawPolygon(
*poly,
nil,
draw.RGBAToColor(gr.game.config.UI.PointColorRGBA),
draw.RGBAToColor(gr.game.config.UI.OffsetSegmentColorRGBA),
draw.RGBAToColor(gr.game.config.UI.SelectedSegmentColorRGBA),
gr.game.config.UI.PointRadius,
false,
gr.game.config.UI.FillPoints,
img,
gr.game.config.Miscellaneous.LineDrawAlgorithm,
nil,
)
}
}
// draw polygons
for _, poly := range gr.game.polygons {
draw.DrawPolygon(
*poly,
gr.game.selectedSegment,
draw.RGBAToColor(gr.game.config.UI.PointColorRGBA),
draw.RGBAToColor(gr.game.config.UI.SegmentColorRGBA),
draw.RGBAToColor(gr.game.config.UI.SelectedSegmentColorRGBA),
gr.game.config.UI.PointRadius,
true,
gr.game.config.UI.FillPoints,
img,
// gr.game.config.Miscellaneous.LineDrawAlgorithm,
gr.game.lineAlgorithm,
gr.game.widths,
)
}
// draw constraints
for _, constraint := range gr.game.constraints {
draw.DrawConstraint(*constraint, draw.RGBAToColor(gr.game.config.UI.ConstraintCharColorRGBA), img)
}
return img
}