Skip to content

Commit e6b6160

Browse files
authored
Switches shown toolbox based off of type (#117)
* Split out toolboxes by type * Made tests where you pass it a toolbox to test * Fixed how tests work with new scheme * Putting in suggestions from PR
1 parent ce70894 commit e6b6160

10 files changed

+289
-236
lines changed

src/editor/editor.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import * as Blockly from 'blockly/core';
2424
import { extendedPythonGenerator } from './extended_python_generator';
2525
import { GeneratorContext } from './generator_context';
2626
import * as commonStorage from '../storage/common_storage';
27-
import { getToolboxJSON } from '../toolbox/toolbox';
27+
import * as toolboxOpmode from '../toolbox/toolbox_opmode';
28+
import * as toolboxMechanism from '../toolbox/toolbox_mechanism';
29+
import * as toolboxRobot from '../toolbox/toolbox_robot';
30+
2831
//import { testAllBlocksInToolbox } from '../toolbox/toolbox_tests';
2932
import { MethodsCategory} from '../toolbox/methods_category';
3033

@@ -166,7 +169,7 @@ export class Editor {
166169
if (toolbox != this.toolbox) {
167170
this.toolbox = toolbox;
168171
this.blocklyWorkspace.updateToolbox(toolbox);
169-
//testAllBlocksInToolbox();
172+
// testAllBlocksInToolbox(toolbox);
170173
}
171174
}
172175

@@ -182,23 +185,30 @@ export class Editor {
182185

183186
public updateToolbox(shownPythonToolboxCategories: Set<string>): void {
184187
if (this.currentModule) {
185-
if (this.currentModule.moduleType === commonStorage.MODULE_TYPE_PROJECT) {
186-
// If we are editing a Project, we don't add any additional blocks to
187-
// the toolbox.
188-
this.setToolbox(getToolboxJSON([], shownPythonToolboxCategories));
189-
return;
190-
}
191-
// Otherwise, we add the exported blocks from the Project.
192188
if (!this.projectContent) {
193189
// The Project content hasn't been fetched yet. Try again in a bit.
194190
setTimeout(() => {
195191
this.updateToolbox(shownPythonToolboxCategories)
196192
}, 50);
197193
return;
198194
}
199-
const robotBlocks = commonStorage.extractExportedBlocks(
200-
this.currentModule.projectName, this.projectContent);
201-
this.setToolbox(getToolboxJSON(robotBlocks, shownPythonToolboxCategories));
195+
switch(this.currentModule.moduleType){
196+
case commonStorage.MODULE_TYPE_PROJECT:
197+
this.setToolbox(toolboxRobot.getToolboxJSON(shownPythonToolboxCategories));
198+
break;
199+
case commonStorage.MODULE_TYPE_MECHANISM:
200+
this.setToolbox(toolboxMechanism.getToolboxJSON(shownPythonToolboxCategories));
201+
break;
202+
case commonStorage.MODULE_TYPE_OPMODE:
203+
/*
204+
* TODO: When editing an opmode, we'll need to have blocks for all the methods that a robot has.
205+
* Not sure what this will be replaced with, but it will need something.
206+
* const robotBlocks = commonStorage.extractExportedBlocks(
207+
* this.currentModule.projectName, this.projectContent);
208+
*/
209+
this.setToolbox(toolboxOpmode.getToolboxJSON(shownPythonToolboxCategories));
210+
break;
211+
}
202212
}
203213
}
204214

src/toolbox/hardware_category.ts

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const category_robot =
1+
export const category =
22
{
33
kind: 'category',
44
name: 'Hardware',
@@ -169,56 +169,4 @@ const category_robot =
169169
}
170170
},
171171
],
172-
}
173-
174-
const category_mechanism =
175-
{
176-
kind: 'category',
177-
name: 'Hardware',
178-
contents: [
179-
{
180-
kind: 'label',
181-
text: 'Components',
182-
},
183-
{
184-
kind: 'block',
185-
type: 'mrc_component',
186-
fields: {
187-
NAME: 'my_motor',
188-
TYPE: 'SmartMotor'
189-
},
190-
extraState: {
191-
importModule: 'smart_motor',
192-
params: [{ name: 'motor_port', type: 'int' }],
193-
hideParams: true
194-
},
195-
},
196-
{
197-
kind: 'block',
198-
type: 'mrc_component',
199-
fields: {
200-
NAME: 'my_color_range_sensor',
201-
TYPE: 'ColorRangeSensor'
202-
},
203-
extraState: {
204-
importModule: 'color_range_sensor',
205-
params: [{ name: 'i2c_port', type: 'int' }],
206-
hideParams: true
207-
},
208-
},
209-
{
210-
kind: 'block',
211-
type: 'mrc_component',
212-
fields: {
213-
NAME: 'my_touch_sensor',
214-
TYPE: 'RevTouchSensor'
215-
},
216-
extraState: {
217-
importModule: 'rev_touch_sensor',
218-
params: [{ name: 'smartIO_port', type: 'int' }],
219-
hideParams: true
220-
},
221-
},
222-
],
223-
}
224-
export const category = category_mechanism;
172+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export const category =
2+
{
3+
kind: 'category',
4+
name: 'Hardware',
5+
contents: [
6+
{
7+
kind: 'label',
8+
text: 'Components',
9+
},
10+
{
11+
kind: 'block',
12+
type: 'mrc_component',
13+
fields: {
14+
NAME: 'my_motor',
15+
TYPE: 'SmartMotor'
16+
},
17+
extraState: {
18+
importModule: 'smart_motor',
19+
params: [{ name: 'motor_port', type: 'int' }],
20+
hideParams: true
21+
},
22+
},
23+
{
24+
kind: 'block',
25+
type: 'mrc_component',
26+
fields: {
27+
NAME: 'my_color_range_sensor',
28+
TYPE: 'ColorRangeSensor'
29+
},
30+
extraState: {
31+
importModule: 'color_range_sensor',
32+
params: [{ name: 'i2c_port', type: 'int' }],
33+
hideParams: true
34+
},
35+
},
36+
{
37+
kind: 'block',
38+
type: 'mrc_component',
39+
fields: {
40+
NAME: 'my_touch_sensor',
41+
TYPE: 'RevTouchSensor'
42+
},
43+
extraState: {
44+
importModule: 'rev_touch_sensor',
45+
params: [{ name: 'smartIO_port', type: 'int' }],
46+
hideParams: true
47+
},
48+
},
49+
],
50+
}

