// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\brief Implements a bone primitive for use with skeletons & IK
		\author Timothy M. Shead (tshead@k-3d.com)
		\author Brett W. McCoy (bmccoy@chapelperilous.net
*/

#include <k3dsdk/bounded.h>
#include <k3dsdk/transform.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/module.h>
#include <k3dsdk/transformable.h>
#include <k3dsdk/viewport.h>
#include <sdpgl/sdpgl.h>

namespace
{

class bone_implementation :
	public k3d::viewport::drawable<k3d::bounded<k3d::editor::renderable<k3d::transformable<k3d::persistent<k3d::object> > > > >
{
	typedef k3d::viewport::drawable<k3d::bounded<k3d::editor::renderable<k3d::transformable<k3d::persistent<k3d::object> > > > > base;
		
public:
	bone_implementation(k3d::idocument& Document) :
		base(Document),
		m_length(k3d::init_name("length") + k3d::init_description("Length [number]") + k3d::init_value(5.0) + k3d::init_document(Document) + k3d::init_precision(2) + k3d::init_step_increment(0.1) + k3d::init_units(typeid(k3d::measurement::distance)))

	{
		enable_serialization(k3d::persistence::proxy(m_length));

		register_property(m_length);
		
		m_position.changed_signal().connect(SigC::slot(*this, &bone_implementation::async_redraw_all));
		m_orientation.changed_signal().connect(SigC::slot(*this, &bone_implementation::async_redraw_all));
		m_scale.changed_signal().connect(SigC::slot(*this, &bone_implementation::async_redraw_all));
	}

        const k3d::bounding_box extents()
        {
                const double length = m_length.property_value();
                return k3d::bounding_box(.5, -.5, length, -.5, .5, -.5);
	}

        void viewport_draw()
        {
	  // If we aren't visible, we're done ...
	  if(!AdvancedQuery(k3dIState::VISIBLE))
                        return;

	  sdpgl::trace(__PRETTY_FUNCTION__);

	  const double length = m_length.property_value();
		
	  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	  glDisable(GL_TEXTURE_1D);
	  glDisable(GL_TEXTURE_2D);
	  
	  glDisable(GL_CULL_FACE);

	  glBegin(GL_QUADS);

	  //draw a 4-sided pyramid
	  glColor3d(0, 0, 0);
	  glVertex3d(-.5, 0, 0);
	  glVertex3d(0, 0, .5);
	  glVertex3d(.5, 0, 0);
	  glVertex3d(0, 0, -.5);

	  glVertex3d(-.5, 0, 0);
	  glVertex3d(0, length, 0);

	  glVertex3d(0, 0, .5);
	  glVertex3d(0, length, 0);

	  glVertex3d(.5, 0, 0);
	  glVertex3d(0, length, 0);

	  glVertex3d(0, 0, -.5);
	  glVertex3d(0, length, 0);

	  //draw a little one underneath
	  glVertex3d(-.5, 0, 0);
	  glVertex3d(0, -.5, 0);

	  glVertex3d(0, 0, .5);
	  glVertex3d(0, -.5, 0);

	  glVertex3d(.5, 0, 0);
	  glVertex3d(0, -.5, 0);

	  glVertex3d(0, 0, -.5);
	  glVertex3d(0, -.5, 0);

	  glEnd();
	}

       
        void EditorRender(k3dIEditorEngine* const Engine, const render_state& State)
        {
	  // If we aren't visible, we're done ...
	  if(!AdvancedQuery(k3dIState::VISIBLE))
	    return ;

	  bool selected = (state().QueryState(k3dIState::SELECTED));
	  if(selected)
	    {
	      glPolygonOffset(1.0, 1.0);
	      glEnable(GL_POLYGON_OFFSET_FILL);
	    }
                        
	  draw();

	  if(selected)
	    {
	      const double length = m_length.property_value();

	      glDisable(GL_LIGHTING);

	      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	      glDisable(GL_TEXTURE_1D);
	      glDisable(GL_TEXTURE_2D);
	  
	      glDisable(GL_CULL_FACE);

	      glBegin(GL_QUADS);

	      //draw a 4-sided pyramid
	      glVertex3d(-.5, 0, 0);
	      glVertex3d(0, 0, .5);
	      glVertex3d(.5, 0, 0);
	      glVertex3d(0, 0, -.5);
	      
	      glVertex3d(-.5, 0, 0);
	      glVertex3d(0, length, 0);
	      
	      glVertex3d(0, 0, .5);
	      glVertex3d(0, length, 0);
	      
	      glVertex3d(.5, 0, 0);
	      glVertex3d(0, length, 0);
	      
	      glVertex3d(0, 0, -.5);
	      glVertex3d(0, length, 0);
	      
	      //draw a little one underneath
	      glVertex3d(-.5, 0, 0);
	      glVertex3d(0, -.5, 0);
	      
	      glVertex3d(0, 0, .5);
	      glVertex3d(0, -.5, 0);
	      
	      glVertex3d(.5, 0, 0);
	      glVertex3d(0, -.5, 0);
	      
	      glVertex3d(0, 0, -.5);
	      glVertex3d(0, -.5, 0);

	      glEnd();
	      
	      glDisable(GL_POLYGON_OFFSET_FILL);
	    }
        }

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}
	
	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<k3d::document_plugin<bone_implementation> > factory(
			k3d::uuid(0x54e46452, 0x681f4ed6, 0xb30832d5, 0x06a69295),
			"Bone",
			"Experimental Bone primitive",
			"Objects",
			k3d::iplugin_factory::EXPERIMENTAL);
		
		return factory;
	}

private:

	k3d_measurement_property(double, 
				 k3d::immutable_name, 
				 k3d::change_signal, 
				 k3d::with_undo, 
				 k3d::local_storage, 
				 k3d::no_constraint) m_length;

};

} // namespace

namespace libk3dode
{

//////////////////////////////////////////////////////////////////////////////////////////
// body_factory

k3d::iplugin_factory& bone_factory()
{
	return ::bone_implementation::get_factory();
}

} // namespace libk3dskeleton

