import * as React from "react"; import { Observable } from "babylonjs/Misc/observable"; import { PropertyChangedEvent } from "../propertyChangedEvent"; import { NumericInputComponent } from "./numericInputComponent"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons"; import { Color3 } from 'babylonjs/Maths/math.color'; import { ColorPickerLineComponent } from './colorPickerComponent'; const copyIcon: string = require("./copy.svg"); export interface IColor3LineComponentProps { label: string; target: any; propertyName: string; onPropertyChangedObservable?: Observable; isLinear?: boolean; } export class Color3LineComponent extends React.Component { private _localChange = false; constructor(props: IColor3LineComponentProps) { super(props); this.state = { isExpanded: false, color: this.props.target[this.props.propertyName].clone() }; if (props.isLinear) { this.state.color.toGammaSpaceToRef(this.state.color); } props.target._isLinearColor = props.isLinear; // so that replayRecorder can append toLinearSpace() as appropriate } shouldComponentUpdate(nextProps: IColor3LineComponentProps, nextState: { color: Color3 }) { const currentState = this.props.isLinear ? nextProps.target[nextProps.propertyName].toGammaSpace() : nextProps.target[nextProps.propertyName]; if (!currentState.equals(nextState.color) || this._localChange) { nextState.color = currentState.clone(); this._localChange = false; return true; } return false; } setPropertyValue(newColor: Color3) { this.props.target[this.props.propertyName] = newColor; if (this.props.isLinear) { this.props.target[this.props.propertyName] = newColor.toLinearSpace(); } } onChange(newValue: string) { this._localChange = true; const newColor = Color3.FromHexString(newValue); if (this.props.onPropertyChangedObservable) { this.props.onPropertyChangedObservable.notifyObservers({ object: this.props.target, property: this.props.propertyName, value: newColor, initialValue: this.state.color, }); } this.setPropertyValue(newColor); this.setState({ color: newColor }); } switchExpandState() { this._localChange = true; this.setState({ isExpanded: !this.state.isExpanded }); } raiseOnPropertyChanged(previousValue: Color3) { if (!this.props.onPropertyChangedObservable) { return; } this.props.onPropertyChangedObservable.notifyObservers({ object: this.props.target, property: this.props.propertyName, value: this.state.color, initialValue: previousValue, }); } updateStateR(value: number) { this._localChange = true; const store = this.state.color.clone(); this.state.color.r = value; this.setPropertyValue(this.state.color); this.setState({ color: this.state.color }); this.raiseOnPropertyChanged(store); } updateStateG(value: number) { this._localChange = true; const store = this.state.color.clone(); this.state.color.g = value; this.setPropertyValue(this.state.color); this.setState({ color: this.state.color }); this.raiseOnPropertyChanged(store); } updateStateB(value: number) { this._localChange = true; const store = this.state.color.clone(); this.state.color.b = value; this.setPropertyValue(this.state.color); this.setState({ color: this.state.color }); this.raiseOnPropertyChanged(store); } copyToClipboard() { var element = document.createElement('div'); element.textContent = this.state.color.toHexString(); document.body.appendChild(element); if (window.getSelection) { var range = document.createRange(); range.selectNode(element); window.getSelection()!.removeAllRanges(); window.getSelection()!.addRange(range); } document.execCommand('copy'); element.remove(); } render() { const chevron = this.state.isExpanded ? : ; return (
{this.props.label}
{ this.onChange(color); }} />
this.copyToClipboard()} title="Copy to clipboard">
this.switchExpandState()} title="Expand"> {chevron}
{ this.state.isExpanded &&
this.updateStateR(value)} /> this.updateStateG(value)} /> this.updateStateB(value)} />
}
); } }