|
| 1 | +import { InputType } from 'sb3-types/enum' |
| 2 | +import { fromPrimitiveSource, menuInput } from '../core/block-helper' |
| 3 | +import { attachStack, block, valueBlock } from '../core/composer' |
| 4 | +import type { |
| 5 | + HikkakuNumber, |
| 6 | + HikkakuString, |
| 7 | + PrimitiveSource, |
| 8 | +} from '../core/types' |
| 9 | + |
| 10 | +export type VideoSensingAttribute = 'motion' | 'direction' |
| 11 | +export type VideoSensingSubject = 'this sprite' | 'stage' |
| 12 | +export type VideoState = 'on' | 'off' | 'on-flipped' |
| 13 | + |
| 14 | +/** |
| 15 | + * Runs when motion is greater than a threshold. |
| 16 | + * |
| 17 | + * Input: `reference`, `stack`. |
| 18 | + * Output: Scratch statement block definition that is appended to the current script stack. |
| 19 | + * |
| 20 | + * @param reference See function signature for accepted input values. |
| 21 | + * @param stack See function signature for accepted input values. Optional. |
| 22 | + * @returns Scratch statement block definition that is appended to the current script stack. |
| 23 | + * @example |
| 24 | + * ```ts |
| 25 | + * import { whenMotionGreaterThan } from 'hikkaku/blocks' |
| 26 | + * |
| 27 | + * whenMotionGreaterThan(10, () => {}) |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +export const whenMotionGreaterThan = ( |
| 31 | + reference: PrimitiveSource<HikkakuNumber>, |
| 32 | + stack?: () => void, |
| 33 | +) => { |
| 34 | + const res = block('videoSensing_whenMotionGreaterThan', { |
| 35 | + topLevel: true, |
| 36 | + inputs: { |
| 37 | + REFERENCE: fromPrimitiveSource(InputType.Number, reference, 10), |
| 38 | + }, |
| 39 | + }) |
| 40 | + attachStack(res.id, stack) |
| 41 | + return res |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Returns motion or direction detected from a subject. |
| 46 | + * |
| 47 | + * Input: `attribute`, `subject`. |
| 48 | + * Output: Scratch reporter block definition that can be used as an input value in other blocks. |
| 49 | + * |
| 50 | + * @param attribute See function signature for accepted input values. |
| 51 | + * @param subject See function signature for accepted input values. |
| 52 | + * @returns Scratch reporter block definition that can be used as an input value in other blocks. |
| 53 | + * @example |
| 54 | + * ```ts |
| 55 | + * import { videoOn } from 'hikkaku/blocks' |
| 56 | + * |
| 57 | + * videoOn('motion', 'this sprite') |
| 58 | + * ``` |
| 59 | + */ |
| 60 | +export const videoOn = ( |
| 61 | + attribute: PrimitiveSource<HikkakuString>, |
| 62 | + subject: PrimitiveSource<HikkakuString>, |
| 63 | +) => { |
| 64 | + return valueBlock<HikkakuNumber>('videoSensing_videoOn', { |
| 65 | + inputs: { |
| 66 | + ATTRIBUTE: menuInput(attribute, menuOfVideoSensingAttribute), |
| 67 | + SUBJECT: menuInput(subject, menuOfVideoSensingSubject), |
| 68 | + }, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +export const menuOfVideoSensingAttribute = ( |
| 73 | + attribute: VideoSensingAttribute = 'motion', |
| 74 | +) => { |
| 75 | + return valueBlock<HikkakuString>('videoSensing_menu_ATTRIBUTE', { |
| 76 | + fields: { |
| 77 | + ATTRIBUTE: [attribute, null], |
| 78 | + }, |
| 79 | + isShadow: true, |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +export const menuOfVideoSensingSubject = ( |
| 84 | + subject: VideoSensingSubject = 'this sprite', |
| 85 | +) => { |
| 86 | + return valueBlock<HikkakuString>('videoSensing_menu_SUBJECT', { |
| 87 | + fields: { |
| 88 | + SUBJECT: [subject, null], |
| 89 | + }, |
| 90 | + isShadow: true, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Turns video on/off/flipped. |
| 96 | + * |
| 97 | + * Input: `state`. |
| 98 | + * Output: Scratch statement block definition that is appended to the current script stack. |
| 99 | + * |
| 100 | + * @param state See function signature for accepted input values. |
| 101 | + * @returns Scratch statement block definition that is appended to the current script stack. |
| 102 | + * @example |
| 103 | + * ```ts |
| 104 | + * import { videoToggle } from 'hikkaku/blocks' |
| 105 | + * |
| 106 | + * videoToggle('on') |
| 107 | + * ``` |
| 108 | + */ |
| 109 | +export const videoToggle = (state: PrimitiveSource<HikkakuString>) => { |
| 110 | + return block('videoSensing_videoToggle', { |
| 111 | + inputs: { |
| 112 | + VIDEO_STATE: menuInput(state, menuOfVideoSensingState), |
| 113 | + }, |
| 114 | + }) |
| 115 | +} |
| 116 | + |
| 117 | +export const menuOfVideoSensingState = (state: VideoState = 'on') => { |
| 118 | + return valueBlock<HikkakuString>('videoSensing_menu_VIDEO_STATE', { |
| 119 | + fields: { |
| 120 | + VIDEO_STATE: [state, null], |
| 121 | + }, |
| 122 | + isShadow: true, |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Sets video transparency. |
| 128 | + * |
| 129 | + * Input: `transparency`. |
| 130 | + * Output: Scratch statement block definition that is appended to the current script stack. |
| 131 | + * |
| 132 | + * @param transparency See function signature for accepted input values. |
| 133 | + * @returns Scratch statement block definition that is appended to the current script stack. |
| 134 | + * @example |
| 135 | + * ```ts |
| 136 | + * import { setVideoTransparency } from 'hikkaku/blocks' |
| 137 | + * |
| 138 | + * setVideoTransparency(50) |
| 139 | + * ``` |
| 140 | + */ |
| 141 | +export const setVideoTransparency = ( |
| 142 | + transparency: PrimitiveSource<HikkakuNumber>, |
| 143 | +) => { |
| 144 | + return block('videoSensing_setVideoTransparency', { |
| 145 | + inputs: { |
| 146 | + TRANSPARENCY: fromPrimitiveSource(InputType.Number, transparency, 50), |
| 147 | + }, |
| 148 | + }) |
| 149 | +} |
0 commit comments