source: trunk/Packages/CevoComponents/GraphicSet.pas

Last change on this file was 554, checked in by chronos, 3 weeks ago
  • Added: TButtonG class as a button class component referencing TGraphicSet item.
  • Modified: Code cleanup.
File size: 6.5 KB
Line 
1unit GraphicSet;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, LCLType, DOM,
7 XMLRead, XMLWrite, XML,
8 {$IFDEF DPI}Dpi.Graphics{$ELSE}
9 Graphics{$ENDIF};
10
11type
12 TGraphicSet = class;
13
14 { TGraphicSetItem }
15
16 TGraphicSetItem = class
17 private
18 function GetBoundsRect: TRect;
19 procedure SetBoundsRect(AValue: TRect);
20 public
21 Name: string;
22 Left: Integer;
23 Top: Integer;
24 Width: Integer;
25 Height: Integer;
26 GraphicSet: TGraphicSet;
27 procedure DrawTo(Canvas: TCanvas; Pos: TPoint);
28 procedure LoadFromNode(Node: TDOMNode);
29 procedure SaveToNode(Node: TDOMNode);
30 property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
31 end;
32
33 { TGraphicSetItems }
34
35 TGraphicSetItems = class(TObjectList<TGraphicSetItem>)
36 GraphicSet: TGraphicSet;
37 function SearchByName(Name: string): TGraphicSetItem;
38 function AddNew(Name: string): TGraphicSetItem;
39 procedure LoadFromNode(Node: TDOMNode);
40 procedure SaveToNode(Node: TDOMNode);
41 end;
42
43 { TGraphicSet }
44
45 TGraphicSet = class(TComponent)
46 public
47 Name: string;
48 Data: TBitmap;
49 Mask: TBitmap;
50 PixUsed: array of Byte;
51 Items: TGraphicSetItems;
52 procedure ResetPixUsed;
53 function GetItem(ItemName: string): TGraphicSetItem;
54 procedure LoadFromFile(FileName: string);
55 procedure SaveToFile(FileName: string);
56 constructor Create(AOwner: TComponent); override;
57 destructor Destroy; override;
58 end;
59
60 TGraphicSetClass = class of TGraphicSet;
61
62 { TGraphicSets }
63
64 TGraphicSets = class(TObjectList<TGraphicSet>)
65 function SearchByName(Name: string): TGraphicSet;
66 function AddNew(Name: string): TGraphicSet;
67 procedure ResetPixUsed;
68 end;
69
70const
71 GraphicSetFileRootNode = 'GraphicSet';
72 GraphicSetFileExt = '.grs';
73
74procedure Register;
75
76
77implementation
78
79resourcestring
80 SWrongFileFormat = 'Wrong file format.';
81 SGraphicItemNotFound = 'Graphic item %s not found in graphic set %s.';
82
83procedure Register;
84begin
85 RegisterComponents('C-evo', [TGraphicSet]);
86end;
87
88{ TGraphicSetItem }
89
90function TGraphicSetItem.GetBoundsRect: TRect;
91begin
92 Result := Bounds(Left, Top, Width, Height);
93end;
94
95procedure TGraphicSetItem.SetBoundsRect(AValue: TRect);
96begin
97 Left := AValue.Left;
98 Top := AValue.Top;
99 Width := AValue.Width;
100 Height := AValue.Height;
101end;
102
103procedure TGraphicSetItem.DrawTo(Canvas: TCanvas; Pos: TPoint);
104begin
105{ BitBltCanvas(Canvas, Pos.X, Pos.Y, BoundsRect.Width, BoundsRect.Height,
106 GraphicSet.Mask.Canvas, BoundsRect.Left, BoundsRect.Top, SRCAND);
107 BitBltCanvas(Canvas, Pos.X, Pos.Y, BoundsRect.Width, BoundsRect.Height,
108 GraphicSet.Data.Canvas, BoundsRect.Left, BoundsRect.Top, SRCPAINT);
109}
110end;
111
112procedure TGraphicSetItem.LoadFromNode(Node: TDOMNode);
113begin
114 Name := ReadString(Node, 'Name', Name);
115 Left := ReadInteger(Node, 'Left', Left);
116 Top := ReadInteger(Node, 'Top', Top);
117 Width := ReadInteger(Node, 'Width', Width);
118 Height := ReadInteger(Node, 'Height', Height);
119end;
120
121procedure TGraphicSetItem.SaveToNode(Node: TDOMNode);
122begin
123 WriteString(Node, 'Name', Name);
124 WriteInteger(Node, 'Left', Left);
125 WriteInteger(Node, 'Top', Top);
126 WriteInteger(Node, 'Width', Width);
127 WriteInteger(Node, 'Height', Height);
128end;
129
130{ TGraphicSetItems }
131
132function TGraphicSetItems.SearchByName(Name: string): TGraphicSetItem;
133var
134 I: Integer;
135begin
136 I := 0;
137 while (I < Count) and (Items[I].Name <> Name) do Inc(I);
138 if I < Count then Result := Items[I]
139 else Result := nil;
140end;
141
142function TGraphicSetItems.AddNew(Name: string): TGraphicSetItem;
143begin
144 Result := TGraphicSetItem.Create;
145 Result.Name := Name;
146 Add(Result);
147end;
148
149procedure TGraphicSetItems.LoadFromNode(Node: TDOMNode);
150var
151 Node2: TDOMNode;
152 NewItem: TGraphicSetItem;
153begin
154 Count := 0;
155 Node2 := Node.FirstChild;
156 while Assigned(Node2) and (Node2.NodeName = 'Item') do begin
157 NewItem := TGraphicSetItem.Create;
158 NewItem.GraphicSet := GraphicSet;
159 NewItem.LoadFromNode(Node2);
160 Add(NewItem);
161 Node2 := Node2.NextSibling;
162 end;
163end;
164
165procedure TGraphicSetItems.SaveToNode(Node: TDOMNode);
166var
167 I: Integer;
168 NewNode: TDOMNode;
169begin
170 for I := 0 to Count - 1 do begin;
171 NewNode := Node.OwnerDocument.CreateElement('Item');
172 Node.AppendChild(NewNode);
173 Items[I].SaveToNode(NewNode);
174 end;
175end;
176
177{ TGraphicSet }
178
179procedure TGraphicSet.ResetPixUsed;
180begin
181 SetLength(PixUsed, Data.Height div 49 * 10);
182 if Length(PixUsed) > 0 then
183 FillChar(PixUsed[0], Length(PixUsed), 0);
184end;
185
186function TGraphicSet.GetItem(ItemName: string): TGraphicSetItem;
187begin
188 Result := Items.SearchByName(ItemName);
189 if not Assigned(Result) then
190 raise Exception.Create(Format(SGraphicItemNotFound, [ItemName, Name]));
191end;
192
193procedure TGraphicSet.LoadFromFile(FileName: string);
194var
195 Doc: TXMLDocument;
196 RootNode: TDOMNode;
197 NewNode: TDOMNode;
198begin
199 ReadXMLFile(Doc, FileName);
200 with Doc do
201 try
202 if DocumentElement.NodeName <> GraphicSetFileRootNode then
203 raise Exception.Create(SWrongFileFormat);
204 RootNode := Doc.DocumentElement;
205 with RootNode do begin
206 NewNode := FindNode('Items');
207 if Assigned(NewNode) then
208 Items.LoadFromNode(NewNode);
209 end;
210 finally
211 FreeAndNil(Doc);
212 end;
213end;
214
215procedure TGraphicSet.SaveToFile(FileName: string);
216var
217 NewNode: TDOMNode;
218 Doc: TXMLDocument;
219 RootNode: TDOMNode;
220begin
221 Doc := TXMLDocument.Create;
222 with Doc do
223 try
224 RootNode := CreateElement(GraphicSetFileRootNode);
225 AppendChild(RootNode);
226 with RootNode do begin
227 NewNode := OwnerDocument.CreateElement('Items');
228 AppendChild(NewNode);
229 Items.SaveToNode(NewNode);
230 end;
231 WriteXMLFile(Doc, FileName);
232 finally
233 FreeAndNil(Doc);
234 end;
235end;
236
237constructor TGraphicSet.Create(AOwner: TComponent);
238begin
239 inherited;
240 Data := TBitmap.Create;
241 Data.PixelFormat := TPixelFormat.pf24bit;
242 Mask := TBitmap.Create;
243 Mask.PixelFormat := TPixelFormat.pf24bit;
244 Items := TGraphicSetItems.Create;
245 Items.GraphicSet := Self;
246end;
247
248destructor TGraphicSet.Destroy;
249begin
250 FreeAndNil(Items);
251 FreeAndNil(Data);
252 FreeAndNil(Mask);
253 inherited;
254end;
255
256{ TGraphicSets }
257
258function TGraphicSets.SearchByName(Name: string): TGraphicSet;
259var
260 I: Integer;
261begin
262 I := 0;
263 while (I < Count) and (Items[I].Name <> Name) do Inc(I);
264 if I < Count then Result := Items[I]
265 else Result := nil;
266end;
267
268function TGraphicSets.AddNew(Name: string): TGraphicSet;
269begin
270 Result := TGraphicSet.Create(nil);
271 Result.Name := Name;
272 Add(Result);
273end;
274
275procedure TGraphicSets.ResetPixUsed;
276var
277 I: Integer;
278begin
279 for I := 0 to Count - 1 do
280 Items[I].ResetPixUsed;
281end;
282
283end.
284
285
Note: See TracBrowser for help on using the repository browser.