using System; using System.Drawing; using HeuristicLab.Common; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts { [StorableType("29AED0E8-1190-4DFC-92FC-0AC69FE4DBC1")] public class StringConstantBoxChartElementGenerator : EmptyBoxChartElementGenerator { public ValueParameter TextParameter { get { return (ValueParameter)Parameters["Text"]; } } public ValueParameter FontSizeParameter { get { return (ValueParameter) Parameters["FontSize"]; } } protected string Text { get { return TextParameter.Value.Value; } } protected int FontSize { get { return FontSizeParameter.Value.Value; }} #region Construction & Cloning [StorableConstructor] protected StringConstantBoxChartElementGenerator(StorableConstructorFlag _) : base(_) { } protected StringConstantBoxChartElementGenerator(StringConstantBoxChartElementGenerator original, Cloner cloner) : base(original, cloner) { RegisterEvents(); } public StringConstantBoxChartElementGenerator() { Parameters.Add(new ValueParameter("Text", "The text to be rendered inside this element.", new StringValue("Text"))); Parameters.Add(new ValueParameter("FontSize", "The font size to be used for the text.", new IntValue(11))); RegisterEvents(); if (GetType() == typeof(StringConstantBoxChartElementGenerator)) UpdateName(); } public override IDeepCloneable Clone(Cloner cloner) { return new StringConstantBoxChartElementGenerator(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterEvents(); } #endregion private void RegisterEvents() { TextParameter.ToStringChanged += new EventHandler(Parameters_Changed); } private void Parameters_Changed(object sender, EventArgs eventArgs) { UpdateName(); } protected override string CalculateName() { return string.Format("{0} \"{1}\" ({2})", base.CalculateName(), Text, FontSize); } public override void Draw(IRun run, Graphics g) { using(var font = new Font("Helvetica", FontSize)) { var size = g.MeasureString(Text, font); g.DrawString(Text, font, Brushes.Black, g.ClipBounds.Left+(g.ClipBounds.Width - size.Width)/2, g.ClipBounds.Top+(g.ClipBounds.Height - size.Height)/2); } } } }