@@ -6,6 +6,7 @@ import Box from '@material-ui/core/Box';
66import IconButton from '@material-ui/core/IconButton' ;
77import TreeItem from '@material-ui/lab/TreeItem' ;
88import Visibility from '@material-ui/icons/Visibility' ;
9+ import VisibilityOff from '@material-ui/icons/VisibilityOff' ;
910import ColorLens from '@material-ui/icons/ColorLens' ;
1011import Shuffle from '@material-ui/icons/Shuffle' ;
1112import { ChromePicker } from 'react-color' ;
@@ -40,35 +41,84 @@ const ControlPanelTreeItem = (props) => {
4041 const dispatch = useDispatch ( ) ;
4142 const [ showColorPicker , setShowColorPicker ] = React . useState ( false ) ;
4243 const [ isHoveredOver , setIsHoveredOver ] = React . useState ( false ) ;
43- const [ color , setColor ] = React . useState ( '#ff0000' ) ;
44+ const [ color , setColor ] = React . useState ( {
45+ g : 0.50 , b : 0.60 , r : 1 , a : 1 ,
46+ } ) ;
47+ const [ visibility , setVisibility ] = React . useState ( true ) ;
4448 const instances = useSelector ( ( state ) => state . general . instances ) ;
4549
46- const handleColorSelection = ( color , event , nodeId ) => {
50+ const handleColorSelection = ( _color , event , nodeId ) => {
4751 const newInstances = instances . filter ( ( instance ) => ! ( instance . instancePath . startsWith ( nodeId ) ) ) ;
4852 newInstances . push ( {
4953 instancePath : nodeId ,
5054 color : {
51- r : color . rgb . r / 255 ,
52- g : color . rgb . g / 255 ,
53- b : color . rgb . b / 255 ,
54- a : color . rgb . a ,
55+ r : _color . rgb . r / 255 ,
56+ g : _color . rgb . g / 255 ,
57+ b : _color . rgb . b / 255 ,
58+ a : _color . rgb . a ,
5559 } ,
5660 } ) ;
5761 dispatch ( changeInstanceColor ( newInstances ) ) ;
58- console . log ( color ) ;
59- setColor ( color . hex ) ;
62+ setColor ( _color . rgb ) ;
6063 } ;
6164
62- const generateRandomColor = ( ) => {
65+ const generateRandomColor = ( event , nodeId ) => {
66+ const newInstances = instances . filter ( ( instance ) => ! ( instance . instancePath . startsWith ( nodeId ) ) ) ;
6367 const randomColor = {
64- r : parseFloat ( ( Math . random ( ) * 1.00 ) . toFixed ( 2 ) ) ,
65- g : parseFloat ( ( Math . random ( ) * 1.00 ) . toFixed ( 2 ) ) ,
66- b : parseFloat ( ( Math . random ( ) * 1.00 ) . toFixed ( 2 ) ) ,
68+ r : parseFloat ( ( Math . random ( ) * 255 ) . toFixed ( 2 ) ) ,
69+ g : parseFloat ( ( Math . random ( ) * 255 ) . toFixed ( 2 ) ) ,
70+ b : parseFloat ( ( Math . random ( ) * 255 ) . toFixed ( 2 ) ) ,
6771 a : 1 ,
6872 } ;
73+
74+ newInstances . push ( {
75+ instancePath : nodeId ,
76+ color : {
77+ r : randomColor . r / 255 ,
78+ g : randomColor . g / 255 ,
79+ b : randomColor . b / 255 ,
80+ a : randomColor . a ,
81+ } ,
82+ } ) ;
83+ dispatch ( changeInstanceColor ( newInstances ) ) ;
6984 setColor ( randomColor ) ;
7085 } ;
7186
87+ const changeVisibility = ( event , nodeId ) => {
88+ const copiedInstances = instances . slice ( ) ;
89+ let oldIndex = null ;
90+ let oldInstance = copiedInstances . find ( ( pInstance , index ) => {
91+ if ( pInstance . instancePath === nodeId ) {
92+ oldIndex = index ;
93+ return true ;
94+ }
95+ return false ;
96+ } ) ;
97+ if ( ! oldInstance ) {
98+ oldInstance = {
99+ instancePath : nodeId ,
100+ visibility : false ,
101+ } ;
102+ } else {
103+ copiedInstances . splice ( oldIndex , 1 ) ;
104+ oldInstance . visibility = ( oldInstance ?. visibility !== undefined ) ? ! oldInstance . visibility : false ;
105+ }
106+
107+ const newInstances = instances . map ( ( instance ) => {
108+ if ( ! ( instance . instancePath . startsWith ( nodeId ) ) ) {
109+ return instance ;
110+ }
111+ const newInstance = instance ;
112+ newInstance . visibility = oldInstance . visibility ;
113+ return newInstance ;
114+ } ) ;
115+
116+ newInstances . push ( oldInstance ) ;
117+
118+ dispatch ( changeInstanceColor ( newInstances ) ) ;
119+ setVisibility ( oldInstance . visibility ) ;
120+ } ;
121+
72122 const {
73123 label,
74124 type,
@@ -100,8 +150,10 @@ const ControlPanelTreeItem = (props) => {
100150 ? (
101151 < >
102152
103- < IconButton onClick = { ( event ) => onVisibilityClick ( event , nodeId ) } > < Visibility /> </ IconButton >
104- < IconButton onClick = { generateRandomColor } > < Shuffle /> </ IconButton >
153+ < IconButton onClick = { ( event ) => changeVisibility ( event , nodeId ) } >
154+ { visibility ? < Visibility /> : < VisibilityOff /> }
155+ </ IconButton >
156+ < IconButton onClick = { ( event ) => generateRandomColor ( event , nodeId ) } > < Shuffle /> </ IconButton >
105157 < IconButton onClick = { ( ) => setShowColorPicker ( true ) } > < ColorLens /> </ IconButton >
106158 {
107159 showColorPicker
0 commit comments