home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 November / CDVD1105.ISO / Software / Freeware / programare / graphics32 / Examples / Clx / RotLayer_Ex / GR32_RotLayer.pas next >
Encoding:
Pascal/Delphi Source File  |  2004-07-13  |  4.9 KB  |  187 lines

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