mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-06-02 20:58:23 +02:00
cocoa: adding rotation of the cursor to make diagonal resizing cursors
git-svn-id: trunk@61423 -
This commit is contained in:
parent
44e350ffea
commit
285f0d4cb6
@ -301,6 +301,7 @@ type
|
||||
public
|
||||
constructor CreateStandard(const ACursor: NSCursor);
|
||||
constructor CreateFromBitmap(const ABitmap: TCocoaBitmap; const hotSpot: NSPoint);
|
||||
constructor CreateFromCustomCursor(const ACursor: NSCursor);
|
||||
destructor Destroy; override;
|
||||
function Install: TCocoaCursor;
|
||||
procedure SetCursor;
|
||||
@ -1185,6 +1186,12 @@ begin
|
||||
FStandard := False;
|
||||
end;
|
||||
|
||||
constructor TCocoaCursor.CreateFromCustomCursor(const ACursor: NSCursor);
|
||||
begin
|
||||
FCursor := ACursor;
|
||||
FStandard := False;
|
||||
end;
|
||||
|
||||
destructor TCocoaCursor.Destroy;
|
||||
begin
|
||||
FreeAndNil(FBitmap);
|
||||
|
@ -36,16 +36,16 @@ begin
|
||||
crIBeam : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.IBeamCursor));
|
||||
crSizeNS,
|
||||
crVSplit : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.resizeUpDownCursor));
|
||||
crSizeNESW,
|
||||
crSizeNWSE,
|
||||
crSizeNESW : Result := HCursor(TCocoaCursor.CreateFromCustomCursor( AllocCursorFromCursorByDegrees(NSCursor.resizeLeftRightCursor, -45) ));
|
||||
crSizeNWSE : Result := HCursor(TCocoaCursor.CreateFromCustomCursor( AllocCursorFromCursorByDegrees(NSCursor.resizeLeftRightCursor, 45) ));
|
||||
crSizeWE,
|
||||
crHSplit : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.resizeLeftRightCursor));
|
||||
crSizeN : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.resizeUpCursor));
|
||||
crSizeNW,
|
||||
crSizeSW,
|
||||
crSizeNW : Result := HCursor(TCocoaCursor.CreateFromCustomCursor( AllocCursorFromCursorByDegrees(NSCursor.resizeLeftCursor, -45) ));
|
||||
crSizeSW : Result := HCursor(TCocoaCursor.CreateFromCustomCursor( AllocCursorFromCursorByDegrees(NSCursor.resizeLeftCursor, 45) ));
|
||||
crSizeW : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.resizeLeftCursor));
|
||||
crSizeNE,
|
||||
crSizeSE,
|
||||
crSizeNE : Result := HCursor(TCocoaCursor.CreateFromCustomCursor( AllocCursorFromCursorByDegrees(NSCursor.resizeRightCursor, 45) ));
|
||||
crSizeSE : Result := HCursor(TCocoaCursor.CreateFromCustomCursor( AllocCursorFromCursorByDegrees(NSCursor.resizeRightCursor, -45) ));
|
||||
crSizeE : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.resizeRightCursor));
|
||||
crSizeS : Result := HCursor(TCocoaCursor.CreateStandard(NSCursor.resizeDownCursor));
|
||||
crNo,
|
||||
|
@ -117,6 +117,9 @@ const
|
||||
|
||||
function NSEventRawKeyChar(ev: NSEvent): System.WideChar;
|
||||
|
||||
function AllocImageRotatedByDegrees(src: NSImage; degrees: double): NSImage;
|
||||
function AllocCursorFromCursorByDegrees(src: NSCursor; degrees: double): NSCursor;
|
||||
|
||||
implementation
|
||||
|
||||
procedure ApplicationWillShowModal;
|
||||
@ -938,5 +941,64 @@ begin
|
||||
Result := System.WideChar(m.characterAtIndex(0));
|
||||
end;
|
||||
|
||||
function AllocImageRotatedByDegrees(src: NSImage; degrees: double): NSImage;
|
||||
var
|
||||
imageBounds : NSRect;
|
||||
pathBounds : NSBezierPath;
|
||||
transform : NSAffineTransform;
|
||||
rotatedBounds : NSRect;
|
||||
rotatedImage : NSImage;
|
||||
begin
|
||||
if not Assigned(src) then
|
||||
begin
|
||||
Result := nil;
|
||||
Exit;
|
||||
end;
|
||||
|
||||
// src: https://stackoverflow.com/questions/31699235/rotate-nsimage-in-swift-cocoa-mac-osx
|
||||
|
||||
imageBounds.size := src.size;
|
||||
pathBounds := NSBezierPath.bezierPathWithRect(imageBounds);
|
||||
transform := NSAffineTransform.alloc.init;
|
||||
transform.rotatebyDegrees(degrees);
|
||||
pathBounds.transformUsingAffineTransform(transform);
|
||||
rotatedBounds := NSMakeRect(NSZeroPoint.x, NSZeroPoint.y, src.size.width, src.size.height );
|
||||
rotatedImage := NSImage(NSImage.alloc).initWithSize(rotatedBounds.size);
|
||||
|
||||
//Center the image within the rotated bounds
|
||||
imageBounds.origin.x := NSMidX(rotatedBounds) - (NSWidth(imageBounds) / 2);
|
||||
imageBounds.origin.y := NSMidY(rotatedBounds) - (NSHeight(imageBounds) / 2);
|
||||
transform.release;
|
||||
|
||||
// Start a new transform
|
||||
transform := NSAffineTransform.alloc.init;
|
||||
// Move coordinate system to the center (since we want to rotate around the center)
|
||||
transform.translateXBy_yBy(rotatedBounds.size.width / 2, rotatedBounds.size.width / 2);
|
||||
transform.rotateByDegrees(degrees);
|
||||
// Move the coordinate system bak to normal
|
||||
transform.translateXBy_yBy(-rotatedBounds.size.width / 2, -rotatedBounds.size.height / 2);
|
||||
// Draw the original image, rotated, into the new image
|
||||
rotatedImage.lockFocus;
|
||||
transform.concat();
|
||||
src.drawInRect_fromRect_operation_fraction(imageBounds, NSZeroRect, NSCompositeCopy, 1.0);
|
||||
rotatedImage.unlockFocus();
|
||||
Result := rotatedImage;
|
||||
|
||||
transform.release;
|
||||
end;
|
||||
|
||||
function AllocCursorFromCursorByDegrees(src: NSCursor; degrees: double): NSCursor;
|
||||
var
|
||||
img : NSImage;
|
||||
begin
|
||||
img := AllocImageRotatedByDegrees(src.image, degrees);
|
||||
//todo: a better hotspot detection
|
||||
Result := NSCursor.alloc.initWithImage_hotSpot(
|
||||
img,
|
||||
NSMakePoint(img.size.height / 2, img.size.width / 2)
|
||||
);
|
||||
img.release;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user