using System;
using System.Collections.Generic;
using Annulus;
using Azimuth;
using Glass;
namespace Clave.Glass
{
///
/// A row for every action of an editor's sources, in order. It sizes itself to its content, so it can sit on a
/// ScrollView's canvas, with a somewhere that doesn't scroll.
///
public class KeyBindingsScreen : Control
{
private readonly KeyBindingsMaterial material;
private readonly List rows = new List();
// True while the screen lays itself out or refreshes its rows, so a row that resizes doesn't lay it out again.
private bool layingOut;
public KeyBindingsScreen(KeyBindingsEditor editor, KeyBindingsMaterial material)
{
if (editor == null)
{
throw new ArgumentNullException(nameof(editor));
}
this.material = material;
SetMaterial(material.Background ?? global::Glass.Material.Invisible);
foreach (KeyBindings source in editor.Sources)
{
foreach (KeyAction action in source.Actions)
{
var row = new KeyBindingRow(editor, source, action, material);
row.Resized += _ => Layout();
rows.Add(row);
CanvasChildren.Add(row);
}
source.Rebound += _ => ShowChanges();
}
Layout();
}
public IReadOnlyList Rows
{
get { return rows; }
}
///
/// Raised after the screen is laid out again, which may have changed its size.
///
public event Action Resized;
public KeyBindingRow RowOf(KeyBindings source, KeyAction action)
{
foreach (KeyBindingRow row in rows)
{
if (row.Source == source && row.Action == action)
{
return row;
}
}
throw new ArgumentException("That action has no row here.", nameof(action));
}
private void ShowChanges()
{
// Every row, since a clash note can name the action that changed.
layingOut = true;
try
{
foreach (KeyBindingRow row in rows)
{
row.Refresh();
}
}
finally
{
layingOut = false;
}
Layout();
}
///
/// Ends any capture a slot has on, binding nothing, for when the screen is closed.
///
public void CancelCapture()
{
foreach (KeyBindingRow row in rows)
{
foreach (KeyBindingSlot slot in row.Slots)
{
slot.CancelCapture();
}
}
}
///
/// Stacks the rows with their cells lined up, and sizes the screen to fit. Keeps its position.
///
public void Layout()
{
if (layingOut)
{
return;
}
layingOut = true;
try
{
Scalar spacing = material.Spacing;
// Every row's names and cells share widths, so the columns line up down the screen. A cell is at least
// half of the widest binding, so a binding taking a whole line always fits on it.
Scalar nameColumn = 0;
Scalar cell = 0;
Scalar widest = 0;
foreach (KeyBindingRow row in rows)
{
nameColumn = Scalar.Max(nameColumn, row.NameWidth);
cell = Scalar.Max(cell, row.CellWidth);
widest = Scalar.Max(widest, row.WidestSlot);
}
cell = Scalar.Max(cell, (widest - spacing * (KeyBindingRow.Columns - 1)) / KeyBindingRow.Columns);
foreach (KeyBindingRow row in rows)
{
row.Layout(nameColumn, cell);
}
// Measure: the rows, one under the other.
var stack = new RectAggregator(new Vector(0, spacing));
Scalar width = 0;
foreach (KeyBindingRow row in rows)
{
stack.NewRow(row.Size.Y);
width = Scalar.Max(width, row.Size.X);
}
Bounds = Bounds.WithSize(new Vector(width, stack.Rect.Height));
// Arrange: the same rows, top to bottom.
Rect remainder = Rect.FromPositionAndSize(Vector.Zero, CanvasRect.Size);
foreach (KeyBindingRow row in rows)
{
Rect rowRect = remainder.NewRow(row.Size.Y, margin: spacing, shortfall: Shortfall.UseWhatIsLeft);
row.Bounds = rowRect.NewCol(row.Size.X, HorizontalJustification.Left, 0, Shortfall.UseWhatIsLeft);
}
}
finally
{
layingOut = false;
}
Resized?.Invoke(this);
}
}
}