src/toolbox/robotpy_toolbox.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
import * as toolboxItems from './items';
3939

4040

41-
export function getToolboxCategories(): toolboxItems.Category[] {
41+
export function getToolboxCategories(shownPythonToolboxCategories: Set<string> | null): toolboxItems.Category[] {
4242
const contents: toolboxItems.Category[] = [];
4343

4444
const allCategories: {[key: string]: toolboxItems.Category} = {};
@@ -87,6 +87,9 @@ export function getToolboxCategories(): toolboxItems.Category[] {
8787

8888
recursivelyRemoveEmptyCategories(contents);
8989

90+
// TODO: Maybe later there is a better way to do this...
91+
filterRobotPyCategories(contents, shownPythonToolboxCategories);
92+
9093
return contents;
9194
}
9295

@@ -175,3 +178,72 @@ function recursivelyRemoveEmptyCategories(contents: toolboxItems.ContentsType[])
175178
}
176179
}
177180
}
181+
182+
function filterRobotPyCategories(
183+
contents: toolboxItems.ContentsType[], shownPythonToolboxCategories: Set<string> | null) {
184+
contents.forEach((item) => {
185+
if (item.kind === 'category') {
186+
const category = item as toolboxItems.Category;
187+
// Traverse the tree depth-first so we can easily identify and remove empty categories.
188+
if (category.contents) {
189+
filterRobotPyCategories(category.contents, shownPythonToolboxCategories);
190+
}
191+
if ((category as toolboxItems.PythonModuleCategory).moduleName) {
192+
const moduleName = (item as toolboxItems.PythonModuleCategory).moduleName;
193+
if (shownPythonToolboxCategories != null && !shownPythonToolboxCategories.has(moduleName)) {
194+
if (category.contents) {
195+
removeBlocksAndSeparators(category.contents);
196+
}
197+
}
198+
}
199+
if ((category as toolboxItems.PythonClassCategory).className) {
200+
const className = (item as toolboxItems.PythonClassCategory).className;
201+
if (shownPythonToolboxCategories != null && !shownPythonToolboxCategories.has(className)) {
202+
if (category.contents) {
203+
removeBlocksAndSeparators(category.contents);
204+
}
205+
}
206+
}
207+
}
208+
});
209+
removeEmptyCategories(contents, shownPythonToolboxCategories);
210+
}
211+
212+
function removeBlocksAndSeparators(contents: toolboxItems.ContentsType[]) {
213+
let i = 0;
214+
while (i < contents.length) {
215+
const remove = (contents[i].kind === 'block' || contents[i].kind === 'sep');
216+
if (remove) {
217+
contents.splice(i, 1);
218+
continue;
219+
}
220+
i++;
221+
}
222+
}
223+
224+
function removeEmptyCategories(
225+
contents: toolboxItems.ContentsType[], shownPythonToolboxCategories: Set<string> | null) {
226+
let i = 0;
227+
while (i < contents.length) {
228+
let remove = false;
229+
if (contents[i].kind === 'category') {
230+
const category = contents[i] as toolboxItems.Category;
231+
let fullCategoryName = '';
232+
if ((category as toolboxItems.PythonModuleCategory).moduleName) {
233+
fullCategoryName = (category as toolboxItems.PythonModuleCategory).moduleName
234+
} else if ((category as toolboxItems.PythonClassCategory).className) {
235+
fullCategoryName = (category as toolboxItems.PythonClassCategory).className;
236+
}
237+
if (category.contents &&
238+
category.contents.length == 0 &&
239+
shownPythonToolboxCategories != null && !shownPythonToolboxCategories.has(fullCategoryName)) {
240+
remove = true;
241+
}
242+
}
243+
if (remove) {
244+
contents.splice(i, 1);
245+
continue;
246+
}
247+
i++;
248+
}
249+
}

0 commit comments

Comments
 (0)