Debian Codegraph @

examples/platformer/code/gameviewoptions.pas

raw
1 {
2 Copyright 2021-2021 Andrzej Kilijański, 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 { Simple options user interface, that allows to change sound settings. }
17 unit GameViewOptions;
18
19 interface
20
21 uses Classes,
22 CastleComponentSerialize, CastleUIControls, CastleControls,
23 CastleSoundEngine;
24
25 type
26 { Simple options user interface, that allows to change sound settings. }
27 TViewOptions = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 ButtonMenu: TCastleButton;
32 VolumeGroup: TCastleHorizontalGroup;
33 MusicGroup: TCastleHorizontalGroup;
34 private
35 procedure SetVolume(const Volume: Single);
36 procedure SetMusic(const Music: Single);
37 procedure ClickMenu(Sender: TObject);
38 procedure ClickVolume(Sender: TObject);
39 procedure ClickMusic(Sender: TObject);
40 public
41 constructor Create(AOwner: TComponent); override;
42 procedure Start; override;
43 end;
44
45 var
46 ViewOptions: TViewOptions;
47
48 implementation
49
50 uses CastleApplicationProperties, CastleWindow, CastleConfig,
51 GameViewMenu;
52
53 { TViewMenu ----------------------------------------------------------------- }
54
55 constructor TViewOptions.Create(AOwner: TComponent);
56 begin
57 inherited;
58 DesignUrl := 'castle-data:/gameviewoptions.castle-user-interface';
59 end;
60
61 procedure TViewOptions.Start;
62 var
63 I: Integer;
64 Button: TCastleButton;
65 begin
66 inherited;
67
68 ButtonMenu.OnClick := {$ifdef FPC}@{$endif}ClickMenu;
69
70 for I := 1 to VolumeGroup.ControlsCount - 1 do
71 begin
72 Button := VolumeGroup.Controls[I] as TCastleButton;
73 Button.OnClick := {$ifdef FPC}@{$endif}ClickVolume;
74 end;
75
76 for I := 1 to MusicGroup.ControlsCount - 1 do
77 begin
78 Button := MusicGroup.Controls[I] as TCastleButton;
79 Button.OnClick := {$ifdef FPC}@{$endif}ClickMusic;
80 end;
81
82 SetVolume(UserConfig.GetFloat('volume', 1.0));
83 SetMusic(UserConfig.GetFloat('music', 1.0));
84 end;
85
86 procedure TViewOptions.SetVolume(const Volume: Single);
87 var
88 I: Integer;
89 MaxButtonIndex: Integer;
90 Button: TCastleButton;
91 begin
92 SoundEngine.Volume := Volume;
93 UserConfig.SetFloat('volume', Volume);
94 UserConfig.Save;
95
96 MaxButtonIndex := Round(Volume * 10) + 1;
97
98 for I := 1 to VolumeGroup.ControlsCount - 1 do
99 begin
100 Button := VolumeGroup.Controls[I] as TCastleButton;
101 if I <= MaxButtonIndex then
102 Button.CustomBackgroundNormal.URL := 'castle-data:/ui/red_circle.png'
103 else
104 Button.CustomBackgroundNormal.URL := 'castle-data:/ui/grey_circle.png';
105 end;
106 end;
107
108 procedure TViewOptions.SetMusic(const Music: Single);
109 var
110 I: Integer;
111 MaxButtonIndex: Integer;
112 Button: TCastleButton;
113 begin
114 SoundEngine.LoopingChannel[0].Volume := Music;
115 UserConfig.SetFloat('music', Music);
116 UserConfig.Save;
117
118 MaxButtonIndex := Round(Music * 10) + 1;
119
120 for I := 1 to MusicGroup.ControlsCount - 1 do
121 begin
122 Button := MusicGroup.Controls[I] as TCastleButton;
123 if I <= MaxButtonIndex then
124 Button.CustomBackgroundNormal.URL := 'castle-data:/ui/red_circle.png'
125 else
126 Button.CustomBackgroundNormal.URL := 'castle-data:/ui/grey_circle.png';
127 end;
128 end;
129
130 procedure TViewOptions.ClickMenu(Sender: TObject);
131 begin
132 Container.View := ViewMenu;
133 end;
134
135 procedure TViewOptions.ClickVolume(Sender: TObject);
136 var
137 ClickedButtonIndex: Integer;
138 begin
139 ClickedButtonIndex := VolumeGroup.IndexOfControl(Sender as TCastleUserInterface);
140
141 SetVolume((ClickedButtonIndex -1) / 10);
142 end;
143
144 procedure TViewOptions.ClickMusic(Sender: TObject);
145 var
146 ClickedButtonIndex: Integer;
147 begin
148 ClickedButtonIndex := MusicGroup.IndexOfControl(Sender as TCastleUserInterface);
149
150 SetMusic((ClickedButtonIndex -1) / 10);
151 end;
152
153 end.