File: UGraphicSet.pas

package info (click to toggle)
c-evo-dh 3.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 10,548 kB
  • sloc: pascal: 57,426; xml: 256; makefile: 114; sh: 4
file content (273 lines) | stat: -rw-r--r-- 6,383 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
unit UGraphicSet;

interface

uses
  Classes, SysUtils, Graphics, Generics.Collections, LCLType, UPixelPointer, DOM,
  XMLRead, XMLWrite, UXMLUtils;

type
  TGraphicSet = class;

  { TGraphicSetItem }

  TGraphicSetItem = class
  private
    function GetBoundsRect: TRect;
    procedure SetBoundsRect(AValue: TRect);
  public
    Name: string;
    Left: Integer;
    Top: Integer;
    Width: Integer;
    Height: Integer;
    GraphicSet: TGraphicSet;
    procedure DrawTo(Canvas: TCanvas; Pos: TPoint);
    procedure LoadFromNode(Node: TDOMNode);
    procedure SaveToNode(Node: TDOMNode);
    property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
  end;

  { TGraphicSetItems }

  TGraphicSetItems = class(TObjectList<TGraphicSetItem>)
    GraphicSet: TGraphicSet;
    function SearchByName(Name: string): TGraphicSetItem;
    function AddNew(Name: string): TGraphicSetItem;
    procedure LoadFromNode(Node: TDOMNode);
    procedure SaveToNode(Node: TDOMNode);
  end;

  { TGraphicSet }

  TGraphicSet = class
    Name: string;
    Data: TBitmap;
    Mask: TBitmap;
    pixUsed: array of Byte;
    Items: TGraphicSetItems;
    procedure ResetPixUsed;
    function GetItem(ItemName: string): TGraphicSetItem;
    procedure LoadFromFile(FileName: string);
    procedure SaveToFile(FileName: string);
    constructor Create;
    destructor Destroy; override;
  end;

  TGraphicSetClass = class of TGraphicSet;

  { TGraphicSets }

  TGraphicSets = class(TObjectList<TGraphicSet>)
    function SearchByName(Name: string): TGraphicSet;
    function AddNew(Name: string): TGraphicSet;
    procedure ResetPixUsed;
  end;

const
  GraphicSetFileRootNode = 'GraphicSet';
  GraphicSetFileExt = '.grs';


implementation

resourcestring
  SWrongFileFormat = 'Wrong file format.';
  SGraphicItemNotFound = 'Graphic item %s not found in graphic set %s.';

{ TGraphicSetItem }

function TGraphicSetItem.GetBoundsRect: TRect;
begin
  Result := Bounds(Left, Top, Width, Height);
end;

procedure TGraphicSetItem.SetBoundsRect(AValue: TRect);
begin
  Left := AValue.Left;
  Top := AValue.Top;
  Width := AValue.Width;
  Height := AValue.Height;
end;

procedure TGraphicSetItem.DrawTo(Canvas: TCanvas; Pos: TPoint);
begin
{  BitBltCanvas(Canvas, Pos.X, Pos.Y, BoundsRect.Width, BoundsRect.Height,
    GraphicSet.Mask.Canvas, BoundsRect.Left, BoundsRect.Top, SRCAND);
  BitBltCanvas(Canvas, Pos.X, Pos.Y, BoundsRect.Width, BoundsRect.Height,
    GraphicSet.Data.Canvas, BoundsRect.Left, BoundsRect.Top, SRCPAINT);
}
end;

procedure TGraphicSetItem.LoadFromNode(Node: TDOMNode);
begin
  Name := ReadString(Node, 'Name', Name);
  Left := ReadInteger(Node, 'Left', Left);
  Top := ReadInteger(Node, 'Top', Top);
  Width := ReadInteger(Node, 'Width', Width);
  Height := ReadInteger(Node, 'Height', Height);
end;

procedure TGraphicSetItem.SaveToNode(Node: TDOMNode);
begin
  WriteString(Node, 'Name', Name);
  WriteInteger(Node, 'Left', Left);
  WriteInteger(Node, 'Top', Top);
  WriteInteger(Node, 'Width', Width);
  WriteInteger(Node, 'Height', Height);
end;

{ TGraphicSetItems }

