-
Notifications
You must be signed in to change notification settings - Fork 8
Mechanisms #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Mechanisms #44
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
acfc06e
Adding in mechanisms
alan412 423ed15
Simple mechanism
alan412 c249ff8
remove add mechanism
alan412 57b9d7e
Move to use extended python
alan412 b09335a
Fix typo
alan412 6fc7202
remove init and loop from toolbox
alan412 f2a6d76
Change to use json for start state
alan412 a7ca6f5
Put holder for mechanisms in generated code
alan412 ae82b2a
add extra state for imported module
alan412 624c453
changed starting x, y
alan412 ea7fc4d
Added import module to mechanism
alan412 aff65d9
Support parameters for mechanisms
alan412 51d6d45
fixed imports in wrong place
alan412 520821a
Changed OpMode and Mechanism to be PascalCase
alan412 d2ec25e
add known issues
alan412 a734a67
Remove unused field minus and plus
alan412 0b3f5fb
removed custom canDelete to use blockly deletable
alan412 7d242b7
Fix failure of CI
alan412 8d5daed
Added mechanisms to the app (new mechanism, show mechanism in the tre…
lizlooney 7c1ae9a
Respond to PR comments
alan412 25ce1fd
change back from const to let
alan412 596893f
Changed module types to be all lowercase
alan412 b63e3a0
Merge pull request #45 from lizlooney/pr_mechanism_files
lizlooney 18a1d22
Merge branch 'mechanisms' of github.com:alan412/systemcore-blocks-int…
lizlooney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 Porpoiseful LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Create a mechanism with a name of a certain type | ||
* @author [email protected] (Alan Smith) | ||
*/ | ||
import * as Blockly from 'blockly'; | ||
import { Order, PythonGenerator } from 'blockly/python'; | ||
|
||
import { MRC_STYLE_FUNCTIONS } from '../themes/styles' | ||
import { createFieldNonEditableText } from '../fields/FieldNonEditableText'; | ||
import { ExtendedPythonGenerator } from '../editor/extended_python_generator'; | ||
import { getAllowedTypesForSetCheck, getOutputCheck } from './utils/python'; | ||
|
||
export const BLOCK_NAME = 'mrc_mechanism'; | ||
export const OUTPUT_NAME = 'mrc_mechansim'; | ||
|
||
export type ConstructorArg = { | ||
name: string, | ||
type: string, | ||
}; | ||
|
||
type MechanismExtraState = { | ||
importModule?: string, | ||
params?: ConstructorArg[], | ||
} | ||
|
||
type MechanismBlock = Blockly.Block & MechanismMixin; | ||
interface MechanismMixin extends MechanismMixinType { | ||
mrcArgs: ConstructorArg[], | ||
mrcImportModule: string, | ||
|
||
} | ||
type MechanismMixinType = typeof MECHANISM_FUNCTION; | ||
|
||
const MECHANISM_FUNCTION = { | ||
/** | ||
* Block initialization. | ||
*/ | ||
init: function (this: MechanismBlock): void { | ||
this.setStyle(MRC_STYLE_FUNCTIONS); | ||
this.appendDummyInput() | ||
.appendField(new Blockly.FieldTextInput('my_mech'), 'NAME') | ||
.appendField('of type') | ||
.appendField(createFieldNonEditableText(''), 'TYPE'); | ||
this.setPreviousStatement(true); | ||
this.setNextStatement(true); | ||
}, | ||
|
||
/** | ||
* Returns the state of this block as a JSON serializable object. | ||
*/ | ||
saveExtraState: function (this: MechanismBlock): MechanismExtraState { | ||
let extraState: MechanismExtraState = { | ||
alan412 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}; | ||
extraState.params = []; | ||
this.mrcArgs.forEach((arg) => { | ||
extraState.params!.push({ | ||
'name': arg.name, | ||
'type': arg.type, | ||
}); | ||
}); | ||
if (this.mrcImportModule) { | ||
extraState.importModule = this.mrcImportModule; | ||
} | ||
return extraState; | ||
}, | ||
/** | ||
* Applies the given state to this block. | ||
*/ | ||
loadExtraState: function (this: MechanismBlock, extraState: MechanismExtraState): void { | ||
this.mrcImportModule = extraState.importModule ? extraState.importModule : ''; | ||
this.mrcArgs = []; | ||
|
||
if(extraState.params){ | ||
extraState.params.forEach((arg) => { | ||
this.mrcArgs.push({ | ||
'name': arg.name, | ||
'type': arg.type, | ||
}); | ||
}); | ||
} | ||
this.mrcArgs = extraState.params ? extraState.params : []; | ||
this.updateBlock_(); | ||
}, | ||
/** | ||
* Update the block to reflect the newly loaded extra state. | ||
*/ | ||
updateBlock_: function(this: MechanismBlock): void { | ||
// Add input sockets for the arguments. | ||
for (let i = 0; i < this.mrcArgs.length; i++) { | ||
const input = this.appendValueInput('ARG' + i) | ||
.setAlign(Blockly.inputs.Align.RIGHT) | ||
.appendField(this.mrcArgs[i].name); | ||
if (this.mrcArgs[i].type) { | ||
input.setCheck(getAllowedTypesForSetCheck(this.mrcArgs[i].type)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const setup = function () { | ||
Blockly.Blocks[BLOCK_NAME] = MECHANISM_FUNCTION; | ||
} | ||
|
||
//TODO: This needs to cause our own init to create the mechanisms line | ||
export const pythonFromBlock = function ( | ||
mechanismBlock: MechanismBlock, | ||
generator: ExtendedPythonGenerator, | ||
) { | ||
if (mechanismBlock.mrcImportModule) { | ||
generator.addImport(mechanismBlock.mrcImportModule); | ||
} | ||
let code = 'self.mechanisms["' + mechanismBlock.getFieldValue('NAME') + '"] = ' | ||
+ mechanismBlock.getFieldValue('TYPE') + '(' | ||
|
||
for (let i = 0; i < mechanismBlock.mrcArgs.length; i++) { | ||
let fieldName = 'ARG' + i; | ||
alan412 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if(i != 0){ | ||
code += ', ' | ||
} | ||
code += mechanismBlock.mrcArgs[i].name + ' = ' + generator.valueToCode(mechanismBlock, fieldName, Order.NONE); | ||
} | ||
|
||
code += ')' + "\n" | ||
alan412 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return code | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"blocks": { | ||
"languageVersion": 0, | ||
"blocks": [ | ||
{ | ||
"type": "mrc_class_method_def", | ||
"id": "ElbONc{)s(,UprTW(|1C", | ||
"x": 10, | ||
"y": 10, | ||
"deletable": false, | ||
"extraState": { | ||
"canChangeSignature": false, | ||
"returnType": "None", | ||
"params": [], | ||
"pythonMethodName": "__init__" | ||
}, | ||
"fields": { | ||
"NAME": "init" | ||
} | ||
}, | ||
{ | ||
"type": "mrc_class_method_def", | ||
"id": "wxFAh6eaR1|3fTuV:UAd", | ||
"x": 10, | ||
"y": 200, | ||
"deletable": false, | ||
"extraState": { | ||
"canChangeSignature": false, | ||
"returnType": "None", | ||
"params": [] | ||
}, | ||
"fields": { | ||
"NAME": "loop" | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I fixed this in PR #45 !