浏览代码

adjust grid to shape, use grid when creating shape, grid w/ lesser zoom

Unknown 6 年之前
父节点
当前提交
afaaf32a75
共有 3 个文件被更改,包括 44 次插入5 次删除
  1. 6 0
      lazpaintcontrols/lcvectororiginal.pas
  2. 6 0
      lazpaintcontrols/lcvectorrectshapes.pas
  3. 32 5
      vectoredit/umain.pas

+ 6 - 0
lazpaintcontrols/lcvectororiginal.pas

@@ -91,6 +91,7 @@ type
     function GetIsSlow({%H-}AMatrix: TAffineMatrix): boolean; virtual;
     class function Fields: TVectorShapeFields; virtual;
     class function Usermodes: TVectorShapeUsermodes; virtual;
+    class function PreferPixelCentered: boolean; virtual;
     property OnChange: TShapeChangeEvent read FOnChange write FOnChange;
     property OnEditingChange: TShapeEditingChangeEvent read FOnEditingChange write FOnEditingChange;
     property PenColor: TBGRAPixel read GetPenColor write SetPenColor;
@@ -611,6 +612,11 @@ begin
   if vsfPenFill in Fields then result += [vsuEditPenFill];
 end;
 
+class function TVectorShape.PreferPixelCentered: boolean;
+begin
+  result := true;
+end;
+
 procedure TVectorShape.SetContainer(AValue: TVectorOriginal);
 begin
   if FContainer=AValue then Exit;

+ 6 - 0
lazpaintcontrols/lcvectorrectshapes.pas

@@ -105,6 +105,7 @@ type
     destructor Destroy; override;
     function GetCornerPositition: single; override;
     class function Fields: TVectorShapeFields; override;
+    class function PreferPixelCentered: boolean; override;
     procedure ConfigureEditor(AEditor: TBGRAOriginalEditor); override;
     procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); override;
     procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); override;
@@ -878,6 +879,11 @@ begin
   Result:= [vsfBackFill];
 end;
 
+class function TPhongShape.PreferPixelCentered: boolean;
+begin
+  Result:= false;
+end;
+
 procedure TPhongShape.ConfigureEditor(AEditor: TBGRAOriginalEditor);
 var
   idxLight: Integer;

+ 32 - 5
vectoredit/umain.pas

@@ -217,6 +217,8 @@ type
     procedure RequestPenFillUpdate(Sender: TObject);
     procedure AdjustToolbarTop;
     procedure UpdateSplineToolbar;
+    function SnapToGrid(APoint: TPointF): TPointF;
+    function ImgCoordToOriginalCoord(APoint: TPointF): TPointF;
   public
     { public declarations }
     img: TBGRALazPaintImage;
@@ -413,7 +415,7 @@ begin
 
   if not justDown and not Assigned(newShape) then
   begin
-    newStartPoint := AffineMatrixInverse(AffineMatrixTranslation(-0.5,-0.5)*vectorTransform*AffineMatrixTranslation(0.5,0.5))*imgPtF;
+    newStartPoint := ImgCoordToOriginalCoord(imgPtF);
     newButton := Button;
     justDown := true;
   end;
@@ -636,7 +638,7 @@ begin
   img.MouseMove(Shift, imgPtF.X, imgPtF.Y, cur, handled);
   UpdateViewCursor(cur);
 
-  ptF := AffineMatrixInverse(AffineMatrixTranslation(-0.5,-0.5)*vectorTransform*AffineMatrixTranslation(0.5,0.5))*imgPtF;
+  ptF := ImgCoordToOriginalCoord(imgPtF);
   if justDown and not Assigned(newShape) and IsCreateShapeTool(currentTool) and
     (VectLen(ptF-newStartPoint) >= EditorPointSize) then
   begin
@@ -1493,18 +1495,27 @@ begin
 end;
 
 procedure TForm1.SetEditorGrid(AActive: boolean);
+var pixelCentered: boolean;
 begin
   if Assigned(img) and Assigned(img.OriginalEditor) then
   begin
-    if zoomFactor > 4 then
+    if Assigned(vectorOriginal) and Assigned(vectorOriginal.SelectedShape) then
+      pixelCentered:= vectorOriginal.SelectedShape.PreferPixelCentered
+    else if PaintToolClass[currentTool]<>nil then
+      pixelCentered:= PaintToolClass[currentTool].PreferPixelCentered;
+
+    if zoomFactor >= 2.1 then
       img.OriginalEditor.GridMatrix := AffineMatrixTranslation(-0.5,-0.5)*
                                        vectorTransform*AffineMatrixTranslation(0.5,0.5)*
                                        AffineMatrixScale(0.5,0.5)
     else
+    if pixelCentered then
       img.OriginalEditor.GridMatrix := AffineMatrixTranslation(-0.5,-0.5)*
-                                       vectorTransform*AffineMatrixTranslation(0.5,0.5);
+                                       vectorTransform*AffineMatrixTranslation(0.5,0.5)
+    else
+      img.OriginalEditor.GridMatrix := AffineMatrixTranslation(-0.5,-0.5)*vectorTransform;
 
-    img.OriginalEditor.GridActive := AActive;
+    img.OriginalEditor.GridActive := AActive or (zoomFactor < 2.1);
   end;
 end;
 
@@ -1579,6 +1590,22 @@ begin
   end;
 end;
 
+function TForm1.SnapToGrid(APoint: TPointF): TPointF;
+begin
+  if Assigned(img) and Assigned(img.OriginalEditor) and img.OriginalEditor.GridActive then
+    result := img.OriginalEditor.SnapToGrid(APoint, false)
+  else
+    result := APoint;
+end;
+
+function TForm1.ImgCoordToOriginalCoord(APoint: TPointF): TPointF;
+begin
+  if Assigned(img) and Assigned(img.OriginalEditor) and img.OriginalEditor.GridActive then
+    result := SnapToGrid(AffineMatrixInverse(vectorTransform)*APoint)
+  else
+    result := SnapToGrid(AffineMatrixInverse(AffineMatrixTranslation(-0.5,-0.5)*vectorTransform*AffineMatrixTranslation(0.5,0.5))*APoint);
+end;
+
 procedure TForm1.DoCopy;
 begin
   if Assigned(vectorOriginal) and Assigned(vectorOriginal.SelectedShape) then