Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit e697c74

Browse files
authored
Support mouse click (#68)
1 parent e285184 commit e697c74

File tree

9 files changed

+114
-45
lines changed

9 files changed

+114
-45
lines changed

graphics/window.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphics
22

33
import (
4+
"image"
45
"time"
56

67
"candy/input"
@@ -130,9 +131,11 @@ func (e EbitenWindow) pollEvents() []input.Input {
130131
})
131132
}
132133
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
134+
x, y := ebiten.CursorPosition()
133135
inputs = append(inputs, input.Input{
134-
Action: input.SinglePress,
135-
Device: input.MouseLeftButton,
136+
Action: input.SinglePress,
137+
Device: input.MouseLeftButton,
138+
CursorPosition: image.Point{X: x, Y: y},
136139
})
137140
}
138141
return inputs

input/input.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package input
22

33
import (
44
"fmt"
5+
"image"
56
)
67

78
type Input struct {
8-
Action action
9-
Device device
9+
Action action
10+
Device device
11+
CursorPosition image.Point
1012
}
1113

1214
func (in Input) String() string {

ui/box.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"candy/ui/ptr"
99
)
1010

11+
type BoxProps struct {
12+
OnClick onClickHandler
13+
}
14+
1115
var _ Component = (*Box)(nil)
1216

