-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrender.go
451 lines (395 loc) · 12 KB
/
render.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package main
import (
"fmt"
"image"
"image/draw"
"math"
"os"
"path/filepath"
"strconv"
"strings"
)
func renderFocus(dst draw.Image, x, y int, id string) error {
f, ok := focusMap[id]
if !ok {
return fmt.Errorf("focus id \"" + id + "\" not found")
}
if !f.AllowBranch {
return nil
}
// Original game uses "GFX_technology_unavailable_item_bg" for some reason and replaces it with "GFX_focus_unavailable" via hardcoded part.
s := gfxMap["GFX_focus_unavailable"]
if len(f.Prerequisite) == 0 && f.Available {
s = gfxMap["GFX_focus_can_start"]
}
err := renderSprite(dst, x+gui.BG.Position.X, y+gui.BG.Position.Y, gui.BG.Orientation, gui.BG.CenterPosition, s)
if err != nil {
return fmt.Errorf("%v: %v", s.TextureFile, err)
}
symbol, ok := gfxMap[f.Icon]
if !ok {
symbol = gfxMap["GFX_goal_unknown"]
}
err = renderSprite(dst, x+gui.Symbol.Position.X, y+gui.Symbol.Position.Y, gui.Symbol.Orientation, gui.Symbol.CenterPosition, symbol)
if err != nil {
return fmt.Errorf("%v: %v", symbol.TextureFile, err)
}
text := f.Text
if text == "" {
text = f.ID
}
textX := x + gui.Name.Position.X
textY := y + gui.Name.Position.Y
if strings.ToLower(gui.Name.Format) == "center" {
textX += gui.Name.MaxWidth / 2
}
if strings.ToLower(gui.Name.VerticalAlignment) == "center" {
textY += gui.Name.MaxHeight / 2
}
font.RenderTextBox(dst, textX, textY, gui.Name.MaxWidth+2, gui.Name.MaxHeight, true, true, locMap[language][text].Value)
return nil
}
func renderSprite(dst draw.Image, x, y int, orientation, centerPosition string, sprite SpriteType) error {
// Read image data.
err := sprite.readTexture()
if err != nil {
return err
}
if strings.ToLower(orientation) == "center" {
x += gui.NationalFocusItem.Width / 2
y += gui.NationalFocusItem.Height / 2
}
if strings.ToLower(centerPosition) == "yes" {
x -= sprite.Image.Bounds().Max.X / 2
y -= sprite.Image.Bounds().Max.Y / 2
}
draw.Draw(dst, image.Rectangle{image.Point{x, y}, image.Point{x + sprite.Image.Bounds().Max.X, y + sprite.Image.Bounds().Max.Y}}, sprite.Image, image.ZP, draw.Over)
return nil
}
func renderExclusiveLines(dst *image.RGBA) error {
for _, f1 := range focusMap {
if !f1.AllowBranch {
continue
}
OUTER:
for _, e1 := range f1.MutuallyExclusive {
f2 := focusMap[e1]
if !f2.AllowBranch {
continue
}
// Ignore focuses with different Y coordinates, exclusivity links are not drawn in that case.
// Ignore focuses on the right side of the exclusivity link. We gonna draw from the left ones.
if (f1.Y != f2.Y) || (f1.X > f2.X) {
continue
}
// Ignore exclusivity links that pass through other focuses.
for _, e2 := range f1.MutuallyExclusive {
f3 := focusMap[e2]
if (f1.Y == f3.Y) && (f2.X > f3.X) && (f1.X < f3.X) {
continue OUTER
}
}
x := f1.X*gui.FocusSpacing.X + gui.NationalFocusExclusiveItem.Position.X + gui.ExclusiveOffset.X + spacingX
y := f1.Y*gui.FocusSpacing.Y + gui.NationalFocusExclusiveItem.Position.Y + gui.ExclusiveOffset.Y + spacingY
// 1x32 if 2 pos difference
// 4x32 if 3 pos difference
// 7x32 if 4 pos difference
xDifference := f2.X - f1.X
// Just draw mid part if the position difference is only 2.
if xDifference == 2 {
// Mid.
mid := gfxMap[gui.Mid.SpriteType]
err := mid.readTexture()
if err != nil {
return err
}
img, err := mid.getFrame(gui.Mid.Frame)
if err != nil {
return err
}
draw.Draw(dst,
image.Rectangle{
image.Point{x, y},
image.Point{x + img.Bounds().Max.X, y + img.Bounds().Max.Y}},
img,
image.ZP,
draw.Over)
} else if xDifference > 2 {
lineSize := (xDifference - 2) * 3 * 32
// Link1.
link1 := gfxMap[gui.Link1.SpriteType]
err := link1.readTexture()
if err != nil {
return err
}
img, err := link1.getFrame(gui.Link1.Frame)
if err != nil {
return err
}
for i := 0; i < lineSize/gui.LinkSpacing.X; i++ {
draw.Draw(dst,
image.Rectangle{
image.Point{x + gui.Link1.Position.X + gui.LinkSpacing.X*i, y + gui.Link1.Position.Y - 2},
image.Point{x + img.Bounds().Max.X + gui.Link1.Position.X + gui.LinkSpacing.X*i, y + img.Bounds().Max.Y + gui.Link1.Position.Y - 2}},
img,
image.ZP,
draw.Over)
}
// Left.
left := gfxMap[gui.Left.SpriteType]
err = left.readTexture()
if err != nil {
return err
}
img, err = left.getFrame(gui.Left.Frame)
if err != nil {
return err
}
draw.Draw(dst,
image.Rectangle{
image.Point{x + gui.Right.Position.X, y + gui.Right.Position.Y},
image.Point{x + img.Bounds().Max.X + gui.Right.Position.X, y + img.Bounds().Max.Y + gui.Right.Position.Y}},
img,
image.ZP,
draw.Over)
// Right.
right := gfxMap[gui.Right.SpriteType]
err = right.readTexture()
if err != nil {
return err
}
img, err = right.getFrame(gui.Right.Frame)
if err != nil {
return err
}
draw.Draw(dst,
image.Rectangle{
image.Point{x + lineSize + gui.Right.Position.X, y + gui.Right.Position.Y},
image.Point{x + img.Bounds().Max.X + lineSize + gui.Right.Position.X, y + img.Bounds().Max.Y + gui.Right.Position.Y}},
img,
image.ZP,
draw.Over)
// Mid.
mid := gfxMap[gui.Mid.SpriteType]
err = mid.readTexture()
if err != nil {
return err
}
img, err = mid.getFrame(gui.Mid.Frame)
if err != nil {
return err
}
draw.Draw(dst,
image.Rectangle{
image.Point{x + lineSize/2 + gui.Right.Position.X, y + gui.Right.Position.Y},
image.Point{x + img.Bounds().Max.X + lineSize/2 + gui.Right.Position.X, y + img.Bounds().Max.Y + gui.Right.Position.Y}},
img,
image.ZP,
draw.Over)
}
}
}
return nil
}
func renderLines(dst *image.RGBA) error {
var err error
// Load the textures.
UD, UDdash, err = readTextureAndGetFrames("GFX_focus_link_up_down", 3, 4)
UL, ULdash, err = readTextureAndGetFrames("GFX_focus_link_up_left", 3, 4)
UR, URdash, err = readTextureAndGetFrames("GFX_focus_link_up_right", 3, 4)
DL, DLdash, err = readTextureAndGetFrames("GFX_focus_link_down_left", 3, 4)
DR, DRdash, err = readTextureAndGetFrames("GFX_focus_link_down_right", 3, 4)
LR, LRdash, err = readTextureAndGetFrames("GFX_focus_link_left_right", 3, 4)
UDL, UDLdash, err = readTextureAndGetFrames("GFX_focus_link_up_down_left", 3, 4)
UDR, UDRdash, err = readTextureAndGetFrames("GFX_focus_link_up_down_right", 3, 4)
ULR, ULRdash, err = readTextureAndGetFrames("GFX_focus_link_up_left_right", 3, 4)
DLR, DLRdash, err = readTextureAndGetFrames("GFX_focus_link_down_left_right", 3, 4)
UDLR, UDLRdash, err = readTextureAndGetFrames("GFX_focus_link_up_down_left_right", 3, 4)
if err != nil {
return err
}
var drawnCoords []image.Point
for _, p := range focusMap {
if len(p.Children) > 0 && p.AllowBranch {
x := p.X*gui.FocusSpacing.X + gui.NationalFocusLink.Position.X + gui.LinkBegin.X + gui.LinkOffsets.X + spacingX
y := p.Y*gui.FocusSpacing.Y + gui.NationalFocusLink.Position.Y + gui.LinkBegin.Y + gui.LinkOffsets.Y + spacingY - 16
// First link (out).
img := UD
if p.Out.Dir < 16 {
img = UDdash
}
draw.Draw(dst,
image.Rectangle{
image.Point{x, y},
image.Point{x + img.Bounds().Max.X, y + img.Bounds().Max.Y}},
img,
image.ZP,
draw.Over)
y += UD.Bounds().Max.Y
// First corner (out).
img = p.Out.Get()
draw.Draw(dst,
image.Rectangle{
image.Point{x, y},
image.Point{x + img.Bounds().Max.X, y + img.Bounds().Max.Y}},
img,
image.ZP,
draw.Over)
drawnCoords = append(drawnCoords, image.Point{x, y})
cornerXvalues := []int{x}
for _, c := range p.Children {
c := focusMap[c.ID]
if c.AllowBranch {
cornerXvalues = append(cornerXvalues, c.X*gui.FocusSpacing.X+gui.NationalFocusLink.Position.X+gui.LinkBegin.X+gui.LinkOffsets.X+spacingX)
}
}
var isPrevSolid bool
for _, c := range p.Children {
c := focusMap[c.ID]
if !c.AllowBranch {
continue
}
x := c.X*gui.FocusSpacing.X + gui.NationalFocusLink.Position.X + gui.LinkEnd.X + gui.LinkOffsets.X + spacingX
// Children horizontal lines.
if c.X != p.X {
step := gui.LinkSpacing.X
if c.X > p.X {
step = -gui.LinkSpacing.X
isPrevSolid = false
for _, c2 := range p.Children {
c2 := focusMap[c2.ID]
if c2.X > c.X && c2.In[p.Y].Dir > 16 {
isPrevSolid = true
}
}
}
x := c.X*gui.FocusSpacing.X + gui.NationalFocusLink.Position.X + gui.LinkBegin.X + gui.LinkOffsets.X + spacingX
length := int(math.Abs(float64(c.X-p.X)))*gui.FocusSpacing.Y + gui.LinkBegin.X + gui.LinkOffsets.X + spacingX
img = LRdash
if (c.In[p.Y].Dir > 16 || isPrevSolid) && p.Out.Dir > 16 {
img = LR
isPrevSolid = true
}
for i := 1; i < length/gui.LinkSpacing.X; i++ {
x += step
if containsInt(cornerXvalues, x) {
break
}
draw.Draw(dst,
image.Rectangle{
image.Point{x, y},
image.Point{x + img.Bounds().Max.X, y + img.Bounds().Max.Y}},
img,
image.ZP,
draw.Over)
}
}
// Children corner (in).
a := c.In[p.Y]
img := a.Get()
if !containsPoint(drawnCoords, image.Point{x, y}) {
draw.Draw(dst,
image.Rectangle{
image.Point{x, y},
image.Point{x + img.Bounds().Max.X, y + img.Bounds().Max.Y}},
img,
image.ZP,
draw.Over)
}
drawnCoords = append(drawnCoords, image.Point{x, y})
// Children vertical lines (in).
if c.Y-p.Y > 0 {
img = UD
if c.In[p.Y].Dir < 16 {
img = UDdash
}
nextCornerY := maxYinRange(c.In, p.Y)
childY := c.Y
if nextCornerY != 0 {
childY = nextCornerY
}
length := (childY-p.Y)*gui.FocusSpacing.Y + gui.LinkEnd.Y - gui.LinkSpacing.Y*2
if nextCornerY != 0 {
length += gui.LinkSpacing.Y
}
var i int
for i = 1; i <= length/gui.LinkSpacing.Y; i++ {
if !containsPoint(drawnCoords, image.Point{x, y + gui.LinkSpacing.Y*i}) {
draw.Draw(dst,
image.Rectangle{
image.Point{x, y + gui.LinkSpacing.Y*i},
image.Point{x + img.Bounds().Max.X, y + img.Bounds().Max.Y + gui.LinkSpacing.Y*i}},
img,
image.ZP,
draw.Over)
}
drawnCoords = append(drawnCoords, image.Point{x, y + gui.LinkSpacing.Y*i})
}
leftover := length - (i-1)*gui.LinkSpacing.Y
if leftover > 0 {
draw.Draw(dst,
image.Rectangle{
image.Point{x, y + gui.LinkSpacing.Y*i},
image.Point{x + img.Bounds().Max.X, y + leftover + gui.LinkSpacing.Y*i}},
img,
image.ZP,
draw.Over)
}
}
}
}
}
return nil
}
func readTextureAndGetFrames(texture string, frame1, frame2 int) (image.Image, image.Image, error) {
s := gfxMap[texture]
err := s.readTexture()
if err != nil {
return nil, nil, err
}
f1, err := s.getFrame(frame1)
if err != nil {
return nil, nil, err
}
f2, err := s.getFrame(frame2)
if err != nil {
return nil, nil, err
}
return f1, f2, nil
}
func (s *SpriteType) readTexture() error {
imgFile, err := os.Open(s.TextureFile)
if err != nil {
// Try looking for the sprite in other declared mod/game folders.
texture := s.TextureFile
for _, p := range modPaths {
texture = strings.TrimPrefix(texture, p)
}
for i := len(modPaths) - 1; i >= 0; i-- {
imgFile, err = os.Open(filepath.Join(modPaths[i], texture))
if err == nil {
goto TextureFileFound
}
}
return err
}
TextureFileFound:
defer imgFile.Close()
s.Image, _, err = image.Decode(imgFile)
if err != nil {
return err
}
return nil
}
func (s *SpriteType) getFrame(f int) (image.Image, error) {
if s.Image == nil {
return nil, fmt.Errorf(s.Name + " has no image data")
}
if f < 1 {
return nil, fmt.Errorf("frame number must be higher then 0, it is currently " + strconv.Itoa(f))
}
frameSize := image.Point{s.Image.Bounds().Max.X / s.NoOfFrames, s.Image.Bounds().Max.Y}
dst := image.NewRGBA(image.Rectangle{image.ZP, frameSize})
draw.Draw(dst, dst.Bounds(), s.Image, image.Point{frameSize.X * (f - 1), 0}, draw.Src)
return dst, nil
}