Debian Codegraph @

tools/castle-editor/code/frameanchors.pas

raw
1 {
2 Copyright 2018-2018 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Frame to choose UI anchors (left/middle/right, top/middle/bottom). }
17 unit FrameAnchors;
18
19 {$mode objfpc}{$H+}
20
21 interface
22
23 uses
24 Classes, SysUtils, FileUtil, Forms, Controls, StdCtrls, Buttons,
25 CastleRectangles;
26
27 type
28 TAnchorsFrame = class(TFrame)
29 ButtonLeftTop: TSpeedButton;
30 ButtonLeftMiddle: TSpeedButton;
31 ButtonLeftBottom: TSpeedButton;
32 ButtonMiddleMiddle: TSpeedButton;
33 ButtonMiddleBottom: TSpeedButton;
34 ButtonMiddleTop: TSpeedButton;
35 ButtonRightTop: TSpeedButton;
36 ButtonRightMiddle: TSpeedButton;
37 ButtonRightBottom: TSpeedButton;
38 GroupBox1: TGroupBox;
39 procedure AnyButtonClick(Sender: TObject);
40 private
41 FHorizontalAnchor: THorizontalPosition;
42 FVerticalAnchor: TVerticalPosition;
43 Buttons: array [THorizontalPosition, TVerticalPosition] of TSpeedButton;
44 procedure SetHorizontalAnchor(const AValue: THorizontalPosition);
45 procedure SetVerticalAnchor(const AValue: TVerticalPosition);
46 { Make buttons state reflect current FHorizontalAnchor, FVerticalAnchor. }
47 procedure UpdateButtons;
48 public
49 { Called when change happens because user pressed a button.
50 Not called when XxxAnchor changed by code. }
51 OnAnchorChange: TNotifyEvent;
52 constructor Create(AOwner: TComponent); override;
53 property HorizontalAnchor: THorizontalPosition read FHorizontalAnchor write SetHorizontalAnchor;
54 property VerticalAnchor: TVerticalPosition read FVerticalAnchor write SetVerticalAnchor;
55 end;
56
57 implementation
58
59 uses Dialogs,
60 CastleUtils;
61
62 {$R *.lfm}
63
64 constructor TAnchorsFrame.Create(AOwner: TComponent);
65 begin
66 inherited;
67 Buttons[hpLeft , vpBottom] := ButtonLeftBottom;
68 Buttons[hpMiddle, vpBottom] := ButtonMiddleBottom;
69 Buttons[hpRight , vpBottom] := ButtonRightBottom;
70 Buttons[hpLeft , vpMiddle] := ButtonLeftMiddle;
71 Buttons[hpMiddle, vpMiddle] := ButtonMiddleMiddle;
72 Buttons[hpRight , vpMiddle] := ButtonRightMiddle;
73 Buttons[hpLeft , vpTop ] := ButtonLeftTop;
74 Buttons[hpMiddle, vpTop ] := ButtonMiddleTop;
75 Buttons[hpRight , vpTop ] := ButtonRightTop;
76
77 // make buttons reflect default value of FHorizontalAnchor, FVerticalAnchor
78 UpdateButtons;
79 end;
80
81 procedure TAnchorsFrame.UpdateButtons;
82 //var
83 // H: THorizontalPosition;
84 // V: TVerticalPosition;
85 begin
86 Buttons[FHorizontalAnchor, FVerticalAnchor].Down := true;
87
88 { The rest of the buttons will automatically have Down=false.
89 for H in THorizontalPosition do
90 for V in TVerticalPosition do
91 Buttons[H, V].Down := (H = FHorizontalAnchor) and (V = FVerticalAnchor);
92 }
93 end;
94
95 procedure TAnchorsFrame.AnyButtonClick(Sender: TObject);
96
97 procedure FindButton(const Sender: TSpeedButton;
98 out H: THorizontalPosition; out V: TVerticalPosition);
99 begin
100 for H in THorizontalPosition do
101 for V in TVerticalPosition do
102 if Buttons[H, V] = Sender then
103 Exit;
104 raise EInternalError.Create('Cannot find sender button');
105 end;
106
107 var
108 H: THorizontalPosition;
109 V: TVerticalPosition;
110 begin
111 FindButton(Sender as TSpeedButton, H, V);
112
113 Buttons[H, V].Down := true;
114 // The rest of the buttons will automatically have Down=false.
115 FHorizontalAnchor := H;
116 FVerticalAnchor := V;
117
118 if Assigned(OnAnchorChange) then
119 OnAnchorChange(Self);
120 end;
121
122 procedure TAnchorsFrame.SetHorizontalAnchor(const AValue: THorizontalPosition);
123 begin
124 if FHorizontalAnchor = AValue then Exit;
125 FHorizontalAnchor := AValue;
126 UpdateButtons;
127 end;
128
129 procedure TAnchorsFrame.SetVerticalAnchor(const AValue: TVerticalPosition);
130 begin
131 if FVerticalAnchor = AValue then Exit;
132 FVerticalAnchor := AValue;
133 UpdateButtons;
134 end;
135
136 end.