Advertisement
Stella_209

StObjects.pas

May 21st, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 13.01 KB | None | 0 0
  1. (*
  2.     StellaSOFT objektumok gyüjteménye
  3.     ---------------------------------
  4.     TUndoRedo:      UndoRedo folyamatokat megvalósító objektum;
  5.  
  6. *)
  7. unit StObjects;
  8.  
  9. interface
  10.  
  11. uses Windows, Classes, Graphics;
  12.  
  13.  
  14. Type
  15.  
  16.   T2DPoint = class(TPersistent)
  17.   private
  18.     FOnChange: TNotifyEvent;
  19.     Fx : extended;
  20.     Fy : extended;
  21.     FID: integer;
  22.     procedure Setx(Value:extended);
  23.     procedure Sety(Value:extended);
  24.     procedure Changed; dynamic;
  25.   public
  26.     constructor Create(AOwner:TObject; px,py: extended);
  27.   published
  28.     property ID          : integer  read FID write FID;
  29.     property x           : extended read Fx write Setx;
  30.     property y           : extended read Fy write Sety;
  31.     property OnChange    : TNotifyEvent read FOnChange write FOnChange;
  32.   end;
  33.  
  34.   T3DPoint = class(TPersistent)
  35.   private
  36.     FOnChange: TNotifyEvent;
  37.     FID: integer;
  38.     Fx : extended;
  39.     Fy : extended;
  40.     Fz : extended;
  41.     procedure Changed; dynamic;
  42.     procedure Setx(Value:extended);
  43.     procedure Sety(Value:extended);
  44.     procedure Setz(const Value: extended);
  45.   public
  46.     constructor Create(AOwner:TObject; px,py,pz: extended);
  47.   published
  48.     property ID          : integer  read FID write FID;
  49.     property x           : extended read Fx write Setx;
  50.     property y           : extended read Fy write Sety;
  51.     property z           : extended read Fz write Setz;
  52.     property OnChange    : TNotifyEvent read FOnChange write FOnChange;
  53.   end;
  54.  
  55.   TUndoRedoChangeEvent = procedure(Sender:TObject; Undo,Redo:boolean) of object;
  56.   TUndoSaveEvent = procedure(Sender:TObject; MemSt:TMemoryStream) of object;
  57.   TUndoSaveProcedure = procedure(var MemSt:TMemoryStream) of object;
  58.   TUndoRedoProcedure = procedure(MemSt:TMemoryStream) of object;
  59.  
  60.   {---- UndoRedo objektum -----}
  61.   TUndoRedo = class
  62.   private
  63.     fEnable: boolean;
  64.     fUndoLimit: integer;
  65.     FUndoRedo:TUndoRedoChangeEvent;
  66.     FUndoSave: TUndoSaveEvent;
  67.     fUndoSaveProcedure: TUndoSaveProcedure;
  68.     fUndoRedoProcedure: TUndoRedoProcedure;
  69.     procedure SetUndoLimit(const Value: integer);
  70.     procedure SetEnable(const Value: boolean);
  71.   protected
  72.     UndoSaveCount : integer;
  73.     UndoCount     : integer;
  74.     UndoStart     : integer;
  75.     UndoPointer   : integer;
  76.     UndoEnable,RedoEnable : boolean;
  77.     function GetIndex(us:integer): integer;
  78.   public
  79.     UndoStreams   : array[0..999] of TMemoryStream;
  80.     constructor Create; virtual;
  81.     destructor Destroy; override;
  82.     procedure UndoInit;
  83.     procedure UndoSave;
  84.     procedure Undo;
  85.     procedure Redo;
  86.     property Enable : boolean read fEnable write SetEnable;
  87.     property UndoLimit : integer read fUndoLimit write SetUndoLimit;
  88.     property UndoSaveProcedure: TUndoSaveProcedure read fUndoSaveProcedure write fUndoSaveProcedure;
  89.     property UndoRedoProcedure: TUndoRedoProcedure read fUndoRedoProcedure write fUndoRedoProcedure;
  90.     property OnUndoRedo : TUndoRedoChangeEvent read FUndoRedo write FUndoRedo;
  91.     property OnUndoSave : TUndoSaveEvent read FUndoSave write FUndoSave;
  92.   end;
  93.  
  94.    THRTimer = Class(TObject)
  95.      Constructor Create;
  96.      Function StartTimer : Boolean;
  97.      Function ReadTimer : Double;
  98.    private
  99.    public
  100.      Exists    : Boolean;
  101.      StartTime : Double;
  102.      ClockRate : Double;
  103.      PROCEDURE Delay(ms: double);
  104.    End;
  105.  
  106.   TLayerName = String[30];
  107.  
  108.   TLayer = class(TPersistent)
  109.   private
  110.     fVisible: Boolean;
  111.     fHomogen: Boolean;
  112.     fModified: Boolean;
  113.     fActive: Boolean;
  114.     fTag: LongInt;
  115.     FNote: string;
  116.     fBrush: TBrush;
  117.     fName: TLayerName;
  118.     fPen: TPen;
  119.     fLayerId: Byte;
  120.     procedure SetBrush(const Value: TBrush);
  121.     procedure SetName(const Value: TLayerName);
  122.     procedure SetPen(const Value: TPen);
  123.   published
  124.     constructor Create(Idx: Byte);
  125.     destructor Destroy; override;
  126.     procedure SaveToStream(const Stream: TStream); virtual;
  127.     procedure LoadFromStream(const Stream: TStream); virtual;
  128.     property Name: TLayerName read fName write SetName;
  129.     property LayerID: Byte read fLayerId;
  130.     property Pen: TPen read fPen write SetPen;
  131.     property Brush: TBrush read fBrush write SetBrush;
  132.     property Active: Boolean read fActive write FActive;
  133.     property Modified: Boolean read fModified;
  134.     property Homogen: Boolean read fHomogen write fHomogen;
  135.     property Visible: Boolean read fVisible write fVisible;
  136.     property Note: string read FNote write fNote;
  137.     property Tag: LongInt read fTag write fTag;
  138.   end;
  139.  
  140.   TMetric = (meMM,meInch);
  141.   TGridStyle  = (gsNone,gsLine,gsDot,gsCross);
  142.  
  143.   TGrid = Class(TPersistent)
  144.   private
  145.     fVisible: boolean;
  146.     fGridStyle: TGridStyle;
  147.     fSubGridColor: TColor;
  148.     fMainGridColor: TColor;
  149.     FOnChange: TNotifyEvent;
  150.     fMetric: TMetric;
  151.     fMargin: integer;
  152.     fOnlyOnPaper: boolean;
  153.     procedure SetMainGridColor(Value: TColor);
  154.     procedure SetGridStyle(const Value: TGridStyle);
  155.     procedure SetSubGridColor(Value: TColor);
  156.     procedure SetVisible(const Value: boolean);
  157.     procedure Changed;
  158.     procedure SetMetric(const Value: TMetric);
  159.     procedure SetMargin(const Value: integer);
  160.     procedure SetOnlyOnPaper(const Value: boolean);
  161.   protected
  162.   public
  163.     constructor Create;
  164.     procedure Change(Sender: TObject);
  165.   published
  166.     property MainGridColor: TColor read fMainGridColor write SetMainGridColor;
  167.     property Margin: integer read fMargin write SetMargin;
  168.     property SubGridColor: TColor read fSubGridColor write SetSubGridColor;
  169.     property Style: TGridStyle read fGridStyle write SetGridStyle default gsNone;
  170.     property Metric: TMetric read fMetric write SetMetric default meMM;
  171.     property Visible: boolean read fVisible write SetVisible default True;
  172.     property OnlyOnPaper: boolean read fOnlyOnPaper write SetOnlyOnPaper default True;
  173.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  174.   end;
  175.  
  176.  
  177. implementation
  178.  
  179. { -----------  TUndoRedo --------- }
  180.  
  181. constructor TUndoRedo.Create;
  182. var i: integer;
  183. begin
  184.   Inherited Create;
  185.   UndoLimit := 1000;
  186.   Enable    := True;
  187.   UndoInit;
  188. end;
  189.  
  190. destructor TUndoRedo.Destroy;
  191. var i: integer;
  192. begin
  193.   for i:=0 to fUndoLimit-1 do
  194.       if  UndoStreams[i]<>nil then UndoStreams[i].Destroy;
  195.   Inherited Destroy;
  196. end;
  197.  
  198. {Az Undo stream-eket alapra hozza}
  199. procedure TUndoRedo.UndoInit;
  200. var i: integer;
  201. begin
  202.   UndoSaveCount := 0;
  203.   UndoCount     := 0;
  204.   UndoStart     := 0;
  205.   UndoPointer   := 0;
  206.   UndoEnable    := False;
  207.   RedoEnable    := False;
  208.   for i:=0 to fUndoLimit-1 do
  209.       if UndoStreams[i]=nil then UndoStreams[i].Create
  210.       else UndoStreams[i].Clear;
  211.   If Assigned(FUndoRedo) then FUndoRedo(Self,False,False);
  212. end;
  213.  
  214. {Undo mentés az sbl stream tartalmát menti az UndoStreams n. streamjére;
  215.  az undopointer és undoCount értékét 1-el növeli }
  216. procedure TUndoRedo.UndoSave;
  217. begin
  218. If Enable then begin
  219.   UndoStart := UndoPointer;
  220.   UndoStreams[UndoPointer].Clear;
  221.   If Assigned(fUndoSaveProcedure) then
  222.      fUndoSaveProcedure(UndoStreams[UndoPointer]);
  223.   Inc(UndoPointer);
  224.   UndoPointer := UndoPointer mod UndoLimit;
  225.   Inc(UndoSaveCount);
  226.   UndoCount := 0;
  227.   UndoEnable    := UndoSaveCount>0;
  228.   RedoEnable    := False;
  229.   If Assigned(FUndoRedo) then FUndoRedo(Self,UndoEnable,RedoEnable);
  230. end;
  231. end;
  232.  
  233. function TUndoRedo.GetIndex(us:integer): integer;
  234. begin
  235.   Result := us;
  236.   If us>(Undolimit-1) then Result:=us mod Undolimit;
  237.   if us<0 then Result:=UndoLimit-(Trunc(Abs(us)) mod Undolimit)
  238. end;
  239.  
  240. procedure TUndoRedo.Undo;
  241. var UC,IDX: integer;
  242. begin
  243. If Enable then begin
  244.    UC := UndoPointer-1;
  245.    If UndoSaveCount>=UndoLimit then UC:=UndoLimit-1;
  246.    UndoEnable := UndoCount<UC;
  247.    if UndoEnable then begin
  248.         Dec(UndoStart);
  249.         IDX := GetIndex(UndoStart);
  250.         UndoStreams[IDX].Seek(0,0);
  251.         If Assigned(fUndoRedoProcedure) then
  252.            fUndoRedoProcedure(UndoStreams[IDX]);
  253.         Inc(UndoCount);
  254.         UndoEnable := UndoCount<UC;
  255.         RedoEnable := UndoCount>0;
  256.    end;
  257.    If Assigned(FUndoRedo) then FUndoRedo(Self,UndoEnable,RedoEnable);
  258. end;
  259. end;
  260.  
  261. procedure TUndoRedo.Redo;
  262. var UC,IDX: integer;
  263. begin
  264. If Enable then begin
  265.    RedoEnable := UndoCount>0;
  266.    if RedoEnable then begin
  267.         Inc(UndoStart);
  268.         IDX := GetIndex(UndoStart);
  269.         UndoStreams[IDX].Seek(0,0);
  270.         If Assigned(fUndoRedoProcedure) then
  271.            fUndoRedoProcedure(UndoStreams[IDX]);
  272.         Dec(UndoCount);
  273.         RedoEnable := UndoCount>0;
  274.         UndoEnable := True;
  275.    end;
  276.    If Assigned(FUndoRedo) then FUndoRedo(Self,UndoEnable,RedoEnable);
  277. end;
  278. end;
  279.  
  280. procedure TUndoRedo.SetUndoLimit(const Value: integer);
  281. var i: integer;
  282. begin
  283.   If fUndoLimit <> Value then begin
  284.      fUndoLimit := Value;
  285.      If fUndoLimit>High(UndoStreams) then fUndoLimit:=High(UndoStreams);
  286.      for i:=0 to fUndoLimit-1 do
  287.        if UndoStreams[i]=nil then UndoStreams[i]:=TMemoryStream.Create;
  288.      for i:=fUndoLimit to High(UndoStreams) do
  289.        if UndoStreams[i]<>nil then UndoStreams[i].Destroy;
  290.   end;
  291. end;
  292.  
  293. procedure TUndoRedo.SetEnable(const Value: boolean);
  294. begin
  295.   fEnable := Value;
  296.   if Value then begin
  297.    If Assigned(FUndoRedo) then FUndoRedo(Self,UndoEnable,RedoEnable)
  298.   end else
  299.    If Assigned(FUndoRedo) then FUndoRedo(Self,False,False);
  300. end;
  301.  
  302. { -----------  T2DPoint --------- }
  303.  
  304. procedure T2DPoint.Changed;
  305. begin
  306.   if Assigned(FOnChange) then
  307.      FOnChange(Self);
  308. end;
  309.  
  310. constructor T2DPoint.Create(AOwner:TObject; px,py: extended);
  311. begin
  312.   inherited Create;
  313.   x := px; y := py;
  314.   ID := 0;
  315. end;
  316.  
  317. procedure T2DPoint.Setx(Value:extended);
  318. begin
  319.   If Fx<>Value then begin
  320.      Fx:=Value;
  321.      Changed;
  322.   end;
  323. end;
  324.  
  325. procedure T2DPoint.Sety(Value:extended);
  326. begin
  327.   If Fy<>Value then begin
  328.      Fy:=Value;
  329.      Changed;
  330.   end;
  331. end;
  332.  
  333. //-----------THRTimer-----------------
  334.  
  335. Constructor THRTimer.Create;
  336. Var  QW : _Large_Integer;
  337. BEGIN
  338.    Inherited Create;
  339.    Exists := QueryPerformanceFrequency(TLargeInteger(QW));
  340.    ClockRate := QW.QuadPart;
  341. END;
  342.  
  343. Function THRTimer.StartTimer : Boolean;
  344. Var
  345.   QW : _Large_Integer;
  346. BEGIN
  347.    Result := QueryPerformanceCounter(TLargeInteger(QW));
  348.    StartTime := QW.QuadPart;
  349. END;
  350.  
  351. Function THRTimer.ReadTimer : Double;
  352. Var
  353.   ET : _Large_Integer;
  354. BEGIN
  355.    QueryPerformanceCounter(TLargeInteger(ET));
  356.    Result := 1000.0*(ET.QuadPart - StartTime)/ClockRate;
  357. END;
  358.  
  359. PROCEDURE THRTimer.Delay(ms: double);
  360. Var
  361.   QW,ET : _Large_Integer;
  362.   Start_Time, dt : double;
  363. BEGIN
  364.    QueryPerformanceCounter(TLargeInteger(QW));
  365.    Start_Time := QW.QuadPart;
  366.    repeat
  367.          QueryPerformanceCounter(TLargeInteger(ET));
  368.          dt := 1000.0*(ET.QuadPart - Start_Time)/ClockRate;
  369.    Until dt>=ms;
  370. END;
  371.  
  372. {------------------------------------------------------------------------------}
  373.  
  374. { TLayer }
  375.  
  376. constructor TLayer.Create(Idx: Byte);
  377. begin
  378.   fPen := TPen.Create;
  379.   fBrush := TBrush.Create;
  380.   fLayerID := Idx;
  381.   Tag := 0;
  382. end;
  383.  
  384. destructor TLayer.Destroy;
  385. begin
  386.   fPen.Free;
  387.   fBrush.Free;
  388.   inherited;
  389. end;
  390.  
  391. procedure TLayer.LoadFromStream(const Stream: TStream);
  392. begin
  393.  
  394. end;
  395.  
  396. procedure TLayer.SaveToStream(const Stream: TStream);
  397. begin
  398.  
  399. end;
  400.  
  401. procedure TLayer.SetBrush(const Value: TBrush);
  402. begin
  403.   fBrush := Value;
  404. end;
  405.  
  406. procedure TLayer.SetName(const Value: TLayerName);
  407. begin
  408.   fName := Value;
  409. end;
  410.  
  411. procedure TLayer.SetPen(const Value: TPen);
  412. begin
  413.   fPen := Value;
  414. end;
  415.  
  416. { TGrid }
  417.  
  418. constructor TGrid.Create;
  419. begin
  420.   inherited;
  421.   fMainGridColor := clGray;
  422.   fSubGridColor  := clSilver;
  423.   fOnlyOnPaper   := True;
  424.   fMetric        := meMM;
  425.   fVisible       := True;
  426. end;
  427.  
  428. procedure TGrid.Change(Sender: TObject);
  429. begin
  430.     Changed;
  431. end;
  432.  
  433. procedure TGrid.Changed;
  434. begin if Assigned(FOnChange) then FOnChange(Self); end;
  435.  
  436. procedure TGrid.SetOnlyOnPaper(const Value: boolean);
  437. begin
  438.   fOnlyOnPaper := Value;
  439.   Changed;
  440. end;
  441.  
  442. procedure TGrid.SetMargin(const Value: integer);
  443. begin
  444.   fMargin := Value;
  445.   Changed;
  446. end;
  447.  
  448. procedure TGrid.SetMainGridColor(Value: TColor);
  449. begin
  450.   fMainGridColor:=Value;
  451.   Changed;
  452. end;
  453.  
  454. procedure TGrid.SetGridStyle(const Value: TGridStyle);
  455. begin
  456.   fGridStyle := Value;
  457.   Changed;
  458. end;
  459.  
  460. procedure TGrid.SetSubGridColor(Value: TColor);
  461. begin
  462.  fSubGridColor := Value;
  463.   Changed;
  464. end;
  465.  
  466. procedure TGrid.SetMetric(const Value: TMetric);
  467. begin
  468.   fMetric := Value;
  469.   Changed;
  470. end;
  471.  
  472. procedure TGrid.SetVisible(const Value: boolean);
  473. begin
  474.   fVisible := Value;
  475.   Changed;
  476. end;
  477.  
  478.  
  479. { T3DPoint }
  480.  
  481. procedure T3DPoint.Changed;
  482. begin
  483.   if Assigned(FOnChange) then
  484.      FOnChange(Self);
  485. end;
  486.  
  487. constructor T3DPoint.Create(AOwner: TObject; px, py, pz: extended);
  488. begin
  489.   inherited Create;
  490.   x := px; y := py; z := pz;
  491.   ID := 0;
  492. end;
  493.  
  494. procedure T3DPoint.Setx(Value: extended);
  495. begin
  496.   If Fx<>Value then begin
  497.      Fx:=Value;
  498.      Changed;
  499.   end;
  500. end;
  501.  
  502. procedure T3DPoint.Setz(const Value: extended);
  503. begin
  504.   If Fz<>Value then begin
  505.      Fz:=Value;
  506.      Changed;
  507.   end;
  508. end;
  509.  
  510. procedure T3DPoint.Sety(Value: extended);
  511. begin
  512.   If Fy<>Value then begin
  513.      Fy:=Value;
  514.      Changed;
  515.   end;
  516. end;
  517.  
  518. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement