Skip to content

Commit af4e5ff

Browse files
committed
updated example
1 parent 71c6566 commit af4e5ff

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

controls.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
)
1111

1212
const (
13-
sliderHeight = 10.0
14-
sliderHPadding = 5.0
15-
sliderVPadding = 2.0
16-
sliderMouseWheelThreshold = 0.5
13+
SliderHeight = 10.0
14+
SliderHPadding = 5.0
15+
SliderVPadding = 2.0
16+
SliderMouseWheelThreshold = 0.5
1717
)
1818

1919
var sliderOutline color.Color = color.Black
@@ -36,10 +36,10 @@ func (s *Slider) GetPercentage() float64 {
3636
}
3737

3838
func (s *Slider) GetRect(ctx *gg.Context) Rect {
39-
x := s.Pos.X - sliderHPadding
40-
y := s.Pos.Y - sliderVPadding - ctx.FontHeight()
41-
w := s.Width + 2*sliderHPadding
42-
h := s.Height + ctx.FontHeight() + sliderVPadding
39+
x := s.Pos.X - SliderHPadding
40+
y := s.Pos.Y - SliderVPadding - ctx.FontHeight()
41+
w := s.Width + 2*SliderHPadding
42+
h := s.Height + ctx.FontHeight() + SliderVPadding
4343
return Rect{X: x, Y: y, W: w, H: h}
4444
}
4545

