Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions _examples/widget_demos/button/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func (g *game) Update() error {
// update the UI
g.ui.Update()
if inpututil.IsKeyJustPressed(ebiten.KeyB) {
g.btn.Submit()
g.btn.Click()
}

//Test that you can call Submit on the focused widget.
//Test that you can call Click on the focused widget.
if inpututil.IsKeyJustPressed(ebiten.KeyF) {
if btn, ok := g.ui.GetFocusedWidget().(*widget.Button); ok {
btn.Submit()
btn.Click()
}
}

Expand Down
55 changes: 23 additions & 32 deletions _examples/widget_demos/checkbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
"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"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)

// Game object used by ebiten
type game struct {
ui *ebitenui.UI
ui *ebitenui.UI
checkBox *widget.Checkbox
}

func main() {
game := game{}
// load images for button states: idle, hover, and pressed
buttonImage, _ := loadButtonImage()

// load button text font
//face, _ := loadFont(20)
// face, _ := loadFont(20)

// construct a new container that serves as the root of the UI hierarchy
rootContainer := widget.NewContainer(
Expand All @@ -41,32 +41,35 @@ func main() {
checkedImage := ebiten.NewImage(20, 20)
checkedImage.Fill(color.NRGBA{255, 255, 0, 255})

checkBox := widget.NewCheckbox(
game.checkBox = widget.NewCheckbox(
widget.CheckboxOpts.ButtonOpts(
widget.ButtonOpts.WidgetOpts(
//Set the location of the checkbox
// Set the location of the checkbox
widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter,
VerticalPosition: widget.AnchorLayoutPositionCenter,
}),
//Set the minimum size of the checkbox
// Set the minimum size of the checkbox
widget.WidgetOpts.MinSize(30, 30),
),
//Set the background images - idle, hover, pressed
// Set the background images - idle, hover, pressed
widget.ButtonOpts.Image(buttonImage),

// This disables space and enter triggering the checkbox
// widget.ButtonOpts.DisableDefaultKeys(),
),
//Set the check object images
// Set the check object images
widget.CheckboxOpts.Image(&widget.CheckboxGraphicImage{
//When the checkbox is unchecked
// When the checkbox is unchecked
Unchecked: &widget.ButtonImageImage{
Idle: uncheckedImage,
},
//When the checkbox is checked
// When the checkbox is checked
Checked: &widget.ButtonImageImage{
Idle: checkedImage,
},
}),
//Set the state change handler
// Set the state change handler
widget.CheckboxOpts.StateChangedHandler(func(args *widget.CheckboxChangedEventArgs) {
if args.State == widget.WidgetChecked {
fmt.Println("Checkbox is Checked")
Expand All @@ -75,21 +78,18 @@ func main() {
}
}),
)
rootContainer.AddChild(checkBox)

rootContainer.AddChild(game.checkBox)

// construct the UI
ui := ebitenui.UI{
game.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 {
Expand All @@ -106,6 +106,10 @@ func (g *game) Layout(outsideWidth int, outsideHeight int) (int, int) {
func (g *game) Update() error {
// update the UI
g.ui.Update()

if inpututil.IsKeyJustPressed(ebiten.KeyC) {
g.checkBox.Click()
}
return nil
}

Expand All @@ -128,16 +132,3 @@ func loadButtonImage() (*widget.ButtonImage, error) {
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
}
13 changes: 13 additions & 0 deletions _examples/widget_demos/combobox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ebitenui/ebitenui/widget"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
)
Expand Down Expand Up @@ -159,13 +160,25 @@ func (g *game) Layout(outsideWidth int, outsideHeight int) (int, int) {
func (g *game) Update() error {
// update the UI
g.ui.Update()

if list, ok := g.ui.GetFocusedWidget().(*widget.ListComboButton); ok {
//Test that you can call Click on the focused widget.
if inpututil.IsKeyJustPressed(ebiten.KeyW) {
list.FocusPrevious()
} else if inpututil.IsKeyJustPressed(ebiten.KeyS) {
list.FocusNext()
} else if inpututil.IsKeyJustPressed(ebiten.KeyB) {
list.SelectFocused()
}
}
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) {
Expand Down
73 changes: 41 additions & 32 deletions _examples/widget_demos/labeled_checkbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import (
"github.com/ebitenui/ebitenui/widget"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
)

// Game object used by ebiten
type game struct {
ui *ebitenui.UI
ui *ebitenui.UI
labeledCheckBox1 *widget.LabeledCheckbox
}

func main() {
g := game{}
// load images for button states: idle, hover, and pressed
buttonImage, _ := loadButtonImage()

Expand Down Expand Up @@ -47,37 +50,40 @@ func main() {
checkedImage := ebiten.NewImage(20, 20)
checkedImage.Fill(color.NRGBA{255, 255, 0, 255})

labeledCheckBox1 := widget.NewLabeledCheckbox(
//Set the labeled checkbox's position
g.labeledCheckBox1 = widget.NewLabeledCheckbox(
// Set the labeled checkbox's position
widget.LabeledCheckboxOpts.WidgetOpts(
//Set the location of the checkbox
// Set the location of the checkbox
widget.WidgetOpts.LayoutData(widget.RowLayoutData{
Position: widget.RowLayoutPositionCenter,
Stretch: false,
}),
),
//Set the checkbox Opts
// Set the checkbox Opts
widget.LabeledCheckboxOpts.CheckboxOpts(
widget.CheckboxOpts.ButtonOpts(
widget.ButtonOpts.WidgetOpts(
//Set the minimum size of the checkbox
widget.WidgetOpts.MinSize(30, 30),
),
//Set the background images - idle, hover, pressed
// Set the background images - idle, hover, pressed
widget.ButtonOpts.Image(buttonImage),

// This disables space and enter triggering the checkbox
// widget.ButtonOpts.DisableDefaultKeys(),
),
//Set the check object images
// Set the check object images
widget.CheckboxOpts.Image(&widget.CheckboxGraphicImage{
//When the checkbox is unchecked
// When the checkbox is unchecked
Unchecked: &widget.ButtonImageImage{
Idle: uncheckedImage,
},
//When the checkbox is checked
// When the checkbox is checked
Checked: &widget.ButtonImageImage{
Idle: checkedImage,
},
}),
//Set the state change handler
// Set the state change handler
widget.CheckboxOpts.StateChangedHandler(func(args *widget.CheckboxChangedEventArgs) {
if args.State == widget.WidgetChecked {
fmt.Println("Checkbox1 is Checked")
Expand All @@ -86,49 +92,49 @@ func main() {
}
}),
),
//Set the label
// Set the label
widget.LabeledCheckboxOpts.LabelOpts(widget.LabelOpts.Text("Labeled Checkbox1", face, &widget.LabelColor{
Idle: color.White,
Disabled: color.White,
})),
//Set the spacing between the label and the checkbox
// Set the spacing between the label and the checkbox
widget.LabeledCheckboxOpts.Spacing(15),
//Set the label to be before the checkbox.
//widget.LabeledCheckboxOpts.LabelFirst(),
// Set the label to be before the checkbox.
// widget.LabeledCheckboxOpts.LabelFirst(),
)
rootContainer.AddChild(labeledCheckBox1)
rootContainer.AddChild(g.labeledCheckBox1)

labeledCheckBox2 := widget.NewLabeledCheckbox(
//Set the labeled checkbox's position
// Set the labeled checkbox's position
widget.LabeledCheckboxOpts.WidgetOpts(
//Set the location of the checkbox
// Set the location of the checkbox
widget.WidgetOpts.LayoutData(widget.RowLayoutData{
Position: widget.RowLayoutPositionCenter,
Stretch: false,
}),
),
//Set the checkbox Opts
// Set the checkbox Opts
widget.LabeledCheckboxOpts.CheckboxOpts(
widget.CheckboxOpts.ButtonOpts(
widget.ButtonOpts.WidgetOpts(
//Set the minimum size of the checkbox
// Set the minimum size of the checkbox
widget.WidgetOpts.MinSize(30, 30),
),
//Set the background images - idle, hover, pressed
// Set the background images - idle, hover, pressed
widget.ButtonOpts.Image(buttonImage),
),
//Set the check object images
// Set the check object images
widget.CheckboxOpts.Image(&widget.CheckboxGraphicImage{
//When the checkbox is unchecked
// When the checkbox is unchecked
Unchecked: &widget.ButtonImageImage{
Idle: uncheckedImage,
},
//When the checkbox is checked
// When the checkbox is checked
Checked: &widget.ButtonImageImage{
Idle: checkedImage,
},
}),
//Set the state change handler
// Set the state change handler
widget.CheckboxOpts.StateChangedHandler(func(args *widget.CheckboxChangedEventArgs) {
if args.State == widget.WidgetChecked {
fmt.Println("Checkbox2 is Checked")
Expand All @@ -137,17 +143,17 @@ func main() {
}
}),
),
//Set the label
// Set the label
widget.LabeledCheckboxOpts.LabelOpts(widget.LabelOpts.Text("Labeled Checkbox2", face, &widget.LabelColor{
Idle: color.White,
Disabled: color.White,
})),
//Set the spacing between the label and the checkbox
// Set the spacing between the label and the checkbox
widget.LabeledCheckboxOpts.Spacing(15),
//Set the label to be before the checkbox.
// Set the label to be before the checkbox.
widget.LabeledCheckboxOpts.LabelFirst(),
)
//Set this checkbox as Checked by default
// Set this checkbox as Checked by default
labeledCheckBox2.SetState(widget.WidgetChecked)

rootContainer.AddChild(labeledCheckBox2)
Expand All @@ -160,12 +166,10 @@ func main() {
ebiten.SetWindowSize(400, 400)
ebiten.SetWindowTitle("Ebiten UI - Labeled Checkbox")

game := game{
ui: &ui,
}
g.ui = &ui

// run Ebiten main loop
err := ebiten.RunGame(&game)
err := ebiten.RunGame(&g)
if err != nil {
log.Println(err)
}
Expand All @@ -180,6 +184,11 @@ func (g *game) Layout(outsideWidth int, outsideHeight int) (int, int) {
func (g *game) Update() error {
// update the UI
g.ui.Update()

if inpututil.IsKeyJustPressed(ebiten.KeyC) {
g.labeledCheckBox1.Click()
}

return nil
}

Expand Down
Loading