using Amphora; using Annulus; using Azimuth; using Glass; using System; namespace Prism.Glass { /// /// Two number fields that set where the selected point of a function is, and follow it. They reach values beyond /// what a plot can show, up to the bounds of their grids. /// public class FunctionPointFields : Panel { /// /// The values the X field holds by default. X is 0 through 1. /// public static readonly NumberGrid DefaultXGrid = new NumberGrid { Min = 0, Max = 1, Resolution = 0.00001m, Step = 0.01m, FineStep = 0.001m, CoarseStep = 0.1m, }; /// /// The values the Y field holds by default. /// public static readonly NumberGrid DefaultYGrid = new NumberGrid { Min = -1000, Max = 1000, Resolution = 0.00001m, Step = 0.01m, FineStep = 0.001m, CoarseStep = 0.1m, }; // Setting a field from the model raises its change event, which mustn't move the point again. private bool refreshing; private FunctionPointFieldsMaterial fieldsMaterial; /// /// Raised with why, when the model refuses a value typed or stepped into a field. The field is put back first. /// public event Action OnRefused; /// /// Gets the function being edited. /// public FunctionModel Model { get; } /// /// Gets the field for where the selected point is along the function. /// public NumberField XField { get; } /// /// Gets the field for the selected point's value. /// public NumberField YField { get; } /// /// Gets the letter before the X field. Call after changing a label's text. /// public Label XLabel { get; } = new Label { Text = "X" }; /// /// Gets the letter before the Y field. /// public Label YLabel { get; } = new Label { Text = "Y" }; /// /// Makes fields for the selected point of a function. They have no look until they are given a . /// /// The values X can be typed or stepped to, or null for . /// The values Y can be typed or stepped to, or null for . public FunctionPointFields( FunctionModel model, NumberFieldBehavior fieldBehavior, NumberGrid? xGrid = null, NumberGrid? yGrid = null) { Model = model ?? throw new ArgumentNullException(nameof(model)); XField = new NumberField(fieldBehavior, new NumberField.Data { Grid = xGrid ?? DefaultXGrid }); YField = new NumberField(fieldBehavior, new NumberField.Data { Grid = yGrid ?? DefaultYGrid }); CanvasChildren.Add(XLabel); CanvasChildren.Add(XField); CanvasChildren.Add(YLabel); CanvasChildren.Add(YField); model.Changed += _ => Refresh(); model.SelectionChanged += _ => Refresh(); // Written out per field so a change doesn't allocate a closure. XField.OnValueChanged += (_, _, now) => { if (!refreshing) { MoveSelected((double)now, Model.GetY(Model.SelectedIndex)); } }; YField.OnValueChanged += (_, _, now) => { if (!refreshing) { MoveSelected(Model.GetX(Model.SelectedIndex), (double)now); } }; ApplyMaterials(); Layout(); Refresh(); } // A field's range can be narrower than what the model holds, and a number outside a decimal's range can't be // converted, so the value is held to the range first. private static decimal Shown(Scalar value, NumberGrid grid) { double held = Math.Min(Math.Max(value.Value, (double)grid.Min), (double)grid.Max); return (decimal)held; } // A refusal raises no change, so nothing else would put the field back. private void MoveSelected(Scalar x, Scalar y) { StatusOr moved = Model.MovePoint(Model.SelectedIndex, x, y); if (!moved.IsOk) { Refresh(); OnRefused?.Invoke(moved.Status); } } private void Refresh() { refreshing = true; XField.Value = Shown(Model.GetX(Model.SelectedIndex), XField.Grid); YField.Value = Shown(Model.GetY(Model.SelectedIndex), YField.Grid); refreshing = false; } private Scalar LabelWidth(Label label) { return new TextMaterial { Text = label.Text, Style = fieldsMaterial.FieldLabel } .PreferredSize(Constraints.Unbounded).X; } private void ApplyMaterials() { Material = fieldsMaterial.Background; foreach (NumberField field in new[] { XField, YField }) { field.Visible = fieldsMaterial.Field.HasValue; if (fieldsMaterial.Field.HasValue) { field.NumberFieldMaterial = fieldsMaterial.Field.Value; } } foreach (Label label in new[] { XLabel, YLabel }) { label.Visible = fieldsMaterial.Field.HasValue && fieldsMaterial.FieldLabel != null; if (label.Visible) { label.Style = fieldsMaterial.FieldLabel; } } } /// /// Gets or sets the look of the fields, which also sizes the panel to them. /// public FunctionPointFieldsMaterial FieldsMaterial { get => fieldsMaterial; set { fieldsMaterial = value; ApplyMaterials(); Layout(); } } /// /// Sizes the panel to its fields and puts them in place. Setting does it too. /// public void Layout() { Scalar spacing = fieldsMaterial.Spacing; Scalar cellWidth = 0; Scalar height = 0; if (XField.Visible) { foreach (var (label, field) in new[] { (XLabel, XField), (YLabel, YField) }) { var cell = new RectAggregator(new Vector(spacing, 0)); if (label.Visible) { cell.NewCol(LabelWidth(label)); height = Scalar.Max(height, fieldsMaterial.FieldLabel.SingleLineHeight); } Vector fieldSize = field.PreferredBoundsSize; cell.NewCol(fieldSize.X); cellWidth = Scalar.Max(cellWidth, cell.Rect.Width); height = Scalar.Max(height, fieldSize.Y); } } var across = new RectAggregator(new Vector(spacing, 0)); if (XField.Visible) { across.NewCols(cellWidth, 2); } var inside = new Vector(across.Rect.Width, height); Vector boundsSize = inside; if (Material != null) { boundsSize = Material.BoundsSizeFor(inside); } Bounds = Bounds.WithSize(boundsSize); if (XField.Visible) { const Shortfall tooSmall = Shortfall.UseWhatIsLeft; Rect remainder = CanvasRect; RectDivider cells = remainder.NewCols(2, spacing); Place(XLabel, XField, cells[0], tooSmall); Place(YLabel, YField, cells[1], tooSmall); } } private void Place(Label label, NumberField field, Rect cell, Shortfall tooSmall) { if (label.Visible) { label.Bounds = cell.NewCol( LabelWidth(label), HorizontalJustification.Left, fieldsMaterial.Spacing, tooSmall); } field.Bounds = cell; } } }