function TGraphicSetItems.SearchByName(Name: string): TGraphicSetItem;
var
  I: Integer;
begin
  I := 0;
  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
  if I < Count then Result := Items[I]
    else Result := nil;
end;

function TGraphicSetItems.AddNew(Name: string): TGraphicSetItem;
begin
  Result := TGraphicSetItem.Create;
  Result.Name := Name;
  Add(Result);
end;

procedure TGraphicSetItems.LoadFromNode(Node: TDOMNode);
var
  Node2: TDOMNode;
  NewItem: TGraphicSetItem;
begin
  Count := 0;
  Node2 := Node.FirstChild;
  while Assigned(Node2) and (Node2.NodeName = 'Item') do begin
    NewItem := TGraphicSetItem.Create;
    NewItem.GraphicSet := GraphicSet;
    NewItem.LoadFromNode(Node2);
    Add(NewItem);
    Node2 := Node2.NextSibling;
  end;
end;

procedure TGraphicSetItems.SaveToNode(Node: TDOMNode);
var
  I: Integer;
  NewNode: TDOMNode;
begin
  for I := 0 to Count - 1 do begin;
    NewNode := Node.OwnerDocument.CreateElement('Item');
    Node.AppendChild(NewNode);
    Items[I].SaveToNode(NewNode);
  end;
end;

{ TGraphicSet }

procedure TGraphicSet.ResetPixUsed;
begin
  SetLength(pixUsed, Data.Height div 49 * 10);
  if Length(pixUsed) > 0 then
    FillChar(pixUsed[0], Length(pixUsed), 0);
end;

function TGraphicSet.GetItem(ItemName: string): TGraphicSetItem;
begin
  Result := Items.SearchByName(ItemName);
  if not Assigned(Result) then
    raise Exception.Create(Format(SGraphicItemNotFound, [ItemName, Name]));
end;

procedure TGraphicSet.LoadFromFile(FileName: string);
var
  Doc: TXMLDocument;
  RootNode: TDOMNode;
  NewNode: TDOMNode;
begin
  ReadXMLFile(Doc, FileName);
  with Doc do
  try
    if DocumentElement.NodeName <> GraphicSetFileRootNode then
      raise Exception.Create(SWrongFileFormat);
    RootNode := Doc.DocumentElement;
    with RootNode do begin
      NewNode := FindNode('Items');
      if Assigned(NewNode) then
        Items.LoadFromNode(NewNode);
    end;
  finally
    FreeAndNil(Doc);
  end;
end;

procedure TGraphicSet.SaveToFile(FileName: string);
var
  NewNode: TDOMNode;
  Doc: TXMLDocument;
  RootNode: TDOMNode;
begin
  Doc := TXMLDocument.Create;
  with Doc do
  try
    RootNode := CreateElement(GraphicSetFileRootNode);
    AppendChild(RootNode);
    with RootNode do begin
      NewNode := OwnerDocument.CreateElement('Items');
      AppendChild(NewNode);
      Items.SaveToNode(NewNode);
    end;
    WriteXMLFile(Doc, FileName);
  finally
    FreeAndNil(Doc);
  end;
end;

constructor TGraphicSet.Create;
begin
  Data := TBitmap.Create;
  Data.PixelFormat := pf24bit;
  Mask := TBitmap.Create;
  Mask.PixelFormat := pf24bit;
  Items := TGraphicSetItems.Create;
  Items.GraphicSet := Self;
end;

destructor TGraphicSet.Destroy;
begin
  FreeAndNil(Items);
  FreeAndNil(Data);
  FreeAndNil(Mask);
  inherited;
end;

{ TGraphicSets }

function TGraphicSets.SearchByName(Name: string): TGraphicSet;
var
  I: Integer;
begin
  I := 0;
  while (I < Count) and (Items[I].Name <> Name) do Inc(I);
  if I < Count then Result := Items[I]
    else Result := nil;
end;

function TGraphicSets.AddNew(Name: string): TGraphicSet;
begin
  Result := TGraphicSet.Create;
  Result.Name := Name;
  Add(Result);
end;

procedure TGraphicSets.ResetPixUsed;
var
  I: Integer;
begin
  for I := 0 to Count - 1 do
    Items[I].ResetPixUsed;
end;

end.