Debian Codegraph @

examples/window/multi_window/multi_window.dpr

raw
1 {
2 Copyright 2004-2023 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 { Demo of using multiple open windows with TCastleWindow.
17
18 Additional demo:
19 press c to change cursor in the window that has focus, this is to demo
20 that TCastleWindow.Cursor indeed works as appropriate, i.e. changes the cursor
21 only for the given window.
22 }
23
24 program multi_window;
25
26 {$ifdef MSWINDOWS} {$apptype GUI} {$endif}
27
28 uses SysUtils, Classes,
29 CastleWindow, CastleUtils, CastleGLUtils, CastleKeysMouse, CastleVectors,
30 CastleStringUtils, CastleColors, CastleControls, CastleUIControls, CastleLog;
31
32 type
33 TWindowView = class(TCastleUserInterface)
34 private
35 FStatusLabel: TCastleLabel;
36 public
37 WindowIndex: Integer;
38 DarkColor: TCastleColor;
39 ParentWindow: TCastleWindow;
40 constructor Create(AOwner: TComponent); override;
41 procedure Update(const SecondsPassed: Single; var HandleInput: boolean); override;
42 procedure Render; override;
43 function Press(const Event: TInputPressRelease): boolean; override;
44 function CapturesEventsAtPosition(const Position: TVector2): boolean; override;
45 end;
46
47 constructor TWindowView.Create(AOwner: TComponent);
48 begin
49 inherited;
50
51 FStatusLabel := TCastleLabel.Create(Self);
52 FStatusLabel.FontSize := 10;
53 FStatusLabel.Anchor(hpLeft, 10);
54 FStatusLabel.Anchor(vpBottom, 10);
55 // random light color
56 FStatusLabel.Color := Vector4(
57 RandomFloatRange(0.5, 1.0),
58 RandomFloatRange(0.5, 1.0),
59 RandomFloatRange(0.5, 1.0), 1);
60 InsertFront(FStatusLabel);
61 end;
62
63 procedure TWindowView.Update(const SecondsPassed: Single; var HandleInput: boolean);
64 begin
65 inherited;
66 FStatusLabel.Caption := Format('Window %d' + NL + 'Pixels width / height: %d / %d', [
67 WindowIndex,
68 ParentWindow.Width,
69 ParentWindow.Height
70 ]);
71 end;
72
73 procedure TWindowView.Render;
74 begin
75 inherited;
76 { TODO: use TCastleRectangleControl for drawing this.
77 TODO: TWindowView should be class(TCastleView)
78 }
79 DrawRectangle(ParentRect, DarkColor);
80 end;
81
82 function TWindowView.Press(const Event: TInputPressRelease): boolean;
83 var
84 Url: String;
85 begin
86 Result := inherited;
87 if Result then Exit;
88
89 case Event.KeyCharacter of
90 'c':
91 begin
92 if Cursor = High(Cursor) then Cursor := Low(Cursor) else Cursor := Succ(Cursor);
93 Exit(true);
94 end;
95 'o':
96 begin
97 Url := '';
98 { when file dialog is open, note that the other windows
99 are still active as they should. }
100 ParentWindow.FileDialog('Test open file dialog', Url, true);
101 Exit(true);
102 end;
103 end;
104 end;
105
106 function TWindowView.CapturesEventsAtPosition(const Position: TVector2): boolean;
107 begin
108 Result := true; // always
109 end;
110
111 var
112 I: Integer;
113 Windows: array [0..4] of TCastleWindow;
114 WindowView: TWindowView;
115 begin
116 InitializeLog;
117
118 for i := 0 to High(Windows) do
119 begin
120 Windows[I] := TCastleWindow.Create(Application);
121
122 WindowView := TWindowView.Create(Windows[I]);
123 WindowView.WindowIndex := I;
124 WindowView.DarkColor := Vector4(Random*0.7, Random*0.7, Random*0.7, 1);
125 WindowView.ParentWindow := Windows[I];
126 Windows[I].Controls.InsertFront(WindowView);
127
128 Windows[I].Caption := 'Window ' + IntToStr(I);
129 Windows[I].Width := 200;
130 Windows[I].Height := 200;
131 Windows[I].Left := 30 + 250 * (I mod 3);
132 Windows[I].Top := 30 + 250 * (I div 3);
133
134 Windows[I].Open;
135 Windows[I].SetDemoOptions(keyF11, CharEscape, true);
136 end;
137
138 Application.Run;
139 end.