Obtain GUI Component Within onValueChangedObservable on Slider and ColorPicker

I’m using the same observer for multiple sliders, and I want to obtain the specific slider control being changed from the onValueChangedObservable.

This time, I’m using a slider (whereas here is the same issue I posted a year ago about ColorPicker).

I think the change required is in one place for slider, and one place for color picker:

line 160 in baseSlider:

this.onValueChangedObservable.notifyObservers(this._value);

And line 90 in colorpicker.ts:

this.onValueChangedObservable.notifyObservers(this._value);

Recommend to change those lines to:

this.onValueChangedObservable.notifyObservers(this._value, -1, this, this);

Background

from Babylon.js docs

notifyObservers(
eventData: T,
mask?: number,
target?: any,
currentTarget?: any,
userInfo?: any,
): boolean

Both target and currentTarget are defined as Control.
Looking at pointer observables, target comes from a parameter to the calling function, but currentTarget is “this”: the control from which the observable is called. In the Button Control, target and currentTarget are identical in my simple usage/experiments.

For JavaScript Events in general, here is the difference between target and currentTarget (from sqlpey.com):

  • target: The target property refers to the element that dispatched the event. For instance, if a user clicks on a child <button> element within a <div>, the target will be the button itself. This property is helpful for determining exactly which element triggered the event.
  • currentTarget: Conversely, the currentTarget property represents the element to which the event listener is currently attached. Continuing with the previous example, if the <div> has an event listener for the click event, the currentTarget will be the <div> no matter which button is clicked.

From this description and because the “event listener” (aka observable) is on the slider itself, both target and currentTarget should probably be the same in the case of these slider and colorpicker modifications.

cc @georgie

1 Like

@HiGreg hi greg! that seems like a reasonable change, do you want to create a PR with the proposed change? would be great to also see a playground using said change for testing purposes (one that is broken otherwise)

Small note, I recommend sending undefined for the mask parameter

cc @amoebachant who reviewed the original post shared above

I don’t have a way to test the pull request. But I created both pull requests separately. First ever to babylon, not sure I did it right. Playground incoming.

1 Like

Exciting to create first PRs! Once you have a playground snippet, you can test it using the generated links within the PR comments. Looks like the build is failing due to formatting, likely the newline added at the end of the file

Playground testing target in slider event.

I edited on github, which added the newline. I can try to fix.

baseSlider.ts PR works well. Lower right (last word) in popup shows slider name instead of “undefined”.

colorPicker.ts testing onValueChanged state.target.

Test newest nightly
Test Pull Request

Failed test results in no color changes to sphere. Passing test results in color changes.

Will update this post when commit is passing test.
Test above is passing. Pull Request un-draft-ed.

Things I don’t know how to do: squash commits into one, tag commit with label (“feature request”), rename PR

The commits get squashed into 1 when you merge so you don’t have to worry about doing it manually. To add a tag you select ‘label’ shown in the below screenshot. To edit PR title you click the ‘edit’ button.

Also, here are our docs for contributing to babylon. It is easiest to clone the repo / use vscode to make the change. Formatting will be taken into account if you install the recommended vscode extension prettier. You can also test locally before pushing a PR by using the instructions in the docs
Start Contributing to Babylon.js | Babylon.js Documentation

1 Like

I’ve updated the PR descriptions, added the labels, updated title, and approved PRs. Thank you!

1 Like