Author Topic: Navigating UIScrollView with keyboard and gamepad (UIKeyNavigation)?  (Read 6481 times)

twelvety

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Hi,

Apologies as I have posted this twice (in the NGUI 3 Documentation ยป UIScrollView too) but was not sure which was the right 'room'

Is it possible to control a UIScrollView with keyboard and gamepad? We do not use mouse input and we would like to scroll through the elements of the scrollview using the keyboard or gamepad. So far we have attached UIKeyNavigation and UIButton components to the children of the UIScrollView and OnSelect() of each child we try to position the scrollview using SpringPanel.Begin(panel.cachedGameObject, offset, strength);

However it proves quite hard to do so in a way that mimics a natural movement through a list of items

Is there an example we could refer to or some advice

Thank you in advance

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Navigating UIScrollView with keyboard and gamepad (UIKeyNavigation)?
« Reply #1 on: November 07, 2014, 02:52:22 PM »
Yes, easiest thing to do is to have a script that listens to OnSelect(true) on the items inside your scroll view, and does the same thing UICenterOnChild would do when clicked on. SpringPanel.Begin, basically.

twelvety

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Navigating UIScrollView with keyboard and gamepad (UIKeyNavigation)?
« Reply #2 on: November 07, 2014, 04:57:52 PM »
Hi - this is what I do - but I don't want to center the items when selected. I want to mimic a scroll list behaviour. i.e. start at the top and select item below till you reach the bottom of the first 'page', and then the scrollview starts moving up one by one till you reach the last 'page' of the list. When you select the item above though, it should stay to that current page till you reach the top of the page where the scrollview will start moving down one by one till you reach the first page

I was hoping that there is some code to do that - or some pseudocode to explain the steps - or someone has done this already

twelvety

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Navigating UIScrollView with keyboard and gamepad (UIKeyNavigation)?
« Reply #3 on: November 09, 2014, 07:42:19 AM »
I guess this isn't something that you have come across before - but again this is quite standard functionality when you design a game with no mouse support and you have got a list of selectable items i.e. achievements or rooms in a multiplayer game

will post my progress here

twelvety

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Navigating UIScrollView with keyboard and gamepad (UIKeyNavigation)?
« Reply #4 on: November 09, 2014, 07:31:38 PM »
This is the script attached to the items of the UIScrollView - hope it helps someone out there!

  1. public int ID;
  2. public static int PreviousID = -1;
  3.  
  4. private float scrollViewTopY = 178.0f;
  5. private float itemHeight = 30.0f;
  6.  
  7. UIPanel panel;
  8. UIScrollView scrollView;
  9. Vector3 offset = Vector3.zero;
  10.  
  11. public void AnimateToItem()
  12. {
  13.         SpringPanel.Begin(panel.cachedGameObject, offset, 6f);
  14. }
  15.  
  16. void OnSelect (bool selected)
  17. {
  18.         if (!selected) return;
  19.  
  20.         offset.y = -transform.localPosition.y;
  21.  
  22.         float posDiff = panel.transform.localPosition.y - scrollViewTopY + transform.localPosition.y - itemHeight;
  23.         posDiff = -posDiff;
  24.                
  25.         if (ID > PreviousID && posDiff > scrollView.panel.finalClipRegion.w ) {
  26.                 offset.y -= scrollView.panel.finalClipRegion.w/2 - itemHeight/2;
  27.                 AnimateToItem();
  28.         }
  29.         else if (ID < PreviousID && posDiff < itemHeight) {
  30.                 offset.y += scrollView.panel.finalClipRegion.w/2 - itemHeight/2;
  31.                 AnimateToItem();
  32.         }
  33.  
  34.         PreviousID = ID;
  35. }
  36.  
  37. void Start ()
  38. {
  39.         panel = NGUITools.FindInParents<UIPanel>(gameObject);
  40.         scrollView = panel.GetComponent<UIScrollView>();
  41. }
« Last Edit: November 10, 2014, 04:28:48 AM by twelvety »