forked from sonosaurus/sonobus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleEditView.h
361 lines (284 loc) · 10.6 KB
/
SampleEditView.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// SPDX-License-Identifier: GPLv3-or-later WITH Appstore-exception
// Copyright (C) 2021 Jesse Chappell
#pragma once
#include <JuceHeader.h>
#include "Soundboard.h"
#include "SonoTextButton.h"
#include "SonoDrawableButton.h"
#include "SonoMultiStateDrawableButton.h"
#include "SoundSampleButtonColourPicker.h"
/**
* Dialog for creating/editing soundboard samples.
* Meant for use in a CallOutBox.
*
* @author Hannah Schellekens
*/
class SampleEditView : public Component
{
public:
/**
* The recommended width of the Sample edit view.
*/
constexpr static const float DEFAULT_VIEW_WIDTH = 350;
/**
* The recommended height of the Sample edit view.
*/
constexpr static const float DEFAULT_VIEW_HEIGHT = 500;
/**
* @param submitcallback Function with the actual selected name that gets called when the submit button is pressed.
* @param gaincallback Function that will get called for gain changes
* @param soundSample The sample that must be edited, or null when a new sample must be created.
* @param lastOpenedDirectoryString Where to store the directory that was last opened using the browse button,
* or nullptr when the last directory should not be stored.
*/
explicit SampleEditView(
std::function<void(SampleEditView&)> submitcallback,
std::function<void(SampleEditView&)> gaincallback,
const SoundSample* soundSample = nullptr,
String* lastOpenedDirectoryString = nullptr
);
/**
* @return The sample name that was entered.
*/
[[nodiscard]] String getSampleName() const;
/**
* @return The absolute file path to the sound file of the sound sample.
*/
[[nodiscard]] String getAbsoluteFilePath() const;
/**
* @return The absolute file path to the sound file of the sound sample.
*/
[[nodiscard]] juce::URL getFileURL() const;
/**
* Whether the dialog is in edit mode.
*
* @return true if the dialog is in edit mode.
*/
[[nodiscard]] bool isEditMode() const { return editModeEnabled; }
void setEditMode(bool flag);
/**
* Whether the dialog is in create mode.
*
* @return true if the dialog is in create mode.
*/
[[nodiscard]] bool isCreateMode() const { return !editModeEnabled; }
/**
* @return true if the sample must be deleted.
*/
[[nodiscard]] bool isDeleteSample() const { return deleteSample; }
/**
* @return The selected button colour without alpha value.
*/
[[nodiscard]] int getButtonColour() const { return selectedColour; }
/**
* @return The selected playback behaviour.
*/
[[nodiscard]] SoundSample::EndPlaybackBehaviour getEndPlaybackBehaviour() const { return static_cast<SoundSample::EndPlaybackBehaviour>(mLoopButton->getState()); }
[[nodiscard]] SoundSample::PlaybackBehaviour getPlaybackBehaviour() const { return static_cast<SoundSample::PlaybackBehaviour>(mPlaybackBehaviourButton->getState()); }
[[nodiscard]] SoundSample::ButtonBehaviour getButtonBehaviour() const { return static_cast<SoundSample::ButtonBehaviour>(mButtonBehaviourButton->getState()); }
[[nodiscard]] SoundSample::ReplayBehaviour getReplayBehaviour() const { return static_cast<SoundSample::ReplayBehaviour>(mReplayBehaviourButton->getState()); }
/**
* @return The selected gain.
*/
[[nodiscard]] float getGain() const;
/**
* @return The key code for the hotkey.
*/
[[nodiscard]] int getHotkeyCode() const { return hotkeyCode; };
bool keyPressed(const KeyPress& key) override;
void paint(Graphics&) override;
void resized() override;
/**
* Function to call whenever the submit button is clicked.
* Parameter is the inputted name.
*/
std::function<void(SampleEditView&)> submitCallback;
/**
* Function to call whenever the gain/volume is changed
*/
std::function<void(SampleEditView&)> gainChangeCallback;
/**
* Function to call whenever the applyPlaybackOptionsToOthers button is pressed
*/
std::function<void(SampleEditView&)> applyPlaybackOptionsToOthersCallback;
int getMinimumContentWidth() const { return minContentWidth; }
int getMinimumContentHeight() const { return minContentHeight; }
private:
constexpr static const float ELEMENT_MARGIN = 4;
constexpr static const float CONTROL_HEIGHT = 24;
/**
* Constant for indicating that a custom colour will be used.
*/
constexpr static const int CUSTOM_COLOUR = -1;
/**
* All the button colours that can be selected (in order).
*/
const std::vector<int64> BUTTON_COLOURS = {
SoundboardButtonColors::DEFAULT_BUTTON_COLOUR,
SoundboardButtonColors::RED,
SoundboardButtonColors::ORANGE,
SoundboardButtonColors::YELLOW,
SoundboardButtonColors::YELLOW_GREEN,
SoundboardButtonColors::GREEN,
SoundboardButtonColors::CYAN,
SoundboardButtonColors::BLUE,
SoundboardButtonColors::PURPLE,
SoundboardButtonColors::PINK,
SoundboardButtonColors::WHITE,
CUSTOM_COLOUR
};
/**
* true if the dialog is in rename mode, or false when the dialog is in create mode.
*/
bool editModeEnabled;
/**
* Whether the sample must be deleted.
*/
bool deleteSample = false;
/**
* The selected button colour without alpha value.
*/
uint32 selectedColour = SoundboardButtonColors::DEFAULT_BUTTON_COLOUR;
/**
* The name that is shown upon opening the dialog.
*/
String initialName = "";
/**
* The file path that is shown upon opening the dialog.
*/
String initialFilePath = "";
/**
* Playback behaviour option that is chosen upon opening the dialog.
*/
SoundSample::PlaybackBehaviour initialPlaybackBehaviour = SoundSample::PlaybackBehaviour::SIMULTANEOUS;
SoundSample::ButtonBehaviour initialButtonBehaviour = SoundSample::ButtonBehaviour::TOGGLE;
SoundSample::ReplayBehaviour initialReplayBehaviour = SoundSample::ReplayBehaviour::REPLAY_FROM_START;
SoundSample::EndPlaybackBehaviour initialEndPlaybackBehaviour = SoundSample::EndPlaybackBehaviour::STOP_AT_END;
/**
* Gain option that is chosen upon opening the dialog.
*/
float initialGain = 1.0;
/**
* The key code of the hotkey for the sample, -1 for no hotkey.
*/
int hotkeyCode = -1;
/**
* The directory that was last opened by the file chooser.
* nullptr when this should not be stored.
*/
String* lastOpenedDirectory = nullptr;
int minContentWidth = 320;
int minContentHeight = 300;
FlexBox buttonBox;
FlexBox colourButtonBox;
FlexBox colourButtonRowTopBox;
FlexBox colourButtonRowBottomBox;
std::unique_ptr<Label> mNameLabel;
std::unique_ptr<TextEditor> mNameInput;
std::unique_ptr<Label> mFilePathLabel;
std::unique_ptr<TextEditor> mFilePathInput;
std::unique_ptr<SonoTextButton> mBrowseFilePathButton;
std::unique_ptr<FileChooser> mFileChooser;
std::unique_ptr<Label> mColourInputLabel;
std::vector<std::unique_ptr<SonoDrawableButton>> mColourButtons;
std::unique_ptr<SoundSampleButtonColourPicker> mColourPicker;
std::unique_ptr<Label> mPlaybackOptionsLabel;
std::unique_ptr<SonoMultiStateDrawableButton> mLoopButton;
std::unique_ptr<Label> mVolumeLabel;
std::unique_ptr<Slider> mVolumeSlider;
std::unique_ptr<SonoMultiStateDrawableButton> mPlaybackBehaviourButton;
std::unique_ptr<SonoMultiStateDrawableButton> mButtonBehaviourButton;
std::unique_ptr<SonoMultiStateDrawableButton> mReplayBehaviourButton;
std::unique_ptr<Label> mHotkeyLabel;
std::unique_ptr<SonoTextButton> mHotkeyButton;
std::unique_ptr<SonoTextButton> mRemoveHotkeyButton;
std::unique_ptr<SonoTextButton> mSubmitButton;
std::unique_ptr<SonoTextButton> mDeleteButton;
std::unique_ptr<SonoTextButton> mApplyPlaybackOptionsToOthersButton;
juce::URL mFileURL;
/**
* Creates the input controls for the sample name.
*/
void createNameInputs();
/**
* Creates the input controls for the file path.
*/
void createFilePathInputs();
/**
* Adds the input controls for the sample button colour.
*/
void createColourInput();
/**
* Adds the input controls for playback option configuration.
*/
void createPlaybackOptionInputs();
/**
* Adds the input controls for the volume settings.
*/
void createVolumeInputs();
/**
* Creates the loop playback option toggle.
*/
void createLoopButton();
/**
* Creates the playback behaviour option toggle.
*/
void createPlaybackBehaviourButton();
void createButtonBehaviourButton();
void createReplayBehaviourButton();
/**
* Adds the input controls for the hotkey configuration.
*/
void createHotkeyInput();
/**
* Creates a new colour pick button.
*
* @param index The ith button (index in BUTTON_COLOURS).
* @return Reference to the created button.
*/
std::unique_ptr<SonoDrawableButton> createColourButton(const int index);
/**
* Updates the colour buttons to show a checkmark if its colour is slected and hide the checkmark if that colour
* has not been selected.
*/
void updateColourButtonCheckmark();
/**
* Creates the button bar elements.
*/
void createButtonBar();
/**
* Lets the user browse for a sound file to select.
*/
void browseFilePath();
/**
* Fills in the sample name field based on the given file name.
*/
void inferSampleName();
/**
* Submit the input.
*/
void submitDialog(bool dismiss=true);
/**
* Closes the dialog.
*/
void dismissDialog();
/**
* Get the index of the colour in BUTTON_COLOURS, when not a standard colour, returns the last index in
* BUTTON_COLOURS.
*
* @param colourWithoutAlpha The colour to look up without alpha value, i.e. only RGB bits, rest 0.
* @return The index of the given colour in BUTTON_COLOURS, or #BUTTON_COLOURS - 1 when it is not a default
* colour (CUSTOM_COLOUR).
*/
unsigned int indexOfColour(int colourWithoutAlpha)
{
auto colourCount = BUTTON_COLOURS.size();
for (int i = 0; i < colourCount; ++i) {
if (BUTTON_COLOURS[i] == colourWithoutAlpha) {
return i;
}
}
return colourCount - 1;
}
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SampleEditView)
};