Skip to content

Commit

Permalink
Add option to edit numeric data as unsigned number in structured reso…
Browse files Browse the repository at this point in the history
…urces

- Existing context menu entry "Edit as number" renamed to "Edit as signed number"
- Added new context menu entry "Edit as unsigned number"
- Values in "Edit as hexadecimal number" mode are treated as unsigned numbers
  • Loading branch information
Argent77 committed Jul 6, 2024
1 parent a7f717d commit 047cd5c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
33 changes: 33 additions & 0 deletions src/org/infinity/datatype/UnsignHexNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Near Infinity - An Infinity Engine Browser and Editor
// Copyright (C) 2001 Jon Olav Hauglid
// See LICENSE.txt for license information

package org.infinity.datatype;

import java.nio.ByteBuffer;

public class UnsignHexNumber extends UnsignDecNumber {
public UnsignHexNumber(ByteBuffer buffer, int offset, int length, String desc) {
super(buffer, offset, length, desc);
}

// --------------------- Begin Interface InlineEditable ---------------------

@Override
public boolean update(Object value) {
try {
setValue(UnsignDecNumber.parseNumber(value, getSize(), false, true));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

// --------------------- End Interface InlineEditable ---------------------

@Override
public String toString() {
return Long.toHexString(getLongValue() & 0xffffffffL) + " h";
}
}
23 changes: 18 additions & 5 deletions src/org/infinity/gui/StructViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
import org.infinity.datatype.Editable;
import org.infinity.datatype.EffectType;
import org.infinity.datatype.Flag;
import org.infinity.datatype.HexNumber;
import org.infinity.datatype.InlineEditable;
import org.infinity.datatype.IsNumeric;
import org.infinity.datatype.IsReference;
Expand All @@ -82,6 +81,8 @@
import org.infinity.datatype.Unknown;
import org.infinity.datatype.UnknownBinary;
import org.infinity.datatype.UnknownDecimal;
import org.infinity.datatype.UnsignDecNumber;
import org.infinity.datatype.UnsignHexNumber;
import org.infinity.gui.menu.BrowserMenuBar;
import org.infinity.gui.menu.ViewMode;
import org.infinity.icon.Icons;
Expand Down Expand Up @@ -129,6 +130,7 @@ public final class StructViewer extends JPanel implements ListSelectionListener,
public static final String CMD_TOBIN = "ToBin";
public static final String CMD_TODEC = "ToDec";
public static final String CMD_TOINT = "ToInt";
public static final String CMD_TOUINT = "ToUint";
public static final String CMD_TOHEXINT = "ToHexInt";
public static final String CMD_TOFLAGS = "ToFlags";
public static final String CMD_TORESLIST = "ToResList";
Expand Down Expand Up @@ -163,7 +165,8 @@ public final class StructViewer extends JPanel implements ListSelectionListener,
private final JMenuItem miToString = createMenuItem(CMD_TOSTRING, "Edit as string", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToBin = createMenuItem(CMD_TOBIN, "Edit as binary data", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToDec = createMenuItem(CMD_TODEC, "Edit as decimal data", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToInt = createMenuItem(CMD_TOINT, "Edit as number", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToInt = createMenuItem(CMD_TOINT, "Edit as signed number", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToUint = createMenuItem(CMD_TOUINT, "Edit as unsigned number", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToHexInt = createMenuItem(CMD_TOHEXINT, "Edit as hexadecimal number", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miToFlags = createMenuItem(CMD_TOFLAGS, "Edit as bit field", Icons.ICON_REFRESH_16.getIcon(), this);
private final JMenuItem miReset = createMenuItem(CMD_RESET, "Reset field type", Icons.ICON_REFRESH_16.getIcon(), this);
Expand Down Expand Up @@ -288,6 +291,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
popupmenu.add(miToBin);
popupmenu.add(miToDec);
popupmenu.add(miToInt);
popupmenu.add(miToUint);
popupmenu.add(miToHexInt);
popupmenu.add(miToFlags);
popupmenu.add(miToResref);
Expand All @@ -312,6 +316,7 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
miToBin.setEnabled(false);
miToDec.setEnabled(false);
miToInt.setEnabled(false);
miToUint.setEnabled(false);
miToHexInt.setEnabled(false);
miToFlags.setEnabled(false);
miToResref.setEnabled(false);
Expand Down Expand Up @@ -600,6 +605,8 @@ public void actionPerformed(ActionEvent event) {
convertAttribute(min, miToDec);
} else if (CMD_TOINT.equals(cmd)) {
convertAttribute(min, miToInt);
} else if (CMD_TOUINT.equals(cmd)) {
convertAttribute(min, miToUint);
} else if (CMD_TOHEXINT.equals(cmd)) {
convertAttribute(min, miToHexInt);
} else if (CMD_TOFLAGS.equals(cmd)) {
Expand Down Expand Up @@ -716,6 +723,7 @@ public void valueChanged(ListSelectionEvent event) {
miToBin.setEnabled(false);
miToDec.setEnabled(false);
miToInt.setEnabled(false);
miToUint.setEnabled(false);
miToHexInt.setEnabled(false);
miToFlags.setEnabled(false);
miToResref.setEnabled(false);
Expand Down Expand Up @@ -771,9 +779,12 @@ public void valueChanged(ListSelectionEvent event) {
miToInt.setEnabled(
isDataType && isReadable && ((Datatype) selected).getSize() <= 4 && !(selected instanceof SectionCount
|| selected instanceof SectionOffset || selected instanceof AbstractCode));
miToUint.setEnabled(
isDataType && isReadable && ((Datatype) selected).getSize() <= 4 && !(selected instanceof SectionCount
|| selected instanceof SectionOffset || selected instanceof AbstractCode));
miToHexInt.setEnabled(isDataType && isReadable && ((Datatype) selected).getSize() <= 4
&& !(selected instanceof HexNumber || selected instanceof SectionCount || selected instanceof SectionOffset
|| selected instanceof AbstractCode));
&& !(selected instanceof UnsignHexNumber || selected instanceof SectionCount ||
selected instanceof SectionOffset || selected instanceof AbstractCode));
miToFlags.setEnabled(isDataType && isReadable && ((Datatype) selected).getSize() <= 4
&& !(selected instanceof Flag || selected instanceof SectionCount || selected instanceof SectionOffset
|| selected instanceof AbstractCode));
Expand Down Expand Up @@ -1186,8 +1197,10 @@ private void convertAttribute(int index, JMenuItem menuitem) {
newentry = new UnknownDecimal(bb, 0, entry.getSize(), entry.getName());
} else if (menuitem == miToInt) {
newentry = new DecNumber(bb, 0, entry.getSize(), entry.getName());
} else if (menuitem == miToUint) {
newentry = new UnsignDecNumber(bb, 0, entry.getSize(), entry.getName());
} else if (menuitem == miToHexInt) {
newentry = new HexNumber(bb, 0, entry.getSize(), entry.getName());
newentry = new UnsignHexNumber(bb, 0, entry.getSize(), entry.getName());
} else if (menuitem == miToFlags) {
newentry = new Flag(bb, 0, entry.getSize(), entry.getName(), null);
} else if (CMD_TORESLIST.equals(menuitem.getActionCommand())) {
Expand Down

0 comments on commit 047cd5c

Please sign in to comment.