@@ -49,7 +49,7 @@ func (s *Slider) AutoHeight(ctx *gg.Context) {
4949

5050
func (s *Slider) IsInside(x float64, y float64) bool {
5151
return x >= s.Pos.X &&
52-
x <= s.Pos.X+s.Width && y >= s.Pos.Y && y <= s.Pos.Y+sliderHeight
52+
x <= s.Pos.X+s.Width && y >= s.Pos.Y && y <= s.Pos.Y+SliderHeight
5353
}
5454

5555
func (s *Slider) Update(x float64) {
@@ -67,7 +67,7 @@ func (s *Slider) CheckAndUpdate() error {
6767
} else {
6868
if s.IsInside(float64(x), float64(y)) {
6969
_, dy := ebiten.Wheel()
70-
if math.Abs(dy) > sliderMouseWheelThreshold {
70+
if math.Abs(dy) > SliderMouseWheelThreshold {
7171
if dy < 0 {
7272
s.Val -= s.Incr
7373
} else {
@@ -82,22 +82,22 @@ func (s *Slider) CheckAndUpdate() error {
8282

8383
func (s *Slider) Draw(ctx *gg.Context) {
8484
ctx.SetColor(sliderBackground)
85-
ctx.DrawRectangle(s.Pos.X, s.Pos.Y, s.Width, sliderHeight)
85+
ctx.DrawRectangle(s.Pos.X, s.Pos.Y, s.Width, SliderHeight)
8686
ctx.Fill()
8787
ctx.SetColor(sliderFill)
88-
ctx.DrawRectangle(s.Pos.X, s.Pos.Y, s.Width*s.GetPercentage(), sliderHeight)
88+
ctx.DrawRectangle(s.Pos.X, s.Pos.Y, s.Width*s.GetPercentage(), SliderHeight)
8989
ctx.Fill()
9090
ctx.SetColor(sliderOutline)
91-
ctx.DrawRectangle(s.Pos.X, s.Pos.Y, s.Width, sliderHeight)
91+
ctx.DrawRectangle(s.Pos.X, s.Pos.Y, s.Width, SliderHeight)
9292
ctx.Stroke()
9393
digits := 0
9494
if s.Incr < 1 {
9595
digits = int(math.Ceil(math.Abs(math.Log10(s.Incr))))
9696
}
97-
ctx.DrawStringWrapped(s.Name, s.Pos.X, s.Pos.Y-ctx.FontHeight()-sliderVPadding, 0, 0, s.Width, 1, gg.AlignLeft)
97+
ctx.DrawStringWrapped(s.Name, s.Pos.X, s.Pos.Y-ctx.FontHeight()-SliderVPadding, 0, 0, s.Width, 1, gg.AlignLeft)
9898
ctx.DrawStringWrapped(
9999
strconv.FormatFloat(s.Val, 'f', digits, 64),
100-
s.Pos.X, s.Pos.Y-ctx.FontHeight()-sliderVPadding,
100+
s.Pos.X, s.Pos.Y-ctx.FontHeight()-SliderVPadding,
101101
0, 0, s.Width, 1, gg.AlignRight)
102102
}
103103

example/config.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"SketchWidth": 1080,
3+
"SketchHeight": 1080,
4+
"ControlWidth": 200,
5+
"Controls": [
6+
{
7+
"Name": "slider1",
8+
"MinVal": 0,
9+
"MaxVal": 100,
10+
"Val": 25,
11+
"Incr": 1
12+
},
13+
{
14+
"Name": "slider2",
15+
"MinVal": 0,
16+
"MaxVal": 360,
17+
"Val": 180,
18+
"Incr": 1
19+
},
20+
{
21+
"Name": "slider3",
22+
"MinVal": 0,
23+
"MaxVal": 1,
24+
"Val": 0.3,
25+
"Incr": 0.01
26+
}
27+
]
28+
}

example/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55

66
"github.com/aldernero/sketchy"
7+
"github.com/fogleman/gg"
78
"github.com/hajimehoshi/ebiten/v2"
89
)
910

@@ -12,6 +13,8 @@ func main() {
1213
if err != nil {
1314
log.Fatal(err)
1415
}
16+
ctx := gg.NewContext(int(s.ControlWidth), int(s.SketchHeight))
17+
s.PlaceControls(s.ControlWidth, s.SketchHeight, ctx)
1518
ebiten.SetWindowSize(int(s.ControlWidth+s.SketchWidth), int(s.SketchHeight))
1619
ebiten.SetWindowTitle("Sketchy Sketch")
1720
ebiten.SetWindowResizable(false)

sketch.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Sketch struct {
1717
Controls []Slider
1818
}
1919

20-
func NewSketchFromFile(fname string) (Sketch, error) {
20+
func NewSketchFromFile(fname string) (*Sketch, error) {
2121
jsonFile, err := os.Open(fname)
2222
if err != nil {
2323
log.Fatal(err)
@@ -27,29 +27,29 @@ func NewSketchFromFile(fname string) (Sketch, error) {
2727
if err = parser.Decode(&sketch); err != nil {
2828
log.Fatal(err)
2929
}
30-
return sketch, nil
30+
return &sketch, nil
3131
}
3232

3333
func (s *Sketch) UpdateControls() {
34-
for _, c := range s.Controls {
35-
c.CheckAndUpdate()
34+
for i := range s.Controls {
35+
s.Controls[i].CheckAndUpdate()
3636
}
3737
}
3838

3939
func (s *Sketch) PlaceControls(w float64, h float64, ctx *gg.Context) {
40-
for i, c := range s.Controls {
41-
c.AutoHeight(ctx)
42-
c.Width = s.ControlWidth - 2*sliderHPadding
43-
c.Pos = Point{
44-
X: sliderHPadding,
45-
Y: 2*float64(i)*c.Height + c.Height,
40+
for i := range s.Controls {
41+
s.Controls[i].AutoHeight(ctx)
42+
s.Controls[i].Width = s.ControlWidth - 2*SliderHPadding
43+
s.Controls[i].Pos = Point{
44+
X: SliderHPadding,
45+
Y: 2*float64(i)*s.Controls[i].Height + s.Controls[i].Height,
4646
}
4747
}
4848
}
4949

5050
func (s *Sketch) DrawControls(ctx *gg.Context) {
51-
for _, c := range s.Controls {
52-
c.Draw(ctx)
51+
for i := range s.Controls {
52+
s.Controls[i].Draw(ctx)
5353
}
5454
}
5555

@@ -71,5 +71,6 @@ func (s *Sketch) Draw(screen *ebiten.Image) {
7171
cc.SetColor(color.White)
7272
cc.Fill()
7373
s.DrawControls(cc)
74+
screen.DrawImage(ebiten.NewImageFromImage(cc.Image()), nil)
7475
// Custom draw code goes here
7576
}

0 commit comments

Comments
 (0)