unit UOSD;
interface
uses System.Types, System.Classes, FMX.Graphics, FMX.Controls
{$IFDEF MACOS}
, Macapi.AppKit
{$ENDIF};
type
TOSD =
class(TControl)
protected
FBitmap: TBitmap;
FNativePaint: Boolean;
FNeedUpdate: Boolean;
{$IFDEF MACOS}
FOverlayView: NSImageView;
{$ENDIF}
procedure Paint;
override;
procedure Resize;
override;
procedure SetBitmap(
const ABitmap: TBitmap);
procedure SetNativePaint(
const ANativePaint: Boolean);
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
published
property Bitmap: TBitmap
read FBitmap
write SetBitmap;
property Opacity;
property NativePaint: Boolean
read FNativePaint
write SetNativePaint
default false;
end;
implementation
uses FMX.Types, FMX.Forms
{$IFDEF MACOS}, FMX.
Platform.Mac, FMX.Helpers.Mac, Macapi.CocoaTypes, Macapi.CoreGraphics
{$ENDIF};
function GetParentForm(Control: TFmxObject): TCommonCustomForm;
begin
if (Control.Root <>
nil)
and (Control.Root.GetObject
is TCommonCustomForm)
then
Result := TCommonCustomForm(Control.Root.GetObject)
else
Result :=
nil;
end;
constructor TOSD.Create(AOwner: TComponent);
begin
inherited;
Width := 100;
Height := 100;
FBitmap := TBitmap.Create;
end;
destructor TOSD.Destroy;
begin
FBitmap.free;
inherited;
end;
procedure TOSD.SetNativePaint(
const ANativePaint: Boolean);
begin
if FNativePaint <> ANativePaint
then
begin
FNativePaint := ANativePaint;
{$IFDEF MACOS}
if assigned(FOverlayView)
then
FOverlayView.release;
FNeedUpdate := true;
{$ENDIF}
Repaint;
end;
end;
procedure TOSD.SetBitmap(
const ABitmap: TBitmap);
begin
FBitmap.Assign(ABitmap);
FNeedUpdate := true;
Repaint;
end;
procedure TOSD.Resize;
{$IFDEF MACOS}
var
R: TRectF;
{$ENDIF}
begin
inherited;
{$IFDEF MACOS}
if assigned(FOverlayView)
then
begin
R := GetAbsoluteRect;
FOverlayView.setFrame(MakeNSRect(R.Left, R.Top, R.Width, R.Height));
end;
{$ENDIF}
end;
procedure TOSD.Paint;
var
DstRect: TRectF;
{$IFDEF MACOS}
FormView: NSView;
Form: TCommonCustomForm;
R: TRectF;
{$ENDIF}
begin
{$IFDEF MACOS}
if FNativePaint
then
begin
if FNeedUpdate
then
begin
if not assigned(FOverlayView)
then
begin
R := BoundsRect;
FOverlayView := TNSImageView.Wrap
(TNSImageView.Alloc.initWithFrame(MakeNSRect(R.Left, R.Top, R.Width,
R.Height)));
FOverlayView.retain;
Form := GetParentForm(self);
if assigned(Form)
then
begin
FormView := TNSView.Wrap(WindowHandleToPlatform(Form.Handle)
.Wnd.contentView);
FormView.addSubview(FOverlayView, NSWindowAbove,
nil);
end;
FOverlayView.setWantsLayer(true);
FOverlayView.setFrame(MakeNSRect(R.Left, R.Top, R.Width, R.Height));
end;
FOverlayView.setImage(BitmapToMacBitmap(FBitmap));
FNeedUpdate := false;
end;
exit;
end;
{$ENDIF}
DstRect := FBitmap.BoundsF;
DstRect.Fit(BoundsRect);
Canvas.DrawBitmap(FBitmap, FBitmap.BoundsF, DstRect, Opacity)
end;
end.