File: multi_window.lpr

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (106 lines) | stat: -rw-r--r-- 3,063 bytes parent folder | download | duplicates (2)
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
{
  Copyright 2004-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Demo of
  - using multiple windows with CastleWindow unit
  - instead of registering OnXxx callbacks overriding EventXxx methods
    (more OOP approach)
  - press c to change cursor in the window that has focus, this is to demo
    that TCastleWindowCustom.Cursor indeed works as appropriate, i.e. changes the cursor
    only for the given window.
}

program multi_window;

{$ifdef MSWINDOWS} {$apptype GUI} {$endif}

uses CastleWindow, SysUtils, CastleUtils, CastleGLUtils, CastleKeysMouse, CastleVectors,
  CastleStringUtils, CastleColors, CastleControls, CastleUIControls;

type
  TText = class(TUIControl)
  public
    Text: string;
    LightColor, DarkColor: TCastleColor;
    ParentWindow: TCastleWindowCustom;
    procedure Render; override;
    function Press(const Event: TInputPressRelease): boolean; override;
    function CapturesEventsAtPosition(const Position: TVector2): boolean; override;
  end;

procedure TText.Render;
begin
  inherited;
  DrawRectangle(ParentRect, DarkColor);
  UIFont.Print(10, 10, LightColor, Text);
end;

function TText.Press(const Event: TInputPressRelease): boolean;
var
  URL: string;
begin
  Result := inherited;
  if Result then Exit;

  case Event.KeyCharacter of
    'c':
      begin
        if Cursor = High(Cursor) then Cursor := Low(Cursor) else Cursor := Succ(Cursor);
        Result := ExclusiveEvents;
      end;
    'o':
      begin
        URL := '';
        { when file dialog is open, note that the other windows
          are still active as they should. }
        ParentWindow.FileDialog('Test open file dialog', URL, true);
        Result := ExclusiveEvents;
      end;
  end;
end;

function TText.CapturesEventsAtPosition(const Position: TVector2): boolean;
begin
  Result := true; // always
end;

var
  I: Integer;
  Windows: array [0..4] of TCastleWindowCustom;
  Text: TText;
begin
  for i := 0 to High(Windows) do
  begin
    Windows[I] := TCastleWindowCustom.Create(Application);

    Text := TText.Create(Windows[I]);
    Text.Text := 'Window ' + IntToStr(I);
    Text.LightColor := Vector4(Random*1.5, Random*1.5, Random*1.5, 1);
    Text.DarkColor  := Vector4(Random*0.7, Random*0.7, Random*0.7, 1);
    Text.ParentWindow := Windows[I];
    Windows[I].Controls.InsertFront(Text);

    Windows[I].Caption := 'Window ' + IntToStr(I);
    Windows[I].Width := 200;
    Windows[I].Height := 200;
    Windows[I].Left := 30 + 250 * (I mod 3);
    Windows[I].Top := 30 + 250 * (I div 3);

    Windows[I].Open;
    Windows[I].SetDemoOptions(K_F11, CharEscape, true);
  end;

  Application.Run;
end.