Skip to content

Commit

Permalink
use the Sprite type instead of MenuSprite for all block APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Jan 22, 2025
1 parent cb01db1 commit 3d4d76f
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 2 deletions.
213 changes: 211 additions & 2 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace miniMenu {
item10?: MenuItem,
item11?: MenuItem,
item12?: MenuItem,
) {
): Sprite {
_init();

const m = new MenuSprite();
Expand Down Expand Up @@ -114,7 +114,7 @@ namespace miniMenu {
//% help=github:arcade-mini-menu/docs/create-menu-from-array
export function createMenuFromArray(
items: MenuItem[]
) {
): Sprite {
_init();

const m = new MenuSprite();
Expand Down Expand Up @@ -157,4 +157,213 @@ namespace miniMenu {
export function createBorderBox(left: number, top: number, right: number, bottom: number) {
return packMargin(left, top, right, bottom);
}

/**
* Sets whether or not button events on this MenuSprite will be fired.
*
* @param enabled If true, button events are enabled. If false, they are disabled
*/
//% blockId=mini_menu_sprite_set_button_events_enabled_new
//% block="$sprite set button events enabled $enabled"
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% enable.shadow=toggleOnOff
//% group="Controls"
//% weight=80
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-button-events-enabled
export function setButtonEventsEnabled(sprite: Sprite, enabled: boolean) {
const menu = assertMenuSprite(sprite);
menu.setButtonEventsEnabled(enabled);
}

/**
* Moves the selection cursor in the MenuSprite in the given direction.
*
* @param direction The direction to move the cursor in
*/
//% blockId=mini_menu_sprite_move_selection_up_new
//% block="$sprite move selection $direction"
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% direction.shadow=mini_menu_move_direction
//% group="Controls"
//% weight=90
//% blockGap=8
//% help=github:arcade-mini-menu/docs/move-selection
export function moveSelection(sprite: Sprite, direction: number) {
const menu = assertMenuSprite(sprite);
menu.moveSelection(direction);
}

/**
* Destroys the MenuSprite. This is exactly the same as using the destroy block in the Sprites category
*/
//% blockId=mini_menu_sprite_close_menu_new
//% block="close $sprite"
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% group="Create"
//% weight=10
//% help=github:arcade-mini-menu/docs/close
export function close(sprite: Sprite) {
const menu = assertMenuSprite(sprite);
menu.close();
}

/**
* Runs some code whenever a button is pressed and the given MenuSprite has not been destroyed. Using this with one of the direction buttons will override the default behavior.
*
* @param button The button to listen to
* @param handler The code to run when the button is pressed
*/
//% blockId=mini_menu_on_button_pressed_new
//% block="$sprite on $button pressed with $selection"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% handlerStatement
//% draggableParameters=reporter
//% group="Controls"
//% weight=100
//% blockGap=8
//% help=github:arcade-mini-menu/docs/on-button-pressed
export function onButtonPressed(sprite: Sprite, button: controller.Button, handler: (selection: string, selectedIndex: number) => void) {
const menu = assertMenuSprite(sprite);
menu.onButtonPressed(button, handler);
}

/**
* Runs some code whenever the selection cursor in the specified MenuSprite moves. This will also fire once immediately when this function is called
*
* @param handler The code to run when the selection changes
*/
//% blockId=mini_menu_on_selection_changed_new
//% block="$sprite on selection changed $selection $selectedIndex"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% handlerStatement
//% draggableParameters=reporter
//% group="Controls"
//% weight=90
//% help=github:arcade-mini-menu/docs/on-selection-changed
export function onSelectionChanged(sprite: Sprite, handler: (selection: string, selectedIndex: number) => void) {
const menu = assertMenuSprite(sprite);
menu.onSelectionChanged(handler);
}

/**
* Sets a style property for the specified part of the MenuSprite. See the help page for more info on what these properties mean.
*
* @param kind The part of the MenuSprite to style
* @param property The property to set the value of
* @param value The value to set the property to
*/
//% blockId=mini_menu_set_style_property_new
//% block="set $kind item style for $sprite $property to $value"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% inlineInputMode=inline
//% group="Styling"
//% weight=50
//% help=github:arcade-mini-menu/docs/set-style-property
export function setStyleProperty(sprite: Sprite, kind: StyleKind, property: StyleProperty, value: number) {
const menu = assertMenuSprite(sprite);
menu.setStyleProperty(kind, property, value);
}

/**
* Sets a style property on a MenuSprite. See the help page for more info on what these properties mean.
*
* @param property The property to set the value of
* @param value The value to set the property to
*/
//% blockId=mini_menu_set_menu_style_property_new
//% block="set menu style for $sprite $property to $value"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% inlineInputMode=inline
//% group="Styling"
//% weight=100
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-menu-style-property
export function setMenuStyleProperty(sprite: Sprite, property: MenuStyleProperty, value: number) {
const menu = assertMenuSprite(sprite);
menu.setMenuStyleProperty(property, value);
}

/**
* Sets the title of the MenuSprite. The title is displayed above the menu can can be customized using the setStyleProperty function.
*
* @param title The title to set for the MenuSprite
*/
//% blockId=mini_menu_set_menu_title_new
//% block="set $sprite title to $title"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% title.defl="title"
//% inlineInputMode=inline
//% group="Create"
//% weight=20
//% help=github:arcade-mini-menu/docs/set-title
export function setTitle(sprite: Sprite, title: string) {
const menu = assertMenuSprite(sprite);
menu.setTitle(title);
}

/**
* Sets the width and height of the MenuSprite. If the width or height is too small to fit the menu's content, the menu will scroll.
*
* @param width The desired width of the MenuSprite or 0 for the content width
* @param height The desired height of the MenuSprite or 0 for the content height
*/
//% blockId=mini_menu_set_menu_dimensions_new
//% block="set $sprite width $width height $height"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% width.defl=100
//% height.defl=100
//% inlineInputMode=inline
//% group="Create"
//% weight=30
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-dimensions
export function setDimensions(sprite: Sprite, width: number, height: number) {
const menu = assertMenuSprite(sprite);
menu.setDimensions(width, height);
}

/**
* Sets the frame for the MenuSprite. The image must be square and have a width and height that are divisible by 3
*
* @param frame An image to use as the template for drawing the MenuSprite's frame
*/
//% blockId=mini_menu_set_menu_frame_new
//% block="set $sprite frame to $frame"
//% this.shadow=variables_get
//% sprite.defl=myMenu
//% sprite.shadow=variables_get
//% frame.shadow=dialog_image_picker
//% inlineInputMode=inline
//% group="Styling"
//% weight=100
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-frame
export function setFrame(sprite: Sprite, frame: Image) {
const menu = assertMenuSprite(sprite);
menu.setFrame(frame);
}

function assertMenuSprite(sprite: Sprite): MenuSprite {
if (!(sprite instanceof MenuSprite)) {
throw "MenuSprite function called on a non-menu Sprite";
}

return sprite as MenuSprite;
}
}
10 changes: 10 additions & 0 deletions custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ namespace miniMenu {
//% weight=80
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-button-events-enabled
//% blockHidden=1
setButtonEventsEnabled(enabled: boolean) {
this.buttonEventsEnabled = enabled;
}
Expand All @@ -1087,6 +1088,7 @@ namespace miniMenu {
//% weight=90
//% blockGap=8
//% help=github:arcade-mini-menu/docs/move-selection
//% blockHidden=1
moveSelection(direction: number) {
if (this.items.length === 0) return;

Expand Down Expand Up @@ -1168,6 +1170,7 @@ namespace miniMenu {
//% group="Create"
//% weight=10
//% help=github:arcade-mini-menu/docs/close
//% blockHidden=1
close() {
this.destroy();
}
Expand All @@ -1188,6 +1191,7 @@ namespace miniMenu {
//% weight=100
//% blockGap=8
//% help=github:arcade-mini-menu/docs/on-button-pressed
//% blockHidden=1
onButtonPressed(button: controller.Button, handler: (selection: string, selectedIndex: number) => void) {
this.onButtonEvent(button, handler);
}
Expand All @@ -1206,6 +1210,7 @@ namespace miniMenu {
//% group="Controls"
//% weight=90
//% help=github:arcade-mini-menu/docs/on-selection-changed
//% blockHidden=1
onSelectionChanged(handler: (selection: string, selectedIndex: number) => void) {
this.itemSelectedHandler = handler;

Expand All @@ -1230,6 +1235,7 @@ namespace miniMenu {
//% group="Styling"
//% weight=50
//% help=github:arcade-mini-menu/docs/set-style-property
//% blockHidden=1
setStyleProperty(kind: StyleKind, property: StyleProperty, value: number) {
switch (kind) {
case StyleKind.Default:
Expand Down Expand Up @@ -1270,6 +1276,7 @@ namespace miniMenu {
//% weight=100
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-menu-style-property
//% blockHidden=1
setMenuStyleProperty(property: MenuStyleProperty, value: number) {
this.menuStyle.setMenuStyleProperty(property, value);

Expand Down Expand Up @@ -1311,6 +1318,7 @@ namespace miniMenu {
//% group="Create"
//% weight=20
//% help=github:arcade-mini-menu/docs/set-title
//% blockHidden=1
setTitle(title: string) {
this.title = new miniMenu.MenuItem(title, undefined);
this.updateDimensions();
Expand All @@ -1333,6 +1341,7 @@ namespace miniMenu {
//% weight=30
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-dimensions
//% blockHidden=1
setDimensions(width: number, height: number) {
this.setMenuStyleProperty(MenuStyleProperty.Width, width);
this.setMenuStyleProperty(MenuStyleProperty.Height, height);
Expand All @@ -1353,6 +1362,7 @@ namespace miniMenu {
//% weight=100
//% blockGap=8
//% help=github:arcade-mini-menu/docs/set-frame
//% blockHidden=1
setFrame(frame: Image) {
if (!frame) {
this.frame = frame;
Expand Down

0 comments on commit 3d4d76f

Please sign in to comment.