//	PitchSetEditor.h - Game state used to edit pitch sets.
//  ----------------------------------------------------------------------------
//	This file is part of 'GameTemplate', template code for a game.
//	Copyright (C) 2011  Niall Moody
//	
//	This program is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program.  If not, see <http://www.gnu.org/licenses/>.
//	----------------------------------------------------------------------------

#ifndef PITCHSETEDITOR_H_
#define PITCHSETEDITOR_H_

#include "gui/WidgetContainer.h"
#include "gui/Callbacks.h"
#include "GameState.h"

#include <sstream>

class TonesSounder;
class Button;

///	Game state used to edit pitch sets.
class PitchSetEditor : public GameState,
					   public TriggerCallback
{
  public:
	///	Constructor.
	PitchSetEditor(Application& app, SounderPtr& sound);
	///	Destructor.
	~PitchSetEditor();

	///	Draws the current state.
	void draw(DrawerPtr& d, float timestepInterp);

	///	Called when a key down event is received.
	void keyDown(const std::wstring& action,
				 const std::wstring& keyPressed,
				 int keyModifier);

	///	Called when a mouse down event is received.
	void mouseDown(int button, const TwoFloats& pos);
	///	Called when a mouse up event is received.
	void mouseUp(int button, const TwoFloats& pos);
	///	Called when a mouse move event is received.
	void mouseMove(const TwoFloats& pos);

	///	Called when something is triggered.
	void triggered(Widget *w, bool val, const std::wstring& id);

	///	Returns the name of this state.
	const std::wstring getName() const {return L"PitchSetEditor";};
	///	Used to construct instances of this state.
	static GameStateCreator<PitchSetEditor> creator;
  private:
	///	The number of buttons.
	enum
	{
		NumNotes = 36
	};

	///	Widget to hold all the buttons.
	WidgetContainer buttonContainer;

	///	The Note buttons.
	Button *notes[NumNotes];
	///	The Hi Octave Listen buttons.
	Button *hiListen[NumNotes];
	///	The Med Octave Listen buttons.
	Button *medListen[NumNotes];
	///	The Lo Octave Listen buttons.
	Button *loListen[NumNotes];

	///	Which notes we have selected.
	bool selectedNotes[NumNotes];

	///	Keeps track of a playing synth voice so we know when to noteOff it.
	struct SynthVoice
	{
		///	The SynthSingleton index this voice has.
		int index;
		///	Counter used to determine when to noteOff it.
		int counter;
	};

	///	Any currently playing synth voices.
	std::vector<SynthVoice> synthVoices;

	///	The current filename.
	std::wstringstream filename;
	///	If the user is currently entering the filename.
	bool enteringFilename;

	///	Our copy of the game's Sounder.
	shared_ptr<TonesSounder> sounder;
};

#endif
