using System; using System.Drawing; using Amphora; using Annulus; using Azimuth; using Blacklight.Core; using Glass; using Glass.Blacklight; using Prism.Glass; using Material = Glass.Material; namespace Konis.Testbed { /// /// A floating window that edits one function at a time: the plot with its points, the way the line runs between /// them, and the position and value of the selected point. Every part is built once, and opening it on another /// function reloads them. /// public class FunctionWindow { private const int PlotWidth = 256; private const int PlotHeight = 128; private const int MaxPoints = 16; private const int MarkerDiameter = 12; private const double Spacing = 6; private const int ProblemLines = 2; // What a def with no function over life has, which is one everywhere. private static readonly IScalarFunction One = new PiecewiseLinear((0, 1)); private readonly FunctionModel model = new FunctionModel(One); private readonly FunctionModePicker modePicker; private readonly FunctionEditor editor; private readonly FunctionPointFields fields; private readonly Label problemLabel; private Func apply; /// /// Builds the window, hidden, with textures made through . /// public FunctionWindow(TestbedSkin skin, WindowHandle window) { var plot = new FunctionPlot(window, PlotWidth, PlotHeight); modePicker = new FunctionModePicker(model, name => new Label(skin.Text, name, Constraints.Unbounded)) { PickerMaterial = new FunctionModePickerMaterial { Background = Material.Invisible, Dropdown = skin.Dropdown, }, }; string removeText = "Remove point"; editor = new FunctionEditor( model, MaxPoints, PlotWidth, PlotHeight, FunctionPlot.Colors, plot.Upload, ButtonBehavior.Default) { EditorMaterial = new FunctionEditorMaterial { Background = Material.Invisible, Plot = plot.Material, Marker = TextureOf(window, PickerArt.Ring(MarkerDiameter), MarkerDiameter), SelectedMarker = TextureOf(window, PickerArt.Dot(MarkerDiameter), MarkerDiameter), RemoveButton = skin.ButtonOfSize(skin.ButtonSizeFor(removeText)), Spacing = Spacing, }, }; editor.RemoveButton.CanvasChildren.Add( new Label(skin.ButtonText, removeText, Constraints.Tight(editor.RemoveButton.CanvasRect.Size))); fields = new FunctionPointFields(model, new NumberFieldBehavior { AllowsTyping = true }) { FieldsMaterial = new FunctionPointFieldsMaterial { Background = Material.Invisible, Field = skin.NumberField, FieldLabel = skin.HeadingText, Spacing = Spacing, }, }; problemLabel = new Label(skin.ProblemText, "", Constraints.Unbounded) { DebugName = "function problem" }; Frame = new Window { DebugName = "function", TitleStyle = skin.HeadingText, WindowMaterial = skin.Window, Visible = false, }; Scalar problemHeight = ProblemLines * skin.Text.SingleLineHeight; var contentSize = new RectAggregator(new Vector(0, Spacing)); contentSize.NewRow(modePicker.Bounds.Height); contentSize.NewRow(editor.Bounds.Height); contentSize.NewRow(fields.Bounds.Height); contentSize.NewRow(problemHeight); Scalar contentWidth = Scalar.Max( modePicker.Bounds.Width, Scalar.Max(editor.Bounds.Width, fields.Bounds.Width)); var content = new Vector(contentWidth, contentSize.Rect.Height); Rect remainder = Rect.FromPositionAndSize(Vector.Zero, content); const Shortfall tooSmall = Shortfall.UseWhatIsLeft; Place(modePicker, remainder.NewRow(modePicker.Bounds.Height, VerticalJustification.Top, Spacing, tooSmall)); Place(editor, remainder.NewRow(editor.Bounds.Height, VerticalJustification.Top, Spacing, tooSmall)); Place(fields, remainder.NewRow(fields.Bounds.Height, VerticalJustification.Top, Spacing, tooSmall)); problemLabel.Bounds = remainder.NewRow(problemHeight, VerticalJustification.Top, Spacing, tooSmall); Frame.CanvasChildren.Add(modePicker); Frame.CanvasChildren.Add(editor); Frame.CanvasChildren.Add(fields); 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))); model.Apply = function => apply == null ? Status.Ok : apply(function); model.Changed += _ => problemLabel.Text = ""; modePicker.OnRefused += ShowProblem; editor.OnRefused += ShowProblem; fields.OnRefused += ShowProblem; } /// /// Gets the window, to add to the world. /// public Window Frame { get; } /// /// Gets what the window says is wrong with the last edit it turned down, or an empty string. /// public string Problem => problemLabel.Text.ToString(); /// /// Gets whether the window is showing. /// public bool IsOpen => Frame.Visible; /// /// Returns whether the window can edit a function. Only a function made of points can be, and a def with none /// starts from one everywhere. /// public static bool CanEdit(IScalarFunction function) { return function == null || function is PointFunction; } private static void Place(Control child, Rect row) { child.Bounds = row.NewCol(child.Bounds.Width, HorizontalJustification.Left, 0, Shortfall.UseWhatIsLeft); } private static Material TextureOf(WindowHandle window, byte[] rgba, int diameter) { Texture texture = window.CreateTexture( new DecodedImage(rgba, new Size(diameter, diameter)), generateMipmaps: false); return new TextureMaterial { Texture = new BlacklightGlassTexture { Texture = texture } }; } private void ShowProblem(Status status) { problemLabel.Text = status.Describe(); } /// /// Shows the window on a function, or on one everywhere when it is null, and gives /// each function an edit would make. An edit it turns down isn't made, and the window says why. /// /// The function isn't one that takes. public void Edit(IScalarFunction function, Func apply, string title) { if (!CanEdit(function)) { throw new ArgumentException("Only a function made of points can be edited.", nameof(function)); } this.apply = apply ?? throw new ArgumentNullException(nameof(apply)); model.Load(function ?? One); Frame.Title = title; Frame.Visible = true; } /// /// Hides the window. /// public void Close() { Frame.Close(); } } }