1317
type Box struct {
@@ -65,7 +69,10 @@ func (b Box) ComputeLeafSize(_ Constraints) Size {
6569
return Size{width: width, height: height}
6670
}
6771

68-
func NewBox(children []Component, style *Style) *Box {
72+
func NewBox(pros *BoxProps, children []Component, style *Style) *Box {
73+
if pros == nil {
74+
pros = &BoxProps{}
75+
}
6976
if style == nil {
7077
style = &Style{
7178
LayoutType: (*LayoutType)(ptr.Int(int(BoxLayoutType))),
@@ -79,10 +86,12 @@ func NewBox(children []Component, style *Style) *Box {
7986
}
8087
return &Box{
8188
SharedComponent: SharedComponent{
89+
name: "Box",
8290
layout: newLayout(*style.LayoutType),
8391
style: *style,
8492
children: children,
8593
childrenOffset: []Offset{},
94+
events: Events{onClick: pros.OnClick},
8695
}}
8796
}
8897

ui/button.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
type ButtonProps struct {
10-
Text *string
10+
Text *string
11+
OnClick onClickHandler
1112
}
1213

1314
func (b ButtonProps) getText() string {
@@ -24,11 +25,7 @@ type Button struct {
2425
SharedComponent
2526
}
2627

27-
func (b Button) GetName() string {
28-
return "Button"
29-
}
30-
31-
func (b Button) Paint(painter *Painter, destLayer draw.Image, offset Offset) {
28+
func (b *Button) Paint(painter *Painter, destLayer draw.Image, offset Offset) {
3229
b.children[0].Paint(painter, destLayer, offset)
3330
}
3431

@@ -62,11 +59,17 @@ func NewButton(props *ButtonProps, style *Style) *Button {
6259
}
6360
return &Button{
6461
SharedComponent: SharedComponent{
62+
name: "Button",
6563
layout: newLayout(InlineLayoutType),
6664
children: []Component{
67-
NewBox([]Component{
68-
NewText(&TextProps{Text: props.getText()}, style),
69-
}, style),
65+
NewBox(
66+
&BoxProps{
67+
OnClick: props.OnClick,
68+
}, []Component{
69+
NewText(&TextProps{Text: props.getText()}, style),
70+
},
71+
style,
72+
),
7073
},
7174
childrenOffset: make([]Offset, 0),
7275
},

ui/component.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ui
22

33
import (
4+
"image"
45
"image/draw"
56
"time"
67

@@ -10,6 +11,7 @@ import (
1011
type Component interface {
1112
GetName() string
1213
HandleInput(in input.Input)
14+
handleInput(in input.Input, offset Offset)
1315
Update(timeElapsed time.Duration)
1416
getLayout() layout
1517
getChildren() []Component
@@ -34,15 +36,42 @@ type Offset struct {
3436
}
3537

3638
type SharedComponent struct {
39+
name string
3740
layout layout
3841
style Style
3942
size Size
4043
childrenOffset []Offset
4144
children []Component
45+
events Events
4246
}
4347

44-
func (s SharedComponent) HandleInput(_ input.Input) {
45-
return
48+
func (s *SharedComponent) HandleInput(in input.Input) {
49+
s.handleInput(in, Offset{
50+
x: 0,
51+
y: 0,
52+
z: 0,
53+
})
54+
}
55+
56+
func (s *SharedComponent) handleInput(in input.Input, offset Offset) {
57+
for index, child := range s.children {
58+
child.handleInput(in, s.childrenOffset[index])
59+
}
60+
61+
switch in.Action {
62+
case input.SinglePress:
63+
switch in.Device {
64+
case input.MouseLeftButton:
65+
rect := s.BoundingBox(offset)
66+
if in.CursorPosition.In(rect) {
67+
s.events.tryOnClick()
68+
}
69+
}
70+
}
71+
}
72+
73+
func (s *SharedComponent) BoundingBox(offset Offset) image.Rectangle {
74+
return image.Rect(offset.x, offset.y, offset.x+s.size.width, offset.y+s.size.height)
4675
}
4776

4877
func (s SharedComponent) Update(timeElapsed time.Duration) {
@@ -51,6 +80,10 @@ func (s SharedComponent) Update(timeElapsed time.Duration) {
5180
}
5281
}
5382

83+
func (s SharedComponent) GetName() string {
84+
return s.name
85+
}
86+
5487
func (s SharedComponent) getChildren() []Component {
5588
return s.children
5689
}

ui/event.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ui
2+
3+
type onClickHandler func()
4+
5+
type Events struct {
6+
onClick onClickHandler
7+
}
8+
9+
func (e *Events) tryOnClick() {
10+
if e.onClick == nil {
11+
return
12+
}
13+
e.onClick()
14+
}

ui/example/app.go

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"candy/assets"
57
"candy/ui"
68
"candy/ui/ptr"
@@ -13,12 +15,21 @@ type app struct {
1315
}
1416

1517
func newApp(assets *assets.Assets) *app {
16-
return &app{ui.NewBox([]ui.Component{
17-
ui.NewButton(
18-
&ui.ButtonProps{Text: ptr.String("Click")},
19-
nil,
20-
),
21-
ui.NewText(&ui.TextProps{Text: `
18+
return &app{ui.NewBox(
19+
&ui.BoxProps{OnClick: func() {
20+
fmt.Println("Box clicked")
21+
}},
22+
[]ui.Component{
23+
ui.NewButton(
24+
&ui.ButtonProps{
25+
Text: ptr.String("Click"),
26+
OnClick: func() {
27+
fmt.Println("Button clicked")
28+
},
29+
},
30+
nil,
31+
),
32+
ui.NewText(&ui.TextProps{Text: `
2233
I guess we could discuss the implications of the phrase "meant to be."
2334
That is if we wanted to drown ourselves in a sea of backwardly referential
2435
semantics and other mumbo-jumbo. Maybe such a discussion would result in the
@@ -27,20 +38,20 @@ seems to be, and that none of us is actually meant to be doing anything at all.
2738
But that's my existential underpants underpinnings showing. It's the way the
2839
cookie crumbles. And now I want a cookie.
2940
`}, &ui.Style{FontStyle: ui.FontStyle{
30-
Family: ptr.String("Source Code Pro"),
31-
Weight: ptr.String("ExtraLight"),
32-
Italic: ptr.Bool(false),
33-
Size: ptr.Int(20),
34-
LineHeight: ptr.Int(24),
35-
Color: &ui.Color{
36-
Red: 255,
37-
Green: 255,
38-
Blue: 255,
39-
Alpha: 255,
40-
}}}),
41-
ui.NewImage(assets, &ui.ImageProps{ImagePath: "test/image3.png"}, nil),
42-
ui.NewImage(assets, &ui.ImageProps{ImagePath: "test/image1.jpg"}, nil),
43-
ui.NewImage(assets, &ui.ImageProps{ImagePath: "test/image2.jpg"}, nil),
44-
},
41+
Family: ptr.String("Source Code Pro"),
42+
Weight: ptr.String("ExtraLight"),
43+
Italic: ptr.Bool(false),
44+
Size: ptr.Int(20),
45+
LineHeight: ptr.Int(24),
46+
Color: &ui.Color{
47+
Red: 255,
48+
Green: 255,
49+
Blue: 255,
50+
Alpha: 255,
51+
}}}),
52+
ui.NewImage(assets, &ui.ImageProps{ImagePath: "test/image3.png"}, nil),
53+
ui.NewImage(assets, &ui.ImageProps{ImagePath: "test/image1.jpg"}, nil),
54+
ui.NewImage(assets, &ui.ImageProps{ImagePath: "test/image2.jpg"}, nil),
55+
},
4556
nil)}
4657
}

ui/image.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ type Image struct {
2020
image image.Image
2121
}
2222

23-
func (i *Image) GetName() string {
24-
return "Image"
25-
}
26-
2723
func (i *Image) Paint(painter *Painter, destLayer draw.Image, offset Offset) {
2824
if i.image == nil {
2925
return
@@ -79,6 +75,7 @@ func NewImage(assets *assets.Assets, props *ImageProps, style *Style) *Image {
7975
assets: assets,
8076
image: img,
8177
SharedComponent: SharedComponent{
78+
name: "Image",
8279
style: *style,
8380
},
8481
}

ui/text.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ type Text struct {
3131
shouldUpdate bool
3232
}
3333

34-
func (t *Text) GetName() string {
35-
return "Text"
36-
}
37-
3834
func (t *Text) ComputeLeafSize(constraints Constraints) Size {
3935
if len(t.props.Text) == 0 || t.fontFace == nil {
4036
return Size{width: 0, height: 0}
@@ -169,6 +165,7 @@ func NewText(props *TextProps, style *Style) *Text {
169165
return &Text{
170166
shouldUpdate: true,
171167
SharedComponent: SharedComponent{
168+
name: "Text",
172169
style: *style,
173170
},
174171
props: *props,

0 commit comments

Comments
 (0)