color3LineComponent.tsx 6.4 KB

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