Author Topic: Reparenting and repositioning programatically.  (Read 4253 times)

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Reparenting and repositioning programatically.
« on: March 28, 2014, 12:48:20 AM »
So for my purposes once the player drags and drops an item from one grid to another, it then checks based on certain specifications whether or not it was a legal move or not...if not it should sent it back to the grid/parent it came from...now i am able to set the parents, but getting them to reposition themselves i am not having much luck here is how I'm trying to do it

  1. NGUITools.MarkParentAsChanged(gameObject);
  2. bCard.transform.GetComponent<WCDragDropItemSelection>().Parent = bCard.previousParent;
  3. bCard.previousParent.GetComponent<UIGrid>().repositionNow = true;
  4.  

WCDragDropItemSelection is a subclass of UIDragDropItem...I have also tried using reposition() and i have looked at UIDragDropItem OnDragDropRelease, and i can't seem to figure out what it is i need to do to move the"illegal" item back to the parent it came from.

Thanks in advance.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Reparenting and repositioning programatically.
« Reply #1 on: March 28, 2014, 01:04:23 AM »
Change the parent first, and only then call MarkParentAsChanged.

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Reparenting and repositioning programatically.
« Reply #2 on: March 28, 2014, 01:10:33 AM »
Thanks for the reply aren, but unfortunately no luck... here is the whole function

  1. private int addCardToPlayerDeck()
  2. {
  3.         int cardsSelectedCount = 0;
  4.                
  5.         foreach( Transform child in transform )
  6.         {
  7.                 BattleCard bCard = child.GetComponent<BattleCard>();
  8.                        
  9.                 bCard.cardController = cardOwner;
  10.                 bCard.cardOwner = cardOwner;
  11.                 child.GetComponent<UISprite>().color = mColor;
  12.                 child.GetComponent<UIDragDropItem>().restriction = UIDragDropItem.Restriction.Horizontal;
  13.                        
  14.                 if( !_selectedBattleCards.Contains(bCard) )
  15.                 {
  16.                         if(onCardSelected(bCard))
  17.                         {
  18.                                 _selectedBattleCards.Add(bCard);
  19.                         }
  20.                         else
  21.                         {
  22.                                 Debug.Log("current: "+bCard.transform.GetComponent<WCDragDropItemSelection>().Parent+" Previous: " + bCard.previousParent);
  23.                                 bCard.transform.GetComponent<WCDragDropItemSelection>().Parent = bCard.previousParent;
  24.                                 NGUITools.MarkParentAsChanged(gameObject);
  25.                                 bCard.previousParent.GetComponent<UIGrid>().Reposition();
  26.                         }
  27.                 }
  28.                 ++cardsSelectedCount;
  29.         }
  30.  
  31.         return cardsSelectedCount;
  32. }
  33.  

I did as you suggested and the drag drop item does not switch parents..or at least not visually anyway.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Reparenting and repositioning programatically.
« Reply #3 on: March 28, 2014, 01:11:53 AM »
What does "bCard.transform.GetComponent<WCDragDropItemSelection>().Parent = bCard.previousParent;" do?

You switch parents by changing transform.parent.

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Reparenting and repositioning programatically.
« Reply #4 on: March 28, 2014, 02:58:09 AM »
apologies went for lunch...

WCDragDropItemSelection exposes the MParent variable via property in UIDragDropItem and i am setting that to previousParent if the movement is not valid. Previous parent which is a public transform inside my class BattleCard which is set whenever it gets to a new legal parent.

so at the moment i am moving the battle card from one UIGrid set as its parent...to another UIGrid, and i do some checks to see if the movement is valid...if it is not i would like to programatically move it back to it's "previousParent". which is the UIGrid it was in before the drag and drop release occurs.

I can provide a better description if necessary.

cheers

-EDIT-
right okay i see what you mean i changed from

  1. bCard.transform.GetComponent<WCDragDropItemSelection>().Parent = bCard.previousParent;
  2. NGUITools.MarkParentAsChanged(gameObject);
  3. bCard.previousParent.GetComponent<UIGrid>().Reposition();
  4.  

to

  1. bCard.transform.parent = bCard.previousParent;
  2. NGUITools.MarkParentAsChanged(gameObject);
  3. bCard.previousParent.GetComponent<UIGrid>().Reposition();
  4.  

And it moves it back to the correct position, which is fantastic...Problem now is its now at the wrong depth... its going back to a rather nested grid like so

Panel->Container->UISprite->UIScrollView->Container(UIDragView)->UIGrid

Rather convoluted i know, but it is achieves the interface and functionality i desired. so it going back to the UIGrid show below but it's behind the UISprite(as fas as i can ascertain visually). not only that but it's position.z goes to -99 and even changing that manually to 0 while the scene is running seems to not do much.
« Last Edit: March 28, 2014, 03:13:14 AM by Genhain »

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Reparenting and repositioning programatically.
« Reply #5 on: March 28, 2014, 04:05:45 AM »
-UPDATE-

finally got it working here is the code if anyone else is interested...

  1. bCard.transform.parent = bCard.previousParent;
  2. Vector3 pos = bCard.transform.localPosition;
  3. pos.z = 0f;
  4. bCard.transform.localPosition = pos;
  5.  
  6. UIGrid mGrid = NGUITools.FindInParents<UIGrid>(bCard.transform.parent);
  7. UITable mTable = NGUITools.FindInParents<UITable>(bCard.transform.parent);
  8.  
  9. NGUITools.MarkParentAsChanged(bCard.gameObject);
  10.  
  11. if (mTable != null) mTable.repositionNow = true;
  12. if (mGrid != null) mGrid.repositionNow = true;
  13.  

I was calling MarkParentAsChanged() on the wrong instance. so my card was not going back to the correct panel...hence why it was on the wrong depth.

@ArenMook Cheers