|
| 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 Create a mechanism with a name of a certain type |
| 20 | + * @author [email protected] (Alan Smith) |
| 21 | + */ |
| 22 | +import * as Blockly from 'blockly'; |
| 23 | +import { Order, PythonGenerator } from 'blockly/python'; |
| 24 | + |
| 25 | +import { MRC_STYLE_FUNCTIONS } from '../themes/styles' |
| 26 | +import { createFieldNonEditableText } from '../fields/FieldNonEditableText'; |
| 27 | +import { ExtendedPythonGenerator } from '../editor/extended_python_generator'; |
| 28 | +import { getAllowedTypesForSetCheck, getOutputCheck } from './utils/python'; |
| 29 | + |
| 30 | +export const BLOCK_NAME = 'mrc_mechanism'; |
| 31 | +export const OUTPUT_NAME = 'mrc_mechansim'; |
| 32 | + |
| 33 | +export type ConstructorArg = { |
| 34 | + name: string, |
| 35 | + type: string, |
| 36 | +}; |
| 37 | + |
| 38 | +type MechanismExtraState = { |
| 39 | + importModule?: string, |
| 40 | + params?: ConstructorArg[], |
| 41 | +} |
| 42 | + |
| 43 | +type MechanismBlock = Blockly.Block & MechanismMixin; |
| 44 | +interface MechanismMixin extends MechanismMixinType { |
| 45 | + mrcArgs: ConstructorArg[], |
| 46 | + mrcImportModule: string, |
| 47 | + |
| 48 | +} |
| 49 | +type MechanismMixinType = typeof MECHANISM_FUNCTION; |
| 50 | + |
| 51 | +const MECHANISM_FUNCTION = { |
| 52 | + /** |
| 53 | + * Block initialization. |
| 54 | + */ |
| 55 | + init: function (this: MechanismBlock): void { |
| 56 | + this.setStyle(MRC_STYLE_FUNCTIONS); |
| 57 | + this.appendDummyInput() |
| 58 | + .appendField(new Blockly.FieldTextInput('my_mech'), 'NAME') |
| 59 | + .appendField('of type') |
| 60 | + .appendField(createFieldNonEditableText(''), 'TYPE'); |
| 61 | + this.setPreviousStatement(true); |
| 62 | + this.setNextStatement(true); |
| 63 | + }, |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the state of this block as a JSON serializable object. |
| 67 | + */ |
| 68 | + saveExtraState: function (this: MechanismBlock): MechanismExtraState { |
| 69 | + const extraState: MechanismExtraState = { |
| 70 | + }; |
| 71 | + extraState.params = []; |
| 72 | + this.mrcArgs.forEach((arg) => { |
| 73 | + extraState.params!.push({ |
| 74 | + 'name': arg.name, |
| 75 | + 'type': arg.type, |
| 76 | + }); |
| 77 | + }); |
| 78 | + if (this.mrcImportModule) { |
| 79 | + extraState.importModule = this.mrcImportModule; |
| 80 | + } |
| 81 | + return extraState; |
| 82 | + }, |
| 83 | + /** |
| 84 | + * Applies the given state to this block. |
| 85 | + */ |
| 86 | + loadExtraState: function (this: MechanismBlock, extraState: MechanismExtraState): void { |
| 87 | + this.mrcImportModule = extraState.importModule ? extraState.importModule : ''; |
| 88 | + this.mrcArgs = []; |
| 89 | + |
| 90 | + if(extraState.params){ |
| 91 | + extraState.params.forEach((arg) => { |
| 92 | + this.mrcArgs.push({ |
| 93 | + 'name': arg.name, |
| 94 | + 'type': arg.type, |
| 95 | + }); |
| 96 | + }); |
| 97 | + } |
| 98 | + this.mrcArgs = extraState.params ? extraState.params : []; |
| 99 | + this.updateBlock_(); |
| 100 | + }, |
| 101 | + /** |
| 102 | + * Update the block to reflect the newly loaded extra state. |
| 103 | + */ |
| 104 | + updateBlock_: function(this: MechanismBlock): void { |
| 105 | + // Add input sockets for the arguments. |
| 106 | + for (let i = 0; i < this.mrcArgs.length; i++) { |
| 107 | + const input = this.appendValueInput('ARG' + i) |
| 108 | + .setAlign(Blockly.inputs.Align.RIGHT) |
| 109 | + .appendField(this.mrcArgs[i].name); |
| 110 | + if (this.mrcArgs[i].type) { |
| 111 | + input.setCheck(getAllowedTypesForSetCheck(this.mrcArgs[i].type)); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export const setup = function () { |
| 118 | + Blockly.Blocks[BLOCK_NAME] = MECHANISM_FUNCTION; |
| 119 | +} |
| 120 | + |
| 121 | +//TODO: This needs to cause our own init to create the mechanisms line |
| 122 | +export const pythonFromBlock = function ( |
| 123 | + mechanismBlock: MechanismBlock, |
| 124 | + generator: ExtendedPythonGenerator, |
| 125 | +) { |
| 126 | + if (mechanismBlock.mrcImportModule) { |
| 127 | + generator.addImport(mechanismBlock.mrcImportModule); |
| 128 | + } |
| 129 | + let code = 'self.mechanisms["' + mechanismBlock.getFieldValue('NAME') + '"] = ' |
| 130 | + + mechanismBlock.getFieldValue('TYPE') + '(' |
| 131 | + |
| 132 | + for (let i = 0; i < mechanismBlock.mrcArgs.length; i++) { |
| 133 | + const fieldName = 'ARG' + i; |
| 134 | + if(i != 0){ |
| 135 | + code += ', ' |
| 136 | + } |
| 137 | + code += mechanismBlock.mrcArgs[i].name + ' = ' + generator.valueToCode(mechanismBlock, fieldName, Order.NONE); |
| 138 | + } |
| 139 | + |
| 140 | + code += ')' + "\n"; |
| 141 | + |
| 142 | + return code |
| 143 | +} |
0 commit comments