commandDropdownComponent.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Engine } from "babylonjs/Engines/engine";
  2. import * as React from "react";
  3. import { GlobalState } from '../globalState';
  4. import { Utilities } from '../tools/utilities';
  5. interface ICommandDropdownComponentProps {
  6. globalState: GlobalState;
  7. icon?: string;
  8. tooltip: string;
  9. defaultValue?: string;
  10. items: {
  11. label: string,
  12. onClick?: () => void,
  13. onCheck?: (value: boolean) => void,
  14. storeKey?: string,
  15. isActive?: boolean,
  16. defaultValue?: boolean | string;
  17. subItems?: string[];
  18. }[];
  19. toRight?: boolean;
  20. }
  21. export class CommandDropdownComponent extends React.Component<ICommandDropdownComponentProps, {isExpanded: boolean, activeState: string}> {
  22. public constructor(props: ICommandDropdownComponentProps) {
  23. super(props);
  24. this.state = {isExpanded: false, activeState: Utilities.ReadStringFromStore(this.props.tooltip, this.props.defaultValue!)};
  25. this.props.globalState.OnNewDropdownButtonClicked.add((source) => {
  26. if (source === this) {
  27. return;
  28. }
  29. this.setState({isExpanded: false});
  30. });
  31. }
  32. public render() {
  33. var engineVersionSub = Engine.Version.indexOf("-");
  34. var engineVersion = Engine.Version;
  35. if (engineVersionSub ! -1) {
  36. engineVersion = engineVersion.substr(0, engineVersionSub);
  37. }
  38. return (
  39. <>
  40. {
  41. this.state.isExpanded &&
  42. <div className="command-dropdown-blocker" onClick={() => {
  43. this.setState({isExpanded: false});
  44. }}>
  45. </div>
  46. }
  47. <div className="command-dropdown-root">
  48. <div className={"command-dropdown" + (this.state.isExpanded ? " activated" : "")} title={this.props.tooltip}
  49. onClick={() => {
  50. this.props.globalState.OnNewDropdownButtonClicked.notifyObservers(this);
  51. this.setState({isExpanded: !this.state.isExpanded});
  52. }}>
  53. {
  54. this.props.icon &&
  55. <div className="command-dropdown-icon">
  56. <img src={"imgs/" + this.props.icon + ".svg"}/>
  57. </div>
  58. }
  59. {
  60. !this.props.icon &&
  61. <div className="command-dropdown-active">
  62. {
  63. this.state.activeState === "Latest" ? engineVersion : this.state.activeState
  64. }
  65. </div>
  66. }
  67. </div>
  68. {
  69. this.state.isExpanded &&
  70. <div className={"command-dropdown-content sub1" + (this.props.toRight ? " toRight" : "")}>
  71. {
  72. this.props.items.map(m => {
  73. return (
  74. <div className={"command-dropdown-label" + (m.isActive ? " active" : "")} key={m.label} onClick={() => {
  75. if (!m.onClick) {
  76. let newValue = !Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false);
  77. Utilities.StoreBoolToStore(m.storeKey!, newValue);
  78. this.forceUpdate();
  79. m.onCheck!(newValue);
  80. return;
  81. }
  82. if (!m.subItems) {
  83. m.onClick();
  84. Utilities.StoreStringToStore(this.props.tooltip, m.label);
  85. this.setState({isExpanded: false, activeState: m.label});
  86. }
  87. }} title={m.label}>
  88. <div className="command-dropdown-label-text">
  89. {(m.isActive ? "> " : "") + m.label}
  90. </div>
  91. {
  92. m.onCheck &&
  93. <input type="checkBox" className="command-dropdown-label-check"
  94. onChange={(evt) => {
  95. Utilities.StoreBoolToStore(m.storeKey!, evt.target.checked);
  96. this.forceUpdate();
  97. m.onCheck!(evt.target.checked);
  98. }}
  99. checked={Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false)}/>
  100. }
  101. {
  102. m.subItems &&
  103. <div className="command-dropdown-arrow">
  104. {">"}
  105. </div>
  106. }
  107. {
  108. m.subItems &&
  109. <div className={"sub-items " + (this.props.globalState.language === "JS" ? "background-js" : "background-ts")}>
  110. {
  111. m.subItems.map(s => {
  112. return (
  113. <div key={s} className={"sub-item" + (Utilities.ReadStringFromStore(m.storeKey!, m.defaultValue as string) === s ? " checked" : "")}
  114. onClick={() => {
  115. Utilities.StoreStringToStore(m.storeKey!, s);
  116. m.onClick!();
  117. this.setState({isExpanded: false});
  118. }}>
  119. <div className="sub-item-label">
  120. {s}
  121. </div>
  122. </div>
  123. )
  124. })
  125. }
  126. </div>
  127. }
  128. </div>
  129. )
  130. })
  131. }
  132. </div>
  133. }
  134. </div>
  135. </>
  136. );
  137. }
  138. }