JD2022-TU1/main/tools/apps/ConductorDebugger/Source/View/BrickViewModel.cs

141 lines
No EOL
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using ConductorDebugger.Source.Brick;
using JD.Application.View.Timeline;
using JD.Application.View.WindowLess;
using TeaBox.Gui.Windowless;
namespace ConductorDebugger.Source.View
{
public class BrickViewModel : LocalizedTrackItem
{
#region Constants
private static readonly Font _font = new Font(FontFamily.GenericSansSerif, 10f);
#endregion
#region Variables
private readonly BrickModel _model;
#endregion
#region Constructor
public BrickViewModel(BrickModel model)
{
_model = model;
_model.Changed += ModelChanged;
}
protected override void Dispose(bool disposing)
{
_model.Changed -= ModelChanged;
base.Dispose(disposing);
}
#endregion
#region Properties
public BrickModel Model
{
get { return _model; }
}
public override string ToolTipText
{
get
{
return $"BrickName: {_model.Name}\n";
}
}
#endregion
#region Painting
protected override void OnPaint(PaintEventArgs e)
{
var baseColor = ComputeBackColor();
if (Bounds.Width > 0)
{
using (var brush = new LinearGradientBrush(Bounds, baseColor, baseColor.Add(-30), LinearGradientMode.Vertical))
using (var format = new StringFormat(StringFormatFlags.NoWrap) { Trimming = StringTrimming.EllipsisCharacter })
{
e.Graphics.FillRectangle(brush, Bounds);
e.Graphics.DrawString(Model.Name, _font, Brushes.DarkSlateGray, Bounds, format);
var rect = Bounds;
rect.Width--;
rect.Height--;
e.Graphics.DrawRectangle(Pens.Gray, rect);
}
}
}
private Color ComputeBackColor()
{
if (Selected)
{
return Color.Yellow;
}
switch (Model.BrickState)
{
case BrickState.Ready:
return Color.YellowGreen;
case BrickState.Started:
return Color.LimeGreen;
case BrickState.Preparing:
return Color.Orange;
case BrickState.NotCreated:
return Color.CornflowerBlue;
case BrickState.Discarding:
return Color.Red;
case BrickState.Seeking:
return Color.Violet;
}
return Color.LightCyan.Add((int) Model.BrickState*-10);
}
#endregion
#region Handlers
void ModelChanged(object sender, EventArgs e)
{
Invalidate();
}
#endregion
/// <summary>
/// Bounds relative to parent track bottom.
/// </summary>
public Rectangle RelativeBounds { get; set; }
public void UpdatePosition(Graphics graphics, List<Rectangle> unavailableSpace)
{
var rowHeight = 24;
int left = Timeline.TimeToPixel(_model.AbsoluteStartBeat);
int right = Timeline.TimeToPixel(_model.AbsoluteStartBeat + _model.Duration);
var relativeBounds = Rectangle.FromLTRB(left, -rowHeight, right, 0);
while (unavailableSpace.Any(rectangle => rectangle.Contains(relativeBounds.Location)))
{
relativeBounds.Y -= rowHeight + 1;
}
RelativeBounds = relativeBounds;
}
}
}