-
Notifications
You must be signed in to change notification settings - Fork 46
/
export.go
397 lines (286 loc) · 10.2 KB
/
export.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
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/signintech/gopdf"
"github.com/veandco/go-sdl2/sdl"
)
const (
ExportModePNG = "PNG"
ExportModePDF = "PDF"
)
const (
BackgroundNormal = iota
BackgroundNoGrid
BackgroundTransparent
)
type ScreenshotOptions struct {
Exporting bool
ExportIndex int
BackgroundOption int
HideGUI bool
ExportMode string
Filename string
}
type screenshotOutput struct {
Page *Page
Screenshot *image.RGBA
}
var activeScreenshotOutputs []screenshotOutput
var activeScreenshot *ScreenshotOptions
func TakeScreenshot(options *ScreenshotOptions) {
if options == nil {
opt := &ScreenshotOptions{}
// Use the current time for screenshot names; ".00" adds the fractional second
screenshotFileName := fmt.Sprintf("screenshot_%s.png", time.Now().Format(FileTimeFormat+".00"))
screenshotPath := LocalRelativePath(screenshotFileName)
if projectScreenshotsPath := globals.Settings.Get(SettingsScreenshotPath).AsString(); projectScreenshotsPath != "" {
if FolderExists(projectScreenshotsPath) {
screenshotPath = filepath.Join(projectScreenshotsPath, screenshotFileName)
} else {
globals.EventLog.Log("Warning: Custom screenshot folder [%s] doesn't exist; screenshots will be saved next to MasterPlan executable instead.", true, projectScreenshotsPath)
}
}
opt.Filename = screenshotPath
opt.ExportMode = ExportModePNG
activeScreenshot = opt
} else {
activeScreenshot = options
}
}
func createScreenshotImage(surf *sdl.Surface, width, height int32) *image.RGBA {
surf.FillRect(nil, 0x00000000)
if err := globals.Renderer.ReadPixels(nil, surf.Format.Format, surf.Data(), int(surf.Pitch)); err != nil {
globals.EventLog.Log(err.Error(), false)
} else {
img := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
for y := 0; y < int(height); y++ {
for x := 0; x < int(width); x++ {
r, g, b, a := ColorAt(surf, int32(x), int32(y))
img.Set(x, y, color.RGBA{r, g, b, a})
}
}
return img
}
return nil
}
func handleScreenshots() {
if activeScreenshot != nil {
pages := []*Page{globals.Project.Pages[0]}
if activeScreenshot.Exporting {
globals.State = StateExport
for _, page := range globals.Project.Pages[1:] {
if page.Valid() {
pages = append(pages, page)
}
}
}
camera := globals.Project.Camera
origPosition := camera.Position
origTargetPosition := camera.TargetPosition
origZoom := camera.Zoom
origTargetZoom := camera.TargetZoom
origPage := globals.Project.CurrentPage
origScreenSize := globals.ScreenSize // The screen size changes because we're changing the renderer's render target, which may have a different size from the screen
type shotPiece struct {
Piece *image.RGBA
Offset image.Point
}
if !activeScreenshot.Exporting {
// For an ordinary screenshot, we don't have to do much; we just take a screenshot using the already bound backing globals.Renderer render target, and export it.
shot := createScreenshotImage(globals.ScreenshotSurf, int32(globals.ScreenSize.X), int32(globals.ScreenSize.Y))
activeScreenshotOutputs = []screenshotOutput{{
Page: globals.Project.CurrentPage,
Screenshot: shot,
},
}
} else if activeScreenshot.ExportIndex < len(pages) {
screenshotWidth := 1920
screenshotHeight := 1080
globals.ScreenSize.X = float32(screenshotWidth)
globals.ScreenSize.Y = float32(screenshotHeight)
page := globals.Project.Pages[activeScreenshot.ExportIndex]
// But for exporting a project, we have to piece together a larger screenshot for all of each page, not just what the camera currently sees.
SetRenderTarget(globals.ScreenshotTexture)
globals.Renderer.SetDrawColor(0, 0, 0, 0)
globals.Renderer.Clear()
globals.Project.CurrentPage = page
cardBounds := NewCorrectingRect(0, 0, 0, 0)
if len(page.Cards) > 0 {
card := page.Cards[0]
cardBounds.X1 = card.Rect.X
cardBounds.Y1 = card.Rect.Y
cardBounds.X2 = cardBounds.X1
cardBounds.Y2 = cardBounds.Y1
cardBounds.AddXY(card.Rect.X+card.Rect.W, card.Rect.Y+card.Rect.H)
}
deadlineDisplaySetting := globals.Settings.Get(SettingsDeadlineDisplay).AsString()
for _, card := range page.Cards {
cardBounds = cardBounds.AddXY(card.Rect.X, card.Rect.Y)
cardBounds = cardBounds.AddXY(card.Rect.X+card.Rect.W, card.Rect.Y+card.Rect.H)
if card.DeadlineState() != DeadlineStateDone {
if deadlineDisplaySetting != DeadlineDisplayIcons {
deadlineText := card.DeadlineText()
measure := globals.TextRenderer.MeasureText([]rune(deadlineText), 1)
cardBounds = cardBounds.AddXY(card.Rect.X-measure.X, card.Rect.Y)
} else {
cardBounds = cardBounds.AddXY(card.Rect.X-32, card.Rect.Y)
}
}
}
globals.Project.Camera.TargetZoom = 1
globals.Project.Camera.Zoom = 1
pieces := []shotPiece{}
offsetX := 0
offsetY := 0
// Screenshot size for export is 1920x1080 base
halfSSW := float32(screenshotWidth) / 2
halfSSH := float32(screenshotHeight) / 2
oneShot := false // One picture because all cards are visible on one screen; no need to stitch
for y := cardBounds.Y1 + halfSSH - 64; y <= cardBounds.Y2+halfSSH; y += float32(screenshotHeight) {
offsetX = 0
for x := cardBounds.X1 + halfSSW - 64; x <= cardBounds.X2+halfSSW; x += float32(screenshotWidth) {
if cardBounds.Width() > float32(screenshotWidth) || cardBounds.Height() > float32(screenshotHeight) {
globals.Project.Camera.Position.X = x
globals.Project.Camera.Position.Y = y
} else {
globals.Project.Camera.Position = cardBounds.Center()
oneShot = true
}
globals.Project.Camera.TargetPosition = globals.Project.Camera.Position
if activeScreenshot.BackgroundOption != BackgroundTransparent {
clearColor := getThemeColor(GUIBGColor)
globals.Renderer.SetDrawColor(clearColor.RGBA())
} else {
globals.Renderer.SetDrawColor(0, 0, 0, 0)
}
globals.Renderer.Clear()
if activeScreenshot.BackgroundOption == BackgroundNormal {
globals.Project.DrawGrid()
}
page.Update()
page.Draw()
if !activeScreenshot.HideGUI {
globals.MenuSystem.Draw()
}
pieces = append(pieces, shotPiece{
Piece: createScreenshotImage(globals.ExportSurf, int32(screenshotWidth), int32(screenshotHeight)), // We've binded the screenshot texture for this, which is 1920x1080
Offset: image.Point{offsetX, offsetY},
})
offsetX += screenshotWidth
if oneShot {
break
}
}
offsetY += screenshotHeight
if oneShot {
break
}
}
shot := image.NewRGBA(image.Rect(0, 0, int(offsetX), int(offsetY)))
for _, p := range pieces {
D := p.Piece.Bounds().Add(p.Offset)
draw.Draw(shot, D, p.Piece, image.Point{0, 0}, draw.Src)
}
activeScreenshotOutputs = append(activeScreenshotOutputs, screenshotOutput{
Page: page,
Screenshot: shot,
})
activeScreenshot.ExportIndex++
}
SetRenderTarget(nil)
globals.ScreenSize = origScreenSize
globals.Project.Camera.Zoom = origZoom
globals.Project.Camera.TargetZoom = origTargetZoom
globals.Project.Camera.Position = origPosition
globals.Project.Camera.TargetPosition = origTargetPosition
globals.Project.CurrentPage = origPage
if !activeScreenshot.Exporting || activeScreenshot.ExportIndex >= len(pages) {
switch activeScreenshot.ExportMode {
case ExportModePNG:
var err error
exportedPageNames := map[string]bool{}
for _, img := range activeScreenshotOutputs {
pagePath := activeScreenshot.Filename
if activeScreenshot.Exporting {
projectName := ""
if globals.Project.Filepath != "" {
_, projectName = filepath.Split(globals.Project.Filepath)
if ind := strings.Index(projectName, filepath.Ext(projectName)); ind >= 0 {
projectName = projectName[:ind]
}
}
name := img.Page.Name()
i := 2
_, existsAlready := exportedPageNames[name]
for existsAlready {
name += strconv.Itoa(i)
_, existsAlready = exportedPageNames[name]
}
exportedPageNames[name] = true
pagePath = filepath.Join(pagePath, projectName+"_Export_"+name+".png")
}
var screenshotFile *os.File
screenshotFile, err = os.Create(pagePath)
if err != nil {
break
}
err = png.Encode(screenshotFile, img.Screenshot)
if err != nil {
break
}
screenshotFile.Sync()
screenshotFile.Close()
}
if err == nil {
if activeScreenshot.Exporting {
globals.EventLog.Log("Project successfully exported in %s format to folder: %s.", false, activeScreenshot.ExportMode, activeScreenshot.Filename)
} else {
globals.EventLog.Log("Screenshot saved successfully to %s.", false, activeScreenshot.Filename)
}
} else {
globals.EventLog.Log(err.Error(), true)
}
case ExportModePDF:
pagePath := activeScreenshot.Filename
projectName := ""
if globals.Project.Filepath != "" {
_, projectName = filepath.Split(globals.Project.Filepath)
if ind := strings.Index(projectName, filepath.Ext(projectName)); ind >= 0 {
projectName = projectName[:ind]
}
}
pagePath = filepath.Join(pagePath, projectName+"_Export.pdf")
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{})
pdf.SetNoCompression()
for _, img := range activeScreenshotOutputs {
pageWidth := float64(img.Screenshot.Bounds().Dx()) / 3
pageHeight := float64(img.Screenshot.Bounds().Dy()) / 3
pdf.AddPageWithOption(gopdf.PageOption{PageSize: &gopdf.Rect{W: pageWidth, H: pageHeight}})
pdf.ImageFrom(img.Screenshot, 0, 0, &gopdf.Rect{W: pageWidth, H: pageHeight})
}
if err := pdf.WritePdf(pagePath); err != nil {
globals.EventLog.Log(err.Error(), true)
} else {
if activeScreenshot.Exporting {
globals.EventLog.Log("Project successfully exported in [%s] format to folder: %s.", false, activeScreenshot.ExportMode, activeScreenshot.Filename)
} else {
globals.EventLog.Log("Screenshot saved successfully to %s.", false, activeScreenshot.Filename)
}
}
}
activeScreenshot = nil // Handled
activeScreenshotOutputs = []screenshotOutput{}
globals.State = StateNeutral
}
}
}