Skip to content

Commit

Permalink
csonsolidate changeAgentsColor and handler, and remove named ColorSet…
Browse files Browse the repository at this point in the history
…ting type from setInitialAgentColors
  • Loading branch information
interim17 committed Jul 18, 2024
1 parent d015892 commit 627c414
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
17 changes: 4 additions & 13 deletions src/simularium/SelectionInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class SelectionInterface {
public setInitialAgentColors(
uiDisplayData: UIDisplayData,
colors: (string | number)[],
setColorForIds: (setting: ColorSetting) => void
setColorForIds: (agentIds: number[], color: string | number) => void
): (string | number)[] {
let defaultColorIndex = 0;
uiDisplayData.forEach((group) => {
Expand All @@ -330,10 +330,7 @@ class SelectionInterface {
if (!hasNewColors) {
// if no colors have been set by the user for this name,
// just give all states of this agent name the same color
setColorForIds({
agentIds: ids,
color: colors[defaultColorIndex],
});
setColorForIds(ids, colors[defaultColorIndex]);
this.updateUiDataColor(
ids,
colors[defaultColorIndex],
Expand All @@ -359,8 +356,7 @@ class SelectionInterface {
// need update the display data with the default color being used
this.updateUiDataColor(
[ids[index]],
colors[groupColorIndex],
group.name
colors[groupColorIndex]
);
}
// if the user used all the same colors for all states of this agent,
Expand All @@ -371,10 +367,7 @@ class SelectionInterface {
} else {
groupColorIndex = -1;
}
setColorForIds({
agentIds: [ids[index]],
color: colors[agentColorIndex],
});
setColorForIds([ids[index]], colors[agentColorIndex]);
});
}
if (groupColorIndex > -1) {
Expand Down Expand Up @@ -410,15 +403,13 @@ class SelectionInterface {
color: agent.color,
name: agent.name,
});
// }

agent.displayStates.forEach((newState) => {
settings.push({
agentIds: this.getAgentIdsByNamesAndTags([
{ name: agent.name, tags: [newState.name] },
]),
color: newState.color,
name: newState.name,
});
});
});
Expand Down
31 changes: 13 additions & 18 deletions src/viewport/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ type ViewportProps = {
onTrajectoryFileInfoChanged: (
cachedData: TrajectoryFileInfo
) => void | undefined;
onUIDisplayDataChanged: (
data: UIDisplayData
) => void | undefined;
onUIDisplayDataChanged: (data: UIDisplayData) => void | undefined;
loadInitialData: boolean;
hideAllAgents: boolean;
showPaths: boolean;
Expand Down Expand Up @@ -235,9 +233,7 @@ class Viewport extends React.Component<
if (!isEqual(updatedColors, agentColors)) {
this.visGeometry.createMaterials(updatedColors);
}
onUIDisplayDataChanged(
this.selectionInterface.getUIDisplayData()
);
onUIDisplayDataChanged(this.selectionInterface.getUIDisplayData());
}

public componentDidMount(): void {
Expand Down Expand Up @@ -352,7 +348,7 @@ class Viewport extends React.Component<
) &&
selectionStateInfo.colorSettings.length > 0
) {
this.handleColorSettings(selectionStateInfo.colorSettings);
this.changeAgentsColor(selectionStateInfo.colorSettings);
}
}

Expand Down Expand Up @@ -601,12 +597,19 @@ class Viewport extends React.Component<
}
}

public changeAgentsColor(colorSettings: ColorSetting[]): void {
if (colorSettings.length === 0) {
public changeAgentsColor(newData: UIDisplayData): void {
console.log("changeAgentsColor, newData", newData);
if (newData.length === 0) {
return;
}
const colorSettings =
this.selectionInterface.deriveColorSettingsFromUIData(newData);
console.log("colorSettings in changeAgentsColor", colorSettings);
colorSettings.forEach((setting) => {
this.visGeometry.applyColorToAgents(setting);
this.visGeometry.applyColorToAgents(
setting.agentIds,
setting.color
);
this.selectionInterface.updateUiDataColor(
setting.agentIds,
setting.color,
Expand All @@ -615,14 +618,6 @@ class Viewport extends React.Component<
});
}

private handleColorSettings(newData: UIDisplayData): void {
// color sessions to do:
// only process necessary changes
const colorSettings =
this.selectionInterface.deriveColorSettingsFromUIData(newData);
this.changeAgentsColor(colorSettings);
}

public stopAnimate(): void {
if (this.animationRequestID !== 0) {
cancelAnimationFrame(this.animationRequestID);
Expand Down
6 changes: 4 additions & 2 deletions src/visGeometry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,10 @@ class VisGeometry {
this.setAgentColors();
}

public applyColorToAgents(setting: ColorSetting): void {
const { agentIds, color } = setting;
public applyColorToAgents(
agentIds: number[],
color: string | number
): void {
const newColorData = this.colorHandler.setColorForAgentTypes(
agentIds,
color
Expand Down

0 comments on commit 627c414

Please sign in to comment.