| 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 |
{ A simple program using CastleWindow. |
| 17 |
Demonstrates the use of MainMenu in TCastleWindow. |
| 18 |
|
| 19 |
Shows |
| 20 |
- menu, |
| 21 |
- submenus, |
| 22 |
- menus with keyboard shortcuts, |
| 23 |
- checked menu items, |
| 24 |
- radio menu items, |
| 25 |
- adding menus at runtime, |
| 26 |
- using menu mnemonics (underscore, for Alt+keypress), |
| 27 |
- replacing whole main menu temporarily with other main menu. |
| 28 |
} |
| 29 |
program window_menu; |
| 30 |
|
| 31 |
{$ifdef MSWINDOWS} {$apptype GUI} {$endif} |
| 32 |
|
| 33 |
uses SysUtils, |
| 34 |
CastleVectors, CastleKeysMouse, CastleColors, CastleLog, |
| 35 |
CastleWindow, CastleGLUtils, CastleMessages, CastleStringUtils, |
| 36 |
CastleViewport, CastleScene, X3DNodes; |
| 37 |
|
| 38 |
var |
| 39 |
Window: TCastleWindow; |
| 40 |
MenuHorizLeft, MenuHorizMiddle, MenuHorizRight: TMenuItemRadio; |
| 41 |
MainMenu, AlternativeMainMenu: TMenu; |
| 42 |
|
| 43 |
const |
| 44 |
Colors: array [0..6] of TCastleColorRGB = |
| 45 |
( (X: 1; Y: 0; Z: 0), |
| 46 |
(X: 0; Y: 1; Z: 0), |
| 47 |
(X: 0; Y: 0; Z: 1), |
| 48 |
(X: 1; Y: 1; Z: 0), |
| 49 |
(X: 1; Y: 1; Z: 1), |
| 50 |
(X: 0.5; Y: 0.5; Z: 0.5), |
| 51 |
(X: 0; Y: 0; Z: 0) |
| 52 |
); |
| 53 |
|
| 54 |
var |
| 55 |
{ Scene 3D information. Displayed only to show that menu commands actually work. } |
| 56 |
Scene: TCastleScene; |
| 57 |
|
| 58 |
Filled: boolean = true; |
| 59 |
Translation: TVector3; |
| 60 |
|
| 61 |
Shape: TShapeNode; |
| 62 |
Material: TMaterialNode; |
| 63 |
Transform: TTransformNode; |
| 64 |
GeometryRect: TQuadSetNode; |
| 65 |
GeometryTriangle: TTriangleSetNode; |
| 66 |
|
| 67 |
procedure CreateScene; |
| 68 |
var |
| 69 |
Appearance: TAppearanceNode; |
| 70 |
CoordRect, CoordTriangle: TCoordinateNode; |
| 71 |
Root: TX3DRootNode; |
| 72 |
begin |
| 73 |
CoordRect := TCoordinateNode.Create; |
| 74 |
CoordRect.SetPoint([ |
| 75 |
Vector3(-0.5, -0.5, 0), |
| 76 |
Vector3( 0.5, -0.5, 0), |
| 77 |
Vector3( 0.5, 0.5, 0), |
| 78 |
Vector3(-0.5, 0.5, 0)]); |
| 79 |
|
| 80 |
CoordTriangle := TCoordinateNode.Create; |
| 81 |
CoordTriangle.SetPoint([ |
| 82 |
Vector3(-0.5, -0.5, 0), |
| 83 |
Vector3( 0.5, -0.5, 0), |
| 84 |
Vector3( 0, 0.5, 0)]); |
| 85 |
|
| 86 |
GeometryRect := TQuadSetNode.Create; |
| 87 |
GeometryRect.Coord := CoordRect; |
| 88 |
|
| 89 |
GeometryTriangle := TTriangleSetNode.Create; |
| 90 |
GeometryTriangle.Coord := CoordTriangle; |
| 91 |
|
| 92 |
Material := TMaterialNode.Create; |
| 93 |
Material.DiffuseColor := Colors[0]; // initial value |
| 94 |
|
| 95 |
Appearance := TAppearanceNode.Create; |
| 96 |
Appearance.Material := Material; |
| 97 |
|
| 98 |
Shape := TShapeNode.Create; |
| 99 |
Shape.Geometry := GeometryTriangle; // initial value |
| 100 |
Shape.Appearance := Appearance; |
| 101 |
|
| 102 |
Transform := TTransformNode.Create; |
| 103 |
Transform.Translation := Translation; |
| 104 |
Transform.AddChildren(Shape); |
| 105 |
|
| 106 |
Root := TX3DRootNode.Create; |
| 107 |
Root.AddChildren(Transform); |
| 108 |
|
| 109 |
Scene := TCastleScene.Create(nil); |
| 110 |
Scene.Load(Root, true); |
| 111 |
|
| 112 |
{ Do not free GeometryXxx nodes automatically when unused. |
| 113 |
Usually, X3D nodes are freed automatically when they are no longer part |
| 114 |
of scene hierarchy, but in case of GeometryXxx, one of them is always *not* |
| 115 |
in hierarchy. } |
| 116 |
GeometryTriangle.KeepExistingBegin; |
| 117 |
GeometryRect.KeepExistingBegin; |
| 118 |
end; |
| 119 |
|
| 120 |
procedure DestroyScene; |
| 121 |
begin |
| 122 |
GeometryTriangle.KeepExistingEnd; |
| 123 |
GeometryRect.KeepExistingEnd; |
| 124 |
FreeIfUnusedAndNil(GeometryTriangle); |
| 125 |
FreeIfUnusedAndNil(GeometryRect); |
| 126 |
FreeAndNil(Scene); |
| 127 |
end; |
| 128 |
|
| 129 |
var |
| 130 |
ChangeableMenu: TMenu; |
| 131 |
|
| 132 |
procedure MenuClick(Container: TCastleContainer; Item: TMenuItem); |
| 133 |
|
| 134 |
procedure ChangeChecked(Item: TMenuItemRadio); |
| 135 |
begin |
| 136 |
Item.Checked := MessageYesNo(Window, 'Should menu item "' + |
| 137 |
SRemoveMnemonics(Item.Caption) + '" be checked ?'); |
| 138 |
if Item.Checked then |
| 139 |
case Item.IntData of |
| 140 |
25: begin Translation.X := -0.5; Transform.Translation := Translation; end; |
| 141 |
26: begin Translation.X := 0.0; Transform.Translation := Translation; end; |
| 142 |
27: begin Translation.X := 0.5; Transform.Translation := Translation; end; |
| 143 |
end; |
| 144 |
end; |
| 145 |
|
| 146 |
var |
| 147 |
M: TMenu; |
| 148 |
begin |
| 149 |
WritelnLog('You clicked menu item "%s" with id %d', [ |
| 150 |
SRemoveMnemonics(Item.Caption), |
| 151 |
Item.SmallId |
| 152 |
]); |
| 153 |
case Item.IntData of |
| 154 |
10: begin |
| 155 |
Shape.FdGeometry.Value := GeometryRect; |
| 156 |
Shape.FdGeometry.Changed; |
| 157 |
end; |
| 158 |
11: begin |
| 159 |
Shape.FdGeometry.Value := GeometryTriangle; |
| 160 |
Shape.FdGeometry.Changed; |
| 161 |
end; |
| 162 |
20: Window.Close; |
| 163 |
|
| 164 |
21: begin Translation.Y := 0.5; Transform.Translation := Translation; end; |
| 165 |
22: begin Translation.Y := 0; Transform.Translation := Translation; end; |
| 166 |
23: begin Translation.Y := -0.5; Transform.Translation := Translation; end; |
| 167 |
|
| 168 |
25..27: ChangeChecked(Item as TMenuItemRadio); |
| 169 |
|
| 170 |
31: begin |
| 171 |
Filled := not Filled; |
| 172 |
if Filled then |
| 173 |
Scene.RenderOptions.WireframeEffect := weNormal |
| 174 |
else |
| 175 |
Scene.RenderOptions.WireframeEffect := weWireframeOnly; |
| 176 |
end; |
| 177 |
40: Window.MainMenu.Append(TMenuItem.Create('New item', -1)); |
| 178 |
41: begin |
| 179 |
M := TMenu.Create('New submenu'); |
| 180 |
M.Append(TMenuItem.Create('_One', -1)); |
| 181 |
M.Append(TMenuItem.Create('_Two', -1)); |
| 182 |
ChangeableMenu.Append(M); |
| 183 |
end; |
| 184 |
42: ChangeableMenu.Append(TMenuItem.Create('New item', -1)); |
| 185 |
|
| 186 |
100: Window.MainMenu := AlternativeMainMenu; |
| 187 |
101: Window.MainMenu := MainMenu; |
| 188 |
|
| 189 |
200.. 200 + High(Colors): Material.DiffuseColor := Colors[Item.IntData - 200]; |
| 190 |
|
| 191 |
else Exit; |
| 192 |
end; |
| 193 |
Window.Invalidate; |
| 194 |
end; |
| 195 |
|
| 196 |
var |
| 197 |
M, M2: TMenu; |
| 198 |
{ Helper variables for setting up radio items } |
| 199 |
Radio: TMenuItemRadio; |
| 200 |
RadioGroup: TMenuItemRadioGroup; |
| 201 |
Viewport: TCastleViewport; |
| 202 |
begin |
| 203 |
InitializeLog; |
| 204 |
|
| 205 |
Window := TCastleWindow.Create(Application); |
| 206 |
Application.MainWindow := Window; // makes MessageYesNo work OK |
| 207 |
|
| 208 |
Viewport := TCastleViewport.Create(Application); |
| 209 |
Viewport.FullSize := true; |
| 210 |
Window.Controls.InsertFront(Viewport); |
| 211 |
|
| 212 |
CreateScene; |
| 213 |
|
| 214 |
Viewport.Items.Add(Scene); |
| 215 |
|
| 216 |
// headlight |
| 217 |
Viewport.Camera.Add(TCastleDirectionalLight.Create(Application)); |
| 218 |
|
| 219 |
// good position to see default shape |
| 220 |
Viewport.AssignDefaultCamera; |
| 221 |
|
| 222 |
{ create menu } |
| 223 |
MainMenu := TMenu.Create('Main menu'); |
| 224 |
M := TMenu.Create('_File'); |
| 225 |
M.Append(TMenuItem.Create('Change to alternative main menu', 100)); |
| 226 |
M.Append(TMenuItem.Create('_Exit', 20)); |
| 227 |
MainMenu.Append(M); |
| 228 |
M := TMenu.Create('_Color'); |
| 229 |
M.AppendRadioGroup([ |
| 230 |
'_Red', |
| 231 |
'_Green', |
| 232 |
'_Blue', |
| 233 |
'_Yellow', |
| 234 |
'_White', |
| 235 |
'Gr_ay', |
| 236 |
'B_lack' |
| 237 |
], |
| 238 |
{ BaseIntData: MenuClick should handle Item.IntData values |
| 239 |
from 200 to High(Colors) + 200 } |
| 240 |
200, |
| 241 |
{ SelectedIndex, corresponds to dafault color. } |
| 242 |
0, |
| 243 |
{ AutoCheckToggle: The clicked radio item will automatically |
| 244 |
have Checked := true, other radio items in group will have Checked := false. } |
| 245 |
true, |
| 246 |
{ QuoteCaption: no, because we use _ above for mnemonics. } |
| 247 |
false |
| 248 |
); |
| 249 |
MainMenu.Append(M); |
| 250 |
M := TMenu.Create('_Placement'); |
| 251 |
{ Radio menu items test } |
| 252 |
{ First radio test: simple use AutoCheckToggle := true } |
| 253 |
Radio := TMenuItemRadio.Create('_Top', 21, false, true); |
| 254 |
RadioGroup := Radio.Group; { First: get Group } |
| 255 |
M.Append(Radio); |
| 256 |
Radio := TMenuItemRadio.Create('_Middle', 22, true, true); |
| 257 |
Radio.Group := RadioGroup; |
| 258 |
M.Append(Radio); |
| 259 |
Radio := TMenuItemRadio.Create('_Bottom', 23, false, true); |
| 260 |
Radio.Group := RadioGroup; |
| 261 |
M.Append(Radio); |
| 262 |
{ Second radio test: use without AutoCheckToggle := true, |
| 263 |
we will explicitly set Checked. } |
| 264 |
M.Append(TMenuSeparator.Create); |
| 265 |
MenuHorizLeft := TMenuItemRadio.Create('_Left', 25, false, false); |
| 266 |
RadioGroup := MenuHorizLeft.Group; |
| 267 |
M.Append(MenuHorizLeft); |
| 268 |
MenuHorizMiddle := TMenuItemRadio.Create('M_iddle', 26, true, false); |
| 269 |
MenuHorizMiddle.Group := RadioGroup; |
| 270 |
M.Append(MenuHorizMiddle); |
| 271 |
MenuHorizRight := TMenuItemRadio.Create('_Right', 27, true, false); |
| 272 |
MenuHorizRight.Group := RadioGroup; |
| 273 |
M.Append(MenuHorizRight); |
| 274 |
MainMenu.Append(M); |
| 275 |
M := TMenu.Create('_Shape'); |
| 276 |
M.Append(TMenuItemChecked.Create('_Filled', 31, Filled, true)); |
| 277 |
M2 := TMenu.Create('_More options'); |
| 278 |
M2.Append(TMenuItem.Create('_foo with underscore : __', 101)); |
| 279 |
M2.Append(TMenuItem.Create('_bar', 102)); |
| 280 |
M.Append(M2); |
| 281 |
M.Append(TMenuItem.Create('_Rectangle', 10, 'r')); |
| 282 |
M.Append(TMenuItem.Create('_Triangle', 11, 't')); |
| 283 |
MainMenu.Append(M); |
| 284 |
M := TMenu.Create('_Change menu'); |
| 285 |
ChangeableMenu := M; |
| 286 |
M.Append(TMenuItem.Create('Create new _main menu item', 40)); |
| 287 |
M.Append(TMenuItem.Create('Create new _submenu here', 41)); |
| 288 |
M.Append(TMenuItem.Create('Create new menu _item here', 42)); |
| 289 |
M.Append(TMenuSeparator.Create); |
| 290 |
MainMenu.Append(M); |
| 291 |
|
| 292 |
AlternativeMainMenu := TMenu.Create('Alternative main menu'); |
| 293 |
M := TMenu.Create('Second menu'); |
| 294 |
M.Append(TMenuItem.Create('Change back to first menu', 101)); |
| 295 |
AlternativeMainMenu.Append(M); |
| 296 |
|
| 297 |
Window.MainMenu := MainMenu; |
| 298 |
{ this allows to free MainMenu and AlternativeMainMenu easier at the end } |
| 299 |
Window.OwnsMainMenu := false; |
| 300 |
|
| 301 |
Window.OnMenuClick := @MenuClick; |
| 302 |
Window.ParseParameters; |
| 303 |
Window.Width := 300; |
| 304 |
Window.Height := 300; |
| 305 |
Window.DepthBits := 0; |
| 306 |
Window.SetDemoOptions(keyF11, CharEscape, true); |
| 307 |
Window.Caption := 'Demo CastleWindow Menu'; |
| 308 |
Window.OpenAndRun; |
| 309 |
|
| 310 |
DestroyScene; |
| 311 |
Window.MainMenu := nil; |
| 312 |
FreeAndNil(MainMenu); |
| 313 |
FreeAndNil(AlternativeMainMenu); |
| 314 |
end. |