colorPickerComponent.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import * as React from "react";
  2. import { Color4, Color3 } from 'babylonjs/Maths/math.color';
  3. import { SketchPicker } from 'react-color';
  4. import { GlobalState } from '../globalState';
  5. export interface IColorPickerComponentProps {
  6. value: Color4 | Color3;
  7. onColorChanged: (newOne: string) => void;
  8. globalState: GlobalState;
  9. disableAlpha?: boolean;
  10. }
  11. interface IColorPickerComponentState {
  12. pickerEnabled: boolean;
  13. color: {
  14. r: number,
  15. g: number,
  16. b: number,
  17. a?: number
  18. },
  19. hex: string
  20. }
  21. export class ColorPickerLineComponent extends React.Component<IColorPickerComponentProps, IColorPickerComponentState> {
  22. private _floatRef: React.RefObject<HTMLDivElement>
  23. private _floatHostRef: React.RefObject<HTMLDivElement>
  24. constructor(props: IColorPickerComponentProps) {
  25. super(props);
  26. this.state = {pickerEnabled: false, color: {
  27. r: this.props.value.r * 255,
  28. g: this.props.value.g * 255,
  29. b: this.props.value.b * 255,
  30. a: this.props.value instanceof Color4 ? this.props.value.a * 100 : 100,
  31. }, hex: this.props.value.toHexString()};
  32. this._floatRef = React.createRef();
  33. this._floatHostRef = React.createRef();
  34. }
  35. syncPositions() {
  36. const div = this._floatRef.current as HTMLDivElement;
  37. const host = this._floatHostRef.current as HTMLDivElement;
  38. if (!div || !host) {
  39. return;
  40. }
  41. let top = host.getBoundingClientRect().top;
  42. let height = div.getBoundingClientRect().height;
  43. if (top + height + 10 > window.innerHeight) {
  44. top = window.innerHeight - height - 10;
  45. }
  46. div.style.top = top + "px";
  47. div.style.left = host.getBoundingClientRect().left - div.getBoundingClientRect().width + "px";
  48. }
  49. shouldComponentUpdate(nextProps: IColorPickerComponentProps, nextState: IColorPickerComponentState) {
  50. let result = nextProps.value.toHexString() !== this.props.value.toHexString()
  51. || nextProps.disableAlpha !== this.props.disableAlpha
  52. || nextState.hex !== this.state.hex
  53. || nextState.pickerEnabled !== this.state.pickerEnabled;
  54. if(result) {
  55. nextState.color = {
  56. r: nextProps.value.r * 255,
  57. g: nextProps.value.g * 255,
  58. b: nextProps.value.b * 255,
  59. a: nextProps.value instanceof Color4 ? nextProps.value.a : 1,
  60. };
  61. nextState.hex = nextProps.value.toHexString();
  62. }
  63. return result;
  64. }
  65. componentDidUpdate() {
  66. this.syncPositions();
  67. }
  68. componentDidMount() {
  69. this.syncPositions();
  70. }
  71. setPickerState(enabled: boolean) {
  72. this.setState({ pickerEnabled: enabled });
  73. this.props.globalState.blockKeyboardEvents = enabled;
  74. }
  75. render() {
  76. var color = this.state.color;
  77. this.props.globalState.blockKeyboardEvents = this.state.pickerEnabled;
  78. return (
  79. <div className="color-picker">
  80. <div className="color-rect" ref={this._floatHostRef}
  81. style={{background: this.state.hex}}
  82. onClick={() => this.setPickerState(true)}>
  83. </div>
  84. {
  85. this.state.pickerEnabled &&
  86. <>
  87. <div className="color-picker-cover" onClick={() => this.setPickerState(false)}></div>
  88. <div className="color-picker-float" ref={this._floatRef}>
  89. <SketchPicker color={color}
  90. disableAlpha={this.props.disableAlpha}
  91. onChange={(color) => {
  92. let hex: string;
  93. if (this.props.disableAlpha) {
  94. let newColor3 = Color3.FromInts(color.rgb.r, color.rgb.g, color.rgb.b);
  95. hex = newColor3.toHexString();
  96. } else {
  97. let newColor4 = Color4.FromInts(color.rgb.r, color.rgb.g, color.rgb.b, 255 * (color.rgb.a || 0));
  98. hex = newColor4.toHexString();
  99. }
  100. this.setState({hex: hex, color: color.rgb});
  101. this.props.onColorChanged(hex);
  102. }}
  103. />
  104. </div>
  105. </>
  106. }
  107. </div>
  108. );
  109. }
  110. }