How to focus on an area on a screen with JUCE UI


#1

Hi all,
I have a screen that has a ScreenComponent is inside a ViewportComponent to make is can be scrolled. I draw some thing on ScreenComponent and the UI has a vertical scroll bar on the right side. How to set UI focuses to some where on the screen?


#2

Viewport::setViewPosition()

https://docs.juce.com/master/classViewport.html#a138c2e06be0437fbeab40542c00bbbe1

Rail


#3

I got it. Thank you so much @Rail_Jon_Rogut


#4

@Rail_Jon_Rogut, do you know how to get current position of vertical scroll bar inside a viewport?
I mean how to do if I want an icon moving follow the vertical scroll bar :slight_smile:


#5
 double dPos = yourViewport.getVerticalScrollBar().getCurrentRange().getStart();

Rail


#6

And if myViewport is inside a component, how to catch it’s scrollbar moving even @Rail_Jon_Rogut?


#7

If you look at the ViewPort class source…

class JUCE_API  Viewport  : public Component,
                            private ComponentListener,
                            private ScrollBar::Listener
{

Notice that it’s derived from ScrollBar::Listener and has a method:

void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
    auto newRangeStartInt = roundToInt (newRangeStart);

    if (scrollBarThatHasMoved == horizontalScrollBar.get())
    {
        setViewPosition (newRangeStartInt, getViewPositionY());
    }
    else if (scrollBarThatHasMoved == verticalScrollBar.get())
    {
        setViewPosition (getViewPositionX(), newRangeStartInt);
    }
}

You can derive a class from ViewPort and override scrollBarMoved() which calls the base class and also does what you need.

Rail


#8

Yes @Rail_Jon_Rogut, and I found that we can also use void visibleAreaChanged(const Rectangle<int>& newVisibleArea) override;
Thank you so much :slight_smile: