Debian Codegraph @

src/window/deprecated_units/castlesoundmenu.pas

raw
1 {
2 Copyright 2006-2022 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 { Menu items (suitable for TCastleOnScreenMenu) to control the TRepoSoundEngine. }
17 unit CastleSoundMenu
18 deprecated 'use TCastleVerticalGroup with buttons, sliders to control volume using UI';
19
20 {$I castleconf.inc}
21
22 interface
23
24 uses Classes,
25 CastleOnScreenMenu, CastleSoundEngine, CastleUIControls,
26 CastleControls;
27
28 type
29 { On-screen menu item that displays sound information.
30 Simply add it to your TCastleOnScreenMenu like
31
32 @longCode(#
33 OnScreenMenu.Add(TSoundInfoMenuItem.Create(OnScreenMenu));
34 #)
35
36 Also be sure to assign Application.MainWindow, it is used
37 as the window from which we show modal dialog box with sound information. }
38 TSoundInfoMenuItem = class(TCastleOnScreenMenuItem)
39 protected
40 procedure DoClick; override;
41 public
42 constructor Create(AOwner: TComponent); override;
43 end;
44
45 { Float slider suitable for volume setting.
46 Range is always [0 .. 1] and when the slider is exactly
47 on 0.0 position it shows "Off". }
48 TMenuVolumeSlider = class(TCastleFloatSlider)
49 function ValueToStr(const AValue: Single): string; override;
50 end;
51
52 { On-screen menu item that allows to control sound volume.
53 Simply add it to your TCastleOnScreenMenu like
54
55 @longCode(#
56 OnScreenMenu.Add(TSoundVolumeMenuItem.Create(OnScreenMenu));
57 #) }
58 TSoundVolumeMenuItem = class(TCastleOnScreenMenuItem)
59 private
60 FSlider: TMenuVolumeSlider;
61 property Slider: TMenuVolumeSlider read FSlider;
62 procedure SliderValueChanged(Sender: TObject);
63 public
64 constructor Create(AOwner: TComponent); override;
65 { Call this if volume changed by something outside of this class. }
66 procedure Refresh;
67 end;
68
69 { On-screen menu item that allows to control music volume.
70 Simply add it to your TCastleOnScreenMenu like
71
72 @longCode(#
73 OnScreenMenu.Add(TMusicVolumeMenuItem.Create(OnScreenMenu));
74 #) }
75 TMusicVolumeMenuItem = class(TCastleOnScreenMenuItem)
76 private
77 FSlider: TMenuVolumeSlider;
78 property Slider: TMenuVolumeSlider read FSlider;
79 procedure SliderValueChanged(Sender: TObject);
80 public
81 constructor Create(AOwner: TComponent); override;
82 { Call this if volume changed by something outside of this class. }
83 procedure Refresh;
84 end;
85
86 implementation
87
88 uses CastleWindow, CastleClassUtils, CastleUtils, CastleMessages;
89
90 { TSoundInfoMenuItem ------------------------------------------------------- }
91
92 constructor TSoundInfoMenuItem.Create(AOwner: TComponent);
93 begin
94 inherited;
95 Caption := 'View sound information';
96 end;
97
98 procedure TSoundInfoMenuItem.DoClick;
99 var
100 S: TStringList;
101 begin
102 inherited;
103 S := TStringList.Create;
104 try
105 S.Append('Sound library (OpenAL) status:');
106 S.Append('');
107 Strings_AddSplittedString(S, SoundEngine.Information, nl);
108
109 if Application.MainWindow = nil then
110 raise EInternalError.Create('You must assign Application.MainWindow to make clicks on TSoundInfoMenuItem work');
111
112 MessageOK(Application.MainWindow, S);
113 finally S.Free end;
114 end;
115
116 { TMenuVolumeSlider ---------------------------------------------------- }
117
118 function TMenuVolumeSlider.ValueToStr(const AValue: Single): string;
119 begin
120 if AValue = 0.0 then
121 Result := 'Off' else
122 Result := inherited ValueToStr(AValue);
123 end;
124
125 { TSoundVolumeMenuItem ----------------------------------------------------- }
126
127 constructor TSoundVolumeMenuItem.Create(AOwner: TComponent);
128 begin
129 inherited;
130 Caption := 'Volume';
131
132 FSlider := TMenuVolumeSlider.Create(Self);
133 FSlider.Value := SoundEngine.Volume;
134 FSlider.OnChange := {$ifdef FPC}@{$endif} SliderValueChanged;
135 InsertFront(FSlider);
136 end;
137
138 procedure TSoundVolumeMenuItem.SliderValueChanged(Sender: TObject);
139 begin
140 SoundEngine.Volume := Slider.Value;
141 end;
142
143 procedure TSoundVolumeMenuItem.Refresh;
144 begin
145 Slider.Value := SoundEngine.Volume;
146 end;
147
148 { TMusicVolumeMenuItem ----------------------------------------------------- }
149
150 constructor TMusicVolumeMenuItem.Create(AOwner: TComponent);
151 begin
152 inherited;
153 Caption := 'Music volume';
154
155 FSlider := TMenuVolumeSlider.Create(Self);
156 FSlider.Value := SoundEngine.LoopingChannel[0].Volume;
157 FSlider.OnChange := {$ifdef FPC}@{$endif} SliderValueChanged;
158 InsertFront(FSlider);
159 end;
160
161 procedure TMusicVolumeMenuItem.SliderValueChanged(Sender: TObject);
162 begin
163 SoundEngine.LoopingChannel[0].Volume := Slider.Value;
164 end;
165
166 procedure TMusicVolumeMenuItem.Refresh;
167 begin
168 Slider.Value := SoundEngine.LoopingChannel[0].Volume;
169 end;
170
171 end.