Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
2 : import type {
3 : IColorWidgetOptions,
4 : INumericWidget
5 : } from '@comfyorg/graph-model/widgets'
6 : import { useSettingStore } from '@/platform/settings/settingStore'
7 : import type { InputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
8 : import { isIntInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
9 : import type { ComfyWidgetConstructorV2 } from '@/types/widgetConstructor'
10 : import { addValueControlWidget } from '@/core/graph/widgets/valueControlWidgets'
11 : import { transformInputSpecV2ToV1 } from '@comfyorg/object-info-parser/migration'
12 :
13 161 : function onValueChange(this: INumericWidget, v: number) {
14 : // For integers, always round to the nearest step
15 : // step === 0 is invalid, assign 1 if options.step is 0
16 161 : const step = this.options.step2 || 1
17 :
18 161 : if (step === 1) {
19 : // Simple case: round to nearest integer
20 127 : this.value = Math.round(v)
21 : } else {
22 : // Round to nearest multiple of step
23 : // First, determine if min value creates an offset
24 : const min = this.options.min ?? 0
25 34 : const offset = min % step
26 :
27 : // Round to nearest step, accounting for offset
28 34 : this.value = Math.round((v - offset) / step) * step + offset
29 : }
30 : }
31 :
32 : export const _for_testing = {
33 : onValueChange
34 : }
35 :
36 2356 : export const useIntWidget = () => {
37 2356 : const widgetConstructor: ComfyWidgetConstructorV2 = (
38 : node: LGraphNode,
39 : inputSpec: InputSpec
40 : ) => {
41 16175 : if (!isIntInputSpec(inputSpec)) {
42 0 : throw new Error(`Invalid input data: ${inputSpec}`)
43 : }
44 :
45 : const settingStore = useSettingStore()
46 16175 : const sliderEnabled = !settingStore.get('Comfy.DisableSliders')
47 16175 : const display_type = inputSpec.display
48 16175 : const widgetType =
49 : display_type == 'color'
50 : ? 'color'
51 : : sliderEnabled && display_type == 'slider'
52 : ? 'slider'
53 : : display_type == 'knob'
54 : ? 'knob'
55 : : 'number'
56 :
57 16175 : const step = inputSpec.step ?? 1
58 : /** Assertion {@link inputSpec.default} */
59 16175 : const defaultValue = (inputSpec.default as number | undefined) ?? 0
60 16175 : const options: IColorWidgetOptions = {
61 : min: inputSpec.min ?? 0,
62 : max: inputSpec.max ?? 2048,
63 : /** @deprecated Use step2 instead. The 10x value is a legacy implementation. */
64 : step: step * 10,
65 : step2: step,
66 : precision: 0
67 : }
68 16175 : if (display_type == 'color') options.format = 'int'
69 :
70 16175 : const widget = node.addWidget(
71 : widgetType,
72 : inputSpec.name,
73 : defaultValue,
74 : onValueChange,
75 : options
76 : )
77 :
78 : const controlAfterGenerate =
79 : inputSpec.control_after_generate ??
80 : ['seed', 'noise_seed'].includes(inputSpec.name)
81 :
82 16175 : if (controlAfterGenerate) {
83 : const defaultType =
84 : typeof inputSpec.control_after_generate === 'string'
85 : ? inputSpec.control_after_generate
86 : : 'randomize'
87 : const controlWidget = addValueControlWidget(
88 : node,
89 : widget,
90 : defaultType,
91 : undefined,
92 : undefined,
93 : transformInputSpecV2ToV1(inputSpec)
94 : )
95 3527 : widget.linkedWidgets = [controlWidget]
96 : }
97 :
98 16175 : return widget
99 : }
100 2356 : return widgetConstructor
101 : }
|