Skip to content

Commit

Permalink
Add "Properties..." button to graphics resource viewer (BMP, PNG)
Browse files Browse the repository at this point in the history
  • Loading branch information
Argent77 committed Jul 21, 2023
1 parent 589bd53 commit cb24e1a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/org/infinity/resource/graphics/BmpDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private void init(ByteBuffer buffer) throws Exception {
pb.rewind();
palette = new Palette(pb, 0, pb.capacity());
}
info = new Info(image, 0, 0);
info = new Info(image);
} catch (Exception e) {
image = null;
palette = null;
Expand Down
27 changes: 27 additions & 0 deletions src/org/infinity/resource/graphics/GraphicsResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Locale;
import java.util.function.Function;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.infinity.gui.ButtonPanel;
import org.infinity.gui.RenderCanvas;
import org.infinity.icon.Icons;
import org.infinity.resource.Referenceable;
import org.infinity.resource.Resource;
import org.infinity.resource.ResourceFactory;
Expand All @@ -26,6 +30,8 @@
import org.infinity.search.ReferenceSearcher;

public final class GraphicsResource implements Resource, Referenceable, ActionListener {
private static final ButtonPanel.Control PROPERTIES = ButtonPanel.Control.CUSTOM_1;

private final ResourceEntry entry;
private final BmpDecoder decoder;
private final ButtonPanel buttonPanel = new ButtonPanel();
Expand All @@ -45,6 +51,8 @@ public void actionPerformed(ActionEvent event) {
searchReferences(panel.getTopLevelAncestor());
} else if (buttonPanel.getControlByType(ButtonPanel.Control.EXPORT_BUTTON) == event.getSource()) {
ResourceFactory.exportResource(entry, panel.getTopLevelAncestor());
} else if (buttonPanel.getControlByType(PROPERTIES) == event.getSource()) {
showProperties();
}
}

Expand Down Expand Up @@ -85,6 +93,11 @@ public JComponent makeViewer(ViewableContainer container) {
((JButton) buttonPanel.addControl(ButtonPanel.Control.FIND_REFERENCES)).addActionListener(this);
((JButton) buttonPanel.addControl(ButtonPanel.Control.EXPORT_BUTTON)).addActionListener(this);

JButton bProperties = new JButton("Properties...", Icons.ICON_EDIT_16.getIcon());
bProperties.setMnemonic('p');
bProperties.addActionListener(this);
buttonPanel.addControl(bProperties, PROPERTIES);

panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
Expand All @@ -106,4 +119,18 @@ public Palette getPalette() {
public BmpDecoder.Info getInfo() {
return decoder.getInfo();
}

private void showProperties() {
// Width, Height, BitsPerPixel, Compression
final Function<Integer, String> space = (i) -> new String(new char[i]).replace("\0", "&nbsp;");
final String br = "<br/>";
final String resName = entry.getResourceName().toUpperCase(Locale.ENGLISH);
final StringBuilder sb = new StringBuilder("<html><div style='font-family:monospace'>");
sb.append("Width:").append(space.apply(7)).append(getInfo().getWidth()).append(br);
sb.append("Height:").append(space.apply(6)).append(getInfo().getHeight()).append(br);
sb.append("Bits/Pixel:").append(space.apply(2)).append(getInfo().getBitsPerPixel()).append(br);
sb.append("</div></html>");

JOptionPane.showMessageDialog(panel, sb.toString(), "Properties of " + resName, JOptionPane.INFORMATION_MESSAGE);
}
}

0 comments on commit cb24e1a

Please sign in to comment.