From fe949aafda44bb8622c5121bc39300f25616d806 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Aug 2021 13:38:19 -0700 Subject: [PATCH 1/5] UI: Track page up/down and home/end as focus moves. Doesn't actually do anything yet when pressed. --- Common/UI/Root.cpp | 18 +++++++++++++++++- Common/UI/View.h | 8 ++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Common/UI/Root.cpp b/Common/UI/Root.cpp index ef60f64e12..ad4b9c2c4c 100644 --- a/Common/UI/Root.cpp +++ b/Common/UI/Root.cpp @@ -195,11 +195,23 @@ static std::set heldKeys; const double repeatDelay = 15 * (1.0 / 60.0f); // 15 frames like before. const double repeatInterval = 5 * (1.0 / 60.0f); // 5 frames like before. +bool IsScrollKey(const KeyInput &input) { + switch (input.keyCode) { + case NKCODE_PAGE_UP: + case NKCODE_PAGE_DOWN: + case NKCODE_MOVE_HOME: + case NKCODE_MOVE_END: + return true; + default: + return false; + } +} + bool KeyEvent(const KeyInput &key, ViewGroup *root) { bool retval = false; // Ignore repeats for focus moves. if ((key.flags & (KEY_DOWN | KEY_IS_REPEAT)) == KEY_DOWN) { - if (IsDPadKey(key)) { + if (IsDPadKey(key) || IsScrollKey(key)) { // Let's only repeat DPAD initially. HeldKey hk; hk.key = key.keyCode; @@ -399,6 +411,10 @@ void UpdateViewHierarchy(ViewGroup *root) { case NKCODE_DPAD_RIGHT: MoveFocus(root, FOCUS_RIGHT); break; case NKCODE_DPAD_UP: MoveFocus(root, FOCUS_UP); break; case NKCODE_DPAD_DOWN: MoveFocus(root, FOCUS_DOWN); break; + case NKCODE_PAGE_UP: MoveFocus(root, FOCUS_PREV_PAGE); break; + case NKCODE_PAGE_DOWN: MoveFocus(root, FOCUS_NEXT_PAGE); break; + case NKCODE_MOVE_HOME: MoveFocus(root, FOCUS_FIRST); break; + case NKCODE_MOVE_END: MoveFocus(root, FOCUS_LAST); break; } } } diff --git a/Common/UI/View.h b/Common/UI/View.h index fd05d38e9b..860b133881 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -128,6 +128,10 @@ enum FocusDirection { FOCUS_RIGHT, FOCUS_NEXT, FOCUS_PREV, + FOCUS_FIRST, + FOCUS_LAST, + FOCUS_PREV_PAGE, + FOCUS_NEXT_PAGE, }; enum { @@ -177,6 +181,10 @@ inline FocusDirection Opposite(FocusDirection d) { case FOCUS_RIGHT: return FOCUS_LEFT; case FOCUS_PREV: return FOCUS_NEXT; case FOCUS_NEXT: return FOCUS_PREV; + case FOCUS_FIRST: return FOCUS_LAST; + case FOCUS_LAST: return FOCUS_FIRST; + case FOCUS_PREV_PAGE: return FOCUS_NEXT_PAGE; + case FOCUS_NEXT_PAGE: return FOCUS_PREV_PAGE; } return d; } From 8df188af2b1c834b726681546ff82317fd278591 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Aug 2021 13:40:55 -0700 Subject: [PATCH 2/5] UI: Change focus with home/end. --- Common/UI/ViewGroup.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index 7f133a25b6..3f876ace34 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -272,7 +272,7 @@ static float VerticalOverlap(const Bounds &a, const Bounds &b) { return std::min(1.0f, overlap / std::min(a.h, b.h)); } -float GetDirectionScore(View *origin, View *destination, FocusDirection direction) { +float GetDirectionScore(int originIndex, View *origin, View *destination, FocusDirection direction) { // Skip labels and things like that. if (!destination->CanBeFocused()) return 0.0f; @@ -332,6 +332,20 @@ float GetDirectionScore(View *origin, View *destination, FocusDirection directio } vertical = true; break; + case FOCUS_FIRST: + if (originIndex == -1) + return 0.0f; + if (dirX > 0.0f || dirY > 0.0f) + return 0.0f; + // More distance is good. + return distance; + case FOCUS_LAST: + if (originIndex == -1) + return 0.0f; + if (dirX < 0.0f || dirY < 0.0f) + return 0.0f; + // More distance is good. + return distance; case FOCUS_PREV: case FOCUS_NEXT: ERROR_LOG(SYSTEM, "Invalid focus direction"); @@ -398,13 +412,15 @@ NeighborResult ViewGroup::FindNeighbor(View *view, FocusDirection direction, Nei case FOCUS_LEFT: case FOCUS_RIGHT: case FOCUS_DOWN: + case FOCUS_FIRST: + case FOCUS_LAST: { // First, try the child views themselves as candidates for (size_t i = 0; i < views_.size(); i++) { if (views_[i] == view) continue; - float score = GetDirectionScore(view, views_[i], direction); + float score = GetDirectionScore(num, view, views_[i], direction); if (score > result.score) { result.score = score; result.view = views_[i]; @@ -782,13 +798,6 @@ bool ScrollView::Key(const KeyInput &input) { case NKCODE_PAGE_UP: ScrollRelative(-(orientation_ == ORIENT_VERTICAL ? bounds_.h : bounds_.w) + 50); break; - case NKCODE_MOVE_HOME: - ScrollTo(0); - break; - case NKCODE_MOVE_END: - if (views_.size()) - ScrollTo(orientation_ == ORIENT_VERTICAL ? views_[0]->GetBounds().h : views_[0]->GetBounds().w); - break; } } return ViewGroup::Key(input); From b52a495b9609f868ed3677d4b1e9002d9bb64014 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Aug 2021 14:46:05 -0700 Subject: [PATCH 3/5] UI: Handle focus on page up/down. This moves keyboard focus along with paging, but it also only responds to paging when already focused inside the scroll view. --- Common/UI/View.h | 1 + Common/UI/ViewGroup.cpp | 101 +++++++++++++++++++++++++++++++++++++--- Common/UI/ViewGroup.h | 8 +++- 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/Common/UI/View.h b/Common/UI/View.h index 860b133881..f21f4475fd 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -448,6 +448,7 @@ public: // Fake RTTI virtual bool IsViewGroup() const { return false; } + virtual bool ContainsSubview(const View *view) const { return false; } Point GetFocusPosition(FocusDirection dir); diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index 3f876ace34..0f3bb0f843 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -57,6 +57,14 @@ void ViewGroup::RemoveSubview(View *view) { } } +bool ViewGroup::ContainsSubview(const View *view) const { + for (const View *subview : views_) { + if (subview == view || subview->ContainsSubview(view)) + return true; + } + return false; +} + void ViewGroup::Clear() { std::lock_guard guard(modifyLock_); for (size_t i = 0; i < views_.size(); i++) { @@ -444,11 +452,57 @@ NeighborResult ViewGroup::FindNeighbor(View *view, FocusDirection direction, Nei return result; } + case FOCUS_PREV_PAGE: + case FOCUS_NEXT_PAGE: + return FindScrollNeighbor(view, Point(INFINITY, INFINITY), direction, result); + default: return result; } } +float GetTargetScore(const Point &target, View *view) { + if (!view->CanBeFocused()) + return 0.0f; + if (view->IsEnabled() == false) + return 0.0f; + if (view->GetVisibility() != V_VISIBLE) + return 0.0f; + + Point viewPos = view->GetBounds().Center(); + float dx = viewPos.x - target.x; + float dy = viewPos.y - target.y; + + float distance = sqrtf(dx * dx + dy * dy); + return 10.0f / std::max(1.0f, distance); +} + +NeighborResult ViewGroup::FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best) { + if (!IsEnabled()) + return best; + if (GetVisibility() != V_VISIBLE) + return best; + + if (target.x < INFINITY && target.y < INFINITY) { + for (auto v : views_) { + // Note: we consider the origin itself, which might already be the best option. + float score = GetTargetScore(target, v); + if (score > best.score) { + best.score = score; + best.view = v; + } + } + } + for (auto v : views_) { + if (v->IsViewGroup()) { + ViewGroup *vg = static_cast(v); + if (vg) + best = vg->FindScrollNeighbor(view, target, direction, best); + } + } + return best; +} + // TODO: This code needs some cleanup/restructuring... void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) { MeasureBySpec(layoutParams_->width, 0.0f, horiz, &measuredWidth_); @@ -792,12 +846,6 @@ bool ScrollView::Key(const KeyInput &input) { case NKCODE_EXT_MOUSEWHEEL_DOWN: ScrollRelative(250); break; - case NKCODE_PAGE_DOWN: - ScrollRelative((orientation_ == ORIENT_VERTICAL ? bounds_.h : bounds_.w) - 50); - break; - case NKCODE_PAGE_UP: - ScrollRelative(-(orientation_ == ORIENT_VERTICAL ? bounds_.h : bounds_.w) + 50); - break; } } return ViewGroup::Key(input); @@ -903,6 +951,47 @@ bool ScrollView::SubviewFocused(View *view) { return true; } +NeighborResult ScrollView::FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best) { + if (ContainsSubview(view) && views_[0]->IsViewGroup()) { + ViewGroup *vg = static_cast(views_[0]); + int found = -1; + for (int i = 0, n = vg->GetNumSubviews(); i < n; ++i) { + View *child = vg->GetViewByIndex(i); + if (child == view || child->ContainsSubview(view)) { + found = i; + break; + } + } + + // Okay, the previously focused view is inside this. + if (found != -1) { + float mult = 0.0f; + switch (direction) { + case FOCUS_PREV_PAGE: + mult = -1.0f; + break; + case FOCUS_NEXT_PAGE: + mult = 1.0f; + break; + default: + break; + } + + // Okay, now where is our ideal target? + Point targetPos = view->GetBounds().Center(); + if (orientation_ == ORIENT_VERTICAL) + targetPos.y += mult * bounds_.h; + else + targetPos.x += mult * bounds_.x; + + // Okay, which subview is closest to that? + return vg->FindScrollNeighbor(view, targetPos, direction, best); + } + } + + return ViewGroup::FindScrollNeighbor(view, target, direction, best); +} + void ScrollView::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) { ViewGroup::PersistData(status, anonId, storage); diff --git a/Common/UI/ViewGroup.h b/Common/UI/ViewGroup.h index 730f74f10b..5619d2317b 100644 --- a/Common/UI/ViewGroup.h +++ b/Common/UI/ViewGroup.h @@ -63,9 +63,11 @@ public: // Assumes that layout has taken place. NeighborResult FindNeighbor(View *view, FocusDirection direction, NeighborResult best); + virtual NeighborResult FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best); - virtual bool CanBeFocused() const override { return false; } - virtual bool IsViewGroup() const override { return true; } + bool CanBeFocused() const override { return false; } + bool IsViewGroup() const override { return true; } + bool ContainsSubview(const View *view) const override; virtual void SetBG(const Drawable &bg) { bg_ = bg; } @@ -268,6 +270,8 @@ public: void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override; void SetVisibility(Visibility visibility) override; + NeighborResult FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best) override; + // Quick hack to prevent scrolling to top in some lists void SetScrollToTop(bool t) { scrollToTopOnSizeChange_ = t; } From 700a7baa9b0802ba3b1973833b57d0cf5272f9fa Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Aug 2021 15:20:18 -0700 Subject: [PATCH 4/5] UI: Refactor to reuse distance logic. --- Common/UI/ViewGroup.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index 0f3bb0f843..efae72cdf8 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -280,7 +280,7 @@ static float VerticalOverlap(const Bounds &a, const Bounds &b) { return std::min(1.0f, overlap / std::min(a.h, b.h)); } -float GetDirectionScore(int originIndex, View *origin, View *destination, FocusDirection direction) { +float GetTargetScore(const Point &originPos, int originIndex, View *origin, View *destination, FocusDirection direction) { // Skip labels and things like that. if (!destination->CanBeFocused()) return 0.0f; @@ -289,7 +289,6 @@ float GetDirectionScore(int originIndex, View *origin, View *destination, FocusD if (destination->GetVisibility() != V_VISIBLE) return 0.0f; - Point originPos = origin->GetFocusPosition(direction); Point destPos = destination->GetFocusPosition(Opposite(direction)); float dx = destPos.x - originPos.x; @@ -305,8 +304,10 @@ float GetDirectionScore(int originIndex, View *origin, View *destination, FocusD float horizOverlap = HorizontalOverlap(origin->GetBounds(), destination->GetBounds()); float vertOverlap = VerticalOverlap(origin->GetBounds(), destination->GetBounds()); if (horizOverlap == 1.0f && vertOverlap == 1.0f) { - INFO_LOG(SYSTEM, "Contain overlap"); - return 0.0; + if (direction != FOCUS_PREV_PAGE && direction != FOCUS_NEXT_PAGE) { + INFO_LOG(SYSTEM, "Contain overlap"); + return 0.0; + } } float originSize = 0.0f; switch (direction) { @@ -354,6 +355,11 @@ float GetDirectionScore(int originIndex, View *origin, View *destination, FocusD return 0.0f; // More distance is good. return distance; + case FOCUS_PREV_PAGE: + case FOCUS_NEXT_PAGE: + // Not always, but let's go with the bonus on height. + vertical = true; + break; case FOCUS_PREV: case FOCUS_NEXT: ERROR_LOG(SYSTEM, "Invalid focus direction"); @@ -385,6 +391,11 @@ float GetDirectionScore(int originIndex, View *origin, View *destination, FocusD return 10.0f / std::max(1.0f, distance - distanceBonus) + overlap; } +float GetDirectionScore(int originIndex, View *origin, View *destination, FocusDirection direction) { + Point originPos = origin->GetFocusPosition(direction); + return GetTargetScore(originPos, originIndex, origin, destination, direction); +} + NeighborResult ViewGroup::FindNeighbor(View *view, FocusDirection direction, NeighborResult result) { if (!IsEnabled()) return result; @@ -461,22 +472,6 @@ NeighborResult ViewGroup::FindNeighbor(View *view, FocusDirection direction, Nei } } -float GetTargetScore(const Point &target, View *view) { - if (!view->CanBeFocused()) - return 0.0f; - if (view->IsEnabled() == false) - return 0.0f; - if (view->GetVisibility() != V_VISIBLE) - return 0.0f; - - Point viewPos = view->GetBounds().Center(); - float dx = viewPos.x - target.x; - float dy = viewPos.y - target.y; - - float distance = sqrtf(dx * dx + dy * dy); - return 10.0f / std::max(1.0f, distance); -} - NeighborResult ViewGroup::FindScrollNeighbor(View *view, const Point &target, FocusDirection direction, NeighborResult best) { if (!IsEnabled()) return best; @@ -486,7 +481,7 @@ NeighborResult ViewGroup::FindScrollNeighbor(View *view, const Point &target, Fo if (target.x < INFINITY && target.y < INFINITY) { for (auto v : views_) { // Note: we consider the origin itself, which might already be the best option. - float score = GetTargetScore(target, v); + float score = GetTargetScore(target, -1, view, v, direction); if (score > best.score) { best.score = score; best.view = v; From b43069109bb478565c0500c7fb92e0596f9d67e2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Aug 2021 15:23:12 -0700 Subject: [PATCH 5/5] UI: Avoid reselecting on page up at top. --- Common/UI/ViewGroup.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index efae72cdf8..55d1cbe5ef 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -980,7 +980,11 @@ NeighborResult ScrollView::FindScrollNeighbor(View *view, const Point &target, F targetPos.x += mult * bounds_.x; // Okay, which subview is closest to that? - return vg->FindScrollNeighbor(view, targetPos, direction, best); + best = vg->FindScrollNeighbor(view, targetPos, direction, best); + // Avoid reselecting the same view. + if (best.view == view) + best.view = nullptr; + return best; } }