File size: 6,466 Bytes
c6535db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import { drawTextInArea } from "@/composable/canvas.js"
import { Rectangle } from "@/composable/infrastructure/Rectangle.js"
export class BaseWidget {
/** From node edge to widget edge */
static margin = 15
/** From widget edge to tip of arrow button */
static arrowMargin = 6
/** Arrow button width */
static arrowWidth = 10
/** Absolute minimum display width of widget values */
static minValueWidth = 42
/** Minimum gap between label and value */
static labelValueGap = 5
#node;
/** The node that this widget belongs to. */
get node() {
return this.#node
}
#value;
get value() {
return this.#value
}
set value(value) {
this.#value = value
}
constructor(widget, node) {
// 处理不同的构造函数调用形式
if (node === undefined) {
node = widget.node;
}
// 私有字段
this.#node = node;
this.#value = widget.value;
// 属性设置
this.name = widget.name;
this.options = widget.options;
this.type = widget.type;
this.y = 0;
// 避免命名冲突
const { node: _, outline_color, background_color, height, text_color,
secondary_text_color, disabledTextColor, displayName, displayValue,
labelBaseline, ...safeValues } = widget;
Object.assign(this, safeValues);
}
get outline_color() {
return this.advanced ? LiteGraph.WIDGET_ADVANCED_OUTLINE_COLOR : LiteGraph.WIDGET_OUTLINE_COLOR;
}
get background_color() {
return LiteGraph.WIDGET_BGCOLOR;
}
get height() {
return LiteGraph.NODE_WIDGET_HEIGHT;
}
get text_color() {
return LiteGraph.WIDGET_TEXT_COLOR;
}
get secondary_text_color() {
return LiteGraph.WIDGET_SECONDARY_TEXT_COLOR;
}
get disabledTextColor() {
return LiteGraph.WIDGET_DISABLED_TEXT_COLOR;
}
get displayName() {
return this.label || this.name;
}
get _displayValue() {
return String(this.value);
}
get labelBaseline() {
return this.y + this.height * 0.7;
}
/**
* 绘制标准控件形状 - 细长的胶囊形状
* @param {CanvasRenderingContext2D} ctx 画布上下文
* @param {Object} options 绘制选项
*/
drawWidgetShape(ctx, { width, showText, isEasyUseTheme }) {
const { height, y } = this;
const { margin } = BaseWidget;
ctx.textAlign = "left";
ctx.strokeStyle = this.outline_color;
ctx.fillStyle = this.background_color;
ctx.beginPath();
if (showText) {
if(isEasyUseTheme) ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.25]);
else ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.5]);
} else {
ctx.rect(margin, y, width - margin * 2, height);
}
ctx.fill();
if (showText && !this.computedDisabled) ctx.stroke();
}
/**
* 绘制文本,如果超出可用宽度则截断
*/
drawTruncatingText({
ctx,
width,
leftPadding = 5,
rightPadding = 20,
isEasyUseTheme,
}) {
const { height, y } = this;
const { margin } = BaseWidget;
// 测量标签和值的宽度
const { displayName, _displayValue } = this;
const labelWidth = ctx.measureText(displayName).width;
const valueWidth = ctx.measureText(_displayValue).width;
const gap = BaseWidget.labelValueGap;
const x = margin * 2 + (isEasyUseTheme ? (leftPadding ? 2 : -8 ): leftPadding);
const totalWidth = width - x - 2 * margin - (isEasyUseTheme ? 4 : rightPadding);
const requiredWidth = labelWidth + gap + valueWidth;
const area = new Rectangle(x, y, totalWidth, height * 0.7);
// 如果启用EasyUse主题,则使用更小的字体
if(isEasyUseTheme){
ctx.font = "11px Inter"
}
ctx.fillStyle = this.secondary_text_color;
if (requiredWidth <= totalWidth) {
// 正常绘制标签和值
drawTextInArea({ ctx, text: displayName, area, align: "left" });
} else if (LiteGraph.truncateWidgetTextEvenly) {
// 标签+值不适合 - 均匀缩放以适应
const scale = (totalWidth - gap) / (requiredWidth - gap);
area.width = labelWidth * scale;
drawTextInArea({ ctx, text: displayName, area, align: "left" });
// 将区域向右移动以渲染值
area.right = x + totalWidth;
area.setWidthRightAnchored(valueWidth * scale);
} else if (LiteGraph.truncateWidgetValuesFirst) {
// 标签+值不适合 - 先缩放值的旧方法
const cappedLabelWidth = Math.min(labelWidth, totalWidth);
area.width = cappedLabelWidth;
drawTextInArea({ ctx, text: displayName, area, align: "left" });
area.right = x + totalWidth;
area.setWidthRightAnchored(Math.max(totalWidth - gap - cappedLabelWidth, 0));
} else {
// 标签+值不适合 - 先缩放标签
const cappedValueWidth = Math.min(valueWidth, totalWidth);
area.width = Math.max(totalWidth - gap - cappedValueWidth, 0);
drawTextInArea({ ctx, text: displayName, area, align: "left" });
area.right = x + totalWidth;
area.setWidthRightAnchored(cappedValueWidth);
}
ctx.fillStyle = this.text_color;
drawTextInArea({ ctx, text: _displayValue, area, align: "right" });
}
/**
* 设置控件的值
*/
setValue(value, { e, node, canvas }) {
const oldValue = this.value;
if (value === this.value) return;
const v = this.type === "number" ? Number(value) : value;
this.value = v;
if (
this.options?.property &&
node.properties[this.options.property] !== undefined
) {
node.setProperty(this.options.property, v);
}
const pos = canvas.graph_mouse;
if (this.callback) {
this.callback(this.value, canvas, node, pos, e);
}
if (node.onWidgetChanged) {
node.onWidgetChanged(this.name ?? "", v, oldValue, this);
}
if (node.graph) node.graph._version++;
}
} |