diff --git a/examples/ColorPicker.tsx b/examples/ColorPicker.tsx new file mode 100644 index 00000000..a2814b9d --- /dev/null +++ b/examples/ColorPicker.tsx @@ -0,0 +1,123 @@ +import React, { useState } from "react"; +import { SelectionEntry } from "../type-declarations/simularium/SelectionInterface"; + +const ColorPicker = ({ + uiDisplayData, + particleTypeNames, + agentColors, + setColorSelectionInfo, + updateAgentColorArray, +}) => { + const [subAgents, setSubAgents] = useState([{ name: "", id: "" }]); + const [selectedAgent, setSelectedAgent] = useState(""); + const [selectedColor, setSelectedColor] = useState(""); + const [selectedSubagent, setSelectedSubAgent] = useState(""); + const [colorToAppend, setColorToAppend] = useState(""); + + const getSubAgentsforAgent = (agentName: string) => { + const agent = uiDisplayData.find( + (element) => element.name === agentName + ); + if (!agent) { + throw new Error("No agent found"); + } + setSubAgents(agent.displayStates); + }; + + const handleAgentSelection = (event) => { + const value = event.target.value; + getSubAgentsforAgent(value); + setSelectedAgent(value); + }; + + const assignColorToAgent = () => { + if (!selectedAgent) { + throw new Error("No agent selected"); + } else if (!selectedColor) { + throw new Error("No color selected"); + } else { + let subAgent: string[] = selectedSubagent ? [selectedSubagent] : []; + // hooks doesn't save an empty string + // but an empty string is a possible tag + // that represents the unmodified state + if (selectedSubagent === "") { + subAgent = [""]; + } + const entry: SelectionEntry[] = [ + { + name: selectedAgent, + tags: subAgent, + }, + ]; + setColorSelectionInfo([{ + agents: entry, + color: selectedColor, + }]); + } + }; + + const addColorToColorArray = (customColor: string) => { + const hexColorCodeRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/; + const color = customColor; + if (hexColorCodeRegex.test(color)) { + updateAgentColorArray(color); + } else { + alert("Please enter a valid hex color code"); + } + }; + + return ( + <> + Color change agent selections: + + + + + { + console.log(event.target.value) + setColorToAppend(event.target.value)}} + > + + + ); +}; + +export default ColorPicker; diff --git a/examples/Viewer.tsx b/examples/Viewer.tsx index 7cd831bc..45db80fb 100644 --- a/examples/Viewer.tsx +++ b/examples/Viewer.tsx @@ -20,6 +20,7 @@ import PointSimulatorLive from "./PointSimulatorLive"; import PdbSimulator from "./PdbSimulator"; import SinglePdbSimulator from "./SinglePdbSimulator"; import CurveSimulator from "./CurveSimulator"; +import ColorPicker from "./ColorPicker"; import { SMOLDYN_TEMPLATE, UI_BASE_TYPES, @@ -139,6 +140,7 @@ const initialState: ViewerState = { selectionStateInfo: { highlightedAgents: [], hiddenAgents: [], + colorChanges: [{ agents: [], color: "" }], }, filePending: null, simulariumFile: null, @@ -354,9 +356,8 @@ class Viewer extends React.Component { } public convertFile(obj: Record, fileType: TrajectoryType) { - simulariumController.convertAndLoadTrajectory( - this.netConnectionSettings, obj, fileType - ) + simulariumController + .convertAndLoadTrajectory(this.netConnectionSettings, obj, fileType) .then(() => { this.clearPendingFile(); }) @@ -590,6 +591,23 @@ class Viewer extends React.Component { URL.revokeObjectURL(downloadLink.href); } + public updateAgentColorArray = (color) => { + const agentColors = [...this.state.agentColors, color] as string[]; + this.setState({ agentColors }); + }; + + public setColorSelectionInfo = (colorChanges) => { + this.setState({ + ...this.state, + selectionStateInfo: { + hiddenAgents: this.state.selectionStateInfo.hiddenAgents, + highlightedAgents: + this.state.selectionStateInfo.highlightedAgents, + colorChanges: colorChanges, + }, + }); + }; + public render(): JSX.Element { if (this.state.filePending) { const fileType = this.state.filePending.type; @@ -624,15 +642,33 @@ class Viewer extends React.Component { - - - - - - - - - + + + + + + + + + @@ -795,7 +831,9 @@ class Viewer extends React.Component {