Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 6b7ccd9

Browse files
committed
all: add handling of left/middle/right mouse buttons
1 parent 79c7b9e commit 6b7ccd9

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (gctx *gcontext) run(setup, draw Func) error {
9191
Event.Mouse.Position.X = float64(e.Position.X)
9292
Event.Mouse.Position.Y = float64(e.Position.Y)
9393
}
94+
Event.Mouse.Buttons = Buttons(e.Buttons)
9495

9596
case system.FrameEvent:
9697
gctx.draw(e, draw)

event.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,21 @@ var Event struct {
1616
X float64
1717
Y float64
1818
}
19+
Buttons Buttons
1920
}
2021
}
22+
23+
// Buttons is a set of mouse buttons.
24+
type Buttons uint8
25+
26+
// Contain reports whether the set b contains
27+
// all of the buttons.
28+
func (b Buttons) Contain(buttons Buttons) bool {
29+
return b&buttons == buttons
30+
}
31+
32+
const (
33+
ButtonLeft Buttons = 1 << iota
34+
ButtonRight
35+
ButtonMiddle
36+
)

example/mouse-pressed/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ func setup() {
2222
func draw() {
2323
switch {
2424
case p5.Event.Mouse.Pressed:
25-
p5.Fill(color.RGBA{R: 255, A: 255})
25+
if p5.Event.Mouse.Buttons.Contain(p5.ButtonLeft) {
26+
p5.Fill(color.RGBA{R: 255, A: 255})
27+
}
2628
default:
2729
p5.Fill(color.Transparent)
2830
}

0 commit comments

Comments
 (0)