// ViewWnd3D.cpp : implementation file
//

#include "stdafx.h"
#include "MFC Gos.h"
#include "ViewWnd3D.h"

#include <ElementRenderer\ElementRendererHeaders.hpp>
#include <gosFX\gosFX.hpp>
#include <MLR\MLRHeaders.hpp>
#include <MFCPlatform\MFCPlatform.hpp>


extern ElementRenderer::CameraElement *Camera;
extern ElementRenderer::StateChange *Camera_State;
extern bool Window3DOpen;

extern bool gActive;
extern bool gGotFocus;
extern CMFCGosApp theApp;


/////////////////////////////////////////////////////////////////////////////
// ViewWnd3D

IMPLEMENT_DYNCREATE(ViewWnd3D, CFrameWnd)

ViewWnd3D::ViewWnd3D()
{
	GOSActive=GOSInit=false;
}

ViewWnd3D::~ViewWnd3D()
{
}


BEGIN_MESSAGE_MAP(ViewWnd3D, CFrameWnd)
//{{AFX_MSG_MAP(ViewWnd3D)
ON_WM_CLOSE()
ON_WM_CREATE()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL ViewWnd3D::PreTranslateMessage( MSG* pMsg )
{
	if ((pMsg->message == WM_KEYDOWN && pMsg->message == WM_KEYUP) && pMsg->wParam == VK_LMENU)
		{
		return TRUE;	
		}
	return CFrameWnd::PreTranslateMessage(pMsg);
}
/////////////////////////////////////////////////////////////////////////////
// ViewWnd3D message handlers

