//
// Copyright (c) 2008-2014 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "Audio.h"
#include "Button.h"
#include "Engine.h"
#include "Font.h"
#include "Input.h"
#include "Log.h"
#include "ResourceCache.h"
#include "Scene.h"
#include "Slider.h"
#include "Sound.h"
#include "SoundSource.h"
#include "Text.h"
#include "UI.h"
#include "UIEvents.h"
#include "SoundEffects.h"
#include "DebugNew.h"
// Custom variable identifier for storing sound effect name within the UI element
static const StringHash VAR_SOUNDRESOURCE("SoundResource");
static const unsigned NUM_SOUNDS = 3;
static String soundNames[] = {
"Fist",
"Explosion",
"Power-up"
};
static String soundResourceNames[] = {
"Sounds/PlayerFistHit.wav",
"Sounds/BigExplosion.wav",
"Sounds/Powerup.wav"
};
DEFINE_APPLICATION_MAIN(SoundEffects)
SoundEffects::SoundEffects(Context* context) :
Sample(context)
{
}
void SoundEffects::Start()
{
// Execute base class startup
Sample::Start();
// Enable OS cursor
GetSubsystem()->SetMouseVisible(true);
// Create the user interface
CreateUI();
}
void SoundEffects::CreateUI()
{
// Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
scene_ = new Scene(context_);
UIElement* root = GetSubsystem()->GetRoot();
ResourceCache* cache = GetSubsystem();
XMLFile* uiStyle = cache->GetResource("UI/DefaultStyle.xml");
// Set style to the UI root so that elements will inherit it
root->SetDefaultStyle(uiStyle);
// Create buttons for playing back sounds
for (unsigned i = 0; i < NUM_SOUNDS; ++i)
{
Button* button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]);
// Store the sound effect resource name as a custom variable into the button
button->SetVar(VAR_SOUNDRESOURCE, soundResourceNames[i]);
SubscribeToEvent(button, E_PRESSED, HANDLER(SoundEffects, HandlePlaySound));
}
// Create buttons for playing/stopping music
Button* button = CreateButton(20, 80, 120, 40, "Play Music");
SubscribeToEvent(button, E_RELEASED, HANDLER(SoundEffects, HandlePlayMusic));
button = CreateButton(160, 80, 120, 40, "Stop Music");
SubscribeToEvent(button, E_RELEASED, HANDLER(SoundEffects, HandleStopMusic));
Audio* audio = GetSubsystem