Hello again!
I have a question regarding the construction of a custom control using another control defined in the Babylon GUI Editor. I hope this question hasn’t been answered before. I tried searching for it, but I would appreciate it if you could provide a link if it has been addressed previously.
My initial problem was that I wanted to have a custom ScrollViewer that I created in the GUI editor, including its children and settings. However, when parsing the .json file from the editor, I wanted to load it as a ScrollViewer with an ImageBasedSlider instead of a regular Slider.
I know that you can achieve this in code by adding “true” when constructing a new ScrollViewer, like this:
const myScrollViewer = new BABYLON.GUI.ScrollViewer("", true);
So, I thought I could recreate it by copying everything from the old ScrollViewer and adding the required images somehow like this:
private prepareScrollViewers(scrollViewers: Control[]): void {
scrollViewers.forEach((scrollViewer) => {
// Copy properties from scrollViewerOld to scrollViewerImageBased
for (const prop in scrollViewerOld) {
if (scrollViewerOld.hasOwnProperty(prop)) {
scrollViewerImageBased[prop] = scrollViewerOld[prop];
}
}
// Set specific properties for the image-based ScrollViewer
scrollViewerImageBased.thumbImage = new Image("thumb", "./assets/sprites/SVG/scroll_thumb.svg");
scrollViewerImageBased.barImage = new Image("bar", "./assets/sprites/SVG/scroll_bar.png");
});
}
However, this approach didn’t work because it also copied the “don’t use imageBasedSlider” option. So, I tried to copy only some settings, which partially worked. The problem arose with all the children of the old ScrollViewer, as I struggled to set them as children of the new control and properly assign the new control as their parent using addControl
, but I failed in my attempts.
Eventually, I decided to abandon the idea and use the non-imageBased ScrollViewer instead.
However, today I wanted to make the ScrollViewer work on mobile devices by enabling “touch anywhere to scroll” functionality throughout the entire ScrollViewer. The scroll bar was too small, and it made sense to allow users to swipe anywhere on the ScrollViewer to scroll it.
I found this amazing person who solved it by creating a custom ScrollViewer. You can find their solution in this forum post: Link to the forum post
They also provided a demo of the custom ScrollViewer in action, which you can find here: Link to the demo
Now, the problem resurfaces. I would like to define all my GUI using the .json file in the GUI editor, but I would also like to convert all the ScrollViewer controls in my project to use this customScrollViewer.
Is there a way to construct, copy, or duplicate a control from a .json file in the code to create a custom control with the same settings, children, and parents?
Thank you for any advice and for taking the time to read my post!