File: window_events.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 (161 lines) | stat: -rw-r--r-- 4,913 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  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 numerous TCastleWindowCustom events.
  Displays many OnXxx events as they happen,
  also shows Pressed and Pressed.Characters.
  Also a demo of CastleNotifications unit. }

program window_events;

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

uses SysUtils, CastleUtils, CastleGLUtils, CastleNotifications, CastleWindow,
  CastleKeysMouse, CastleStringUtils, CastleColors, Classes, CastleMessages,
  CastleControls, CastleVectors, CastleRectangles;

var
  Window: TCastleWindowCustom;
  Notifications: TCastleNotifications;

procedure Open(Container: TUIContainer);
begin
  Notifications.Show('Open message');
end;

procedure Close(Container: TUIContainer);
begin
end;

procedure Resize(Container: TUIContainer);
begin
  Notifications.Show(Format('Resize message : new size %d %d (pos %d, %d)',
    [Window.Width, Window.Height, Window.Left, Window.Top]));
end;

procedure BeforeRender(Container: TUIContainer);
begin
  { Part of functionality of OnRender moved to BeforeRender.
    In this program there is no point in doing that.
    But I wanted just to show that BeforeRender really works. }
  RenderContext.Clear([cbColor], Black);
end;

procedure Render(Container: TUIContainer);
var
  C: Char;
  Key: TKey;
  S: string;
const
  Margin = 20;
begin
  S := '';
  for C := Low(C) to High(C) do
    if Window.Pressed.Characters[C] then
    begin
      if S <> '' then S += ', ';
      S += CharToNiceStr(C);
    end;
  S := 'Characters pressed: [' + S + ']';
  UIFont.PrintBrokenString(Margin, 100, Gray, S, Window.Width - Margin * 2, false, 0);

  S := '';
  for Key := Low(Key) to High(Key) do
    if Window.Pressed[Key] then
    begin
      if S <> '' then S += ', ';
      S += KeyToStr(Key);
    end;
  S := 'Keys pressed: [' + S + ']';
  UIFont.PrintBrokenString(Margin, 200, Gray, S, Window.Width - Margin * 2, false, 0);
end;

procedure Update(Container: TUIContainer);
begin
  if Window.Pressed[K_F12] then
    // MessageOk(Window, 'F12 key pressed. This is just a test that MessageOk works even from callbacks like OnUpdate.');
    Window.MessageOk('F12 key pressed. This is just a test that MessageOk works even from callbacks like OnUpdate.', mtInfo);
end;

procedure Timer(Container: TUIContainer);
begin
  Notifications.Show(Format('Timer message. Time now %s', [FormatDateTime('tt', Time)]));
end;

procedure Press(Container: TUIContainer; const Event: TInputPressRelease);
begin
  Notifications.Show('Press message : ' + Event.ToString);

  { Cursor tests: }
  case Event.KeyCharacter of
    'n': Notifications.Cursor := mcNone;
    'd': Notifications.Cursor := mcDefault;
    'w': Notifications.Cursor := mcWait;
    '1': Window.MousePosition := Vector2(0           , 0);
    '2': Window.MousePosition := Vector2(Window.Width, 0);
    '3': Window.MousePosition := Vector2(Window.Width, Window.Height);
    '4': Window.MousePosition := Vector2(0           , Window.Height);
    '5': Window.MousePosition := Vector2(Window.Width / 2, Window.Height / 2);
  end;

  { Test what messages happen when switching FullScreen }
  if Event.IsKey(K_F11) {$ifdef DARWIN} and Window.Pressed[K_Ctrl] {$endif} then
    Window.FullScreen := not Window.FullScreen;
end;

procedure Release(Container: TUIContainer; const Event: TInputPressRelease);
begin
  Notifications.Show('Release message : ' + Event.ToString);
end;

procedure Motion(Container: TUIContainer; const Event: TInputMotion);
begin
  Notifications.Show(Format('Motion : old pos %f %f, new pos %f %f',
    [ Event.OldPosition[0],
      Event.OldPosition[1],
      Event.Position[0],
      Event.Position[1] ]));
end;

begin
  Window := TCastleWindowCustom.Create(Application);

  Window.ParseParameters;

  Window.OnOpen := @Open;
  Window.OnClose := @Close;
  Window.OnResize := @Resize;
  Window.OnBeforeRender := @BeforeRender;
  Window.OnRender := @Render;
  Window.OnPress := @Press;
  Window.OnRelease := @Release;
  Window.OnMotion := @Motion;
  Window.SetDemoOptions(K_F11, CharEscape, true);

  Window.OnUpdate := @Update;
  Application.TimerMilisec := 5000;
  Window.OnTimer := @Timer;

  Notifications := TCastleNotifications.Create(Window);
  Notifications.Anchor(hpMiddle);
  Notifications.Anchor(vpTop, -5);
  Notifications.TextAlignment := hpMiddle;
  Notifications.Color := Yellow;
  Notifications.MaxMessages := 15;
  Notifications.Timeout := 20000;
  Window.Controls.InsertFront(Notifications);

  Window.OpenAndRun;
end.