// madnessEditorFormView.cpp : implementation file
//

#include "stdafx.h"
#include "madnessEditor.h"
#include "madnessEditorDoc.h"
#include "madnessEditorFormView.h"
#include "madnessEditorView.h"

// CmadnessEditorFormView

IMPLEMENT_DYNCREATE(CmadnessEditorFormView, CFormView)

CmadnessEditorFormView::CmadnessEditorFormView()
    : CFormView(CmadnessEditorFormView::IDD)
{


}

CmadnessEditorFormView::~CmadnessEditorFormView()
{
}
void CmadnessEditorFormView::OnInitialUpdate()
{
CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    CFormView::OnInitialUpdate();
    
    for (int i=0; i<(pDoc->Map.numtex);i++)
    {
        Cmb_Tex.InsertString(i,pDoc->Map.Tex[i]);
    
    } 

    pDoc->Map.numtex=Cmb_Tex.GetCount();
    for (int i=0; i<Cmb_Tex.GetCount();i++)
    {
        Cmb_Tex.GetLBText(i,pDoc->Map.Tex[i]);

    }
    /*Slr_AreaGravity.SetRangeMin(0,false);
    Slr_AreaGravity.SetRangeMax(10,false); */
    Slr_AreaGravity.SetRange(-160,160,false);
    Slr_AreaAttrito.SetRange(2,20,false);
    //Slr_AreaArtrito.
    Slr_AreaGravity.SetPos(-160);
    Slr_AreaAttrito.SetPos(7);
    CString buf;
    buf.Format("%d",Slr_AreaGravity.GetPos());
    EdSetG.SetWindowText(buf);
    buf.Format("%d",Slr_AreaAttrito.GetPos());
    EdSetA.SetWindowText(buf);
    buf.Format("*");
    EdTexT.SetWindowText(buf);
    buf.Format("*");
    EdTexW.SetWindowText(buf);
    ResizeParentToFit(false);
}


void CmadnessEditorFormView::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_COMBO2,Cmb_Tex);
    DDX_Control(pDX, IDC_SLIDER1,Slr_AreaAttrito);
    DDX_Control(pDX, IDC_SLIDER2,Slr_AreaGravity);
    DDX_Control(pDX, IDC_EDIT1,EdTexT);
    DDX_Control(pDX, IDC_EDIT2,EdTexW);
    DDX_Control(pDX, IDC_EDIT3,EdSetA);
    DDX_Control(pDX, IDC_EDIT4,EdSetG);
}

BEGIN_MESSAGE_MAP(CmadnessEditorFormView, CFormView)

    ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedApply)
    ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedUpdateTex)
    ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedTexTile)
    ON_BN_CLICKED(IDC_BUTTON4, OnBnClickedTexWall)
    ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)

    ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnNMCustomdrawSlider1)
    ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnNMCustomdrawSlider2)

END_MESSAGE_MAP()


// CmadnessEditorFormView diagnostics

#ifdef _DEBUG
void CmadnessEditorFormView::AssertValid() const
{
    CFormView::AssertValid();
}

void CmadnessEditorFormView::Dump(CDumpContext& dc) const
{
    CFormView::Dump(dc);
}
CmadnessEditorDoc* CmadnessEditorFormView::GetDocument() const // la versione non debug è inline.
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CmadnessEditorDoc)));
    return (CmadnessEditorDoc*)m_pDocument;
}

#endif //_DEBUG


// CmadnessEditorFormView message handlers


void CmadnessEditorFormView::OnBnClickedApply()
{CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
//Slr_AreaArtrito.GetPos()
    for (int j=pDoc->Map.Colored.first.X;j<=pDoc->Map.Colored.last.X;j++) 
        for (int i=pDoc->Map.Colored.first.Z;i<=pDoc->Map.Colored.last.Z;i++) { 
                pDoc->Map.Map[j][i].attr=(double)Slr_AreaAttrito.GetPos();
                pDoc->Map.Map[j][i].grav=(double)Slr_AreaGravity.GetPos();

        }

        pDoc->UpdateAllViews(this);
    // TODO: Add your control notification handler code here
}



void CmadnessEditorFormView::OnBnClickedUpdateTex()
{CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    for (int i=0; i<120;i++) 
        Cmb_Tex.DeleteString(0);



    Cmb_Tex.Dir(DDL_READWRITE,"*.jpg");   

    

    Cmb_Tex.DeleteString(Cmb_Tex.FindString(0,"tile.jpg"));
    Cmb_Tex.DeleteString(Cmb_Tex.FindString(0,"wall.jpg"));
    Cmb_Tex.DeleteString(Cmb_Tex.FindString(0,"enemy.jpg"));
    Cmb_Tex.InsertString(0,"tile.jpg");
    Cmb_Tex.InsertString(1,"wall.jpg");
    Cmb_Tex.InsertString(2,"enemy.jpg");
    pDoc->Map.numtex=Cmb_Tex.GetCount();
    for (int i=0; i<Cmb_Tex.GetCount();i++)
    {
        Cmb_Tex.GetLBText(i,pDoc->Map.Tex[i]);
    
    
    }
    pDoc->Map.InitTexture();
    // TODO: Add your control notification handler code here
        }
void CmadnessEditorFormView::OnBnClickedTexTile()
{CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    for (int j=pDoc->Map.Colored.first.X;j<=pDoc->Map.Colored.last.X;j++) 
        for (int i=pDoc->Map.Colored.first.Z;i<=pDoc->Map.Colored.last.Z;i++) { 
            if (Cmb_Tex.GetCurSel()!=-1) pDoc->Map.Map[j][i].typet=Cmb_Tex.GetCurSel();
        }

        pDoc->UpdateAllViews(this);
    // TODO: Add your control notification handler code here
}

void CmadnessEditorFormView::OnBnClickedTexWall()
{
    CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    for (int j=pDoc->Map.Colored.first.X;j<=pDoc->Map.Colored.last.X;j++) 
        for (int i=pDoc->Map.Colored.first.Z;i<=pDoc->Map.Colored.last.Z;i++) { 
            if (Cmb_Tex.GetCurSel()!=-1) pDoc->Map.Map[j][i].typew=Cmb_Tex.GetCurSel();
        }

        pDoc->UpdateAllViews(this);

    // TODO: Add your control notification handler code here
}

void CmadnessEditorFormView::OnBnClickedButton5()
{
CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
        if (Cmb_Tex.GetCurSel()!=-1) pDoc->Map.TexEnemy=Cmb_Tex.GetCurSel();
        for (int i=0;i<MAXENEMY;i++) pDoc->enemy[i].setTexture(pDoc->Map.Tex[pDoc->Map.TexEnemy]);
        pDoc->UpdateAllViews(this);
    // TODO: Add your control notification handler code here
}


void CmadnessEditorFormView::OnNMCustomdrawSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    // TODO: Add your control notification handler code here
    *pResult = 0;
CString buf;
buf.Format("%d",Slr_AreaAttrito.GetPos());
    EdSetA.SetWindowText(buf);
}



void CmadnessEditorFormView::OnNMCustomdrawSlider2(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    // TODO: Add your control notification handler code here
    *pResult = 0;
        CString buf;
    buf.Format("%d",Slr_AreaGravity.GetPos());
    EdSetG.SetWindowText(buf);



}






void CmadnessEditorFormView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{   

        CmadnessEditorDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    CString buf;
    int g=pDoc->Map.getColoredGravity();
    int a=pDoc->Map.getColoredAttrito();
    int w=pDoc->Map.getColoredTexWall();
    int t=pDoc->Map.getColoredTexTile();
    buf.Format("*");

    if (g!=-1) {
    buf.Format("%d",g);
    Slr_AreaGravity.SetPos(g);
    }   
    EdSetG.SetWindowText(buf);
    buf.Format("*");
    if (a!=-1){
    buf.Format("%d",a);
    Slr_AreaAttrito.SetPos(a);
    }
    EdSetA.SetWindowText(buf);
    buf.Format("*");
    if (w!=-1) {
    buf.Format("%s",pDoc->Map.Tex[w]);
    }
EdTexW.SetWindowText(buf);
        buf.Format("*");
if (t!=-1) {
    buf.Format("%s",pDoc->Map.Tex[t]);
    }
    EdTexT.SetWindowText(buf);


    // TODO: Add your specialized code here and/or call the base class
}