UIExGridItemCtrlBase GetItem() { UIExGridItemCtrlBase item = null; if (cachedItemList.Count == 0) { item = GameObject.Instantiate <UIExGridItemCtrlBase>(templateItem); item.transform.parent = transform; item.transform.localScale = Vector3.one; } else { item = cachedItemList.First.Value; cachedItemList.RemoveFirst(); } return(item); }
//if the last line is visible, add new line after it. void AddToRight() { if (itemList.Count == 0) { return; } LinkedListNode <UIExGridItemCtrlBase> node = itemList.Last; if (node.Value.index == dataCount - 1) { return; } if (node.Value.left < clipRegionRight) { Vector3 pos = node.Value.transform.localPosition; //this node must be line end pos.x += node.Value.halfWidth; //cur item's half width, and next item's half width pos.y = startPos.y; for (int i = 1; i <= itemPerCol; i++) { UIExGridItemCtrlBase item = GetItem(); item.Init(node.Value.index + i, node.Value.xIndex + 1, i - 1, cellWith, cellHeight, this); item.SetData(userData); item.Cache(); item.SetBoxCollider(); if (i == 1) { pos.x += item.halfWidth; //cur item's half width, and next item's half width } item.transform.localPosition = pos; pos.y -= cellHeight; itemList.AddLast(item); //Debug.Log("add to right " + cachedItemList.Count); if (item.index == dataCount - 1) { break; } } } }
//if the last line is visible, add new line after it. void AddToBottom() { if (itemList.Count == 0) { return; } LinkedListNode <UIExGridItemCtrlBase> node = itemList.Last; if (node.Value.index == dataCount - 1) { return; } if (node.Value.top > clipRegionBottom) { Vector3 pos = node.Value.transform.localPosition; //this node must be line end pos.x = startPos.x; pos.y -= node.Value.halfHeight; //cur item's half height, and next item's half height for (int i = 1; i <= itemPerRow; i++) { UIExGridItemCtrlBase item = GetItem(); item.Init(node.Value.index + i, i - 1, node.Value.yIndex + 1, cellWith, cellHeight, this); item.SetData(userData); item.Cache(); item.SetBoxCollider(); if (i == 1) { pos.y -= item.halfHeight; //cur item's half height, and next item's half height } item.transform.localPosition = pos; pos.x += cellWith; itemList.AddLast(item); //Debug.Log("add to bottom " + cachedItemList.Count); if (item.index == dataCount - 1) { break; } } } }
//if the first line is visible, then add new line after it. void AddToLeft() { if (itemList.Count == 0) { return; } LinkedListNode <UIExGridItemCtrlBase> node = itemList.First; if (node.Value.index == 0) { return; } if (node.Value.right > clipRegionLeft) { Vector3 pos = node.Value.transform.localPosition; //this node must be line begin pos.x -= node.Value.halfWidth; //cur item's half width, and next item's half width pos.y -= cellHeight * (itemPerCol - 1); for (int i = 0; i < itemPerCol; i++) { UIExGridItemCtrlBase item = GetItem(); item.Init(node.Value.index - i - 1, node.Value.xIndex - 1, itemPerCol - i - 1, cellWith, cellHeight, this); item.SetData(userData); item.Cache(); item.SetBoxCollider(); if (i == 0) { pos.x -= item.halfWidth; } item.transform.localPosition = pos; pos.y += cellHeight; itemList.AddFirst(item); //Debug.Log("add to left " + cachedItemList.Count); } } }
//if the first line is visible, then add new line after it. void AddToTop() { if (itemList.Count == 0) { return; } LinkedListNode <UIExGridItemCtrlBase> node = itemList.First; if (node.Value.index == 0) { return; } if (node.Value.bottom < clipRegionTop) { Vector3 pos = node.Value.transform.localPosition; //this node must be line begin pos.x += cellWith * (itemPerRow - 1); pos.y += node.Value.halfHeight; //cur item's half height, and next item's half height for (int i = 0; i < itemPerRow; i++) { UIExGridItemCtrlBase item = GetItem(); item.Init(node.Value.index - i - 1, itemPerRow - i - 1, node.Value.yIndex - 1, cellWith, cellHeight, this); item.SetData(userData); item.Cache(); item.SetBoxCollider(); if (i == 0) { pos.y += item.halfHeight; } item.transform.localPosition = pos; pos.x -= cellWith; itemList.AddFirst(item); //Debug.Log("add to top " + cachedItemList.Count); } } }
public void SetGrid_Horz() { if (isVariableWidth && itemPerCol != 1) { itemPerCol = 1; Debug.LogError("itemPerCol must be 1, if it is variable width"); } int xIndex = 0; int yIndex = 0; Vector3 pos = Vector3.zero; bool isChangeLine = false; for (int i = 0; i < dataCount; i++) { UIExGridItemCtrlBase item = GetItem(); item.Init(i, xIndex, yIndex, cellWith, cellHeight, this); item.SetData(userData); item.Cache(); item.SetBoxCollider(); //calculate start pos here, because in variable with mode, the first item's width is confirmed just now if (i == 0) { if (pivotHorz == EmPivotHorz.LeftCenter) { startPos.x = clipRegionLeft + item.halfWidth; startPos.y = (itemPerCol - 1) * item.halfHeight; //it is: itemPerCol * cellHeigth*0.5f - cellHeight * 0.5f; } else if (pivotHorz == EmPivotHorz.TopLeft) { startPos.x = clipRegionLeft + item.halfWidth; startPos.y = clipRegionTop - item.halfHeight; } pos = startPos; } if (isChangeLine) { pos.x += item.halfWidth; //cur item's half width, and next item's half width isChangeLine = false; } item.transform.localPosition = pos; itemList.AddLast(item); yIndex++; //下一个item的位置 if (yIndex < itemPerCol) { pos.y -= item.height; } else { pos.x += item.halfWidth; //cur item's half width, and next item's half width pos.y = startPos.y; xIndex++; yIndex = 0; isChangeLine = true; //if the last item's right is out of clip region, stop show item if (item.right > clipRegionRight) { break; } } } isFit = false; if (itemList.Count > 0) { UIExGridItemCtrlBase item = itemList.Last.Value; isFit = item.right <= clipRegionRight && itemList.Count == dataCount; } }
void SetGrid_Vert() { if (isVariableHeight && itemPerRow != 1) { itemPerRow = 1; Debug.LogError("itemPerRow must be 1, if it is variable height"); } int xIndex = 0; int yIndex = 0; Vector3 pos = Vector3.zero; bool isChangeLine = false; for (int i = 0; i < dataCount; i++) { UIExGridItemCtrlBase item = GetItem(); item.Init(i, xIndex, yIndex, cellWith, cellHeight, this); item.SetData(userData); item.Cache(); item.SetBoxCollider(); //calculate start pos here, because in variable height mode, the first item's height is confirmed now if (i == 0) { if (pivotVert == EmPivotVert.TopCenter) { startPos.x = (1 - itemPerRow) * item.halfWidth; //it is: -itemPerRow * cellWith*0.5f + cellWith * 0.5f; startPos.y = clipRegionTop - item.halfHeight; } else if (pivotVert == EmPivotVert.TopLeft) { startPos.x = clipRegionLeft + item.halfWidth; startPos.y = clipRegionTop - item.halfHeight; } pos = startPos; } if (isChangeLine) { pos.y -= item.halfHeight; //cur item's half height, and next item's half height isChangeLine = false; } item.transform.localPosition = pos; itemList.AddLast(item); xIndex++; //下一个item的位置 if (xIndex < itemPerRow) { pos.x += item.width; } else { pos.x = startPos.x; pos.y -= item.halfHeight; //cur item's half height, and next item's half height xIndex = 0; yIndex++; isChangeLine = true; //if the last item's bottom is out of clip region, stop show item if (item.bottom < clipRegionBottom) { break; } } } isFit = false; if (itemList.Count > 0) { UIExGridItemCtrlBase item = itemList.Last.Value; isFit = item.bottom >= clipRegionBottom && itemList.Count == dataCount; } }