Debian Codegraph @

src/ui/castlecontrols_switchcontrol.inc

raw
1 {%MainUnit castlecontrols.pas}
2 {
3 Copyright 2017-2023 Michalis Kamburelis and Jan Adamec.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 (*
18
19 {$ifdef read_interface}
20
21 { Touch-friendly checkbox control representing on/off state. }
22 TCastleSwitchControl = class(TCastleUserInterface)
23 strict private
24 FChecked: boolean;
25 FEnabled: boolean;
26 FPressed: boolean;
27 FOnChange: TNotifyEvent;
28
29 procedure SetChecked(const Value: boolean);
30 procedure SetEnabled(const Value: boolean);
31 public
32 const
33 DefaultSwitchControlWidth = 50;
34 DefaultSwitchControlHeight = 28;
35
36 constructor Create(AOwner: TComponent); override;
37 destructor Destroy; override;
38 procedure Render; override;
39 function Press(const Event: TInputPressRelease): boolean; override;
40 function Release(const Event: TInputPressRelease): boolean; override;
41 function PropertySections(const PropertyName: String): TPropertySections; override;
42
43 {$ifdef FPC}
44 property IsOn: boolean read FChecked write SetChecked default false;
45 deprecated 'use Checked instead, consistent with LCL TCheckBox.Checked';
46 {$endif}
47 published
48 { Switch state. }
49 property Checked: boolean read FChecked write SetChecked default false;
50 property Enabled: boolean read FEnabled write SetEnabled default true;
51 { Event sent when @link(Checked) value was changed by a user click.
52 Note that this is @italic(not) called when you change @link(Checked)
53 property programmatically by setting the property. }
54 property OnChange: TNotifyEvent read FOnChange write FOnChange;
55 property Width {$ifdef FPC}default DefaultSwitchControlWidth{$endif};
56 property Height {$ifdef FPC}default DefaultSwitchControlHeight{$endif};
57 end deprecated 'use TCastleCheckbox or TCastleButton with TCastleButton.Toggle=true and custom backgrounds';
58
59 {$endif read_interface}
60
61 {$ifdef read_implementation}
62
63 {
64 This may be brought back from deprecation, but we need to address some things:
65
66 TODO:
67
68 - Default UI design of this has to change to look more modern.
69 The current look of it was done by reusing some images for other UI controls.
70
71 - UI design has to be 100% adjustable using TCastleImagePersistent subcomponents.
72 See e.g. how TCastleEdit, TCastleAbstractSlider do it.
73
74 - Name maybe should change -- "Control" suffix is likely unnecessary,
75 we generally try to avoid such suffix for UI controls.
76 See what other UI toolkits call it.
77
78 - To be really more useful than "TCastleButton with Toggle=true and custom backgrounds"
79 the thumb should animate when toggling.
80 }
81
82 { TCastleSwitchControl ---------------------------------------------------------- }
83
84 constructor TCastleSwitchControl.Create(AOwner: TComponent);
85 begin
86 inherited;
87 FChecked := false;
88 FEnabled := true;
89 FPressed := false;
90 FOnChange := nil;
91 Width := DefaultSwitchControlWidth;
92 Height := DefaultSwitchControlHeight;
93 end;
94
95 destructor TCastleSwitchControl.Destroy;
96 begin
97 inherited;
98 end;
99
100 procedure TCastleSwitchControl.Render;
101 var
102 BaseRect, BackRect, HandleRect: TFloatRectangle;
103 HandleImage: TThemeImage;
104 begin
105 inherited;
106
107 BaseRect := RenderRect;
108
109 // background
110 BackRect := BaseRect;
111 BackRect.Height := Round(BaseRect.Height * 0.5); //*0.4; //BaseRect.Height / 2;
112 BackRect.Bottom := BaseRect.Bottom + ((BaseRect.Height - BackRect.Height) / 2);
113 Theme.Draw(BackRect, tiSwitchControl, UIScale);
114 if Checked then
115 Theme.Draw(BackRect, tiSwitchControlFill, UIScale);
116
117 // handle
118 HandleRect := BaseRect;
119 HandleRect.Width := BaseRect.Width / 3; //HandleRect.Height;
120 if Checked then
121 HandleRect.Left := BaseRect.Right - HandleRect.Width;
122
123 if FPressed then
124 HandleImage := tiButtonPressed
125 else if not Enabled then
126 HandleImage := tiButtonDisabled
127 else if Focused then
128 HandleImage := tiButtonFocused
129 else
130 HandleImage := tiButtonNormal;
131 Theme.Draw(HandleRect, HandleImage, UIScale)
132 end;
133
134 procedure TCastleSwitchControl.SetEnabled(const Value: boolean);
135 begin
136 if FEnabled <> Value then
137 begin
138 FEnabled := Value;
139 VisibleChange([chRender]);
140 end;
141 end;
142
143 procedure TCastleSwitchControl.SetChecked(const Value: boolean);
144 begin
145 if FChecked <> Value then
146 begin
147 FChecked := Value;
148 VisibleChange([chRender]);
149 end;
150 end;
151
152 function TCastleSwitchControl.Press(const Event: TInputPressRelease): boolean;
153 begin
154 Result := inherited;
155 if Result then Exit;
156
157 if Event.IsMouseButton(buttonLeft) or
158 Event.IsKey(keySpace) or
159 Event.IsKey(keyEnter) then
160 begin
161 FPressed := true;
162 Exit(true);
163 end;
164 end;
165
166 function TCastleSwitchControl.Release(const Event: TInputPressRelease): boolean;
167 begin
168 Result := inherited;
169 if Result then Exit;
170
171 if FPressed and (
172 Event.IsMouseButton(buttonLeft) or
173 Event.IsKey(keySpace) or
174 Event.IsKey(keyEnter)) then
175 begin
176 FPressed := false;
177
178 { Because of mouse capture mechanism in TCastleContainer,
179 we receive Release notifications about all clicks that started
180 within our rect, regardless where they finished.
181
182 Just like TCastleButton, the click only counts
183 if it happens over our rect. }
184 if CapturesEventsAtPosition(Event.Position) then
185 begin
186 FChecked := not FChecked; // FPressed, FChecked changed
187 VisibleChange([chRender]);
188
189 { Make sure to execute FOnChange at the end,
190 to have consistent state when calling the callback that can
191 do anything with this TCastleSwitchControl. }
192 if Assigned(FOnChange) then
193 FOnChange(Self);
194 end else
195 begin
196 VisibleChange([chRender]); // FPressed changed
197 end;
198
199 Exit(true);
200 end;
201 end;
202
203 function TCastleSwitchControl.PropertySections(
204 const PropertyName: String): TPropertySections;
205 begin
206 if (PropertyName = 'Checked') then
207 Result := [psBasic]
208 else
209 Result := inherited PropertySections(PropertyName);
210 end;
211
212 {$endif read_implementation}
213
214 *)