Skip to content

Commit

Permalink
Add missing docs to HexRow
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Jun 24, 2024
1 parent 1bc64a5 commit 2e2b25c
Showing 1 changed file with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import org.fxmisc.flowless.Cell;
import org.fxmisc.flowless.VirtualFlow;
import software.coley.recaf.ui.pane.editing.hex.HexConfig;
import software.coley.recaf.ui.pane.editing.hex.HexUtil;
import software.coley.recaf.ui.pane.editing.hex.ops.HexAccess;
Expand All @@ -21,6 +22,11 @@
import java.util.List;
import java.util.function.Consumer;

/**
* Cell for {@link VirtualFlow} to draw a row of bytes.
*
* @author Matt Coley
*/
public class HexRow implements Cell<Integer, Node> {
private final HBox layout = new HBox();
private final HexConfig config;
Expand All @@ -40,6 +46,9 @@ public HexRow(@Nonnull HexConfig config, @Nonnull IntegerProperty rowCount, @Non
buildLayout();
}

/**
* Re-populate the layout.
*/
public void redraw() {
reset();
buildLayout();
Expand Down Expand Up @@ -131,12 +140,21 @@ private void buildLayout() {
updateSelection(ops.navigation().selectionOffset());
}

/**
* @return {@code true} when this row contains the current selected offset.
*/
public boolean isRowSelected() {
return layout.getChildren().stream()
.anyMatch(child -> child instanceof HexCell cell
&& cell.offset() == ops.navigation().selectionOffset());
}

/**
* Notifies cells of selection updates, primarily to refresh visual cues.
*
* @param offset
* Target selection offset.
*/
public void updateSelection(int offset) {
CollectionUtil.safeForEach(layout.getChildren(), child -> {
boolean hexColumn = ops.navigation().isHexColumnSelected();
Expand All @@ -152,16 +170,31 @@ public void updateSelection(int offset) {
}, (cell, error) -> {});
}

/**
* @param offset
* Offset in the data to check.
*
* @return {@code true} if this row contains the offset.
*/
public boolean hasOffset(int offset) {
return offset >= baseOffset && offset <= baseOffset + config.getRowLength().getValue();
}

public void engage(int offset, boolean doEdit) {
/**
* Toggles editing in the cell at the given offset.
*
* @param offset
* Target offset to toggle editing on.
* @param initiateEdit
* {@code true} to trigger the target cell at the offset to begin editing.
* {@code false} to cancel editing.
*/
public void engage(int offset, boolean initiateEdit) {
Consumer<HexCellBase> action = cell -> {
boolean match = cell.offset() == offset;
if (match) {
if (!doEdit || cell.isEditing()) {
cell.endEdit(doEdit);
if (!initiateEdit || cell.isEditing()) {
cell.endEdit(false); // Cancel edit, do not commit
} else {
cell.beginEdit();
}
Expand All @@ -179,6 +212,14 @@ public void engage(int offset, boolean doEdit) {
}, (cell, error) -> {});
}

/**
* Finds the cell matching the given offset and delegates key handling for the given key-code to it.
*
* @param offset
* Target offset to send key to.
* @param code
* Key to send.
*/
public void sendKeyToCurrentEngaged(int offset, @Nonnull KeyCode code) {
Consumer<HexCellBase> action = cell -> {
boolean match = cell.offset() == offset;
Expand All @@ -196,6 +237,14 @@ public void sendKeyToCurrentEngaged(int offset, @Nonnull KeyCode code) {
}, (cell, error) -> {});
}

/**
* @param x
* Layout x position.
* @param y
* Layout y position.
*
* @return Offset into the data for the closest cell to the given coordinates.
*/
public int pickOffsetAtPosition(double x, double y) {
HexCell closestChild = null;
double closestDistance = Integer.MAX_VALUE;
Expand All @@ -220,6 +269,9 @@ public int pickOffsetAtPosition(double x, double y) {
return offset;
}

/**
* Fixed size spacer to put between columns.
*/
private static class SmallSpacer extends Spacer {
private SmallSpacer() {
setMaxWidth(12);
Expand Down

0 comments on commit 2e2b25c

Please sign in to comment.