Skip to content

Commit

Permalink
Restructure WMP viewer and add XNEWAREA.2DA support for BG2:SoA worldmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Argent77 committed Jan 22, 2025
1 parent 272384c commit 7f6e116
Show file tree
Hide file tree
Showing 20 changed files with 1,803 additions and 375 deletions.
5 changes: 5 additions & 0 deletions src/org/infinity/gui/ViewerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ public JList<StructEntry> getList() {
return list;
}

/** Provides access to the model of the list component. */
public SimpleListModel<StructEntry> getListModel() {
return listModel;
}

@Override
public void actionPerformed(ActionEvent event) {
new ViewFrame(getTopLevelAncestor(), (Viewable) list.getSelectedValue());
Expand Down
10 changes: 9 additions & 1 deletion src/org/infinity/resource/key/BufferedResourceEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ public String getExtension() {

@Override
public ByteBuffer getResourceBuffer(boolean ignoreOverride) throws Exception {
return buffer;
// returns a deep copy
final ByteBuffer retVal =
buffer.isDirect() ? ByteBuffer.allocateDirect(buffer.capacity()) : ByteBuffer.allocate(buffer.capacity());
retVal.order(buffer.order());
final ByteBuffer bbRead = buffer.asReadOnlyBuffer();
bbRead.position(0);
retVal.put(bbRead);
retVal.flip();
return retVal;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/org/infinity/resource/text/PlainTextResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public class PlainTextResource

protected final String text;

private final ButtonPanel buttonPanel = new ButtonPanel();

private ButtonPanel buttonPanel;
private JMenuItem iFindAll;
private JMenuItem iFindThis;
private JMenuItem miFormatTrim;
Expand Down Expand Up @@ -528,6 +527,7 @@ public JComponent makeViewer(ViewableContainer container) {
editor.setWrapStyleWord(true);
}

buttonPanel = new ButtonPanel();
iFindAll = new JMenuItem("in all " + ext + " files");
iFindThis = new JMenuItem("in this file only");
ButtonPopupMenu bpmFind = (ButtonPopupMenu) buttonPanel.addControl(ButtonPanel.Control.FIND_MENU);
Expand Down
4 changes: 2 additions & 2 deletions src/org/infinity/resource/wmp/AreaEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.infinity.resource.AbstractStruct;
import org.infinity.resource.HasViewerTabs;

final public class AreaEntry extends AbstractStruct implements HasViewerTabs {
public class AreaEntry extends AbstractStruct implements HasViewerTabs {
// WMP/AreaEntry-specific field labels
public static final String WMP_AREA = "Area";
public static final String WMP_AREA_CURRENT = "Current area";
Expand All @@ -45,7 +45,7 @@ final public class AreaEntry extends AbstractStruct implements HasViewerTabs {
private static final String[] FLAGS_ARRAY = { "No flags set", "Visible", "Reveal from linked area", "Can be visited",
"Has been visited" };

AreaEntry(AbstractStruct superStruct, ByteBuffer buffer, int offset, int nr) throws Exception {
public AreaEntry(AbstractStruct superStruct, ByteBuffer buffer, int offset, int nr) throws Exception {
super(superStruct, WMP_AREA + " " + nr, buffer, offset);
}

Expand Down
6 changes: 3 additions & 3 deletions src/org/infinity/resource/wmp/AreaLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.infinity.resource.AbstractStruct;
import org.infinity.util.io.StreamUtils;

abstract class AreaLink extends AbstractStruct {
public abstract class AreaLink extends AbstractStruct {
// WMP/AreaLink-specific field labels
public static final String WMP_LINK_TARGET_AREA = "Target area";
public static final String WMP_LINK_TARGET_ENTRANCE = "Target entrance";
Expand All @@ -25,11 +25,11 @@ abstract class AreaLink extends AbstractStruct {

public static final String[] ENTRANCE_ARRAY = { "No default set", "North", "East", "South", "West" };

AreaLink(String name) throws Exception {
public AreaLink(String name) throws Exception {
super(null, name, StreamUtils.getByteBuffer(216), 0);
}

AreaLink(AbstractStruct superStruct, ByteBuffer buffer, int offset, String name) throws Exception {
public AreaLink(AbstractStruct superStruct, ByteBuffer buffer, int offset, String name) throws Exception {
super(superStruct, name, buffer, offset);
}

Expand Down
6 changes: 3 additions & 3 deletions src/org/infinity/resource/wmp/AreaLinkEast.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import org.infinity.resource.AbstractStruct;

class AreaLinkEast extends AreaLink {
public class AreaLinkEast extends AreaLink {
// WMP/AreaLinkEast-specific field labels
public static final String WMP_LINK_EAST = "East link";

AreaLinkEast() throws Exception {
public AreaLinkEast() throws Exception {
super(WMP_LINK_EAST);
}

AreaLinkEast(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
public AreaLinkEast(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
super(superStruct, buffer, offset, WMP_LINK_EAST + " " + number);
}
}
6 changes: 3 additions & 3 deletions src/org/infinity/resource/wmp/AreaLinkNorth.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import org.infinity.resource.AbstractStruct;

class AreaLinkNorth extends AreaLink {
public class AreaLinkNorth extends AreaLink {
// WMP/AreaLinkNorth-specific field labels
public static final String WMP_LINK_NORTH = "North link";

AreaLinkNorth() throws Exception {
public AreaLinkNorth() throws Exception {
super(WMP_LINK_NORTH);
}

AreaLinkNorth(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
public AreaLinkNorth(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
super(superStruct, buffer, offset, WMP_LINK_NORTH + " " + number);
}
}
6 changes: 3 additions & 3 deletions src/org/infinity/resource/wmp/AreaLinkSouth.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import org.infinity.resource.AbstractStruct;

class AreaLinkSouth extends AreaLink {
public class AreaLinkSouth extends AreaLink {
// WMP/AreaLinkSouth-specific field labels
public static final String WMP_LINK_SOUTH = "South link";

AreaLinkSouth() throws Exception {
public AreaLinkSouth() throws Exception {
super(WMP_LINK_SOUTH);
}

AreaLinkSouth(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
public AreaLinkSouth(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
super(superStruct, buffer, offset, WMP_LINK_SOUTH + " " + number);
}
}
6 changes: 3 additions & 3 deletions src/org/infinity/resource/wmp/AreaLinkWest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

import org.infinity.resource.AbstractStruct;

class AreaLinkWest extends AreaLink {
public class AreaLinkWest extends AreaLink {
// WMP/AreaLinkWest-specific field labels
public static final String WMP_LINK_WEST = "West link";

AreaLinkWest() throws Exception {
public AreaLinkWest() throws Exception {
super(WMP_LINK_WEST);
}

AreaLinkWest(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
public AreaLinkWest(AbstractStruct superStruct, ByteBuffer buffer, int offset, int number) throws Exception {
super(superStruct, buffer, offset, WMP_LINK_WEST + " " + number);
}
}
13 changes: 10 additions & 3 deletions src/org/infinity/resource/wmp/MapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.infinity.resource.AbstractStruct;
import org.infinity.resource.HasViewerTabs;
import org.infinity.resource.Profile;
import org.infinity.resource.wmp.viewer.ViewerMap;
import org.tinylog.Logger;

final public class MapEntry extends AbstractStruct implements HasViewerTabs {
public class MapEntry extends AbstractStruct implements HasViewerTabs {
// WMP/MapEntry-specific field labels
public static final String WMP_MAP = "Map";
public static final String WMP_MAP_RESREF = "Map";
Expand All @@ -38,7 +40,7 @@ final public class MapEntry extends AbstractStruct implements HasViewerTabs {

private static final String[] FLAGS_ARRAY = { "No flags set", "Colored icon", "Ignore palette" };

MapEntry(AbstractStruct superStruct, ByteBuffer buffer, int offset, int nr) throws Exception {
public MapEntry(AbstractStruct superStruct, ByteBuffer buffer, int offset, int nr) throws Exception {
super(superStruct, WMP_MAP + " " + nr, buffer, offset);
}

Expand All @@ -56,7 +58,12 @@ public String getViewerTabName(int index) {

@Override
public JComponent getViewerTab(int index) {
return new ViewerMap(this);
try {
return new ViewerMap(this);
} catch (Exception e) {
Logger.error(e);
}
return null;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/org/infinity/resource/wmp/ViewerArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.infinity.icon.Icons;
import org.infinity.resource.Viewable;

final class ViewerArea extends JPanel implements ActionListener {
class ViewerArea extends JPanel implements ActionListener {
private final JButton bOpen = new JButton("View/Edit", Icons.ICON_ZOOM_16.getIcon());

private JList<Object> list;
Expand All @@ -41,7 +41,7 @@ private static JPanel makeInfoPanel(AreaEntry areaEntry) {
return panel;
}

ViewerArea(AreaEntry areaEntry) {
public ViewerArea(AreaEntry areaEntry) {
JPanel flagPanel = ViewerUtil.makeCheckPanel((Flag) areaEntry.getAttribute(AreaEntry.WMP_AREA_FLAGS), 1);
JPanel infoPane = makeInfoPanel(areaEntry);
JComponent icon = ViewerUtil.makeBamPanel((ResourceRef) areaEntry.getParent().getAttribute(MapEntry.WMP_MAP_ICONS),
Expand Down
2 changes: 1 addition & 1 deletion src/org/infinity/resource/wmp/WmpResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* <p>
* A WMP resource must have at least one area entry, and one area link to be considered valid.
*/
public final class WmpResource extends AbstractStruct implements Resource, HasViewerTabs {
public class WmpResource extends AbstractStruct implements Resource, HasViewerTabs {
// WMP-specific field labels
public static final String WMP_NUM_MAPS = "# maps";
public static final String WMP_OFFSET_MAPS = "Maps offset";
Expand Down
122 changes: 122 additions & 0 deletions src/org/infinity/resource/wmp/viewer/AreaListCellRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Near Infinity - An Infinity Engine Browser and Editor
// Copyright (C) 2001 Jon Olav Hauglid
// See LICENSE.txt for license information

package org.infinity.resource.wmp.viewer;

import java.awt.Component;
import java.util.Locale;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;

import org.infinity.datatype.IsNumeric;
import org.infinity.datatype.IsReference;
import org.infinity.datatype.StringRef;
import org.infinity.gui.ViewerUtil.ListValueRenderer;
import org.infinity.gui.menu.BrowserMenuBar;
import org.infinity.resource.AbstractStruct;
import org.infinity.resource.graphics.BamDecoder;
import org.infinity.resource.graphics.BamDecoder.BamControl;
import org.infinity.resource.graphics.BamDecoder.FrameEntry;
import org.infinity.resource.wmp.AreaEntry;
import org.infinity.util.StringTable;

/**
* Specialized list item renderer for worldmap area entries.
*/
final class AreaListCellRenderer extends DefaultListCellRenderer implements ListValueRenderer {
private static final int TEXT_GAP = 4;

private final BamDecoder bam;
private final BamControl ctrl;
private final int maxFrameWidth;

public AreaListCellRenderer(BamDecoder decoder) {
bam = decoder;
ctrl = (bam != null) ? bam.createControl() : null;

int maxWidth = 0;
for (int i = 0, count = ctrl.cycleCount(); i < count; i++) {
if (ctrl.cycleFrameCount(i) > 0) {
final int frameIndex = ctrl.cycleGetFrameIndexAbsolute(i, 0);
if (frameIndex >= 0) {
final FrameEntry fe = bam.getFrameInfo(frameIndex);
if (fe != null) {
maxWidth = Math.max(maxWidth, fe.getWidth());
}
}
}
}
maxFrameWidth = maxWidth;
}

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
label.setText(getListValue(value, true));

int iconIndex = -1;
if (value instanceof AbstractStruct) {
final AbstractStruct struct = (AbstractStruct)value;
iconIndex = ((IsNumeric) struct.getAttribute(AreaEntry.WMP_AREA_ICON_INDEX)).getValue();
} else if (value instanceof VirtualAreaEntry) {
final VirtualAreaEntry vae = (VirtualAreaEntry)value;
iconIndex = vae.getAreaIconIndex();
}

if (ctrl != null && iconIndex >= 0) {
final int frameIndex = ctrl.cycleGetFrameIndexAbsolute(iconIndex, 0);
setIcon(new ImageIcon(bam.frameGet(ctrl, frameIndex)));

// properly aligning label text
final FrameEntry fe = bam.getFrameInfo(frameIndex);
final int gap = Math.max(0, maxFrameWidth - fe.getWidth());
setIconTextGap(gap + TEXT_GAP);
} else {
setIcon(null);
setIconTextGap(maxFrameWidth + TEXT_GAP);
}

return label;
}

@Override
public String getListValue(Object value) {
return getListValue(value, false);
}

private String getListValue(Object value, boolean showFull) {
String text1 = "";
String text2 = "";
StringTable.Format fmt = BrowserMenuBar.getInstance().getOptions().showStrrefs() ? StringTable.Format.STRREF_SUFFIX
: StringTable.Format.NONE;

if (value instanceof AbstractStruct) {
final AbstractStruct struct = (AbstractStruct)value;

StringRef areaName = (StringRef)struct.getAttribute(AreaEntry.WMP_AREA_NAME);
IsReference areaRef = (IsReference)struct.getAttribute(AreaEntry.WMP_AREA_CURRENT);
if (areaName.getValue() >= 0) {
text1 = StringTable.getStringRef(areaName.getValue(), fmt);
}
text2 = areaRef.getResourceName();
} else if (value instanceof VirtualAreaEntry) {
final VirtualAreaEntry vae = (VirtualAreaEntry)value;
text1 = StringTable.getStringRef(vae.getAreaLabelStrref(), fmt);
text2 = vae.getAreaResource().getResourceName();
}

if (!text2.equalsIgnoreCase("NONE")) {
text2 = text2.toUpperCase(Locale.ENGLISH).replace(".ARE", "");
}

if (showFull) {
return '[' + text2 + "] " + text1;
}
return text2;
}
}
Loading

0 comments on commit 7f6e116

Please sign in to comment.