color4LineComponent.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { 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 IColor4LineComponentProps {
  12. label: string;
  13. target: any;
  14. propertyName: string;
  15. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  16. onChange?: () => void;
  17. globalState: GlobalState;
  18. }
  19. export class Color4LineComponent extends React.Component<IColor4LineComponentProps, { isExpanded: boolean, color: Color4 }> {
  20. private _localChange = false;
  21. constructor(props: IColor4LineComponentProps) {
  22. super(props);
  23. let value = this.props.target[this.props.propertyName];
  24. let currentColor = value.getClassName() === "Color4" ? value.clone() : new Color4(value.r, value.g, value.b, 1.0);
  25. this.state = { isExpanded: false, color: currentColor };
  26. }
  27. shouldComponentUpdate(nextProps: IColor4LineComponentProps, nextState: { color: Color4 }) {
  28. const currentState = nextProps.target[nextProps.propertyName];
  29. let currentColor = currentState.getClassName() === "Color4" ? currentState : new Color4(currentState.r, currentState.g, currentState.b, 1.0);
  30. if (!currentColor.equals(nextState.color) || this._localChange) {
  31. nextState.color = currentColor.clone();
  32. this._localChange = false;
  33. return true;
  34. }
  35. return false;
  36. }
  37. onChange(newValue: string) {
  38. this._localChange = true;
  39. const newColor = Color4.FromHexString(newValue);
  40. if (this.props.onPropertyChangedObservable) {
  41. this.props.onPropertyChangedObservable.notifyObservers({
  42. object: this.props.target,
  43. property: this.props.propertyName,
  44. value: newColor,
  45. initialValue: this.state.color
  46. });
  47. }
  48. this.props.target[this.props.propertyName] = newColor;
  49. this.setState({ color: this.props.target[this.props.propertyName] });
  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: Color4) {
  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. updateStateA(value: number) {
  100. this._localChange = true;
  101. const store = this.state.color.clone();
  102. this.props.target[this.props.propertyName].a = value;
  103. this.state.color.a = value;
  104. this.props.target[this.props.propertyName] = this.state.color;
  105. this.setState({ color: this.state.color });
  106. this.raiseOnPropertyChanged(store);
  107. }
  108. copyToClipboard() {
  109. var element = document.createElement('div');
  110. element.textContent = this.state.color.toHexString();
  111. document.body.appendChild(element);
  112. if (window.getSelection) {
  113. var range = document.createRange();
  114. range.selectNode(element);
  115. window.getSelection()!.removeAllRanges();
  116. window.getSelection()!.addRange(range);
  117. }
  118. document.execCommand('copy');
  119. element.remove();
  120. }
  121. render() {
  122. const expandedIcon = this.state.isExpanded ? minusIcon : plusIcon;
  123. return (
  124. <div className="color3Line">
  125. <div className="firstLine">
  126. <div className="label">
  127. {this.props.label}
  128. </div>
  129. <div className="color3">
  130. <ColorPickerLineComponent globalState={this.props.globalState} value={this.state.color} onColorChanged={color => {
  131. this.onChange(color);
  132. }} />
  133. </div>
  134. <div className="copy hoverIcon" onClick={() => this.copyToClipboard()} title="Copy to clipboard">
  135. <img src={copyIcon} alt=""/>
  136. </div>
  137. <div className="expand hoverIcon" onClick={() => this.switchExpandState()} title="Expand">
  138. <img src={expandedIcon} alt=""/>
  139. </div>
  140. </div>
  141. {
  142. this.state.isExpanded &&
  143. <div className="secondLine">
  144. <NumericInputComponent globalState={this.props.globalState} label="r" value={this.state.color.r} onChange={(value) => this.updateStateR(value)} />
  145. <NumericInputComponent globalState={this.props.globalState} label="g" value={this.state.color.g} onChange={(value) => this.updateStateG(value)} />
  146. <NumericInputComponent globalState={this.props.globalState} label="b" value={this.state.color.b} onChange={(value) => this.updateStateB(value)} />
  147. <NumericInputComponent globalState={this.props.globalState} label="a" value={this.state.color.a} onChange={(value) => this.updateStateA(value)} />
  148. </div>
  149. }
  150. </div>
  151. );
  152. }
  153. }