Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: adding image hex slider, hex scorch, etc #6621

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions megamek/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ task testAi(type: JavaExec, dependsOn: jar) {
jvmArgs = mmJvmOptions
}

task splitImage(type: JavaExec, dependsOn: jar) {
description = 'Split Image into hexagons'
group = 'utility'
classpath = sourceSets.main.runtimeClasspath
mainClass = 'megamek.utilities.ImageHexSlicer'
args(project.hasProperty("splitImageArgs") ? project.property("splitImageArgs").split(' ') : "")

jvmArgs = mmJvmOptions
}

tasks.withType(Checkstyle) {
minHeapSize = "200m"
maxHeapSize = "1g"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions megamek/src/megamek/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,7 @@ protected boolean handleGameSpecificPacket(Packet packet) {
List<Coords> coords = new ArrayList<>((Set<Coords>) packet.getObject(0));
List<Hex> hexes = new ArrayList<>((Set<Hex>) packet.getObject(1));
game.getBoard().setHexes(coords, hexes);
game.processGameEvent(new GameBoardChangeEvent(this));
break;
case BLDG_UPDATE:
receiveBuildingUpdate(packet);
Expand Down
3 changes: 2 additions & 1 deletion megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
import static megamek.common.Configuration.gameSummaryImagesMMDir;

public class ClientGUI extends AbstractClientGUI implements BoardViewListener,
ActionListener, IPreferenceChangeListener, MekDisplayListener, ILocalBots, IDisconnectSilently, IHasUnitDisplay, IHasBoardView, IHasMenuBar, IHasCurrentPanel {
ActionListener, IPreferenceChangeListener, MekDisplayListener, ILocalBots, IDisconnectSilently, IHasUnitDisplay, IHasBoardView,
IHasMenuBar, IHasCurrentPanel {
private final static MMLogger logger = MMLogger.create(ClientGUI.class);

// region Variable Declarations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2020-2021 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MegaMek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.client.ui.swing.boardview;

import megamek.client.ui.swing.tileset.HexTileset;
import megamek.common.Coords;
import megamek.common.Terrains;
import megamek.common.util.ImageUtil;

import java.awt.*;

/**
* Contains common functionality for wreck sprites (currently isometric and regular)
* @author Luana Coppio
*/
public abstract class BaseScorchedGroundSprite extends Sprite {
protected Coords coords;
protected Coords offset;
protected int radius;

public BaseScorchedGroundSprite(BoardView boardView, Coords coords, Coords offset, int radius) {
super(boardView);
this.coords = coords;
this.offset = offset;
this.radius = radius;
}

@Override
public Rectangle getBounds() {
// Start with the hex and add the label
bounds = new Rectangle(0, 0, bv.hex_size.width, bv.hex_size.height);

// Move to board position, save this origin for correct drawing
Point hexOrigin = bounds.getLocation();
Point ePos = bv.getHexLocation(coords);
bounds.setLocation(hexOrigin.x + ePos.x, hexOrigin.y + ePos.y);

return bounds;
}

/**
* Creates the sprite for this entity. It is an extra pain to create
* transparent images in AWT.
*/
@Override
public void prepare() {
if (bv.game.getBoard().getHex(coords).containsAnyTerrainOf(Terrains.ULTRA_SUBLEVEL, Terrains.MAGMA)) {
return;
}
// create image for buffer
image = ImageUtil.createAcceleratedImage(HexTileset.HEX_W, HexTileset.HEX_H);
Graphics2D graph = (Graphics2D) image.getGraphics();

// if the entity is underwater or would sink underwater, we want to make the wreckage translucent
// so it looks like it sunk
boolean entityIsUnderwater = bv.game.getBoard().getHex(coords).containsTerrain(Terrains.WATER);

if (entityIsUnderwater) {
graph.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.35f));
}
int y = offset.getY();
if (coords.getX() % 2 == 0) {
y = Math.max(y - 1, 0);
}
Image destroyed = bv.tileManager.bottomLayerExplosionMarker(offset.getX(), y, radius * 2 + 1);
graph.drawImage(destroyed, 0, 0, this);

if (entityIsUnderwater) {
graph.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 1.0f));
}

// create final image
image = bv.getScaledImage(image, false);
graph.dispose();
}

public Coords getPosition() {
return coords;
}
}
84 changes: 68 additions & 16 deletions megamek/src/megamek/client/ui/swing/boardview/BoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;
Expand Down Expand Up @@ -88,6 +89,7 @@
import megamek.common.util.fileUtils.MegaMekFile;
import megamek.logging.MMLogger;
import megamek.server.props.OrbitalBombardment;
import org.apache.commons.collections4.EnumerationUtils;

