| 1 | unit GraphicSet;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Generics.Collections, LCLType, DOM,
|
|---|
| 7 | XMLRead, XMLWrite, XML,
|
|---|
| 8 | {$IFDEF DPI}Dpi.Graphics{$ELSE}
|
|---|
| 9 | Graphics{$ENDIF};
|
|---|
| 10 |
|
|---|
| 11 | type
|
|---|
| 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 |
|
|---|
| 70 | const
|
|---|
| 71 | GraphicSetFileRootNode = 'GraphicSet';
|
|---|
| 72 | GraphicSetFileExt = '.grs';
|
|---|
| 73 |
|
|---|
| 74 | procedure Register;
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | implementation
|
|---|
| 78 |
|
|---|
| 79 | resourcestring
|
|---|
| 80 | SWrongFileFormat = 'Wrong file format.';
|
|---|
| 81 | SGraphicItemNotFound = 'Graphic item %s not found in graphic set %s.';
|
|---|
| 82 |
|
|---|
| 83 | procedure Register;
|
|---|
| 84 | begin
|
|---|
| 85 | RegisterComponents('C-evo', [TGraphicSet]);
|
|---|
| 86 | end;
|
|---|
| 87 |
|
|---|
| 88 | { TGraphicSetItem }
|
|---|
| 89 |
|
|---|
| 90 | function TGraphicSetItem.GetBoundsRect: TRect;
|
|---|
| 91 | begin
|
|---|
| 92 | Result := Bounds(Left, Top, Width, Height);
|
|---|
| 93 | end;
|
|---|
| 94 |
|
|---|
| 95 | procedure TGraphicSetItem.SetBoundsRect(AValue: TRect);
|
|---|
| 96 | begin
|
|---|
| 97 | Left := AValue.Left;
|
|---|
| 98 | Top := AValue.Top;
|
|---|
| 99 | Width := AValue.Width;
|
|---|
| 100 | Height := AValue.Height;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | procedure TGraphicSetItem.DrawTo(Canvas: TCanvas; Pos: TPoint);
|
|---|
| 104 | begin
|
|---|
| 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 | }
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | procedure TGraphicSetItem.LoadFromNode(Node: TDOMNode);
|
|---|
| 113 | begin
|
|---|
| 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);
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | procedure TGraphicSetItem.SaveToNode(Node: TDOMNode);
|
|---|
| 122 | begin
|
|---|
| 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);
|
|---|
| 128 | end;
|
|---|
| 129 |
|
|---|
| 130 | { TGraphicSetItems }
|
|---|
| 131 |
|
|---|
| 132 | function TGraphicSetItems.SearchByName(Name: string): TGraphicSetItem;
|
|---|
| 133 | var
|
|---|
| 134 | I: Integer;
|
|---|
| 135 | begin
|
|---|
| 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;
|
|---|
| 140 | end;
|
|---|
| 141 |
|
|---|
| 142 | function TGraphicSetItems.AddNew(Name: string): TGraphicSetItem;
|
|---|
| 143 | begin
|
|---|
| 144 | Result := TGraphicSetItem.Create;
|
|---|
| 145 | Result.Name := Name;
|
|---|
| 146 | Add(Result);
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | procedure TGraphicSetItems.LoadFromNode(Node: TDOMNode);
|
|---|
| 150 | var
|
|---|
| 151 | Node2: TDOMNode;
|
|---|
| 152 | NewItem: TGraphicSetItem;
|
|---|
| 153 | begin
|
|---|
| 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;
|
|---|
| 163 | end;
|
|---|
| 164 |
|
|---|
| 165 | procedure TGraphicSetItems.SaveToNode(Node: TDOMNode);
|
|---|
| 166 | var
|
|---|
| 167 | I: Integer;
|
|---|
| 168 | NewNode: TDOMNode;
|
|---|
| 169 | begin
|
|---|
| 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;
|
|---|
| 175 | end;
|
|---|
| 176 |
|
|---|
| 177 | { TGraphicSet }
|
|---|
| 178 |
|
|---|
| 179 | procedure TGraphicSet.ResetPixUsed;
|
|---|
| 180 | begin
|
|---|
| 181 | SetLength(PixUsed, Data.Height div 49 * 10);
|
|---|
| 182 | if Length(PixUsed) > 0 then
|
|---|
| 183 | FillChar(PixUsed[0], Length(PixUsed), 0);
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | function TGraphicSet.GetItem(ItemName: string): TGraphicSetItem;
|
|---|
| 187 | begin
|
|---|
| 188 | Result := Items.SearchByName(ItemName);
|
|---|
| 189 | if not Assigned(Result) then
|
|---|
| 190 | raise Exception.Create(Format(SGraphicItemNotFound, [ItemName, Name]));
|
|---|
| 191 | end;
|
|---|
| 192 |
|
|---|
| 193 | procedure TGraphicSet.LoadFromFile(FileName: string);
|
|---|
| 194 | var
|
|---|
| 195 | Doc: TXMLDocument;
|
|---|
| 196 | RootNode: TDOMNode;
|
|---|
| 197 | NewNode: TDOMNode;
|
|---|
| 198 | begin
|
|---|
| 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;
|
|---|
| 213 | end;
|
|---|
| 214 |
|
|---|
| 215 | procedure TGraphicSet.SaveToFile(FileName: string);
|
|---|
| 216 | var
|
|---|
| 217 | NewNode: TDOMNode;
|
|---|
| 218 | Doc: TXMLDocument;
|
|---|
| 219 | RootNode: TDOMNode;
|
|---|
| 220 | begin
|
|---|
| 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;
|
|---|
| 235 | end;
|
|---|
| 236 |
|
|---|
| 237 | constructor TGraphicSet.Create(AOwner: TComponent);
|
|---|
| 238 | begin
|
|---|
| 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;
|
|---|
| 246 | end;
|
|---|
| 247 |
|
|---|
| 248 | destructor TGraphicSet.Destroy;
|
|---|
| 249 | begin
|
|---|
| 250 | FreeAndNil(Items);
|
|---|
| 251 | FreeAndNil(Data);
|
|---|
| 252 | FreeAndNil(Mask);
|
|---|
| 253 | inherited;
|
|---|
| 254 | end;
|
|---|
| 255 |
|
|---|
| 256 | { TGraphicSets }
|
|---|
| 257 |
|
|---|
| 258 | function TGraphicSets.SearchByName(Name: string): TGraphicSet;
|
|---|
| 259 | var
|
|---|
| 260 | I: Integer;
|
|---|
| 261 | begin
|
|---|
| 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;
|
|---|
| 266 | end;
|
|---|
| 267 |
|
|---|
| 268 | function TGraphicSets.AddNew(Name: string): TGraphicSet;
|
|---|
| 269 | begin
|
|---|
| 270 | Result := TGraphicSet.Create(nil);
|
|---|
| 271 | Result.Name := Name;
|
|---|
| 272 | Add(Result);
|
|---|
| 273 | end;
|
|---|
| 274 |
|
|---|
| 275 | procedure TGraphicSets.ResetPixUsed;
|
|---|
| 276 | var
|
|---|
| 277 | I: Integer;
|
|---|
| 278 | begin
|
|---|
| 279 | for I := 0 to Count - 1 do
|
|---|
| 280 | Items[I].ResetPixUsed;
|
|---|
| 281 | end;
|
|---|
| 282 |
|
|---|
| 283 | end.
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|