Skip to content

Commit

Permalink
The tree list in colopedia now scales using the scaleFactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
stiangre committed Apr 20, 2022
1 parent 4346895 commit cf37ae1
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 50 deletions.
12 changes: 6 additions & 6 deletions src/net/sf/freecol/client/gui/ImageLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,14 @@ public static BufferedImage getCanvasBackgroundImage() {
: getUnscaledImage(key);
}

public static BufferedImage getColopediaCellImage(boolean expanded) {
public BufferedImage getColopediaCellImage(boolean expanded) {
final String key = "image.icon.Colopedia."
+ ((expanded) ? "open" : "closed") + "Section";
return getUnscaledImage(key);
return getScaledImage(key);
}

public static BufferedImage getColopediaConceptImage() {
return getUnscaledImage("image.icon.Colopedia.idea");
public BufferedImage getColopediaConceptImage() {
return getScaledImage("image.icon.Colopedia.idea");
}

public static BufferedImage getColorCellRendererBackground() {
Expand Down Expand Up @@ -606,7 +606,7 @@ public BufferedImage getLCRImage(Dimension size) {
}

public BufferedImage getLibertyImage() {
return this.imageCache.getSizedImage(BELLS, ICON_SIZE, false);
return this.imageCache.getSizedImage(BELLS, scale(ICON_SIZE), false);
}

public static BufferedImage getListBackground() {
Expand Down Expand Up @@ -685,7 +685,7 @@ private BufferedImage getObjectImageInternal(FreeColObject display,
* @return The appropriate {@code BufferedImage}.
*/
public BufferedImage getObjectImage(FreeColObject display) {
return getObjectImageInternal(display, scale(ICON_SIZE, scaleFactor));
return getObjectImageInternal(display, scale(ICON_SIZE));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.awt.Dimension;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -56,6 +57,7 @@
import net.sf.freecol.common.model.ProductionType;
import net.sf.freecol.common.model.StringTemplate;
import net.sf.freecol.common.model.UnitType;
import net.sf.freecol.common.util.ImageUtils;


/**
Expand Down Expand Up @@ -94,12 +96,14 @@ public void addSubTrees(DefaultMutableTreeNode root) {
: getSpecification().getBuildingTypeList()) {
if (buildingType.getUpgradesFrom() == null) {
String name = Messages.getName(buildingType);
final Dimension listItemIconSize = getListItemIconSize();
final BufferedImage buildingTypeImage = getImageLibrary().getBuildingTypeImage(buildingType, new Dimension(-1, listItemIconSize.height));
final BufferedImage centeredImage = ImageUtils.createCenteredImage(buildingTypeImage, listItemIconSize);

DefaultMutableTreeNode item =
new DefaultMutableTreeNode(new ColopediaTreeItem(
this, buildingType.getId(), name,
new ImageIcon(getImageLibrary()
.getBuildingTypeImage(buildingType,
new Dimension(-1, ImageLibrary.ICON_SIZE.height)))));
new ImageIcon(centeredImage)));
buildingHash.put(buildingType, item);
parent.add(item);
} else {
Expand All @@ -108,18 +112,21 @@ public void addSubTrees(DefaultMutableTreeNode root) {
}

while (!buildingTypes.isEmpty()) {
Iterator<BuildingType> iterator = buildingTypes.iterator();
final Iterator<BuildingType> iterator = buildingTypes.iterator();
while (iterator.hasNext()) {
BuildingType buildingType = iterator.next();
DefaultMutableTreeNode node = buildingHash.get(buildingType.getUpgradesFrom());
final BuildingType buildingType = iterator.next();
final DefaultMutableTreeNode node = buildingHash.get(buildingType.getUpgradesFrom());
if (node != null) {
String name = Messages.getName(buildingType);
DefaultMutableTreeNode item =
final Dimension listItemIconSize = getListItemIconSize();
final BufferedImage buildingTypeImage = getImageLibrary().getBuildingTypeImage(buildingType, new Dimension(-1, listItemIconSize.height));
final BufferedImage centeredImage = ImageUtils.createCenteredImage(buildingTypeImage, listItemIconSize);

final DefaultMutableTreeNode item =
new DefaultMutableTreeNode(new ColopediaTreeItem(
this, buildingType.getId(), name,
new ImageIcon(getImageLibrary()
.getBuildingTypeImage(buildingType,
new Dimension(-1, ImageLibrary.ICON_SIZE.height)))));
new ImageIcon(centeredImage)));

node.add(item);
buildingHash.put(buildingType, item);
iterator.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

package net.sf.freecol.client.gui.panel.colopedia;

import java.awt.Graphics2D;
import java.awt.Image;
import static net.sf.freecol.common.util.CollectionUtils.alwaysTrue;
import static net.sf.freecol.common.util.CollectionUtils.first;
import static net.sf.freecol.common.util.CollectionUtils.transform;

import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -36,9 +39,11 @@
import javax.swing.tree.DefaultMutableTreeNode;

import net.sf.freecol.client.FreeColClient;
import net.sf.freecol.client.gui.*;
import net.sf.freecol.client.gui.ImageLibrary;
import net.sf.freecol.client.gui.ModifierFormat;
import net.sf.freecol.client.gui.action.ColopediaAction.PanelType;
import net.sf.freecol.client.gui.panel.*;
import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.Utility;
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.model.Ability;
import net.sf.freecol.common.model.AbstractGoods;
Expand All @@ -49,7 +54,7 @@
import net.sf.freecol.common.model.ResourceType;
import net.sf.freecol.common.model.Specification;
import net.sf.freecol.common.model.UnitType;
import static net.sf.freecol.common.util.CollectionUtils.*;
import net.sf.freecol.common.util.ImageUtils;


/**
Expand Down Expand Up @@ -127,18 +132,9 @@ protected void addSubTrees(DefaultMutableTreeNode root, String id,
String name = getName();
ColopediaTreeItem cti = new ColopediaTreeItem(this, id, name, null);
DefaultMutableTreeNode node = new DefaultMutableTreeNode(cti);
int width = ImageLibrary.ICON_SIZE.width;
int height = ImageLibrary.ICON_SIZE.height;
for (FreeColSpecObjectType type : types) {
Image image = lib.getObjectImage(type, ImageLibrary.ICON_SIZE);
int x = (width - image.getWidth(null)) / 2;
int y = (height - image.getHeight(null)) / 2;
BufferedImage centeredImage
= new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = centeredImage.createGraphics();
g.drawImage(image, x, y, null);
g.dispose();
node.add(buildItem(type, new ImageIcon(centeredImage)));
final BufferedImage image = ImageUtils.createCenteredImage(lib.getObjectImage(type), getListItemIconSize());
node.add(buildItem(type, new ImageIcon(image)));
}
root.add(node);
}
Expand Down Expand Up @@ -269,6 +265,10 @@ public void appendRequiredAbility(StyledDocument doc, String key, boolean value)
logger.log(Level.WARNING, "Insert fail", ble);
}
}

public Dimension getListItemIconSize() {
return colopediaPanel.getListItemIconSize();
}


// Override Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import net.miginfocom.swing.MigLayout;
import net.sf.freecol.client.FreeColClient;
import net.sf.freecol.client.gui.ImageLibrary;
import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.MigPanel;
import net.sf.freecol.client.gui.panel.Utility;
Expand Down Expand Up @@ -154,7 +155,7 @@ private JTree buildTree() {
DefaultTreeModel treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.setCellRenderer(new ColopediaTreeCellRenderer());
tree.setCellRenderer(new ColopediaTreeCellRenderer(this, getImageLibrary()));
tree.setOpaque(false);
tree.addTreeSelectionListener(this);

Expand All @@ -166,6 +167,16 @@ private JTree buildTree() {
}
return tree;
}

/**
* Gets the preferred size for the list item images in the colopedia tree.
*/
public Dimension getListItemIconSize() {
final int width = getImageLibrary().scaleInt(ImageLibrary.ICON_SIZE.width * 3 / 2);
final int height = getImageLibrary().scaleInt(ImageLibrary.ICON_SIZE.height);

return new Dimension(width, height);
}

/**
* This function analyzes a tree selection event and calls the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

import java.awt.Color;
import java.awt.Component;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import net.sf.freecol.client.gui.ImageLibrary;
import net.sf.freecol.common.util.ImageUtils;


/**
Expand All @@ -36,10 +39,17 @@
*/
public class ColopediaTreeCellRenderer extends DefaultTreeCellRenderer {

private final ColopediaPanel colopediaPanel;
private final ImageLibrary lib;


/**
* The constructor makes sure that the backgrounds are transparent.
*/
public ColopediaTreeCellRenderer() {
public ColopediaTreeCellRenderer(ColopediaPanel colopediaPanel, ImageLibrary lib) {
this.colopediaPanel = colopediaPanel;
this.lib = lib;

setBackgroundNonSelectionColor(new Color(0,0,0,1));
}

Expand All @@ -59,8 +69,8 @@ public Component getTreeCellRendererComponent(JTree tree, Object value,
if (nodeItem.getIcon() != null) {
setIcon(nodeItem.getIcon());
} else if (!leaf) {
setIcon(new ImageIcon(ImageLibrary
.getColopediaCellImage(expanded)));
final BufferedImage image = ImageUtils.createCenteredImage(lib.getColopediaCellImage(expanded), colopediaPanel.getListItemIconSize());
setIcon(new ImageIcon(image));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -41,6 +42,7 @@
import net.sf.freecol.client.gui.panel.FreeColPanel;
import net.sf.freecol.client.gui.panel.Utility;
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.util.ImageUtils;


/**
Expand Down Expand Up @@ -139,8 +141,9 @@ public void addSubTrees(DefaultMutableTreeNode root) {
for (String concept : concepts) {
String nodeId = "colopedia.concepts." + concept;
String nodeName = Messages.getName(nodeId);
final BufferedImage image = ImageUtils.createCenteredImage(getImageLibrary().getColopediaConceptImage(), colopediaPanel.getListItemIconSize());
nodes.add(new DefaultMutableTreeNode(new ColopediaTreeItem(this,
nodeId, nodeName, new ImageIcon(ImageLibrary.getColopediaConceptImage()))));
nodeId, nodeName, new ImageIcon(image))));
}
Collections.sort(nodes, nodeComparator);
for (DefaultMutableTreeNode n : nodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.model.FoundingFather;
import net.sf.freecol.common.model.FoundingFather.FoundingFatherType;
import net.sf.freecol.common.util.ImageUtils;
import net.sf.freecol.common.model.Specification;
import net.sf.freecol.common.model.StringTemplate;
import net.sf.freecol.common.model.Turn;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void addSubTrees(DefaultMutableTreeNode root) {
for (FoundingFather foundingFather : spec.getFoundingFathers()) {
fathersByType.get(foundingFather.getType()).add(foundingFather);
}
ImageIcon icon = new ImageIcon(getImageLibrary().getLibertyImage());
ImageIcon icon = new ImageIcon(ImageUtils.createCenteredImage(getImageLibrary().getLibertyImage(), getListItemIconSize()));
for (FoundingFatherType fatherType : FoundingFatherType.values()) {
String id = fatherType.getTypeKey();
String typeName = Messages.message(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import net.sf.freecol.common.model.IndianNationType;
import net.sf.freecol.common.model.NationType;
import net.sf.freecol.common.model.UnitType;
import net.sf.freecol.common.util.ImageUtils;
import net.sf.freecol.common.util.RandomChoice;


Expand Down Expand Up @@ -86,7 +87,7 @@ public void addSubTrees(DefaultMutableTreeNode root) {
nations.addAll(getSpecification().getEuropeanNationTypes());
nations.addAll(getSpecification().getREFNationTypes());
nations.addAll(getSpecification().getIndianNationTypes());
ImageIcon icon = new ImageIcon(getImageLibrary().getLibertyImage());
ImageIcon icon = new ImageIcon(ImageUtils.createCenteredImage(getImageLibrary().getLibertyImage(), getListItemIconSize()));
for (NationType type : nations) {
// Suppress special case. FIXME: This is a kludge
if ("model.nationType.optionOnly".equals(type.getId())) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.sf.freecol.common.model.Modifier;
import net.sf.freecol.common.model.ResourceType;
import net.sf.freecol.common.model.TileType;
import net.sf.freecol.common.util.ImageUtils;


/**
Expand Down Expand Up @@ -76,17 +77,9 @@ public void addSubTrees(DefaultMutableTreeNode root) {
= new DefaultMutableTreeNode(new ColopediaTreeItem(this, getId(),
getName(), null));
for (TileType t : getSpecification().getTileTypeList()) {
BufferedImage tileImage
= getImageLibrary().getTileImageWithOverlayAndForest(t,
new Dimension(-1, ImageLibrary.ICON_SIZE.height));
BufferedImage image = new BufferedImage(tileImage.getWidth(null),
ImageLibrary.ICON_SIZE.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(tileImage, 0,
(ImageLibrary.ICON_SIZE.height - tileImage.getHeight(null)) / 2,
null);
g.dispose();
ImageIcon icon = new ImageIcon(image);
final Dimension size = getListItemIconSize();
final BufferedImage tileImage = getImageLibrary().getTileImageWithOverlayAndForest(t, new Dimension(-1, size.height));
final ImageIcon icon = new ImageIcon(ImageUtils.createCenteredImage(tileImage, size));
node.add(buildItem(t, icon));
}
root.add(node);
Expand Down
32 changes: 32 additions & 0 deletions src/net/sf/freecol/common/util/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ public static void fillTexture(Graphics2D g2d, BufferedImage img,
g2d.setPaint(paint);
g2d.fillRect(x, y, width, height);
}

/**
* Creates a new image of the given size with the provided image centered.
*
* @param image The image to be drawn in the center (both vertically and horizontally) of
* the new image.
* @param size The size of the new image.
* @return A new image.
*/
public static BufferedImage createCenteredImage(BufferedImage image, Dimension size) {
return createCenteredImage(image, size.width, size.height);
}

/**
* Creates a new image of the given size with the provided image centered.
*
* @param image The image to be drawn in the center (both vertically and horizontally) of
* the new image.
* @param width The width of the new image.
* @param height The height of the new image.
* @return A new image.
*/
public static BufferedImage createCenteredImage(BufferedImage image, int width, int height) {
final int x = (width - image.getWidth(null)) / 2;
final int y = (height - image.getHeight(null)) / 2;

final BufferedImage centeredImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g = centeredImage.createGraphics();
g.drawImage(image, x, y, null);
g.dispose();
return centeredImage;
}

/**
* Given a dimension with potential wildcard (non-positive) parts,
Expand Down

0 comments on commit cf37ae1

Please sign in to comment.