Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: getluky on April 25, 2012, 09:29:11 PM

Title: UIDraggablePanel and UIButtonKeys navigation...
Post by: getluky on April 25, 2012, 09:29:11 PM
I am looking into making UIDraggablePanel move along with current position in an array of UIButtonKeys elements inside a dynamically-sized UIGrid. Specifically, I have an array of video resolution buttons that might be paged by controller or keyboard. I have a basic plan for this, which is to have a UIButtonKeysScrollSimulator script on each element that knows its position inside the grid. If it is the UICamera.selectedObject, then set the scrollbar's scrollValue property manually according to its relative position horizontally / vertically.

Is this a good idea, or is there a much better way to accomplish this?
Title: Re: UIDraggablePanel and UIButtonKeys navigation...
Post by: ArenMook on April 25, 2012, 09:33:57 PM
What you're trying to accomplish seems pretty specific... moving the panel and selection with keys? There is no built-in functionality for moving the panel with keys, so your custom solution is the way to go.
Title: Re: UIDraggablePanel and UIButtonKeys navigation...
Post by: getluky on April 26, 2012, 01:22:23 PM
Right... well, specifically I'm thinking about moving the panel viewport so that the current UICamera.selectedObject is always in view. So now that I think of it that way, is it better to have the UIDraggablePanel monitor for changes in selectedObject? Pushing the scrollbar around from keys just sounds a bit hacky.
Title: Re: UIDraggablePanel and UIButtonKeys navigation...
Post by: ArenMook on April 26, 2012, 03:23:04 PM
Draggable panel really doesn't have anything to do with currently selected object. You can have many draggable panels on the screen at once. Create a custom script attached to the panel that will monitor your selection, and if it's outside of the panel's bounds, moves it. Alternatively you can use Vector3.Lerp(currentPos, selectedObjectPos, deltaTime * 5f) to do it smoothly -- for example always centering on the selected object.
Title: Re: UIDraggablePanel and UIButtonKeys navigation...
Post by: getluky on April 26, 2012, 04:58:50 PM
You are correct, I misspoke and meant to say that I felt it might be more useful for me to monitor at the UIDraggablePanel gameObject through a custom script. :)

It was a good hint, and I think I got something together. Here's my implementation that just keeps the current selection just inside the clip bounds, instead of being centered. It uses SpringPanel for smooth animation.

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent(typeof(UIDraggablePanel))]
  4. public class UIDraggablePanelSelectedObjectFollower : MonoBehaviour {
  5.  
  6.         UIDraggablePanel draggablePanel;
  7.         Transform mTrans;
  8.         GameObject mGO;
  9.         GameObject lastFollowed;
  10.        
  11.         void Awake() {
  12.                 mGO = gameObject;
  13.                 mTrans = transform;
  14.                 draggablePanel = GetComponent<UIDraggablePanel>();     
  15.         }
  16.        
  17.         void Update() {
  18.                 if (enabled && mGO.active && UICamera.selectedObject != null && UICamera.selectedObject != lastFollowed) {
  19.                         if (!UICamera.selectedObject.transform.IsChildOf(draggablePanel.transform)) return;
  20.                        
  21.                         var draggableBounds = draggablePanel.bounds;
  22.                        
  23.                         var widgetBounds = NGUIMath.CalculateRelativeWidgetBounds(draggablePanel.transform, UICamera.selectedObject.transform);
  24.  
  25.                                 UIPanel mPanel = GetComponent<UIPanel>();
  26.                                
  27.                         Vector3 constraint = mPanel.CalculateConstrainOffset(widgetBounds.min, widgetBounds.max);
  28.                         if (constraint.sqrMagnitude > 0.000001f) {
  29.                                 SpringPanel.Begin(mPanel.gameObject, mTrans.localPosition + constraint, 13f);
  30.                                 lastFollowed = UICamera.selectedObject;
  31.                         }
  32.                 }
  33.         }
  34. }
  35.  
  36.  
Title: Re: UIDraggablePanel and UIButtonKeys navigation...
Post by: Masaaki on December 21, 2012, 09:18:25 PM
Oh my god, THANK YOU. Have been looking for the solution to this for AGES!
In my case, I have two scrolling views (one has a list of multiplayer games running, the other has a list of achievements), and I plan on supporting gamepad input for them. I specifically did NOT want Skyrim-style centering the selected item in view, I wanted to scroll *just* enough to get the selected item in view and no further.
Thanks again.