Show kcursor3d.cpp syntax highlighted
/*
Copyright (C) 2003, 2004, 2005 by Luca Cappa
Written by Luca Cappa groton@users.sourceforge.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cssysdef.h"
#include "csutil/sysfunc.h"
#include "csutil/cscolor.h"
#include "csutil/cmdhelp.h"
#include "csutil/cspmeter.h"
#include "csutil/csstring.h"
#include "csutil/scfstr.h"
#include "csutil/flags.h"
#include "csutil/xmltiny.h"
#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "cstool/collider.h"
#include "csutil/array.h"
#include "csutil/event.h"
#include "iutil/vfs.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iutil/plugin.h"
#include "iutil/string.h"
#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/texture.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"
#include "iengine/material.h"
#include "iengine/rview.h"
#include "imesh/genmesh.h"
#include "imesh/object.h"
#include "imesh/sprite3d.h"
#include "imesh/ball.h"
#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/natwin.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"
#include "igraphic/imageio.h"
#include "imap/loader.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/conout.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/collider.h"
#include "csgeom/quaterni.h"
#include "csgeom/transfrm.h"
#include "csgeom/math3d_d.h"
#include "csgeom/math3d.h"
#include "csgeom/math2d.h"
#include "csgeom/path.h"
#include "csgeom/tri.h"
#include "igeom/polymesh.h"
#include "igeom/objmodel.h"
#include "iaws/aws.h"
#include "iaws/awscnvs.h"
//
#include "ske.h"
#include "klandmark.h"
#include "ksprite3d.h"
#include "kwireframe.h"
#include "kmap.h"
#include "kmousemanager.h"
//Include file for this file
#include "kcursor3d.h"
SCF_IMPLEMENT_IBASE (KCursor3D)
SCF_IMPLEMENTS_INTERFACE (iEventHandler)
SCF_IMPLEMENT_IBASE_END
KCursor3D::KCursor3D (): m_invisible (false)//,
//csMeshObject (g_ske->m_engine)//??
{
SCF_CONSTRUCT_IBASE (0);
//
//Register itself in the event queue.
csRef<iEventQueue> l_eQ = CS_QUERY_REGISTRY (g_objReg, iEventQueue);
CS_ASSERT (l_eQ != 0);
l_eQ->RegisterListener (this, CSMASK_Broadcast | CSMASK_Nothing);
m_loader = CS_QUERY_REGISTRY (g_objReg, iLoader);
m_engine = CS_QUERY_REGISTRY (g_objReg, iEngine);
};
KCursor3D::~KCursor3D ()
{
//The mesh is removed from the engine upon destruction.
m_engine->GetMeshes ()->Remove (m_cursorMW);
}
bool KCursor3D::Initialize ()
{
csRef<iMeshFactoryWrapper> lFact = m_engine->CreateMeshFactory (
"crystalspace.mesh.object.genmesh", "cursor_factory");
csRef<iGeneralFactoryState> lFactState = SCF_QUERY_INTERFACE (
lFact->GetMeshObjectFactory (), iGeneralFactoryState);
lFactState->SetVertexCount (8);
lFactState->GetVertices ()[0].Set (-0.5f, -0.05f, -0.5f);
//??factstate->GetTexels ()[0].Set (0, 0);
lFactState->GetVertices ()[1].Set (-0.5f, 2.2f, -0.5f);
lFactState->GetVertices ()[2].Set (0.5f, 2.2f, -0.5f);
lFactState->GetVertices ()[3].Set (0.5f, -0.05f, -0.5f);
lFactState->GetVertices ()[4].Set (-0.5f, -0.05f, 0.5f);
lFactState->GetVertices ()[5].Set (0.5f, -0.05f, 0.5f);
lFactState->GetVertices ()[6].Set (0.5f, 2.2f, 0.5f);
lFactState->GetVertices ()[7].Set (-0.5f, 2.2f, 0.5f);
//bottom
lFactState->SetTriangleCount (12);
lFactState->GetTriangles ()[0].Set (0, 1, 3);
lFactState->GetTriangles ()[1].Set (1, 2, 3);
lFactState->GetTriangles ()[2].Set (1, 7, 6);
lFactState->GetTriangles ()[3].Set (1, 6, 2);
lFactState->GetTriangles ()[4].Set (4, 7, 1);
lFactState->GetTriangles ()[5].Set (4, 1, 0);
lFactState->GetTriangles ()[6].Set (2, 6, 3);
lFactState->GetTriangles ()[7].Set (6, 5, 3);
lFactState->GetTriangles ()[8].Set (6, 7, 5);
lFactState->GetTriangles ()[9].Set (5, 7, 4);
lFactState->GetTriangles ()[10].Set (0, 5, 4);
lFactState->GetTriangles ()[11].Set (0, 3, 5);
lFactState->SetColor (csColor (0, 1, 0));
lFactState->CalculateNormals ();
// Now create an instance:
m_cursorMW = m_engine->CreateMeshWrapper (lFact, "cursor");
csRef<iGeneralMeshState> meshstate = SCF_QUERY_INTERFACE (
m_cursorMW->GetMeshObject (), iGeneralMeshState);
//meshstate->SetMaterialWrapper (myMaterial);
//Create the wireframe object.
m_wireframe.AttachNew (new KWireframe ("m_wireframe"));
//
Untrack ();
return true;
}
bool KCursor3D::HandleEvent (iEvent& p_e)
{
//
//
if (p_e.Type == csevBroadcast &&
csCommandEventHelper::GetCode (&p_e) == cscmdProcess)
{
//If we are in PUTTING_BLOCK_MODE, show the 3d cursor!
if (g_ske->m_mouseManager->GetMode () == PUTTING_BLOCK_MODE )
{
//
//Cursor position set happens here.
//
//
//Get the intersection of mouse and plane Y=0
csVector3 lInt;
float lDist;
if (!g_ske->CalculateIntersection (g_ske->m_mouseManager->GetLastX (), //??
g_ske->m_mouseManager->GetLastY (), csPlane3 (0,1,0), lInt, lDist))
return true;
//
//Update the position of the cursor.
//display it only if the mouse is not over the GUI and if we are in m_puttingBlockMode,
//and if the intersection is over the ground of out KMap.
csVector2 l_v = g_ske->m_map->GetSize ();
csVector3 l_int = csVector3 (kround (lInt.x), 0,
kround (lInt.z));
if (!g_ske->m_aws->MouseInComponent (g_ske->m_mouseManager->GetLastX (),//??
g_ske->m_mouseManager->GetLastY ()) && //??
(l_int.x >= 0) && (l_int.x < l_v.x) && (l_int.z >= 0) &&
(l_int.z < l_v.y))
{
SetVisible (true);
m_cursorMW->GetMovable ()->SetSector (g_ske->m_world);//??
m_cursorMW->GetMovable ()->SetPosition (l_int);
m_cursorMW->GetMovable ()->UpdateMove ();
}//if
else
{
SetVisible (false);
}//else
}//if
return true;
}
return false;
}
bool KCursor3D::Track (csRefArray<iMeshWrapper> p_mWL, bool p_trackedVisible)
{
m_wireframe->SetTracked (p_mWL, p_trackedVisible, false);
return true;
}
bool KCursor3D::Untrack (bool p_inv)
{
csRefArray<iMeshWrapper> l_mWL;
l_mWL.Push (m_cursorMW);
m_wireframe->SetTracked (l_mWL , false);
return true;
}
void KCursor3D::SetVisible (bool p_visible)
{
m_wireframe->SetVisible (p_visible);
}
bool KCursor3D::GetVisile ()
{
return m_wireframe->GetVisible ();
}
See more files for this project here