-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.go
143 lines (120 loc) · 3.5 KB
/
main.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
package main
import (
"fmt"
"image/color"
"log"
"github.com/ebitenui/ebitenui"
"github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
)
// Game object used by ebiten
type game struct {
ui *ebitenui.UI
}
func main() {
// load images for button states: idle, hover, and pressed
buttonImage, _ := loadButtonImage()
// load button text font
//face, _ := loadFont(20)
// construct a new container that serves as the root of the UI hierarchy
rootContainer := widget.NewContainer(
// the container will use a plain color as its background
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(color.NRGBA{0x13, 0x1a, 0x22, 0xff})),
// the container will use an anchor layout to layout its single child widget
widget.ContainerOpts.Layout(widget.NewAnchorLayout()),
)
uncheckedImage := ebiten.NewImage(20, 20)
uncheckedImage.Fill(color.White)
checkedImage := ebiten.NewImage(20, 20)
checkedImage.Fill(color.NRGBA{255, 255, 0, 255})
checkBox := widget.NewCheckbox(
widget.CheckboxOpts.ButtonOpts(
widget.ButtonOpts.WidgetOpts(
//Set the location of the checkbox
widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter,
VerticalPosition: widget.AnchorLayoutPositionCenter,
}),
//Set the minimum size of the checkbox
widget.WidgetOpts.MinSize(30, 30),
),
//Set the background images - idle, hover, pressed
widget.ButtonOpts.Image(buttonImage),
),
//Set the check object images
widget.CheckboxOpts.Image(&widget.CheckboxGraphicImage{
//When the checkbox is unchecked
Unchecked: &widget.ButtonImageImage{
Idle: uncheckedImage,
},
//When the checkbox is checked
Checked: &widget.ButtonImageImage{
Idle: checkedImage,
},
}),
//Set the state change handler
widget.CheckboxOpts.StateChangedHandler(func(args *widget.CheckboxChangedEventArgs) {
if args.State == widget.WidgetChecked {
fmt.Println("Checkbox is Checked")
} else {
fmt.Println("Checkbox is Unchecked")
}
}),
)
rootContainer.AddChild(checkBox)
// construct the UI
ui := ebitenui.UI{
Container: rootContainer,
}
// Ebiten setup
ebiten.SetWindowSize(400, 400)
ebiten.SetWindowTitle("Ebiten UI - Checkbox")
game := game{
ui: &ui,
}
// run Ebiten main loop
err := ebiten.RunGame(&game)
if err != nil {
log.Println(err)
}
}
// Layout implements Game.
func (g *game) Layout(outsideWidth int, outsideHeight int) (int, int) {
return outsideWidth, outsideHeight
}
// Update implements Game.
func (g *game) Update() error {
// update the UI
g.ui.Update()
return nil
}
// Draw implements Ebiten's Draw method.
func (g *game) Draw(screen *ebiten.Image) {
// draw the UI onto the screen
g.ui.Draw(screen)
}
func loadButtonImage() (*widget.ButtonImage, error) {
idle := image.NewNineSliceColor(color.NRGBA{R: 170, G: 170, B: 180, A: 255})
hover := image.NewNineSliceColor(color.NRGBA{R: 130, G: 130, B: 150, A: 255})
pressed := image.NewNineSliceColor(color.NRGBA{R: 100, G: 100, B: 120, A: 255})
return &widget.ButtonImage{
Idle: idle,
Hover: hover,
Pressed: pressed,
}, nil
}
func loadFont(size float64) (font.Face, error) {
ttfFont, err := truetype.Parse(goregular.TTF)
if err != nil {
return nil, err
}
return truetype.NewFace(ttfFont, &truetype.Options{
Size: size,
DPI: 72,
Hinting: font.HintingFull,
}), nil
}