#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace HeuristicLab.Visualization { public abstract class LinearPrimitiveBase : PrimitiveBase { protected IGroup SelectionRectangles; private PointD myStart; public virtual PointD Start { get { return myStart; } } private PointD myEnd; public virtual PointD End { get { return myEnd; } } public virtual SizeD Size { get { return new SizeD(End.X - Start.X, End.Y - Start.Y); } } public virtual double Length { get { return (Start - End).Length; } } protected LinearPrimitiveBase(IChart chart, PointD start, PointD end) : base(chart) { SelectionRectangles = new Group(chart); myStart = start; myEnd = end; } protected LinearPrimitiveBase(IChart chart, PointD start, PointD end, Pen pen, Brush brush) : base(chart, pen, brush) { SelectionRectangles = new Group(chart); myStart = start; myEnd = end; } public virtual void SetPosition(PointD start, PointD end) { myStart = start; myEnd = end; OnRedrawRequired(); } public override void Move(Offset delta) { SetPosition(Start + delta, End + delta); } public override void Move(PointD point, Offset delta) { if (Selected && SelectionRectangles.ContainsPoint(point)) { var rect = (SelectionRectangle)SelectionRectangles.GetPrimitive(point); if (rect.Point == Start) { SetPosition(Start + delta, End); } else if (rect.Point == End) { SetPosition(Start, End + delta); } } else SetPosition(Start + delta, End + delta); } public override void SnapToGrid(IGrid grid) { var bl = grid.GetBottomLeftGridPoint(Start); var ur = grid.GetUpperRightGridPoint(Start); var sx = Math.Abs(bl.X - Start.X) <= Math.Abs(ur.X - Start.X) ? bl.X : ur.X; var sy = Math.Abs(bl.Y - Start.Y) <= Math.Abs(ur.Y - Start.Y) ? bl.Y : ur.Y; var end = new PointD(End.X + (sx - Start.X), End.Y + (sy - Start.Y)); bl = grid.GetBottomLeftGridPoint(end); ur = grid.GetUpperRightGridPoint(end); var ex = Math.Abs(bl.X - end.X) <= Math.Abs(ur.X - end.X) ? bl.X : ur.X; var ey = Math.Abs(bl.Y - end.Y) <= Math.Abs(ur.Y - end.Y) ? bl.Y : ur.Y; SetPosition(new PointD(sx, sy), new PointD(ex, ey)); } public override void SnapToGrid(PointD point, IGrid grid) { if (Selected && SelectionRectangles.ContainsPoint(point)) { var rect = (SelectionRectangle)SelectionRectangles.GetPrimitive(point); if (rect.Point == Start) { var bl = grid.GetBottomLeftGridPoint(Start); var ur = grid.GetUpperRightGridPoint(Start); var sx = Math.Abs(bl.X - Start.X) <= Math.Abs(ur.X - Start.X) ? bl.X : ur.X; var sy = Math.Abs(bl.Y - Start.Y) <= Math.Abs(ur.Y - Start.Y) ? bl.Y : ur.Y; SetPosition(new PointD(sx, sy), End); } else if (rect.Point == End) { var bl = grid.GetBottomLeftGridPoint(End); var ur = grid.GetUpperRightGridPoint(End); var ex = Math.Abs(bl.X - End.X) <= Math.Abs(ur.X - End.X) ? bl.X : ur.X; var ey = Math.Abs(bl.Y - End.Y) <= Math.Abs(ur.Y - End.Y) ? bl.Y : ur.Y; SetPosition(Start, new PointD(ex, ey)); } } else SnapToGrid(grid); } public override bool ContainsPoint(PointD point) { if (Selected && SelectionRectangles.GetPrimitive(point) != null) return true; return false; } public override Cursor GetCursor(PointD point) { if (Selected) { var cursor = SelectionRectangles.GetCursor(point); if (cursor != null) return cursor; } return base.GetCursor(point); } public override void PostDraw(Graphics graphics) { SelectionRectangles.Clear(); if (Selected) { var pen = new Pen(Color.LightGray, 3) { DashStyle = DashStyle.Dash }; graphics.DrawLine(pen, Chart.TransformWorldToPixel(Start), Chart.TransformWorldToPixel(End)); SelectionRectangles.Add(new SelectionRectangle(Chart, Start, Cursors.SizeAll)); SelectionRectangles.Add(new SelectionRectangle(Chart, End, Cursors.SizeAll)); SelectionRectangles.Draw(graphics); } } } }