/
renderer.go
246 lines (198 loc) · 6.35 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package sgmrender
import (
"embed"
"fmt"
"log"
"github.com/beevik/etree"
"github.com/myaut/stellaris-galaxy-map/pkg/sgm"
"github.com/myaut/stellaris-galaxy-map/pkg/sgmmath"
)
const (
mapTitle = "Galaxy Map"
footerText = "Made with Stellaris Galaxy Map"
canvasPadding = 40
canvasSize = 2160
fontSize = 2.0
maxCellSize = 48.0
countryBorderGap = 1.2
iconSizeSm = 4.0
iconStepSm = 3.6
iconSizeMd = 5.6
iconStepMd = 4.8
svgIconSize = 16
gridSplit = 16
)
const (
traceFlagStarIndex = 1 << iota
traceFlagCountrySegments
traceFlagShowGraphEdges
traceFlags = 0
)
//go:embed icons/*.svg
var iconsFS embed.FS
type RenderOptions struct {
NoGrid bool `json:"no_grid"`
NoInsignificantStars bool `json:"no_insignificant_stars"`
NoStarSystems bool `json:"no_star_systems"`
NoHyperLanes bool `json:"no_hyperlanes"`
NoFleets bool `json:"no_fleets"`
ShowSectors bool `json:"show_sectors"`
}
type Renderer struct {
state *sgm.GameState
opts RenderOptions
doc *etree.Document
canvas *etree.Element
defs *etree.Element
bounds sgmmath.BoundingRect
innerBounds sgmmath.BoundingRect
starGeoIndex StarGeoIndex
iconCache map[string]*etree.Document
}
func NewRenderer(state *sgm.GameState, opts RenderOptions) *Renderer {
r := &Renderer{state: state, opts: opts}
r.computeBounds()
r.buildStarIndex()
r.doc = etree.NewDocument()
r.doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
w, h := r.bounds.Size()
r.canvas = r.doc.CreateElement("svg")
r.canvas.CreateAttr("xmlns", "http://www.w3.org/2000/svg")
r.canvas.CreateAttr("width", fmt.Sprint(w/h*canvasSize))
r.canvas.CreateAttr("height", fmt.Sprint(canvasSize))
r.canvas.CreateAttr("viewBox",
fmt.Sprintf("%f %f %f %f",
r.bounds.Min.X, r.bounds.Min.Y, w, h))
r.defs = r.canvas.CreateElement("defs")
r.iconCache = make(map[string]*etree.Document)
return r
}
func (r *Renderer) getIcon(iconFile string) *etree.Document {
icon, ok := r.iconCache[iconFile]
if ok {
return icon
}
defer func() { r.iconCache[iconFile] = icon }()
buf, err := iconsFS.ReadFile("icons/" + iconFile + ".svg")
if err != nil {
log.Printf("error reading icon %s: %s", iconFile, err.Error())
return nil
}
icon = etree.NewDocument()
err = icon.ReadFromBytes(buf)
if err != nil {
log.Printf("error parsing icon %s: %s", iconFile, err.Error())
return nil
}
// Remove some inkscape junk
svg := icon.Root()
svg.RemoveChild(svg.FindElement("defs"))
svg.RemoveChild(svg.FindElement("metadata"))
return icon
}
func (r *Renderer) computeBounds() {
for _, star := range r.state.Stars {
r.bounds.Add(star.Point())
}
r.innerBounds = r.bounds
r.bounds.Expand(canvasPadding)
}
func (r *Renderer) Render() {
r.createRect(r.canvas, backgroundStyle, r.bounds)
countries := r.renderCountries()
r.renderGrid()
r.renderHyperlanes()
significantStars := r.renderStars()
r.renderCountryNames(countries)
for _, ctx := range significantStars {
r.renderStarbase(ctx)
r.renderStarFeatures(ctx)
}
for _, ctx := range significantStars {
r.renderStarName(ctx)
}
titlePoint := r.innerBounds.Min
r.createText(r.canvas, countryTextStyle, titlePoint, mapTitle)
titlePoint.Y += countryFontSize
r.createText(r.canvas, countryLegendStyle, titlePoint, r.state.Name)
titlePoint.Y += 0.6 * countryFontSize
r.createText(r.canvas, countryLegendStyle, titlePoint,
fmt.Sprintf("Year %d", r.state.Date.Year()))
footerStyle := starTextStyle.With(StyleOption{"text-anchor", "end"})
footerPoint := r.innerBounds.Max.Add(sgmmath.Point{0.0, -2 * fontSize})
r.createText(r.canvas, footerStyle, footerPoint, footerText)
}
func (r *Renderer) renderGrid() {
if r.opts.NoGrid {
return
}
w, h := r.bounds.Size()
gridStepX, gridStepY := w/gridSplit, h/gridSplit
for x := r.bounds.Min.X + gridStepX; x < r.bounds.Max.X-gridStepX/2; x += gridStepX {
startPoint := sgmmath.Point{x, r.bounds.Min.Y - canvasPadding/2}
path := NewPath().MoveToPoint(startPoint).VertLine(r.bounds.Max.Y + canvasPadding/2)
r.createPath(r.canvas, gridStyle, path)
}
for y := r.bounds.Min.Y + gridStepY; y < r.bounds.Max.Y-gridStepY/2; y += gridStepY {
startPoint := sgmmath.Point{r.bounds.Min.X - canvasPadding/2, y}
path := NewPath().MoveToPoint(startPoint).HorLine(r.bounds.Max.X + canvasPadding/2)
r.createPath(r.canvas, gridStyle, path)
}
}
func (r *Renderer) createPath(el *etree.Element, style Style, path Path) *etree.Element {
pathEl := el.CreateElement("path")
pathEl.CreateAttr("style", style.String())
pathEl.CreateAttr("d", path.String())
return pathEl
}
func (r *Renderer) createRect(el *etree.Element, style Style, rect sgmmath.BoundingRect) *etree.Element {
w, h := rect.Size()
rectEl := el.CreateElement("rect")
rectEl.CreateAttr("style", style.String())
rectEl.CreateAttr("x", fmt.Sprintf("%.4f", rect.Min.X))
rectEl.CreateAttr("y", fmt.Sprintf("%.4f", rect.Min.Y))
rectEl.CreateAttr("width", fmt.Sprintf("%.4f", w))
rectEl.CreateAttr("height", fmt.Sprintf("%.4f", h))
return rectEl
}
func (r *Renderer) createCircle(
el *etree.Element, style Style, center sgmmath.Point, radius float64,
) *etree.Element {
circleEl := el.CreateElement("circle")
circleEl.CreateAttr("cx", fmt.Sprintf("%.4f", center.X))
circleEl.CreateAttr("cy", fmt.Sprintf("%.4f", center.Y))
circleEl.CreateAttr("r", fmt.Sprintf("%.4f", radius))
circleEl.CreateAttr("style", style.String())
return circleEl
}
func (r *Renderer) createText(
el *etree.Element, style Style, point sgmmath.Point, text string,
) *etree.Element {
textEl := el.CreateElement("text")
textEl.CreateAttr("x", fmt.Sprintf("%.4f", point.X))
textEl.CreateAttr("y", fmt.Sprintf("%.4f", point.Y))
textEl.CreateAttr("style", style.String())
textEl.CreateText(text)
return textEl
}
func (r *Renderer) createIcon(el *etree.Element, p sgmmath.Point, iconName string, size float64) {
g := el.CreateElement("g")
g.CreateAttr("transform",
fmt.Sprintf("translate(%f, %f) scale(%f)", p.X, p.Y, size/float64(svgIconSize)))
icon := r.getIcon(iconName)
if icon == nil {
// TODO: render some placeholder
return
}
g.AddChild(icon.Root().Copy())
}
func (r *Renderer) createTitle(el *etree.Element, title string) {
titleEl := el.CreateElement("title")
titleEl.CreateText(title)
}
func (r *Renderer) Write(outPath string) error {
return r.doc.WriteToFile(outPath)
}
func (r *Renderer) WriteToBytes() ([]byte, error) {
return r.doc.WriteToBytes()
}