mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
gesture,widget,f32: [API] use integer coordinates for gesture coordinates
Most widget code operate in integer coordinates. This change makes gesture pointer coordinates integer, to lessen the number of float32 to int conversions. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -81,6 +81,14 @@ func (p Point) In(r Rectangle) bool {
|
||||
r.Min.Y <= p.Y && p.Y < r.Max.Y
|
||||
}
|
||||
|
||||
// Round returns the integer point closest to p.
|
||||
func (p Point) Round() image.Point {
|
||||
return image.Point{
|
||||
X: int(math.Round(float64(p.X))),
|
||||
Y: int(math.Round(float64(p.Y))),
|
||||
}
|
||||
}
|
||||
|
||||
// Size returns r's width and height.
|
||||
func (r Rectangle) Size() Point {
|
||||
return Point{X: r.Dx(), Y: r.Dy()}
|
||||
|
||||
+3
-3
@@ -91,7 +91,7 @@ type Click struct {
|
||||
// TypeClick for a completed click.
|
||||
type ClickEvent struct {
|
||||
Type ClickType
|
||||
Position f32.Point
|
||||
Position image.Point
|
||||
Source pointer.Source
|
||||
Modifiers key.Modifiers
|
||||
// NumClicks records successive clicks occurring
|
||||
@@ -198,7 +198,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
|
||||
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.Round(), Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks})
|
||||
} else {
|
||||
events = append(events, ClickEvent{Type: TypeCancel})
|
||||
}
|
||||
@@ -224,7 +224,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
|
||||
break
|
||||
}
|
||||
c.pressed = true
|
||||
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers})
|
||||
events = append(events, ClickEvent{Type: TypePress, Position: e.Position.Round(), Source: e.Source, Modifiers: e.Modifiers})
|
||||
case pointer.Leave:
|
||||
if !c.pressed {
|
||||
c.pid = e.PointerID
|
||||
|
||||
+1
-2
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"time"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/gesture"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
@@ -39,7 +38,7 @@ type Click struct {
|
||||
// Press represents a past pointer press.
|
||||
type Press struct {
|
||||
// Position of the press.
|
||||
Position f32.Point
|
||||
Position image.Point
|
||||
// Start is when the press began.
|
||||
Start time.Time
|
||||
// End is when the press was ended by a release or cancel.
|
||||
|
||||
@@ -297,7 +297,7 @@ func drawInk(gtx layout.Context, c widget.Press) {
|
||||
ink := paint.ColorOp{Color: rgba}
|
||||
ink.Add(gtx.Ops)
|
||||
rr := size * .5
|
||||
defer op.Offset(c.Position.Add(f32.Point{
|
||||
defer op.Offset(layout.FPt(c.Position).Add(f32.Point{
|
||||
X: -rr,
|
||||
Y: -rr,
|
||||
})).Push(gtx.Ops).Pop()
|
||||
|
||||
Reference in New Issue
Block a user