| 6 | import {type KnitTexture, texturesMap} from './texture.js'; |
| 7 | |
| 8 | class Section { |
| 9 | widthTop: PatternFactoryInput; |
| 10 | widthBottom: PatternFactoryInput; |
| 11 | texture: PatternFactoryInput; |
| 12 | inputs: PatternFactoryInput[]; |
| 13 | knittingStyleInput: PatternFactoryInput; |
| 14 | |
| 15 | constructor( |
| 16 | sectionId: number, sectionCountInput: PatternFactoryInput, |
| 17 | knittingStyleInput: PatternFactoryInput) { |
| 18 | this.widthTop = new PatternFactoryInput( |
| 19 | `${sectionId}: Top Width`, |
| 20 | `How wide should section ${sectionId} be at the top?`, 3, 'stitches'); |
| 21 | this.widthBottom = new PatternFactoryInput( |
| 22 | `${sectionId}: Bottom Width`, |
| 23 | `How wide should section ${sectionId} be at the bottom?`, 30, |
| 24 | 'stitches'); |
| 25 | this.texture = new PatternFactoryInput( |
| 26 | `${sectionId}: Texture`, |
| 27 | `What type of pattern should section ${sectionId} use?`, 'Stockinette', |
| 28 | null, Array.from(texturesMap.keys())); |
| 29 | this.inputs = [this.widthTop, this.widthBottom, this.texture]; |
| 30 | this.knittingStyleInput = knittingStyleInput; |
| 31 | this.inputs.forEach( |
| 32 | (i) => i.addVisibilityRequirement( |
| 33 | () => sectionCountInput.numberValue() > sectionId)); |
| 34 | sectionCountInput.listener.addListener( |
| 35 | () => this.inputs.forEach((i) => i.adjustVisibility())); |
| 36 | } |
| 37 | |
| 38 | getInputs(): PatternFactoryInput[] { |
| 39 | return this.inputs; |
| 40 | } |
| 41 | |
| 42 | getStitches(rowIndex: number, rowLength: number): Stitch[] { |
| 43 | const previousWidth = rowIndex === 0 ? |
| 44 | this.widthTop.numberValue() : |
| 45 | this.#getRowWidth(rowIndex - 1, rowLength); |
| 46 | const desiredWidth = this.#getRowWidth(rowIndex, rowLength); |
| 47 | const textureValue = this.texture.value(); |
| 48 | if (textureValue === undefined || typeof textureValue !== 'string') { |
| 49 | throw new Error(`Invalid texture value: ${textureValue}`); |
| 50 | } |
| 51 | |
| 52 | const currentIncreaseAmount = |
| 53 | (desiredWidth > previousWidth) ? (desiredWidth - previousWidth) : 0; |
| 54 | |
| 55 | const rawTextureOutput: Stitch[] = |
| 56 | this.#applyTexture(textureValue, rowIndex); |
| 57 | |
| 58 | // The core texture needs to be `desiredWidth - currentIncreaseAmount` long. |
| 59 | // The `widthBottom` will be used as the base width for generating the |
| 60 | // texture. The `startTrim` is to center the `desiredWidth` segment within |
| 61 | // the `widthBottom`. |
| 62 | const stitchesToExtract = desiredWidth - currentIncreaseAmount; |
| 63 | const totalTrim = this.widthBottom.numberValue() - stitchesToExtract; |
| 64 | const startTrim = totalTrim / 2; |
| 65 |
nothing calls this directly
no outgoing calls
no test coverage detected