color3LineComponent.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { Color3, Color4 } from "babylonjs/Maths/math.color";
  4. import { PropertyChangedEvent } from "./propertyChangedEvent";
  5. import { NumericInputComponent } from "./numericInputComponent";
  6. import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
  7. import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons";
  8. import { GlobalState } from '../globalState';
  9. const copyIcon: string = require("./copy.svg");
  10. export interface IColor3LineComponentProps {
  11. label: string;
  12. target: any;
  13. propertyName: string;
  14. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  15. onChange?: () => void;
  16. globalState: GlobalState;
  17. }
  18. export class Color3LineComponent extends React.Component<IColor3LineComponentProps, { isExpanded: boolean, color: Color3 }> {
  19. private _localChange = false;
  20. constructor(props: IColor3LineComponentProps) {
  21. super(props);
  22. this.state = { isExpanded: false, color: this.props.target[this.props.propertyName].clone() };
  23. }
  24. shouldComponentUpdate(nextProps: IColor3LineComponentProps, nextState: { color: Color3 }) {
  25. const currentState = nextProps.target[nextProps.propertyName];
  26. if (!currentState.equals(nextState.color) || this._localChange) {
  27. nextState.color = currentState.clone();
  28. this._localChange = false;
  29. return true;
  30. }
  31. return false;
  32. }
  33. onChange(newValue: string) {
  34. this._localChange = true;
  35. const newColor = Color3.FromHexString(newValue);
  36. if (this.props.onPropertyChangedObservable) {
  37. this.props.onPropertyChangedObservable.notifyObservers({
  38. object: this.props.target,
  39. property: this.props.propertyName,
  40. value: newColor,
  41. initialValue: this.state.color
  42. });
  43. }
  44. if (this.props.target[this.props.propertyName].getClassName() === "Color4") {
  45. this.props.target[this.props.propertyName] = new Color4(newColor.r, newColor.g, newColor.b, 1.0);
  46. } else {
  47. this.props.target[this.props.propertyName] = newColor;
  48. }
  49. this.setState({ color: newColor });
  50. if (this.props.onChange) {
  51. this.props.onChange();
  52. }
  53. }
  54. switchExpandState() {
  55. this._localChange = true;
  56. this.setState({ isExpanded: !this.state.isExpanded });
  57. }
  58. raiseOnPropertyChanged(previousValue: Color3) {
  59. if (this.props.onChange) {
  60. this.props.onChange();
  61. }
  62. if (!this.props.onPropertyChangedObservable) {
  63. return;
  64. }
  65. this.props.onPropertyChangedObservable.notifyObservers({
  66. object: this.props.target,
  67. property: this.props.propertyName,
  68. value: this.state.color,
  69. initialValue: previousValue
  70. });
  71. }
  72. updateStateR(value: number) {
  73. this._localChange = true;
  74. const store = this.state.color.clone();
  75. this.props.target[this.props.propertyName].x = value;
  76. this.state.color.r = value;
  77. this.props.target[this.props.propertyName] = this.state.color;
  78. this.setState({ color: this.state.color });
  79. this.raiseOnPropertyChanged(store);
  80. }
  81. updateStateG(value: number) {
  82. this._localChange = true;
  83. const store = this.state.color.clone();
  84. this.props.target[this.props.propertyName].g = value;
  85. this.state.color.g = value;
  86. this.props.target[this.props.propertyName] = this.state.color;
  87. this.setState({ color: this.state.color });
  88. this.raiseOnPropertyChanged(store);
  89. }
  90. updateStateB(value: number) {
  91. this._localChange = true;
  92. const store = this.state.color.clone();
  93. this.props.target[this.props.propertyName].b = value;
  94. this.state.color.b = value;
  95. this.props.target[this.props.propertyName] = this.state.color;
  96. this.setState({ color: this.state.color });
  97. this.raiseOnPropertyChanged(store);
  98. }
  99. copyToClipboard() {
  100. var element = document.createElement('div');
  101. element.textContent = this.state.color.toHexString();
  102. document.body.appendChild(element);
  103. if (window.getSelection) {
  104. var range = document.createRange();
  105. range.selectNode(element);
  106. window.getSelection()!.removeAllRanges();
  107. window.getSelection()!.addRange(range);
  108. }
  109. document.execCommand('copy');
  110. element.remove();
  111. }
  112. render() {
  113. const chevron = this.state.isExpanded ? <FontAwesomeIcon icon={faMinus} /> : <FontAwesomeIcon icon={faPlus} />;
  114. const colorAsColor3 = this.state.color.getClassName() === "Color3" ? this.state.color : new Color3(this.state.color.r, this.state.color.g, this.state.color.b);
  115. return (
  116. <div className="color3Line">
  117. <div className="firstLine">
  118. <div className="label">
  119. {this.props.label}
  120. </div>
  121. <div className="color3">
  122. <input type="color" value={colorAsColor3.toHexString()} onChange={(evt) => this.onChange(evt.target.value)} />
  123. </div>
  124. <div className="copy hoverIcon" onClick={() => this.copyToClipboard()} title="Copy to clipboard">
  125. <img src={copyIcon} alt=""/>
  126. </div>
  127. <div className="expand hoverIcon" onClick={() => this.switchExpandState()} title="Expand">
  128. {chevron}
  129. </div>
  130. </div>
  131. {
  132. this.state.isExpanded &&
  133. <div className="secondLine">
  134. <NumericInputComponent globalState={this.props.globalState} label="r" value={this.state.color.r} onChange={(value) => this.updateStateR(value)} />
  135. <NumericInputComponent globalState={this.props.globalState} label="g" value={this.state.color.g} onChange={(value) => this.updateStateG(value)} />
  136. <NumericInputComponent globalState={this.props.globalState} label="b" value={this.state.color.b} onChange={(value) => this.updateStateB(value)} />
  137. </div>
  138. }
  139. </div>
  140. );
  141. }
  142. }