Skip to content

Commit

Permalink
parse bitfield according to @tmadlener bitwise operations
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed Jul 15, 2024
1 parent 1fcfeab commit 28b2d70
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
28 changes: 16 additions & 12 deletions js/types/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { getName } from "../lib/getName.js";
import { linkTypes } from "./links.js";
import { parseCharge } from "../lib/parseCharge.js";
import { SimStatusBitFieldDisplayValues } from "../../mappings/sim-status.js";
import { getSimStatusDisplayValuesFromBit } from "../../mappings/sim-status.js";

const TOP_MARGIN = 40;

Expand Down Expand Up @@ -114,7 +114,17 @@ export class MCParticle extends EDMObject {
const topLines = [];
topLines.push("ID: " + this.index);
topLines.push("Gen. stat.: " + this.generatorStatus);
topLines.push("Sim. stat.: " + this.simulatorStatus);
const simulatorStatus = getSimStatusDisplayValuesFromBit(
this.simulatorStatus
);
const simulatorStatusFirstLetter = simulatorStatus
.map((s) => s[0])
.join(", ");
const simulatorStatusString =
simulatorStatusFirstLetter !== ""
? simulatorStatusFirstLetter
: this.simulatorStatus;
topLines.push("Sim. stat.: " + simulatorStatusString);

const bottomY = this.y + this.height * 0.6;
const bottomLines = [];
Expand All @@ -133,17 +143,11 @@ export class MCParticle extends EDMObject {
const x = this.x;
const y = this.y - 10;
const collectionName = "Collection: " + this.collectionName;
const displaySimulatorStatus =
SimStatusBitFieldDisplayValues[this.simulatorStatus];
let simulatorStatus = "";

if (displaySimulatorStatus) {
simulatorStatus = "Simulator status: " + displaySimulatorStatus;
} else {
simulatorStatus = "Simulator status: " + this.simulatorStatus;
}
const simulatorStatus = getSimStatusDisplayValuesFromBit(
this.simulatorStatus
);

drawObjectInfoTip(ctx, x, y, collectionName, simulatorStatus);
drawObjectInfoTip(ctx, x, y, collectionName, ...simulatorStatus);
}

static setup(mcCollection) {
Expand Down
24 changes: 24 additions & 0 deletions mappings/sim-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,27 @@ export const SimStatusBitFieldDisplayValues = {
29: "Backscatter",
30: "CreatedInSimulation",
};

export function parseBits(bit) {
const bits = [];

for (let i = 0; i < 32; i++) {
if (bit & (1 << i)) {
bits.push(i);
}
}

return bits;
}

export function getSimStatusDisplayValues(bits) {
return bits.map((bit) =>
SimStatusBitFieldDisplayValues[bit] !== undefined
? SimStatusBitFieldDisplayValues[bit]
: `Bit ${bit}`
);
}

export function getSimStatusDisplayValuesFromBit(bit) {
return getSimStatusDisplayValues(parseBits(bit));
}

0 comments on commit 28b2d70

Please sign in to comment.