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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
| {*****************************************************************************
*
* UnitMacrosList.pas - Nostromo N50 Macros Editor (21-November-2008)
*
* Copyright (c) 2008 Wilfrid AVRILLON
*
* Author: Wilfrid AVRILLON
* E-mail: wilfrid.avrillon@gmail.com
* Homepage: http://sourceforge.net/projects/nostromon50onvi/
*
*
* This file is part of Nostromo N50 Macros Editor.
*
* Nostromo N50 Macros Editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Nostromo N50 Commander is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nostromo N50 Macros Editor.
* If not, see <http://www.gnu.org/licenses/>.
*
*----------------------------------------------------------------------------
*
* Revision history:
*
* DATE REV DESCRIPTION
* ----------- --- ----------------------------------------------------------
*
* Nov 21 2008 TF First Release !
* TMacro : Class definition of a macro-command
* (One Macro=one key or one key combination)
* TMacroList : handles a list of TMacro
* Dec 18 2008 1.2 Feature : Mouse support
*****************************************************************************}
unit UnitMacrosList;
interface
uses Sysutils, Classes, Contnrs, StdCtrls;
Type
TTouche=record
down:boolean;
VK_code:word;
end;
PTTouche = ^TTouche;
TMacro = class(TObject)
private
FList:TList;
FMacroName: string;
FMacroID: integer;
function GetItem(Index: Integer): TTouche;
procedure SetItem(Index: Integer; const Value: TTouche);
function GetCount: Integer;
procedure SetCount(const Value: Integer);
procedure SetMacroName(const Value: string);
procedure SetMacroID(const Value: integer);
protected
procedure SaveToStream(AStream:TStream);
function LoadFromStream(AStream:TStream):boolean;
public
FirstKeyUp,LastKeyDown,IndexUp:integer;
constructor Create;
destructor Destroy; override;
function Add(Item:TTouche): Integer;
procedure Delete(Index: Integer);
procedure Insert(Index: Integer; Item: TTouche);
procedure Move(CurIndex, NewIndex: Integer);
procedure Exchange(Index1, Index2: Integer);
procedure Clear;
procedure ComputeIndexUp;
function IndexOf(const Value: TTouche): Integer;
property MacroName:string read FMacroName write SetMacroName;
Property MacroID:integer read FMacroID write SetMacroID;
property Items[Index:Integer]:TTouche read GetItem write SetItem;default;
property Count: Integer read GetCount write SetCount;
end;
TMacroList = class
private
FList: TObjectList;
FFichier: string;
FGameApp: string;
function GetItem(Index: Integer): TMacro;
function GetCapacity: Integer;
function GetCount: Integer;
procedure SetItem(Index: Integer; const Value: TMacro);
procedure SetCapacity(const Value: Integer);
procedure SetCount(const Value: Integer);
function GetOwnsObjects: boolean;
procedure SetOwnsObjects(const Value: boolean);
procedure SetFichier(const Value: string);
procedure SetGameApp(const Value: string);
protected
procedure SaveToStream(AStream:TStream);
function LoadFromStream(AStream:TStream):boolean;
public
constructor Create;
destructor Destroy; override;
function Add(Item: TMacro): Integer;
procedure Clear; virtual;
procedure Delete(Index: Integer);
procedure Exchange(Index1, Index2: Integer);
function Expand: TMacroList;
function Extract(Item: TMacro): TMacro;
function First: TMacro;
function IndexOf(Item: TMacro): Integer;
function IndexOfID(AMacroID:Integer): Integer;
procedure Insert(Index: Integer; Item: TMacro);
function Last: TMacro;
procedure Move(CurIndex, NewIndex: Integer);
procedure SortByNames;
function Remove(Item: TMacro): Integer;
procedure Pack;
procedure Sort(Compare: TListSortCompare);
procedure Assign(ListA: TMacroList; AOperator: TListAssignOp = laCopy; ListB: TMacroList = nil);
property GameApp:string read FGameApp write SetGameApp;
property Capacity: Integer read GetCapacity write SetCapacity;
property Count: Integer read GetCount write SetCount;
property Items[Index: Integer]: TMacro read GetItem write SetItem; default;
property OwnsObjects: boolean read GetOwnsObjects write SetOwnsObjects;
property Fichier:string read FFichier write SetFichier;
function LoadFromFile:boolean;
procedure SaveToFile;
end;
implementation
{ TMacro }
procedure WriteStreamString(Stream : TStream; UneChaine : string);
var LongueurChaine : integer;
begin
{obtenir la longueur de la chaîne de caractères}
LongueurChaine := Length(UneChaine);
{écrire cette longueur dans le flux}
Stream.Write(LongueurChaine,SizeOf(integer));
{écrire les caractères (tous d'un coup !)}
Stream.Write(UneChaine[1], LongueurChaine);
end;
{retourne une chaîne de caractères depuis le flux}
function ReadStreamString(Stream : TStream) : string;
var LongueurChaine : integer;
begin
{obtenir la longueur de la chaîne de caractères}
Stream.Read(LongueurChaine,SizeOf(integer));
{Redimensionner la chaine pour allouer la mémoire nécessaire}
SetLength(Result, LongueurChaine);
{Lire les caractères (Tous d'un coup)}
Stream.Read(Result[1], LongueurChaine);
end;
function TMacro.Add(Item:TTouche): Integer;
var
p:PTTouche;
begin
GetMem(p,SizeOf(Item));
p^ := Item;
result := FList.Add(p);
end;
procedure TMacro.Clear;
var
i:integer;
begin
for i := 0 to FList.Count-1 do FreeMem(FList[i]);
FList.Clear;
end;
procedure TMacro.ComputeIndexUp;
var i:integer;
begin
FirstKeyUp:=Count;
LastKeyDown:=0;
for I := 0 to Count - 1 do
begin
if Items[i].down then
begin
if (i>LastKeyDown) then LastKeyDown:=i;
end
else begin
if (i<FirstKeyUp) then FirstKeyUp:=i;
end;
end;
if (FirstKeyUp<LastKeyDown)
then IndexUp:=Count
else IndexUp:=FirstKeyUp;
end;
constructor TMacro.Create;
begin
FList := TList.Create;
end;
procedure TMacro.Delete(Index: Integer);
begin
FreeMem(FList[Index]);
FList.Delete(Index);
end;
destructor TMacro.Destroy;
begin
Clear;
FList.Free;
inherited;
end;
procedure TMacro.Exchange(Index1, Index2: Integer);
begin
FList.Exchange(Index1, Index2);
end;
function TMacro.GetCount: Integer;
begin
result := FList.Count;
end;
function TMacro.GetItem(Index: Integer): TTouche;
begin
result := PTTouche(FList[Index])^;
end;
function TMacro.IndexOf(const Value: TTouche): Integer;
var i: Integer;
found:Boolean;
AnItem:TTouche;
begin
Result := -1;
found:=False;
i:=0;
while ((i<FList.Count) and (not found)) do
begin
AnItem:=GetItem(i);
found:=CompareMem(@Value,@AnItem,SizeOf(TTouche));
if not found then Inc(i);
end;
if found then Result:=i;
end;
procedure TMacro.Insert(Index: Integer; Item: TTouche);
var
p: PTTouche;
begin
GetMem(p, SizeOf(Item));
p^ := Item;
FList.Insert(Index, p);
end;
function TMacro.LoadFromStream(AStream: TStream): boolean;
var NbreTouches,i:integer;
ATouche:TTouche;
begin
try
result:=true;
FMacroName:=ReadStreamString(AStream);
AStream.Read(FMacroID,sizeof(integer));
AStream.Read(NbreTouches,sizeof(integer));
for i := 0 to NbreTouches - 1 do
begin
AStream.Read(ATouche,sizeof(TTouche));
add(Atouche);
end;
except
result:=false;
end;
end;
procedure TMacro.Move(CurIndex, NewIndex: Integer);
begin
FList.Move(CurIndex, NewIndex);
end;
procedure TMacro.SaveToStream(AStream: TStream);
var NbreTouches,i:integer;
ATouche:TTouche;
begin
WriteStreamString(AStream,FMacroName);
NbreTouches:=Count;
AStream.Write(FMacroID,SizeOf(integer));
AStream.Write(NbreTouches,SizeOf(integer));
for i := 0 to NbreTouches - 1 do
begin
Atouche:=Items[i];
AStream.Write(ATouche,sizeof(TTouche));
end;
end;
procedure TMacro.SetCount(const Value: Integer);
var
i, ListCount: Integer;
p: PTTouche;
begin
if FList.Count = Value then
Exit
else
if Value < FList.Count then
begin
ListCount := FList.Count;
for i := ListCount downto Value+1 do
begin
FList.Delete(i);
end;
end else
if FList.Count < Value then
begin
for i := FList.Count+1 to Value do
begin
GetMem(p, SizeOf(TTouche));
FillChar(p^, SizeOf(TTouche), 0);
FList.Add(p);
end;
end;
end;
procedure TMacro.SetItem(Index: Integer; const Value: TTouche);
begin
PTTouche(FList[Index])^ := value;
end;
procedure TMacro.SetMacroID(const Value: integer);
begin
FMacroID := Value;
end;
procedure TMacro.SetMacroName(const Value: string);
begin
FMacroName := Value;
end;
{ TMacroList }
function TMacroList.Add(Item: TMacro): Integer;
var AnID:integer;
begin
AnID:=0;
while IndexOfID(AnID)<>-1 do inc(AnID);
Item.MacroID:=AnID;
Result := FList.Add(Item);
end;
procedure TMacroList.Assign(ListA: TMacroList; AOperator: TListAssignOp;
ListB: TMacroList);
begin
if ListB <> nil then
FList.Assign(ListA.FList, AOperator, ListB.FList) else
FList.Assign(ListA.FList);
end;
procedure TMacroList.Clear;
begin
FList.Clear;
end;
constructor TMacroList.Create;
begin
FList := TObjectList.Create;
end;
procedure TMacroList.Delete(Index: Integer);
begin
FList.Delete(Index);
end;
destructor TMacroList.Destroy;
begin
FList.Free;
inherited;
end;
procedure TMacroList.Exchange(Index1, Index2: Integer);
begin
FList.Exchange(Index1, Index2);
end;
function TMacroList.Expand: TMacroList;
begin
Result := TMacroList(FList.Expand);
end;
function TMacroList.Extract(Item: TMacro): TMacro;
begin
Result := Extract(Item);
end;
function TMacroList.First: TMacro;
begin
Result := TMacro(FList.First);
end;
function TMacroList.GetItem(Index: Integer): TMacro;
begin
Result := TObject(FList[Index]) as TMacro;
end;
function TMacroList.GetCapacity: Integer;
begin
Result := FList.Capacity;
end;
function TMacroList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TMacroList.IndexOf(Item: TMacro): Integer;
begin
Result := FList.IndexOf(Item);
end;
function TMacroList.IndexOfID(AMacroID: Integer): Integer;
begin
Result := 0;
while (Result < Count) and (Items[Result].MacroID <> AMacroID) do
Inc(Result);
if Result = Count then
Result := -1;
end;
procedure TMacroList.Insert(Index: Integer; Item: TMacro);
begin
FList.Insert(Index, Item);
end;
function TMacroList.Last: TMacro;
begin
Result := TMacro(FList.Last);
end;
function TMacroList.LoadFromFile:boolean;
var AFS:TFileStream;
begin
result:=false;
if not FileExists(Fichier) then exit;
AFS:=TFileStream.Create(Fichier,fmOpenRead);
try
result:=LoadFromStream(AFS);
finally
AFS.Free;
end;
end;
function TMacroList.LoadFromStream(AStream: TStream):boolean;
var nbreMacros,i:integer;
AMacro:TMacro;
begin
try
result:=true;
Clear;
FGameApp:=ReadStreamString(AStream);
AStream.Read(nbreMacros,SizeOf(integer));
for i:=0 to nbreMacros-1 do
begin
AMacro:=TMacro.Create;
AMacro.LoadFromStream(AStream);
Add(AMacro);
end;
except
result:=false;
end;
end;
procedure TMacroList.Move(CurIndex, NewIndex: Integer);
begin
FList.Move(CurIndex, NewIndex);
end;
procedure TMacroList.Pack;
begin
FList.Pack;
end;
procedure TMacroList.SetItem(Index: Integer; const Value: TMacro);
begin
FList.Items[Index] := Value;
end;
function TMacroList.Remove(Item: TMacro): Integer;
begin
Result := FList.Remove(Item);
end;
procedure TMacroList.SaveToFile;
var AFS:TFileStream;
begin
SortByNames;
if FileExists(FFichier) then DeleteFile(fichier);
AFS:=TFileStream.Create(Fichier,fmCreate);
try
SaveToStream(AFS);
finally
AFS.Free;
end;
end;
procedure TMacroList.SaveToStream(AStream: TStream);
var nbreMacros,i:integer;
AMacro:TMacro;
begin
WriteStreamString(AStream,FGameApp);
nbreMacros:=Count;
AStream.Write(nbreMacros,SizeOf(integer));
for i:=0 to nbreMacros-1 do
begin
AMacro:=Items[i];
AMacro.SaveToStream(AStream);
end;
end;
procedure TMacroList.SetCapacity(const Value: Integer);
begin
FList.Capacity := Value;
end;
procedure TMacroList.SetCount(const Value: Integer);
begin
FList.Count := Value;
end;
procedure TMacroList.SetFichier(const Value: string);
begin
FFichier := Value;
end;
procedure TMacroList.SetGameApp(const Value: string);
begin
FGameApp := Value;
end;
procedure TMacroList.Sort(Compare: TListSortCompare);
begin
FList.Sort(Compare);
end;
function TMacroList.GetOwnsObjects: boolean;
begin
Result := FList.OwnsObjects;
end;
procedure TMacroList.SetOwnsObjects(const Value: boolean);
begin
FList.OwnsObjects := Value;
end;
procedure TMacroList.SortByNames;
function CompareNames(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TMacro(Item1).MacroName, TMacro(Item2).MacroName);
end;
begin
Sort(@CompareNames);
end;
end. |
Partager