LRESULT ViewWnd3D::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	
	if(GOSActive)
	{
		switch(message)
		{
					case WM_RBUTTONDOWN:
					case WM_LBUTTONDOWN:
					case WM_MOUSEMOVE:
							theApp.DrawScreen();
					break;
					case WM_KEYDOWN:
						{
								theApp.UpdateCameraFromKeyMessage(message, wParam, lParam);
								theApp.DrawScreen();
						}
					case WM_KEYUP:
						{
						if (GetCapture() == this)
							{
							ReleaseCapture();
							}
						}
		
		case WM_ACTIVATE: 
			gActive = true;
			gGotFocus= true;
			break;		
		case WM_COMMAND:
			{
				DWORD ID;
				ID=LOWORD(wParam);   // menu item or submenu index 
				switch(ID)
				{
				case ID_ALPHA_ONEZEROMODE           :
					{
						Camera_State->SetAlphaMode(MidLevelRenderer::MLRState::OneZeroMode);
					}
					break;
				case ID_ALPHA_ONEONEMODE            :
					{
						Camera_State->SetAlphaMode(MidLevelRenderer::MLRState::OneOneMode);
					}
					break;
				case ID_ALPHA_ALPHAONEMODE          :
					{
						Camera_State->SetAlphaMode(MidLevelRenderer::MLRState::AlphaOneMode);
					}
					break;
				case ID_ALPHA_ONEALPHAMODE          :
					{
						Camera_State->SetAlphaMode(MidLevelRenderer::MLRState::OneAlphaMode);
					}
					break;
				case ID_ALPHA_ALPHAINVALPHAMODE     :
					{
						Camera_State->SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
					}
					break;
				case ID_ALPHA_KEYEDALPHAMODE     :
					{
						Camera_State->SetAlphaMode(MidLevelRenderer::MLRState::KeyedAlphaMode);
					}
					break;
				case ID_BACKFACE_ON                 :
					{
						if (Camera_State->IsBackfaceCullingEnabled())
							Camera_State->DisableBackfaceCulling();
						else
							Camera_State->EnableBackfaceCulling();
					}
					break;
				case ID_ZBUFFER_COMPARE             :
					{
						if (Camera_State->IsZBufferCompareEnabled())
							Camera_State->DisableZBufferCompare();
						else
							Camera_State->EnableZBufferCompare();
					}
					break;
				case ID_DITHERING_ON                :
					{
						if (Camera_State->IsDitheringEnabled())
							Camera_State->DisableDithering();
						else
							Camera_State->EnableDithering();
						break;
					}
					break;
				case ID_FILTER_NONE                 :
					{
						Camera_State->SetFilterMode(MidLevelRenderer::MLRState::NoFilterMode);
					}
					break;
				case ID_FILTER_BILINEAR             :
					{
						Camera_State->SetFilterMode(MidLevelRenderer::MLRState::BiLinearFilterMode);
					}
					break;
				case ID_FILTER_TRILINEAR            :
					{
						Camera_State->SetFilterMode(MidLevelRenderer::MLRState::TriLinearFilterMode);
					}
					break;
					
				case ID_PERSPECTIVE_CORRECT         :
					{
						if (Camera_State->IsPerspectiveCorrectionEnabled())
							Camera_State->DisablePerspectiveCorrection();
						else
							Camera_State->EnablePerspectiveCorrection();
					}
					break;
				case ID_TEXTURE_ON                  :
					{
					}
					break;
				case ID_WIREFRAME_ON                :
					{
						Camera_State->SetWireFrameMode(MidLevelRenderer::MLRState::WireFrameOnlyMode);
					}
					break;
				case ID_WIREFRAME_WIREFRAMETEXTURE   :
					{
						Camera_State->SetWireFrameMode(MidLevelRenderer::MLRState::WireFrameAddMode);
					}
					break;
				case ID_WIREFRAME_TEXTURE            :
					{
						Camera_State->SetWireFrameMode(MidLevelRenderer::MLRState::WireFrameOffMode);
					}
					break;
				case ID_ZBUFFER_WRITE               :
					{
						if (Camera_State->IsZBufferWriteEnabled())
							Camera_State->DisableZBufferWrite();
						else
							Camera_State->EnableZBufferWrite();	
					}
					break;
					
		}
					case WM_CLOSE:
					return CFrameWnd::WindowProc(message, wParam, lParam);
					break;
		
	}
	
}	
UpdateMenu();
return GameOSWinProc(m_hWnd, message, wParam, lParam);
}
return CFrameWnd::WindowProc(message, wParam, lParam);
}
/*

  if(!GOSActive)
  return CFrameWnd::WindowProc(message, wParam, lParam);
  
	while (message!=WM_MOVE && GOSInit==false) 
	return CFrameWnd::WindowProc(message, wParam, lParam);
	
	  if(Camera_State!=NULL)
	  {
	  
		
		  switch(message)
		  {
		  case WM_COMMAND:
		  {
		  DWORD ID;
		  ID=LOWORD(wParam);   // menu item or submenu index 
		  switch(ID)
		  {
		  case ID_ALPHA_ONEZEROMODE           :
		  {
		  Camera_State->SetAlphaMode(ElementRenderer::StateChange::OneZeroMode);
		  }
		  break;
		  case ID_ALPHA_ONEONEMODE            :
		  {
		  Camera_State->SetAlphaMode(ElementRenderer::StateChange::OneOneMode);
		  }
		  break;
		  case ID_ALPHA_ALPHAONEMODE          :
		  {
		  Camera_State->SetAlphaMode(ElementRenderer::StateChange::AlphaOneMode);
		  }
		  break;
		  case ID_ALPHA_ONEALPHAMODE          :
		  {
		  Camera_State->SetAlphaMode(ElementRenderer::StateChange::OneAlphaMode);
		  }
		  break;
		  case ID_ALPHA_ALPHAINVALPHAMODE     :
		  {
		  Camera_State->SetAlphaMode(ElementRenderer::StateChange::AlphaInvAlphaMode);
		  }
		  break;
		  case ID_BACKFACE_ON                 :
		  {
		  if (Camera_State->IsBackfaceCullingEnabled())
		  Camera_State->DisableBackfaceCulling();
		  else
		  Camera_State->EnableBackfaceCulling();
		  }
		  break;
		  case ID_ZBUFFER_COMPARE             :
		  {
		  if (Camera_State->IsZBufferCompareEnabled())
		  Camera_State->DisableZBufferCompare();
		  else
		  Camera_State->EnableZBufferCompare();
		  }
		  break;
		  case ID_DITHERING_ON                :
		  {
		  if (Camera_State->IsDitheringEnabled())
		  Camera_State->DisableDithering();
		  else
		  Camera_State->EnableDithering();
		  break;
		  }
		  break;
		  case ID_FILTER_NONE                 :
		  {
		  Camera_State->SetFilterMode(ElementRenderer::StateChange::NoFilterMode);
		  }
		  break;
		  case ID_FILTER_BILINEAR             :
		  {
		  Camera_State->SetFilterMode(ElementRenderer::StateChange::BiLinearFilterMode);
		  }
		  break;
		  case ID_FILTER_TRILINEAR            :
		  {
		  Camera_State->SetFilterMode(ElementRenderer::StateChange::TriLinearFilterMode);
		  }
		  break;
		  
			case ID_PERSPECTIVE_CORRECT         :
			{
			if (Camera_State->IsPerspectiveCorrectionEnabled())
			Camera_State->DisablePerspectiveCorrection();
			else
			Camera_State->EnablePerspectiveCorrection();
			}
			break;
			case ID_TEXTURE_ON                  :
			{
			}
			break;
			case ID_WIREFRAME_ON                :
			{
			Camera_State->SetWireFrameMode(ElementRenderer::StateChange::WireFrameOnlyMode);
			}
			break;
			case ID_WIREFRAME_WIREFRAMETEXTURE   :
			{
			Camera_State->SetWireFrameMode(ElementRenderer::StateChange::WireFrameAddMode);
			}
			break;
			case ID_WIREFRAME_TEXTURE            :
			{
			Camera_State->SetWireFrameMode(ElementRenderer::StateChange::WireFrameOffMode);
			}
			break;
			case ID_ZBUFFER_WRITE               :
			{
			if (Camera_State->IsZBufferWriteEnabled())
			Camera_State->DisableZBufferWrite();
			else
			Camera_State->EnableZBufferWrite();	
			}
			break;
			
			  
				} //end of Menu cs
				}
				break;
				
				  } //end of CS
				  }// end if camera created
				  UpdateMenu();
				  
					if(message==WM_CLOSE)
					{
					return CFrameWnd::WindowProc(message, wParam, lParam);
					}
					else
					return	GameOSWinProc( 	m_hWnd,message,wParam,lParam );
					}
*/


void ViewWnd3D::UpdateMenu()
{
	if(Camera_State==NULL) return;
	
	CMenu *menu=GetMenu();
	
	
	MLRState mlr_state = Camera_State->GetMLRState();
	
	menu->CheckMenuItem(ID_ALPHA_ONEZEROMODE,Camera_State->GetAlphaMode()==MidLevelRenderer::MLRState::OneZeroMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ALPHA_ONEONEMODE,Camera_State->GetAlphaMode()==MidLevelRenderer::MLRState::OneOneMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ALPHA_ALPHAONEMODE,Camera_State->GetAlphaMode()==MidLevelRenderer::MLRState::AlphaOneMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ALPHA_ONEALPHAMODE,Camera_State->GetAlphaMode()==MidLevelRenderer::MLRState::OneAlphaMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ALPHA_ALPHAINVALPHAMODE,Camera_State->GetAlphaMode()==MidLevelRenderer::MLRState::AlphaInvAlphaMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ALPHA_KEYEDALPHAMODE,Camera_State->GetAlphaMode()==MidLevelRenderer::MLRState::KeyedAlphaMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_BACKFACE_ON,Camera_State->IsBackfaceCullingEnabled()?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ZBUFFER_COMPARE,Camera_State->IsZBufferCompareEnabled()?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_DITHERING_ON,Camera_State->IsDitheringEnabled()?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_FILTER_NONE,Camera_State->GetFilterMode()==MidLevelRenderer::MLRState::NoFilterMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_FILTER_BILINEAR,Camera_State->GetFilterMode()==MidLevelRenderer::MLRState::BiLinearFilterMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_FILTER_TRILINEAR,Camera_State->GetFilterMode()==MidLevelRenderer::MLRState::TriLinearFilterMode?MF_CHECKED:MF_UNCHECKED);
	//	menu->CheckMenuItem(ID_FOG_ON,Camera_State->IsFogEnabled()?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_PERSPECTIVE_CORRECT,Camera_State->IsPerspectiveCorrectionEnabled()?MF_CHECKED:MF_UNCHECKED);
	
	menu->CheckMenuItem(ID_TEXTURE_ON,1?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_WIREFRAME_ON,Camera_State->GetWireFrameMode()==MidLevelRenderer::MLRState::WireFrameOnlyMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_WIREFRAME_WIREFRAMETEXTURE,Camera_State->GetWireFrameMode()==MidLevelRenderer::MLRState::WireFrameAddMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_WIREFRAME_TEXTURE,Camera_State->GetWireFrameMode()==MidLevelRenderer::MLRState::WireFrameOffMode?MF_CHECKED:MF_UNCHECKED);
	menu->CheckMenuItem(ID_ZBUFFER_WRITE,Camera_State->IsZBufferWriteEnabled()?MF_CHECKED:MF_UNCHECKED);
	
	
	
}

void ViewWnd3D::OnClose() 
{
	//	Window3DOpen=false;
	ShowWindow(SW_HIDE);
	//		CFrameWnd::OnClose();
}


int ViewWnd3D::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	Window3DOpen=true;
	return 0;
}

void ViewWnd3D::PreGosCleanUp()
{
	
	Camera_State=NULL;
}

void ViewWnd3D::Fix3DView()
{
	gActive = true;
	gGotFocus= true;
	GameOSWinProc(m_hWnd, WM_MOVE, NULL, NULL);
}

void ViewWnd3D::OnMouseMove(UINT nFlags, CPoint point) 
{
	CFrameWnd::OnMouseMove(nFlags, point);
}

