using System;
using System.Drawing;
using Amphora;
using Annulus;
using Azimuth;
using Blacklight.Core;
using Glass;
using Glass.Blacklight;
using Prism;
using Prism.Glass;
using Color = Prism.Color;
using Material = Glass.Material;
namespace Konis.Testbed
{
///
/// A floating window that edits one gradient at a time: the bar with its stops, the space and hue direction the
/// blend goes through, and a color picker for the selected stop. Every part is built once, and opening it on
/// another gradient reloads them.
///
public class GradientWindow
{
private const int BarPixels = 256;
private const int BarHeight = 24;
private const int MaxStops = 16;
private const int DiscDiameter = 192;
private const int StripWidth = 16;
private const int MarkerWidth = 13;
private const int MarkerHeight = 14;
private const double Spacing = 6;
private const int ProblemLines = 2;
private readonly GradientModel model = new GradientModel(Gradient.Constant(Colors.White));
private readonly ColorPickerModel pickerModel = new ColorPickerModel(Colors.White);
private readonly GradientBar bar;
private readonly BlendPicker blend;
private readonly GradientEditor editor;
private readonly ColorPicker picker;
private readonly Label problemLabel;
private readonly StopColorLink colorLink;
private Func apply;
///
/// Builds the window, hidden, with textures made through .
///
public GradientWindow(TestbedSkin skin, WindowHandle window)
{
bar = new GradientBar(window, BarPixels);
colorLink = new StopColorLink(model, pickerModel);
blend = new BlendPicker(model, name => new Label(skin.Text, name, Constraints.Unbounded))
{
BlendMaterial = new BlendPickerMaterial
{
Background = Material.Invisible,
Dropdown = skin.Dropdown,
Spacing = Spacing,
},
};
string removeText = "Remove stop";
byte[] plainMarker = PickerArt.Marker(MarkerWidth, MarkerHeight, selected: false);
byte[] selectedMarker = PickerArt.Marker(MarkerWidth, MarkerHeight, selected: true);
editor = new GradientEditor(model, MaxStops, BarPixels, bar.Upload, ButtonBehavior.Default)
{
EditorMaterial = new GradientEditorMaterial
{
Background = Material.Invisible,
BarBackdrop = TextureOf(window, PickerArt.Backdrop(BarPixels, BarHeight, 8), BarPixels, BarHeight),
Bar = bar.Material,
Marker = TextureOf(window, plainMarker, MarkerWidth, MarkerHeight),
SelectedMarker = TextureOf(window, selectedMarker, MarkerWidth, MarkerHeight),
RemoveButton = skin.ButtonOfSize(skin.ButtonSizeFor(removeText)),
Spacing = Spacing,
BarHeight = BarHeight,
},
};
editor.RemoveButton.CanvasChildren.Add(
new Label(skin.ButtonText, removeText, Constraints.Tight(editor.RemoveButton.CanvasRect.Size)));
picker = new ColorPicker(
pickerModel,
ColorPickerBehavior.Default,
new TextBoxBehavior(),
new NumberFieldBehavior { AllowsTyping = true })
{
PickerMaterial = new ColorPickerMaterial
{
Background = Material.Invisible,
Disc = TextureOf(window, PickerArt.Disc(DiscDiameter), DiscDiameter, DiscDiameter),
ValueStrip = TextureOf(
window, PickerArt.ValueStrip(StripWidth, DiscDiameter), StripWidth, DiscDiameter),
AlphaStrip = TextureOf(
window, PickerArt.AlphaStrip(StripWidth, DiscDiameter), StripWidth, DiscDiameter),
AlphaBackdrop = TextureOf(
window, PickerArt.Backdrop(StripWidth, DiscDiameter, 4), StripWidth, DiscDiameter),
DiscMarker = TextureOf(window, PickerArt.Ring(12), 12, 12),
StripMarker = TextureOf(window, PickerArt.Line(), 1, 3),
HexBox = skin.HexBox,
Field = skin.NumberField,
FieldLabel = skin.HeadingText,
Spacing = Spacing,
SwatchHeight = 20,
},
};
problemLabel = new Label(skin.ProblemText, "", Constraints.Unbounded) { DebugName = "gradient problem" };
Frame = new Window
{
DebugName = "gradient",
TitleStyle = skin.HeadingText,
WindowMaterial = skin.Window,
Visible = false,
};
Scalar problemHeight = ProblemLines * skin.Text.SingleLineHeight;
var contentSize = new RectAggregator(new Vector(0, Spacing));
contentSize.NewRow(blend.Bounds.Height);
contentSize.NewRow(editor.Bounds.Height);
contentSize.NewRow(picker.Bounds.Height);
contentSize.NewRow(problemHeight);
Scalar contentWidth = Scalar.Max(blend.Bounds.Width, Scalar.Max(editor.Bounds.Width, picker.Bounds.Width));
var content = new Vector(contentWidth, contentSize.Rect.Height);
Rect remainder = Rect.FromPositionAndSize(Vector.Zero, content);
const Shortfall tooSmall = Shortfall.UseWhatIsLeft;
Place(blend, remainder.NewRow(blend.Bounds.Height, VerticalJustification.Top, Spacing, tooSmall));
Place(editor, remainder.NewRow(editor.Bounds.Height, VerticalJustification.Top, Spacing, tooSmall));
Place(picker, remainder.NewRow(picker.Bounds.Height, VerticalJustification.Top, Spacing, tooSmall));
problemLabel.Bounds = remainder.NewRow(problemHeight, VerticalJustification.Top, Spacing, tooSmall);
Frame.CanvasChildren.Add(blend);
Frame.CanvasChildren.Add(editor);
Frame.CanvasChildren.Add(picker);
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 = gradient => apply == null ? Status.Ok : apply(gradient);
model.Changed += _ => problemLabel.Text = "";
blend.OnRefused += ShowProblem;
editor.OnRefused += ShowProblem;
colorLink.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;
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 width, int height)
{
Texture texture = window.CreateTexture(
new DecodedImage(rgba, new Size(width, height)), generateMipmaps: false);
return new TextureMaterial { Texture = new BlacklightGlassTexture { Texture = texture } };
}
private void ShowProblem(Status status)
{
problemLabel.Text = status.Describe();
}
///
/// Shows the window on a gradient, and gives each gradient an edit would make. An
/// edit it turns down isn't made, and the window says why.
///
public void Edit(Gradient gradient, Func apply, string title)
{
this.apply = apply ?? throw new ArgumentNullException(nameof(apply));
model.Load(gradient);
Frame.Title = title;
Frame.Visible = true;
}
///
/// Hides the window.
///
public void Close()
{
Frame.Close();
}
}
}