From 4ae8a5167683104508c24fb235f1d55b24d03312 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 15 Apr 2023 14:39:41 -0400 Subject: [PATCH] Display camera on map + right-click to pan --- .../mindustry/graphics/MinimapRenderer.java | 31 +++++++++++++------ core/src/mindustry/input/DesktopInput.java | 6 ++++ core/src/mindustry/input/InputHandler.java | 5 +++ .../ui/fragments/MinimapFragment.java | 25 +++++++++++++-- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/core/src/mindustry/graphics/MinimapRenderer.java b/core/src/mindustry/graphics/MinimapRenderer.java index 3750d33522..59e88f2be3 100644 --- a/core/src/mindustry/graphics/MinimapRenderer.java +++ b/core/src/mindustry/graphics/MinimapRenderer.java @@ -99,15 +99,15 @@ public class MinimapRenderer{ region = new TextureRegion(texture); } - public void drawEntities(float x, float y, float w, float h, float scaling, boolean withLabels){ + public void drawEntities(float x, float y, float w, float h, float scaling, boolean fullView){ lastX = x; lastY = y; lastW = w; lastH = h; lastScl = scaling; - worldSpace = withLabels; + worldSpace = fullView; - if(!withLabels){ + if(!fullView){ updateUnitArray(); }else{ units.clear(); @@ -125,8 +125,8 @@ public class MinimapRenderer{ for(Unit unit : units){ if(unit.inFogTo(player.team()) || !unit.type.drawMinimap) continue; - float rx = !withLabels ? (unit.x - rect.x) / rect.width * w : unit.x / (world.width() * tilesize) * w; - float ry = !withLabels ? (unit.y - rect.y) / rect.width * h : unit.y / (world.height() * tilesize) * h; + float rx = !fullView ? (unit.x - rect.x) / rect.width * w : unit.x / (world.width() * tilesize) * w; + float ry = !fullView ? (unit.y - rect.y) / rect.width * h : unit.y / (world.height() * tilesize) * h; Draw.mixcol(unit.team.color, 1f); float scale = Scl.scl(1f) / 2f * scaling * 32f; @@ -135,7 +135,7 @@ public class MinimapRenderer{ Draw.reset(); } - if(withLabels && net.active()){ + if(fullView && net.active()){ for(Player player : Groups.player){ if(!player.dead()){ float rx = player.x / (world.width() * tilesize) * w; @@ -149,7 +149,7 @@ public class MinimapRenderer{ Draw.reset(); if(state.rules.fog){ - if(withLabels){ + if(fullView){ float z = zoom; //max zoom out fixes everything, somehow? setZoom(99999f); @@ -186,12 +186,25 @@ public class MinimapRenderer{ } //TODO might be useful in the standard minimap too - if(withLabels){ + if(fullView){ drawSpawns(x, y, w, h, scaling); + + if(!mobile){ + //draw bounds for camera - not drawn on mobile because you can't shift it by tapping anyway + Rect r = Core.camera.bounds(Tmp.r1); + Vec2 bot = transform(Tmp.v1.set(r.x, r.y)); + Vec2 top = transform(Tmp.v2.set(r.x + r.width, r.y + r.height)); + Lines.stroke(Scl.scl(3f)); + Draw.color(Pal.accent); + Lines.rect(bot.x,bot.y, top.x - bot.x, top.y - bot.y); + Draw.reset(); + } } state.rules.objectives.eachRunning(obj -> { - for(var marker : obj.markers) marker.drawMinimap(this); + for(var marker : obj.markers){ + marker.drawMinimap(this); + } }); } diff --git a/core/src/mindustry/input/DesktopInput.java b/core/src/mindustry/input/DesktopInput.java index 0044991083..8a88370704 100644 --- a/core/src/mindustry/input/DesktopInput.java +++ b/core/src/mindustry/input/DesktopInput.java @@ -770,6 +770,12 @@ public class DesktopInput extends InputHandler{ } } + @Override + public void panCamera(Vec2 position){ + panning = true; + camera.position.set(position); + } + protected void updateMovement(Unit unit){ boolean omni = unit.type.omniMovement; diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index 589c1ae30b..ad9dc9c09c 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -1482,6 +1482,11 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ return World.toTile(vec.y); } + /** Forces the camera to a position and enables panning on desktop. */ + public void panCamera(Vec2 position){ + camera.position.set(position); + } + public boolean selectedBlock(){ return isPlacing(); } diff --git a/core/src/mindustry/ui/fragments/MinimapFragment.java b/core/src/mindustry/ui/fragments/MinimapFragment.java index ea7b360f16..76f7aa202a 100644 --- a/core/src/mindustry/ui/fragments/MinimapFragment.java +++ b/core/src/mindustry/ui/fragments/MinimapFragment.java @@ -5,9 +5,11 @@ import arc.graphics.*; import arc.graphics.g2d.*; import arc.input.*; import arc.math.*; +import arc.math.geom.*; import arc.scene.*; import arc.scene.event.*; import arc.scene.ui.layout.*; +import arc.util.*; import mindustry.gen.*; import mindustry.input.*; import mindustry.ui.*; @@ -20,6 +22,16 @@ public class MinimapFragment{ private float baseSize = Scl.scl(5f); public Element elem; + protected Rect getRectBounds(){ + float + w = Core.graphics.getWidth(), + h = Core.graphics.getHeight(), + ratio = renderer.minimap.getTexture() == null ? 1f : (float)renderer.minimap.getTexture().height / renderer.minimap.getTexture().width, + size = baseSize * zoom * world.width(); + + return Tmp.r1.set(w/2f + panx*zoom - size/2f, h/2f + pany*zoom - size/2f * ratio, size, size * ratio); + } + public void build(Group parent){ elem = parent.fill((x, y, w, h) -> { w = Core.graphics.getWidth(); @@ -35,7 +47,8 @@ public class MinimapFragment{ TextureRegion reg = Draw.wrap(renderer.minimap.getTexture()); Draw.rect(reg, w/2f + panx*zoom, h/2f + pany*zoom, size, size * ratio); - renderer.minimap.drawEntities(w/2f + panx*zoom - size/2f, h/2f + pany*zoom - size/2f * ratio, size, size * ratio, zoom, true); + Rect bounds = getRectBounds(); + renderer.minimap.drawEntities(bounds.x, bounds.y, bounds.width, bounds.height, zoom, true); } Draw.reset(); @@ -69,8 +82,14 @@ public class MinimapFragment{ @Override public void pan(InputEvent event, float x, float y, float deltaX, float deltaY){ - panx += deltaX / zoom; - pany += deltaY / zoom; + if(event.keyCode != KeyCode.mouseRight){ + panx += deltaX / zoom; + pany += deltaY / zoom; + }else{ + Rect r = getRectBounds(); + Tmp.v1.set(x, y).sub(r.x, r.y).scl(1f / r.width, 1f / r.height).scl(world.unitWidth(), world.unitHeight()); + control.input.panCamera(Tmp.v1); + } } @Override