void Marker3D::CreateMapObject(CMapView2D *pView)
{
	CMapWorld *pWorld = m_pDocument->GetMapWorld();
	CMapClass *pobj = NULL;

	//
	// Handle prefab creation.
	//
	if (GetMainWnd()->m_ObjectBar.IsEntityToolCreatingPrefab())
	{			
		GetHistory()->MarkUndoPosition(m_pDocument->Selection_GetList(), "New Prefab");

		CMapClass *pPrefabObject = GetMainWnd()->m_ObjectBar.BuildPrefabObjectAtPoint(m_vecPos);

		if (pPrefabObject == NULL)
		{
			pView->MessageBox("Unable to load prefab", "Error", MB_OK);
			SetEmpty();
			return;
		}

		m_pDocument->ExpandObjectKeywords(pPrefabObject, pWorld);
		m_pDocument->AddObjectToWorld(pPrefabObject);

		GetHistory()->KeepNew(pPrefabObject);

		pobj = pPrefabObject;
	}
	//
	// Handle entity creation.
	//
	else if (GetMainWnd()->m_ObjectBar.IsEntityToolCreatingEntity())
	{
		GetHistory()->MarkUndoPosition(m_pDocument->Selection_GetList(), "New Entity");
		
		CMapEntity *pEntity = new CMapEntity;
		
		pEntity->SetPlaceholder(TRUE);
		pEntity->SetOrigin(m_vecPos);
		pEntity->SetClass(CObjectBar::GetDefaultEntityClass());

		m_pDocument->AddObjectToWorld(pEntity);
		
		pobj = pEntity;
		
		GetHistory()->KeepNew(pEntity);
	}

	m_pDocument->ToolUpdateViews(CMapView2D::updEraseTool);

	//
	// Select the new object.
	//
	m_pDocument->SelectObject(pobj, CMapDoc::scClear | CMapDoc::scSelect | CMapDoc::scUpdateDisplay);

	//
	// Update all the views.
	//	
	UpdateBox ub;
	CMapObjectList ObjectList;
	ObjectList.AddTail(pobj);
	ub.Objects = &ObjectList;

	Vector mins;
	Vector maxs;
	pobj->GetRender2DBox(mins, maxs);
	ub.Box.SetBounds(mins, maxs);

	m_pDocument->UpdateAllViews(NULL, MAPVIEW_UPDATE_OBJECTS, &ub);

	SetEmpty();

	m_pDocument->SetModifiedFlag();
}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pView - 
//			nFlags - 
//			point - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool Marker3D::OnLMouseDown3D(CMapView3D *pView, UINT nFlags, CPoint point)
{
	ULONG ulFace;
	CMapClass *pObject = pView->NearestObjectAt(point, ulFace);
	if (pObject != NULL)
	{
		CMapSolid *pSolid = dynamic_cast <CMapSolid *> (pObject);
		if (pSolid == NULL)
		{
			// Clicked on a point entity - do nothing.
			return true;
		}

		CMapDoc *pDoc = pView->GetDocument();

		//
		// Build a ray to trace against the face that they clicked on to
		// find the point of intersection.
		//			
		Vector Start;
		Vector End;
		pView->BuildRay(point, Start, End);

		Vector HitPos, HitNormal;
		CMapFace *pFace = pSolid->GetFace(ulFace);
		if (pFace->TraceLine(HitPos, HitNormal, Start, End))
		{
			if (GetMainWnd()->m_ObjectBar.IsEntityToolCreatingPrefab())
			{
				//
				// Prefab creation.
				//
				pDoc->Snap(HitPos);

				GetHistory()->MarkUndoPosition(pDoc->Selection_GetList(), "New Prefab");

				// Get prefab object
				CMapClass *pPrefabObject = GetMainWnd()->m_ObjectBar.BuildPrefabObjectAtPoint(HitPos);

				//
				// Add prefab to the world.
				//
				CMapWorld *pWorld = pDoc->GetMapWorld();
				pDoc->ExpandObjectKeywords(pPrefabObject, pWorld);
				pDoc->AddObjectToWorld(pPrefabObject);
				GetHistory()->KeepNew(pPrefabObject);

				//
				// Select the new object.
				//
				pDoc->SelectObject(pPrefabObject, CMapDoc::scClear | CMapDoc::scSelect | CMapDoc::scUpdateDisplay);

				//
				// Update world bounds.
				//
				UpdateBox ub;
				CMapObjectList ObjectList;
				ObjectList.AddTail(pPrefabObject);
				ub.Objects = &ObjectList;

				Vector mins;
				Vector maxs;
				pPrefabObject->GetRender2DBox(mins, maxs);
				ub.Box.SetBounds(mins, maxs);

				pDoc->UpdateAllViews(NULL, MAPVIEW_UPDATE_OBJECTS, &ub);
				
				pDoc->SetModifiedFlag();
			}
			else if (GetMainWnd()->m_ObjectBar.IsEntityToolCreatingEntity())
			{
				//
				// Entity creation.
				//
				GetHistory()->MarkUndoPosition(pDoc->Selection_GetList(), "New Entity");
				
				CMapEntity *pEntity = new CMapEntity;
				pEntity->SetPlaceholder(TRUE);
				pEntity->SetOrigin(HitPos);
				pEntity->SetClass(CObjectBar::GetDefaultEntityClass());
				
				//Align the entity on the plane properly
				//				pEntity->AlignOnPlane(HitPos, &pFace->plane, (pFace->plane.normal[2] > 0.0f) ? CMapEntity::ALIGN_BOTTOM : CMapEntity::ALIGN_TOP);
				pEntity->AlignOnPlane(HitPos, &pFace->plane, (HitNormal[2] > 0.0f) ? CMapEntity::ALIGN_BOTTOM : CMapEntity::ALIGN_TOP);
									
				CMapWorld *pWorld = pDoc->GetMapWorld();
				pDoc->AddObjectToWorld(pEntity);
				
				GetHistory()->KeepNew(pEntity);

				//
				// Select the new object.
				//
				pDoc->SelectObject(pEntity, CMapDoc::scClear | CMapDoc::scSelect | CMapDoc::scUpdateDisplay);
				
 				UpdateBox ub;
				CMapObjectList ObjectList;
				ObjectList.AddTail(pEntity);
				ub.Objects = &ObjectList;

				Vector mins;
				Vector maxs;
				pEntity->GetRender2DBox(mins, maxs);
				ub.Box.SetBounds(mins, maxs);

				pDoc->UpdateAllViews(NULL, MAPVIEW_UPDATE_OBJECTS, &ub);
			
				pDoc->SetModifiedFlag();
			}
		}
	}

	return true;
}