Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/21/15 14:18:31 (9 years ago)
Author:
jkarder
Message:

#1265: merged changes from abeham

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/LinearPrimitiveBase.cs

    r12535 r13045  
    2020#endregion
    2121
     22using System;
    2223using System.Drawing;
    2324using System.Drawing.Drawing2D;
     
    2627namespace HeuristicLab.Visualization {
    2728  public abstract class LinearPrimitiveBase : PrimitiveBase {
    28     protected IGroup selectionRectangles;
     29    protected IGroup SelectionRectangles;
    2930
    3031    private PointD myStart;
     
    4546    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end)
    4647      : base(chart) {
    47       selectionRectangles = new Group(chart);
     48      SelectionRectangles = new Group(chart);
    4849      myStart = start;
    4950      myEnd = end;
    5051    }
    51     protected LinearPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2)
    52       : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
    53     }
    5452    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end, Pen pen, Brush brush)
    5553      : base(chart, pen, brush) {
    56       selectionRectangles = new Group(chart);
     54      SelectionRectangles = new Group(chart);
    5755      myStart = start;
    5856      myEnd = end;
    59     }
    60     protected LinearPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush)
    61       : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) {
    6257    }
    6358
     
    6560      myStart = start;
    6661      myEnd = end;
    67       OnUpdate();
     62      OnRedrawRequired();
    6863    }
    69     public void SetPosition(double x1, double y1, double x2, double y2) {
    70       SetPosition(new PointD(x1, y1), new PointD(x2, y2));
    71     }
     64
    7265    public override void Move(Offset delta) {
    7366      SetPosition(Start + delta, End + delta);
    7467    }
    7568
    76     public override bool ContainsPoint(PointD point) {
    77       if (Selected) {
    78         if (selectionRectangles.GetPrimitive(point) != null) return true;
    79       }
    80       return false;
     69    public override void Move(PointD point, Offset delta) {
     70      if (Selected && SelectionRectangles.ContainsPoint(point)) {
     71        var rect = (SelectionRectangle)SelectionRectangles.GetPrimitive(point);
     72        if (rect.Point == Start) {
     73          SetPosition(Start + delta, End);
     74        } else if (rect.Point == End) {
     75          SetPosition(Start, End + delta);
     76        }
     77      } else SetPosition(Start + delta, End + delta);
    8178    }
    8279
    83     public override void MouseDrag(PointD point, Offset offset, MouseButtons button) {
    84       if (button == MouseButtons.Left) {
    85         if (Selected) {
    86           if (selectionRectangles.ContainsPoint(point)) {
    87             SelectionRectangle rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
    88             if (rect.Point == Start) {
    89               SetPosition(Start + offset, End);
    90             } else if (rect.Point == End) {
    91               SetPosition(Start, End + offset);
    92             }
    93           } else {
    94             base.MouseDrag(point, offset, button);
    95           }
     80    public override void SnapToGrid(IGrid grid) {
     81      var bl = grid.GetBottomLeftGridPoint(Start);
     82      var ur = grid.GetUpperRightGridPoint(Start);
     83      var sx = Math.Abs(bl.X - Start.X) <= Math.Abs(ur.X - Start.X) ? bl.X : ur.X;
     84      var sy = Math.Abs(bl.Y - Start.Y) <= Math.Abs(ur.Y - Start.Y) ? bl.Y : ur.Y;
     85
     86      var end = new PointD(End.X + (sx - Start.X), End.Y + (sy - Start.Y));
     87
     88      bl = grid.GetBottomLeftGridPoint(end);
     89      ur = grid.GetUpperRightGridPoint(end);
     90      var ex = Math.Abs(bl.X - end.X) <= Math.Abs(ur.X - end.X) ? bl.X : ur.X;
     91      var ey = Math.Abs(bl.Y - end.Y) <= Math.Abs(ur.Y - end.Y) ? bl.Y : ur.Y;
     92
     93      SetPosition(new PointD(sx, sy), new PointD(ex, ey));
     94    }
     95
     96    public override void SnapToGrid(PointD point, IGrid grid) {
     97      if (Selected && SelectionRectangles.ContainsPoint(point)) {
     98        var rect = (SelectionRectangle)SelectionRectangles.GetPrimitive(point);
     99        if (rect.Point == Start) {
     100          var bl = grid.GetBottomLeftGridPoint(Start);
     101          var ur = grid.GetUpperRightGridPoint(Start);
     102          var sx = Math.Abs(bl.X - Start.X) <= Math.Abs(ur.X - Start.X) ? bl.X : ur.X;
     103          var sy = Math.Abs(bl.Y - Start.Y) <= Math.Abs(ur.Y - Start.Y) ? bl.Y : ur.Y;
     104          SetPosition(new PointD(sx, sy), End);
     105        } else if (rect.Point == End) {
     106          var bl = grid.GetBottomLeftGridPoint(End);
     107          var ur = grid.GetUpperRightGridPoint(End);
     108          var ex = Math.Abs(bl.X - End.X) <= Math.Abs(ur.X - End.X) ? bl.X : ur.X;
     109          var ey = Math.Abs(bl.Y - End.Y) <= Math.Abs(ur.Y - End.Y) ? bl.Y : ur.Y;
     110          SetPosition(Start, new PointD(ex, ey));
    96111        }
    97       }
     112      } else SnapToGrid(grid);
     113    }
     114
     115    public override bool ContainsPoint(PointD point) {
     116      if (Selected && SelectionRectangles.GetPrimitive(point) != null) return true;
     117      return false;
    98118    }
    99119
    100120    public override Cursor GetCursor(PointD point) {
    101121      if (Selected) {
    102         Cursor cursor = selectionRectangles.GetCursor(point);
     122        var cursor = SelectionRectangles.GetCursor(point);
    103123        if (cursor != null) return cursor;
    104124      }
     
    107127
    108128    public override void PostDraw(Graphics graphics) {
    109       selectionRectangles.Clear();
     129      SelectionRectangles.Clear();
    110130      if (Selected) {
    111         Pen pen = new Pen(Color.LightGray, 3);
    112         pen.DashStyle = DashStyle.Dash;
     131        var pen = new Pen(Color.LightGray, 3) { DashStyle = DashStyle.Dash };
    113132        graphics.DrawLine(pen,
    114133                          Chart.TransformWorldToPixel(Start),
    115134                          Chart.TransformWorldToPixel(End));
    116         selectionRectangles.Add(new SelectionRectangle(Chart, Start, Cursors.SizeAll));
    117         selectionRectangles.Add(new SelectionRectangle(Chart, End, Cursors.SizeAll));
     135        SelectionRectangles.Add(new SelectionRectangle(Chart, Start, Cursors.SizeAll));
     136        SelectionRectangles.Add(new SelectionRectangle(Chart, End, Cursors.SizeAll));
    118137
    119         selectionRectangles.Draw(graphics);
     138        SelectionRectangles.Draw(graphics);
    120139      }
    121140    }
Note: See TracChangeset for help on using the changeset viewer.