GR32_RotLayer.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. unit GR32_RotLayer;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Rotation Layer Example
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Alex A. Denisov
  19. *
  20. * Portions created by the Initial Developer are Copyright (C) 2000-2005
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. *
  25. * ***** END LICENSE BLOCK ***** *)
  26. interface
  27. {$IFDEF FPC}
  28. {$MODE Delphi}
  29. {$ENDIF}
  30. {$IFNDEF FPC}
  31. {$DEFINE Windows}
  32. {$ENDIF}
  33. uses
  34. SysUtils, Classes, Controls, Forms, Graphics, GR32, GR32_Layers, GR32_Transforms;
  35. type
  36. TCustomAffineLayer = class(TCustomLayer)
  37. private
  38. FAlphaHit: Boolean;
  39. FTransformation: TAffineTransformation;
  40. FBitmap: TBitmap32;
  41. procedure BitmapChanged(Sender: TObject);
  42. procedure SetBitmap(Value: TBitmap32);
  43. protected
  44. FBitmapCenter: TFloatPoint;
  45. procedure AdjustTransformation; virtual;
  46. function DoHitTest(X, Y: Integer): Boolean; override;
  47. procedure Paint(Buffer: TBitmap32); override;
  48. property Transformation: TAffineTransformation read FTransformation;
  49. public
  50. constructor Create(ALayerCollection: TLayerCollection); override;
  51. destructor Destroy; override;
  52. property AlphaHit: Boolean read FAlphaHit write FAlphaHit;
  53. property Bitmap: TBitmap32 read FBitmap write SetBitmap;
  54. end;
  55. TAffineLayer = class(TCustomAffineLayer)
  56. public
  57. property Transformation;
  58. end;
  59. TRotLayer = class(TCustomAffineLayer)
  60. private
  61. FPosition: TFloatPoint;
  62. FScaled: Boolean;
  63. FAngle: Single;
  64. procedure SetAngle(Value: Single);
  65. procedure SetPosition(const Value: TFloatPoint);
  66. procedure SetScaled(Value: Boolean);
  67. procedure SetBitmapCenter(const Value: TFloatPoint);
  68. protected
  69. procedure AdjustTransformation; override;
  70. public
  71. property Angle: Single read FAngle write SetAngle;
  72. property BitmapCenter: TFloatPoint read FBitmapCenter write SetBitmapCenter;
  73. property Scaled: Boolean read FScaled write SetScaled;
  74. property Position: TFloatPoint read FPosition write SetPosition;
  75. end;
  76. implementation
  77. uses
  78. Types;
  79. { TCustomAffineLayer }
  80. type TATAccess = class(TAffineTransformation);
  81. procedure TCustomAffineLayer.AdjustTransformation;
  82. begin
  83. // do nothing here
  84. end;
  85. procedure TCustomAffineLayer.BitmapChanged(Sender: TObject);
  86. begin
  87. Transformation.SrcRect := FloatRect(Bitmap.BoundsRect);
  88. Changed;
  89. end;
  90. constructor TCustomAffineLayer.Create(ALayerCollection: TLayerCollection);
  91. begin
  92. inherited;
  93. FBitmap := TBitmap32.Create;
  94. FBitmap.OnChange := BitmapChanged;
  95. FTransformation := TAffineTransformation.Create;
  96. end;
  97. destructor TCustomAffineLayer.Destroy;
  98. begin
  99. FTransformation.Free;
  100. FBitmap.Free;
  101. inherited;
  102. end;
  103. function TCustomAffineLayer.DoHitTest(X, Y: Integer): Boolean;
  104. var
  105. Pt: TPoint;
  106. begin
  107. Result := False;
  108. with TATAccess(Transformation) do
  109. Pt := ReverseTransform(GR32.Point(X, Y));
  110. if GR32.PtInRect(Rect(0, 0, Bitmap.Width, Bitmap.Height), Pt) then
  111. Result := True;
  112. if Result and AlphaHit and (Bitmap.PixelS[Pt.X, Pt.Y] and $FF000000 = 0) then
  113. Result := False;
  114. end;
  115. procedure TCustomAffineLayer.Paint(Buffer: TBitmap32);
  116. begin
  117. AdjustTransformation;
  118. Transform(Buffer, FBitmap, Transformation);
  119. end;
  120. procedure TCustomAffineLayer.SetBitmap(Value: TBitmap32);
  121. begin
  122. FBitmap.Assign(Value);
  123. end;
  124. { TRotLayer }
  125. procedure TRotLayer.AdjustTransformation;
  126. var
  127. ScaleX, ScaleY,
  128. ShiftX, ShiftY: Single;
  129. begin
  130. Transformation.Clear;
  131. Transformation.Translate(-BitmapCenter.X, -BitmapCenter.Y);
  132. Transformation.Rotate(0, 0, Angle);
  133. Transformation.Translate(Position.X, Position.Y);
  134. if Scaled and Assigned(LayerCollection) then
  135. with LayerCollection do
  136. begin
  137. GetViewportScale(ScaleX, ScaleY);
  138. GetViewportShift(ShiftX, ShiftY);
  139. Transformation.Scale(ScaleX, ScaleY);
  140. Transformation.Translate(ShiftX, ShiftY);
  141. end;
  142. end;
  143. procedure TRotLayer.SetAngle(Value: Single);
  144. begin
  145. FAngle := Value;
  146. Changed;
  147. end;
  148. procedure TRotLayer.SetBitmapCenter(const Value: TFloatPoint);
  149. begin
  150. FBitmapCenter := Value;
  151. Changed;
  152. end;
  153. procedure TRotLayer.SetPosition(const Value: TFloatPoint);
  154. begin
  155. FPosition := Value;
  156. Changed;
  157. end;
  158. procedure TRotLayer.SetScaled(Value: Boolean);
  159. begin
  160. FScaled := Value;
  161. Changed;
  162. end;
  163. end.