|
| 1 | +// React imports |
| 2 | +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' |
| 3 | +import { useMediaQuery } from 'react-responsive' |
| 4 | +import ButtonGroup from 'react-bootstrap/ButtonGroup' |
| 5 | +import Dropdown from 'react-bootstrap/Dropdown' |
| 6 | +import DropdownButton from 'react-bootstrap/DropdownButton' |
| 7 | +import { View } from 'react-native' |
| 8 | + |
| 9 | +// Local imports |
| 10 | +import { PLANNING_SCENE_PARAM, PLANNING_SCENE_GET_PARAMETERS_SERVICE_NAME, PLANNING_SCENE_GET_PARAMETERS_SERVICE_TYPE } from '../Constants' |
| 11 | +import { useGlobalState, SETTINGS_STATE } from '../GlobalState' |
| 12 | +import { useROS, createROSService, createROSServiceRequest, getValueFromParameter } from '../../ros/ros_helpers' |
| 13 | +import SettingsPageParent from './SettingsPageParent' |
| 14 | + |
| 15 | +/** |
| 16 | + * The PlanningScene component allows users to change the planning scene that a particular |
| 17 | + * settings namespace is configured with |
| 18 | + */ |
| 19 | +const PlanningScene = () => { |
| 20 | + // Get relevant global state variables |
| 21 | + const setSettingsState = useGlobalState((state) => state.setSettingsState) |
| 22 | + |
| 23 | + // Flag to check if the current orientation is portrait |
| 24 | + const isPortrait = useMediaQuery({ query: '(orientation: portrait)' }) |
| 25 | + // Indicator of how to arrange screen elements based on orientation |
| 26 | + let dimension = isPortrait ? 'column' : 'row' |
| 27 | + // Rendering variables |
| 28 | + let textFontSize = '3.5vh' |
| 29 | + |
| 30 | + // Configure the parameters for SettingsPageParent |
| 31 | + const paramNames = useMemo(() => [PLANNING_SCENE_PARAM], []) |
| 32 | + const [currentParams, setCurrentParams] = useState([null]) |
| 33 | + |
| 34 | + /** |
| 35 | + * Connect to ROS, if not already connected. Put this in useRef to avoid |
| 36 | + * re-connecting upon re-renders. |
| 37 | + */ |
| 38 | + const ros = useRef(useROS().ros) |
| 39 | + |
| 40 | + // Get the options for the planning scene |
| 41 | + let getParametersService = useRef( |
| 42 | + createROSService(ros.current, PLANNING_SCENE_GET_PARAMETERS_SERVICE_NAME, PLANNING_SCENE_GET_PARAMETERS_SERVICE_TYPE) |
| 43 | + ) |
| 44 | + const [planningSceneNamespaces, setPlanningSceneNamespaces] = useState([]) |
| 45 | + useEffect(() => { |
| 46 | + let service = getParametersService.current |
| 47 | + let request = createROSServiceRequest({ |
| 48 | + names: ['namespaces'] |
| 49 | + }) |
| 50 | + service.callService(request, (response) => { |
| 51 | + if (response.values.length > 0 && response.values[0].type === 9) { |
| 52 | + setPlanningSceneNamespaces(getValueFromParameter(response.values[0])) |
| 53 | + } else { |
| 54 | + console.error('PlanningScene: Error getting planning scene namespaces') |
| 55 | + } |
| 56 | + }) |
| 57 | + }, [getParametersService, setPlanningSceneNamespaces]) |
| 58 | + |
| 59 | + // Render the settings for the planning scene |
| 60 | + const renderPlanningSceneSettings = useCallback(() => { |
| 61 | + if (currentParams.some((param) => param === null)) { |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <View |
| 65 | + style={{ |
| 66 | + flex: 1, |
| 67 | + flexDirection: 'column', |
| 68 | + justifyContent: 'center', |
| 69 | + alignItems: 'center', |
| 70 | + width: '100%' |
| 71 | + }} |
| 72 | + > |
| 73 | + <h5 style={{ textAlign: 'center', fontSize: textFontSize }}>Loading...</h5> |
| 74 | + </View> |
| 75 | + </> |
| 76 | + ) |
| 77 | + } else { |
| 78 | + return ( |
| 79 | + <View |
| 80 | + style={{ |
| 81 | + flex: 1, |
| 82 | + flexDirection: dimension, |
| 83 | + justifyContent: 'center', |
| 84 | + alignItems: 'center', |
| 85 | + width: '100%', |
| 86 | + height: '100%' |
| 87 | + }} |
| 88 | + > |
| 89 | + <DropdownButton |
| 90 | + as={ButtonGroup} |
| 91 | + key='planningSceneOptions' |
| 92 | + id={`dropdown-button-drop-down`} |
| 93 | + drop='down' |
| 94 | + variant='secondary' |
| 95 | + title={currentParams[0]} |
| 96 | + size='lg' |
| 97 | + > |
| 98 | + {planningSceneNamespaces.map((namespace) => ( |
| 99 | + <Dropdown.Item key={namespace} onClick={() => setCurrentParams([namespace])} active={namespace === currentParams[0]}> |
| 100 | + {namespace} |
| 101 | + </Dropdown.Item> |
| 102 | + ))} |
| 103 | + </DropdownButton> |
| 104 | + </View> |
| 105 | + ) |
| 106 | + } |
| 107 | + }, [currentParams, dimension, planningSceneNamespaces, setCurrentParams, textFontSize]) |
| 108 | + |
| 109 | + return ( |
| 110 | + <SettingsPageParent |
| 111 | + title='Planning Scene ⚙' |
| 112 | + doneCallback={() => setSettingsState(SETTINGS_STATE.MAIN)} |
| 113 | + modalShow={false} |
| 114 | + modalOnHide={null} |
| 115 | + modalChildren={null} |
| 116 | + paramNames={paramNames} |
| 117 | + localParamValues={currentParams} |
| 118 | + setLocalParamValues={setCurrentParams} |
| 119 | + > |
| 120 | + {renderPlanningSceneSettings()} |
| 121 | + </SettingsPageParent> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export default PlanningScene |
0 commit comments