Statistics
| Branch: | Tag: | Revision:

root / Server / URegions.pas @ 13:c78b5eafa10e

History | View | Annotate | Download (5.6 kB)

1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2007 Andreas Schneider
25
 *)
26
unit URegions;
27
28
{$mode objfpc}{$H+}
29
30
interface
31
32
uses
33
  Classes, SysUtils, contnrs, DOM, UXmlHelper, UInterfaces, URectList;
34
35
type
36
37
  { TRegion }
38
39
  TRegion = class(TObject, ISerializable, IInvalidate)
40
    constructor Create(AOwner: IInvalidate; AName: string);
41
    constructor Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
42
    destructor Destroy; override;
43
    procedure Serialize(AElement: TDOMElement);
44
  protected
45
    FOwner: IInvalidate;
46
    FName: string;
47
    FAreas: TRectList;
48
    procedure SetName(const AValue: string);
49
  public
50
    property Name: string read FName write SetName;
51
    property Areas: TRectList read FAreas write FAreas;
52
    procedure Invalidate;
53
  end;
54
55
  { TRegionList }
56
57
  TRegionList = class(TObjectList, ISerializable, IInvalidate)
58
    constructor Create(AOwner: IInvalidate); reintroduce;
59
    constructor Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
60
    procedure Serialize(AElement: TDOMElement);
61
  protected
62
    FOwner: IInvalidate;
63
  public
64
    function IndexOf(AName: string): Integer;
65
    function Find(AName: string): TRegion;
66
    procedure Delete(AName: string); overload;
67
    procedure Invalidate;
68
  end;
69
  
70
implementation
71
72
uses
73
  UCEDServer, UConfig;
74
75
{ TRegion }
76
77
constructor TRegion.Create(AOwner: IInvalidate; AName: string);
78
begin
79
  inherited Create;
80
  FOwner := AOwner;
81
  FName := AName;
82
  FAreas := TRectList.Create;
83
end;
84
85
constructor TRegion.Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
86
var
87
  nodelist: TDOMNodeList;
88
  i, x1, y1, x2, y2: Integer;
89
  xmlElement, xmlArea: TDOMElement;
90
begin
91
  inherited Create;
92
  FOwner := AOwner;
93
  FName := TXmlHelper.ReadString(AElement, 'Name', 'Unnamed');
94
  FAreas := TRectList.Create;
95
  xmlElement := TDOMElement(AElement.FindNode('Area'));
96
  if xmlElement <> nil then
97
  begin
98
    nodeList := xmlElement.GetChildNodes;
99
    for i := 0 to nodeList.Count - 1 do
100
    begin
101
      if nodeList.Item[i].NodeName = 'Rect' then
102
      begin
103
        xmlArea := TDOMElement(nodeList.Item[i]);
104
        x1 := 0;
105
        y1 := 0;
106
        x2 := 0;
107
        y2 := 0;
108
        if TryStrToInt(xmlArea.AttribStrings['x1'], x1) and
109
          TryStrToInt(xmlArea.AttribStrings['y1'], y1) and
110
          TryStrToInt(xmlArea.AttribStrings['x2'], x2) and
111
          TryStrToInt(xmlArea.AttribStrings['y2'], y2) then
112
        begin
113
          FAreas.Add(x1, y1, x2, y2);
114
        end else
115
          raise TInvalidConfigException.Create('Invalid area.');
116
      end;
117
    end;
118
    nodeList.Free;
119
  end;
120
end;
121
122
destructor TRegion.Destroy;
123
begin
124
  if FAreas <> nil then FreeAndNil(FAreas);
125
  inherited Destroy;
126
end;
127
128
procedure TRegion.SetName(const AValue: string);
129
begin
130
  FName := AValue;
131
  Invalidate;
132
end;
133
134
procedure TRegion.Invalidate;
135
begin
136
  FOwner.Invalidate;
137
end;
138
139
procedure TRegion.Serialize(AElement: TDOMElement);
140
var
141
  i : Integer;
142
  child, area: TDOMElement;
143
begin
144
  TXmlHelper.WriteString(AElement, 'Name', FName);
145
  child := TXmlHelper.AssureElement(AElement, 'Area');
146
  for i := 0 to FAreas.Count -1 do
147
  begin
148
    area := child.OwnerDocument.CreateElement('Rect');
149
    child.AppendChild(area);
150
    area.AttribStrings['x1'] := IntToStr(FAreas.Rects[i].Left);
151
    area.AttribStrings['y1'] := IntToStr(FAreas.Rects[i].Top);
152
    area.AttribStrings['x2'] := IntToStr(FAreas.Rects[i].Right);
153
    area.AttribStrings['y2'] := IntToStr(FAreas.Rects[i].Bottom);
154
  end;
155
end;
156
157
{ TRegionList }
158
159
constructor TRegionList.Create(AOwner: IInvalidate);
160
begin
161
  inherited Create(True);
162
  FOwner := AOwner;
163
end;
164
165
constructor TRegionList.Deserialize(AOwner: IInvalidate; AElement: TDOMElement);
166
var
167
  nodelist: TDOMNodeList;
168
  i: Integer;
169
begin
170
  Create(AOwner);
171
  nodeList := AElement.GetChildNodes;
172
  for i := 0 to nodeList.Count - 1 do
173
  begin
174
    if nodeList.Item[i].NodeName = 'Region' then
175
      Add(TRegion.Deserialize(Self, TDOMElement(nodeList.Item[i])));
176
  end;
177
  nodeList.Free;
178
end;
179
180
function TRegionList.IndexOf(AName: string): Integer;
181
var
182
  i: Integer;
183
begin
184
  Result := -1;
185
  i := 0;
186
  while (i < Count) and (Result = -1) do
187
  begin
188
    if TRegion(Items[i]).Name = AName then
189
      Result := i;
190
    Inc(i);
191
  end;
192
end;
193
194
function TRegionList.Find(AName: string): TRegion;
195
var
196
  i: Integer;
197
begin
198
  i := IndexOf(AName);
199
  if i > -1 then
200
    Result := TRegion(Items[i])
201
  else
202
    Result := nil;
203
end;
204
205
procedure TRegionList.Delete(AName: string);
206
var
207
  i: Integer;
208
begin
209
  i := IndexOf(AName);
210
  if i > -1 then
211
    inherited Delete(i);
212
end;
213
214
procedure TRegionList.Invalidate;
215
begin
216
  FOwner.Invalidate;
217
end;
218
219
procedure TRegionList.Serialize(AElement: TDOMElement);
220
var
221
  i: Integer;
222
  xmlRegion: TDOMElement;
223
begin
224
  for i := 0 to Count - 1 do
225
  begin
226
    xmlRegion := AElement.OwnerDocument.CreateElement('Region');
227
    AElement.AppendChild(xmlRegion);
228
    TRegion(Items[i]).Serialize(xmlRegion);
229
  end;
230
end;
231
232
end.
233