public void init(myUGUIObject background, myUGUIObject foreground, myUGUIObject thumb = null, SLIDER_MODE mode = SLIDER_MODE.FILL) { mName = "UGUISlider"; mMode = mode; mBackground = background; mForeground = foreground; mThumb = thumb; if (mThumb != null && mThumb.getParent() != mForeground) { logError("Foreground must be parent of Thumb"); return; } if (mMode == SLIDER_MODE.SIZING) { mOriginForegroundSize = mForeground.getWindowSize(); mOriginForegroundPosition = mForeground.getPosition(); if (mBackground == null) { logError("Background can not be null while slider mode is SIZING"); return; } if (mForeground.getParent() != mBackground) { logError("Background must be parent of Foreground"); return; } } if (mBackground != null) { mBackground.setOnMouseDown(onMouseDown); mBackground.setOnScreenMouseUp(onScreenMouseUp); mBackground.setOnMouseMove(onMouseMove); if (mBackground.getCollider() != null) { mScript.registeCollider(mBackground); } } }
public void recalculateDragView <T>(List <T> itemList, Vector2 itemSize, float space, myUGUIObject dragViewParent, Vector2 viewportSize, int countPerLine = 0, float lineSpace = 0.0f, bool horiFirst = false) where T : IDragViewItem { // 计算拖拽窗口大小 int count = itemList.Count; int widthCount = 0; int heightCount = 0; DRAG_DIRECTION dragDirection = mDragViewComponent.getDragDirection(); if (dragDirection == DRAG_DIRECTION.HORIZONTAL || dragDirection == DRAG_DIRECTION.FREE) { widthCount = countPerLine; if (widthCount > 0) { heightCount = ceil((float)count / widthCount); } else { heightCount = 1; widthCount = count; } } else if (dragDirection == DRAG_DIRECTION.VERTICAL || dragDirection == DRAG_DIRECTION.FREE) { heightCount = countPerLine; if (heightCount > 0) { widthCount = ceil((float)count / heightCount); } else { widthCount = 1; heightCount = count; } } Vector2 windowSize = getWindowSize(); if (dragDirection == DRAG_DIRECTION.HORIZONTAL || dragDirection == DRAG_DIRECTION.FREE) { windowSize.x = itemSize.x * widthCount + space * (widthCount - 1); windowSize.y = itemSize.y * heightCount + lineSpace * (heightCount - 1); } else if (dragDirection == DRAG_DIRECTION.VERTICAL || dragDirection == DRAG_DIRECTION.FREE) { windowSize.x = itemSize.x * widthCount + lineSpace * (widthCount - 1); windowSize.y = itemSize.y * heightCount + space * (heightCount - 1); } clampMin(ref windowSize.x); clampMin(ref windowSize.y); setWindowSize(windowSize); // 设置所有子节点的位置 for (int i = 0; i < count; ++i) { Vector3 itemPos = itemList[i].getPosition(); if (dragDirection == DRAG_DIRECTION.HORIZONTAL || dragDirection == DRAG_DIRECTION.FREE) { // 排成多排 if (countPerLine > 0) { int horiIndex = horiFirst ? i % widthCount : i / heightCount; int vertIndex = horiFirst ? i / widthCount : i % heightCount; itemPos.x = (itemSize.x + space) * horiIndex + itemSize.x * 0.5f - windowSize.x * 0.5f; itemPos.y = (itemSize.y + lineSpace) * (heightCount - vertIndex - 1) + itemSize.y * 0.5f - windowSize.y * 0.5f; } // 只排一排 else { itemPos.x = (itemSize.x + space) * i + itemSize.x * 0.5f - windowSize.x * 0.5f; } } if (dragDirection == DRAG_DIRECTION.VERTICAL || dragDirection == DRAG_DIRECTION.FREE) { // 排成多列 if (countPerLine > 0) { int horiIndex = horiFirst ? i % widthCount : i / heightCount; int vertIndex = horiFirst ? i / widthCount : i % heightCount; itemPos.x = (itemSize.x + lineSpace) * (horiIndex - widthCount - 1) + itemSize.x * 0.5f - windowSize.x * 0.5f; itemPos.y = (itemSize.y + space) * vertIndex + itemSize.y * 0.5f - windowSize.y * 0.5f; } // 排成一列 else { itemPos.y = (itemSize.y + space) * (count - i - 1) + itemSize.y * 0.5f - windowSize.y * 0.5f; } } itemList[i].setPosition(itemPos); } // 重新计算拖拽窗口父节点的大小和位置,用于限定拖拽窗口的拖拽范围 Vector3 parentPos = dragViewParent.getPosition(); Vector2 parentSize = Vector2.zero; autoAdjustParent(ref parentPos, ref parentSize, viewportSize); dragViewParent.setWindowSize(parentSize); FT.MOVE(dragViewParent, parentPos); autoResetPosition(); }