Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector input binder for changing all inputs #347

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/NumericInput/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class NumericInput extends InputElement {
this._updatePosition(evt.movementX, evt.shiftKey);
};

protected _onSliderMouseDown = () => {
protected _onSliderMouseDown = (evt: MouseEvent) => {
this._sliderControl.dom.requestPointerLock();
this._sliderMovement = 0.0;
this._sliderPrevValue = this.value;
Expand All @@ -169,6 +169,8 @@ class NumericInput extends InputElement {
this.binding.historyCombine = true;
this.binding.historyPostfix = `(${Date.now()})`;
}

this.emit('slider:mousedown', evt);
};

protected _onSliderMouseUp = () => {
Expand All @@ -185,6 +187,9 @@ class NumericInput extends InputElement {
this._historyPostfix = null;
}
this.focus();

this.emit('slider:mouseup');

kpal81xd marked this conversation as resolved.
Show resolved Hide resolved
};

protected _onInputChange(evt: Event) {
Expand Down
36 changes: 33 additions & 3 deletions src/components/VectorInput/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class VectorInput extends Element implements IBindable, IFocusable, IPlaceholder

protected _applyingChange = false;

protected _bindAllInputs = false;

constructor(args: Readonly<VectorInputArgs> = {}) {
const elementArgs = { ...args };
// set binding after inputs have been created
Expand All @@ -64,8 +66,25 @@ class VectorInput extends Element implements IBindable, IFocusable, IPlaceholder
renderChanges: args.renderChanges,
placeholder: args.placeholder ? (Array.isArray(args.placeholder) ? args.placeholder[i] : args.placeholder) : null
});
input.on('slider:mousedown', (evt: MouseEvent) => {
this._bindAllInputs = !!evt.altKey;
if (this._bindAllInputs) {
for (let i = 0; i < this._inputs.length; i++) {
if (this._inputs[i] === input) continue;
this._inputs[i].class.add(pcuiClass.FOCUS);
}
}
});
input.on('slider:mouseup', () => {
this._onInputChange(input);
this._bindAllInputs = false;
for (let i = 0; i < this._inputs.length; i++) {
if (this._inputs[i] === input) continue;
this._inputs[i].class.remove(pcuiClass.FOCUS);
}
});
input.on('change', () => {
this._onInputChange();
this._onInputChange(input);
});
input.on('focus', () => {
this.emit('focus');
Expand All @@ -90,7 +109,8 @@ class VectorInput extends Element implements IBindable, IFocusable, IPlaceholder
}
}

protected _onInputChange() {

protected _onInputChange(input: NumericInput) {
if (this._applyingChange) return;

// check if any of our inputs have the MULTIPLE_VALUES class and if so inherit it for us as well
Expand All @@ -102,7 +122,17 @@ class VectorInput extends Element implements IBindable, IFocusable, IPlaceholder
this.class.remove(pcuiClass.MULTIPLE_VALUES);
}

this.emit('change', this.value);
if (this._bindAllInputs) {
const value = Array(this._inputs.length).fill(input.value);
const changed = this._updateValue(value);

if (changed && this._binding) {
this._binding.setValue(value);
}
} else {
this.emit('change', this.value);
}

}

protected _updateValue(value: number[]) {
Expand Down
Loading