Skip to content

Commit 2f94135

Browse files
committed
added more screenshot options
1 parent d4ee08c commit 2f94135

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

colors.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ type ColorConfig struct {
2424

2525
func (cc *ColorConfig) Set(hexString string, colorType ColorType, defaultString string) {
2626
colorHexString := defaultString
27+
var c color.Color
2728
if hexString != "" {
2829
colorHexString = hexString
2930
}
30-
c, err := colorful.Hex(colorHexString)
31-
if err != nil {
32-
panic(err)
31+
if colorHexString == "" {
32+
c = color.Transparent
33+
} else {
34+
d, err := colorful.Hex(colorHexString)
35+
if err != nil {
36+
panic(err)
37+
}
38+
c = d
3339
}
3440
switch colorType {
3541
case BackgroundColorType:

examples/scale_rotate/sketch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"SketchWidth": 800,
55
"SketchHeight": 800,
66
"ControlWidth": 240,
7+
"DisableClearBetweenFrames": true,
78
"Controls": [
89
{
910
"Name": "N",

sketch.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package sketchy
33
import (
44
"encoding/json"
55
"fmt"
6+
"image"
7+
"image/color"
8+
"image/png"
69
"io/ioutil"
710
"log"
811
"os"
@@ -13,10 +16,11 @@ import (
1316
)
1417

1518
const (
16-
DefaultTitle = "Sketch"
17-
DefaultPrefix = "sketch"
18-
DefaultBackgroundColor = "#1e1e1e"
19-
DefaultOutlineColor = "#ffdb00"
19+
DefaultTitle = "Sketch"
20+
DefaultPrefix = "sketch"
21+
DefaultBackgroundColor = "#1e1e1e"
22+
DefaultOutlineColor = "#ffdb00"
23+
DefaultSketchOutlineColor = ""
2024
)
2125

2226
type SketchUpdater func(s *Sketch)
@@ -43,6 +47,7 @@ type Sketch struct {
4347
controlColorConfig ColorConfig `json:"-"`
4448
sketchColorConfig ColorConfig `json:"-"`
4549
isSavingPNG bool `json:"-"`
50+
isSavingScreen bool `json:"-"`
4651
needToClear bool `json:"-"`
4752
}
4853

@@ -77,6 +82,7 @@ func (s *Sketch) Init() {
7782
if s.DisableClearBetweenFrames {
7883
ebiten.SetScreenClearedEveryFrame(false)
7984
}
85+
os.Setenv("EBITEN_SCREENSHOT_KEY", "escape")
8086
}
8187

8288
func (s *Sketch) Var(name string) float64 {
@@ -92,6 +98,9 @@ func (s *Sketch) UpdateControls() {
9298
if inpututil.IsKeyJustReleased(ebiten.KeyS) {
9399
s.isSavingPNG = true
94100
}
101+
if inpututil.IsKeyJustReleased(ebiten.KeyQ) {
102+
s.isSavingScreen = true
103+
}
95104
if inpututil.IsKeyJustReleased(ebiten.KeyC) {
96105
s.saveConfig()
97106
}
@@ -158,9 +167,11 @@ func (s *Sketch) Draw(screen *ebiten.Image) {
158167
cc.Fill()
159168
s.needToClear = false
160169
}
161-
cc.SetColor(s.sketchColorConfig.Outline)
162-
cc.DrawRectangle(s.ControlWidth+2.5, 2.5, s.SketchWidth-5, s.SketchHeight-5)
163-
cc.Stroke()
170+
if s.sketchColorConfig.Outline != color.Transparent {
171+
cc.SetColor(s.sketchColorConfig.Outline)
172+
cc.DrawRectangle(s.ControlWidth+2.5, 2.5, s.SketchWidth-5, s.SketchHeight-5)
173+
cc.Stroke()
174+
}
164175
screen.DrawImage(ebiten.NewImageFromImage(cc.Image()), nil)
165176
ctx := gg.NewContext(int(s.SketchWidth), H)
166177
ctx.Push()
@@ -172,6 +183,23 @@ func (s *Sketch) Draw(screen *ebiten.Image) {
172183
fmt.Println("Saved ", fname)
173184
s.isSavingPNG = false
174185
}
186+
if s.isSavingScreen {
187+
fname := s.Prefix + "_" + GetTimestampString() + ".png"
188+
sketchImage := screen.SubImage(s.getSketchImageRect())
189+
f, err := os.Create(fname)
190+
if err != nil {
191+
log.Fatal("error while trying to create screenshot file", err)
192+
}
193+
if err := png.Encode(f, sketchImage); err != nil {
194+
f.Close()
195+
log.Fatal("error while trying to encode screenshot image", err)
196+
}
197+
if err := f.Close(); err != nil {
198+
log.Fatal("error while trying to close screenshot file", err)
199+
}
200+
fmt.Println("Saved ", fname)
201+
s.isSavingScreen = false
202+
}
175203
op := &ebiten.DrawImageOptions{}
176204
op.GeoM.Translate(s.ControlWidth, 0)
177205
screen.DrawImage(ebiten.NewImageFromImage(ctx.Image()), op)
@@ -196,7 +224,7 @@ func (s *Sketch) parseColors() {
196224
s.controlColorConfig.Set(s.ControlBackgroundColor, BackgroundColorType, DefaultBackgroundColor)
197225
s.controlColorConfig.Set(s.ControlOutlineColor, OutlineColorType, DefaultOutlineColor)
198226
s.sketchColorConfig.Set(s.SketchBackgroundColor, BackgroundColorType, DefaultBackgroundColor)
199-
s.sketchColorConfig.Set(s.SketchOutlineColor, OutlineColorType, DefaultOutlineColor)
227+
s.sketchColorConfig.Set(s.SketchOutlineColor, OutlineColorType, DefaultSketchOutlineColor)
200228
for i := range s.Controls {
201229
s.Controls[i].parseColors()
202230
}
@@ -211,3 +239,11 @@ func (s *Sketch) saveConfig() {
211239
}
212240
fmt.Println("Saved config ", fname)
213241
}
242+
243+
func (s *Sketch) getSketchImageRect() image.Rectangle {
244+
left := int(s.ControlWidth)
245+
top := 0
246+
right := left + int(s.SketchWidth)
247+
bottom := int(s.SketchHeight)
248+
return image.Rect(left, top, right, bottom)
249+
}

template/sketch.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@
1717
"Val": 0.9,
1818
"Incr": 0.01
1919
}
20-
]
20+
],
21+
"SketchBackgroundColor": "#1e1e1e",
22+
"SketchOutlineColor": "#ffdb00",
23+
"ControlBackgroundColor": "#1e1e1e",
24+
"ControlOutlineColor": "#ffdb00"
2125
}

0 commit comments

Comments
 (0)