using System; using Annulus; using Azimuth; using Clave; using Glass; using Material = Glass.Material; namespace Konis.Testbed { /// /// A floating window that edits a set of clip slabs: for each one a direction, which needn't be of length one, and /// the distances along it where the slab starts and ends. Either end can be left open. Every part is built once, /// and opening it on another set reloads them. /// public class SlabWindow { private const double ColumnWidth = 84; private const double ColumnGap = 4; private const double RowGap = 4; private const int Columns = 4; private const string RemoveText = "x"; private const string AddText = "+"; private const int ProblemLines = 2; private static readonly NumberGrid DirectionGrid = new NumberGrid { Min = -1000, Max = 1000, Resolution = 0.00001m, Step = 0.1m, FineStep = 0.01m, CoarseStep = 1, }; private static readonly NumberGrid OffsetGrid = new NumberGrid { Min = -10000, Max = 10000, Resolution = 0.0001m, Step = 1, FineStep = 0.01m, CoarseStep = 10, }; private class SlabRow { public Label Title; public Button Remove; public Label DirectionLabel; public NumberField X; public NumberField Y; public NumberField Z; public Checkbox HasMin; public NumberField Min; public Checkbox HasMax; public NumberField Max; public Control[] Parts; } private readonly TestbedSkin skin; private readonly NumberFieldBehavior fieldBehavior = new NumberFieldBehavior { AllowsTyping = true }; private readonly SlabRow[] rows = new SlabRow[Slab.MaxPerSet]; private readonly Button addButton; // Where the add button sits for each count of slabs, which is the place the next slab's title would be. private readonly Rect[] addSlots = new Rect[Slab.MaxPerSet]; private readonly Label problemLabel; private Action changed; private int count; // Set while slabs are being put into the fields, so that doing so isn't taken for an edit. private bool loading; /// /// Builds the window, hidden. /// public SlabWindow(TestbedSkin skin) { this.skin = skin ?? throw new ArgumentNullException(nameof(skin)); Scalar lineHeight = skin.Text.SingleLineHeight; Vector removeSize = skin.GlyphButtonSizeFor(RemoveText); Vector addSize = skin.GlyphButtonSizeFor(AddText); // The two glyph buttons are one size, so the pair looks like a matched set. Vector glyphSize = new Vector(Scalar.Max(removeSize.X, addSize.X), Scalar.Max(removeSize.Y, addSize.Y)); Scalar buttonHeight = glyphSize.Y; Scalar fieldHeight = skin.NumberField.BoundsSizeFor(new Vector(0, skin.ButtonText.SingleLineHeight)).Y; Frame = new Window { DebugName = "slabs", TitleStyle = skin.HeadingText, WindowMaterial = skin.Window, Resizable = false, Visible = false, }; for (int i = 0; i < rows.Length; i++) { rows[i] = BuildRow(i); } addButton = BuildGlyphButton("add slab button"); problemLabel = new Label(skin.ProblemText, "", Constraints.Unbounded) { DebugName = "slab problem" }; var note = new Label(skin.RespawnHeadingText, "amber: takes effect on respawn", Constraints.Unbounded); // Measure: the smallest the window can be. var columns = new RectAggregator(new Vector(ColumnGap, 0)); columns.NewCols(ColumnWidth, Columns); var stack = new RectAggregator(new Vector(0, RowGap)); stack.NewRow(lineHeight); for (int i = 0; i < rows.Length; i++) { stack.NewRow(buttonHeight); stack.NewRow(fieldHeight); stack.NewRow(fieldHeight); } stack.NewRow(ProblemLines * lineHeight); var content = new Vector(columns.Rect.Width, stack.Rect.Height); // Arrange: the same parts, carved out of the space there is. Rect remainder = Rect.FromPositionAndSize(Vector.Zero, content); const Shortfall tooSmall = Shortfall.UseWhatIsLeft; Place(note, remainder.NewRow(lineHeight, VerticalJustification.Top, RowGap, tooSmall)); for (int i = 0; i < rows.Length; i++) { SlabRow row = rows[i]; Rect titleRow = remainder.NewRow(buttonHeight, VerticalJustification.Top, RowGap, tooSmall); RectDivider titleCells = titleRow.NewCols(Columns, ColumnGap); Rect titleCell = titleCells[0]; Rect removeCell = titleCells[Columns - 1]; addSlots[i] = titleCell.NewCol(glyphSize.X, HorizontalJustification.Left, 0, tooSmall); Place(row.Title, titleCells[0]); Place(row.Remove, removeCell.NewCol(glyphSize.X, HorizontalJustification.Right, 0, tooSmall)); Rect directionRow = remainder.NewRow(fieldHeight, VerticalJustification.Top, RowGap, tooSmall); RectDivider directionCells = directionRow.NewCols(Columns, ColumnGap); Place(row.DirectionLabel, directionCells[0]); Place(row.X, directionCells[1]); Place(row.Y, directionCells[2]); Place(row.Z, directionCells[3]); Rect offsetRow = remainder.NewRow(fieldHeight, VerticalJustification.Top, RowGap, tooSmall); RectDivider offsetCells = offsetRow.NewCols(Columns, ColumnGap); Place(row.HasMin, offsetCells[0]); Place(row.Min, offsetCells[1]); Place(row.HasMax, offsetCells[2]); Place(row.Max, offsetCells[3]); } Place(addButton, addSlots[0]); Rect problemRow = remainder.NewRow(ProblemLines * lineHeight, VerticalJustification.Top, RowGap, tooSmall); Place(problemLabel, problemRow); Frame.CanvasChildren.Add(note); foreach (SlabRow row in rows) { Caption(row.Remove, RemoveText); Caption(row.HasMin, "from"); Caption(row.HasMax, "to"); foreach (Control part in row.Parts) { Frame.CanvasChildren.Add(part); } } Caption(addButton, AddText); addButton.Tooltip = skin.TooltipFor("Add a slab"); addButton.OnActivate += (ButtonBase _, in KeyboardSnapshot __, KeyboardManager ___) => Add(); Frame.CanvasChildren.Add(addButton); Frame.CanvasChildren.Add(problemLabel); Frame.MinContentSize = content; Frame.Bounds = Rect.FromPositionAndSize( Vector.Zero, skin.Window.BoundsSizeFor(content, Frame.TitleBarHeight)); Frame.CloseButton.CanvasChildren.Add( new Label(skin.ButtonText, "x", Constraints.Tight(Frame.CloseButton.CanvasRect.Size))); ShowRows(); } /// /// Gets the window, to add to the world. /// public Window Frame { get; } /// /// Gets whether the window is showing. /// public bool IsOpen => Frame.Visible; /// /// Gets how many slabs the window holds. /// public int SlabCount => count; /// /// Gets what the window says is wrong with the slabs, or an empty string when nothing is. /// public string Problem => problemLabel.Text.ToString(); private static void Place(Control child, Rect slot) { child.Bounds = slot; } // A caption is as big as the control it sits on, so it can only be made once the control has its place. private void Caption(Control control, string text) { control.CanvasChildren.Add(new Label(skin.ButtonText, text, Constraints.Tight(control.CanvasRect.Size))); } private static decimal Shown(double value, NumberGrid grid) { return (decimal)Math.Min(Math.Max(value, (double)grid.Min), (double)grid.Max); } private NumberField BuildField(NumberGrid grid, string name) { var returnMe = new NumberField(fieldBehavior, new NumberField.Data { Grid = grid }) { NumberFieldMaterial = skin.NumberField, DebugName = name, }; returnMe.OnValueChanged += (_, __, ___) => Changed(); return returnMe; } private Button BuildGlyphButton(string name) { return new Button(ButtonBehavior.Default) { ButtonMaterial = skin.GlyphButton, DebugName = name }; } private Checkbox BuildCheckbox(string name) { return new Checkbox(ButtonBehavior.Default) { UncheckedMaterial = skin.CheckOff, CheckedMaterial = skin.CheckOn, DebugName = name, }; } private SlabRow BuildRow(int index) { string name = "slab " + (index + 1); var returnMe = new SlabRow { Title = new Label(skin.HeadingText, name, Constraints.Unbounded), Remove = BuildGlyphButton(name + " remove button"), DirectionLabel = new Label(skin.HeadingText, "direction", Constraints.Unbounded), X = BuildField(DirectionGrid, name + " x field"), Y = BuildField(DirectionGrid, name + " y field"), Z = BuildField(DirectionGrid, name + " z field"), HasMin = BuildCheckbox(name + " from checkbox"), Min = BuildField(OffsetGrid, name + " from field"), HasMax = BuildCheckbox(name + " to checkbox"), Max = BuildField(OffsetGrid, name + " to field"), }; returnMe.Parts = new Control[] { returnMe.Title, returnMe.Remove, returnMe.DirectionLabel, returnMe.X, returnMe.Y, returnMe.Z, returnMe.HasMin, returnMe.Min, returnMe.HasMax, returnMe.Max, }; returnMe.Remove.Tooltip = skin.TooltipFor("Remove this slab"); returnMe.Remove.OnActivate += (ButtonBase _, in KeyboardSnapshot __, KeyboardManager ___) => Remove(index); returnMe.HasMin.OnCheckedChanged += _ => OpenEndsChanged(returnMe); returnMe.HasMax.OnCheckedChanged += _ => OpenEndsChanged(returnMe); return returnMe; } // A field for an open end holds no value, so it can't be typed in. private static void SetEnabledFields(SlabRow row) { row.Min.Enabled = row.HasMin.Checked; row.Max.Enabled = row.HasMax.Checked; } private void OpenEndsChanged(SlabRow row) { SetEnabledFields(row); Changed(); } private void ShowRows() { for (int i = 0; i < rows.Length; i++) { foreach (Control part in rows[i].Parts) { part.Visible = i < count; } } // With every slab in use there is no place left for the button. addButton.Enabled = count < rows.Length; addButton.Visible = addButton.Enabled; if (addButton.Enabled) { addButton.Bounds = addSlots[count]; } } private static void Load(SlabRow row, Slab slab) { row.X.Value = Shown(slab.Normal.X.Value, DirectionGrid); row.Y.Value = Shown(slab.Normal.Y.Value, DirectionGrid); row.Z.Value = Shown(slab.Normal.Z.Value, DirectionGrid); row.HasMin.Checked = !Scalar.IsNegativeInfinity(slab.Min); row.HasMax.Checked = !Scalar.IsPositiveInfinity(slab.Max); row.Min.Value = row.HasMin.Checked ? Shown(slab.Min.Value, OffsetGrid) : 0; row.Max.Value = row.HasMax.Checked ? Shown(slab.Max.Value, OffsetGrid) : 0; SetEnabledFields(row); } private static void Copy(SlabRow from, SlabRow to) { to.X.Value = from.X.Value; to.Y.Value = from.Y.Value; to.Z.Value = from.Z.Value; to.HasMin.Checked = from.HasMin.Checked; to.HasMax.Checked = from.HasMax.Checked; to.Min.Value = from.Min.Value; to.Max.Value = from.Max.Value; SetEnabledFields(to); } // The slabs the fields hold, or null with a reason when one of them can't be a slab. private Slab[] Collect(out string problem) { var returnMe = new Slab[count]; for (int i = 0; i < count; i++) { SlabRow row = rows[i]; var direction = new Vector3((double)row.X.Value, (double)row.Y.Value, (double)row.Z.Value); Scalar min = Scalar.NegativeInfinity; Scalar max = Scalar.PositiveInfinity; if (row.HasMin.Checked) { min = (double)row.Min.Value; } if (row.HasMax.Checked) { max = (double)row.Max.Value; } if (!(direction.Length() > 0)) { problem = $"Slab {i + 1} needs a direction that isn't zero."; return null; } if (min > max) { problem = $"Slab {i + 1} starts after it ends."; return null; } returnMe[i] = Slab.Between(direction, min, max); } problem = ""; return returnMe; } // A set that can't be built is shown as a problem and not reported, so the def keeps the last good one. private void Changed() { if (loading) { return; } Slab[] slabs = Collect(out string problem); problemLabel.Text = problem; if (slabs != null) { changed?.Invoke(slabs); } } private void Remove(int index) { loading = true; try { for (int i = index; i < count - 1; i++) { Copy(rows[i + 1], rows[i]); } } finally { loading = false; } count--; ShowRows(); Changed(); } private void Add() { loading = true; try { Load(rows[count], Slab.Above(Axis.Z, 0)); } finally { loading = false; } count++; ShowRows(); Changed(); } /// /// Shows the window on a set of slabs, or on none when it is null, and tells of each /// edit that follows. An edit that leaves a slab that can't exist is shown as a problem and not reported. /// /// There are more slabs than . public void Edit(Slab[] slabs, Action changed, string title) { if (slabs != null && slabs.Length > rows.Length) { throw new ArgumentException($"There can be at most {rows.Length} slabs.", nameof(slabs)); } this.changed = changed ?? throw new ArgumentNullException(nameof(changed)); loading = true; try { count = slabs == null ? 0 : slabs.Length; for (int i = 0; i < count; i++) { Load(rows[i], slabs[i]); } } finally { loading = false; } problemLabel.Text = ""; ShowRows(); Frame.Title = title; Frame.Visible = true; } /// /// Hides the window. /// public void Close() { Frame.Close(); } } }