From 5ebd58249828ee8e95b325193784261e4bf72229 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 11 May 2020 12:05:54 +0200 Subject: [PATCH] Added keyboard support for zooming. Added information but to show intro. --- gamecontrols.go | 1 + projection.go | 23 +++++++++++++++++++++++ terrainrenderer.go | 28 +++++++++++++++++----------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/gamecontrols.go b/gamecontrols.go index 890aad8..9b3a777 100644 --- a/gamecontrols.go +++ b/gamecontrols.go @@ -127,6 +127,7 @@ func (c *GameControls) Init(ctx *Context) error { c.game.New() 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 }) diff --git a/projection.go b/projection.go index 6ac9914..de31e7b 100644 --- a/projection.go +++ b/projection.go @@ -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) +} diff --git a/terrainrenderer.go b/terrainrenderer.go index 3df1b89..ea35496 100644 --- a/terrainrenderer.go +++ b/terrainrenderer.go @@ -78,17 +78,10 @@ func (r *terrainRenderer) Handle(ctx *Context, event sdl.Event) bool { } case *sdl.MouseWheelEvent: if r.hover != nil { - zoom := r.project.zoom - if e.Y < 0 && r.project.zoom > .25 { - zoom *= .5 - } else if e.Y > 0 && r.project.zoom < 2 { - 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) + if e.Y < 0 { + r.project.ZoomOut(ctx, r.hover.ToPtF()) + } else { + r.project.ZoomIn(ctx, r.hover.ToPtF()) } } case *sdl.WindowEvent: @@ -96,6 +89,19 @@ func (r *terrainRenderer) Handle(ctx *Context, event sdl.Event) bool { r.hover = nil 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 }