Skip to content

Commit 4c72990

Browse files
authored
Merge pull request #5 from aldernero/feature-add-checkboxes
added checkboxes
2 parents e012ff4 + 80c7a05 commit 4c72990

File tree

13 files changed

+196
-63
lines changed

13 files changed

+196
-63
lines changed

controls.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/fogleman/gg"
88
"github.com/hajimehoshi/ebiten/v2"
9+
"github.com/hajimehoshi/ebiten/v2/inpututil"
910
)
1011

1112
const (
@@ -17,6 +18,13 @@ const (
1718
SliderOutlineColor = "#ffdb00"
1819
SliderFillColor = "#ffdb00"
1920
SliderTextColor = "#ffffff"
21+
CheckboxHeight = 15.0
22+
CheckboxHPadding = 12.0
23+
CheckboxVPadding = 7.0
24+
CheckboxBackgroundColor = "#1e1e1e"
25+
CheckboxOutlineColor = "#ffdb00"
26+
CheckboxFillColor = "#ffdb00"
27+
CheckboxTextColor = "#ffffff"
2028
)
2129

2230
type Slider struct {
@@ -36,6 +44,20 @@ type Slider struct {
3644
DidJustChange bool `json:"-"`
3745
}
3846

47+
type Checkbox struct {
48+
Name string `json:"Name"`
49+
Pos Point `json:"-"`
50+
Width float64 `json:"Width"`
51+
Height float64 `json:"Height"`
52+
Checked bool `json:"Checked"`
53+
OutlineColor string `json:"OutlineColor"`
54+
BackgroundColor string `json:"BackgroundColor"`
55+
FillColor string `json:"FillColor"`
56+
TextColor string `json:"TextColor"`
57+
colors ColorConfig `json:"-"`
58+
DidJustChange bool `json:"-"`
59+
}
60+
3961
func (s *Slider) GetPercentage() float64 {
4062
return Map(s.MinVal, s.MaxVal, 0, 1, s.Val)
4163
}
@@ -137,9 +159,69 @@ func NewRadiansSlider(name string, steps int) Slider {
137159
return s
138160
}
139161

162+
func (c *Checkbox) GetRect(ctx *gg.Context) Rect {
163+
x := c.Pos.X - CheckboxHPadding
164+
y := c.Pos.Y - CheckboxVPadding
165+
w := c.Width + 2*CheckboxHPadding
166+
h := c.Height + CheckboxVPadding
167+
return Rect{X: x, Y: y, W: w, H: h}
168+
}
169+
170+
func (c *Checkbox) AutoHeight(ctx *gg.Context) {
171+
c.Height = ctx.FontHeight()
172+
}
173+
174+
func (c *Checkbox) IsInside(x float64, y float64) bool {
175+
return x >= c.Pos.X &&
176+
x <= c.Pos.X+c.Height && y >= c.Pos.Y && y <= c.Pos.Y+c.Height
177+
}
178+
179+
func (c *Checkbox) Update() {
180+
c.Checked = !c.Checked
181+
}
182+
183+
func (c *Checkbox) CheckAndUpdate() (bool, error) {
184+
didChange := false
185+
x, y := ebiten.CursorPosition()
186+
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
187+
if c.IsInside(float64(x), float64(y)) {
188+
c.Update()
189+
didChange = true
190+
}
191+
}
192+
c.DidJustChange = didChange
193+
return didChange, nil
194+
}
195+
196+
func (c *Checkbox) Draw(ctx *gg.Context) {
197+
ctx.SetLineCapButt()
198+
ctx.SetLineWidth(1.5)
199+
ctx.SetColor(c.colors.Background)
200+
ctx.DrawRectangle(c.Pos.X, c.Pos.Y, c.Height, c.Height)
201+
ctx.Fill()
202+
if c.Checked {
203+
ctx.SetColor(c.colors.Fill)
204+
ctx.DrawLine(c.Pos.X, c.Pos.Y, c.Pos.X+c.Height, c.Pos.Y+c.Height)
205+
ctx.DrawLine(c.Pos.X, c.Pos.Y+c.Height, c.Pos.X+c.Height, c.Pos.Y)
206+
ctx.Stroke()
207+
}
208+
ctx.SetColor(c.colors.Outline)
209+
ctx.DrawRectangle(c.Pos.X, c.Pos.Y, c.Height, c.Height)
210+
ctx.Stroke()
211+
ctx.SetColor(c.colors.Text)
212+
ctx.DrawStringWrapped(c.Name, c.Pos.X, c.Pos.Y, 0, 0, c.Width, 1, gg.AlignRight)
213+
}
214+
140215
func (s *Slider) parseColors() {
141216
s.colors.Set(s.BackgroundColor, BackgroundColorType, SliderBackgroundColor)
142217
s.colors.Set(s.OutlineColor, OutlineColorType, SliderOutlineColor)
143218
s.colors.Set(s.TextColor, TextColorType, SliderTextColor)
144219
s.colors.Set(s.FillColor, FillColorType, SliderFillColor)
145220
}
221+
222+
func (c *Checkbox) parseColors() {
223+
c.colors.Set(c.BackgroundColor, BackgroundColorType, CheckboxBackgroundColor)
224+
c.colors.Set(c.OutlineColor, OutlineColorType, CheckboxOutlineColor)
225+
c.colors.Set(c.TextColor, TextColorType, CheckboxTextColor)
226+
c.colors.Set(c.FillColor, FillColorType, CheckboxFillColor)
227+
}

docs/getting-started.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ So far this is identical to the previous section. Let's look at the contents of
7373
"SketchWidth": 800,
7474
"SketchHeight": 800,
7575
"ControlWidth": 240,
76-
"Controls": [
76+
"Sliders": [
7777
{
7878
"Name": "control1",
7979
"MinVal": 1,
@@ -97,7 +97,7 @@ This is the default configuration with 2 example controls. The first 3 lines def
9797
"SketchWidth": 800,
9898
"SketchHeight": 800,
9999
"ControlWidth": 240,
100-
"Controls": [
100+
"Sliders": [
101101
{
102102
"Name": "radius",
103103
"MinVal": 1,
@@ -139,22 +139,22 @@ func draw(s *sketchy.Sketch, c *gg.Context) {
139139
Notice that the `draw` function takes two arguments. The first argument stores the Sketch struct used to store our sketch information, including the two slider controls. Here is how you get the value from a slider:
140140

141141
```go
142-
val := s.Var("slider name")
142+
val := s.Slider("slider name")
143143
```
144144

145145
The value will be a float64. For our case we could define two variables that are tied to the controls we defined earlier:
146146
```go
147-
radius := s.Var("radius")
148-
thickness := s.Var("thickness")
147+
radius := s.Slider("radius")
148+
thickness := s.Slider("thickness")
149149
```
150150
Notice the argument to `Var` is the same name we used in `sketch.json`.
151151

152152
The other argument to `draw` is a `gg` drawing context. See the [gg](https://github.com/fogleman/gg) documentation for full details. For this example we will simply 1) set a drawing color, 2) set the line thickness, 3) define the circle object, and 4) draw the circle. Here is the entire draw function:
153153
```go
154154
func draw(s *sketchy.Sketch, c *gg.Context) {
155155
// Drawing code goes here
156-
radius := s.Var("radius")
157-
thickness := s.Var("thickness")
156+
radius := s.Slider("radius")
157+
thickness := s.Slider("thickness")
158158
c.SetColor(color.White)
159159
c.SetLineWidth(thickness)
160160
c.DrawCircle(s.SketchWidth/2, s.SketchHeight/2, radius)

docs/sketch-configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Each sketch is accompanied by a JSON configuration file. Although everything cou
88
"SketchWidth": 800,
99
"SketchHeight": 800,
1010
"ControlWidth": 240,
11-
"Controls": [
11+
"Sliders": [
1212
{
1313
"Name": "control1",
1414
"MinVal": 1,
@@ -55,7 +55,7 @@ There are other parameters not listed in the template. Here are the missing para
5555

5656
# Referencing Sketch Parameters in Code
5757

58-
The Sketch parameters can be referenced like `s.<ParameterName>` in the `update` and `draw` functions. The value of each control can be referenced via `s.Var("<control name>")`, where `<control name>` is the name you used in the JSON configuration file.
58+
The Sketch parameters can be referenced like `s.<ParameterName>` in the `update` and `draw` functions. The value of each control can be referenced via `s.Slider("<control name>")`, where `<control name>` is the name you used in the JSON configuration file.
5959

6060

6161
# Command Line Parameters

examples/10print/main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ func (t *Truchet) flip(r int, c int) {
5858
}
5959

6060
func reset(s *sketchy.Sketch) {
61-
cellSize := s.Var("cellSize")
62-
board.rng.SetSeed(int64(s.Var("seed")))
63-
board.rng.SetNoiseOctaves(int(s.Var("octaves")))
64-
board.rng.SetNoisePersistence(s.Var("persistence"))
65-
board.rng.SetNoiseLacunarity(s.Var("lacunarity"))
66-
board.rng.SetNoiseScaleX(s.Var("xscale"))
67-
board.rng.SetNoiseScaleY(s.Var("yscale"))
68-
board.rng.SetNoiseOffsetX(s.Var("xoffset"))
69-
board.rng.SetNoiseOffsetY(s.Var("yoffset"))
61+
cellSize := s.Slider("cellSize")
62+
board.rng.SetSeed(int64(s.Slider("seed")))
63+
board.rng.SetNoiseOctaves(int(s.Slider("octaves")))
64+
board.rng.SetNoisePersistence(s.Slider("persistence"))
65+
board.rng.SetNoiseLacunarity(s.Slider("lacunarity"))
66+
board.rng.SetNoiseScaleX(s.Slider("xscale"))
67+
board.rng.SetNoiseScaleY(s.Slider("yscale"))
68+
board.rng.SetNoiseOffsetX(s.Slider("xoffset"))
69+
board.rng.SetNoiseOffsetY(s.Slider("yoffset"))
7070
board.init(int(s.SketchWidth/cellSize), int(s.SketchHeight/cellSize))
7171
}
7272

examples/10print/sketch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"SketchBackgroundColor": "#1e1e1e",
1010
"SketchOutlineColor": "#1e1e1e",
1111
"RandomSeed": 0,
12-
"Controls": [
12+
"Sliders": [
1313
{
1414
"Name": "cellSize",
1515
"Width": 226,

examples/lissajous/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
var lissa sketchy.Lissajous = sketchy.Lissajous{Nx: 3, Ny: 2}
1414

1515
func update(s *sketchy.Sketch) {
16-
lissa.Nx = int(s.Var("nx"))
17-
lissa.Ny = int(s.Var("ny"))
18-
lissa.Px += s.Var("phaseChange")
19-
lissa.Py = sketchy.Deg2Rad(s.Var("yphase"))
16+
lissa.Nx = int(s.Slider("nx"))
17+
lissa.Ny = int(s.Slider("ny"))
18+
lissa.Px += s.Slider("phaseChange")
19+
lissa.Py = sketchy.Deg2Rad(s.Slider("yphase"))
2020
}
2121

2222
func draw(s *sketchy.Sketch, c *gg.Context) {
23-
radius := s.Var("radius")
23+
radius := s.Slider("radius")
2424
origin := sketchy.Point{X: s.SketchWidth / 2, Y: s.SketchHeight / 2}
2525
curve := sketchy.GenLissajous(lissa, 1000, origin, radius)
2626
c.SetColor(color.CMYK{C: 200})

examples/lissajous/sketch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"SketchWidth": 1080,
33
"SketchHeight": 1080,
44
"ControlWidth": 240,
5-
"Controls": [
5+
"Sliders": [
66
{
77
"Name": "nx",
88
"MinVal": 1,

examples/noise/main.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@ import (
1313
var tick int64
1414

1515
func update(s *sketchy.Sketch) {
16-
s.Rand.SetSeed(int64(s.Var("seed")))
17-
s.Rand.SetNoiseOctaves(int(s.Var("octaves")))
18-
s.Rand.SetNoisePersistence(s.Var("persistence"))
19-
s.Rand.SetNoiseLacunarity(s.Var("lacunarity"))
20-
s.Rand.SetNoiseScaleX(s.Var("xscale"))
21-
s.Rand.SetNoiseScaleY(s.Var("yscale"))
22-
s.Rand.SetNoiseOffsetX(s.Var("xoffset"))
23-
s.Rand.SetNoiseOffsetY(s.Var("yoffset"))
16+
s.Rand.SetSeed(int64(s.Slider("seed")))
17+
s.Rand.SetNoiseOctaves(int(s.Slider("octaves")))
18+
s.Rand.SetNoisePersistence(s.Slider("persistence"))
19+
s.Rand.SetNoiseLacunarity(s.Slider("lacunarity"))
20+
s.Rand.SetNoiseScaleX(s.Slider("xscale"))
21+
s.Rand.SetNoiseScaleY(s.Slider("yscale"))
22+
s.Rand.SetNoiseOffsetX(s.Slider("xoffset"))
23+
s.Rand.SetNoiseOffsetY(s.Slider("yoffset"))
2424
s.Rand.SetNoiseScaleZ(0.005)
2525
s.Rand.SetNoiseOffsetZ(float64(tick))
26-
tick++
26+
if s.Checkbox("animate") {
27+
tick++
28+
}
2729
}
2830

2931
func draw(s *sketchy.Sketch, c *gg.Context) {
30-
cellSize := s.Var("cellSize")
32+
cellSize := s.Slider("cellSize")
3133
c.SetLineWidth(0)
3234
for x := 0.0; x < s.SketchWidth; x += cellSize {
3335
for y := 0.0; y < s.SketchHeight; y += cellSize {

examples/noise/sketch.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"SketchWidth": 1080,
33
"SketchHeight": 1080,
44
"ControlWidth": 240,
5-
"Controls": [
5+
"Sliders": [
66
{
77
"Name": "cellSize",
88
"MinVal": 1,
@@ -66,5 +66,11 @@
6666
"Val": 0,
6767
"Incr": 1
6868
}
69+
],
70+
"Checkboxes": [
71+
{
72+
"Name": "animate",
73+
"Checked": false
74+
}
6975
]
7076
}

examples/scale_rotate/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ func update(s *sketchy.Sketch) {
2020

2121
func draw(s *sketchy.Sketch, c *gg.Context) {
2222
// Drawing code goes here
23-
N := int(s.Var("N"))
24-
sides := int(s.Var("sides"))
25-
rotate := sketchy.Deg2Rad(s.Var("rotate"))
26-
scale := s.Var("scale")
23+
N := int(s.Slider("N"))
24+
sides := int(s.Slider("sides"))
25+
rotate := sketchy.Deg2Rad(s.Slider("rotate"))
26+
scale := s.Slider("scale")
2727
c.SetColor(color.CMYK{
2828
C: 200,
2929
M: 0,

0 commit comments

Comments
 (0)