-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLayoutViewer.pas
1177 lines (1016 loc) · 32.8 KB
/
LayoutViewer.pas
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit LayoutViewer;
{$mode objfpc}{$H+}
interface
uses
Classes, Controls, Graphics, ExtCtrls, ViewTypes, Animators, OpenGLContext,
LMessages;
type
TMouseState = (msNone, msDown, msDragging);
TZOrderAnimator = class;
TActiveViewChangedEvent = procedure(NewView: TView) of object;
TActiveBranchChangedEvent = procedure(NewBranch: TView) of object;
{ TLayoutViewer }
TLayoutViewer = class(TOpenGLControl)
private
FMode3D: boolean;
FLayout: IViewLayout;
FShowContent: boolean;
FShowWireframes: boolean;
FCurrentAnimator: TAnimator;
FZOrderAnimator: TZOrderAnimator;
FToggleMode3DAnimator: TFloatArrayAnimator;
FScaleZAnimator: TFloatAnimator;
FZoomLevelAnimator: TFloatAnimator;
FResetCameraAnimator: TFloatArrayAnimator;
FOriginX: single;
FOriginY: single;
FOriginZ: single;
FHighlightedView: TView;
FMouseState: TMouseState;
FLastMouseX: integer;
FLastMouseY: integer;
FRotationX: single; // degrees
FRotationY: single; // degrees
FZoomLevel: single;
FScaleZ: single;
FCameraZ: single;
FOnActiveViewChanged: TActiveViewChangedEvent;
FOnActiveBranchChanged: TActiveBranchChangedEvent;
FActiveViewChangedTimer: TTimer;
function GetFinalOriginX: single; inline;
function GetFinalOriginY: single; inline;
function GetFinalOriginZ: single; inline;
function GetFinalRotationX: single; inline;
function GetFinalRotationY: single; inline;
function GetFinalScaleZ: single; inline;
function GetFinalZoomLevel: single; inline;
procedure SetMode3D(AValue: boolean);
procedure SetHighlightedView(AValue: TView);
procedure SetOriginZ(AValue: single);
procedure SetRotationX(Degres: single);
procedure SetRotationY(Degres: single);
procedure SetShowContent(AValue: boolean);
procedure SetShowWireFrames(AValue: boolean);
procedure SetZoomLevel(AValue: single);
procedure SetScaleZ(AValue: single);
procedure SetOriginX(AValue: single);
procedure SetOriginY(AValue: single);
protected
procedure ActiveViewChangedTimerTimer(Sender: TObject);
procedure LayoutAnimationStart(Sender: TObject);
procedure ResetCameraAnimate(Sender: TFloatArrayAnimator;
const Values: array of single);
procedure MouseLeaveHandler(Sender: TObject);
procedure ToggleMode3DAnimate(Sender: TFloatArrayAnimator;
const Values: array of single);
procedure MouseDownHandler(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
procedure MouseClickHandler(Sender: TObject);
procedure MouseDblClickHandler(Sender: TObject);
procedure MouseUpHandler(Sender: TObject; {%H-}Button: TMouseButton;
{%H-}Shift: TShiftState; {%H-}X, {%H-}Y: integer);
procedure MouseMoveHandler(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure MouseWheelHandler(Sender: TObject; Shift: TShiftState;
WheelDelta: integer; {%H-}MousePos: TPoint; var Handled: boolean);
procedure DoActiveViewChanged; inline;
procedure ZoomLevelAnimate(Sender: TFloatAnimator; Value: single);
procedure ScaleZAnimate(Sender: TFloatAnimator; Value: single);
procedure ZOrderAnimatorUpdate(Sender: TAnimator;
const {%H-}InterpolatedFraction: single);
procedure GetActiveBranchCenter(
out CenterX, CenterY, CenterZ, CameraDistance: single);
procedure WMSize(var {%H-}Message: TLMSize); message LM_SIZE;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DoOnPaint; override;
procedure Zoom(Delta: integer);
procedure Collapse(Root: TView);
procedure Expand(Root: TView);
procedure SetActiveBranch(AValue: TView);
function SetActiveView(AValue: TView): boolean;
procedure SetLayout(AValue: IViewLayout);
procedure ResetCamera(ResetRotation: boolean; Animate: boolean = True);
function SelectNextMatch: TView;
function SelectPreviousMatch: TView;
procedure SetViewSeparation(Delta: integer);
property RotationX: single read FRotationX write SetRotationX;
property RotationY: single read FRotationY write SetRotationY;
property ZoomLevel: single read FZoomLevel write SetZoomLevel;
property ScaleZ: single read FScaleZ write SetScaleZ;
property OriginX: single read FOriginX write SetOriginX;
property OriginY: single read FOriginY write SetOriginY;
property OriginZ: single read FOriginZ write SetOriginZ;
property HighlightedView: TView read FHighlightedView write SetHighlightedView;
// Fired when user changes active view.
property OnActiveViewChanged: TActiveViewChangedEvent
read FOnActiveViewChanged write FOnActiveViewChanged;
// Fires when user changes active branch.
property OnActiveBranchChanged: TActiveBranchChangedEvent
write FOnActiveBranchChanged;
property Mode3D: boolean read FMode3D write SetMode3D;
property ShowWireframes: boolean read FShowWireframes write SetShowWireframes;
property ShowContent: boolean read FShowContent write SetShowContent;
// The following properties return the equivalent property's value
// (without the "Final" suffix) after any running animation have finished.
// The idea is to get a property's value, eg. OriginX, as if
// layout transitions occurred immediately, that is, without animations.
property FinalRotationX: single read GetFinalRotationX;
property FinalRotationY: single read GetFinalRotationY;
property FinalZoomLevel: single read GetFinalZoomLevel;
property FinalScaleZ: single read GetFinalScaleZ;
property FinalOriginX: single read GetFinalOriginX;
property FinalOriginY: single read GetFinalOriginY;
property FinalOriginZ: single read GetFinalOriginZ;
end;
TZOrderAnimatorTargetRec = record
View: TView;
StartValue: single;
EndValue: single;
end;
{ TZOrderAnimator }
TZOrderAnimator = class(TAnimator)
private
FTargets: array of TZOrderAnimatorTargetRec;
protected
procedure DoFrameUpdate(const InterpolatedFraction: single); override;
public
procedure ClearTargets;
procedure AddTarget(View: TView; const StartValue, EndValue: single);
end;
implementation
uses
SysUtils, LCLIntf, LCLProc, Math, gl, glu, Logging;
type
TColorRGBA = record
case integer of
0: (ABGR: GLuint);
1: (
R: GLubyte;
G: GLubyte;
B: GLubyte;
A: GLubyte;
);
end;
const
rgbaCanvasColor: TColorRGBA = (ABGR: $FF000000);
rgbaActiveRectColor: TColorRGBA = (ABGR: $FFFF9430);
rgbaFilterMatchRectColor: TColorRGBA = (ABGR: $C091EEFF);
rgbaRectColor: TColorRGBA = (ABGR: $C0636363);
rgbaFillColor: TColorRGBA = (ABGR: $70FF824A);
rgbaPaddingColor: TColorRGBA = (ABGR: $50C3DEB7);
rgbaMarginColor: TColorRGBA = (ABGR: $50A0C5E8);
InitialZoomLevel = 1;
InitialRotationY = 0;
Mode3DScaleZ = 20;
StepZoomLevel = 0.2;
StepScaleZ = 20;
MinZoomLevel = 0.2;
MinScaleZ = 0;
MaxZoomLevel = 10;
MaxScaleZ = 200;
MinRotationX = -90;
MinRotationY = -90;
MaxRotationX = 90;
MaxRotationY = 90;
MinCameraDistance = 1000;
// FToggleMode3DAnimator element indexes.
t3daRotationY = 0;
t3daScaleZ = 1;
// FResetCameraAnimator element indexes.
rcaOriginX = 0;
rcaOriginY = 1;
rcaOriginZ = 2;
rcaRotationY = 3;
{ TZOrderAnimator }
procedure TZOrderAnimator.DoFrameUpdate(const InterpolatedFraction: single);
var
I: integer;
begin
for I := 0 to Length(FTargets) - 1 do
FTargets[I].View.ZOrder :=
FloatEvaluator(InterpolatedFraction, FTargets[I].StartValue, FTargets[I].EndValue);
inherited;
end;
procedure TZOrderAnimator.ClearTargets;
begin
SetLength(FTargets, 0);
end;
procedure TZOrderAnimator.AddTarget(View: TView; const StartValue, EndValue: single);
var
I: integer;
begin
// TODO: should I optimize this list or leave it good-enough as it is now?
I := Length(FTargets);
SetLength(FTargets, I + 1);
FTargets[I].View := View;
FTargets[I].StartValue := StartValue;
FTargets[I].EndValue := EndValue;
end;
{ TLayoutViewer }
constructor TLayoutViewer.Create(AOwner: TComponent);
begin
inherited;
FMouseState := msNone;
FShowWireFrames := True;
FShowContent := True;
FActiveViewChangedTimer := TTimer.Create(Self);
FActiveViewChangedTimer.Enabled := False;
FActiveViewChangedTimer.Interval := GetDoubleClickTime;
FActiveViewChangedTimer.OnTimer := @ActiveViewChangedTimerTimer;
MultiSampling := 4;
AutoResizeViewport := True;
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
FMode3D := True;
FToggleMode3DAnimator := TFloatArrayAnimator.Create(2, @ToggleMode3DAnimate);
FToggleMode3DAnimator.Duration := 300;
FToggleMode3DAnimator.OnStart := @LayoutAnimationStart;
FScaleZAnimator := TFloatAnimator.Create(@ScaleZAnimate);
FScaleZAnimator.OnStart := @LayoutAnimationStart;
FZoomLevelAnimator := TFloatAnimator.Create(@ZoomLevelAnimate);
FZoomLevelAnimator.OnStart := @LayoutAnimationStart;
FResetCameraAnimator := TFloatArrayAnimator.Create(4, @ResetCameraAnimate);
FResetCameraAnimator.Duration := 300;
FResetCameraAnimator.OnStart := @LayoutAnimationStart;
FZOrderAnimator := TZOrderAnimator.Create;
FZOrderAnimator.OnUpdate := @ZOrderAnimatorUpdate;
FZOrderAnimator.OnStart := @LayoutAnimationStart;
end;
destructor TLayoutViewer.Destroy;
begin
FZOrderAnimator.Free;
FZoomLevelAnimator.Free;
FScaleZAnimator.Free;
FToggleMode3DAnimator.Free;
FResetCameraAnimator.Free;
inherited Destroy;
end;
procedure TLayoutViewer.Zoom(Delta: integer);
begin
FZoomLevelAnimator.SetValueInterval(ZoomLevel,
EnsureRange(FZoomLevel + Delta * StepZoomLevel, MinZoomLevel, MaxZoomLevel));
FZoomLevelAnimator.Restart;
end;
procedure TLayoutViewer.Collapse(Root: TView);
procedure Visit(View: TView);
var
ViewChild: TView;
begin
ViewChild := View.FirstChild;
if Assigned(ViewChild) then
begin
View.Expanded := False;
repeat
FZOrderAnimator.AddTarget(ViewChild, ViewChild.ZOrder, Root.ZOrder);
Visit(ViewChild);
ViewChild := ViewChild.NextSibbling;
until ViewChild = View.FirstChild;
end
else
FZOrderAnimator.AddTarget(View, View.ZOrder, Root.ZOrder);
end;
begin
Log('TLayoutViewer.Collapse %s: Root.ZOrder=%f', [DbgS(Root), Root.ZOrder]);
FZOrderAnimator.Finish;
FZOrderAnimator.ClearTargets;
Visit(Root);
FZOrderAnimator.Start;
end;
procedure TLayoutViewer.Expand(Root: TView);
procedure Visit(View, LastVisibleParent: TView);
var
ViewChild: TView;
begin
ViewChild := View.FirstChild;
if not Assigned(ViewChild) then
Exit;
if View.Expanded then
repeat
FZOrderAnimator.AddTarget(ViewChild, Root.ZOrder,
ViewChild.ZOrderOriginal);
Visit(ViewChild, ViewChild);
ViewChild := ViewChild.NextSibbling;
until ViewChild = View.FirstChild
else
repeat
FZOrderAnimator.AddTarget(ViewChild, Root.ZOrder,
LastVisibleParent.ZOrderOriginal);
Visit(ViewChild, LastVisibleParent);
ViewChild := ViewChild.NextSibbling;
until ViewChild = View.FirstChild;
end;
begin
Log('TLayoutViewer.Expand %s', [DbgS(Root)]);
FZOrderAnimator.Finish;
FZOrderAnimator.ClearTargets;
Visit(Root, Root);
FZOrderAnimator.Start;
end;
procedure TLayoutViewer.SetActiveBranch(AValue: TView);
begin
if Assigned(FLayout) and FLayout.SetActiveBranch(AValue) then
Invalidate;
end;
procedure TLayoutViewer.SetLayout(AValue: IViewLayout);
begin
if Assigned(AValue) then
Log('TLayoutViewer.SetViewLayout %s: ActiveBranch=%s',
[DbgS(Pointer(AValue)), DbgS(AValue.ActiveBranch)])
else
Log('TLayoutViewer.SetViewLayout nil');
FLayout := AValue;
FRotationY := 0;
FRotationX := 0;
FZoomLevel := InitialZoomLevel;
FScaleZ := 0;
if Assigned(FLayout) then
begin
OnMouseDown := @MouseDownHandler;
OnMouseUp := @MouseUpHandler;
OnMouseMove := @MouseMoveHandler;
OnMouseWheel := @MouseWheelHandler;
OnMouseLeave := @MouseLeaveHandler;
OnClick := @MouseClickHandler;
OnDblClick := @MouseDblClickHandler;
// TODO: resize to fit
GetActiveBranchCenter(FOriginX, FOriginY, FOriginZ, FCameraZ);
if Mode3D then
begin
FToggleMode3DAnimator.SetElementInterval(t3daRotationY, 0, 0);
FToggleMode3DAnimator.SetElementInterval(t3daScaleZ, 0, Mode3DScaleZ);
FToggleMode3DAnimator.Restart;
end
else
Invalidate;
end
else
begin
OnMouseDown := nil;
OnMouseUp := nil;
OnMouseMove := nil;
OnMouseWheel := nil;
OnMouseLeave := nil;
OnClick := nil;
OnDblClick := nil;
Invalidate;
end;
end;
procedure TLayoutViewer.ResetCamera(ResetRotation: boolean; Animate: boolean);
var
EndOriginX, EndOriginY, EndOriginZ, EndCameraDistance: single;
begin
if Assigned(FLayout) then
if Animate then
begin
GetActiveBranchCenter(EndOriginX, EndOriginY, EndOriginZ,
EndCameraDistance);
FResetCameraAnimator.SetElementInterval(rcaOriginX, OriginX, EndOriginX);
FResetCameraAnimator.SetElementInterval(rcaOriginY, OriginY, EndOriginY);
FResetCameraAnimator.SetElementInterval(rcaOriginZ, OriginZ, EndOriginZ);
if ResetRotation then
FResetCameraAnimator.SetElementInterval(rcaRotationY, RotationY,
InitialRotationY)
else
FResetCameraAnimator.SetElementInterval(rcaRotationY, RotationY, RotationY);
FResetCameraAnimator.Restart;
end
else
begin
GetActiveBranchCenter(FOriginX, FOriginY, FOriginZ, FCameraZ);
if ResetRotation then
RotationY := InitialRotationY;
Invalidate;
end;
end;
function TLayoutViewer.SelectNextMatch: TView;
var
FirstView, Current: TView;
begin
Result := nil;
if not Assigned(FLayout) then
Exit;
if Assigned(FLayout.ActiveView) then
FirstView := FLayout.ActiveView
else
FirstView := FLayout.ActiveBranch;
Current := FirstView.NextDown;
while Current <> FirstView do
begin
if Current.MatchFilter then
begin
if SetActiveView(Current) then
Result := Current;
Break;
end;
Current := Current.NextDown;
end;
end;
function TLayoutViewer.SelectPreviousMatch: TView;
var
FirstView, Current: TView;
begin
Result := nil;
if not Assigned(FLayout) then
Exit;
if Assigned(FLayout.ActiveView) then
FirstView := FLayout.ActiveView
else
FirstView := FLayout.ActiveBranch;
Current := FirstView.PreviousUp;
while Current <> FirstView do
begin
if Current.MatchFilter then
begin
if SetActiveView(Current) then
Result := Current;
Break;
end;
Current := Current.PreviousUp;
end;
end;
procedure TLayoutViewer.SetViewSeparation(Delta: integer);
begin
if Mode3D then
begin
FScaleZAnimator.SetValueInterval(ScaleZ,
EnsureRange(FScaleZ + Delta * StepScaleZ, MinScaleZ, MaxScaleZ));
FScaleZAnimator.Restart;
end;
end;
procedure TLayoutViewer.ActiveViewChangedTimerTimer(Sender: TObject);
begin
LogEnterMethod('TLayoutViewer.ActiveViewChangedTimerTimer');
FActiveViewChangedTimer.Enabled := False;
DoActiveViewChanged;
LogExitMethod('TLayoutViewer.ActiveViewChangedTimerTimer');
end;
procedure TLayoutViewer.LayoutAnimationStart(Sender: TObject);
begin
// Keep track of current animation.
// Whenever a layout animation is started, we "cancel" any active
// animation so that only one is running at any given time.
// We do this to prevent animations from interfering with each other.
if Assigned(FCurrentAnimator) then
begin
if FCurrentAnimator <> Sender then
begin
FCurrentAnimator.Cancel;
FCurrentAnimator := TAnimator(Sender);
end;
end
else
FCurrentAnimator := TAnimator(Sender);
end;
procedure TLayoutViewer.ResetCameraAnimate(Sender: TFloatArrayAnimator;
const Values: array of single);
begin
FOriginX := Values[rcaOriginX];
FOriginY := Values[rcaOriginY];
FOriginZ := Values[rcaOriginZ];
FRotationY := Values[rcaRotationY];
Invalidate;
end;
procedure TLayoutViewer.MouseLeaveHandler(Sender: TObject);
begin
HighlightedView := nil;
end;
procedure TLayoutViewer.SetRotationX(Degres: single);
begin
if FRotationX <> Degres then
begin
FRotationX := Degres;
Invalidate;
end;
end;
function TLayoutViewer.SetActiveView(AValue: TView): boolean;
begin
if Assigned(FLayout) and FLayout.SetActiveView(AValue) then
begin
Invalidate;
Result := True;
end
else
Result := False;
end;
procedure TLayoutViewer.SetMode3D(AValue: boolean);
begin
if FMode3D = AValue then
Exit;
FMode3D := AValue;
if FMode3D then
begin
// Don't RotateY.
FToggleMode3DAnimator.SetElementInterval(t3daRotationY, 0, 0);
FToggleMode3DAnimator.SetElementInterval(t3daScaleZ, 0, Mode3DScaleZ);
end
else
begin
FToggleMode3DAnimator.SetElementInterval(t3daRotationY, RotationY, 0);
FToggleMode3DAnimator.SetElementInterval(t3daScaleZ, ScaleZ, 0);
end;
FToggleMode3DAnimator.Restart;
end;
function TLayoutViewer.GetFinalOriginX: single;
begin
if FResetCameraAnimator.IsRunning then
Result := FResetCameraAnimator.EndValues[rcaOriginX]
else
Result := OriginX;
end;
function TLayoutViewer.GetFinalOriginY: single;
begin
if FResetCameraAnimator.IsRunning then
Result := FResetCameraAnimator.EndValues[rcaOriginY]
else
Result := OriginY;
end;
function TLayoutViewer.GetFinalOriginZ: single;
begin
if FResetCameraAnimator.IsRunning then
Result := FResetCameraAnimator.EndValues[rcaOriginZ]
else
Result := OriginZ;
end;
function TLayoutViewer.GetFinalRotationX: single;
begin
Result := RotationX;
end;
function TLayoutViewer.GetFinalRotationY: single;
begin
if FToggleMode3DAnimator.IsRunning then
Result := FToggleMode3DAnimator.EndValues[t3daRotationY]
else if FResetCameraAnimator.IsRunning then
Result := FResetCameraAnimator.EndValues[rcaRotationY]
else
Result := RotationY;
end;
function TLayoutViewer.GetFinalScaleZ: single;
begin
if FToggleMode3DAnimator.IsRunning then
Result := FToggleMode3DAnimator.EndValues[t3daScaleZ]
else if FScaleZAnimator.IsRunning then
Result := FScaleZAnimator.EndValue
else
Result := ScaleZ;
end;
function TLayoutViewer.GetFinalZoomLevel: single;
begin
if FZoomLevelAnimator.IsRunning then
Result := FZoomLevelAnimator.EndValue
else
Result := ZoomLevel;
end;
procedure TLayoutViewer.SetHighlightedView(AValue: TView);
begin
if FHighlightedView <> AValue then
begin
FHighlightedView := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetOriginZ(AValue: single);
begin
if FOriginZ <> AValue then
begin
FOriginZ := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetRotationY(Degres: single);
begin
if FRotationY <> Degres then
begin
FRotationY := Degres;
Invalidate;
end;
end;
procedure TLayoutViewer.SetShowContent(AValue: boolean);
begin
if FShowContent <> AValue then
begin
FShowContent := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetShowWireFrames(AValue: boolean);
begin
if FShowWireframes <> AValue then
begin
FShowWireframes := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetZoomLevel(AValue: single);
begin
if FZoomLevel <> AValue then
begin
FZoomLevel := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetScaleZ(AValue: single);
begin
if FScaleZ <> AValue then
begin
FScaleZ := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetOriginX(AValue: single);
begin
if FOriginX <> AValue then
begin
FOriginX := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.SetOriginY(AValue: single);
begin
if FOriginY <> AValue then
begin
FOriginY := AValue;
Invalidate;
end;
end;
procedure TLayoutViewer.DoOnPaint;
var
ModelViewMatrix, ProjectionMatrix: T16dArray;
ViewportRect: TViewPortArray;
procedure DrawTexture(Left, Top, Right, Bottom, Z: single;
TextureName: integer); inline;
begin
// Set color to fully opaque white as the texture is GL_COMBINEd with it.
// by default. The result will be only the texture's color components.
glColor4f(1, 1, 1, 1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, TextureName);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0);
glVertex3f(Left, Top, Z);
glTexCoord2f(1, 0);
glVertex3f(Right, Top, Z);
glTexCoord2f(1, 1);
glVertex3f(Right, Bottom, Z);
glTexCoord2f(0, 1);
glVertex3f(Left, Bottom, Z);
glEnd;
glDisable(GL_TEXTURE_2D);
end;
procedure PolyFill(Left, Top, Right, Bottom, Z: single; Color: TColorRGBA); inline;
begin
glColor4ubv(@Color.ABGR);
glBegin(GL_POLYGON);
glVertex3f(Left, Top, Z);
glVertex3f(Right, Top, Z);
glVertex3f(Right, Bottom, Z);
glVertex3f(Left, Bottom, Z);
glEnd;
end;
procedure PolyLine(Left, Top, Right, Bottom, Z: single; Color: TColorRGBA); inline;
begin
glColor4ubv(@Color.ABGR);
glBegin(GL_LINE_LOOP);
glVertex3f(Left, Top, Z);
glVertex3f(Right, Top, Z);
glVertex3f(Right, Bottom, Z);
glVertex3f(Left, Bottom, Z);
glEnd;
end;
procedure DrawPadding(View: TView);
begin
with View do
begin
if PaddingLeft <> 0 then
PolyFill(Left, Top, Left + PaddingLeft, Bottom, ZOrder, rgbaPaddingColor);
if PaddingRight <> 0 then
PolyFill(Right - PaddingRight, Top, Right, Bottom, ZOrder, rgbaPaddingColor);
// Don't draw top paddig over left/right paddings.
if PaddingTop <> 0 then
if PaddingLeft <> 0 then
if PaddingRight <> 0 then
PolyFill(Left + PaddingLeft, Top, Right - PaddingRight,
Top + PaddingTop, ZOrder, rgbaPaddingColor)
else
PolyFill(Left + PaddingLeft, Top, Right, Top + PaddingTop,
ZOrder, rgbaPaddingColor)
else
PolyFill(Left, Top, Right, Top + PaddingTop, ZOrder, rgbaPaddingColor);
// Don't draw bottom paddig over left/right paddings.
if PaddingBottom <> 0 then
if PaddingLeft <> 0 then
if PaddingRight <> 0 then
PolyFill(Left + PaddingLeft, Bottom, Right - PaddingRight,
Bottom - PaddingBottom, ZOrder, rgbaPaddingColor)
else
PolyFill(Left + PaddingLeft, Bottom, Right, Bottom -
PaddingBottom, ZOrder, rgbaPaddingColor)
else
PolyFill(Left, Bottom - PaddingBottom, Right, Bottom,
ZOrder, rgbaPaddingColor);
end;
end;
procedure DrawMargin(View: TView);
begin
with View do
begin
if MarginLeft <> 0 then
PolyFill(Left - MarginLeft, Top, Left, Bottom, ZOrder, rgbaMarginColor);
if MarginRight <> 0 then
PolyFill(Right, Top, Right + MarginRight, Bottom, ZOrder, rgbaMarginColor);
// Don't draw top margin over left/right margins.
if MarginTop <> 0 then
if MarginLeft <> 0 then
if MarginRight <> 0 then
PolyFill(Left - MarginLeft, Top - MarginTop, Right +
MarginRight, Top, ZOrder, rgbaMarginColor)
else
PolyFill(Left - MarginLeft, Top - MarginTop, Right, Top,
ZOrder, rgbaMarginColor)
else
PolyFill(Left, Top - MarginTop, Right, Top, ZOrder, rgbaMarginColor);
// Don't draw bottom margin over left/right margins.
if MarginBottom <> 0 then
if MarginLeft <> 0 then
if MarginRight <> 0 then
PolyFill(Left - MarginLeft, Bottom, Right + MarginRight,
Bottom + MarginBottom, ZOrder, rgbaMarginColor)
else
PolyFill(Left - MarginLeft, Bottom, Right, Bottom +
MarginBottom, ZOrder, rgbaMarginColor)
else
PolyFill(Left, Bottom, Right, Bottom + MarginBottom, ZOrder, rgbaMarginColor);
end;
end;
procedure DrawView(V: TView; Texture: cardinal);
function GetWindowPoint(const X, Y, Z: single): TPoint; inline;
var
WinX, WinY, WinZ: GLdouble;
begin
gluProject(X, Y, Z, ModelViewMatrix, ProjectionMatrix,
ViewportRect, @WinX, @WinY, @WinZ);
Result.X := Round(WinX);
Result.Y := Round(ViewportRect[3] - WinY);
end;
begin
if (Texture > 0) and FShowContent then
DrawTexture(V.Left, V.Top, V.Right, V.Bottom, V.ZOrder, Texture);
if FLayout.ActiveView = V then
begin
DrawMargin(V);
DrawPadding(V);
PolyFill(V.Left + V.PaddingLeft, V.Top + V.PaddingTop, V.Right -
V.PaddingRight, V.Bottom - V.PaddingBottom, V.ZOrder, rgbaFillColor);
PolyLine(V.Left + V.PaddingLeft, V.Top + V.PaddingTop,
V.Right - V.PaddingRight, V.Bottom - V.PaddingBottom, V.ZOrder,
rgbaActiveRectColor);
end
else if HighlightedView = V then
begin
PolyFill(V.Left, V.Top, V.Right, V.Bottom, V.ZOrder, rgbaFillColor);
PolyLine(V.Left, V.Top, V.Right, V.Bottom, V.ZOrder, rgbaRectColor);
end
else if V.MatchFilter then
PolyLine(V.Left, V.Top, V.Right, V.Bottom, V.ZOrder, rgbaFilterMatchRectColor)
else if FShowWireframes then
PolyLine(V.Left, V.Top, V.Right, V.Bottom, V.ZOrder, rgbaRectColor);
with V do
begin
// Store transformed points; we use them to perform hit test.
ViewportRect[0] := {%H-}GetWindowPoint(Left, Top, ZOrder);
ViewportRect[1] := GetWindowPoint(Right, Top, ZOrder);
ViewportRect[2] := GetWindowPoint(Right, Bottom, ZOrder);
ViewportRect[3] := GetWindowPoint(Left, Bottom, ZOrder);
end;
end;
var
View, CurrentRoot: TView;
begin
with rgbaCanvasColor do
glClearColor(R / 255, G / 255, B / 255, A / 255);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
try
if not Assigned(FLayout) then
Exit;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
// Note that the order in which we specify transformations is crucial.
// They are applied in reverse, that is, the last "specified"
// transformation is the first one to be "applied".
// Move hierarchy far away from camera.
glTranslatef(0, 0, -FCameraZ);
if FToggleMode3DAnimator.IsRunning or Mode3D then
begin
// Scale Z, rotate Y around origin and invert Y-axis.
glRotatef(FRotationY, 0, 1, 0);
glScalef(1, -1, FScaleZ);
end
else
// Drop Z coord (2D mode) and invert Y-axis.
glScalef(1, -1, 0);
// Move to origin.
// Initially our origin is the ActiveBranch's bounding cube center.
glTranslatef(-FOriginX, -FOriginY, -FOriginZ);
// Perspective projection.
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glScalef(FZoomLevel, FZoomLevel, 1);
gluPerspective(45, Width / Height, 0, FCameraZ);
glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, ProjectionMatrix);
glGetIntegerv(GL_VIEWPORT, ViewportRect);
CurrentRoot := FLayout.ActiveBranch;
View := CurrentRoot;
repeat
// Skip views not visible to the user.
if View.VisibilityGone or (View.Width = 0) or (View.Height = 0) then
View := View.Next // continue
else
begin
DrawView(View, View.TextureName);
View := View.Next; // continue
end;
until View = CurrentRoot;
finally
SwapBuffers;
end;
end;
procedure TLayoutViewer.ToggleMode3DAnimate(Sender: TFloatArrayAnimator;
const Values: array of single);
begin
FRotationY := Values[t3daRotationY];
FScaleZ := Values[t3daScaleZ];
Invalidate;
end;
procedure TLayoutViewer.MouseDownHandler(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
begin
if (Button = mbLeft) and not (ssDouble in Shift) then
begin
FMouseState := msDown;
FLastMouseX := X;
FLastMouseY := Y;
end;
end;
procedure TLayoutViewer.MouseClickHandler(Sender: TObject);
var
ClickedView: TView;
begin
LogEnterMethod('TLayoutViewer.MouseClickHandler');
if FMouseState <> msDragging then
begin