-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add pointer.Buttons to MouseClick events #25
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ type Click struct { | |
clicks int | ||
// pressed tracks whether the pointer is pressed. | ||
pressed bool | ||
// Buttons tracks which buttons are pressed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem quite right. Did you mean // Buttons specify the set of buttons to track. The zero value is equivalent to the ? Ah, I see from the rest of your change that I wasn't clear enough in my description. What I meant by the Buttons field is that it should be set by the user from Click, not Click itself. The purpose of (the zero value of) Buttons is maintain the old behaviour where only the primary mouse button would be tracked. Users such as yourself that need to track other buttons can include them in Buttons. |
||
Buttons pointer.Buttons | ||
// entered tracks whether the pointer is inside the gesture. | ||
entered bool | ||
// pid is the pointer.ID. | ||
|
@@ -53,6 +55,7 @@ type ClickEvent struct { | |
Position f32.Point | ||
Source pointer.Source | ||
Modifiers key.Modifiers | ||
Buttons pointer.Buttons | ||
// NumClicks records successive clicks occurring | ||
// within a short duration of each other. | ||
NumClicks int | ||
|
@@ -131,7 +134,7 @@ func (c *Click) Hovered() bool { | |
return c.entered | ||
} | ||
|
||
// Pressed returns whether a pointer is pressing. | ||
// Pressed returns whether a pointer is pressing the left mouse button. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "primary mouse button". Sorry for renaming the buttons while you're working on this change :) |
||
func (c *Click) Pressed() bool { | ||
return c.pressed | ||
} | ||
|
@@ -146,33 +149,34 @@ func (c *Click) Events(q event.Queue) []ClickEvent { | |
} | ||
switch e.Type { | ||
case pointer.Release: | ||
if !c.pressed || c.pid != e.PointerID { | ||
if c.pid != e.PointerID { | ||
break | ||
} | ||
c.pressed = false | ||
b := c.Buttons ^ e.Buttons // the released button | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean "buttons" in plural? Use full comment sentences: // The released buttons. |
||
c.pressed = e.Buttons&pointer.ButtonPrimary > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use "!=" for bitmask operations. A ">" looks like an ordered operation. |
||
c.Buttons = e.Buttons | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Click shouldn't update its Buttons field. See my comment on the field above. |
||
if c.entered { | ||
if e.Time-c.clickedAt < doubleClickDuration { | ||
c.clicks++ | ||
} else { | ||
c.clicks = 1 | ||
} | ||
c.clickedAt = e.Time | ||
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks}) | ||
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source, Buttons: b, Modifiers: e.Modifiers, NumClicks: c.clicks}) | ||
} else { | ||
events = append(events, ClickEvent{Type: TypeCancel}) | ||
events = append(events, ClickEvent{Type: TypeCancel, Buttons: b}) | ||
} | ||
case pointer.Cancel: | ||
wasPressed := c.pressed | ||
c.pressed = false | ||
b := e.Buttons ^ c.Buttons | ||
wasPressed := c.Buttons.Contain(b) | ||
c.pressed = e.Buttons&pointer.ButtonPrimary > 0 | ||
c.Buttons = e.Buttons | ||
c.entered = false | ||
if wasPressed { | ||
events = append(events, ClickEvent{Type: TypeCancel}) | ||
events = append(events, ClickEvent{Type: TypeCancel, Buttons: b}) | ||
} | ||
case pointer.Press: | ||
if c.pressed { | ||
break | ||
} | ||
if e.Source == pointer.Mouse && e.Buttons != pointer.ButtonPrimary { | ||
if e.Source == pointer.Mouse && e.Buttons == c.Buttons { | ||
break | ||
} | ||
if !c.entered { | ||
|
@@ -181,17 +185,17 @@ func (c *Click) Events(q event.Queue) []ClickEvent { | |
if c.pid != e.PointerID { | ||
break | ||
} | ||
c.pressed = true | ||
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers}) | ||
c.pressed = e.Buttons&pointer.ButtonPrimary > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. != |
||
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Buttons: e.Buttons, Modifiers: e.Modifiers}) | ||
case pointer.Leave: | ||
if !c.pressed { | ||
if !c.Buttons.Contain(c.Buttons ^ e.Buttons) { | ||
c.pid = e.PointerID | ||
} | ||
if c.pid == e.PointerID { | ||
c.entered = false | ||
} | ||
case pointer.Enter: | ||
if !c.pressed { | ||
if !c.Buttons.Contain(c.Buttons ^ e.Buttons) { | ||
c.pid = e.PointerID | ||
} | ||
if c.pid == e.PointerID { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this change to something like
// pressed tracks whether one or more buttons from the Buttons set is pressed.
?