using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts { [StorableClass] 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(bool deserializing) : base(deserializing) { } 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); } } } }