1 | unit UGraphicSet;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Graphics, Generics.Collections, LCLType, UPixelPointer, DOM,
|
---|
7 | XMLRead, XMLWrite, UXMLUtils;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
67 | const
|
---|
68 | GraphicSetFileRootNode = 'GraphicSet';
|
---|
69 | GraphicSetFileExt = '.grs';
|
---|
70 |
|
---|
71 |
|
---|
72 | implementation
|
---|
73 |
|
---|
74 | resourcestring
|
---|
75 | SWrongFileFormat = 'Wrong file format.';
|
---|
76 | SGraphicItemNotFound = 'Graphic item %s not found in graphic set %s.';
|
---|
77 |
|
---|
78 | { TGraphicSetItem }
|
---|
79 |
|
---|
80 | function TGraphicSetItem.GetBoundsRect: TRect;
|
---|
81 | begin
|
---|
82 | Result := Bounds(Left, Top, Width, Height);
|
---|
83 | end;
|
---|
84 |
|
---|
85 | procedure TGraphicSetItem.SetBoundsRect(AValue: TRect);
|
---|
86 | begin
|
---|
87 | Left := AValue.Left;
|
---|
88 | Top := AValue.Top;
|
---|
89 | Width := AValue.Width;
|
---|
90 | Height := AValue.Height;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | procedure TGraphicSetItem.DrawTo(Canvas: TCanvas; Pos: TPoint);
|
---|
94 | begin
|
---|
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 | }
|
---|
100 | end;
|
---|
101 |
|
---|
102 | procedure TGraphicSetItem.LoadFromNode(Node: TDOMNode);
|
---|
103 | begin
|
---|
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);
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TGraphicSetItem.SaveToNode(Node: TDOMNode);
|
---|
112 | begin
|
---|
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);
|
---|
118 | end;
|
---|
119 |
|
---|
120 | { TGraphicSetItems }
|
---|
121 |
|
---|
122 | function TGraphicSetItems.SearchByName(Name: string): TGraphicSetItem;
|
---|
123 | var
|
---|
124 | I: Integer;
|
---|
125 | begin
|
---|
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;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | function TGraphicSetItems.AddNew(Name: string): TGraphicSetItem;
|
---|
133 | begin
|
---|
134 | Result := TGraphicSetItem.Create;
|
---|
135 | Result.Name := Name;
|
---|
136 | Add(Result);
|
---|
137 | end;
|
---|
138 |
|
---|
139 | procedure TGraphicSetItems.LoadFromNode(Node: TDOMNode);
|
---|
140 | var
|
---|
141 | Node2: TDOMNode;
|
---|
142 | NewItem: TGraphicSetItem;
|
---|
143 | begin
|
---|
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;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TGraphicSetItems.SaveToNode(Node: TDOMNode);
|
---|
156 | var
|
---|
157 | I: Integer;
|
---|
158 | NewNode: TDOMNode;
|
---|
159 | begin
|
---|
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;
|
---|
165 | end;
|
---|
166 |
|
---|
167 | { TGraphicSet }
|
---|
168 |
|
---|
169 | procedure TGraphicSet.ResetPixUsed;
|
---|
170 | begin
|
---|
171 | SetLength(pixUsed, Data.Height div 49 * 10);
|
---|
172 | if Length(pixUsed) > 0 then
|
---|
173 | FillChar(pixUsed[0], Length(pixUsed), 0);
|
---|
174 | end;
|
---|
175 |
|
---|
176 | function TGraphicSet.GetItem(ItemName: string): TGraphicSetItem;
|
---|
177 | begin
|
---|
178 | Result := Items.SearchByName(ItemName);
|
---|
179 | if not Assigned(Result) then
|
---|
180 | raise Exception.Create(Format(SGraphicItemNotFound, [ItemName, Name]));
|
---|
181 | end;
|
---|
182 |
|
---|
183 | procedure TGraphicSet.LoadFromFile(FileName: string);
|
---|
184 | var
|
---|
185 | Doc: TXMLDocument;
|
---|
186 | RootNode: TDOMNode;
|
---|
187 | NewNode: TDOMNode;
|
---|
188 | begin
|
---|
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;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | procedure TGraphicSet.SaveToFile(FileName: string);
|
---|
206 | var
|
---|
207 | NewNode: TDOMNode;
|
---|
208 | Doc: TXMLDocument;
|
---|
209 | RootNode: TDOMNode;
|
---|
210 | begin
|
---|
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;
|
---|
225 | end;
|
---|
226 |
|
---|
227 | constructor TGraphicSet.Create;
|
---|
228 | begin
|
---|
229 | Data := TBitmap.Create;
|
---|
230 | Data.PixelFormat := pf24bit;
|
---|
231 | Mask := TBitmap.Create;
|
---|
232 | Mask.PixelFormat := pf24bit;
|
---|
233 | Items := TGraphicSetItems.Create;
|
---|
234 | Items.GraphicSet := Self;
|
---|
235 | end;
|
---|
236 |
|
---|
237 | destructor TGraphicSet.Destroy;
|
---|
238 | begin
|
---|
239 | FreeAndNil(Items);
|
---|
240 | FreeAndNil(Data);
|
---|
241 | FreeAndNil(Mask);
|
---|
242 | inherited;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | { TGraphicSets }
|
---|
246 |
|
---|
247 | function TGraphicSets.SearchByName(Name: string): TGraphicSet;
|
---|
248 | var
|
---|
249 | I: Integer;
|
---|
250 | begin
|
---|
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;
|
---|
255 | end;
|
---|
256 |
|
---|
257 | function TGraphicSets.AddNew(Name: string): TGraphicSet;
|
---|
258 | begin
|
---|
259 | Result := TGraphicSet.Create;
|
---|
260 | Result.Name := Name;
|
---|
261 | Add(Result);
|
---|
262 | end;
|
---|
263 |
|
---|
264 | procedure TGraphicSets.ResetPixUsed;
|
---|
265 | var
|
---|
266 | I: Integer;
|
---|
267 | begin
|
---|
268 | for I := 0 to Count - 1 do
|
---|
269 | Items[I].ResetPixUsed;
|
---|
270 | end;
|
---|
271 |
|
---|
272 | end.
|
---|
273 |
|
---|