public static void RandByCurveTests() { DebugHistogram debugHistogram = new DebugHistogram((from x in Enumerable.Range(0, 30) select(float) x).ToArray()); SimpleCurve simpleCurve = new SimpleCurve(); simpleCurve.Add(new CurvePoint(0f, 0f)); simpleCurve.Add(new CurvePoint(10f, 1f)); simpleCurve.Add(new CurvePoint(15f, 2f)); simpleCurve.Add(new CurvePoint(20f, 2f)); simpleCurve.Add(new CurvePoint(21f, 0.5f)); simpleCurve.Add(new CurvePoint(30f, 0f)); SimpleCurve curve = simpleCurve; float num = 0f; for (int i = 0; i < 1000000; i++) { float num2 = Rand.ByCurve(curve); num += num2; debugHistogram.Add(num2); } debugHistogram.Display(); Log.Message($"Average {num / 1000000f}, calculated as {Rand.ByCurveAverage(curve)}"); }
private void DoCurveEditor(Rect screenRect) { Widgets.DrawMenuSection(screenRect); SimpleCurveDrawer.DrawCurve(screenRect, curve); Vector2 mousePosition = Event.current.mousePosition; if (Mouse.IsOver(screenRect)) { Rect rect = new Rect(mousePosition.x + 8f, mousePosition.y + 18f, 100f, 100f); Vector2 v = SimpleCurveDrawer.ScreenToCurveCoords(screenRect, curve.View.rect, mousePosition); Widgets.Label(rect, v.ToStringTwoDigits()); } Rect rect2 = new Rect(0f, 0f, 50f, 24f); rect2.x = screenRect.x; rect2.y = screenRect.y + screenRect.height / 2f - 12f; if (float.TryParse(Widgets.TextField(rect2, curve.View.rect.x.ToString()), out float result)) { curve.View.rect.x = result; } rect2.x = screenRect.xMax - rect2.width; rect2.y = screenRect.y + screenRect.height / 2f - 12f; if (float.TryParse(Widgets.TextField(rect2, curve.View.rect.xMax.ToString()), out result)) { curve.View.rect.xMax = result; } rect2.x = screenRect.x + screenRect.width / 2f - rect2.width / 2f; rect2.y = screenRect.yMax - rect2.height; if (float.TryParse(Widgets.TextField(rect2, curve.View.rect.y.ToString()), out result)) { curve.View.rect.y = result; } rect2.x = screenRect.x + screenRect.width / 2f - rect2.width / 2f; rect2.y = screenRect.y; if (float.TryParse(Widgets.TextField(rect2, curve.View.rect.yMax.ToString()), out result)) { curve.View.rect.yMax = result; } if (Mouse.IsOver(screenRect)) { if (Event.current.type == EventType.ScrollWheel) { float num = -1f * Event.current.delta.y * 0.025f; float num2 = curve.View.rect.center.x - curve.View.rect.x; float num3 = curve.View.rect.center.y - curve.View.rect.y; curve.View.rect.xMin += num2 * num; curve.View.rect.xMax -= num2 * num; curve.View.rect.yMin += num3 * num; curve.View.rect.yMax -= num3 * num; Event.current.Use(); } if (Event.current.type == EventType.MouseDown && (Event.current.button == 0 || Event.current.button == 2)) { List <int> list = PointsNearMouse(screenRect).ToList(); if (list.Any()) { draggingPointIndex = list[0]; } else { draggingPointIndex = -1; } if (draggingPointIndex < 0) { draggingButton = Event.current.button; } Event.current.Use(); } if (Event.current.type == EventType.MouseDown && Event.current.button == 1) { Vector2 mouseCurveCoords = SimpleCurveDrawer.ScreenToCurveCoords(screenRect, curve.View.rect, Event.current.mousePosition); List <FloatMenuOption> list2 = new List <FloatMenuOption>(); list2.Add(new FloatMenuOption("Add point at " + mouseCurveCoords.ToString(), delegate { curve.Add(new CurvePoint(mouseCurveCoords)); })); foreach (int item in PointsNearMouse(screenRect)) { CurvePoint point = curve[item]; list2.Add(new FloatMenuOption("Remove point at " + point.ToString(), delegate { curve.RemovePointNear(point); })); } Find.WindowStack.Add(new FloatMenu(list2)); Event.current.Use(); } } if (draggingPointIndex >= 0) { curve[draggingPointIndex] = new CurvePoint(SimpleCurveDrawer.ScreenToCurveCoords(screenRect, curve.View.rect, Event.current.mousePosition)); curve.SortPoints(); if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { draggingPointIndex = -1; Event.current.Use(); } } if (DraggingView) { if (Event.current.type == EventType.MouseDrag) { Vector2 delta = Event.current.delta; curve.View.rect.x -= delta.x * curve.View.rect.width * 0.002f; curve.View.rect.y += delta.y * curve.View.rect.height * 0.002f; Event.current.Use(); } if (Event.current.type == EventType.MouseUp && Event.current.button == draggingButton) { draggingButton = -1; } } }