|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Porpoiseful LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @fileoverview Block for Opmode details |
| 20 | + * @author [email protected] (Alan Smith) |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @license |
| 25 | + * Copyright 2025 Porpoiseful LLC |
| 26 | + * |
| 27 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 28 | + * you may not use this file except in compliance with the License. |
| 29 | + * You may obtain a copy of the License at |
| 30 | + * |
| 31 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 32 | + * |
| 33 | + * Unless required by applicable law or agreed to in writing, software |
| 34 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 35 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 36 | + * See the License for the specific language governing permissions and |
| 37 | + * limitations under the License. |
| 38 | + */ |
| 39 | + |
| 40 | +/** |
| 41 | + * @fileoverview Create a component with a name of a certain type |
| 42 | + * @author [email protected] (Alan Smith) |
| 43 | + */ |
| 44 | +import * as Blockly from 'blockly'; |
| 45 | + |
| 46 | +import { ExtendedPythonGenerator, OpModeDetails } from '../editor/extended_python_generator'; |
| 47 | +import { createFieldDropdown } from '../fields/FieldDropdown'; |
| 48 | +import { MRC_STYLE_CLASS_BLOCKS } from '../themes/styles'; |
| 49 | + |
| 50 | +export const BLOCK_NAME = 'mrc_opmode_details'; |
| 51 | + |
| 52 | +type OpmodeDetailsBlock = Blockly.Block & OpmodeDetailsMixin; |
| 53 | +interface OpmodeDetailsMixin extends OpmodeDetailsMixinType { |
| 54 | +} |
| 55 | +type OpmodeDetailsMixinType = typeof OPMODE_DETAILS; |
| 56 | + |
| 57 | +const OPMODE_DETAILS = { |
| 58 | + /** |
| 59 | + * Block initialization. |
| 60 | + */ |
| 61 | + init: function (this: OpmodeDetailsBlock): void { |
| 62 | + this.setStyle(MRC_STYLE_CLASS_BLOCKS); |
| 63 | + this.appendDummyInput() |
| 64 | + .appendField('Type') |
| 65 | + .appendField(createFieldDropdown(['Auto', 'Teleop', 'Test']), 'TYPE') |
| 66 | + .appendField(' ') |
| 67 | + .appendField('Enabled') |
| 68 | + .appendField(new Blockly.FieldCheckbox(true), 'ENABLED'); |
| 69 | + |
| 70 | + this.appendDummyInput() |
| 71 | + .appendField('Display Name') |
| 72 | + .appendField(new Blockly.FieldTextInput(''), 'NAME') |
| 73 | + this.appendDummyInput() |
| 74 | + .appendField('Display Group') |
| 75 | + .appendField(new Blockly.FieldTextInput(''), 'GROUP'); |
| 76 | + |
| 77 | + this.getField('TYPE')?.setTooltip('What sort of OpMode this is'); |
| 78 | + this.getField('ENABLED')?.setTooltip('Whether the OpMode is shown on Driver Station'); |
| 79 | + this.getField('NAME')?.setTooltip('The name shown on the Driver Station. If blank will use the class name.'); |
| 80 | + this.getField('GROUP')?.setTooltip('An optional group to group OpModes on Driver Station'); |
| 81 | + }, |
| 82 | +} |
| 83 | + |
| 84 | +export const setup = function () { |
| 85 | + Blockly.Blocks[BLOCK_NAME] = OPMODE_DETAILS; |
| 86 | +} |
| 87 | + |
| 88 | +export const pythonFromBlock = function ( |
| 89 | + block: OpmodeDetailsBlock, |
| 90 | + generator: ExtendedPythonGenerator, |
| 91 | +) { |
| 92 | + generator.setOpModeDetails(new OpModeDetails( |
| 93 | + block.getFieldValue('NAME'), |
| 94 | + block.getFieldValue('GROUP'), |
| 95 | + block.getFieldValue('ENABLED') == 'TRUE', |
| 96 | + block.getFieldValue('TYPE') |
| 97 | + )); |
| 98 | + return ''; |
| 99 | +} |
0 commit comments