Added keyboard support for zooming.

Added information but to show intro.
This commit is contained in:
Sander Schobers 2020-05-11 12:05:54 +02:00
parent a6004a8ab6
commit 5ebd582498
3 changed files with 41 additions and 11 deletions

View File

@ -127,6 +127,7 @@ func (c *GameControls) Init(ctx *Context) error {
c.game.New() c.game.New()
c.updateFlowerControls(ctx) c.updateFlowerControls(ctx)
}), }),
NewIconButton("control-information", func(*Context) { c.dialogs.ShowIntro() }),
} }
c.shovel = NewIconButtonConfig("control-shovel", func(*Context) { c.game.SelectShovel() }, func(b *IconButton) { b.IconHeight = 32 }) c.shovel = NewIconButtonConfig("control-shovel", func(*Context) { c.game.SelectShovel() }, func(b *IconButton) { b.IconHeight = 32 })

View File

@ -100,3 +100,26 @@ func (p *projection) visibleTiles(action func(int32, int32, Point)) {
} }
} }
} }
func (p *projection) ZoomOut(ctx *Context, center PointF) {
if p.zoom <= .25 {
return
}
p.SetZoom(ctx, center, .5*p.zoom)
}
func (p *projection) ZoomIn(ctx *Context, center PointF) {
if p.zoom >= 2 {
return
}
p.SetZoom(ctx, center, 2*p.zoom)
}
func (p *projection) SetZoom(ctx *Context, center PointF, zoom float32) {
if p.zoom == zoom {
return
}
p.center = center.Sub(center.Sub(p.center).Mul(p.zoom / zoom))
p.zoom = zoom
p.update(ctx.Renderer)
}

View File

@ -78,17 +78,10 @@ func (r *terrainRenderer) Handle(ctx *Context, event sdl.Event) bool {
} }
case *sdl.MouseWheelEvent: case *sdl.MouseWheelEvent:
if r.hover != nil { if r.hover != nil {
zoom := r.project.zoom if e.Y < 0 {
if e.Y < 0 && r.project.zoom > .25 { r.project.ZoomOut(ctx, r.hover.ToPtF())
zoom *= .5 } else {
} else if e.Y > 0 && r.project.zoom < 2 { r.project.ZoomIn(ctx, r.hover.ToPtF())
zoom *= 2
}
if zoom != r.project.zoom {
hover := r.hover.ToPtF()
r.project.center = hover.Sub(hover.Sub(r.project.center).Mul(r.project.zoom / zoom))
r.project.zoom = zoom
r.project.update(ctx.Renderer)
} }
} }
case *sdl.WindowEvent: case *sdl.WindowEvent:
@ -96,6 +89,19 @@ func (r *terrainRenderer) Handle(ctx *Context, event sdl.Event) bool {
r.hover = nil r.hover = nil
r.project.update(ctx.Renderer) r.project.update(ctx.Renderer)
} }
case *sdl.KeyboardEvent:
if e.Type == sdl.KEYDOWN {
switch e.Keysym.Sym {
case sdl.K_PLUS:
r.project.ZoomIn(ctx, r.project.center)
case sdl.K_KP_PLUS:
r.project.ZoomIn(ctx, r.project.center)
case sdl.K_MINUS:
r.project.ZoomOut(ctx, r.project.center)
case sdl.K_KP_MINUS:
r.project.ZoomOut(ctx, r.project.center)
}
}
} }
return false return false
} }