examples/ui: Use event-handler way

This commit is contained in:
Hajime Hoshi 2018-01-31 00:45:42 +09:00
parent 8a26841a88
commit a2225abcf9

View File

@ -172,11 +172,11 @@ type Button struct {
Text string Text string
mouseDown bool mouseDown bool
pressed bool
onPressed func(b *Button)
} }
func (b *Button) Update() { func (b *Button) Update() {
b.pressed = false
if theInput.IsMouseButtonPressed() { if theInput.IsMouseButtonPressed() {
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
if b.Rect.Min.X <= x && x < b.Rect.Max.X && b.Rect.Min.Y <= y && y < b.Rect.Max.Y { if b.Rect.Min.X <= x && x < b.Rect.Max.X && b.Rect.Min.Y <= y && y < b.Rect.Max.Y {
@ -186,7 +186,9 @@ func (b *Button) Update() {
} }
} else { } else {
if b.mouseDown { if b.mouseDown {
b.pressed = true if b.onPressed != nil {
b.onPressed(b)
}
} }
b.mouseDown = false b.mouseDown = false
} }
@ -206,8 +208,8 @@ func (b *Button) Draw(dst *ebiten.Image) {
text.Draw(dst, b.Text, uiFont, x, y, color.Black) text.Draw(dst, b.Text, uiFont, x, y, color.Black)
} }
func (b *Button) Pressed() bool { func (b *Button) SetOnPressed(f func(b *Button)) {
return b.pressed b.onPressed = f
} }
const VScrollBarWidth = 16 const VScrollBarWidth = 16
@ -395,9 +397,10 @@ type CheckBox struct {
Y int Y int
Text string Text string
checked bool checked bool
mouseDown bool mouseDown bool
checkChanged bool
onCheckChanged func(c *CheckBox)
} }
func (c *CheckBox) width() int { func (c *CheckBox) width() int {
@ -407,7 +410,6 @@ func (c *CheckBox) width() int {
} }
func (c *CheckBox) Update() { func (c *CheckBox) Update() {
c.checkChanged = false
if theInput.IsMouseButtonPressed() { if theInput.IsMouseButtonPressed() {
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
if c.X <= x && x < c.X+c.width() && c.Y <= y && y < c.Y+checkboxHeight { if c.X <= x && x < c.X+c.width() && c.Y <= y && y < c.Y+checkboxHeight {
@ -417,13 +419,13 @@ func (c *CheckBox) Update() {
} }
} else { } else {
if c.mouseDown { if c.mouseDown {
c.checkChanged = true c.checked = !c.checked
if c.onCheckChanged != nil {
c.onCheckChanged(c)
}
} }
c.mouseDown = false c.mouseDown = false
} }
if c.checkChanged {
c.checked = !c.checked
}
} }
func (c *CheckBox) Draw(dst *ebiten.Image) { func (c *CheckBox) Draw(dst *ebiten.Image) {
@ -446,8 +448,8 @@ func (c *CheckBox) Checked() bool {
return c.checked return c.checked
} }
func (c *CheckBox) CheckChanged() bool { func (c *CheckBox) SetOnCheckChanged(f func(c *CheckBox)) {
return c.checkChanged c.onCheckChanged = f
} }
var ( var (
@ -469,6 +471,24 @@ var (
} }
) )
func init() {
button1.SetOnPressed(func(b *Button) {
textBoxLog.AppendLine("Button 1 Pressed")
})
button2.SetOnPressed(func(b *Button) {
textBoxLog.AppendLine("Button 2 Pressed")
})
checkBox.SetOnCheckChanged(func(c *CheckBox) {
msg := "Check box check changed"
if c.Checked() {
msg += " (Checked)"
} else {
msg += " (Unchecked)"
}
textBoxLog.AppendLine(msg)
})
}
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
theInput.Update() theInput.Update()
@ -477,22 +497,6 @@ func update(screen *ebiten.Image) error {
checkBox.Update() checkBox.Update()
textBoxLog.Update() textBoxLog.Update()
if button1.Pressed() {
textBoxLog.AppendLine("Button 1 Pressed")
}
if button2.Pressed() {
textBoxLog.AppendLine("Button 2 Pressed")
}
if checkBox.CheckChanged() {
msg := "Check box check changed"
if checkBox.Checked() {
msg += " (Checked)"
} else {
msg += " (Unchecked)"
}
textBoxLog.AppendLine(msg)
}
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }