Debian Codegraph @

src/ui/castleuicontrols_theme.inc

raw
1 {%MainUnit castleuicontrols.pas}
2 {
3 Copyright 2010-2023 Michalis Kamburelis.
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 {$ifdef read_interface}
18
19 { Type of theme image, use of @link(TCastleTheme.ImagesPersistent). }
20 TThemeImage = (
21 tiPanel, tiPanelSeparator,
22 tiButtonPressed, tiButtonDisabled, tiButtonFocused, tiButtonNormal,
23 tiWindow, tiScrollbarFrame, tiScrollbarSlider,
24 tiSliderBackground, tiSliderThumb, tiLabel, tiGroup, tiActiveFrame, tiTooltip,
25 tiTouchCtlInner, tiTouchCtlOuter, tiTouchCtlFlyInner, tiTouchCtlFlyOuter,
26 tiCrosshair1, tiCrosshair2,
27 tiSquareEmpty, tiSquareChecked, tiSquarePressedBackground,
28
29 { Image displayed when the application is initializing,
30 during @link(TCastleApplication.OnInitialize Application.OnInitialize)
31 and @link(TCastleWindow.OnOpen Window.OnOpen).
32 And @link(TCastleUserInterface.GLContextOpen) for all initially present UI controls.
33 This "loading image" is loaded and displayed first,
34 so that user does not see a black screen while the resources are prepared.
35
36 It is especially useful on Android, where we can lose the OpenGL context
37 at any moment, as user may switch applications in the middle of the game.
38 When getting back to the application, we need to initiailize some
39 resources, and during this process we also show this image.
40 So this serves as a universal "please wait, we're loading" screen.
41
42 You can customize this image, by setting
43 @link(TCastleTheme.ImagesPersistent Theme.ImagesPersistent[tiLoading].Image),
44 @link(TCastleTheme.LoadingBackgroundColor LoadingBackgroundColor),
45 @link(TCastleTheme.LoadingTextColor LoadingTextColor).
46 See https://castle-engine.io/manual_2d_user_interface.php#section_theme
47 for a sample code that sets a theme image.
48
49 Note that the customization of this image should be done before
50 @link(TCastleApplication.OnInitialize Application.OnInitialize) has
51 started, so it has to be usually done from the "initialization" section
52 of some unit. And in the "initialization" section of a unit,
53 you cannot load files (doing @link(LoadImage) at this point may fail on
54 some Android devices, as we cannot load assets before activity is started).
55 So you can only assign images already available in code ---
56 use image-to-pascal tool to convert any image to a Pascal code for this purpose. }
57 tiLoading,
58
59 { TCastleEdit frame. }
60 tiEdit
61 );
62
63 { Theme for user interface controls.
64
65 You can customize @link(Theme) singleton to change the default look.
66
67 You can also create new instances of TCastleTheme and use them for multiple controls,
68 assigning to @link(TCastleUserInterface.CustomTheme).
69
70 You can also use @link(FallbackTheme) to effectively disable any customizations
71 and always use default hardcoded look. }
72 TCastleTheme = class(TCastleComponent)
73 strict private
74 FImagesPersistent: array [TThemeImage] of TCastleImagePersistent;
75 FMessageFont: TCastleAbstractFont;
76 FOwnsMessageFont: boolean;
77 function GetImages(const ImageType: TThemeImage): TCastleImage;
78 procedure SetImages(const ImageType: TThemeImage; const Value: TCastleImage);
79 function GetOwnsImages(const ImageType: TThemeImage): boolean;
80 procedure SetOwnsImages(const ImageType: TThemeImage; const Value: boolean);
81 function GetImagePersistent(const ImageType: TThemeImage): TCastleImagePersistent;
82 function GetCorners(const ImageType: TThemeImage): TVector4;
83 procedure SetCorners(const ImageType: TThemeImage; const Value: TVector4);
84 procedure SetMessageFont(const Value: TCastleAbstractFont);
85 public
86 TooltipTextColor: TCastleColor;
87 TextColor, DisabledTextColor: TCastleColor;
88 MessageTextColor: TCastleColor;
89 MessageInputTextColor: TCastleColor;
90
91 BarEmptyColor: TVector3Byte;
92 BarFilledColor: TVector3Byte;
93
94 { Color layer displayed between modal dialogs (CastleMessages, CastleDialogViews, TGLModeFrozenScreen)
95 and their background (view underneath or a screenshot).
96 Default is (0, 0, 0, 0.25), making things underneath darker,
97 which helps dialog to stand out. }
98 BackgroundColor: TCastleColor;
99
100 { Color used instead of BackgroundColor when InternalMessageFallbackLook is used.
101 You can only customize the RGB values, alpha is always 1, so the resulting
102 color is always opaque.
103 It's a dark gray color (0.33, 0.33, 0.33) by default. }
104 BackgroundOpaqueColor: TCastleColorRGB;
105
106 { Colors used when displaying the loading image (by default just with the "Loading..." text)
107 when application is loading (before proper CGE UI can be displayed).
108 By default, LoadingBackgroundColor is black, and LoadingColor is white.
109
110 See https://castle-engine.io/loading_image for
111 an example how to set loading image. }
112 LoadingBackgroundColor, LoadingColor: TCastleColor;
113
114 { Adjust loading image following the UI scaling determined by these properties.
115 They determine for what window reference size was the loading image prepared.
116 The loading image is whatever you set at @code(ImagesPersistent[tiLoading]).
117 See https://castle-engine.io/loading_image for
118 an example how to set loading image.
119
120 These properties deliberately match the behavior of
121 @link(TCastleContainer.UIScaling),
122 @link(TCastleContainer.UIReferenceWidth),
123 @link(TCastleContainer.UIReferenceHeight),
124 @link(TCastleContainer.UIExplicitScale).
125 If these LoadingUIXxx properties are equal to the
126 @link(TCastleContainer).UIXxx properties, then the rendered image size will
127 be precisely equal to how TCastleImageControl rendering of this image
128 (with anchors set to middle) would look.
129
130 The reason why these properties exist is that when rendering
131 @code(ImagesPersistent[tiLoading]), TCastleApplication.OnInitialize
132 is not yet run, and so @link(TCastleContainer.UIScaling) may be not yet be initialized
133 (because you cannot read CastleSettings.xml earlier).
134
135 By default, we set LoadingUIScaling = usEncloseReferenceSize,
136 LoadingUIReferenceWidth = 1600,
137 LoadingUIReferenceHeight = 900. Note that this is in contrast to
138 @link(TCastleContainer).UIXxx properties, that by default set no UI scaling.
139
140 @groupBegin }
141 LoadingUIScaling: TUIScaling;
142 LoadingUIReferenceWidth: Single;
143 LoadingUIReferenceHeight: Single;
144 LoadingUIExplicitScale: Single;
145 { @groupEnd }
146
147 { Width of scrollbar for TCastleScrollView. }
148 ScrollBarWidth: Single;
149 { Minumum scrollbar height, to be comfortably visible and draggable. }
150 MinimumScrollbarSliderHeight: Single;
151
152 { Request modal boxes to use safe "fallback" rendering options,
153 to work even when application is in buggy/dirty state
154 because something unexpected happened.
155
156 This is internally set and unset by the CastleWindow exception handler
157 (you should not modify this yourself).
158 It allows to request "fallback" drawing for
159 @link(TCastleWindow.MessageOK) and friends,
160 or modal states by @link(TGLModeFrozenScreen).
161
162 This means:
163
164 @unorderedList(
165 @item(Using opaque color from @link(BackgroundOpaqueColor) such that
166 we don't need to render anything underneath.)
167
168 @item(Use FallbackFont and FallbackTheme, so ignore user customizations
169 to font and theme.)
170
171 @item(Avoid making a screenshot (TStateDialog.BackgroundScreenshot may be false),
172 which is safer since the application may be in some dirty state, so it's better to avoid
173 rendering user stuff.)
174 )
175
176 @exclude }
177 InternalMessageFallbackLook: boolean;
178
179 DefaultLabelWhite: Boolean deprecated 'if you want the labels to be white, adjust TCastleLabel.Color to White after creating every TCastleLabel';
180
181 constructor Create(AOwner: TComponent); override;
182 destructor Destroy; override;
183
184 {$ifdef FPC}
185 { Theme images, represented as TCastleImage.
186 Although they all have sensible defaults, you can also change them
187 at any time. Simply create TCastleImage instance (e.g. by LoadImage
188 function) and assign it here. Be sure to adjust also @link(OwnsImages)
189 if you want the theme to automatically free the image when it's no longer
190 used.
191
192 The alpha channel of the image, if any, is automatically correctly used
193 (for alpha test or alpha blending, see TDrawableImage). }
194 property Images[const ImageType: TThemeImage]: TCastleImage read GetImages write SetImages; deprecated 'use ImagesPersistent[...].Image';
195 {$endif}
196
197 { Images used to draw UI controls by default.
198
199 You can adjust them e.g. to adjust how a default TCastleButton looks.
200 Note that some controls have properties to override the "theme" look
201 for a particular control, e.g. on a particular TCastleButton
202 you can set @link(TCastleButton.CustomBackground) and
203 @link(TCastleButton.CustomBackgroundNormal).
204
205 When adjusting these images, it is simplest to assign
206 the image URL by setting @link(TCastleImagePersistent.URL).
207 See https://castle-engine.io/manual_2d_user_interface.php#section_theme .
208 Adjust also @link(TCastleImagePersistent.ProtectedSides) to use
209 @url(https://en.wikipedia.org/wiki/9-slice_scaling 9-slice scaling). }
210 property ImagesPersistent[const ImageType: TThemeImage]: TCastleImagePersistent read GetImagePersistent;
211
212 {$ifdef FPC}
213 property OwnsImages[const ImageType: TThemeImage]: boolean read GetOwnsImages write SetOwnsImages; deprecated 'use ImagesPersistent[...].OwnsImage';
214
215 { Corners that determine how image on @link(Images) is stretched when
216 drawing by @link(TCastleTheme.Draw) method.
217 Together with assigning @link(Images), adjust also this property.
218 It is used for images rendered using TDrawableImage.Draw3x3,
219 it determines how the image is stretched.
220 The corners are specified as 4D vector, order like in CSS: top, right, down,
221 left. }
222 property Corners[const ImageType: TThemeImage]: TVector4 read GetCorners write SetCorners;
223 deprecated 'use ImagesPersistent[...].ProtectedSides';
224 {$endif FPC}
225
226 { Draw the selected theme image on screen.
227
228 @param(Color Color tint of the image.
229 If you do not specify a color, white will be used, so image will be displayed
230 as-is. Specifying a color means that image will be multiplied by it,
231 just like for @link(TDrawableImage.Color).)
232
233 @param(UIScale Used to properly scale corners, passed to @link(TDrawableImage.ScaleCorners).
234 This parameter does @italic(not) scale the place where image is displayed,
235 to do this just scale the given Rect parameter yourself.)
236 }
237 procedure Draw(const Rect: TFloatRectangle; const ImageType: TThemeImage;
238 const UIScale: Single = 1.0); overload;
239 procedure Draw(const Rect: TFloatRectangle; const ImageType: TThemeImage;
240 const UIScale: Single; const Color: TCastleColor); overload;
241 procedure Draw(const Rect: TRectangle; const ImageType: TThemeImage;
242 const UIScale: Single = 1.0); overload;
243 procedure Draw(const Rect: TRectangle; const ImageType: TThemeImage;
244 const UIScale: Single; const Color: TCastleColor); overload;
245
246 { Font used by dialogs.
247 Leave @nil to use @link(TCastleContainer.DefaultFont). }
248 property MessageFont: TCastleAbstractFont read FMessageFont write SetMessageFont;
249
250 property OwnsMessageFont: boolean
251 read FOwnsMessageFont write FOwnsMessageFont default true;
252
253 { Set dialogs theme to light. }
254 procedure DialogsLight;
255
256 {$ifdef FPC}
257 property LoadingTextColor: TCastleColor
258 read LoadingColor write LoadingColor;
259 deprecated 'use LoadingColor';
260 property LoadingImageForWindowHeight: Single
261 read LoadingUIReferenceHeight write LoadingUIReferenceHeight;
262 deprecated 'use LoadingUIReferenceHeight';
263 {$endif}
264 end;
265
266 { Theme that you can customize, used by default by all UI controls. }
267 function Theme: TCastleTheme;
268
269 { Fallback (do not change this, ever) theme. Default state of @link(Theme). }
270 function FallbackTheme: TCastleTheme;
271
272 const
273 tiSlider = tiSliderBackground deprecated 'use tiSliderBackground';
274 tiSliderPosition = tiSliderThumb deprecated 'use tiSliderThumb';
275
276 {$endif read_interface}
277
278 {$ifdef read_implementation}
279
280 { TCastleTheme --------------------------------------------------------------- }
281
282 constructor TCastleTheme.Create(AOwner: TComponent);
283 var
284 T: TThemeImage;
285 begin
286 inherited;
287 TooltipTextColor := Vector4(0 , 0, 0, 1);
288 TextColor := Vector4(0 , 0, 0, 1);
289 DisabledTextColor := Vector4(0.33, 0.33, 0.33, 1);
290 MessageInputTextColor := Vector4(0.33, 1, 1, 1);
291 MessageTextColor := Vector4(1 , 1, 1, 1);
292 BackgroundColor := Vector4(0, 0, 0, 0.25);
293 BackgroundOpaqueColor := Vector3(0.33, 0.33, 0.33);
294 LoadingBackgroundColor := Black;
295 LoadingColor := White;
296 ScrollBarWidth := 20;
297 MinimumScrollbarSliderHeight := 20;
298 LoadingUIScaling := usEncloseReferenceSize;
299 LoadingUIReferenceWidth := 1600;
300 LoadingUIReferenceHeight := 900;
301
302 FOwnsMessageFont := true;
303
304 for T := Low(TThemeImage) to High(TThemeImage) do
305 begin
306 FImagesPersistent[T] := TCastleImagePersistent.Create;
307 { Hide 9-slices visualization in editor, as user likely didn't set the corners of theme,
308 and could be surprised by all extra drawing (e.g. on every default button). }
309 FImagesPersistent[T].InternalVisualizeProtectedSides := false;
310 FImagesPersistent[T].OwnsImage := false;
311 end;
312
313 FImagesPersistent[tiPanel].Image := Panel;
314 FImagesPersistent[tiPanelSeparator].Image := PanelSeparator;
315 FImagesPersistent[tiButtonNormal].Image := ButtonNormal;
316 FImagesPersistent[tiButtonNormal].ProtectedSides.AllSides := 2;
317 FImagesPersistent[tiButtonDisabled].Image := ButtonDisabled;
318 FImagesPersistent[tiButtonDisabled].ProtectedSides.AllSides := 2;
319 FImagesPersistent[tiButtonPressed].Image := ButtonPressed;
320 FImagesPersistent[tiButtonPressed].ProtectedSides.AllSides := 2;
321 FImagesPersistent[tiButtonFocused].Image := ButtonFocused;
322 FImagesPersistent[tiButtonFocused].ProtectedSides.AllSides := 2;
323 FImagesPersistent[tiWindow].Image := WindowDark;
324 FImagesPersistent[tiWindow].ProtectedSides.AllSides := 2;
325 FImagesPersistent[tiScrollbarFrame].Image := ScrollbarFrame;
326 FImagesPersistent[tiScrollbarFrame].ProtectedSides.AllSides := 1;
327 FImagesPersistent[tiScrollbarSlider].Image := ScrollbarSlider;
328 FImagesPersistent[tiScrollbarSlider].ProtectedSides.AllSides := 3;
329 FImagesPersistent[tiSliderBackground].Image := SliderBackground;
330 FImagesPersistent[tiSliderBackground].ProtectedSides.AllSides := 31;
331 FImagesPersistent[tiSliderBackground].ProtectedSidesScaling := pssMatchHeight;
332 FImagesPersistent[tiSliderThumb].Image := SliderThumb;
333 FImagesPersistent[tiLabel].Image := FrameWhiteBlack;
334 FImagesPersistent[tiLabel].ProtectedSides.AllSides := 2;
335 FImagesPersistent[tiGroup].Image := FrameWhiteBlack;
336 FImagesPersistent[tiGroup].ProtectedSides.AllSides := 2;
337 FImagesPersistent[tiActiveFrame].Image := FrameWhite;
338 FImagesPersistent[tiActiveFrame].ProtectedSides.AllSides := 2;
339 FImagesPersistent[tiTooltip].Image := Tooltip;
340 FImagesPersistent[tiTooltip].ProtectedSides.AllSides := 1;
341 FImagesPersistent[tiTouchCtlInner].Image := TouchCtlInner;
342 FImagesPersistent[tiTouchCtlOuter].Image := TouchCtlOuter;
343 FImagesPersistent[tiTouchCtlFlyInner].Image := TouchCtlFlyInner;
344 FImagesPersistent[tiTouchCtlFlyOuter].Image := TouchCtlFlyOuter;
345 FImagesPersistent[tiCrosshair1].Image := Crosshair1;
346 FImagesPersistent[tiCrosshair2].Image := Crosshair2;
347
348 { Note that tiSquareEmpty and tiSquarePressedBackground could have
349 corners = (4, 4, 4, 4), but tiSquareChecked cannot.
350 And these 3 images must be consistent.
351 So, do not declare any corners for them. }
352 FImagesPersistent[tiSquareEmpty].Image := SquareEmpty;
353 FImagesPersistent[tiSquareChecked].Image := SquareChecked;
354 FImagesPersistent[tiSquarePressedBackground].Image := SquarePressedBackground;
355
356 FImagesPersistent[tiLoading].Image := CastleInternalControlsImages.Loading;
357 FImagesPersistent[tiEdit].Image := Edit;
358 FImagesPersistent[tiEdit].ProtectedSides.AllSides := 2;
359 end;
360
361 destructor TCastleTheme.Destroy;
362 var
363 T: TThemeImage;
364 begin
365 for T := Low(TThemeImage) to High(TThemeImage) do
366 FreeAndNil(FImagesPersistent[T]);
367
368 if OwnsMessageFont then
369 FreeAndNil(FMessageFont)
370 else
371 FMessageFont := nil;
372 inherited;
373 end;
374
375 function TCastleTheme.GetImages(const ImageType: TThemeImage): TCastleImage;
376 begin
377 Result := FImagesPersistent[ImageType].Image as TCastleImage;
378 end;
379
380 procedure TCastleTheme.SetImages(const ImageType: TThemeImage;
381 const Value: TCastleImage);
382 begin
383 FImagesPersistent[ImageType].Image := Value;
384 end;
385
386 function TCastleTheme.GetOwnsImages(const ImageType: TThemeImage): boolean;
387 begin
388 Result := FImagesPersistent[ImageType].OwnsImage;
389 end;
390
391 procedure TCastleTheme.SetOwnsImages(const ImageType: TThemeImage;
392 const Value: boolean);
393 begin
394 FImagesPersistent[ImageType].OwnsImage := Value;
395 end;
396
397 function TCastleTheme.GetImagePersistent(const ImageType: TThemeImage): TCastleImagePersistent;
398 begin
399 Result := FImagesPersistent[ImageType];
400 end;
401
402 function TCastleTheme.GetCorners(const ImageType: TThemeImage): TVector4;
403 var
404 B: TBorder;
405 begin
406 B := FImagesPersistent[ImageType].ProtectedSides;
407 Result := Vector4(
408 B.TotalTop,
409 B.TotalRight,
410 B.TotalBottom,
411 B.TotalLeft
412 );
413 end;
414
415 procedure TCastleTheme.SetCorners(const ImageType: TThemeImage; const Value: TVector4);
416 var
417 B: TBorder;
418 begin
419 B := FImagesPersistent[ImageType].ProtectedSides;
420 B.AllSides := 0;
421 B.Top := Value[0];
422 B.Right := Value[1];
423 B.Bottom := Value[2];
424 B.Left := Value[3];
425 end;
426
427 procedure TCastleTheme.Draw(const Rect: TFloatRectangle; const ImageType: TThemeImage;
428 const UIScale: Single);
429 begin
430 Draw(Rect, ImageType, UIScale, White);
431 end;
432
433 procedure TCastleTheme.Draw(const Rect: TFloatRectangle; const ImageType: TThemeImage;
434 const UIScale: Single; const Color: TCastleColor);
435 begin
436 FImagesPersistent[ImageType].Color := Color;
437 FImagesPersistent[ImageType].ScaleCorners := UIScale;
438 FImagesPersistent[ImageType].Draw(Rect);
439 end;
440
441 procedure TCastleTheme.Draw(const Rect: TRectangle; const ImageType: TThemeImage;
442 const UIScale: Single);
443 begin
444 Draw(FloatRectangle(Rect), ImageType, UIScale);
445 end;
446
447 procedure TCastleTheme.Draw(const Rect: TRectangle; const ImageType: TThemeImage;
448 const UIScale: Single; const Color: TCastleColor);
449 begin
450 Draw(FloatRectangle(Rect), ImageType, UIScale, Color);
451 end;
452
453 procedure TCastleTheme.SetMessageFont(const Value: TCastleAbstractFont);
454 begin
455 if FMessageFont <> Value then
456 begin
457 if OwnsMessageFont then
458 FreeAndNil(FMessageFont);
459 FMessageFont := Value;
460 end;
461 end;
462
463 procedure TCastleTheme.DialogsLight;
464 begin
465 MessageInputTextColor := Vector4(0, 0.4, 0, 1.0);
466 MessageTextColor := Black;
467 FImagesPersistent[tiWindow].Image := WindowGray;
468 FImagesPersistent[tiWindow].OwnsImage := false;
469 FImagesPersistent[tiLabel].Image := FrameYellowBlack;
470 FImagesPersistent[tiLabel].OwnsImage := false;
471 end;
472
473 var
474 FTheme, FFallbackTheme: TCastleTheme;
475
476 function Theme: TCastleTheme;
477 begin
478 Result := FTheme;
479 end;
480
481 function FallbackTheme: TCastleTheme;
482 begin
483 Result := FFallbackTheme;
484 end;
485
486 procedure InitializationTheme;
487 begin
488 FTheme := TCastleTheme.Create(nil);
489 FFallbackTheme := TCastleTheme.Create(nil);
490 end;
491
492 procedure FinalizationTheme;
493 begin
494 FreeAndNil(FTheme);
495 FreeAndNil(FFallbackTheme);
496 end;
497
498 {$endif read_implementation}