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

Scroll view to keep rect into view #2900

Closed
pixtur opened this issue Nov 17, 2019 · 2 comments
Closed

Scroll view to keep rect into view #2900

pixtur opened this issue Nov 17, 2019 · 2 comments

Comments

@pixtur
Copy link

pixtur commented Nov 17, 2019

Version/Branch of Dear ImGui:

Version: 1.62
Branch: Master

Back-end/Renderer/Compiler/OS

Back-ends: imgui.net
Operating System: Win10

My Issue/Question:
I'm struggling to scroll an area of a window into view. I studied the documentation and played around with various combinations of IsRectVisible and SetScrollHereY.

I don't understand, why SetScrollHereY() needs the paramete:. If the out-of-view area was below the visible region, I expect it to be at the bottom. Otherwise at the top. I would prefer a solution like "keep this rectangle visible, but scroll only if necessary and as little as necessary". I know that this is possible, because the Demo-Windows does just that, but can't get it to work.

Screenshots/Video

I'm changing an item with cursor-keys up down. The focus is inside a search-field.
My current (very messy code) looks like this, although I'm not sure if it helps:

            if (ImGui.BeginChildFrame(999, new Vector2(200, 200)))
            {
                if (_selectedSymbol == null && SymbolRegistry.Entries.Values.Any())
                    _selectedSymbol = SymbolRegistry.Entries.Values.FirstOrDefault();

                var index = _filter.MatchingSymbols.IndexOf(_selectedSymbol);
                var rectMin = ImGui.GetWindowContentRegionMin() + ImGui.GetWindowPos() + new Vector2(0,(index +1)*ImGui.GetFrameHeight());
                var rectMax = rectMin + new Vector2(10, 10);
                var isVisible = ImGui.IsRectVisible(rectMin, rectMax);

                // Draw debug rect
                ImGui.GetWindowDrawList().AddRectFilled(rectMin, rectMax, isVisible ? Color.Orange : Color.Blue  );

                if (_filterInputType != null)
                {
                    ImGui.PushFont(Fonts.FontSmall);
                    ImGui.TextDisabled(_filterInputType.Name);
                    ImGui.PopFont();
                }

                foreach (var symbol in _filter.MatchingSymbols)
                {
                    ImGui.PushID(symbol.Id.GetHashCode());
                    {
                        ImGui.Selectable("", symbol == _selectedSymbol);

                        if (ImGui.IsItemActivated())
                        {
                            CreateInstance(symbol);
                        }

                        ImGui.SameLine();
                        ImGui.Text(symbol.Name);
                        if (!String.IsNullOrEmpty(symbol.Namespace))
                        {
                            ImGui.TextDisabled(symbol.Namespace);
                            ImGui.SameLine();
                        }
                    }
                    ImGui.PopID();
                }
                if (!isVisible)
                {
                    Log.Debug("Scrolling into view");
                    var keepPos = ImGui.GetCursorScreenPos();
                    ImGui.SetCursorScreenPos(rectMin);
                    ImGui.SetScrollHereY(0);
                    ImGui.SetCursorScreenPos(keepPos);
                    _lastSelectedSymbol = _selectedSymbol;
                }
            }

            ImGui.EndChildFrame();
@ocornut
Copy link
Owner

ocornut commented Nov 18, 2019

For you can use ScrollToBringRectIntoView() in imgui_internal.h do perform what you want.
But it looks like if you are doing "I'm changing an item with cursor-keys up down" you may just as well just enable Keyboard navigation/controls with io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard and it'll work.

Please note that 1.62 is rather old at this point so some suggestions/issues may not apply or you may run into different issues than if using latest master.

Linking #2812 there is some work to be done on scrolling helpers/policies.

@pixtur
Copy link
Author

pixtur commented Nov 18, 2019

ScrollToBringRectIntoView() would be just the thing I was looking for. Sadly, I can't access it from ImGui.net 🙄 To me it sounds like a method that's worthy to publish in the API.

Regarding ImGuiConfigFlags_NavEnableKeyboard: I actually had to disable this flag when the filter input field has focus, because, I want to use keyboard up/down without losing the focus of this field.

I also checked the version I again. Looks like we're at 1.70. I guess should bump up to 1.72.

@pixtur pixtur closed this as completed Nov 18, 2019
ocornut added a commit that referenced this issue Sep 29, 2021
ocornut added a commit that referenced this issue Sep 29, 2021
ocornut added a commit that referenced this issue Nov 2, 2021
…ed (fix nav in one axis scrolling back and forth between axises when space is tight by just < ItemSpacing*2) (#3692, #2812, #4242, #2900)

Amend 8f495e5
actondev pushed a commit to actondev/imgui that referenced this issue Nov 26, 2021
…ed (fix nav in one axis scrolling back and forth between axises when space is tight by just < ItemSpacing*2) (ocornut#3692, ocornut#2812, ocornut#4242, ocornut#2900)

Amend 8f495e5
ocornut added a commit that referenced this issue Nov 24, 2022
…ocus()/ScrollToRectEx() during an appearing form not centering item. (#5902, #2812, #4242, #2900)

Amend 44f8011 and 8f495e5
ocornut pushed a commit that referenced this issue Dec 20, 2022
…tered element that is not visible but could be would take the item's Y coordinate into account.

Neither behavior were used in the codebase for this axis.
Amend 27c58c3 (#5902, #2812, #4242, #2900)
Signed-off-by: Neil Bickford <nbickford@nvidia.com>
eoudejans added a commit to ObjectVision/GeoDMS that referenced this issue May 2, 2023
…ary. For instance triggered by a treenode one scroll away, navigated to via keyboard valid first item character key press. See also: ocornut/imgui#2900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants