public void Refresh() { ReportSet(); // we use local position as the true value. Ratio position follows it, so it should be updated when refresh for (int c = 0; c < refCurves.Length; c++) { CUIBezierCurve curve = refCurves[c]; if (curve.ControlPoints != null) { var controlPoints = curve.ControlPoints; for (var p = 0; p < CUIBezierCurve.CubicBezierCurvePtNum; p++) { Vector3 ratioPoint = controlPoints[p]; ratioPoint.x = (ratioPoint.x + rectTrans.rect.width * rectTrans.pivot.x) / rectTrans.rect.width; ratioPoint.y = (ratioPoint.y + rectTrans.rect.height * rectTrans.pivot.y) / rectTrans.rect.height; refCurvesControlRatioPoints[c][p] = ratioPoint; } } } //uiText.SetAllDirty(); // need this to refresh the UI text, SetAllDirty does not seem to work for all cases if (uiGraphic != null) { uiGraphic.enabled = false; uiGraphic.enabled = true; } }
protected virtual void OnSceneGUI() { CUIGraphic script = (CUIGraphic)target; script.ReportSet(); for (int c = 0; c < script.RefCurves.Length; c++) { CUIBezierCurve curve = script.RefCurves[c]; if (curve.ControlPoints != null) { Vector2[] controlPoints = curve.ControlPoints; Transform handleTransform = script.transform; Quaternion handleRotation = handleTransform.rotation; for (int p = 0; p < CUIBezierCurve.CubicBezierCurvePtNum; p++) { EditorGUI.BeginChangeCheck(); Handles.Label(handleTransform.TransformPoint(controlPoints[p]), string.Format("Control Point {0}", p + 1)); Vector3 newPt = Handles.DoPositionHandle(handleTransform.TransformPoint(controlPoints[p]), handleRotation); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(script, "Move Point"); EditorUtility.SetDirty(script); controlPoints[p] = handleTransform.InverseTransformPoint(newPt); } } Handles.color = Color.gray; Handles.DrawLine(handleTransform.TransformPoint(controlPoints[0]), handleTransform.TransformPoint(controlPoints[1])); //Handles.DrawLine(handleTransform.TransformPoint(controlPoints[1]), handleTransform.TransformPoint(controlPoints[2])); int sampleSize = 10; Handles.color = Color.white; for (int s = 0; s < sampleSize; s++) { Handles.DrawLine(handleTransform.TransformPoint(curve.GetPoint((float)s / sampleSize)), handleTransform.TransformPoint(curve.GetPoint((float)(s + 1) / sampleSize))); } } } /*if (script.RefCurves != null) * { * Handles.DrawLine(script.RefCurves[0].transform.TransformPoint(script.RefCurves[0].ControlPoints[0]), script.RefCurves[1].transform.TransformPoint(script.RefCurves[1].ControlPoints[0])); * Handles.DrawLine(script.RefCurves[0].transform.TransformPoint(script.RefCurves[0].ControlPoints[3]), script.RefCurves[1].transform.TransformPoint(script.RefCurves[1].ControlPoints[3])); * }*/ script.Refresh(); }
public void ReferenceCUIForBCurves() { // compute the position ratio of this rect transform in perspective of reference rect transform Vector2 localPosition = rectTrans.localPosition; Vector3 posDeltaBetweenBottomLeftCorner = localPosition;// Difference between pivot posDeltaBetweenBottomLeftCorner.x += -rectTrans.rect.width * rectTrans.pivot.x + (refCUIGraphic.rectTrans.rect.width * refCUIGraphic.rectTrans.pivot.x); posDeltaBetweenBottomLeftCorner.y += -rectTrans.rect.height * rectTrans.pivot.y + (refCUIGraphic.rectTrans.rect.height * refCUIGraphic.rectTrans.pivot.y); //posDeltaBetweenBottomLeftCorner.z = rectTrans.localPosition.z; Vector3 bottomLeftPosRatio = new Vector3(posDeltaBetweenBottomLeftCorner.x / refCUIGraphic.RectTrans.rect.width, posDeltaBetweenBottomLeftCorner.y / refCUIGraphic.RectTrans.rect.height, posDeltaBetweenBottomLeftCorner.z); Vector3 topRightPosRatio = new Vector3((posDeltaBetweenBottomLeftCorner.x + rectTrans.rect.width) / refCUIGraphic.RectTrans.rect.width, (posDeltaBetweenBottomLeftCorner.y + rectTrans.rect.height) / refCUIGraphic.RectTrans.rect.height, posDeltaBetweenBottomLeftCorner.z); refCurves[0].ControlPoints[0] = refCUIGraphic.GetBCurveSandwichSpacePoint(bottomLeftPosRatio.x, bottomLeftPosRatio.y) - localPosition; refCurves[0].ControlPoints[3] = refCUIGraphic.GetBCurveSandwichSpacePoint(topRightPosRatio.x, bottomLeftPosRatio.y) - localPosition; refCurves[1].ControlPoints[0] = refCUIGraphic.GetBCurveSandwichSpacePoint(bottomLeftPosRatio.x, topRightPosRatio.y) - localPosition; refCurves[1].ControlPoints[3] = refCUIGraphic.GetBCurveSandwichSpacePoint(topRightPosRatio.x, topRightPosRatio.y) - localPosition; // use two sample points from the reference curves to find the second and third controls points for this curves for (int c = 0; c < refCurves.Length; c++) { CUIBezierCurve curve = refCurves[c]; float yTime = c == 0 ? bottomLeftPosRatio.y : topRightPosRatio.y; Vector3 leftPoint = refCUIGraphic.GetBCurveSandwichSpacePoint(bottomLeftPosRatio.x, yTime); Vector3 rightPoint = refCUIGraphic.GetBCurveSandwichSpacePoint(topRightPosRatio.x, yTime); float quarter = 0.25f, threeQuarter = 0.75f; Vector3 quarterPoint = refCUIGraphic.GetBCurveSandwichSpacePoint((bottomLeftPosRatio.x * 0.75f + topRightPosRatio.x * 0.25f) / 1.0f, yTime); Vector3 threeQuaterPoint = refCUIGraphic.GetBCurveSandwichSpacePoint((bottomLeftPosRatio.x * 0.25f + topRightPosRatio.x * 0.75f) / 1.0f, yTime); float x_1 = 3 * threeQuarter * threeQuarter * quarter, // (1 - t)(1 - t)t y_1 = 3 * threeQuarter * quarter * quarter, x_2 = 3 * quarter * quarter * threeQuarter, y_2 = 3 * quarter * threeQuarter * threeQuarter; Vector3 contant_1 = quarterPoint - Mathf.Pow(threeQuarter, 3) * leftPoint - Mathf.Pow(quarter, 3) * rightPoint, contant_2 = threeQuaterPoint - Mathf.Pow(quarter, 3) * leftPoint - Mathf.Pow(threeQuarter, 3) * rightPoint, p1, p2; solveDoubleEquationWithVector(x_1, y_1, x_2, y_2, contant_1, contant_2, out p1, out p2); curve.ControlPoints[1] = p1 - rectTrans.localPosition; curve.ControlPoints[2] = p2 - rectTrans.localPosition; } // use tangent and start and end time to derive control point 2 and 3 }
public virtual void ReportSet() { if (rectTrans == null) { rectTrans = GetComponent <RectTransform>(); } if (refCurves == null) { refCurves = new CUIBezierCurve[4]; } bool isCurvesReady = true; for (int c = 0; c < 4; c++) { isCurvesReady = isCurvesReady & refCurves[c] != null; } isCurvesReady = isCurvesReady & refCurves.Length == 4; if (!isCurvesReady) { CUIBezierCurve[] curves = refCurves; for (int c = 0; c < 4; c++) { if (refCurves[c] == null) { curves[c] = new CUIBezierCurve(); } else { curves[c] = refCurves[c]; } } refCurves = curves; } if (refCurvesControlRatioPoints == null) { refCurvesControlRatioPoints = new Vector3_Array2D[refCurves.Length]; for (int c = 0; c < refCurves.Length; c++) { refCurvesControlRatioPoints[c].array = new Vector2[refCurves[c].ControlPoints.Length]; } FixTextToRectTrans(); } }
public void UpdateCurveControlPointPositions() { ReportSet(); for (int c = 0; c < refCurves.Length; c++) { CUIBezierCurve curve = refCurves[c]; for (int p = 0; p < refCurves[c].ControlPoints.Length; p++) { Vector3 newPt = refCurvesControlRatioPoints[c][p]; newPt.x = newPt.x * rectTrans.rect.width - rectTrans.rect.width * rectTrans.pivot.x; newPt.y = newPt.y * rectTrans.rect.height - rectTrans.rect.height * rectTrans.pivot.y; curve.ControlPoints[p] = newPt; } } }