source: tags/1.3.1/Packages/CevoComponents/UGraphicSet.pas

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