MainUnit.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. unit MainUnit;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  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. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Rotation Layer Example
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2005
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Michael Hansen <[email protected]>
  32. * - 2007/03/02 - pamTransparentEdge setup, minor GUI changes, image loading
  33. *
  34. * ***** END LICENSE BLOCK ***** *)
  35. interface
  36. {$I GR32.inc}
  37. uses
  38. {$IFNDEF FPC} Windows, {$ELSE} LCLIntf, LResources, Variants, {$ENDIF}
  39. SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, GR32,
  40. GR32_Image, GR32_RotLayer, GR32_Transforms, GR32_RangeBars, GR32_Resamplers;
  41. type
  42. TFormRotLayer = class(TForm)
  43. CbxScaled: TCheckBox;
  44. GbrAngle: TGaugeBar;
  45. GbrPositionX: TGaugeBar;
  46. GbrPositionY: TGaugeBar;
  47. GbrScale: TGaugeBar;
  48. ImgView: TImgView32;
  49. LblAngle: TLabel;
  50. LblPositionX: TLabel;
  51. LblPositionY: TLabel;
  52. LblScale: TLabel;
  53. procedure FormCreate(Sender: TObject);
  54. procedure GbrAngleChange(Sender: TObject);
  55. procedure GbrPositionChange(Sender: TObject);
  56. procedure GbrScaleChange(Sender: TObject);
  57. procedure CbxScaledClick(Sender: TObject);
  58. public
  59. L: TRotLayer;
  60. end;
  61. var
  62. FormRotLayer: TFormRotLayer;
  63. implementation
  64. {$IFDEF FPC}
  65. {$R *.lfm}
  66. {$ELSE}
  67. {$R *.dfm}
  68. {$ENDIF}
  69. uses
  70. {$IFDEF Darwin}
  71. MacOSAll,
  72. {$ENDIF}
  73. {$IFNDEF FPC}
  74. JPEG,
  75. {$ELSE}
  76. LazJPG,
  77. {$ENDIF}
  78. Math;
  79. { TFormRotLayer }
  80. procedure TFormRotLayer.FormCreate(Sender: TObject);
  81. var
  82. ResStream: TResourceStream;
  83. JPEG: TJPEGImage;
  84. begin
  85. // load example image
  86. JPEG := TJPEGImage.Create;
  87. try
  88. ResStream := TResourceStream.Create(HInstance, 'Delphi', RT_RCDATA);
  89. try
  90. JPEG.LoadFromStream(ResStream);
  91. finally
  92. ResStream.Free;
  93. end;
  94. ImgView.Bitmap.Assign(JPEG);
  95. finally
  96. JPEG.Free;
  97. end;
  98. L := TRotLayer.Create(ImgView.Layers);
  99. L.Bitmap := TBitmap32.Create;
  100. with L.Bitmap do
  101. begin
  102. BeginUpdate;
  103. L.Bitmap.LoadFromResourceName(HInstance, 'SpriteTexture');
  104. TLinearResampler.Create(L.Bitmap);
  105. //ensure good looking edge, dynamic alternative to SetBorderTransparent
  106. TCustomResampler(L.Bitmap.Resampler).PixelAccessMode := pamTransparentEdge;
  107. L.BitmapCenter := FloatPoint(Width * 0.5, Height * 0.5);
  108. MasterAlpha := 200;
  109. FrameRectS(BoundsRect, $FFFFFFFF);
  110. DrawMode := dmBlend;
  111. EndUpdate;
  112. Changed;
  113. end;
  114. L.Scaled := True;
  115. L.Position := FloatPoint(100, 100);
  116. end;
  117. procedure TFormRotLayer.GbrAngleChange(Sender: TObject);
  118. begin
  119. L.Angle := GbrAngle.Position;
  120. end;
  121. procedure TFormRotLayer.GbrPositionChange(Sender: TObject);
  122. var
  123. P: TFloatPoint;
  124. begin
  125. P := L.Position;
  126. P.X := GbrPositionX.Position;
  127. P.Y := GbrPositionY.Position;
  128. L.Position := P;
  129. end;
  130. procedure TFormRotLayer.GbrScaleChange(Sender: TObject);
  131. begin
  132. ImgView.Scale := Power(10, GbrScale.Position * 0.01);
  133. end;
  134. procedure TFormRotLayer.CbxScaledClick(Sender: TObject);
  135. begin
  136. L.Scaled := not L.Scaled;
  137. end;
  138. end.