 |
Crossfire JXClient, Trunk
|
Go to the documentation of this file.
23 package com.realtime.crossfire.jxclient.gui.map;
41 import java.awt.Color;
42 import java.awt.Dimension;
43 import java.awt.Graphics;
44 import java.awt.Graphics2D;
45 import java.awt.GraphicsConfiguration;
46 import java.awt.GraphicsDevice;
47 import java.awt.GraphicsEnvironment;
48 import java.awt.Image;
49 import java.awt.Transparency;
50 import java.awt.image.BufferedImage;
51 import java.util.ArrayDeque;
52 import java.util.Deque;
53 import java.util.HashMap;
56 import javax.swing.ImageIcon;
57 import org.jetbrains.annotations.NotNull;
58 import org.jetbrains.annotations.Nullable;
221 private final Map<Color, Image>
images =
new HashMap<>();
230 public void mapChanged(@NotNull
final CfMap map, @NotNull
final Set<CfMapSquare> changedSquares) {
231 assert Thread.holdsLock(map);
233 final int x0 = map.getOffsetX();
234 final int y0 = map.getOffsetY();
238 final int x = mapSquare.getX()+x0;
240 final int y = mapSquare.getY()+y0;
272 private void clearMap(@NotNull
final Graphics2D g) {
274 g.fillRect(0, 0, getWidth(), getHeight());
322 this.mapUpdaterState.addCrossfireMapListener(
mapListener);
325 setMapSize(this.mapUpdaterState.getMapWidth(),
this.mapUpdaterState.getMapHeight());
377 private void redrawTiles(@NotNull
final Graphics g, @NotNull
final CfMap map,
final int x0,
final int y0,
final int x1,
final int y1) {
378 for (
int x = x0; x < x1; x++) {
379 for (
int y = y0; y < y1; y++) {
380 final CfMapSquare mapSquare = map.getMapSquare(x, y);
395 private void redrawTilesUnlessDirty(@NotNull
final Graphics g, @NotNull
final CfMap map,
final int x0,
final int y0,
final int x1,
final int y1) {
396 for (
int x = x0; x < x1; x++) {
397 for (
int y = y0; y < y1; y++) {
413 final CfMapSquare mapSquare = map.getMapSquareUnlessDirty(x, y);
414 if (mapSquare !=
null) {
430 if (x < 0 || y < 0 || x >=
mapWidth || y >=
mapHeight || mapSquare.isFogOfWar()) {
433 final int darkness = mapSquare.getDarkness();
455 final int mapSquareX = mapSquare.getX();
456 final int mapSquareY = mapSquare.getY();
457 boolean foundSquare =
false;
460 if (headMapSquare !=
null) {
461 final Face headFace = headMapSquare.
getFace(layer);
462 assert headFace !=
null;
463 final int dx = headMapSquare.
getX()-mapSquareX;
464 final int dy = headMapSquare.
getY()-mapSquareY;
465 assert dx > 0 || dy > 0;
473 final Face face = mapSquare.getFace(layer);
499 protected abstract void paintSquareBackground(@NotNull
final Graphics g,
final int px,
final int py,
final boolean hasImage, @NotNull
final CfMapSquare mapSquare);
512 final int sx = imageIcon.getIconWidth()-
offsetX;
513 final int sy = imageIcon.getIconHeight()-
offsetY;
523 protected abstract void markPlayer(@NotNull
final Graphics g,
final int dx,
final int dy);
526 @SuppressWarnings(
"MethodDoesntCallSuperMethod")
568 final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
569 final GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
570 final GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
572 bufferedImage = graphicsConfiguration.createCompatibleImage(Math.max(1, getWidth()), Math.max(1, getHeight()), Transparency.TRANSLUCENT);
618 public void setBounds(
final int x,
final int y,
final int width,
final int height) {
619 super.setBounds(x, y, width, height);
643 final int x = Math.max(dx, 0);
644 final int w = dx > 0 ? -dx : dx;
645 final int y = Math.max(dy, 0);
646 final int h = dy > 0 ? -dy : dy;
675 protected void paintColoredSquare(@NotNull
final Graphics g, @NotNull
final Color color,
final int x,
final int y) {
676 Image image =
images.get(color);
678 final BufferedImage tmp =
new BufferedImage(
tileSize,
tileSize, color.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
679 final Graphics g2 = tmp.createGraphics();
730 if (scrollMap ==
null) {
733 final long tmp = scrollMap;
734 final int dx = (int)(tmp>>32);
735 final int dy = (int)tmp;
744 @SuppressWarnings(
"MethodDoesntCallSuperMethod")
void dispose()
Releases all allocated resources.
Interface for listeners interested in received "newmap" messages.
abstract void paintSquareBackground(@NotNull final Graphics g, final int px, final int py, final boolean hasImage, @NotNull final CfMapSquare mapSquare)
Paints the background of a map square.
int getPlayerX()
Returns the x offset of the tile representing the player.
void redrawAllUnlessDirty(@NotNull final Graphics g, @NotNull final CfMap map)
Redraws all non-dirty tiles.
Interface for listeners interested in changes within CfMap instances.
int displayMaxOffsetX
The number of pixels that are visible in the rightmost visible tile.
static int mod(final int numerator, final int denominator)
Calculates the remainder of.
int getSize()
Returns the size of faces in pixels.
final int tileSize
The size of one tile.
final MapScrollListener mapscrollListener
The MapScrollListener registered to receive map_scroll commands.
Implements the map model which is shown in the map and magic map views.
int getMapWidth()
Returns the map width in squares.
void clearMap(@NotNull final Graphics2D g)
Blanks the map display.
int displayMaxY
The tile y coordinate where map drawing ends.
Utility class for mathematical functions.
void paintComponent(@NotNull final Graphics g)
int getPlayerY()
Returns the y offset of the tile representing the player.
Color getDarknessColor(final int darkness)
Returns an overlay color for a darkness value.
final MapSizeListener mapSizeListener
The MapSizeListener registered to detect map size changes.
void setChanged()
Records that the contents have changed and must be repainted.
void paintColoredSquare(@NotNull final Graphics g, @NotNull final Color color, final int x, final int y)
Fills a square with one Color.
int mapHeight
The map height in squares.
Represents a map (as seen by the client).
Abstract base class for GUI elements to be shown in Guis.
void removeCrossfireMapListener(@NotNull final MapListener listener)
Removes a listener to notify about changed map squares.
void removeMapSizeListener(@NotNull final MapSizeListener listener)
Removes a listener to be notified about map size changes.
CfMapSquare getHeadMapSquare(final int layer)
Returns the map square of the head of a multi-square object.
final GUIElementListener elementListener
The GUIElementListener to notify.
int getOffsetY()
Returns the y offset for drawing the square at coordinate 0 of the map.
Utility class for converting darkness values into colors.
static final int DARKNESS_FULL_BRIGHT
The darkness value for a full bright square.
static final Color FOG_OF_WAR_COLOR
The color to use for overlaying fog-of-war tiles.
Manages image information ("faces") needed to display the map view, items, and spell icons.
int offsetY
The y offset for drawing the square at coordinate 0 of the map.
int playerX
The x offset of the tile representing the player.
Abstract base class for GUIElements that display map views.
final DarknessColors darknessColors
The DarknessColors instance for converting darkness values into colors.
final FacesProvider facesProvider
The FacesProvider for looking up faces.
Dimension getMinimumSize()
int playerY
The y offset of the tile representing the player.
ImageIcon getImageIcon(int faceNum, @Nullable AtomicBoolean returnIsUnknownImage)
Returns the face for a face ID.
void redrawTilesUnlessDirty(@NotNull final Graphics g, @NotNull final CfMap map, final int x0, final int y0, final int x1, final int y1)
Redraws a rectangular area of non-dirty tiles.
Interface defining an abstract GUI element.
void redrawSquareUnlessDirty(@NotNull final Graphics g, @NotNull final CfMap map, final int x, final int y)
Redraws one square if it is not dirty.
void setMapSize(final int mapWidth, final int mapHeight)
Sets the map size.
CfMap getMap()
Returns the current map instance.
int offsetX
The x offset for drawing the square at coordinate 0 of the map.
Graphics2D createBufferGraphics(@NotNull final CfMap map)
Returns a Graphics instance for painting into bufferedImage.
static final long serialVersionUID
The serial version UID.
int displayMinY
The tile y coordinate where map drawing starts.
final NewmapListener newmapListener
The NewmapListener registered to receive newmap commands.
Interface defining constants for the "map2" Crossfire protocol message.
Face getFace(final int layer)
Returns the face of a layer.
void paintSmooth(@NotNull final Graphics graphics, final int x, final int y, final int px, final int py, final int layer, @NotNull final CfMap map, final int tileSize)
Draw the smoothing information at given position of map, for a given limit smoothlevel,...
final SmoothingRenderer smoothingRenderer
The SmoothingRenderer to use or.
AbstractGUIMap(final boolean avoidCopyArea, @NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final MapUpdaterState mapUpdaterState, @NotNull final FacesProvider facesProvider, @Nullable final SmoothingRenderer smoothingRenderer, @NotNull final DarknessColors darknessColors, @NotNull final GuiFactory guiFactory)
Creates a new instance.
final GuiFactory guiFactory
The global GuiFactory instance.
void updateScrolledMap(@NotNull final Graphics g, @NotNull final CfMap map, final int dx, final int dy)
Updates the map display after the map contents have scrolled.
static final Color BLACK_AND_FOG_OF_WAR_COLOR
The combined color or Color#BLACK and FOG_OF_WAR_COLOR.
Listener for clients interested in map size changes.
void redrawSquare(@NotNull final Graphics g, @NotNull final CfMapSquare mapSquare, @NotNull final CfMap map, final int x, final int y)
Redraws one square.
void redrawSquare(@NotNull final Graphics g, final int x, final int y, @NotNull final CfMapSquare mapSquare, @NotNull final CfMap map)
Redraws one layer of a square.
final Object bufferedImageSync
Synchronizes access to bufferedImage, clearMapPending, scrollMapPending, playerX, playerY,...
int getY()
Returns the absolute map y-coordinate of this square.
final MapListener mapListener
The MapListener registered to receive map updates.
void redrawAll()
Redraws the complete map view.
Factory for creating Gui instances.
abstract void markPlayer(@NotNull final Graphics g, final int dx, final int dy)
Paints the player location.
final String name
The name of this element.
void setBounds(final int x, final int y, final int width, final int height)
void redrawTiles(@NotNull final Graphics g, @NotNull final CfMap map, final int x0, final int y0, final int x1, final int y1)
Redraws a rectangular area of tiles.
int getX()
Returns the absolute map x-coordinate of this square.
int mapWidth
The map width in squares.
static int divRoundUp(final int numerator, final int denominator)
Returns the quotient of two values, rounded up to the nearest integer.
transient BufferedImage bufferedImage
An BufferedImage having the size of this component.
void removeCrossfireNewmapListener(@NotNull final NewmapListener listener)
Removes a listener to notify about cleared maps.
final Deque< Long > scrollMapPending
Pending map scrolls.
int displayMaxOffsetY
The number of pixels that are visible in the bottommost visible tile.
int NUM_LAYERS
The total number of map layers to display.
int displayMinX
The tile x coordinate where map drawing starts.
Represents a square in a CfMap.
final TooltipManager tooltipManager
The TooltipManager to update.
int getMapHeight()
Returns the map height in squares.
int displayMinOffsetX
The distance the leftmost visible tile reaches outside the map window.
Update a CfMap model from protocol commands.
boolean clearMapPending
Whether the map area should be blanked.
Interface for face providers.
final MapUpdaterState mapUpdaterState
The MapUpdaterState instance to display.
void paintImage(@NotNull final Graphics g, @NotNull final Face face, final int px, final int py, final int offsetX, final int offsetY)
Paints a face into a tile.
int displayMaxX
The tile x coordinate where map drawing ends.
final Map< Color, Image > images
Maps Color to an image filled with this color with a size of one square.
int displayMinOffsetY
The distance the topmost visible tile reaches outside the map window.
Listener for GUIElement related events.
Renderer for painting smoothed faces into map views.
void removeCrossfireMapScrollListener(@NotNull final MapScrollListener listener)
Removes a listener to notify about scrolled maps.
final boolean avoidCopyArea
Whether map scrolling is done by copying pixel areas.
int getOffsetX()
Returns the x offset for drawing the square at coordinate 0 of the map.