feat: hit on primitives
This commit is contained in:
+33
-6
@@ -1,6 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
@@ -47,6 +48,8 @@ type editor struct {
|
||||
viewportImg *image.RGBA
|
||||
viewportW int
|
||||
viewportH int
|
||||
|
||||
hits []world.Hit
|
||||
}
|
||||
|
||||
func (e *editor) CanvasScale() float32 { return e.canvasScale }
|
||||
@@ -171,7 +174,24 @@ func (e *editor) onDradEnd() {
|
||||
e.pan.DragEnd()
|
||||
}
|
||||
|
||||
func (e *editor) wheelZoom(stepDelta int) {}
|
||||
func (e *editor) onTapped(ev *fyne.PointEvent) {
|
||||
hits, err := e.world.HitTest(e.hits, e.wp, int(ev.Position.X*e.canvasScale), int(ev.Position.Y*e.canvasScale))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
m := func(v int) float64 {
|
||||
return float64(v) / float64(world.SCALE)
|
||||
}
|
||||
var coord string
|
||||
for _, hit := range hits {
|
||||
if hit.Kind == world.KindLine {
|
||||
coord = fmt.Sprintf("{%f,%f - %f,%f}", m(hit.X1), m(hit.Y1), m(hit.X2), m(hit.Y2))
|
||||
} else {
|
||||
coord = fmt.Sprintf("{%f,%f}", m(hit.X), m(hit.Y))
|
||||
}
|
||||
fmt.Println("hit:", hit.ID, "Coord:", coord)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *editor) InitImage() {
|
||||
s := fyne.NewSize(292, 292)
|
||||
@@ -203,6 +223,7 @@ func NewEditor() *editor {
|
||||
Options: &world.RenderOptions{DisableWrapScroll: false},
|
||||
},
|
||||
canvasScale: 1.0,
|
||||
hits: make([]world.Hit, 5),
|
||||
}
|
||||
|
||||
// Create a drawer with some initial context; real size will be adjusted on first draw.
|
||||
@@ -213,7 +234,7 @@ func NewEditor() *editor {
|
||||
return e.draw(wPx, hPx)
|
||||
})
|
||||
|
||||
e.canvas = newInteractiveRaster(e, e.raster, e.onMapLayout, e.onScrolled, e.onDragged, e.onDradEnd)
|
||||
e.canvas = newInteractiveRaster(e, e.raster, e.onMapLayout, e.onScrolled, e.onDragged, e.onDradEnd, e.onTapped)
|
||||
e.pan = NewPanController(e)
|
||||
|
||||
// Wire coalescer: it schedules raster.Refresh() on UI thread and renders once per draw call.
|
||||
@@ -241,18 +262,24 @@ func testWorldInit(w *world.World) {
|
||||
StrokeDashes: new([]float64{10.}),
|
||||
})
|
||||
|
||||
circleStyle := w.AddStyleCircle(world.StyleOverride{
|
||||
discStyle := w.AddStyleCircle(world.StyleOverride{
|
||||
FillColor: color.RGBA{R: 255, G: 255, B: 0, A: 255},
|
||||
})
|
||||
|
||||
if _, err := w.AddCircle(150, 150, 50, world.CircleWithStyleID(circleStyle)); err != nil {
|
||||
circleStyle := w.AddStyleCircle(world.StyleOverride{
|
||||
FillColor: world.TransparentFill(),
|
||||
StrokeColor: color.RGBA{R: 255, G: 255, B: 255, A: 255},
|
||||
StrokeWidthPx: new(4.0),
|
||||
})
|
||||
|
||||
if _, err := w.AddCircle(150, 150, 50, world.CircleWithStyleID(discStyle)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if _, err := w.AddCircle(150, 299, 30); err != nil {
|
||||
if _, err := w.AddCircle(150, 299, 30, world.CircleWithStyleID(circleStyle)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if _, err := w.AddCircle(299, 150, 30); err != nil {
|
||||
if _, err := w.AddCircle(299, 150, 30, world.CircleWithStyleID(circleStyle)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user