import { Engine } from "babylonjs/Engines/engine"; import * as React from "react"; import { GlobalState } from '../globalState'; import { Utilities } from '../tools/utilities'; interface ICommandDropdownComponentProps { globalState: GlobalState; icon?: string; tooltip: string; defaultValue?: string; items: { label: string, onClick?: () => void, onCheck?: (value: boolean) => void, storeKey?: string, isActive?: boolean, defaultValue?: boolean | string; subItems?: string[]; }[]; toRight?: boolean; } export class CommandDropdownComponent extends React.Component { public constructor(props: ICommandDropdownComponentProps) { super(props); this.state = {isExpanded: false, activeState: Utilities.ReadStringFromStore(this.props.tooltip, this.props.defaultValue!)}; this.props.globalState.OnNewDropdownButtonClicked.add((source) => { if (source === this) { return; } this.setState({isExpanded: false}); }); } public render() { var engineVersionSub = Engine.Version.indexOf("-"); var engineVersion = Engine.Version; if (engineVersionSub ! -1) { engineVersion = engineVersion.substr(0, engineVersionSub); } return ( <> { this.state.isExpanded &&
{ this.setState({isExpanded: false}); }}>
}
{ this.props.globalState.OnNewDropdownButtonClicked.notifyObservers(this); this.setState({isExpanded: !this.state.isExpanded}); }}> { this.props.icon &&
} { !this.props.icon &&
{ this.state.activeState === "Latest" ? engineVersion : this.state.activeState }
}
{ this.state.isExpanded &&
{ this.props.items.map(m => { return (
{ if (!m.onClick) { let newValue = !Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false); Utilities.StoreBoolToStore(m.storeKey!, newValue); this.forceUpdate(); m.onCheck!(newValue); return; } if (!m.subItems) { m.onClick(); Utilities.StoreStringToStore(this.props.tooltip, m.label); this.setState({isExpanded: false, activeState: m.label}); } }} title={m.label}>
{(m.isActive ? "> " : "") + m.label}
{ m.onCheck && { Utilities.StoreBoolToStore(m.storeKey!, evt.target.checked); this.forceUpdate(); m.onCheck!(evt.target.checked); }} checked={Utilities.ReadBoolFromStore(m.storeKey!, (m.defaultValue as boolean) || false)}/> } { m.subItems &&
{">"}
} { m.subItems &&
{ m.subItems.map(s => { return (
{ Utilities.StoreStringToStore(m.storeKey!, s); m.onClick!(); this.setState({isExpanded: false}); }}>
{s}
) }) }
}
) }) }
}
); } }