/**
* Displays the board; lets the user scroll around and select points on it.
Expand Down Expand Up @@ -269,18 +271,21 @@ public final class BoardView extends AbstractBoardView implements BoardListener,
private Set<Integer> animatedImages = new HashSet<>();

// Move units step by step
private ArrayList<MovingUnit> movingUnits = new ArrayList<>();
private List<MovingUnit> movingUnits = new ArrayList<>();

private long moveWait = 0;

// moving entity sprites
private ArrayList<MovingEntitySprite> movingEntitySprites = new ArrayList<>();
private HashMap<Integer, MovingEntitySprite> movingEntitySpriteIds = new HashMap<>();
private ArrayList<GhostEntitySprite> ghostEntitySprites = new ArrayList<>();
private List<MovingEntitySprite> movingEntitySprites = new ArrayList<>();
private Map<Integer, MovingEntitySprite> movingEntitySpriteIds = new HashMap<>();
private List<GhostEntitySprite> ghostEntitySprites = new ArrayList<>();

// wreck sprites
private ArrayList<WreckSprite> wreckSprites = new ArrayList<>();
private ArrayList<IsometricWreckSprite> isometricWreckSprites = new ArrayList<>();
private List<WreckSprite> wreckSprites = new ArrayList<>();
private List<IsometricWreckSprite> isometricWreckSprites = new ArrayList<>();

private List<ScorchedGroundSprite> scorchedGroundSprites = new ArrayList<>();
private List<IsometricScorchedGroundSprite> isometricScorchedGroundSprites = new ArrayList<>();

private Coords rulerStart;
private Coords rulerEnd;
Expand Down Expand Up @@ -767,6 +772,8 @@ public void preferenceChange(PreferenceChangeEvent e) {
for (Sprite s : isometricWreckSprites) {
s.prepare();
}
isometricScorchedGroundSprites.forEach(Sprite::prepare);
scorchedGroundSprites.forEach(Sprite::prepare);
break;

case GUIPreferences.USE_CAMO_OVERLAY:
Expand Down Expand Up @@ -905,6 +912,7 @@ public void draw(Graphics g) {

// draw wrecks
if (GUIP.getShowWrecks() && !useIsometric()) {
drawSprites(g, scorchedGroundSprites);
drawSprites(g, wreckSprites);
}

Expand Down Expand Up @@ -1219,8 +1227,7 @@ private synchronized void drawIsometricSpritesForHex(Coords c, Graphics g,
* @param g The Graphics object for this board.
* @param spriteArrayList The complete list of all IsometricSprite on the board.
*/
private synchronized void drawIsometricWreckSpritesForHex(Coords c,
Graphics g, ArrayList<IsometricWreckSprite> spriteArrayList) {
private synchronized void drawIsometricWreckSpritesForHex(Coords c, Graphics g, List<IsometricWreckSprite> spriteArrayList) {
Rectangle view = g.getClipBounds();
for (IsometricWreckSprite sprite : spriteArrayList) {
Coords cp = sprite.getPosition();
Expand All @@ -1233,6 +1240,31 @@ private synchronized void drawIsometricWreckSpritesForHex(Coords c,
}
}

/**
* Draws the ScorchedGroundSprite for the given hex. This function is used by the
* isometric rendering process so that sprites are drawn in the order that
* hills are rendered to create the appearance that the sprite is behind the
* hill.
*
* @param c The Coordinates of the hex that the sprites should be
* drawn
* for.
* @param g The Graphics object for this board.
* @param spriteArrayList The complete list of all IsometricScorchedGroundSprite on the board.
*/
private synchronized void drawIsometricScorchedGroundForHex(Coords c, Graphics g, List<IsometricScorchedGroundSprite> spriteArrayList) {
Rectangle view = g.getClipBounds();
for (IsometricScorchedGroundSprite sprite : spriteArrayList) {
Coords cp = sprite.getPosition();
if (cp.equals(c) && view.intersects(sprite.getBounds()) && !sprite.isHidden()) {
if (!sprite.isReady()) {
sprite.prepare();
}
sprite.drawOnto(g, sprite.getBounds().x, sprite.getBounds().y, boardPanel, false);
}
}
}

/**
* Draws a translucent sprite without any of the companion graphics, if it
* is in the current view. This is used only when performing isometric
Expand Down Expand Up @@ -1521,7 +1553,11 @@ private void drawOrbitalBombardmentHexes(Graphics boardGraphics) {
boardGraphics.drawImage(getScaledImage(orbitalBombardmentImage, true), p2.x, p2.y, boardPanel);
}
}

for (Coords c2 : c.allAtDistanceOrLess(orbitalBombardment.getRadius())) {
var hex = game.getBoard().getHex(c2);
hex.setTheme("volcano");
hexImageCache.get(c2).needsUpdating = true;
}
}
}

Expand Down Expand Up @@ -1701,6 +1737,7 @@ public BufferedImage getEntireBoardImage(boolean ignoreUnits, boolean useBaseZoo
if (!ignoreUnits) {
// draw wrecks
if (GUIP.getShowWrecks() && !useIsometric()) {
drawSprites(boardGraph, scorchedGroundSprites);
drawSprites(boardGraph, wreckSprites);
}

Expand Down Expand Up @@ -1835,6 +1872,7 @@ private void drawHexes(Graphics g, Rectangle view, boolean saveBoardImage) {
if (hex != null) {
if (!saveBoardImage) {
if (GUIP.getShowWrecks()) {
drawIsometricScorchedGroundForHex(c, g, isometricScorchedGroundSprites);
drawIsometricWreckSpritesForHex(c, g, isometricWreckSprites);
}
}
Expand Down Expand Up @@ -3006,14 +3044,18 @@ public void redrawAllEntities() {
int numEntities = game.getNoOfEntities();
// Prevent IllegalArgumentException
numEntities = Math.max(1, numEntities);
Queue<EntitySprite> newSprites = new PriorityQueue<>(numEntities);
Queue<IsometricSprite> newIsometricSprites = new PriorityQueue<>(numEntities);
Map<ArrayList<Integer>, EntitySprite> newSpriteIds = new HashMap<>(numEntities);
Map<ArrayList<Integer>, IsometricSprite> newIsoSpriteIds = new HashMap<>(numEntities);

ArrayList<ScorchedGroundSprite> newScorchedGround = new ArrayList<>();
ArrayList<IsometricScorchedGroundSprite> newIsometricScorchedGround = new ArrayList<>();
Enumeration<MultiHexScorchDecalPosition> scorchedGround = game.getScorchedGround();
while (scorchedGround.hasMoreElements()) {
MultiHexScorchDecalPosition decal = scorchedGround.nextElement();
newScorchedGround.add(new ScorchedGroundSprite(this, decal));
newIsometricScorchedGround.add(new IsometricScorchedGroundSprite(this, decal));
}

ArrayList<WreckSprite> newWrecks = new ArrayList<>();
ArrayList<IsometricWreckSprite> newIsometricWrecks = new ArrayList<>();

Enumeration<Entity> e = game.getWreckedEntities();
while (e.hasMoreElements()) {
Entity entity = e.nextElement();
Expand All @@ -3036,6 +3078,11 @@ public void redrawAllEntities() {
}
}

Queue<EntitySprite> newSprites = new PriorityQueue<>(numEntities);
Queue<IsometricSprite> newIsometricSprites = new PriorityQueue<>(numEntities);
Map<ArrayList<Integer>, EntitySprite> newSpriteIds = new HashMap<>(numEntities);
Map<ArrayList<Integer>, IsometricSprite> newIsoSpriteIds = new HashMap<>(numEntities);

clearC3Networks();
clearFlyOverPaths();
for (Entity entity : game.getEntitiesVector()) {
Expand Down Expand Up @@ -3101,6 +3148,11 @@ public void redrawAllEntities() {
wreckSprites = newWrecks;
isometricWreckSprites = newIsometricWrecks;

scorchedGroundSprites.clear();
scorchedGroundSprites.addAll(newScorchedGround);
isometricScorchedGroundSprites.clear();
isometricScorchedGroundSprites.addAll(newIsometricScorchedGround);

// Update ECM list, to ensure that Sprites are updated with ECM info
updateEcmList();
// Re-highlight a selected entity, if present
Expand Down Expand Up @@ -5235,11 +5287,11 @@ public FovHighlightingAndDarkening getFovHighlighting() {
return fovHighlightingAndDarkening;
}

public ArrayList<WreckSprite> getWreckSprites() {
public List<WreckSprite> getWreckSprites() {
return wreckSprites;
}

public ArrayList<IsometricWreckSprite> getIsoWreckSprites() {
public List<IsometricWreckSprite> getIsoWreckSprites() {
return isometricWreckSprites;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package megamek.client.ui.swing.boardview;

import megamek.common.Coords;

import java.awt.*;
import java.awt.image.ImageObserver;

public class IsometricScorchedGroundSprite extends BaseScorchedGroundSprite {
public IsometricScorchedGroundSprite(BoardView boardView, Coords coords, Coords offset, int radius) {
super(boardView, coords, offset, radius);
}

public IsometricScorchedGroundSprite(BoardView boardView, MultiHexScorchDecalPosition decal) {
this(boardView, decal.position(), decal.offset(), decal.radius());
}

@Override
public void drawOnto(Graphics g, int x, int y, ImageObserver observer, boolean makeTranslucent) {
if (isReady()) {
Graphics2D g2 = (Graphics2D) g;
if (makeTranslucent) {
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.35f));
g2.drawImage(image, x, y, observer);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
} else {
g.drawImage(image, x, y, observer);
}
} else {
prepare();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Sprite for an wreck. Consists of an image, drawn from the Tile Manager
* and an identification label.
*/
class IsometricWreckSprite extends AbstractWreckSprite {
public class IsometricWreckSprite extends AbstractWreckSprite {

/**
* Isometric wreck sprite constructor, calculates boundaries
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package megamek.client.ui.swing.boardview;

import megamek.common.Coords;

public record MultiHexScorchDecalPosition(Coords position, Coords offset, int radius) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package megamek.client.ui.swing.boardview;

import megamek.common.Coords;

public class ScorchedGroundSprite extends BaseScorchedGroundSprite {
public ScorchedGroundSprite(BoardView boardView, Coords coords, Coords offset, int radius) {
super(boardView, coords, offset, radius);
}

public ScorchedGroundSprite(BoardView boardView, MultiHexScorchDecalPosition decal) {
this(boardView, decal.position(), decal.offset(), decal.radius());
}
}
Loading