Skip to content

Commit

Permalink
Merge pull request #299 from JiepengTan/builder#297_sprite_pivot_clic…
Browse files Browse the repository at this point in the history
…k_bug

fix: Click-capture issue with sprite pivot #297
  • Loading branch information
xushiwei authored Jul 8, 2024
2 parents c43227c + e7b8b95 commit 8f9159a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions spgdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (p *spriteDrawInfo) getUpdateRotateRect(x, y float64) *math32.RotatedRect {
c := p.sprite.costumes[p.sprite.costumeIndex_]

img, centerX, centerY := c.needImage(p.sprite.g.fs)
p.sprite.applyPivot(c, &centerX, &centerY)
rect := image.Rectangle{}
rect.Min.X = 0
rect.Min.Y = 0
Expand All @@ -134,8 +135,7 @@ func (p *spriteDrawInfo) updateMatrix() {
c := p.sprite.costumes[p.sprite.costumeIndex_]

img, centerX, centerY := c.needImage(p.sprite.g.fs)
centerX += p.sprite.pivot.X * float64(c.bitmapResolution)
centerY -= p.sprite.pivot.Y * float64(c.bitmapResolution)
p.sprite.applyPivot(c, &centerX, &centerY)
rect := image.Rectangle{}
rect.Min.X = 0
rect.Min.Y = 0
Expand Down Expand Up @@ -219,6 +219,7 @@ func (p *Sprite) touchPoint(x, y float64) bool {
}
c := p.costumes[p.costumeIndex_]
img, cx, cy := c.needImage(p.g.fs)
p.applyPivot(c, &cx, &cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

pixel, _ := p.getDrawInfo().getPixel(pos, img, geo)
Expand Down Expand Up @@ -251,6 +252,7 @@ func (p *Sprite) touchRotatedRect(dstRect *math32.RotatedRect) bool {
}
c := p.costumes[p.costumeIndex_]
img, cx, cy := c.needImage(p.g.fs)
p.applyPivot(c, &cx, &cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

//check boun rect pixel
Expand Down Expand Up @@ -287,10 +289,12 @@ func (p *Sprite) touchedColor_(dst *Sprite, color Color) bool {

c := p.costumes[p.costumeIndex_]
pimg, cx, cy := c.needImage(p.g.fs)
p.applyPivot(c, &cx, &cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

c2 := dst.costumes[dst.costumeIndex_]
dstimg, cx2, cy2 := c2.needImage(p.g.fs)
dst.applyPivot(c2, &cx2, &cy2)
geo2 := dst.getDrawInfo().getPixelGeo(cx2, cy2)

cr, cg, cb, ca := color.RGBA()
Expand Down Expand Up @@ -335,10 +339,12 @@ func (p *Sprite) touchingSprite(dst *Sprite) bool {

c := p.costumes[p.costumeIndex_]
pimg, cx, cy := c.needImage(p.g.fs)
p.applyPivot(c, &cx, &cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)

c2 := dst.costumes[dst.costumeIndex_]
dstimg, cx2, cy2 := c2.needImage(p.g.fs)
dst.applyPivot(c2, &cx2, &cy2)
geo2 := dst.getDrawInfo().getPixelGeo(cx2, cy2)
//check boun rect pixel
for x := boundRect.X; x < boundRect.Width+boundRect.X; x++ {
Expand Down Expand Up @@ -411,8 +417,9 @@ func (p *Sprite) hit(hc hitContext) (hr hitResult, ok bool) {
if !rRect.Contains(pos) {
return
}
c2 := p.costumes[p.costumeIndex_]
img, cx, cy := c2.needImage(p.g.fs)
c := p.costumes[p.costumeIndex_]
img, cx, cy := c.needImage(p.g.fs)
p.applyPivot(c, &cx, &cy)
geo := p.getDrawInfo().getPixelGeo(cx, cy)
color1, pos := p.getDrawInfo().getPixel(pos, img, geo)
if debugInstr {
Expand All @@ -424,3 +431,8 @@ func (p *Sprite) hit(hc hitContext) (hr hitResult, ok bool) {
}
return hitResult{Target: p}, true
}

func (p *Sprite) applyPivot(c *costume, cx, cy *float64) {
*cx += p.pivot.X * float64(c.bitmapResolution)
*cy -= p.pivot.Y * float64(c.bitmapResolution)
}

0 comments on commit 8f9159a

Please sign in to comment.