| 1 |
#include "SoundShader.h" |
| 2 |
|
| 3 |
#include "parser/DefTokeniser.h" |
| 4 |
#include "string/convert.h" |
| 5 |
#include <algorithm> |
| 6 |
#include "string/predicate.h" |
| 7 |
|
| 8 |
namespace sound |
| 9 |
{ |
| 10 |
|
| 11 |
struct SoundShader::ParsedContents |
| 12 |
{ |
| 13 |
// List of sound files |
| 14 |
SoundFileList soundFiles; |
| 15 |
|
| 16 |
// min and max radii of the shader |
| 17 |
SoundRadii soundRadii; |
| 18 |
|
| 19 |
// display folder including slashes for sorting the sounds in the sound chooser window |
| 20 |
std::string displayFolder; |
| 21 |
}; |
| 22 |
|
| 23 |
SoundShader::SoundShader(const std::string& name) |
| 24 |
: DeclarationBase<ISoundShader>(decl::Type::SoundShader, name) |
| 25 |
{} |
| 26 |
|
| 27 |
// Destructor must be defined with ParsedContents definition visible, otherwise |
| 28 |
// the scoped_ptr destructor will not compile |
| 29 |
SoundShader::~SoundShader() |
| 30 |
{ } |
| 31 |
|
| 32 |
void SoundShader::onBeginParsing() |
| 33 |
{ |
| 34 |
_contents = std::make_unique<ParsedContents>(); |
| 35 |
} |
| 36 |
|
| 37 |
void SoundShader::parseFromTokens(parser::DefTokeniser& tokeniser) |
| 38 |
{ |
| 39 |
while (tokeniser.hasMoreTokens()) |
| 40 |
{ |
| 41 |
// Get the next token |
| 42 |
auto nextToken = tokeniser.nextToken(); |
| 43 |
|
| 44 |
// Watch out for sound file definitions |
| 45 |
// Check if the token starts with soundSLASH or soundBACKSLASH |
| 46 |
if (string::starts_with(nextToken, "sound") && |
| 47 |
nextToken.length() > 5 && (nextToken[5] == '\\' || nextToken[5] == '/')) |
| 48 |
{ |
| 49 |
// Add this to the list, replace backslashes with forward ones |
| 50 |
std::replace(nextToken.begin(), nextToken.end(), '\\', '/'); |
| 51 |
|
| 52 |
_contents->soundFiles.push_back(nextToken); |
| 53 |
} |
| 54 |
else if (nextToken == "minDistance") |
| 55 |
{ |
| 56 |
// Set the radius and convert to metres |
| 57 |
_contents->soundRadii.setMin(string::convert<float>(tokeniser.nextToken()), true); |
| 58 |
} |
| 59 |
else if (nextToken == "maxDistance") |
| 60 |
{ |
| 61 |
// Set the radius and convert to metres |
| 62 |
_contents->soundRadii.setMax(string::convert<float>(tokeniser.nextToken()), true); |
| 63 |
} |
| 64 |
else if (nextToken == "editor_displayFolder") |
| 65 |
{ |
| 66 |
// Set the display folder |
| 67 |
_contents->displayFolder = tokeniser.nextToken(); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
SoundRadii SoundShader::getRadii() |
| 73 |
{ |
| 74 |
ensureParsed(); |
| 75 |
return _contents->soundRadii; |
| 76 |
} |
| 77 |
|
| 78 |
SoundFileList SoundShader::getSoundFileList() |
| 79 |
{ |
| 80 |
ensureParsed(); |
| 81 |
return _contents->soundFiles; |
| 82 |
} |
| 83 |
|
| 84 |
const std::string& SoundShader::getDisplayFolder() |
| 85 |
{ |
| 86 |
ensureParsed(); |
| 87 |
return _contents->displayFolder; |
| 88 |
} |
| 89 |
|
| 90 |
} // namespace |