Skip to content

Commit a07823c

Browse files
authored
Merge pull request #50 from alan412/default_blocks
Default blocks
2 parents 7f1470f + e8042d9 commit a07823c

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@ WARNING! This is not ready for use and is under heavy development of basic featu
1414
## Known Issues
1515
1. Mechanisms aren't limited to init
1616
2. Mechanisms aren't limited to only Robot or Mechanism class
17-
3. There is no default Robot class
18-
4. No way to specify whether an opmode is auto or teleop
19-
5. Workspace can have blocks
20-
6. No Robot workspace yet
21-
7. No Mechanism workspace yet
22-
8. Something weird is going on with currentModule getting unset
17+
3. No way to specify whether an opmode is auto or teleop

src/editor/extended_python_generator.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ export class ExtendedPythonGenerator extends PythonGenerator {
161161

162162
classParentFromModuleType(moduleType : string) : string{
163163
if(moduleType == commonStorage.MODULE_TYPE_WORKSPACE){
164-
return "Robot";
164+
return "RobotBase";
165165
}
166166
if(moduleType == commonStorage.MODULE_TYPE_OPMODE){
167167
return "OpMode";
168168
}
169169
if(moduleType == commonStorage.MODULE_TYPE_MECHANISM){
170-
return "OpMode";
170+
return "Mechanism";
171171
}
172172
return "";
173173
}
@@ -176,7 +176,10 @@ export class ExtendedPythonGenerator extends PythonGenerator {
176176
if (!this.currentModule) {
177177
return super.finish(code);
178178
}
179-
let className = this.currentModule.moduleName;
179+
let className = 'Robot'; // Default for Workspace
180+
if (this.currentModule.moduleType != commonStorage.MODULE_TYPE_WORKSPACE){
181+
className = this.currentModule.moduleName;
182+
}
180183
let classParent = this.classParentFromModuleType(this.currentModule.moduleType);
181184
this.addImport(classParent);
182185

src/modules/mechanism_start.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"blocks": {
3+
"languageVersion": 0,
4+
"blocks": [
5+
{
6+
"type": "mrc_class_method_def",
7+
"id": "ElbONc{)s(,UprTW(|1C",
8+
"x": 10,
9+
"y": 10,
10+
"deletable": false,
11+
"extraState": {
12+
"canChangeSignature": false,
13+
"returnType": "None",
14+
"params": [],
15+
"pythonMethodName": "__init__"
16+
},
17+
"fields": {
18+
"NAME": "init"
19+
}
20+
},
21+
{
22+
"type": "mrc_class_method_def",
23+
"id": "wxFAh6eaR1|3fTuV:UAd",
24+
"x": 10,
25+
"y": 200,
26+
"deletable": false,
27+
"extraState": {
28+
"canChangeSignature": false,
29+
"returnType": "None",
30+
"params": []
31+
},
32+
"fields": {
33+
"NAME": "update"
34+
}
35+
}
36+
]
37+
}
38+
}

src/modules/robot_start.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"blocks": {
3+
"languageVersion": 0,
4+
"blocks": [
5+
{
6+
"type": "mrc_class_method_def",
7+
"id": "ElbONc{)s(,UprTW(|1C",
8+
"x": 10,
9+
"y": 10,
10+
"deletable": false,
11+
"extraState": {
12+
"canChangeSignature": false,
13+
"returnType": "None",
14+
"params": [],
15+
"pythonMethodName": "__init__"
16+
},
17+
"fields": {
18+
"NAME": "init"
19+
}
20+
}
21+
]
22+
}
23+
}

src/storage/common_storage.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import * as Blockly from 'blockly/core';
2323

2424
import {Block} from "../toolbox/items";
2525
import startingOpModeBlocks from '../modules/opmode_start.json';
26+
import startingMechanismBlocks from '../modules/mechanism_start.json';
27+
import startingRobotBlocks from '../modules/robot_start.json';
2628

2729
import {extendedPythonGenerator} from '../editor/extended_python_generator';
2830

@@ -160,9 +162,15 @@ export function newWorkspaceContent(workspaceName: string): string {
160162
dateModifiedMillis: 0,
161163
};
162164

163-
const pythonCode = '';
164-
const exportedBlocks = '[]';
165-
const blocksContent = '{}';
165+
// Create a headless blockly workspace.
166+
const headlessBlocklyWorkspace = new Blockly.Workspace();
167+
headlessBlocklyWorkspace.options.oneBasedIndex = false;
168+
Blockly.serialization.workspaces.load(startingRobotBlocks, headlessBlocklyWorkspace);
169+
170+
extendedPythonGenerator.setCurrentModule(module);
171+
const pythonCode = extendedPythonGenerator.workspaceToCode(headlessBlocklyWorkspace);
172+
const exportedBlocks = JSON.stringify(extendedPythonGenerator.getExportedBlocks(headlessBlocklyWorkspace));
173+
const blocksContent = JSON.stringify(Blockly.serialization.workspaces.save(headlessBlocklyWorkspace));
166174
return makeModuleContent(module, pythonCode, exportedBlocks, blocksContent);
167175
}
168176

@@ -181,7 +189,7 @@ export function newMechanismContent(workspaceName: string, mechanismName: string
181189
// Create a headless blockly workspace.
182190
const headlessBlocklyWorkspace = new Blockly.Workspace();
183191
headlessBlocklyWorkspace.options.oneBasedIndex = false;
184-
// TODO: Create the blocks for a new mechanism.
192+
Blockly.serialization.workspaces.load(startingMechanismBlocks, headlessBlocklyWorkspace);
185193

186194
extendedPythonGenerator.setCurrentModule(module);
187195
const pythonCode = extendedPythonGenerator.workspaceToCode(headlessBlocklyWorkspace);

0 commit comments

Comments
 (0)