djjd47130

Delphi Template Image Wrapper

Aug 31st, 2014
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.78 KB | None | 0 0
  1. unit TemplateImg;
  2.  
  3. (*
  4.   Template Image Component
  5.   by Jerry Dodge
  6.  
  7.   Use the TTemplateImage component to encapsulate a single image
  8.   to be used as a style template for various controls.
  9.  
  10.   Component can be installed into Delphi and designed in the IDE.
  11.  
  12.   Image is presumed to have a transparent color for a background
  13.   matching that of the component's TransparentColor property.
  14.  
  15.   Load a template image using one of the LoadImage overloads.
  16.  
  17.   Regions of the image are accessible by Rect references by name.
  18.  
  19.   Use the Elements property to populate each possible region
  20.   assigning both a unique name and rectangle bounds.
  21.  
  22.   Draw region on another canvas using one of the GetTemplate overloads.
  23. *)
  24.  
  25. interface
  26.  
  27. uses
  28.   Winapi.Windows, Winapi.Messages,
  29.   System.SysUtils, System.Variants, System.Classes,
  30.   Vcl.Graphics, Jpeg, Gif, PngImage;
  31.  
  32. type
  33.   TTemplateItem = class(TCollectionItem)
  34.   private
  35.     FLeft: Integer;
  36.     FTop: Integer;
  37.     FRight: Integer;
  38.     FBottom: Integer;
  39.     FName: String;
  40.     function GetHeight: Integer;
  41.     function GetWidth: Integer;
  42.     procedure SetHeight(const Value: Integer);
  43.     procedure SetWidth(const Value: Integer);
  44.   protected
  45.     function GetDisplayName: String; override;
  46.   public
  47.     constructor Create(ACollection: TCollection);
  48.     function Bounds: TRect;
  49.     procedure SetBounds(R: TRect); overload;
  50.     procedure SetBounds(ALeft, ATop, ARight, ABottom: Integer); overload;
  51.   published
  52.     property Name: String read FName write FName;
  53.     property Left: Integer read FLeft write FLeft;
  54.     property Top: Integer read FTop write FTop;
  55.     property Right: Integer read FRight write FRight;
  56.     property Bottom: Integer read FBottom write FBottom;
  57.     property Width: Integer read GetWidth write SetWidth;
  58.     property Height: Integer read GetHeight write SetHeight;
  59.   end;
  60.  
  61.   TTemplateItems = class(TOwnedCollection)
  62.   private
  63.     function GetItem(Index: Integer): TTemplateItem;
  64.     procedure SetItem(Index: Integer; const Value: TTemplateItem);
  65.     function GetByName(Name: String): TTemplateItem;
  66.     procedure SetByName(Name: String; const Value: TTemplateItem);
  67.   public
  68.     constructor Create(AOwner: TPersistent);
  69.     function Add: TTemplateItem;
  70.     function Insert(Index: Integer): TTemplateItem;
  71.     procedure SaveToFile(const Filename: String);
  72.     procedure LoadFromFile(const Filename: String);
  73.     property Items[Index: Integer]: TTemplateItem read GetItem write SetItem; default;
  74.     property ByName[Name: String]: TTemplateItem read GetByName write SetByName;
  75.   end;
  76.  
  77.   TTemplateImage = class(TComponent)
  78.   private
  79.     FElements: TTemplateItems;
  80.     FBmp: TBitmap;
  81.     FTransparentColor: TColor;
  82.     procedure SetTransparentColor(const Value: TColor);
  83.     procedure SetElements(const Value: TTemplateItems);
  84.   public
  85.     constructor Create(AOwner: TComponent); override;
  86.     destructor Destroy; override;
  87.     procedure Reset;
  88.     procedure LoadImage(const Filename: String); overload;
  89.     procedure LoadImage(Stream: TStream); overload;
  90.     procedure GetTemplate(const Name: String; Dest: TCanvas; const X, Y: Integer); overload;
  91.     procedure GetTemplate(const Name: String; Dest: TCanvas; const R: TRect); overload;
  92.     function GetRect(const Name: String): TRect;
  93.   published
  94.     property TransparentColor: TColor
  95.       read FTransparentColor write SetTransparentColor;
  96.     property Elements: TTemplateItems read FElements write SetElements;
  97.   end;
  98.  
  99.  
  100. implementation
  101.  
  102. function RectToStr(const R: TRect): String;
  103. begin
  104.   Result:= IntToStr(R.Left);
  105.   Result:= Result + ',' + IntToStr(R.Top);
  106.   Result:= Result + ',' + IntToStr(R.Right);
  107.   Result:= Result + ',' + IntToStr(R.Bottom);
  108. end;
  109.  
  110. function StrToRect(const R: String): TRect;
  111. var
  112.   S, T: String;
  113.   P: Integer;
  114. begin
  115.   S:= R;
  116.  
  117.   P:= Pos(',', S);
  118.   T:= Copy(S, 1, P-1);
  119.   Delete(S, 1, P);
  120.   Result.Left:= StrToIntDef(T, 0);
  121.  
  122.   P:= Pos(',', S);
  123.   T:= Copy(S, 1, P-1);
  124.   Delete(S, 1, P);
  125.   Result.Top:= StrToIntDef(T, 0);
  126.  
  127.   P:= Pos(',', S);
  128.   T:= Copy(S, 1, P-1);
  129.   Delete(S, 1, P);
  130.   Result.Right:= StrToIntDef(T, 0);
  131.  
  132.   Result.Bottom:= StrToIntDef(S, 0);
  133. end;
  134.  
  135. { TTemplateItem }
  136.  
  137. function TTemplateItem.Bounds: TRect;
  138. begin
  139.   Result:= Rect(FLeft, FTop, FRight, FBottom);
  140. end;
  141.  
  142. constructor TTemplateItem.Create(ACollection: TCollection);
  143. begin
  144.   inherited Create(ACollection);
  145.  
  146. end;
  147.  
  148. function TTemplateItem.GetDisplayName: String;
  149. begin
  150.   Result:= FName;
  151. end;
  152.  
  153. function TTemplateItem.GetHeight: Integer;
  154. begin
  155.   Result:= FBottom - FTop;
  156. end;
  157.  
  158. function TTemplateItem.GetWidth: Integer;
  159. begin
  160.   Result:= FRight - FLeft;
  161. end;
  162.  
  163. procedure TTemplateItem.SetBounds(R: TRect);
  164. begin
  165.   FLeft:= R.Left;
  166.   FTop:= R.Top;
  167.   FRight:= R.Right;
  168.   FBottom:= R.Bottom;
  169. end;
  170.  
  171. procedure TTemplateItem.SetBounds(ALeft, ATop, ARight, ABottom: Integer);
  172. begin
  173.   FLeft:= ALeft;
  174.   FTop:= ATop;
  175.   FRight:= ARight;
  176.   FBottom:= ABottom;
  177. end;
  178.  
  179. procedure TTemplateItem.SetHeight(const Value: Integer);
  180. begin
  181.   FBottom:= FTop + Value;
  182. end;
  183.  
  184. procedure TTemplateItem.SetWidth(const Value: Integer);
  185. begin
  186.   FRight:= FLeft + Value;
  187. end;
  188.  
  189. { TTemplateItems }
  190.  
  191. constructor TTemplateItems.Create(AOwner: TPersistent);
  192. begin
  193.   inherited Create(AOwner, TTemplateItem);
  194.  
  195. end;
  196.  
  197. function TTemplateItems.Add: TTemplateItem;
  198. begin
  199.   Result:= TTemplateItem(inherited Add);
  200. end;
  201.  
  202. function TTemplateItems.GetByName(Name: String): TTemplateItem;
  203. var
  204.   X: Integer;
  205. begin
  206.   for X := 0 to Count-1 do begin
  207.     if SameText(Items[X].Name, Name) then begin
  208.       Result:= Items[X];
  209.       Break;
  210.     end;
  211.   end;
  212. end;
  213.  
  214. function TTemplateItems.GetItem(Index: Integer): TTemplateItem;
  215. begin
  216.   Result:= TTemplateItem(inherited GetItem(Index));
  217. end;
  218.  
  219. function TTemplateItems.Insert(Index: Integer): TTemplateItem;
  220. begin
  221.   Result:= TTemplateItem(inherited Insert(Index));
  222. end;
  223.  
  224. procedure TTemplateItems.LoadFromFile(const Filename: String);
  225. var
  226.   L: TStringList;
  227.   X: Integer;
  228.   I: TTemplateItem;
  229.   N: String;
  230. begin
  231.   L:= TStringList.Create;
  232.   try
  233.     Clear;
  234.     L.LoadFromFile(Filename);
  235.     for X := 0 to L.Count-1 do begin
  236.       I:= Add;
  237.       N:= L.Names[X];
  238.       I.Name:= N;
  239.       I.SetBounds(StrToRect(L.Values[N]));
  240.     end;
  241.   finally
  242.     L.Free;
  243.   end;
  244. end;
  245.  
  246. procedure TTemplateItems.SaveToFile(const Filename: String);
  247. var
  248.   L: TStringList;
  249.   X: Integer;
  250. begin
  251.   L:= TStringList.Create;
  252.   try
  253.     for X := 0 to Count - 1 do
  254.       L.Values[Items[X].Name]:= RectToStr(Items[X].Bounds);
  255.     L.SaveToFile(Filename);
  256.   finally
  257.     L.Free;
  258.   end;
  259. end;
  260.  
  261. procedure TTemplateItems.SetByName(Name: String; const Value: TTemplateItem);
  262. var
  263.   X: Integer;
  264. begin
  265.   for X := 0 to Count-1 do begin
  266.     if SameText(Items[X].Name, Name) then begin
  267.       Items[X]:= Value;
  268.       Break;
  269.     end;
  270.   end;
  271. end;
  272.  
  273. procedure TTemplateItems.SetItem(Index: Integer; const Value: TTemplateItem);
  274. begin
  275.   inherited SetItem(Index, Value);
  276. end;
  277.  
  278. { TTemplateImage }
  279.  
  280. constructor TTemplateImage.Create(AOwner: TComponent);
  281. begin
  282.   inherited;
  283.   FElements:= TTemplateItems.Create(Self);
  284.   FBmp:= TBitmap.Create;
  285.   FBmp.Transparent:= True;
  286.   TransparentColor:= clFuchsia;
  287. end;
  288.  
  289. destructor TTemplateImage.Destroy;
  290. begin
  291.   FBmp.Free;
  292.   FElements.Free;
  293.   inherited;
  294. end;
  295.  
  296. function TTemplateImage.GetRect(const Name: String): TRect;
  297. var
  298.   T: TTemplateItem;
  299. begin
  300.   T:= FElements.ByName[Name];
  301.   if Assigned(T) then begin
  302.     Result:= T.Bounds;
  303.   end;
  304. end;
  305.  
  306. procedure TTemplateImage.GetTemplate(const Name: String; Dest: TCanvas;
  307.   const R: TRect);
  308. var
  309.   T: TTemplateItem;
  310. begin
  311.   T:= FElements.ByName[Name];
  312.   if Assigned(T) then begin
  313.     Dest.CopyRect(R, FBmp.Canvas, T.Bounds);
  314.   end;
  315. end;
  316.  
  317. procedure TTemplateImage.GetTemplate(const Name: String; Dest: TCanvas; const X, Y: Integer);
  318. var
  319.   T: TTemplateItem;
  320.   DR: TRect;
  321. begin
  322.   T:= FElements.ByName[Name];
  323.   if Assigned(T) then begin
  324.     DR.Left:= X;
  325.     DR.Top:= Y;
  326.     DR.Width:= T.Width;
  327.     DR.Height:= T.Height;
  328.     Dest.CopyRect(DR, FBmp.Canvas, T.Bounds);
  329.   end;
  330. end;
  331.  
  332. procedure TTemplateImage.LoadImage(Stream: TStream);
  333. var
  334.   I: TWicImage;
  335. begin
  336.   I:= TWicImage.Create;
  337.   try
  338.     Stream.Position:= 0;
  339.     I.LoadFromStream(Stream);
  340.     FBmp.Assign(I);
  341.   finally
  342.     I.Free;
  343.   end;
  344. end;
  345.  
  346. procedure TTemplateImage.Reset;
  347. begin
  348.   FBmp.TransparentColor:= FTransparentColor;
  349. end;
  350.  
  351. procedure TTemplateImage.LoadImage(const Filename: String);
  352. var
  353.   I: TWicImage;
  354. begin
  355.   I:= TWicImage.Create;
  356.   try
  357.     I.LoadFromFile(Filename);
  358.     FBmp.Assign(I);
  359.   finally
  360.     I.Free;
  361.   end;
  362. end;
  363.  
  364. procedure TTemplateImage.SetElements(const Value: TTemplateItems);
  365. begin
  366.   FElements.Assign(Value);
  367. end;
  368.  
  369. procedure TTemplateImage.SetTransparentColor(const Value: TColor);
  370. begin
  371.   FTransparentColor:= Value;
  372.   FBmp.TransparentColor:= Value;
  373. end;
  374.  
  375. end.
Advertisement
Add Comment
Please, Sign In to add comment