
//**************************************************************************/
// Copyright (c) 2009 Autodesk, Inc.
// All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
//
//**************************************************************************/
// DESCRIPTION:
// CREATED: August 2009
//**************************************************************************/

#include "FrameCounter.h"

// This macro provides some information about the plugin to Mudbox. It also
// specifies a function that will be called after all the plug-ins are loaded
MB_PLUGIN( "FrameCounter", "Sample plugin counts frames", "Autodesk", "http://www.mudbox3d.com", FrameCounter::Initializer );

CounterNode* FrameCounter::s_pCounter = NULL;

void FrameCounter::Initializer()
// This is called after the plug-ins are all loaded.
//
// Add two menu items, to the Mudbox interface.
{
	// Add two menu items and a sub-menu with three items to the Edit menu
	Kernel()->AddCallbackMenuItem("Plug-ins","","Start Counting Frames",FrameCounter::StartCounting);
	Kernel()->AddCallbackMenuItem("Plug-ins","","Stop Counting Frames",FrameCounter::StopCounting);
}

void FrameCounter::StartCounting()
{
	if ( s_pCounter == NULL )
	{

		// Create a new counter object.  This counter object will attach itself to to 
		// Mudbox's frame callback, so it will receive an event each time a frame is rendered.
		s_pCounter = new CounterNode;
		
		// Display a message on the HUD
		Kernel()->HUDMessageShow("Frame count has started.", Kernel::HUDmsgFade );
	}
}

void FrameCounter::StopCounting()
{
	if ( s_pCounter != NULL )
	{
		// Tell the user how many frames have been displayed
		QString msg(QString("Number of frames rendered: %1").arg(s_pCounter->m_iNumberOfFrames));
		Kernel()->HUDMessageShow( msg, Kernel::HUDmsgFade );
	
		// delete the counter node, since it is no longer required.
		delete s_pCounter;
		s_pCounter = NULL;
	}
}
	
//---------------------------------------


CounterNode::CounterNode()		// constructor
:	m_iNumberOfFrames(0)		// set the counter to 0
,	m_eEachFrame(this)			// eventGate must be initialized with the 'owning' node
{
	// attach the event member of this class to Mudbox's 'frame' event.  That will
	// cause the 'OnEvent' method to get called each time mudbox renders a frame.
	m_eEachFrame.Connect( Kernel()->m_eFrame );
}

void CounterNode::OnEvent( const EventGate &cEvent )
// This method will be called each time an event member is triggered
{
	// if this is the frame event (there could be other event objects in this class)
	if ( cEvent == m_eEachFrame ) 
	{    
		m_iNumberOfFrames++;		  // count the frame
	}
}


	
	








