variantsPropertyGridComponent.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as React from "react";
  2. import { Observable } from "babylonjs/Misc/observable";
  3. import { PropertyChangedEvent } from "../../../propertyChangedEvent";
  4. import { LineContainerComponent } from "../../lineContainerComponent";
  5. import { LockObject } from "./lockObject";
  6. import { GlobalState } from "../../../globalState";
  7. // import { OptionsLineComponent } from '../../lines/optionsLineComponent';
  8. import { ButtonLineComponent } from '../../lines/buttonLineComponent';
  9. import { CheckBoxLineComponent } from '../../lines/checkBoxLineComponent';
  10. interface IVariantsPropertyGridComponentProps {
  11. globalState: GlobalState;
  12. host: any;
  13. lockObject: LockObject;
  14. onPropertyChangedObservable?: Observable<PropertyChangedEvent>;
  15. }
  16. export class VariantsPropertyGridComponent extends React.Component<IVariantsPropertyGridComponentProps> {
  17. // private _lastOne = 0;
  18. private _selectedTags: string[] = [];
  19. constructor(props: IVariantsPropertyGridComponentProps) {
  20. super(props);
  21. }
  22. render() {
  23. const KHR_materials_variants = (BABYLON as any)?.GLTF2?.Loader?.Extensions?.KHR_materials_variants;
  24. if (!KHR_materials_variants) {
  25. return null;
  26. }
  27. let variants: string[] = KHR_materials_variants.GetAvailableVariants(this.props.host);
  28. if (!variants || variants.length === 0) {
  29. return null;
  30. }
  31. let lastPickedVariants = KHR_materials_variants.GetLastSelectedVariant(this.props.host);
  32. variants.sort((a, b) => {
  33. let aIsActive = lastPickedVariants && lastPickedVariants.indexOf ? lastPickedVariants.indexOf(a) > -1 : lastPickedVariants === a;
  34. let bIsActive = lastPickedVariants && lastPickedVariants.indexOf ? lastPickedVariants.indexOf(b) > -1 : lastPickedVariants === b;
  35. if (!aIsActive && this._selectedTags.indexOf(a) > -1) {
  36. aIsActive = true;
  37. }
  38. if (!bIsActive && this._selectedTags.indexOf(b) > -1) {
  39. bIsActive = true;
  40. }
  41. if (aIsActive && bIsActive || !aIsActive && !bIsActive) {
  42. return a.localeCompare(b);
  43. }
  44. if (aIsActive) {
  45. return -1;
  46. }
  47. return 1
  48. });
  49. // let options = variants.map((v: string, i: number) => {
  50. // return {label: v, value: i + 1}
  51. // });
  52. // options.splice(0, 0, {label: "Original", value: 0})
  53. return (
  54. <div>
  55. <LineContainerComponent globalState={this.props.globalState} title="VARIANTS">
  56. {
  57. variants.map((v: string, i: number) => {
  58. return (
  59. <CheckBoxLineComponent key={i} label={v}
  60. isSelected={() => {
  61. if (lastPickedVariants) {
  62. if (Object.prototype.toString.call(lastPickedVariants) === '[object String]') {
  63. if (lastPickedVariants === v) {
  64. if (this._selectedTags.indexOf(v) === -1) {
  65. this._selectedTags.push(v);
  66. }
  67. return true;
  68. }
  69. } else {
  70. let index = lastPickedVariants.indexOf(v);
  71. if (index > -1) {
  72. return true
  73. }
  74. }
  75. }
  76. return this._selectedTags.indexOf(v) > -1;
  77. }}
  78. onSelect={(value) => {
  79. if (value) {
  80. this._selectedTags.push(v);
  81. KHR_materials_variants.SelectVariant(this.props.host, v);
  82. } else {
  83. // Do something on extension?
  84. let index = this._selectedTags.indexOf(v);
  85. if (index > -1) {
  86. this._selectedTags.splice(index, 1);
  87. }
  88. }
  89. }}
  90. />
  91. )
  92. })
  93. }
  94. {/* <OptionsLineComponent
  95. label="Active variant" options={options} noDirectUpdate={true}
  96. target={this.props.host}
  97. propertyName=""
  98. onSelect={(value: number) => {
  99. if (value === 0) {
  100. KHR_materials_variants.Reset(this.props.host);
  101. } else {
  102. KHR_materials_variants.SelectVariant(this.props.host, variants[value - 1]);
  103. }
  104. this._lastOne = value;
  105. this.forceUpdate();
  106. }}
  107. extractValue={() => {
  108. let lastPickedVariant = KHR_materials_variants.GetLastSelectedVariant(this.props.host) || 0;
  109. if (lastPickedVariant && Object.prototype.toString.call(lastPickedVariant) === '[object String]') {
  110. let index = variants.indexOf(lastPickedVariant as string);
  111. if (index > -1) {
  112. this._lastOne = index + 1;
  113. }
  114. }
  115. return this._lastOne;
  116. }}
  117. /> */}
  118. <ButtonLineComponent label="Reset" onClick={() => {
  119. KHR_materials_variants.Reset(this.props.host);
  120. this._selectedTags = [];
  121. this.forceUpdate();
  122. }} />
  123. </LineContainerComponent>
  124. </div>
  125. );
  126. }
  127. }