2022-08-27 05:51:30 +02:00
|
|
|
// Copyright 2022 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package graphicscommand
|
|
|
|
|
|
|
|
import (
|
2022-08-30 20:10:10 +02:00
|
|
|
"archive/zip"
|
2022-08-27 05:51:30 +02:00
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
"syscall/js"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
)
|
|
|
|
|
2022-08-30 20:10:10 +02:00
|
|
|
func download(buf *bytes.Buffer, mime string, path string) {
|
2022-08-27 05:51:30 +02:00
|
|
|
global := js.Global()
|
|
|
|
|
|
|
|
jsData := global.Get("Uint8Array").New(buf.Len())
|
|
|
|
js.CopyBytesToJS(jsData, buf.Bytes())
|
|
|
|
|
|
|
|
a := global.Get("document").Call("createElement", "a")
|
|
|
|
blob := global.Get("Blob").New(
|
2022-11-03 07:33:09 +01:00
|
|
|
[]any{jsData},
|
|
|
|
map[string]any{"type": mime},
|
2022-08-27 05:51:30 +02:00
|
|
|
)
|
|
|
|
a.Set("href", global.Get("URL").Call("createObjectURL", blob))
|
|
|
|
a.Set("download", path)
|
|
|
|
a.Call("click")
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
2022-08-30 20:10:10 +02:00
|
|
|
// Screen image cannot be dumped.
|
|
|
|
if i.screen {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", nil
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if err := i.dumpTo(buf, graphicsDriver, blackbg, rect); err != nil {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", err
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
download(buf, "image/png", i.dumpName(path))
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
return path, nil
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DumpImages dumps all the specified images to the specified directory.
|
|
|
|
//
|
|
|
|
// This is for testing usage.
|
2022-08-31 06:10:41 +02:00
|
|
|
func DumpImages(images []*Image, graphicsDriver graphicsdriver.Graphics, dir string) (string, error) {
|
2022-08-30 20:10:10 +02:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
zw := zip.NewWriter(buf)
|
|
|
|
|
|
|
|
for _, img := range images {
|
2022-08-31 05:22:28 +02:00
|
|
|
// Screen image cannot be dumped.
|
|
|
|
if img.screen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:10:10 +02:00
|
|
|
f, err := zw.Create(img.dumpName("*.png"))
|
|
|
|
if err != nil {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", err
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := img.dumpTo(f, graphicsDriver, false, image.Rect(0, 0, img.width, img.height)); err != nil {
|
2022-08-31 06:10:41 +02:00
|
|
|
return "", err
|
2022-08-30 20:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:52:46 +02:00
|
|
|
_ = zw.Close()
|
2022-08-30 20:10:10 +02:00
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
zip := dir + ".zip"
|
|
|
|
download(buf, "archive/zip", zip)
|
2022-08-27 05:51:30 +02:00
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
return zip, nil
|
2022-08-27 05:51:30 +02:00
|
|
|
}
|